forked from giorgiodema/Smooth-Boolean-Operations-for-Meshes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboolean.py
256 lines (237 loc) · 9.26 KB
/
boolean.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import pymesh
import numpy as np
import skimage
from typing import Tuple,List
##
# Auxiliary functions
##
def _computeAABB(meshes:List[pymesh.Mesh])->Tuple[int]:
v = meshes[0].vertices
for m in meshes[1:]:
v = np.concatenate([v,m.vertices],axis=0)
minx = np.min(v[:,0])
maxx = np.max(v[:,0])
miny = np.min(v[:,1])
maxy = np.max(v[:,1])
minz = np.min(v[:,2])
maxz = np.max(v[:,2])
return (minx,maxx,miny,maxy,minz,maxz)
def _makeSDFGrid(m:pymesh.Mesh,resolution:int,aabb:Tuple[int],pad:float=0.0)->np.ndarray:
ox = pad * (aabb[1]-aabb[0])/resolution
oy = pad * (aabb[3]-aabb[2])/resolution
oz = pad * (aabb[5]-aabb[4])/resolution
x = np.linspace(aabb[0]-ox,aabb[1]+ox,resolution)
y = np.linspace(aabb[2]-oy,aabb[3]+oy,resolution)
z = np.linspace(aabb[4]-oz,aabb[5]+oz,resolution)
points = np.zeros((resolution**3,3))
for i in range(resolution):
for j in range(resolution):
for k in range(resolution):
points[i*resolution**2 + j*resolution + k] = np.asarray([x[i],y[j],z[k]])
sdf,_,_,_ = pymesh.signed_distance_to_mesh(m,points)
sdf = np.reshape(sdf,(resolution,resolution,resolution))
return sdf,points
def _smoothUnion(sdf1:np.ndarray,sdf2:np.ndarray,smoothness:float)->np.ndarray:
h = np.clip(0.5 + 0.5*(sdf2-sdf1)/smoothness,0.,1.)
interp = sdf2 * (1. - h) + sdf1 * h
res = interp - smoothness * h * (1.-h)
return res
def _smoothSubtraction(sdf1:np.ndarray,sdf2:np.ndarray,smoothness:float)->np.ndarray:
h = np.clip(0.5 - 0.5*(sdf2+sdf1)/smoothness,0.,1.)
interp = sdf2 * (1. - h) - sdf1 * h
res = interp + smoothness * h * (1.-h)
return res
def _smoothIntersection(sdf1:np.ndarray,sdf2:np.ndarray,smoothness:float)->np.ndarray:
h = np.clip(0.5 - 0.5*(sdf2-sdf1)/smoothness,0.,1.)
interp = sdf2 * (1. - h) + sdf1 * h
res = interp + smoothness * h * (1.-h)
return res
##
# Non Smooth Boolean Operations
##
def union(m1:pymesh.Mesh,m2:pymesh.Mesh,resolution:int,pad:float=1.0)->pymesh.Mesh:
"""
Compute the union between two meshes
Parameters:
m1 (pymesh.Mesh): The first operand
m2 (pymesh.Mesh): The second operand
resolution (int): The resolution of the volumetric grid
pad (float): Add padding to the original grid
Returns:
pymesh.Mesh: The result of the boolean operation
"""
aabb = _computeAABB([m1,m2])
sdf1,_ = _makeSDFGrid(m1,resolution,aabb,pad=pad)
sdf2,_ = _makeSDFGrid(m2,resolution,aabb,pad=pad)
sdf = np.minimum(sdf1,sdf2)
ox = pad * (aabb[1]-aabb[0])/resolution
oy = pad * (aabb[3]-aabb[2])/resolution
oz = pad * (aabb[5]-aabb[4])/resolution
spacing = (
(aabb[1]-aabb[0]+2*ox)/resolution,
(aabb[3]-aabb[2]+2*oy)/resolution,
(aabb[5]-aabb[4]+2*oz)/resolution
)
verts,faces,_,_ = skimage.measure.marching_cubes(sdf,level=0.,spacing=spacing)
mesh = pymesh.form_mesh(verts,faces)
return mesh
def subtraction(m1:pymesh.Mesh,m2:pymesh.Mesh,resolution:int,pad:float=1.0)->pymesh.Mesh:
"""
Compute the difference between two meshes (m2 - m1)
Parameters:
m1 (pymesh.Mesh): The first operand
m2 (pymesh.Mesh): The second operand
resolution (int): The resolution of the volumetric grid
pad (float): Add padding to the original grid
Returns:
pymesh.Mesh: The result of the boolean operation
"""
aabb = _computeAABB([m1,m2])
sdf1,_ = _makeSDFGrid(m1,resolution,aabb,pad=pad)
sdf2,_ = _makeSDFGrid(m2,resolution,aabb,pad=pad)
sdf = np.maximum(-sdf1,sdf2)
ox = pad * (aabb[1]-aabb[0])/resolution
oy = pad * (aabb[3]-aabb[2])/resolution
oz = pad * (aabb[5]-aabb[4])/resolution
spacing = (
(aabb[1]-aabb[0]+2*ox)/resolution,
(aabb[3]-aabb[2]+2*oy)/resolution,
(aabb[5]-aabb[4]+2*oz)/resolution
)
verts,faces,_,_ = skimage.measure.marching_cubes(sdf,level=0.,spacing=spacing)
mesh = pymesh.form_mesh(verts,faces)
return mesh
def intersection(m1:pymesh.Mesh,m2:pymesh.Mesh,resolution:int,pad:float=1.0)->pymesh.Mesh:
"""
Compute the intersection between two meshes
Parameters:
m1 (pymesh.Mesh): The first operand
m2 (pymesh.Mesh): The second operand
resolution (int): The resolution of the volumetric grid
pad (float): Add padding to the original grid
Returns:
pymesh.Mesh: The result of the boolean operation
"""
aabb = _computeAABB([m1,m2])
sdf1,_ = _makeSDFGrid(m1,resolution,aabb,pad=pad)
sdf2,_ = _makeSDFGrid(m2,resolution,aabb,pad=pad)
sdf = np.maximum(sdf1,sdf2)
ox = pad * (aabb[1]-aabb[0])/resolution
oy = pad * (aabb[3]-aabb[2])/resolution
oz = pad * (aabb[5]-aabb[4])/resolution
spacing = (
(aabb[1]-aabb[0]+2*ox)/resolution,
(aabb[3]-aabb[2]+2*oy)/resolution,
(aabb[5]-aabb[4]+2*oz)/resolution
)
verts,faces,_,_ = skimage.measure.marching_cubes(sdf,level=0.,spacing=spacing)
mesh = pymesh.form_mesh(verts,faces)
return mesh
##
# Smooth Boolean Operations
##
def smoothUnion(m1:pymesh.Mesh,m2:pymesh.Mesh,smoothness:float,resolution:int,pad:float=10.0)->pymesh.Mesh:
"""
Compute the smooth union between two meshes
Parameters:
m1 (pymesh.Mesh): The first operand
m2 (pymesh.Mesh): The second operand
smoothness (float): The amount of smoothness in actual distance units
resolution (int): The resolution of the volumetric grid
pad (float): Add padding to the original grid
Returns:
pymesh.Mesh: The result of the boolean operation
"""
aabb = _computeAABB([m1,m2])
sdf1,_ = _makeSDFGrid(m1,resolution,aabb,pad=pad)
sdf2,_ = _makeSDFGrid(m2,resolution,aabb,pad=pad)
sdf = _smoothUnion(sdf1,sdf2,smoothness)
ox = pad * (aabb[1]-aabb[0])/resolution
oy = pad * (aabb[3]-aabb[2])/resolution
oz = pad * (aabb[5]-aabb[4])/resolution
spacing = (
(aabb[1]-aabb[0] + 2*ox)/resolution,
(aabb[3]-aabb[2] + 2*oy)/resolution,
(aabb[5]-aabb[4] + 2*oz)/resolution
)
verts,faces,_,_ = skimage.measure.marching_cubes(sdf,level=0.,spacing=spacing)
mesh = pymesh.form_mesh(verts,faces)
return mesh
def smoothSubtraction(m1:pymesh.Mesh,m2:pymesh.Mesh,smoothness:float,resolution:int,pad:float=10.0)->pymesh.Mesh:
"""
Compute the smooth difference between two meshes
Parameters:
m1 (pymesh.Mesh): The first operand
m2 (pymesh.Mesh): The second operand
smoothness (float): The amount of smoothness in actual distance units
resolution (int): The resolution of the volumetric grid
pad (float): Add padding to the original grid
Returns:
pymesh.Mesh: The result of the boolean operation
"""
aabb = _computeAABB([m1,m2])
sdf1,_ = _makeSDFGrid(m1,resolution,aabb,pad=pad)
sdf2,_ = _makeSDFGrid(m2,resolution,aabb,pad=pad)
sdf = _smoothSubtraction(sdf1,sdf2,smoothness)
ox = pad * (aabb[1]-aabb[0])/resolution
oy = pad * (aabb[3]-aabb[2])/resolution
oz = pad * (aabb[5]-aabb[4])/resolution
spacing = (
(aabb[1]-aabb[0] + 2*ox)/resolution,
(aabb[3]-aabb[2] + 2*oy)/resolution,
(aabb[5]-aabb[4] + 2*oz)/resolution
)
verts,faces,_,_ = skimage.measure.marching_cubes(sdf,level=0.,spacing=spacing)
mesh = pymesh.form_mesh(verts,faces)
return mesh
def smoothIntersection(m1:pymesh.Mesh,m2:pymesh.Mesh,smoothness:float,resolution:int,pad:float=10.0)->pymesh.Mesh:
"""
Compute the smooth intersection between two meshes
Parameters:
m1 (pymesh.Mesh): The first operand
m2 (pymesh.Mesh): The second operand
smoothness (float): The amount of smoothness in actual distance units
resolution (int): The resolution of the volumetric grid
pad (float): Add padding to the original grid
Returns:
pymesh.Mesh: The result of the boolean operation
"""
aabb = _computeAABB([m1,m2])
sdf1,_ = _makeSDFGrid(m1,resolution,aabb,pad=pad)
sdf2,_ = _makeSDFGrid(m2,resolution,aabb,pad=pad)
sdf = _smoothIntersection(sdf1,sdf2,smoothness)
ox = pad * (aabb[1]-aabb[0])/resolution
oy = pad * (aabb[3]-aabb[2])/resolution
oz = pad * (aabb[5]-aabb[4])/resolution
spacing = (
(aabb[1]-aabb[0] + 2*ox)/resolution,
(aabb[3]-aabb[2] + 2*oy)/resolution,
(aabb[5]-aabb[4] + 2*oz)/resolution
)
verts,faces,_,_ = skimage.measure.marching_cubes(sdf,level=0.,spacing=spacing)
mesh = pymesh.form_mesh(verts,faces)
return mesh
def round(m:pymesh.Mesh,roundness:float,resolution:int,pad:float=10.0)->pymesh.Mesh:
"""
Compute a rounded version of the original mesh
Parameters:
m (pymesh.Mesh): The input mesh
roundness (float): The amount of roundness in actual distance units
resolution (int): The resolution of the volumetric grid
pad (float): Add padding to the original grid
Returns:
pymesh.Mesh: The rounded mesh
"""
aabb = _computeAABB([m])
sdf,_ = _makeSDFGrid(m,resolution,aabb,pad)
ox = pad * (aabb[1]-aabb[0])/resolution
oy = pad * (aabb[3]-aabb[2])/resolution
oz = pad * (aabb[5]-aabb[4])/resolution
spacing = (
(aabb[1]-aabb[0] + 2*ox)/resolution,
(aabb[3]-aabb[2] + 2*oy)/resolution,
(aabb[5]-aabb[4] + 2*oz)/resolution
)
verts,faces,_,_ = skimage.measure.marching_cubes(sdf,level=roundness,spacing=spacing)
mesh = pymesh.form_mesh(verts,faces)
return mesh