-
Notifications
You must be signed in to change notification settings - Fork 1
/
level_set.py
295 lines (243 loc) · 11.4 KB
/
level_set.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import taichi as ti
import utils
from utils import *
from functools import reduce
@ti.data_oriented
class LevelSet:
def __init__(self, dim, res, dx, real):
self.dim = dim
self.res = res
self.dx = dx
self.real = real
self.valid = ti.field(dtype=ti.i32, shape=res) # indices to the closest points / reuse as visit sign
self.phi = ti.field(dtype=real, shape=res)
self.phi_temp = ti.field(dtype=real, shape=res)
self.eps = 1 * self.dx # the "thickness" of the interface, O(∆x) and is smaller than the local feature size
@ti.func
def theta(self, phi): # smoothed step Heaviside function
theta = ti.cast(0, self.real)
if phi <= -self.eps: theta = 0
elif phi >= self.eps: theta = 1
else: theta = 1/2 + phi/(2*self.eps) + 1/(2*ti.math.pi) * ti.sin(ti.math.pi*phi/self.eps)
return theta
@ti.func
def delta(self, phi): # smoothed regular Dirac delta function
delta = ti.cast(0, self.real)
if phi <= -self.eps or phi >= self.eps: delta = 0
else: delta = (1 + ti.cos(ti.math.pi*phi/self.eps)) / (2*self.eps)
return delta
@ti.func
def distance_of_aabb(self, x, x0, x1):
phi = ti.cast(0, self.real)
if all(x > x0) and all(x < x1): # (inside)
phi = (ti.max(x0 - x, x - x1)).max()
else: # (outside)
# Find the closest point (p,q,r) on the surface of the box
p = ti.zero(x)
for k in ti.static(range(self.dim)):
if x[k] < x0[k]: p[k] = x0[k]
elif x[k] > x1[k]: p[k] = x1[k]
else: p[k] = x[k]
phi = (x - p).norm()
return phi
@ti.kernel
def initialize_with_aabb(self, x0 : ti.template(), x1 : ti.template()):
for I in ti.grouped(self.phi):
self.phi[I] = self.distance_of_aabb((I + 0.5) * self.dx, x0, x1)
@ti.kernel
def initialize_with_sphere(self, x0 : ti.template(), r : ti.template()):
for I in ti.grouped(self.phi):
self.phi[I] = ((I + 0.5) * self.dx - x0).norm() - r
@ti.kernel
def target_surface(self):
for I in ti.grouped(self.phi):
sign_change = False
est = ti.cast(1e20, self.real)
for k in ti.static(range(self.dim)):
for s in ti.static((-1, 1)):
offset = ti.Vector.unit(self.dim, k) * s
I1 = I + offset
if I1[k] >= 0 and I1[k] < self.res[k] and \
ti.math.sign(self.phi[I]) != ti.math.sign(self.phi[I1]):
theta = self.phi[I] / (self.phi[I] - self.phi[I1])
est0 = ti.math.sign(self.phi[I]) * theta * self.dx
est = est0 if ti.abs(est0) < ti.abs(est) else est
sign_change = True
if sign_change:
self.phi_temp[I] = est
self.valid[I] = 0
else:
self.phi_temp[I] = ti.cast(1e20, self.real) # an upper bound for all possible distances
@ti.func
def update_from_neighbor(self, I):
# solve the Eikonal equation
nb = ti.Vector.zero(self.real, self.dim)
for k in ti.static(range(self.dim)):
o = ti.Vector.unit(self.dim, k)
if I[k] == 0 or (I[k] < self.res[k] - 1 and ti.abs(self.phi_temp[I + o]) < ti.abs(self.phi_temp[I - o])): nb[k] = ti.abs(self.phi_temp[I + o])
else: nb[k] = ti.abs(self.phi_temp[I - o])
# sort
for i in ti.static(range(self.dim-1)):
for j in ti.static(range(self.dim-1-i)):
if nb[j] > nb[j + 1]: nb[j], nb[j + 1] = nb[j + 1], nb[j]
# (Try just the closest neighbor)
d = nb[0] + self.dx
if d > nb[1]:
# (Try the two closest neighbors)
d = (1/2) * (nb[0] + nb[1] + ti.sqrt(2 * (self.dx ** 2) - (nb[1] - nb[0]) ** 2))
if ti.static(self.dim == 3):
if d > nb[2]:
# (Use all three neighbors)
d = (1/3) * (nb[0] + nb[1] + nb[2] + ti.sqrt(ti.max(0, (nb[0] + nb[1] + nb[2]) ** 2 - 3 * (nb[0] ** 2 + nb[1] ** 2 + nb[2] ** 2 - self.dx ** 2))))
return d
@ti.kernel
def distance_to_markers(self, markers : ti.template(), total_mk : ti.template()):
# (Initialize the arrays near the input geometry)
for p in range(total_mk):
I = (markers[p] / self.dx).cast(int)
d = (markers[p] - (I + 0.5) * self.dx).norm()
if all(I >= 0 and I < self.res) and d < self.phi[I]:
self.phi[I] = d
self.valid[I] = p
@ti.kernel
def target_minus(self):
for I in ti.grouped(self.phi):
self.phi[I] -= (0.99 * self.dx) # the particle radius r (typically just a little less than the grid cell size dx)
for I in ti.grouped(self.phi):
sign_change = False
for k in ti.static(range(self.dim)):
for s in ti.static((-1, 1)):
offset = ti.Vector.unit(self.dim, k) * s
I1 = I + offset
if I1[k] >= 0 and I1[k] < self.res[k] and \
ti.math.sign(self.phi[I]) != ti.math.sign(self.phi[I1]):
sign_change = True
if sign_change and self.phi[I] <= 0:
self.valid[I] = 0
self.phi_temp[I] = self.phi[I]
elif self.phi[I] <= 0:
self.phi_temp[I] = ti.cast(-1, self.real)
else:
self.phi_temp[I] = self.phi[I]
self.valid[I] = 0
@ti.kernel
def smoothing(self, phi : ti.template(), phi_temp : ti.template()):
for I in ti.grouped(phi_temp):
phi_avg = ti.cast(0, self.real)
tot = ti.cast(0, int)
for k in ti.static(range(self.dim)):
for s in ti.static((-1, 1)):
offset = ti.Vector.unit(self.dim, k) * s
I1 = I + offset
if I1[k] >= 0 and I1[k] < self.res[k]:
phi_avg += phi_temp[I1]
tot += 1
phi_avg /= tot
phi[I] = phi_avg if phi_avg < phi_temp[I] else phi_temp[I]
# H. Zhao. A fast sweeping method for Eikonal equations. Math. Comp., 74:603–627, 2005.
@ti.data_oriented
class FastSweepingLevelSet(LevelSet):
def __init__(self, dim, res, dx, real):
super().__init__(dim, res, dx, real)
self.repeat_times = 10
@ti.func
def propagate_update(self, I, s):
if self.valid[I] == -1:
d = self.update_from_neighbor(I)
if ti.abs(d) < ti.abs(self.phi_temp[I]): self.phi_temp[I] = d * ti.math.sign(self.phi[I])
return s
@ti.func
def markers_propagate_update(self, markers, lI, o, s):
I, offset = ti.Vector(lI), ti.Vector(o)
if all(I + offset >= 0) and all(I + offset < self.res):
d = (markers[self.valid[I + offset]] - (I + 0.5) * self.dx).norm()
if d < self.phi[I]:
self.phi[I] = d
self.valid[I] = self.valid[I + o]
return s
@ti.kernel
def propagate(self):
if ti.static(self.dim == 2):
for i in range(self.res[0]):
j = 0
while j < self.res[1]: j += self.propagate_update([i, j], 1)
for i in range(self.res[0]):
j = self.res[1] - 1
while j >= 0: j += self.propagate_update([i, j], -1)
for j in range(self.res[1]):
i = 0
while i < self.res[1]: i += self.propagate_update([i, j], 1)
for j in range(self.res[1]):
i = self.res[1] - 1
while i >= 0: i += self.propagate_update([i, j], -1)
if ti.static(self.dim == 3):
for i, j in ti.ndrange(self.res[0], self.res[1]):
k = 0
while k < self.res[2]: k += self.propagate_update([i, j, k], 1)
for i, j in ti.ndrange(self.res[0], self.res[1]):
k = self.res[2] - 1
while k >= 0: k += self.propagate_update([i, j, k], -1)
for i, k in ti.ndrange(self.res[0], self.res[2]):
j = 0
while j < self.res[1]: j += self.propagate_update([i, j, k], 1)
for i, k in ti.ndrange(self.res[0], self.res[2]):
j = self.res[1] - 1
while j >= 0: j += self.propagate_update([i, j, k], -1)
for j, k in ti.ndrange(self.res[1], self.res[2]):
i = 0
while i < self.res[1]: i += self.propagate_update([i, j, k], 1)
for j, k in ti.ndrange(self.res[1], self.res[2]):
i = self.res[0] - 1
while i >= 0: i += self.propagate_update([i, j, k], -1)
def redistance(self):
self.valid.fill(-1)
self.target_surface()
self.propagate()
self.phi.copy_from(self.phi_temp)
@ti.kernel
def markers_propagate(self, markers : ti.template(), total_mk : ti.template()):
if ti.static(self.dim == 2):
for i in range(self.res[0]):
j = 0
while j < self.res[1]: j += self.markers_propagate_update(markers, [i, j], [0, 1], 1)
for i in range(self.res[0]):
j = self.res[1] - 1
while j >= 0: j += self.markers_propagate_update(markers, [i, j], [0, -1], -1)
for j in range(self.res[1]):
i = 0
while i < self.res[1]: i += self.markers_propagate_update(markers, [i, j], [1, 0], 1)
for j in range(self.res[1]):
i = self.res[1] - 1
while i >= 0: i += self.markers_propagate_update(markers, [i, j], [-1, 0], -1)
if ti.static(self.dim == 3):
for i, j in ti.ndrange(self.res[0], self.res[1]):
k = 0
while k < self.res[2]: k += self.markers_propagate_update(markers, [i, j, k], [0, 0, 1], 1)
for i, j in ti.ndrange(self.res[0], self.res[1]):
k = self.res[2] - 1
while k >= 0: k += self.markers_propagate_update(markers, [i, j, k], [0, 0, -1], -1)
for i, k in ti.ndrange(self.res[0], self.res[2]):
j = 0
while j < self.res[1]: j += self.markers_propagate_update(markers, [i, j, k], [0, 1, 0], 1)
for i, k in ti.ndrange(self.res[0], self.res[2]):
j = self.res[1] - 1
while j >= 0: j += self.markers_propagate_update(markers, [i, j, k], [0, -1, 0], -1)
for j, k in ti.ndrange(self.res[1], self.res[2]):
i = 0
while i < self.res[1]: i += self.markers_propagate_update(markers, [i, j, k], [1, 0, 0], 1)
for j, k in ti.ndrange(self.res[1], self.res[2]):
i = self.res[0] - 1
while i >= 0: i += self.markers_propagate_update(markers, [i, j, k], [-1, 0, 0], -1)
def build_from_markers(self, markers, total_mk):
self.phi.fill(1e20)
self.valid.fill(-1)
self.distance_to_markers(markers, total_mk)
for i in range(self.repeat_times):
self.markers_propagate(markers, total_mk)
self.valid.fill(-1)
self.target_minus()
for i in range(self.repeat_times):
self.propagate()
self.smoothing(self.phi, self.phi_temp)
self.smoothing(self.phi_temp, self.phi)
self.smoothing(self.phi, self.phi_temp)