[offset] add smooth, open, close
This commit is contained in:
parent
06ab0af763
commit
b755cfb2db
4 changed files with 141 additions and 13 deletions
|
|
@ -97,7 +97,12 @@ from .boolean import (
|
|||
difference_convex as difference_convex,
|
||||
union_convex as union_convex,
|
||||
)
|
||||
from .offset import offset as offset
|
||||
from .offset import (
|
||||
offset as offset,
|
||||
smooth as smooth,
|
||||
opening as opening,
|
||||
closing as closing,
|
||||
)
|
||||
from .util import is_inside as is_inside
|
||||
|
||||
__author__ = 'Jan Petykiewicz'
|
||||
|
|
|
|||
108
adze/offset.py
108
adze/offset.py
|
|
@ -8,6 +8,9 @@ def offset(poly: poly_t, delta: float, miter_limit: float = 10.0) -> poly_t:
|
|||
Offset a polyhedron by a distance delta.
|
||||
Positive delta expands the shape, negative delta shrinks (etches) it.
|
||||
|
||||
This implementation uses a "mitered" offset, shifting the planes of the
|
||||
polyhedron and recalculating vertex positions.
|
||||
|
||||
Args:
|
||||
poly: Input polyhedron in Schlegel representation.
|
||||
delta: Offset distance.
|
||||
|
|
@ -15,7 +18,29 @@ def offset(poly: poly_t, delta: float, miter_limit: float = 10.0) -> poly_t:
|
|||
|
||||
Returns:
|
||||
Offsetted polyhedron.
|
||||
|
||||
Note:
|
||||
- **Topology Preservation:** This operation preserves the graph topology.
|
||||
It will not automatically merge components that collide or remove
|
||||
holes that close up.
|
||||
- **Self-Intersection:** Large offsets (especially inward) can cause
|
||||
faces to pass through each other, creating "inside-out" geometry
|
||||
with negative volume.
|
||||
- **Cleanup Recommendation:** For robust results in simulators, it is
|
||||
recommended to use boolean operations to "clean" the result:
|
||||
- **Shrinking:** Use `intersect(original, result)` to ensure the result
|
||||
remains strictly within the original bounds and to remove inverted geometry.
|
||||
- **Expansion:** Use `union(original, result)` to ensure no "gaps" are created
|
||||
between the original and expanded shape due to numerical drift, and
|
||||
`intersect(boundary, result)` to keep the shape within a global domain.
|
||||
- **Smoothing and Gap-Filling:**
|
||||
- **Smoothing:** High-curvature areas can be smoothed by moving vertices
|
||||
toward the average of their three neighbors.
|
||||
- **Gap-Filling:** Small gaps or numerical "noise" can be filled using a
|
||||
"Closing" operation: an expansion followed by a shrinking of the same
|
||||
magnitude (or vice versa for small protrusions).
|
||||
"""
|
||||
|
||||
if poly['vertices'].shape[0] == 0:
|
||||
return poly
|
||||
|
||||
|
|
@ -89,3 +114,86 @@ def offset(poly: poly_t, delta: float, miter_limit: float = 10.0) -> poly_t:
|
|||
'vertices': new_vertices,
|
||||
'neighbors': neighbors.copy()
|
||||
}
|
||||
|
||||
def smooth(poly: poly_t, factor: float = 0.5) -> poly_t:
|
||||
"""
|
||||
Perform Laplacian smoothing on the polyhedron vertices.
|
||||
Each vertex is moved toward the average position of its three neighbors.
|
||||
|
||||
Args:
|
||||
poly: Input polyhedron.
|
||||
factor: Smoothing factor (0.0 to 1.0). 0.0 is no change, 1.0 is full move.
|
||||
|
||||
Returns:
|
||||
Smoothed polyhedron.
|
||||
"""
|
||||
if poly['vertices'].shape[0] == 0:
|
||||
return poly
|
||||
|
||||
vertices = poly['vertices']
|
||||
neighbors = poly['neighbors']
|
||||
|
||||
# Average of 3 neighbors
|
||||
neighbor_verts = vertices[neighbors] # [N x 3 x 3]
|
||||
avg_neighbor_pos = numpy.mean(neighbor_verts, axis=1) # [N x 3]
|
||||
|
||||
new_vertices = vertices + factor * (avg_neighbor_pos - vertices)
|
||||
|
||||
return {
|
||||
'vertices': new_vertices,
|
||||
'neighbors': neighbors.copy()
|
||||
}
|
||||
|
||||
def closing(poly: poly_t, delta: float, cleanup: bool = True) -> poly_t:
|
||||
"""
|
||||
Perform a morphological 'Closing' operation: Expansion followed by Shrinking.
|
||||
Useful for filling small gaps or cracks.
|
||||
|
||||
Args:
|
||||
poly: Input polyhedron.
|
||||
delta: magnitude of offset (must be positive).
|
||||
cleanup: Whether to perform boolean cleanup after expansion.
|
||||
"""
|
||||
if delta <= 0:
|
||||
return poly
|
||||
|
||||
# 1. Expand
|
||||
expanded = offset(poly, delta)
|
||||
if cleanup:
|
||||
from .boolean import union
|
||||
expanded = union(poly, expanded)
|
||||
|
||||
# 2. Shrink
|
||||
result = offset(expanded, -delta)
|
||||
if cleanup:
|
||||
from .boolean import intersect
|
||||
result = intersect(expanded, result)
|
||||
|
||||
return result
|
||||
|
||||
def opening(poly: poly_t, delta: float, cleanup: bool = True) -> poly_t:
|
||||
"""
|
||||
Perform a morphological 'Opening' operation: Shrinking followed by Expansion.
|
||||
Useful for removing small protrusions or 'noise'.
|
||||
|
||||
Args:
|
||||
poly: Input polyhedron.
|
||||
delta: magnitude of offset (must be positive).
|
||||
cleanup: Whether to perform boolean cleanup after shrinking.
|
||||
"""
|
||||
if delta <= 0:
|
||||
return poly
|
||||
|
||||
# 1. Shrink
|
||||
shrunk = offset(poly, -delta)
|
||||
if cleanup:
|
||||
from .boolean import intersect
|
||||
shrunk = intersect(poly, shrunk)
|
||||
|
||||
# 2. Expand
|
||||
result = offset(shrunk, delta)
|
||||
if cleanup:
|
||||
from .boolean import union
|
||||
result = union(shrunk, result)
|
||||
|
||||
return result
|
||||
|
|
|
|||
|
|
@ -1,22 +1,17 @@
|
|||
import numpy
|
||||
import adze
|
||||
from adze.shapes import build_box
|
||||
from adze.offset import offset
|
||||
from adze.offset import offset, smooth, closing
|
||||
|
||||
def test_offset_cube_expand() -> None:
|
||||
# Unit cube: [0,1]x[0,1]x[0,1], vol=1
|
||||
cube = build_box([[0, 0, 0], [1, 1, 1]])
|
||||
|
||||
# Expand by 0.1 on all sides
|
||||
# New cube: [-0.1, 1.1] on all axes
|
||||
# New side length = 1.2
|
||||
# Expected volume = 1.2^3 = 1.728
|
||||
expanded = offset(cube, 0.1)
|
||||
|
||||
vol = adze.get_volume(expanded)
|
||||
assert numpy.isclose(vol, 1.728)
|
||||
|
||||
# Check bounds
|
||||
vmin, vmax = adze.util.get_aabb(expanded)
|
||||
assert numpy.allclose(vmin, [-0.1, -0.1, -0.1])
|
||||
assert numpy.allclose(vmax, [1.1, 1.1, 1.1])
|
||||
|
|
@ -25,16 +20,11 @@ def test_offset_cube_shrink() -> None:
|
|||
# Unit cube: [0,1]x[0,1]x[0,1], vol=1
|
||||
cube = build_box([[0, 0, 0], [1, 1, 1]])
|
||||
|
||||
# Shrink by 0.1 on all sides
|
||||
# New cube: [0.1, 0.9] on all axes
|
||||
# New side length = 0.8
|
||||
# Expected volume = 0.8^3 = 0.512
|
||||
shrunk = offset(cube, -0.1)
|
||||
|
||||
vol = adze.get_volume(shrunk)
|
||||
assert numpy.isclose(vol, 0.512)
|
||||
|
||||
# Check bounds
|
||||
vmin, vmax = adze.util.get_aabb(shrunk)
|
||||
assert numpy.allclose(vmin, [0.1, 0.1, 0.1])
|
||||
assert numpy.allclose(vmax, [0.9, 0.9, 0.9])
|
||||
|
|
@ -44,3 +34,27 @@ def test_offset_zero() -> None:
|
|||
same = offset(cube, 0.0)
|
||||
assert numpy.isclose(adze.get_volume(same), 1.0)
|
||||
assert numpy.allclose(cube['vertices'], same['vertices'])
|
||||
|
||||
def test_smooth_cube() -> None:
|
||||
# Smoothing a cube should move vertices toward the center
|
||||
cube = build_box([[0, 0, 0], [1, 1, 1]])
|
||||
# Centroid is [0.5, 0.5, 0.5]
|
||||
|
||||
smoothed = smooth(cube, factor=0.1)
|
||||
|
||||
# Check that volume decreased (cube is rounding in)
|
||||
assert adze.get_volume(smoothed) < adze.get_volume(cube)
|
||||
|
||||
# Check one vertex: [0,0,0]
|
||||
# Neighbors of [0,0,0] are [1,0,0], [0,1,0], [0,0,1] (for a unit box)
|
||||
# Average of neighbors = [1/3, 1/3, 1/3]
|
||||
# New V = [0,0,0] + 0.1 * ([1/3, 1/3, 1/3] - [0,0,0]) = [1/30, 1/30, 1/30]
|
||||
assert numpy.allclose(smoothed['vertices'][0], [1/30, 1/30, 1/30])
|
||||
|
||||
def test_closing_cube() -> None:
|
||||
# Closing a cube shouldn't change it much if delta is small and cube is simple
|
||||
cube = build_box([[0, 0, 0], [1, 1, 1]])
|
||||
closed = closing(cube, 0.1, cleanup=True)
|
||||
|
||||
# For a perfect cube, offset(0.1) then offset(-0.1) should return to original
|
||||
assert numpy.isclose(adze.get_volume(closed), 1.0, atol=1e-7)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
from typing import cast, Sequence
|
||||
from typing import cast
|
||||
from collections.abc import Sequence
|
||||
import random
|
||||
import numpy
|
||||
from numpy.typing import NDArray
|
||||
|
|
|
|||
Loading…
Reference in a new issue