Skip to content

Commit

Permalink
Add MappedSketch smoothing
Browse files Browse the repository at this point in the history
  • Loading branch information
FranzBangar committed Jul 10, 2024
1 parent 02aeeef commit b0d6cab
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 217 deletions.
102 changes: 0 additions & 102 deletions src/classy_blocks/construct/flat/map.py

This file was deleted.

19 changes: 13 additions & 6 deletions src/classy_blocks/construct/flat/sketches/mapped.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

from classy_blocks.construct.flat.face import Face
from classy_blocks.construct.flat.sketch import Sketch
from classy_blocks.types import IndexType, NPPointType, PointListType
from classy_blocks.types import IndexType, NPPointListType, NPPointType, PointListType
from classy_blocks.util import functions as f


class MappedSketch(Sketch):
Expand All @@ -13,8 +14,9 @@ class MappedSketch(Sketch):

def __init__(self, positions: PointListType, quads: List[IndexType]):
self._faces: List[Face] = []
self.indexes = quads

for quad in quads:
for quad in self.indexes:
face = Face([positions[iq] for iq in quad])
self._faces.append(face)

Expand All @@ -39,7 +41,12 @@ def center(self) -> NPPointType:
"""Center of this sketch"""
return np.average([face.center for face in self.faces], axis=0)

# def smooth(self, n_iter: int = 5) -> None:
# """Smooth the internal points using laplacian smoothing"""
# for _ in range(n_iter):
# self.quad_map.smooth_laplacian()
@property
def positions(self) -> NPPointListType:
"""Reconstructs positions back from faces so they are always up-to-date,
even after transforms"""
indexes = list(np.array(self.indexes).flatten())
max_index = max(indexes)
all_points = f.flatten_2d_list([face.point_array.tolist() for face in self.faces])

return np.array([all_points[indexes.index(i)] for i in range(max_index + 1)])
8 changes: 4 additions & 4 deletions src/classy_blocks/optimize/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from classy_blocks.optimize.connection import CellConnection
from classy_blocks.types import IndexType, NPPointListType, NPPointType, OrientType
from classy_blocks.util import functions as f
from classy_blocks.util.constants import EDGE_PAIRS, FACE_MAP, VSMALL
from classy_blocks.util.constants import EDGE_PAIRS, VSMALL


class NoCommonSidesError(Exception):
Expand Down Expand Up @@ -48,14 +48,14 @@ def get_common_side(self, candidate: "CellBase") -> OrientType:
"""Returns orient of this cell that is shared with candidate"""
common_vertices = self.get_common_indexes(candidate)

if len(common_vertices) != 4:
if len(common_vertices) != len(self.side_indexes[0]):
raise NoCommonSidesError

corners = {self.get_corner(i) for i in common_vertices}

for orient, indexes in FACE_MAP.items():
for i, indexes in enumerate(self.side_indexes):
if set(indexes) == corners:
return orient
return self.side_names[i]

raise NoCommonSidesError

Expand Down
6 changes: 6 additions & 0 deletions src/classy_blocks/optimize/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np

from classy_blocks.construct.flat.sketches.mapped import MappedSketch
from classy_blocks.mesh import Mesh
from classy_blocks.optimize.cell import CellBase, HexCell, QuadCell
from classy_blocks.optimize.clamps.clamp import ClampBase
Expand Down Expand Up @@ -108,6 +109,11 @@ def quality(self) -> float:
class QuadGrid(GridBase):
cell_class = QuadCell

@classmethod
def from_sketch(cls, sketch: MappedSketch) -> "QuadGrid":
# TODO: make grids from ANY sketch
return QuadGrid(sketch.positions, sketch.indexes)


class HexGrid(GridBase):
cell_class = HexCell
Expand Down
26 changes: 23 additions & 3 deletions src/classy_blocks/optimize/smoother.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import numpy as np

from classy_blocks.construct.flat.sketches.mapped import MappedSketch
from classy_blocks.mesh import Mesh
from classy_blocks.optimize.grid import Grid
from classy_blocks.optimize.grid import GridBase, HexGrid, QuadGrid
from classy_blocks.optimize.junction import Junction


class SmootherBase:
def __init__(self, grid: Grid):
def __init__(self, grid: GridBase):
self.grid = grid

self.inner: List[Junction] = []
Expand All @@ -30,10 +31,29 @@ class MeshSmoother(SmootherBase):
def __init__(self, mesh: Mesh):
self.mesh = mesh

super().__init__(Grid.from_mesh(self.mesh))
super().__init__(HexGrid.from_mesh(self.mesh))

def smooth(self, iterations: int = 5) -> None:
super().smooth(iterations)

for i, point in enumerate(self.grid.points):
self.mesh.vertices[i].move_to(point)


class SketchSmoother(SmootherBase):
def __init__(self, sketch: MappedSketch):
self.sketch = sketch

grid = QuadGrid.from_sketch(self.sketch)

super().__init__(grid)

def smooth(self, iterations: int = 5) -> None:
super().smooth(iterations)

positions = self.grid.points

for i, quad in enumerate(self.sketch.indexes):
points = np.take(positions, quad, axis=0)

self.sketch.faces[i].update(points)
94 changes: 0 additions & 94 deletions tests/test_construct/test_flat/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import numpy as np

from classy_blocks.construct.flat.map import QuadMap
from classy_blocks.construct.flat.quad import Quad
from classy_blocks.util import functions as f

Expand Down Expand Up @@ -53,96 +52,3 @@ def test_e2(self):

def test_normal(self):
np.testing.assert_almost_equal(self.quad.normal, [0, 0, 1])


class QuadMapTests(unittest.TestCase):
@property
def positions(self):
return np.array(
[
[0, 0, 0],
[1, 0, 0],
[2, 0, 0],
[0, 1, 0],
[1.5, 1.5, 0], # a moved vertex
[2, 1, 0],
[0, 2, 0],
[1, 2, 0],
[2, 2, 0],
]
)

@property
def quads(self):
return [
(0, 1, 4, 3),
(1, 2, 5, 4),
(3, 4, 7, 6),
(4, 5, 8, 7),
]

@property
def quad_map(self):
return QuadMap(self.positions, self.quads)

def test_positions(self):
np.testing.assert_equal(self.quad_map.positions, self.positions)

def test_find_neighbours(self):
# A random blocking (quadding)
positions = np.zeros((9, 3))
indexes = [(1, 2, 7, 6), (2, 3, 4, 7), (7, 4, 5, 6), (0, 1, 6, 8)]

quad_map = QuadMap(positions, indexes)

self.assertDictEqual(
quad_map.neighbours,
{
0: {8, 1},
1: {0, 2, 6},
2: {1, 3, 7},
3: {2, 4},
4: {3, 5, 7},
5: {4, 6},
6: {8, 1, 5, 7},
7: {2, 4, 6},
8: {0, 6},
},
)

def test_fixed_points(self):
# Monocylinder, core is quads[0]
positions = np.zeros((8, 3))
indexes = [
(0, 1, 2, 3),
(0, 4, 5, 1),
(1, 5, 6, 2),
(2, 6, 7, 3),
(3, 7, 4, 0),
]

quad_map = QuadMap(positions, indexes)

fixed_points = quad_map.boundary_points

self.assertSetEqual(
fixed_points,
{4, 5, 6, 7},
)

def test_update(self):
positions = self.positions
positions[0] = [0.5, 0.5, 0]

quad_map = self.quad_map
quad_map.update(positions)

np.testing.assert_equal(quad_map.quads[0].points[0], [0.5, 0.5, 0])

def test_smooth(self):
# a grid of vertices 3x3
quad_map = QuadMap(self.positions, self.quads)
for _ in range(10):
quad_map.smooth_laplacian()

np.testing.assert_almost_equal(quad_map.positions[4], [1, 1, 0], decimal=5)
36 changes: 35 additions & 1 deletion tests/test_optimize/optimize_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,47 @@
import unittest
from typing import get_args

import numpy as np

from classy_blocks.construct.operations.box import Box
from classy_blocks.mesh import Mesh
from classy_blocks.modify.find.geometric import GeometricFinder
from classy_blocks.optimize.grid import Grid
from classy_blocks.optimize.grid import Grid, QuadGrid
from classy_blocks.types import AxisType


class SketchTestsBase(unittest.TestCase):

@property
def positions(self):
return np.array(
[
[0, 0, 0],
[1, 0, 0],
[2, 0, 0],
[0, 1, 0],
[1.5, 1.5, 0], # a moved vertex
[2, 1, 0],
[0, 2, 0],
[1, 2, 0],
[2, 2, 0],
]
)

@property
def quads(self):
return [
[0, 1, 4, 3],
[1, 2, 5, 4],
[3, 4, 7, 6],
[4, 5, 8, 7],
]

@property
def grid(self):
return QuadGrid(self.positions, self.quads)


class BoxTestsBase(unittest.TestCase):
def setUp(self):
self.mesh = Mesh()
Expand Down
Loading

0 comments on commit b0d6cab

Please sign in to comment.