This repository has been archived by the owner on Nov 6, 2018. It is now read-only.
forked from num3ric/COMP558
/
surface_tension.py
218 lines (188 loc) · 6.38 KB
/
surface_tension.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
from __future__ import division
import Image
import numpy as np
import matplotlib as mpl
mpl.use('TkAgg')
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from scipy import ndimage
import skfmm
import sys
import itertools
import pickle
eps = sys.float_info.epsilon
wx, wy = 200, 200
grid_shape = (wx,wy)
cx, cy = wx/2, wy/2
def _space_grid(Sm, spacing):
'''
Zero everywhere, except for the points at a certain grid interval.
'''
Ix, Iy = np.indices(Sm.shape)
return Sm * (np.fmod(Ix, spacing) == 0) * (np.fmod(Iy, spacing) == 0)
def _point_shape(dist_fn, radius, spacing, dist_metric=None):
Sm = np.ones(grid_shape)
Sm[cx,cy] = 0
Sm = dist_fn(Sm, metric=dist_metric) if dist_metric else dist_fn(Sm)
#retain only the points at the defined radius
Sm = (Sm == radius)
return _space_grid(Sm, spacing)
def square(radius, spacing=1):
'''
Get a matrix defining a centered square.
Zero everywhere except for points which have value 1.
Note: Since this uses the manhattan distance, the square
rotated 45 degrees.
'''
return _point_shape(ndimage.distance_transform_cdt, radius, spacing, 'taxicab')
def circle(radius, spacing=1):
'''
Get a matrix defining a centered circle.
Zero everywhere except for points which have value 1.
'''
return _point_shape(ndimage.distance_transform_edt, radius, spacing)
def divergence(U, V):
'''
Compute the divergence (scalar field) of the gradient vector field.
'''
[Uu, Uv] = np.gradient(U)
[Vu, Vv] = np.gradient(V)
return Uu + Vv
def gradient_magnitude(U, V):
'''
Compute the magnitude (scalar field) of the gradient vector field.
'''
return np.maximum(np.sqrt(U**2 + V**2), eps)
def lsm_grad_magnitude(f, up):
'''
The gradient magnitude using the upwind scheme.
'''
otype = f.dtype.char
if otype not in ['f', 'd', 'F', 'D']:
otype = 'd'
out = np.zeros_like(f).astype(otype)
for i in xrange(out.shape[0]):
if up:
if i > 0: #difference with preceding row
out[i,:] = (out[i,:]) + np.maximum(f[i,:] - f[i - 1,:], 0)**2
else: # difference with next row 'mirrored' outside the matrix
out[i,:] = (out[i,:]) + np.maximum(f[i,:] - f[i + 1,:], 0)**2
if i < out.shape[0] - 1: #difference from next row
out[i,:] = out[i,:] + np.minimum(f[i + 1,:] - f[i,:], 0)**2
else: # difference current row with next row 'mirrored' outside the matrix
out[i,:] = out[i,:] + np.minimum(f[i - 1,:] - f[i,:], 0)**2
else:
if i > 0:
out[i,:] = (out[i,:]) + np.minimum(f[i,:] - f[i - 1,:], 0)**2
else:
out[i,:] = (out[i,:]) + np.minimum(f[i,:] - f[i + 1,:], 0)**2
if i < out.shape[0] - 1:
out[i,:] = out[i,:] + np.maximum(f[i + 1,:] - f[i,:], 0)**2
else:
out[i,:] = out[i,:] + np.maximum(f[i - 1,:] - f[i,:], 0)**2
for j in xrange(out.shape[1]):
if up:
if j > 0:
out[:,j] = out[:,j] + np.maximum(f[:,j] - f[:,j - 1], 0)**2
else:
out[:,j] = out[:,j] + np.maximum(f[:,j] - f[:,j + 1], 0)**2
if j < out.shape[1] - 1:
out[:,j] = out[:,j] + np.minimum(f[:,j + 1] - f[:,j], 0)**2
else:
out[:,j] = out[:,j] + np.minimum(f[:,j - 1] - f[:,j], 0)**2
else:
if j > 0:
out[:,j] = out[:,j] + np.minimum(f[:,j] - f[:,j - 1], 0)**2
else:
out[:,j] = out[:,j] + np.minimum(f[:,j] - f[:,j + 1], 0)**2
if j < out.shape[1] - 1:
out[:,j] = out[:,j] + np.maximum(f[:,j + 1] - f[:,j], 0)**2
else:
out[:,j] = out[:,j] + np.maximum(f[:,j - 1] - f[:,j], 0)**2
return np.sqrt(out)
# S = square(radius=25, spacing=5) # data set of points
S = np.array(Image.open("double1.png"))
S = ndimage.laplace(ndimage.gaussian_filter(S-0.5,1))
S = np.absolute(S < 30)
D = ndimage.distance_transform_edt(S) # distance to data set
[Du, Dv] = np.gradient(D)
# image = np.array(Image.open("cir.png"))
# Phi0 = (image - image.max() / 2) / 255
Phi0 = np.ones(grid_shape)
Phi0[cx, cy] = 0
Phi0 = ndimage.distance_transform_edt(Phi0)
P = Phi0 - 0.65*np.max(Phi0)
# plt.figure()
# plt.imshow(1-S, cmap='gray')
# fig = plt.figure()
# cax = plt.imshow(D)
# fig.colorbar(cax)
# plt.figure()
# plt.imshow(P,cmap='jet')
# plt.contour(P, levels=[0])
def compute_force(U, V, Gmag):
'''
Compute and return the force for the level set evolution.
'''
PF = (Du * U + Dv * V)/Gmag
ST = D * divergence(U/Gmag, V/Gmag)
return PF + ST
# [U, V] = np.gradient(Phi0)
# Gmag = gradient_magnitude(U, V)
# fig = plt.figure()
# im = plt.imshow((Du * U + Dv * V)/Gmag)
# fig.colorbar(im)
# unitF = np.ones(grid_shape)
def compute_stepsize(F):
mf = np.max(np.absolute(F))
safety_factor = 0.88
return safety_factor / mf
def update_phi(P):
'''
Step update (euler method) the level set PDE.
'''
[U, V] = np.gradient(P)
Gmag = gradient_magnitude(U, V)
F = compute_force(U, V, Gmag)
dt = compute_stepsize(F)
return P + dt * F * Gmag
def update_phi_upwind(P):
'''
Step update (euler method) the level set PDE using the upwind scheme.
'''
[U, V] = np.gradient(P)
Gmag = gradient_magnitude(U, V)
F = compute_force(U, V, Gmag)
dt = compute_stepsize(F)
up = lsm_grad_magnitude(P, True)
down = lsm_grad_magnitude(P, False)
return P + dt *(np.maximum(F, 0)*down + np.minimum(F, 0)*up)
fig = plt.figure()
# plt.ion()
# for i in itertools.count():
# if i % 100 == 0:
# P = update_phi(P, dt)
# plt.clf()
# im = plt.imshow(P, cmap='jet')
# plt.contour(P, levels=[0])
# fig.colorbar(im)
# plt.draw()
im = plt.imshow(P, cmap='jet')
fig.colorbar(im)
def save_figure(P, number=99):
with open(str(number)+'p.pickle', 'w') as f:
pickle.dump(skfmm.distance(P), f)
frame = 0
saved_frames = [0, 84, 220, 280]
def updatefig(*args):
global P, frame
for i in xrange(50):
P = update_phi(P)
P = skfmm.distance(P) #reinitialization
im.set_array(P)
# if frame % 50:
# save_figure(P)
frame = frame + 1
return im,
ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True)
plt.show()