91 lines
3.2 KiB
Python
91 lines
3.2 KiB
Python
import numpy
|
|
import adze
|
|
from adze.shapes import build_box
|
|
from adze.offset import offset, smooth, closing, offset_anisotropic
|
|
|
|
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]])
|
|
|
|
expanded = offset(cube, 0.1)
|
|
|
|
vol = adze.get_volume(expanded)
|
|
assert numpy.isclose(vol, 1.728)
|
|
|
|
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])
|
|
|
|
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]])
|
|
|
|
shrunk = offset(cube, -0.1)
|
|
|
|
vol = adze.get_volume(shrunk)
|
|
assert numpy.isclose(vol, 0.512)
|
|
|
|
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])
|
|
|
|
def test_offset_zero() -> None:
|
|
cube = build_box([[0, 0, 0], [1, 1, 1]])
|
|
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)
|
|
|
|
def test_anisotropic_etch() -> None:
|
|
# Unit cube [0,1]^3
|
|
cube = build_box([[0, 0, 0], [1, 1, 1]])
|
|
|
|
# Define an etch rate function:
|
|
# Fast vertical etch (-0.2), slow horizontal etch (-0.05)
|
|
# Note: Normals are inward facing for adze clipping convention?
|
|
# Wait, planes_from_box in shapes.py returns normals like (0,0,1) for z=0 face.
|
|
# Let's verify normal direction with a quick check if needed, but standard logic:
|
|
# offset uses inward normals. A negative return value from rate_func shrinks.
|
|
|
|
def vertical_etch_rate(normal: numpy.ndarray) -> float:
|
|
# Check if normal is primarily vertical (z-axis)
|
|
if abs(normal[2]) > 0.9:
|
|
return -0.2
|
|
return -0.05
|
|
|
|
etched = offset_anisotropic(cube, vertical_etch_rate)
|
|
|
|
# Expected bounds:
|
|
# Z: [0,1] -> [0.2, 0.8] (shrunk by 0.2 on both top and bottom) -> height 0.6
|
|
# X, Y: [0,1] -> [0.05, 0.95] (shrunk by 0.05 on sides) -> width 0.9
|
|
|
|
vmin, vmax = adze.util.get_aabb(etched)
|
|
|
|
assert numpy.allclose(vmin, [0.05, 0.05, 0.2])
|
|
assert numpy.allclose(vmax, [0.95, 0.95, 0.8])
|
|
|
|
# Volume = 0.9 * 0.9 * 0.6 = 0.486
|
|
assert numpy.isclose(adze.get_volume(etched), 0.486)
|