00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 import numpy as np
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 def gaussian_curvature(points, p=np.matrix([0,0,1]).T):
00041 assert(points.shape[0] == 3)
00042 if points.shape[1] < 2:
00043 return np.matrix([0, 0, 0.0]).T, np.array([0, 0, 0.0])
00044 c = np.cov(points)
00045 try:
00046 u, s, vh = np.linalg.svd(c)
00047 u = np.matrix(u)
00048
00049
00050 if u[:,2].T * p < 0:
00051 return -u[:,2], s
00052 else:
00053 return u[:,2], s
00054 except np.linalg.linalg.LinAlgError, e:
00055 print e
00056 print 'oh no!!!!!! why is this happening?'
00057 print points.shape
00058 print c
00059 print points
00060
00061 def spread(points):
00062 assert(points.shape[0] == 3)
00063 if points.shape[1] < 2:
00064 return np.matrix([0, 0, 0.0]).T, np.array([0, 0, 0.0])
00065
00066 c = np.cov(points)
00067 u, s, vh = np.linalg.svd(c)
00068 u = np.matrix(u)
00069 return u[:,0:2], s[0:2]
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 def gaussian_curvature2(points, x=np.matrix([1,0,0]).T, z=np.matrix([0,0,1]).T):
00080 assert(points.shape[0] == 3)
00081
00082 c = np.cov(points)
00083 u, s, vh = np.linalg.svd(c)
00084 u = np.matrix(u)
00085
00086 choices = range(3)
00087
00088 z_distances = [np.degrees(np.arccos(np.abs(z.T * u[:,i])))[0,0] for i in choices]
00089 z_choice = choices[np.argmin(z_distances)]
00090
00091
00092 choices.remove(z_choice)
00093
00094 x_distances = [np.degrees(np.arccos(np.abs(x.T * u[:,i])))[0,0] for i in choices]
00095 x_choice = choices[np.argmin(x_distances)]
00096
00097
00098 choices.remove(x_choice)
00099
00100
00101 xv = u[:,x_choice]
00102 xs = s[x_choice]
00103
00104 ys = s[choices[0]]
00105
00106 zv = u[:,z_choice]
00107 zs = s[z_choice]
00108
00109
00110 if np.degrees(np.arccos(xv.T * x)) > 90.0:
00111 xv = -xv
00112 if np.degrees(np.arccos(zv.T * z)) > 90.0:
00113 zv = -zv
00114
00115
00116 yv = np.matrix(np.cross(xv.T, zv.T)).T
00117 directions = np.concatenate([xv, yv, zv], axis=1)
00118 sd = [xs, ys, zs]
00119 if np.linalg.det(directions) < 0:
00120 directions[:,1] = -directions[:,1]
00121 assert np.linalg.det(directions) > 0
00122 return directions, sd
00123
00124
00125 def plot_axis(x,y, z, directions):
00126 from enthought.mayavi import mlab
00127 mlab.quiver3d(x, y, z, [directions[0,0]], [directions[1,0]], [directions[2,0]], scale_factor=1, color=(1,0,0))
00128 if directions.shape[1] > 1:
00129 mlab.quiver3d(x, y, z, [directions[0,1]], [directions[1,1]], [directions[2,1]], scale_factor=1, color=(0,1,0))
00130 mlab.quiver3d(x, y, z, [directions[0,2]], [directions[1,2]], [directions[2,2]], scale_factor=1, color=(0,0,1))
00131
00132 def generate_pts():
00133
00134 radius = np.random.random((1, 2000))
00135 angle = np.random.random((1,2000)) * 2*np.pi
00136 x = radius * np.cos(angle)
00137 y = radius * np.sin(angle)
00138 r = np.concatenate((x,y))
00139 r[0,:] = r[0,:] * 2
00140 xy_plane = np.matrix(np.eye(3))[:,(0, 2)]
00141 pts = xy_plane * r
00142 return pts
00143
00144
00145 def demo1():
00146 from enthought.mayavi import mlab
00147 pts = generate_pts()
00148 directions, magnitudes = gaussian_curvature(pts)
00149 print directions.T.A[0].tolist()
00150
00151 print magnitudes.tolist()
00152 mlab.points3d(pts[0,:].A1, pts[1,:].A1, pts[2,:].A1, mode='sphere', scale_factor=.05)
00153 plot_axis(0,0,0, directions)
00154 plot_axis(2,0,0, np.eye(3))
00155 mlab.show()
00156
00157 def demo2():
00158 from enthought.mayavi import mlab
00159 pts = generate_pts()
00160 direction, magnitudes = gaussian_curvature(pts)
00161 print 'eigen values', magnitudes.T
00162 mlab.points3d(pts[0,:].A1, pts[1,:].A1, pts[2,:].A1, mode='sphere', scale_factor=.05)
00163 plot_axis(0,0,0, direction)
00164 plot_axis(2,0,0, np.eye(3))
00165 mlab.show()
00166
00167 if __name__ == '__main__':
00168 demo1()
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255