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 from numpy import array
36 from math import sqrt
37 import numpy as np
38 import csv
39
40 import pyvision as pv
41
43 - def __init__(self,x=0.0,y=0.0,z=0.0,w=1.0,scale=1.0,rotation=0.0):
44 '''
45 Create a point.
46
47 Arguments:
48 x: x coordinate
49 y: y coordinate
50 z: z coordinate
51 w: homoginious coordinate
52 scale: scale selection
53 rotation: rotation selection
54 '''
55 if isinstance(x,tuple):
56
57 self.x = float(x[0])
58 self.y = float(x[1])
59 self.z = 0.0
60 self.w = 1.0
61 if len(x) > 2:
62 self.z = x[3]
63 if len(x) > 3:
64 self.w = x[4]
65 else:
66
67 self.x = float(x)
68 self.y = float(y)
69 self.z = float(z)
70 self.w = float(w)
71 self.scale = scale
72 self.rotation = rotation
73
75 '''Return the x coordiante.'''
76 return float(self.x)/self.w
77
79 '''Return the y coordinate.'''
80 return float(self.y)/self.w
81
83 '''Return the z coordinate.'''
84 return float(self.z)/self.w
85
86 - def asArray(self,homogenious=False):
87 '''
88 returns the point data as a 4 element numpy array.
89
90 if 'homogenious' == True: returns x,y,z,w
91 else: return x,y,z,1.0
92 '''
93 if homogenious:
94 return array([self.X(),self.Y(),self.Z(),1.0])
95 else:
96 return array([self.X(),self.Y(),self.Z(),1.0])
97
98
100 ''' Return a 2D homogenious vector [x,y,w] '''
101 return array([[self.x],[self.y],[self.w]])
102
104 ''' Return a 3D homogenious vector [x,y,z,w] '''
105 return array([[self.x],[self.y],[self.z],[self.w]])
106
108 '''Return as a point compatible with OpenCV'''
109 return (self.X(), self.Y())
110
112 '''Return as a 2 tuple.'''
113 return (self.X(),self.Y())
114
116 '''Return as an array of three elements.'''
117 return np.array((self.X(),self.Y(),self.Z()))
118
120 '''
121 Computes and returns a representation of this point in spherical coordinates: (r,phi,theta).
122
123 r = radius or distance of the point from the origin.
124 phi = is the angle of the projection on the xy plain and the x axis
125 theta = is the angle with the z axis.
126
127 x = r*cos(phi)*sin(theta)
128 y = r*sin(phi)*sin(theta)
129 z = r*cos(theta)
130 '''
131 x,y,z,_ = self.asArray()
132
133 r = np.sqrt(x**2+y**2+z**2)
134 phi = np.arctan2(y,x)
135 theta = np.arctan2(np.sqrt(x**2+y**2),z)
136
137 return r,phi,theta
138
139 - def l2(self,point):
140 ''' Compute the Euclidian distance between two points. '''
141 dx = self.X()-point.X()
142 dy = self.Y()-point.Y()
143 dz = self.Z()-point.Z()
144
145 return sqrt(dx*dx + dy*dy + dz*dz)
146
148 '''
149 Returns a vector in the same direction but of unit length.
150 '''
151 x = self.X()
152 y = self.Y()
153 z = self.Z()
154 l = np.sqrt(x*x+y*y+z*z)
155 if l < 0.000001:
156
157 return pv.Point(0,0,0)
158
159 return (1.0/l)*self
160
162 ''' Compute the magnitude of the point (distance from origin). '''
163 x = self.X()
164 y = self.Y()
165 z = self.Z()
166
167 return np.sqrt(x**2+y**2+z**2)
168
169
173
177
179 ''' Multiply the point by a value. '''
180 if isinstance(val,float) or isinstance(val,int):
181 return Point(self.X()*val,self.Y()*val,self.Z()*val)
182
184 ''' Multiply the point by a value. '''
185 if isinstance(val,float) or isinstance(val,int):
186 return Point(self.X()*val,self.Y()*val,self.Z()*val)
187
189 ''' Return a string representing the point. '''
190 return "pv.Point(%f,%f,%f)"%(self.X(),self.Y(),self.Z())
191
193 ''' Return a string representing the point. '''
194 return "pv.Point(%f,%f,%f)"%(self.X(),self.Y(),self.Z())
195
196
198 '''
199 This function reads a points file that was created by the EyePicker
200 application. EyePicker produces a csv file where each line corresponds
201 to a file and can contain a number of points.
202
203 This function returns a dictionary where the key is the filename and
204 each entry contains a list of points.
205 '''
206 f = csv.reader(open(filename,'rb'))
207
208 result = {}
209
210 for row in f:
211 fname = row[0]
212
213 row = row[1:]
214
215
216 assert len(row) % 2 == 0
217
218 points = []
219 for i in range(0,len(row),2):
220 x = float(row[i])
221 y = float(row[i+1])
222 points.append(Point(x,y))
223
224 result[fname] = points
225
226 return result
227