2 Vector3 is a three dimensional vector class.
4 Below are examples of Vector3 use.
6 >>> from vector3 import Vector3
10 >>> pythagoras = Vector3( 3, 4, 0 )
13 >>> pythagoras.magnitude()
15 >>> pythagoras.magnitudeSquared()
17 >>> triplePythagoras = pythagoras * 3.0
20 >>> plane = pythagoras.dropAxis()
25 from __future__ import absolute_import
31 #Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
34 from fabmetheus_utilities import xml_simple_writer
39 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
40 __credits__ = 'Nophead <http://forums.reprap.org/profile.php?12,28>\nArt of Illusion <http://www.artofillusion.org/>'
41 __date__ = '$Date: 2008/21/04 $'
42 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
46 'A three dimensional vector class.'
47 __slots__ = ['x', 'y', 'z']
49 def __init__(self, x=0.0, y=0.0, z=0.0):
55 'Get the magnitude of the Vector3.'
56 return math.sqrt( self.x * self.x + self.y * self.y + self.z * self.z )
60 def __add__(self, other):
61 'Get the sum of this Vector3 and other one.'
62 return Vector3( self.x + other.x, self.y + other.y, self.z + other.z )
65 'Get the copy of this Vector3.'
66 return Vector3( self.x, self.y, self.z )
72 def __div__(self, other):
73 'Get a new Vector3 by dividing each component of this one.'
74 return Vector3( self.x / other, self.y / other, self.z / other )
76 def __eq__(self, other):
77 'Determine whether this vector is identical to other one.'
80 if other.__class__ != self.__class__:
82 return self.x == other.x and self.y == other.y and self.z == other.z
84 def __floordiv__(self, other):
85 'Get a new Vector3 by floor dividing each component of this one.'
86 return Vector3( self.x // other, self.y // other, self.z // other )
89 'Determine whether this vector is identical to other one.'
90 return self.__repr__().__hash__()
92 def __iadd__(self, other):
93 'Add other Vector3 to this one.'
99 def __idiv__(self, other):
100 'Divide each component of this Vector3.'
106 def __ifloordiv__(self, other):
107 'Floor divide each component of this Vector3.'
113 def __imul__(self, other):
114 'Multiply each component of this Vector3.'
120 def __isub__(self, other):
121 'Subtract other Vector3 from this one.'
127 def __itruediv__(self, other):
128 'True divide each component of this Vector3.'
129 self.x = operator.truediv( self.x, other )
130 self.y = operator.truediv( self.y, other )
131 self.z = operator.truediv( self.z, other )
134 def __mul__(self, other):
135 'Get a new Vector3 by multiplying each component of this one.'
136 return Vector3( self.x * other, self.y * other, self.z * other )
138 def __ne__(self, other):
139 'Determine whether this vector is not identical to other one.'
140 return not self.__eq__(other)
143 return Vector3( - self.x, - self.y, - self.z )
145 def __nonzero__(self):
146 return self.x != 0 or self.y != 0 or self.z != 0
148 def __rdiv__(self, other):
149 'Get a new Vector3 by dividing each component of this one.'
150 return Vector3( other / self.x, other / self.y, other / self.z )
153 'Get the string representation of this Vector3.'
154 return '(%s, %s, %s)' % ( self.x, self.y, self.z )
156 def __rfloordiv__(self, other):
157 'Get a new Vector3 by floor dividing each component of this one.'
158 return Vector3( other // self.x, other // self.y, other // self.z )
160 def __rmul__(self, other):
161 'Get a new Vector3 by multiplying each component of this one.'
162 return Vector3( self.x * other, self.y * other, self.z * other )
164 def __rtruediv__(self, other):
165 'Get a new Vector3 by true dividing each component of this one.'
166 return Vector3( operator.truediv( other , self.x ), operator.truediv( other, self.y ), operator.truediv( other, self.z ) )
168 def __sub__(self, other):
169 'Get the difference between the Vector3 and other one.'
170 return Vector3( self.x - other.x, self.y - other.y, self.z - other.z )
172 def __truediv__(self, other):
173 'Get a new Vector3 by true dividing each component of this one.'
174 return Vector3( operator.truediv( self.x, other ), operator.truediv( self.y, other ), operator.truediv( self.z, other ) )
176 def _getAccessibleAttribute(self, attributeName):
177 'Get the accessible attribute.'
178 if attributeName in globalGetAccessibleAttributeSet:
179 return getattr(self, attributeName, None)
182 def _setAccessibleAttribute(self, attributeName, value):
183 'Set the accessible attribute.'
184 if attributeName in globalSetAccessibleAttributeSet:
185 setattr(self, attributeName, value)
187 def cross(self, other):
188 'Calculate the cross product of this vector with other one.'
189 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)
191 def distance(self, other):
192 'Get the Euclidean distance between this vector and other one.'
193 return math.sqrt( self.distanceSquared(other) )
195 def distanceSquared(self, other):
196 'Get the square of the Euclidean distance between this vector and other one.'
197 separationX = self.x - other.x
198 separationY = self.y - other.y
199 separationZ = self.z - other.z
200 return separationX * separationX + separationY * separationY + separationZ * separationZ
202 def dot(self, other):
203 'Calculate the dot product of this vector with other one.'
204 return self.x * other.x + self.y * other.y + self.z * other.z
206 def dropAxis( self, which = 2 ):
207 'Get a complex by removing one axis of the vector3.'
209 return complex( self.y, self.z )
211 return complex( self.x, self.z )
213 return complex( self.x, self.y )
215 def getFloatList(self):
216 'Get the vector as a list of floats.'
217 return [ float( self.x ), float( self.y ), float( self.z ) ]
219 def getIsDefault(self):
220 'Determine if this is the zero vector.'
227 def getNormalized(self):
228 'Get the normalized Vector3.'
229 magnitude = abs(self)
232 return self / magnitude
234 def magnitudeSquared(self):
235 'Get the square of the magnitude of the Vector3.'
236 return self.x * self.x + self.y * self.y + self.z * self.z
238 def maximize(self, other):
239 'Maximize the Vector3.'
240 self.x = max(other.x, self.x)
241 self.y = max(other.y, self.y)
242 self.z = max(other.z, self.z)
244 def minimize(self, other):
245 'Minimize the Vector3.'
246 self.x = min(other.x, self.x)
247 self.y = min(other.y, self.y)
248 self.z = min(other.z, self.z)
251 '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.'
252 magnitude = abs(self)
256 def reflect( self, normal ):
257 'Reflect the Vector3 across the normal, which is assumed to be normalized.'
258 distance = 2 * ( self.x * normal.x + self.y * normal.y + self.z * normal.z )
259 return Vector3( self.x - distance * normal.x, self.y - distance * normal.y, self.z - distance * normal.z )
261 def setToVector3(self, other):
262 'Set this Vector3 to be identical to other one.'
267 def setToXYZ( self, x, y, z ):
268 'Set the x, y, and z components of this Vector3.'
274 globalGetAccessibleAttributeSet = 'x y z'.split()
275 globalSetAccessibleAttributeSet = globalGetAccessibleAttributeSet
279 __slots__ = ['x', 'y', 'z']
284 def __eq__(self, other):
285 if isinstance(other, Vector3):
286 return self.x == other.x and \
287 self.y == other.y and \
290 assert hasattr(other, '__len__') and len(other) == 3
291 return self.x == other[0] and \
292 self.y == other[1] and \
295 def __getattr__(self, name):
297 return tuple([(self.x, self.y, self.z)['xyz'.index(c)] \
300 raise AttributeError, name
302 def __getitem__(self, key):
303 return (self.x, self.y, self.z)[key]
306 return iter((self.x, self.y, self.z))
312 return 'Vector3(%.2f, %.2f, %.2f)' % (self.x,
316 if _enable_swizzle_set:
317 # This has detrimental performance on ordinary setattr as well
319 def __setattr__(self, name, value):
321 object.__setattr__(self, name, value)
324 l = [self.x, self.y, self.z]
325 for c, v in map(None, name, value):
326 l['xyz'.index(c)] = v
327 self.x, self.y, self.z = l
329 raise AttributeError, name
331 def __setitem__(self, key, value):
332 l = [self.x, self.y, self.z]
334 self.x, self.y, self.z = l