Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed boolean_manifold iteration for improved performance. #2249

Merged
merged 4 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions tests/test_boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,51 @@ def test_empty(self):

assert i.is_empty

def test_boolean_manifold(self):
if manifold3d is None:
return

times = {}
for operation in ["union", "intersection"]:

if operation == "union":
# chain of icospheres
meshes = [g.trimesh.primitives.Sphere(center=[x / 2, 0, 0], subdivisions=0) for x in range(100)]
else:
# closer icospheres for non-empty-intersection
meshes = [g.trimesh.primitives.Sphere(center=[x, x, x], subdivisions=0) for x in g.np.linspace(0, 0.5, 101)]

# the old 'serial' manifold method
tic = g.time.time()
manifolds = [
manifold3d.Manifold(
mesh=manifold3d.Mesh(
vert_properties=g.np.array(mesh.vertices, dtype=g.np.float32),
tri_verts=g.np.array(mesh.faces, dtype=g.np.uint32),
)
)
for mesh in meshes
]
result_manifold = manifolds[0]
for manifold in manifolds[1:]:
if operation == "union":
result_manifold = result_manifold + manifold
else: # operation == "intersection":
result_manifold = result_manifold ^ manifold
result_mesh = result_manifold.to_mesh()
old_mesh = g.trimesh.Trimesh(vertices=result_mesh.vert_properties, faces=result_mesh.tri_verts)
times["serial " + operation] = g.time.time() - tic

# new 'binary' method
tic = g.time.time()
new_mesh = g.trimesh.boolean.boolean_manifold(meshes, operation)
times["binary " + operation] = g.time.time() - tic

assert old_mesh.is_volume == new_mesh.is_volume
assert old_mesh.body_count == new_mesh.body_count
assert g.np.isclose(old_mesh.volume, new_mesh.volume)

g.log.info(times)

if __name__ == "__main__":
g.trimesh.util.attach_to_log()
Expand Down
26 changes: 14 additions & 12 deletions trimesh/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ def boolean_manifold(
meshes: Iterable,
operation: str,
check_volume: bool = True,
debug: bool = False,
**kwargs,
):
"""
Expand All @@ -127,12 +126,12 @@ def boolean_manifold(
Raise an error if not all meshes are watertight
positive volumes. Advanced users may want to ignore
this check as it is expensive.
debug
Enable potentially slow additional checks and debug info.
kwargs
Passed through to the `engine`.

"""
if check_volume and not all(m.is_volume for m in meshes):
raise ValueError("Not all meshes are volumes!")

# Convert to manifold meshes
manifolds = [
Manifold(
Expand All @@ -150,15 +149,18 @@ def boolean_manifold(
raise ValueError("Difference only defined over two meshes.")

result_manifold = manifolds[0] - manifolds[1]
elif operation == "union":
result_manifold = manifolds[0]
for manifold in manifolds[1:]:
result_manifold = result_manifold + manifold
elif operation == "intersection":
elif operation in ["union", "intersection"]:
for _lvl in range(int(1 + np.log2(len(manifolds)))):
results = []
for i in np.arange(len(manifolds) // 2) * 2:
if operation == "union":
results.append(manifolds[i] + manifolds[i + 1])
else: # operation == "intersection":
results.append(manifolds[i] ^ manifolds[i + 1])
if len(manifolds) % 2:
results.append(manifolds[-1])
manifolds = results
result_manifold = manifolds[0]

for manifold in manifolds[1:]:
result_manifold = result_manifold ^ manifold
else:
raise ValueError(f"Invalid boolean operation: '{operation}'")

Expand Down
Loading