[boolean] Add boolean operations

This commit is contained in:
jan 2026-03-07 07:30:43 -08:00
commit 981e0e5b81
3 changed files with 314 additions and 0 deletions

View file

@ -89,6 +89,14 @@ from .shapes import (
planes_from_box as planes_from_box,
)
from .voxel import voxelize as voxelize
from .boolean import (
intersect as intersect,
difference as difference,
union as union,
intersect_convex as intersect_convex,
difference_convex as difference_convex,
union_convex as union_convex,
)
__author__ = 'Jan Petykiewicz'
__version__ = '0.2'

166
adze/boolean.py Normal file
View file

@ -0,0 +1,166 @@
import numpy
from . import poly_t, plane_t
from .split import clip, split
from .boundary import planes_from_boundary, boundary_from_poly
from .util import merge_polyhedra, build_empty, is_inside, get_aabb
def intersect_convex(poly_a: poly_t, poly_b: poly_t, atol: float = 1e-12) -> poly_t:
"""
Intersect two polyhedra, assuming poly_b is convex.
"""
breps = boundary_from_poly(poly_b)
result = poly_a
for brep in breps:
planes = planes_from_boundary(brep['vertices'], brep['faces'])
for plane in planes:
result = clip(result, plane, atol=atol)
if result['vertices'].shape[0] == 0:
return result
return result
def difference_convex(poly_a: poly_t, poly_b: poly_t, atol: float = 1e-12) -> poly_t:
"""
Subtract poly_b from poly_a, assuming poly_b is convex.
"""
breps = boundary_from_poly(poly_b)
components_to_process = [poly_a]
outside_parts = []
for brep in breps:
planes = planes_from_boundary(brep['vertices'], brep['faces'])
for plane in planes:
next_to_process = []
for comp in components_to_process:
in_part, out_part = split(comp, plane, atol=atol)
if out_part['vertices'].shape[0] > 0:
outside_parts.append(out_part)
if in_part['vertices'].shape[0] > 0:
next_to_process.append(in_part)
components_to_process = next_to_process
if not components_to_process:
break
return merge_polyhedra(outside_parts)
def union_convex(poly_a: poly_t, poly_b: poly_t, atol: float = 1e-12) -> poly_t:
"""
Union of two polyhedra, assuming poly_b is convex.
"""
a_minus_b = difference_convex(poly_a, poly_b, atol=atol)
return merge_polyhedra([a_minus_b, poly_b])
def fragment_poly(poly: poly_t, planes: list[plane_t], atol: float = 1e-12) -> list[poly_t]:
"""
Fragment a polyhedron by splitting it with a list of planes.
"""
fragments = [poly]
for plane in planes:
new_fragments = []
for frag in fragments:
p_in, p_out = split(frag, plane, atol=atol)
if p_in['vertices'].shape[0] > 0:
new_fragments.append(p_in)
if p_out['vertices'].shape[0] > 0:
new_fragments.append(p_out)
fragments = new_fragments
if not fragments:
break
return fragments
def _get_poly_planes(poly: poly_t) -> list[plane_t]:
""" Helper to get unique planes from a polyhedron. """
breps = boundary_from_poly(poly)
all_planes = []
for brep in breps:
all_planes.extend(planes_from_boundary(brep['vertices'], brep['faces']))
return all_planes
def _is_poly_convex(poly: poly_t, atol: float = 1e-12) -> bool:
"""
Check if a polyhedron is convex.
A polyhedron is convex if all vertices are 'inside' every face plane.
"""
planes = _get_poly_planes(poly)
vertices = poly['vertices']
for n, d in planes:
dists = d + (vertices @ n)
if numpy.any(dists < -atol):
return False
return True
def intersect(poly_a: poly_t, poly_b: poly_t, atol: float = 1e-12, assume_convex: bool = False) -> poly_t:
"""
General intersection of two polyhedra.
"""
if poly_a['vertices'].shape[0] == 0 or poly_b['vertices'].shape[0] == 0:
return build_empty()
# Bounding box optimization
amin, amax = get_aabb(poly_a)
bmin, bmax = get_aabb(poly_b)
if numpy.any(amin > bmax + atol) or numpy.any(bmin > amax + atol):
return build_empty()
if assume_convex or _is_poly_convex(poly_b, atol=atol):
return intersect_convex(poly_a, poly_b, atol=atol)
# General non-convex algorithm: fragment A by planes of B, keep parts inside B.
planes_b = _get_poly_planes(poly_b)
frags_a = fragment_poly(poly_a, planes_b, atol=atol)
kept_a = []
for frag in frags_a:
point = numpy.mean(frag['vertices'], axis=0)
if is_inside(poly_b, point, atol=atol):
kept_a.append(frag)
return merge_polyhedra(kept_a)
def difference(poly_a: poly_t, poly_b: poly_t, atol: float = 1e-12, assume_convex: bool = False) -> poly_t:
"""
General difference: poly_a - poly_b
"""
if poly_a['vertices'].shape[0] == 0:
return build_empty()
if poly_b['vertices'].shape[0] == 0:
return poly_a
# Bounding box optimization
amin, amax = get_aabb(poly_a)
bmin, bmax = get_aabb(poly_b)
if numpy.any(amin > bmax + atol) or numpy.any(bmin > amax + atol):
return poly_a
if assume_convex or _is_poly_convex(poly_b, atol=atol):
return difference_convex(poly_a, poly_b, atol=atol)
# General non-convex algorithm: fragment A by planes of B, keep parts outside B.
planes_b = _get_poly_planes(poly_b)
frags_a = fragment_poly(poly_a, planes_b, atol=atol)
kept_a = []
for frag in frags_a:
point = numpy.mean(frag['vertices'], axis=0)
if not is_inside(poly_b, point, atol=atol):
kept_a.append(frag)
return merge_polyhedra(kept_a)
def union(poly_a: poly_t, poly_b: poly_t, atol: float = 1e-12, assume_convex: bool = False) -> poly_t:
"""
General union: poly_a + poly_b
"""
if poly_a['vertices'].shape[0] == 0:
return poly_b
if poly_b['vertices'].shape[0] == 0:
return poly_a
if assume_convex or _is_poly_convex(poly_b, atol=atol):
return union_convex(poly_a, poly_b, atol=atol)
# A union B = (A - B) + B
a_minus_b = difference(poly_a, poly_b, atol=atol)
return merge_polyhedra([a_minus_b, poly_b])

140
adze/test/test_boolean.py Normal file
View file

@ -0,0 +1,140 @@
import numpy
import adze
from adze.boolean import intersect, difference, union
from adze.shapes import build_box
def test_intersect_cubes() -> None:
# Cube 1: (0,0,0) to (2,2,2), volume = 8
poly_a = build_box([[0, 0, 0], [2, 2, 2]])
# Cube 2: (1,1,1) to (3,3,3), volume = 8
poly_b = build_box([[1, 1, 1], [3, 3, 3]])
# Overlap is from (1,1,1) to (2,2,2), volume = 1
result = intersect(poly_a, poly_b)
volume = adze.get_volume(result)
assert numpy.isclose(volume, 1.0)
def test_intersect_no_overlap() -> None:
poly_a = build_box([[0, 0, 0], [1, 1, 1]])
poly_b = build_box([[2, 2, 2], [3, 3, 3]])
result = intersect(poly_a, poly_b)
assert result['vertices'].shape[0] == 0
assert adze.get_volume(result) == 0.0
def test_difference_cubes() -> None:
# Cube 1: (0,0,0) to (2,2,2), volume = 8
poly_a = build_box([[0, 0, 0], [2, 2, 2]])
# Cube 2: (1,1,1) to (3,3,3), volume = 8
poly_b = build_box([[1, 1, 1], [3, 3, 3]])
# A - B should be 8 - 1 = 7
result = difference(poly_a, poly_b)
volume = adze.get_volume(result)
assert numpy.isclose(volume, 7.0)
def test_union_cubes() -> None:
# Cube 1: (0,0,0) to (2,2,2), volume = 8
poly_a = build_box([[0, 0, 0], [2, 2, 2]])
# Cube 2: (1,1,1) to (3,3,3), volume = 8
poly_b = build_box([[1, 1, 1], [3, 3, 3]])
# A + B should be 8 + 8 - 1 = 15
result = union(poly_a, poly_b)
volume = adze.get_volume(result)
assert numpy.isclose(volume, 15.0)
def test_union_no_overlap() -> None:
poly_a = build_box([[0, 0, 0], [1, 1, 1]])
poly_b = build_box([[2, 2, 2], [3, 3, 3]])
# A + B should be 1 + 1 = 2
result = union(poly_a, poly_b)
volume = adze.get_volume(result)
assert numpy.isclose(volume, 2.0)
def test_intersect_nonconvex_lshape() -> None:
"""
Test intersection with a non-convex L-shaped object.
The L-shape is formed by two boxes:
B1: [0,2]x[0,1]x[0,1]
B2: [0,1]x[0,2]x[0,1]
Total volume = 2 + 2 - 1 (overlap [0,1]x[0,1]x[0,1]) = 3
"""
b1 = build_box([[0, 0, 0], [2, 1, 1]])
b2 = build_box([[0, 0, 0], [1, 2, 1]])
# union(b1, b2) will produce a non-convex polyhedron
l_shape = union(b1, b2)
assert numpy.isclose(adze.get_volume(l_shape), 3.0)
b3 = build_box([[0.5, 0.5, 0], [1.5, 1.5, 1]])
# Test L-shape as poly_a
res1 = intersect(l_shape, b3)
vol1 = adze.get_volume(res1)
assert numpy.isclose(vol1, 0.75)
# Test L-shape as poly_b
res2 = intersect(b3, l_shape)
vol2 = adze.get_volume(res2)
assert numpy.isclose(vol2, 0.75)
def test_difference_nonconvex() -> None:
# B1: [0,2]x[0,1]x[0,1], vol=2
b1 = build_box([[0, 0, 0], [2, 1, 1]])
# B2: [0,1]x[0,2]x[0,1], vol=2
b2 = build_box([[0, 0, 0], [1, 2, 1]])
# B1 - B2 = [1, 2] x [0, 1] x [0, 1] -> vol=1
res = difference(b1, b2)
assert numpy.isclose(adze.get_volume(res), 1.0)
# L-shape - B3
# L-shape: B1 + B2
# B3: [0.5, 1.5] x [0.5, 1.5] x [0, 1]
# L-shape \cap B3 = 0.75
# L-shape - B3 = 3 - 0.75 = 2.25
l_shape = union(b1, b2)
b3 = build_box([[0.5, 0.5, 0], [1.5, 1.5, 1]])
res2 = difference(l_shape, b3)
assert numpy.isclose(adze.get_volume(res2), 2.25)
def test_robust_normals() -> None:
"""
Test that planes_from_boundary handles collinear vertices.
"""
# A flat square face with collinear points at the start
# [0,0,0], [1,0,0], [2,0,0] are collinear.
vertices = numpy.array([
[0, 0, 0],
[1, 0, 0],
[2, 0, 0],
[2, 2, 0],
[0, 2, 0],
# Add a top point to make it a pyramid (non-degenerate 3D shape)
[1, 1, 1]
])
# Pyramid faces
faces = [
[0, 1, 2, 3, 4], # Base (with collinear points)
[0, 5, 1],
[1, 5, 2],
[2, 5, 3],
[3, 5, 4],
[4, 5, 0]
]
# This should now succeed and produce correct planes
planes = adze.boundary.planes_from_boundary(vertices, faces)
# Check the base plane (first face)
# The normal should be [0, 0, -1] (facing inward for a CCW base viewed from below?)
# Let's check orientation. vertices 0,1,2,3,4 are CCW if viewed from +Z.
# Newell's method for CCW [0,0,0] -> [1,0,0] -> [2,0,0] -> [2,2,0] -> [0,2,0]
# produces [0, 0, Area*2]. So normal [0, 0, 1].
base_plane = planes[0]
assert numpy.allclose(base_plane[0], [0, 0, 1])
assert numpy.isclose(base_plane[1], 0.0)