/
fourier_obstacle.py
297 lines (239 loc) · 6.98 KB
/
fourier_obstacle.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
295
296
'''
Fourier obstacle for collision detection
-Mikola
'''
from scipy import array, exp, pi, sin, cos, arange, conjugate, real, sqrt, zeros
from math import atan2
from numpy import ndenumerate, dot
from scipy.linalg import norm
from scipy.misc import imshow, imrotate
from scipy.signal import fftconvolve
import scipy.ndimage as ndi
import scipy.fftpack.pseudo_diffs as pds
from misc import to_ind, cpad
from polar import pfft, ipfft, pft_mult, pft_rotate
from se2 import se2
'''
Computes the best cutoff for the given indicator function
'''
def best_cutoff(ift, pind, radius):
pvalues = []
for x,v in ndenumerate(ift):
if(norm(array(x) - array(pind.shape) / 2.) <= radius):
pvalues.append( (v, pind[x[0], x[1]]) )
pvalues.sort()
lmiss = zeros((len(pvalues)))
umiss = zeros((len(pvalues)))
l = 0
u = 0
for k in range(len(pvalues)):
if(pvalues[len(pvalues) - k - 1][1] > 0):
u += 1
if(pvalues[k][1] == 0):
l += 1
lmiss[k] = l
umiss[len(pvalues) - k - 1] = u
descr = 3. * lmiss + umiss
best_descr = descr[0]
best_cutoff = 0.
for k in range(len(pvalues)):
if(descr[k] > best_descr):
best_cutoff = pvalues[k][0]
best_descr = descr[k]
return best_cutoff
'''
Shape storage class
'''
class Shape:
'''
Initializes a shape object
'''
def __init__(self, mass_field, R, SHAPE_R):
#Set basic parameters
self.R = R
self.SHAPE_R = SHAPE_R
self.mass_field = mass_field
self.mass = sum(mass_field.flatten())
#Recenter shape so that its center of mass is at the center of image
center = array(ndi.center_of_mass(mass_field))
offset = array(mass_field.shape)/2 - center
nshape = (array(mass_field.shape) + 2. * abs(offset)).round()
tmp = cpad(mass_field, nshape)
self.mass_field = ndi.shift(tmp, offset, order=1)
self.center = array(self.mass_field.shape) / 2
assert(self.mass_field.shape[0] <= SHAPE_R)
assert(self.mass_field.shape[1] <= SHAPE_R)
#Set indicator/shape area
self.indicator = to_ind(self.mass_field, 0.01)
self.area = sum(self.indicator.flatten())
#Compute moment of inertia and radius
self.moment = 0.
self.radius = 0.
for x,p in ndenumerate(self.mass_field):
r = array(x) - self.center
self.moment += p * dot(r,r)
if(p > 0.01):
self.radius = max(self.radius, norm(r))
#Set shape indicator
self.shape_num = -1
#Compute polar fourier truncation of indicator
pind = cpad(self.indicator, array([2*SHAPE_R+1,2*SHAPE_R+1]))
self.pft = pfft(pind, R)
ift = real(ipfft(self.pft, pind.shape[0], pind.shape[1]))
self.pft[0][0] -= min(ift.flatten()) * ((2. * SHAPE_R + 1) ** 2) #Enforce positivity
self.pdft = map(pds.diff, self.pft)
#Compute cutoff parameters
ift = real(ipfft(self.pft, pind.shape[0], pind.shape[1]))
self.cutoff = best_cutoff(ift, pind, self.radius)
ind_ift = to_ind(ift, self.cutoff)
self.int_res = sum((ift * ind_ift).flatten())
self.ext_res = sum((ift * (1. - ind_ift)).flatten())
self.res_area = sum(ind_ift.flatten())
imshow(pind)
imshow(to_ind(ift, self.cutoff))
#Compute residual energy terms
self.energy = []
s = real(self.pft[0][0]) * pi
for r,l in enumerate(self.pft):
s += sum(abs(self.pft[r])) * (r * 2. * pi / len(self.pft[r]))
self.energy.append(s)
self.total_energy = s
for r,e in enumerate(self.energy):
self.energy[r] = s - self.energy[r]
'''
The shape/obstacle data base
'''
class ShapeSet:
'''
Initializes the shape database
'''
def __init__(self, R, SHAPE_R):
self.shape_list = []
self.cutoff_matrix = []
self.R = R
self.SHAPE_R = SHAPE_R
self.tarea = ((2. * self.SHAPE_R + 1)**2)
'''
Adds a shape to the obstacle set
'''
def add_shape(self, f):
#Create shape
S = Shape(f, self.R, self.SHAPE_R)
#Add to shape list
S.shape_num = len(self.shape_list)
self.shape_list.append(S)
row = []
for k in range(len(self.shape_list)):
T = self.shape_list[k]
ift = real(ipfft(pft_mult(pft_rotate(S.pft, 2.*pi/6.), T.pft), 2*self.SHAPE_R+1,2*self.SHAPE_R+1))
Spad = imrotate(cpad(S.indicator, array([2*self.SHAPE_R+1,2*self.SHAPE_R+1])), 360./6.)
Tpad = cpad(T.indicator, array([2*self.SHAPE_R+1,2*self.SHAPE_R+1]))
pind = real(fftconvolve(Spad, Tpad, mode='same'))
imshow(pind)
imshow(ift)
obst = to_ind(pind, 0.001)
imshow(obst)
cutoff = best_cutoff(ift, obst, S.radius + T.radius)
print cutoff
imshow(to_ind(ift, cutoff))
row.append(cutoff * self.tarea)
self.cutoff_matrix.append(row)
return S
'''
Returns the cutoff threshold for shapes A,B
'''
def __get_cutoff(self, A, B):
i = A.shape_num
j = B.shape_num
if(i < j):
t = i
i = j
j = t
C = self.cutoff_matrix[i][j]
return C
'''
Evaluates shape potential field
Not actually useful for collision detection, but somewhat helpful for debugging purposes.
'''
'''
def potential(self, A, B, pa, pb, ra, rb):
#Compute relative transformation
ca = se2(pa, ra)
cb = se2(pb, rb)
rel = ca.inv() * cb
pr = norm(rel.x)
if(pr >= A.radius + B.radius ):
return 0.
#Load shape parameters
fa = A.pft
fb = B.pft
ea = A.energy
eb = B.energy
cutoff = self.__get_cutoff(A,B)
#Compute coordinate coefficients
m = 2.j * pi / (2. * self.SHAPE_R + 1) * pr
phi = atan2(rel.x[1], rel.x[0])
#Sum up energy contributions
s_0 = real(fa[0][0] * fb[0][0] * pi)
for r in range(1, self.R):
#Compute theta terms
rscale = 2. * pi / len(fa[r])
theta = arange(len(fa[r])) * rscale
#Compute energy at this ring
v = pds.shift(fb[r], rel.theta) * fa[r] * exp((m * r) * cos(theta + phi)) * r * rscale
#Check for early out
s_0 += sum(real(v))
if(s_0 + min(ea[r], eb[r]) <= cutoff):
return 0.
if(s_0 <= cutoff):
return 0.
return s_0
'''
'''
Gradient calculation for shape field
A, B - Shapes for the solids
q - Relative transformation
'''
def grad(self, A, B, q):
#Compute relative transformation
pr = norm(q.x)
if(pr >= A.radius + B.radius ):
return array([0., 0., 0., 0.])
#Load shape parameters
fa = A.pft
fb = B.pft
da = A.pdft
db = B.pdft
ea = A.energy
eb = B.energy
#Estimate cutoff threshold
cutoff = self.__get_cutoff(A, B)
#Compute coordinate coefficients
m = 2.j * pi / (2. * self.SHAPE_R + 1) * pr
phi = atan2(q.x[1], q.x[0])
#Set up initial sums
s_0 = real(fa[0][0] * fb[0][0])
s_x = 0.
s_y = 0.
s_ta = 0.
s_tb = 0.
for r in range(1, self.R):
#Compute theta terms
dtheta = 2. * pi / len(fa[r])
theta = arange(len(fa[r])) * dtheta
#Construct multiplier / v
mult = exp((m * r) * cos(theta + phi)) * r * dtheta
u = pds.shift(conjugate(fb[r]), q.theta) * mult
v = fa[r] * u
#Check for early out
s_0 += sum(real(v))
if(s_0 + min(ea[r], eb[r]) <= cutoff):
return array([0.,0.,0.,0.])
#Sum up gradient vectors
v = real(1.j * v)
s_x -= sum(v * sin(theta + phi) )
s_y -= sum(v * cos(theta + phi) )
s_t += sum(real(da[r] * u))
if(s_0 <= cutoff):
return array([0., 0., 0., 0.])
return array([s_x, s_y, s_ta, s_tb, s_0])