Package fabmetheus_utilities :: Module vector3
[hide private]
[frames] | no frames]

Source Code for Module fabmetheus_utilities.vector3

  1  """ 
  2  Vector3 is a three dimensional vector class. 
  3   
  4  Below are examples of Vector3 use. 
  5   
  6  >>> from vector3 import Vector3 
  7  >>> origin = Vector3() 
  8  >>> origin 
  9  0.0, 0.0, 0.0 
 10  >>> pythagoras = Vector3( 3, 4, 0 ) 
 11  >>> pythagoras 
 12  3.0, 4.0, 0.0 
 13  >>> pythagoras.magnitude() 
 14  5.0 
 15  >>> pythagoras.magnitudeSquared() 
 16  25 
 17  >>> triplePythagoras = pythagoras * 3.0 
 18  >>> triplePythagoras 
 19  9.0, 12.0, 0.0 
 20  >>> plane = pythagoras.dropAxis() 
 21  >>> plane 
 22  (3+4j) 
 23  """ 
 24   
 25  from fabmetheus_utilities import xml_simple_writer 
 26  import math 
 27  import operator 
 28  import sys 
 29   
 30  __author__ = 'Enrique Perez (perez_enrique@yahoo.com)' 
 31  __credits__ = 'Nophead <http://forums.reprap.org/profile.php?12,28>\nArt of Illusion <http://www.artofillusion.org/>' 
 32  __date__ = '$Date: 2008/21/04 $' 
 33  __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html' 
 34   
 35   
36 -class Vector3:
37 'A three dimensional vector class.' 38 __slots__ = ['x', 'y', 'z'] 39
40 - def __init__(self, x=0.0, y=0.0, z=0.0):
41 self.x = x 42 self.y = y 43 self.z = z
44
45 - def __abs__(self):
46 'Get the magnitude of the Vector3.' 47 return math.sqrt( self.x * self.x + self.y * self.y + self.z * self.z )
48 49 magnitude = __abs__ 50
51 - def __add__(self, other):
52 'Get the sum of this Vector3 and other one.' 53 return Vector3( self.x + other.x, self.y + other.y, self.z + other.z )
54
55 - def __copy__(self):
56 'Get the copy of this Vector3.' 57 return Vector3( self.x, self.y, self.z )
58 59 __pos__ = __copy__ 60 61 copy = __copy__ 62
63 - def __div__(self, other):
64 'Get a new Vector3 by dividing each component of this one.' 65 return Vector3( self.x / other, self.y / other, self.z / other )
66
67 - def __eq__(self, other):
68 'Determine whether this vector is identical to other one.' 69 if other == None: 70 return False 71 if other.__class__ != self.__class__: 72 return False 73 return self.x == other.x and self.y == other.y and self.z == other.z
74
75 - def __floordiv__(self, other):
76 'Get a new Vector3 by floor dividing each component of this one.' 77 return Vector3( self.x // other, self.y // other, self.z // other )
78
79 - def _getAccessibleAttribute(self, attributeName):
80 'Get the accessible attribute.' 81 if attributeName in globalGetAccessibleAttributeSet: 82 return getattr(self, attributeName, None) 83 return None
84
85 - def __hash__(self):
86 'Determine whether this vector is identical to other one.' 87 return self.__repr__().__hash__()
88
89 - def __iadd__(self, other):
90 'Add other Vector3 to this one.' 91 self.x += other.x 92 self.y += other.y 93 self.z += other.z 94 return self
95
96 - def __idiv__(self, other):
97 'Divide each component of this Vector3.' 98 self.x /= other 99 self.y /= other 100 self.z /= other 101 return self
102
103 - def __ifloordiv__(self, other):
104 'Floor divide each component of this Vector3.' 105 self.x //= other 106 self.y //= other 107 self.z //= other 108 return self
109
110 - def __imul__(self, other):
111 'Multiply each component of this Vector3.' 112 self.x *= other 113 self.y *= other 114 self.z *= other 115 return self
116
117 - def __isub__(self, other):
118 'Subtract other Vector3 from this one.' 119 self.x -= other.x 120 self.y -= other.y 121 self.z -= other.z 122 return self
123
124 - def __itruediv__(self, other):
125 'True divide each component of this Vector3.' 126 self.x = operator.truediv( self.x, other ) 127 self.y = operator.truediv( self.y, other ) 128 self.z = operator.truediv( self.z, other ) 129 return self
130
131 - def __mul__(self, other):
132 'Get a new Vector3 by multiplying each component of this one.' 133 return Vector3( self.x * other, self.y * other, self.z * other )
134
135 - def __ne__(self, other):
136 'Determine whether this vector is not identical to other one.' 137 return not self.__eq__(other)
138
139 - def __neg__(self):
140 return Vector3( - self.x, - self.y, - self.z )
141
142 - def __nonzero__(self):
143 return self.x != 0 or self.y != 0 or self.z != 0
144
145 - def __repr__(self):
146 'Get the string representation of this Vector3.' 147 return '(%s, %s, %s)' % ( self.x, self.y, self.z )
148
149 - def __rdiv__(self, other):
150 'Get a new Vector3 by dividing each component of this one.' 151 return Vector3( other / self.x, other / self.y, other / self.z )
152
153 - def __rfloordiv__(self, other):
154 'Get a new Vector3 by floor dividing each component of this one.' 155 return Vector3( other // self.x, other // self.y, other // self.z )
156
157 - def __rmul__(self, other):
158 'Get a new Vector3 by multiplying each component of this one.' 159 return Vector3( self.x * other, self.y * other, self.z * other )
160
161 - def __rtruediv__(self, other):
162 'Get a new Vector3 by true dividing each component of this one.' 163 return Vector3( operator.truediv( other , self.x ), operator.truediv( other, self.y ), operator.truediv( other, self.z ) )
164
165 - def _setAccessibleAttribute(self, attributeName, value):
166 'Set the accessible attribute.' 167 if attributeName in globalSetAccessibleAttributeSet: 168 setattr(self, attributeName, value)
169
170 - def __sub__(self, other):
171 'Get the difference between the Vector3 and other one.' 172 return Vector3( self.x - other.x, self.y - other.y, self.z - other.z )
173
174 - def __truediv__(self, other):
175 'Get a new Vector3 by true dividing each component of this one.' 176 return Vector3( operator.truediv( self.x, other ), operator.truediv( self.y, other ), operator.truediv( self.z, other ) )
177
178 - def cross(self, other):
179 'Calculate the cross product of this vector with other one.' 180 return Vector3(self.y * other.z - self.z * other.y, -self.x * other.z + self.z * other.x, self.x * other.y - self.y * other.x)
181
182 - def distance(self, other):
183 'Get the Euclidean distance between this vector and other one.' 184 return math.sqrt( self.distanceSquared(other) )
185
186 - def distanceSquared(self, other):
187 'Get the square of the Euclidean distance between this vector and other one.' 188 separationX = self.x - other.x 189 separationY = self.y - other.y 190 separationZ = self.z - other.z 191 return separationX * separationX + separationY * separationY + separationZ * separationZ
192
193 - def dot(self, other):
194 'Calculate the dot product of this vector with other one.' 195 return self.x * other.x + self.y * other.y + self.z * other.z
196
197 - def dropAxis( self, which = 2 ):
198 'Get a complex by removing one axis of the vector3.' 199 if which == 0: 200 return complex( self.y, self.z ) 201 if which == 1: 202 return complex( self.x, self.z ) 203 if which == 2: 204 return complex( self.x, self.y )
205
206 - def getFloatList(self):
207 'Get the vector as a list of floats.' 208 return [ float( self.x ), float( self.y ), float( self.z ) ]
209
210 - def getIsDefault(self):
211 'Determine if this is the zero vector.' 212 if self.x != 0.0: 213 return False 214 if self.y != 0.0: 215 return False 216 return self.z == 0.0
217
218 - def getNormalized(self):
219 'Get the normalized Vector3.' 220 magnitude = abs(self) 221 if magnitude == 0.0: 222 return self.copy() 223 return self / magnitude
224
225 - def magnitudeSquared(self):
226 'Get the square of the magnitude of the Vector3.' 227 return self.x * self.x + self.y * self.y + self.z * self.z
228
229 - def maximize(self, other):
230 'Maximize the Vector3.' 231 self.x =max(other.x, self.x) 232 self.y =max(other.y, self.y) 233 self.z =max(other.z, self.z)
234
235 - def minimize(self, other):
236 'Minimize the Vector3.' 237 self.x =min(other.x, self.x) 238 self.y =min(other.y, self.y) 239 self.z =min(other.z, self.z)
240
241 - def normalize(self):
242 'Scale each component of this Vector3 so that it has a magnitude of 1. If this Vector3 has a magnitude of 0, this method has no effect.' 243 magnitude = abs(self) 244 if magnitude != 0.0: 245 self /= magnitude
246
247 - def reflect( self, normal ):
248 'Reflect the Vector3 across the normal, which is assumed to be normalized.' 249 distance = 2 * ( self.x * normal.x + self.y * normal.y + self.z * normal.z ) 250 return Vector3( self.x - distance * normal.x, self.y - distance * normal.y, self.z - distance * normal.z )
251
252 - def setToVector3(self, other):
253 'Set this Vector3 to be identical to other one.' 254 self.x = other.x 255 self.y = other.y 256 self.z = other.z
257
258 - def setToXYZ( self, x, y, z ):
259 'Set the x, y, and z components of this Vector3.' 260 self.x = x 261 self.y = y 262 self.z = z
263 264 265 globalGetAccessibleAttributeSet = 'x y z'.split() 266 globalSetAccessibleAttributeSet = globalGetAccessibleAttributeSet 267