Skip to content

Boolean Operations with Intersection Edgesยค

Boolean Operations with Intersection Edges

The *_with_edges variants of the boolean operations return the result mesh together with the corefinement intersection curve as vertex-index pairs into the output mesh. This avoids re-deriving the cut boundary from the dense triangulated output, and is useful for recovering clean polygonal outlines or grouping triangles that share a common original face.

The function returns a tuple (V, F, E):

  • V โ€” Nx3 vertex coordinates
  • F โ€” Mx3 triangle face indices
  • E โ€” Kx2 vertex-index pairs marking edges that lie on the intersection curve
from compas.datastructures import Mesh
from compas.geometry import Box
from compas.geometry import Line
from compas.geometry import Sphere
from compas_viewer import Viewer

from compas_cgal.booleans import boolean_difference_mesh_mesh_with_edges


def input():
    """Two triangle meshes that intersect."""
    A = Box(2).to_vertices_and_faces(triangulated=True)
    B = Sphere(1, point=[1, 1, 1]).to_vertices_and_faces(u=64, v=64, triangulated=True)
    return A, B


# =============================================================================
# Boolean difference with intersection-curve edges
# =============================================================================

A, B = input()
V, F, E = boolean_difference_mesh_mesh_with_edges(A, B)

mesh = Mesh.from_vertices_and_faces(V, F)
intersection_lines = [Line(V[i], V[j]) for i, j in E.tolist()]

# =============================================================================
# Visualize
# =============================================================================

viewer = Viewer()
viewer.scene.add(mesh, lineswidth=1, show_points=False)
group = viewer.scene.add_group(name="Intersection curve")
group.add_from_list(intersection_lines, linewidth=3, linecolor=(1.0, 0.0, 0.0))

viewer.show()