[tests] Check data validity and add test_difference_hole()

This commit is contained in:
jan 2026-03-07 23:59:32 -08:00
commit 1999b24b0c

View file

@ -2,6 +2,7 @@ import numpy
import adze import adze
from adze.boolean import intersect, difference, union from adze.boolean import intersect, difference, union
from adze.shapes import build_box from adze.shapes import build_box
from adze.util import check_poly
def test_intersect_cubes() -> None: def test_intersect_cubes() -> None:
# Cube 1: (0,0,0) to (2,2,2), volume = 8 # Cube 1: (0,0,0) to (2,2,2), volume = 8
@ -11,6 +12,7 @@ def test_intersect_cubes() -> None:
# Overlap is from (1,1,1) to (2,2,2), volume = 1 # Overlap is from (1,1,1) to (2,2,2), volume = 1
result = intersect(poly_a, poly_b) result = intersect(poly_a, poly_b)
check_poly(result)
volume = adze.get_volume(result) volume = adze.get_volume(result)
assert numpy.isclose(volume, 1.0) assert numpy.isclose(volume, 1.0)
@ -20,6 +22,7 @@ def test_intersect_no_overlap() -> None:
poly_b = build_box([[2, 2, 2], [3, 3, 3]]) poly_b = build_box([[2, 2, 2], [3, 3, 3]])
result = intersect(poly_a, poly_b) result = intersect(poly_a, poly_b)
check_poly(result)
assert result['vertices'].shape[0] == 0 assert result['vertices'].shape[0] == 0
assert adze.get_volume(result) == 0.0 assert adze.get_volume(result) == 0.0
@ -31,6 +34,7 @@ def test_difference_cubes() -> None:
# A - B should be 8 - 1 = 7 # A - B should be 8 - 1 = 7
result = difference(poly_a, poly_b) result = difference(poly_a, poly_b)
check_poly(result)
volume = adze.get_volume(result) volume = adze.get_volume(result)
assert numpy.isclose(volume, 7.0) assert numpy.isclose(volume, 7.0)
@ -42,6 +46,7 @@ def test_union_cubes() -> None:
# A + B should be 8 + 8 - 1 = 15 # A + B should be 8 + 8 - 1 = 15
result = union(poly_a, poly_b) result = union(poly_a, poly_b)
check_poly(result)
volume = adze.get_volume(result) volume = adze.get_volume(result)
assert numpy.isclose(volume, 15.0) assert numpy.isclose(volume, 15.0)
@ -51,6 +56,7 @@ def test_union_no_overlap() -> None:
# A + B should be 1 + 1 = 2 # A + B should be 1 + 1 = 2
result = union(poly_a, poly_b) result = union(poly_a, poly_b)
check_poly(result)
volume = adze.get_volume(result) volume = adze.get_volume(result)
assert numpy.isclose(volume, 2.0) assert numpy.isclose(volume, 2.0)
@ -67,17 +73,20 @@ def test_intersect_nonconvex_lshape() -> None:
# union(b1, b2) will produce a non-convex polyhedron # union(b1, b2) will produce a non-convex polyhedron
l_shape = union(b1, b2) l_shape = union(b1, b2)
check_poly(l_shape)
assert numpy.isclose(adze.get_volume(l_shape), 3.0) assert numpy.isclose(adze.get_volume(l_shape), 3.0)
b3 = build_box([[0.5, 0.5, 0], [1.5, 1.5, 1]]) b3 = build_box([[0.5, 0.5, 0], [1.5, 1.5, 1]])
# Test L-shape as poly_a # Test L-shape as poly_a
res1 = intersect(l_shape, b3) res1 = intersect(l_shape, b3)
check_poly(res1)
vol1 = adze.get_volume(res1) vol1 = adze.get_volume(res1)
assert numpy.isclose(vol1, 0.75) assert numpy.isclose(vol1, 0.75)
# Test L-shape as poly_b # Test L-shape as poly_b
res2 = intersect(b3, l_shape) res2 = intersect(b3, l_shape)
check_poly(res2)
vol2 = adze.get_volume(res2) vol2 = adze.get_volume(res2)
assert numpy.isclose(vol2, 0.75) assert numpy.isclose(vol2, 0.75)
@ -89,6 +98,7 @@ def test_difference_nonconvex() -> None:
# B1 - B2 = [1, 2] x [0, 1] x [0, 1] -> vol=1 # B1 - B2 = [1, 2] x [0, 1] x [0, 1] -> vol=1
res = difference(b1, b2) res = difference(b1, b2)
check_poly(res)
assert numpy.isclose(adze.get_volume(res), 1.0) assert numpy.isclose(adze.get_volume(res), 1.0)
# L-shape - B3 # L-shape - B3
@ -99,8 +109,21 @@ def test_difference_nonconvex() -> None:
l_shape = union(b1, b2) l_shape = union(b1, b2)
b3 = build_box([[0.5, 0.5, 0], [1.5, 1.5, 1]]) b3 = build_box([[0.5, 0.5, 0], [1.5, 1.5, 1]])
res2 = difference(l_shape, b3) res2 = difference(l_shape, b3)
check_poly(res2)
assert numpy.isclose(adze.get_volume(res2), 2.25) assert numpy.isclose(adze.get_volume(res2), 2.25)
def test_difference_hole() -> None:
"""
Test subtracting a small box strictly inside a larger box (Hole Drilling).
"""
outer = build_box([[0, 0, 0], [3, 3, 3]]) # Vol 27
inner = build_box([[1, 1, 1], [2, 2, 2]]) # Vol 1
diff = difference(outer, inner)
check_poly(diff)
assert numpy.isclose(adze.get_volume(diff), 26.0)
def test_robust_normals() -> None: def test_robust_normals() -> None:
""" """
Test that planes_from_boundary handles collinear vertices. Test that planes_from_boundary handles collinear vertices.