Package pyvision :: Package types :: Module Point
[hide private]
[frames] | no frames]

Source Code for Module pyvision.types.Point

  1  # PyVision License 
  2  # 
  3  # Copyright (c) 2006-2008 David S. Bolme 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  #  
 10  # 1. Redistributions of source code must retain the above copyright 
 11  # notice, this list of conditions and the following disclaimer. 
 12  #  
 13  # 2. Redistributions in binary form must reproduce the above copyright 
 14  # notice, this list of conditions and the following disclaimer in the 
 15  # documentation and/or other materials provided with the distribution. 
 16  #  
 17  # 3. Neither name of copyright holders nor the names of its contributors 
 18  # may be used to endorse or promote products derived from this software 
 19  # without specific prior written permission. 
 20  #  
 21  #  
 22  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 23  # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 24  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 25  # A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR 
 26  # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 27  # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 28  # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 29  # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 30  # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 31  # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 32  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 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   
42 -class Point:
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 # Initialize from a tuple 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 # Initialize from coordinates. 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
74 - def X(self):
75 '''Return the x coordiante.''' 76 return float(self.x)/self.w
77
78 - def Y(self):
79 '''Return the y coordinate.''' 80 return float(self.y)/self.w
81
82 - def Z(self):
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
99 - def asVector2H(self):
100 ''' Return a 2D homogenious vector [x,y,w] ''' 101 return array([[self.x],[self.y],[self.w]])
102
103 - def asVector3H(self):
104 ''' Return a 3D homogenious vector [x,y,z,w] ''' 105 return array([[self.x],[self.y],[self.z],[self.w]])
106
107 - def asOpenCV(self):
108 '''Return as a point compatible with OpenCV''' 109 return (self.X(), self.Y()) #cv.cvPoint(int(round(self.X())),int(round(self.Y())))
110
111 - def asTuple(self):
112 '''Return as a 2 tuple.''' 113 return (self.X(),self.Y())
114
115 - def asArray3D(self):
116 '''Return as an array of three elements.''' 117 return np.array((self.X(),self.Y(),self.Z()))
118
119 - def asSpherical(self):
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
147 - def unit(self):
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 # Point is at 0,0,0 157 return pv.Point(0,0,0) 158 159 return (1.0/l)*self
160
161 - def magnitude(self):
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
170 - def __sub__(self,point):
171 ''' Subtract two points ''' 172 return Point(self.X()-point.X(),self.Y()-point.Y(),self.Z()-point.Z())
173
174 - def __add__(self,point):
175 ''' Add two points. ''' 176 return Point(self.X()+point.X(),self.Y()+point.Y(),self.Z()+point.Z())
177
178 - def __mul__(self,val):
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
183 - def __rmul__(self,val):
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
188 - def __str__(self):
189 ''' Return a string representing the point. ''' 190 return "pv.Point(%f,%f,%f)"%(self.X(),self.Y(),self.Z())
191
192 - def __repr__(self):
193 ''' Return a string representing the point. ''' 194 return "pv.Point(%f,%f,%f)"%(self.X(),self.Y(),self.Z())
195 196
197 -def readPointsFile(filename):
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 # Make sure the number of values is even 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