2024-06-28 20:07:37 -07:00
|
|
|
import cadquery as Cq
|
|
|
|
|
|
|
|
def binary_intersection(a: Cq.Assembly) -> Cq.Shape:
|
2024-07-06 16:41:13 -07:00
|
|
|
objs = [s.toCompound() for _, s in a.traverse()
|
|
|
|
if isinstance(s, Cq.Assembly)]
|
2024-06-28 20:07:37 -07:00
|
|
|
obj1, obj2 = objs[:2]
|
|
|
|
return obj1.intersect(obj2)
|
2024-07-06 16:41:13 -07:00
|
|
|
|
|
|
|
|
|
|
|
def pairwise_intersection(assembly: Cq.Assembly, tol: float=1e-6) -> list[(str, str, float)]:
|
|
|
|
"""
|
|
|
|
Given an assembly, test the pairwise intersection volume of its components.
|
|
|
|
Return the pairs whose intersection volume exceeds `tol`.
|
|
|
|
"""
|
|
|
|
m = {name: (i, shape.moved(loc))
|
|
|
|
for i, (shape, name, loc, _)
|
|
|
|
in enumerate(assembly)}
|
|
|
|
result = []
|
|
|
|
for name, (i1, sh1) in m.items():
|
|
|
|
for name2, (i2, sh2) in m.items():
|
|
|
|
if name == name2:
|
|
|
|
assert i1 == i2
|
|
|
|
continue
|
|
|
|
if i2 <= i1:
|
|
|
|
# Remove the upper diagonal
|
|
|
|
continue
|
|
|
|
vol = sh1.intersect(sh2).Volume()
|
|
|
|
if vol > tol:
|
|
|
|
result.append((name, name2, vol))
|
|
|
|
return result
|