-
Notifications
You must be signed in to change notification settings - Fork 5
/
booleans.py
203 lines (154 loc) · 4.97 KB
/
booleans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from typing import Literal
import numpy as np
from compas.plugins import plugin
from compas_cgal._cgal import booleans
from .types import VerticesFaces
from .types import VerticesFacesNumpy
def _boolean(
A: VerticesFaces,
B: VerticesFaces,
operation: Literal["union", "difference", "intersection"],
) -> VerticesFacesNumpy:
"""Wrapper for all boolean operations.
Parameters
----------
A : VerticesFaces
Mesh A.
B : VerticesFaces
Mesh B.
operation : Literal['union', 'difference', 'intersection']
The type of boolean operation.
Returns
-------
VerticesFacesNumpy
Raises
------
NotImplementedError
If the operation type is not supported.
"""
VA, FA = A
VB, FB = B
VA = np.asarray(VA, dtype=np.float64)
FA = np.asarray(FA, dtype=np.int32)
VB = np.asarray(VB, dtype=np.float64)
FB = np.asarray(FB, dtype=np.int32)
if operation == "union":
result = booleans.boolean_union(VA, FA, VB, FB)
elif operation == "difference":
result = booleans.boolean_difference(VA, FA, VB, FB)
elif operation == "intersection":
result = booleans.boolean_intersection(VA, FA, VB, FB)
elif operation == "split":
result = booleans.split(VA, FA, VB, FB)
else:
raise NotImplementedError
return result
@plugin(category="booleans", pluggable_name="boolean_union_mesh_mesh")
def boolean_union_mesh_mesh(
A: VerticesFaces,
B: VerticesFaces,
) -> VerticesFacesNumpy:
"""Boolean union of two meshes.
Parameters
----------
A : :attr:`compas_cgal.types.VerticesFaces`
Mesh A.
B : :attr:`compas_cgal.types.VerticesFaces`
Mesh B.
Returns
-------
:attr:`compas_cgal.types.VerticesFacesNumpy`
Examples
--------
>>> from compas.geometry import Box, Sphere, Polyhedron
>>> from compas_cgal.booleans import boolean_union_mesh_mesh
>>> box = Box(1)
>>> sphere = Sphere(0.5, point=[1, 1, 1])
>>> A = box.to_vertices_and_faces(triangulated=True)
>>> B = sphere.to_vertices_and_faces(u=32, v=32, triangulated=True)
>>> C = boolean_union_mesh_mesh(A, B)
>>> shape = Polyhedron(*C)
"""
return _boolean(A, B, "union")
@plugin(category="booleans", pluggable_name="boolean_difference_mesh_mesh")
def boolean_difference_mesh_mesh(
A: VerticesFaces,
B: VerticesFaces,
) -> VerticesFacesNumpy:
"""Boolean difference of two meshes.
Parameters
----------
A : :attr:`compas_cgal.types.VerticesFaces`
Mesh A.
B : :attr:`compas_cgal.types.VerticesFaces`
Mesh B.
Returns
-------
:attr:`compas_cgal.types.VerticesFacesNumpy`
Examples
--------
>>> from compas.geometry import Box, Sphere, Polyhedron
>>> from compas_cgal.booleans import boolean_difference_mesh_mesh
>>> box = Box(1)
>>> sphere = Sphere(0.5, point=[1, 1, 1])
>>> A = box.to_vertices_and_faces(triangulated=True)
>>> B = sphere.to_vertices_and_faces(u=32, v=32, triangulated=True)
>>> C = boolean_difference_mesh_mesh(A, B)
>>> shape = Polyhedron(*C)
"""
return _boolean(A, B, "difference")
@plugin(category="booleans", pluggable_name="boolean_intersection_mesh_mesh")
def boolean_intersection_mesh_mesh(
A: VerticesFaces,
B: VerticesFaces,
) -> VerticesFacesNumpy:
"""Boolean intersection of two meshes.
Parameters
----------
A : :attr:`compas_cgal.types.VerticesFaces`
Mesh A.
B : :attr:`compas_cgal.types.VerticesFaces`
Mesh B.
Returns
-------
:attr:`compas_cgal.types.VerticesFacesNumpy`
Examples
--------
>>> from compas.geometry import Box, Sphere, Polyhedron
>>> from compas_cgal.booleans import boolean_intersection_mesh_mesh
>>> box = Box(1)
>>> sphere = Sphere(0.5, point=[1, 1, 1])
>>> A = box.to_vertices_and_faces(triangulated=True)
>>> B = sphere.to_vertices_and_faces(u=32, v=32, triangulated=True)
>>> C = boolean_intersection_mesh_mesh(A, B)
>>> shape = Polyhedron(*C)
"""
return _boolean(A, B, "intersection")
@plugin(category="booleans", pluggable_name="split_mesh_mesh")
def split_mesh_mesh(
A: VerticesFaces,
B: VerticesFaces,
) -> VerticesFacesNumpy:
"""Split one mesh with another.
Parameters
----------
A : :attr:`compas_cgal.types.VerticesFaces`
Mesh A.
B : :attr:`compas_cgal.types.VerticesFaces`
Mesh B.
Returns
-------
:attr:`compas_cgal.types.VerticesFacesNumpy`
Examples
--------
>>> from compas.geometry import Box, Sphere, Polyhedron
>>> from compas_cgal.booleans import split_mesh_mesh
>>> box = Box(1)
>>> sphere = Sphere(0.5, point=[1, 1, 1])
>>> A = box.to_vertices_and_faces(triangulated=True)
>>> B = sphere.to_vertices_and_faces(u=32, v=32, triangulated=True)
>>> V, F = split_mesh_mesh(A, B)
>>> shape = Polyhedron(V.tolist(), F.tolist())
"""
return _boolean(A, B, "split")
split = split_mesh_mesh