Skip to content

Commit

Permalink
Add direct imports, fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
FranzBangar committed Jul 11, 2024
1 parent f0ad33b commit 292f5bb
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 26 deletions.
38 changes: 20 additions & 18 deletions examples/shape/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@

class RoundSquare(cb.MappedSketch):
quads = [
(20, 0, 1, 12),
(12, 1, 2, 13),
(13, 2, 3, 21),
(21, 20, 12, 13),
(21, 3, 4, 14),
(14, 4, 5, 15),
(15, 5, 6, 22),
(22, 21, 14, 15),
(22, 6, 7, 16),
(16, 7, 8, 17),
(17, 8, 9, 23),
(23, 22, 16, 17),
(23, 9, 10, 18),
(18, 10, 11, 19),
(19, 11, 0, 20),
(20, 23, 18, 19),
(21, 22, 23, 20),
[20, 0, 1, 12],
[12, 1, 2, 13],
[13, 2, 3, 21],
[21, 20, 12, 13],
[21, 3, 4, 14],
[14, 4, 5, 15],
[15, 5, 6, 22],
[22, 21, 14, 15],
[22, 6, 7, 16],
[16, 7, 8, 17],
[17, 8, 9, 23],
[23, 22, 16, 17],
[23, 9, 10, 18],
[18, 10, 11, 19],
[19, 11, 0, 20],
[20, 23, 18, 19],
[21, 22, 23, 20],
]

def __init__(self, center: PointType, side: float, corner_round: float):
Expand All @@ -55,7 +55,9 @@ def add_edges(self) -> None:


base_1 = RoundSquare([0, 0, 0], 1, 0.5)
# base_1.smooth(5)
smoother = cb.SketchSmoother(base_1)
smoother.smooth()

shape = cb.ExtrudedShape(base_1, 1)

for op in shape.operations:
Expand Down
9 changes: 6 additions & 3 deletions src/classy_blocks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
from .optimize.clamps.free import FreeClamp
from .optimize.clamps.surface import ParametricSurfaceClamp, PlaneClamp
from .optimize.links import LinkBase, RotationLink, TranslationLink
from .optimize.optimizer import MeshOptimizer
from .optimize.optimizer import MeshOptimizer, SketchOptimizer
from .optimize.smoother import MeshSmoother, SketchSmoother

__all__ = [
# Base
Expand Down Expand Up @@ -105,8 +106,10 @@
"LinkBase",
"TranslationLink",
"RotationLink",
# Optimization: optimizer
# Optimization: optimizers and smoothers
"MeshOptimizer",
# Auto-orientation
"SketchOptimizer",
"MeshSmoother",
"SketchSmoother",
"ViewpointReorienter",
]
16 changes: 16 additions & 0 deletions src/classy_blocks/optimize/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from classy_blocks.construct.flat.sketches.mapped import MappedSketch
from classy_blocks.mesh import Mesh
from classy_blocks.optimize.clamps.clamp import ClampBase
from classy_blocks.optimize.clamps.surface import PlaneClamp
from classy_blocks.optimize.grid import GridBase, HexGrid, QuadGrid
from classy_blocks.optimize.iteration import ClampOptimizationData, IterationDriver
from classy_blocks.optimize.links import LinkBase
Expand Down Expand Up @@ -159,3 +160,18 @@ def __init__(self, sketch: MappedSketch, report: bool = True):

def backport(self):
self.sketch.update(self.grid.points)

def auto_optimize(
self, max_iterations: int = 20, tolerance: float = 0.1, method: MinimizationMethodType = "SLSQP"
) -> None:
"""Adds a PlaneClamp to all non-boundary points and optimize the sketch.
To include boundary points (those that can be moved along a line or a curve),
add clamps manually before calling this method."""
normal = self.sketch.normal

for junction in self.grid.junctions:
if not junction.is_boundary:
clamp = PlaneClamp(junction.point, junction.point, normal)
self.release_vertex(clamp)

super().optimize(max_iterations, tolerance, method)
6 changes: 1 addition & 5 deletions tests/test_optimize/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import numpy as np
from parameterized import parameterized

from classy_blocks.base.exceptions import UndefinedGradingsError
from classy_blocks.construct.flat.sketches.disk import OneCoreDisk
from classy_blocks.construct.flat.sketches.grid import Grid as GridSketch
from classy_blocks.construct.stack import ExtrudedStack
Expand Down Expand Up @@ -43,10 +42,7 @@ def test_junction_internal(self):
mesh = Mesh()
mesh.add(stack)

try:
mesh.assemble() # will fail because there are no chops
except UndefinedGradingsError:
pass
mesh.assemble()

grid = self.get_grid(mesh)

Expand Down
8 changes: 8 additions & 0 deletions tests/test_optimize/test_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,11 @@ def test_optimize_manual(self):
optimizer.optimize(method="L-BFGS-B")

np.testing.assert_almost_equal(sketch.positions[4], [1, 1, 0], decimal=3)

def test_optimize_auto(self):
sketch = MappedSketch(self.positions, self.quads)

optimizer = SketchOptimizer(sketch)
optimizer.auto_optimize(method="L-BFGS-B")

np.testing.assert_almost_equal(sketch.positions[4], [1, 1, 0], decimal=3)

0 comments on commit 292f5bb

Please sign in to comment.