-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
252 lines (176 loc) · 5.87 KB
/
util.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
#!/usr/bin/env python2.5
#
# Written (W) 2011-2013 Christian Widmer
# Copyright (C) 2011-2013 Max-Planck-Society, TU-Berlin, MSKCC
"""
@author: Christian Widmer
@summary: Some utility functions for the project, like sampling from an ellipse or ellipsoid
"""
import random
import numpy
import pylab
import math
class Ellipse(object):
"""
ellipse class defining some plotting and sampling procedures
"""
def __init__(self, cx, cy, cz, rx, ry, alpha):
"""
constructor defining fields consisting of
center coordinates, radii and rotation angle
"""
self.cx = cx
self.cy = cy
self.cz = cz
self.rx = rx
self.ry = ry
self.alpha = alpha
def __str__(self):
"""
string representation
"""
return "cx=%.2g, cy=%.2g, cz=%.2g, rx=%.2g, ry=%.2g, alpha=%.2g" % (self.cx, self.cy, self.cz, self.rx, self.ry, self.alpha)
def to_vector(self):
"""
return numpy vector
"""
return numpy.array([self.cx, self.cy, self.cz, self.rx, self.ry, self.alpha])
def plot(self, num_points=100, style="bo"):
"""
plot ellipse by sampling points
"""
self.plot_noshow(num_points, style)
pylab.show()
def plot_noshow(self, num_points=100, style="bo", label=""):
"""
plot ellipse by sampling points
"""
dat_x, dat_y = self.sample_equidistant(num_points)
pylab.plot(dat_x, dat_y, style, label=label)
def sample_equidistant(self, num_points):
"""
sample from ellipsoid with equal angular spacing
"""
theta = numpy.linspace(0, 2 * numpy.pi, num_points + 1)
return self.sample_given_spacing(theta)
def sample_uniform(self, num_points):
"""
sample from ellipsoid with uniformly distributed spacing
"""
theta = [random.uniform(0, 2 * numpy.pi) for _ in xrange(num_points + 1)]
return self.sample_given_spacing(theta)
def sample_given_spacing(self, theta):
"""
sample points from ellipse given set of angles
center: xc, yc
radii: xr, yr
rotation: alpha
X = Z + Q(ALPHA) * [A * cos(theta); B * sin(theta)]
where Q(ALPHA) is the rotation matrix
Q(ALPHA) = [cos(ALPHA), -sin(ALPHA);
sin(ALPHA), cos(ALPHA)]
"""
# set up points
points = numpy.zeros((2, len(theta)))
points[0, :] = self.rx * numpy.cos(theta)
points[1, :] = self.ry * numpy.sin(theta)
## get initial rotation matrix
rot = numpy.array( [[ numpy.cos(self.alpha), -numpy.sin(self.alpha) ],
[ numpy.sin(self.alpha), numpy.cos(self.alpha) ]])
## center
center = numpy.zeros((2, 1))
center[0] = self.cx
center[1] = self.cy
# perform rotation
dat = numpy.dot(rot , points) + center
return dat
def conic_to_ellipse(theta, use_rotation=False):
"""
convert theta parameterization to Ellipse object
"""
#TODO support rotation
## For clarity, fill in the quadratic form variables
#A = numpy.zeros((2,2))
#A[0,0] = theta[0]
#A.ravel()[1:3] = theta[2]
#A[1,1] = theta[1]
#bv = theta[3:5]
#c = theta[5]
A = numpy.zeros((2,2))
A[0,0] = theta[0]
A.ravel()[1:3] = 0 #theta[2]
A[1,1] = theta[1]
bv = theta[2:4]
c = theta[4]
## find parameters
z, a, b, alpha = conic2parametric(A, bv, c)
return Ellipse(float(z[0]), float(z[1]), 0, float(a), float(b), float(alpha))
def conic2parametric(A, bv, c):
"""
convert conic parameterization to standard ellipse parameterization
"""
## Diagonalise A - find Q, D such at A = Q' * D * Q
D, Q = numpy.linalg.eig(A)
Q = Q.T
## If the determinant < 0, it's not an ellipse
if numpy.prod(D) <= 0:
raise RuntimeError, 'fitellipse:NotEllipse Linear fit did not produce an ellipse'
## We have b_h' = 2 * t' * A + b'
t = -0.5 * numpy.linalg.solve(A, bv)
c_h = numpy.dot( numpy.dot( t.T, A ), t ) + numpy.dot( bv.T, t ) + c
z = t
a = numpy.sqrt(-c_h / D[0])
b = numpy.sqrt(-c_h / D[1])
alpha = math.atan2(Q[0,1], Q[0,0])
return z, a, b, alpha
def ellipsoid(xc, yc, zc, xr, yr, zr, n=200):
"""
sample points from ellipsoid
note: does not support rotation
"""
pi = numpy.pi
theta = numpy.linspace (0, 2 * pi, n + 1);
phi = numpy.linspace (-pi / 2, pi / 2, n + 1);
[theta, phi] = numpy.meshgrid (theta, phi);
lx = xr * numpy.cos(phi) * numpy.cos(theta) + xc;
ly = yr * numpy.cos(phi) * numpy.sin(theta) + yc;
lz = zr * numpy.sin(phi) + zc;
return lx.flatten(), ly.flatten(), lz.flatten()
def plot_point_cloud(x,y,z):
"""
plot 3d point cloud
"""
import mpl_toolkits.mplot3d.axes3d as p3
fig = pylab.figure()
ax = p3.Axes3D(fig)
ax.scatter(x,y,z)
pylab.show()
def plot_ellipse(cx, cy, cz, rx, ry, alpha, figure):
"""
plot ellipse stack
"""
from mayavi import mlab
n = 50
dat = Ellipse(cx, cy, rx, ry, alpha).sample_uniform(n)
#dat = ellipse(e.cx, e.cy, e.rx, e.ry, e.alpha, n)
dx = dat[0]
dy = dat[1]
dz = [cz]*(n+1)
dv = [25]*(n+1)
return mlab.plot3d(dx,dy,dz,dv,
#mode='2dcircle',
#mode='point',
color=(0, 0, 0),
#scale_factor=100*max(self.data.shape),
figure=figure,
line_width=20,
tube_radius=None
)
def main():
"""
main
"""
ellipse = Ellipse(1, 1, 0, 2, 3, 0)
ellipse.plot()
if __name__ == "__main__":
main()