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
27 from fabmetheus_utilities import xml_simple_writer
32 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
33 __credits__ = 'Nophead <http://forums.reprap.org/profile.php?12,28>\nArt of Illusion <http://www.artofillusion.org/>'
34 __date__ = '$Date: 2008/21/04 $'
35 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
38 class Vector3Index(object):
39 'A three dimensional vector index class.'
40 __slots__ = ['index', 'x', 'y', 'z']
42 def __init__( self, index, x = 0.0, y = 0.0, z = 0.0 ):
49 'Get the magnitude of the Vector3.'
50 return math.sqrt( self.x * self.x + self.y * self.y + self.z * self.z )
54 def __add__(self, other):
55 'Get the sum of this Vector3 and other one.'
56 return Vector3Index( self.index, self.x + other.x, self.y + other.y, self.z + other.z )
59 'Get the copy of this Vector3.'
60 return Vector3Index( self.index, self.x, self.y, self.z )
66 def __div__(self, other):
67 'Get a new Vector3 by dividing each component of this one.'
68 return Vector3Index( self.index, self.x / other, self.y / other, self.z / other )
70 def __eq__(self, other):
71 'Determine whether this vector is identical to other one.'
74 if other.__class__ != self.__class__:
76 return self.x == other.x and self.y == other.y and self.z == other.z
78 def __floordiv__(self, other):
79 'Get a new Vector3 by floor dividing each component of this one.'
80 return Vector3Index( self.index, self.x // other, self.y // other, self.z // other )
83 'Determine whether this vector is identical to other one.'
84 return self.__repr__().__hash__()
86 def __iadd__(self, other):
87 'Add other Vector3 to this one.'
93 def __idiv__(self, other):
94 'Divide each component of this Vector3.'
100 def __ifloordiv__(self, other):
101 'Floor divide each component of this Vector3.'
107 def __imul__(self, other):
108 'Multiply each component of this Vector3.'
114 def __isub__(self, other):
115 'Subtract other Vector3 from this one.'
121 def __itruediv__(self, other):
122 'True divide each component of this Vector3.'
123 self.x = operator.truediv( self.x, other )
124 self.y = operator.truediv( self.y, other )
125 self.z = operator.truediv( self.z, other )
128 def __mul__(self, other):
129 'Get a new Vector3 by multiplying each component of this one.'
130 return Vector3Index( self.index, self.x * other, self.y * other, self.z * other )
132 def __ne__(self, other):
133 'Determine whether this vector is not identical to other one.'
134 return not self.__eq__(other)
137 return Vector3Index( self.index, - self.x, - self.y, - self.z )
139 def __nonzero__(self):
140 return self.x != 0 or self.y != 0 or self.z != 0
142 def __rdiv__(self, other):
143 'Get a new Vector3 by dividing each component of this one.'
144 return Vector3Index( self.index, other / self.x, other / self.y, other / self.z )
147 'Get the string representation of this Vector3 index.'
148 return '(%s, %s, %s, %s)' % (self.index, self.x, self.y, self.z)
150 def __rfloordiv__(self, other):
151 'Get a new Vector3 by floor dividing each component of this one.'
152 return Vector3Index( self.index, other // self.x, other // self.y, other // self.z )
154 def __rmul__(self, other):
155 'Get a new Vector3 by multiplying each component of this one.'
156 return Vector3Index( self.index, self.x * other, self.y * other, self.z * other )
158 def __rtruediv__(self, other):
159 'Get a new Vector3 by true dividing each component of this one.'
160 return Vector3Index( self.index, operator.truediv( other , self.x ), operator.truediv( other, self.y ), operator.truediv( other, self.z ) )
162 def __sub__(self, other):
163 'Get the difference between the Vector3 and other one.'
164 return Vector3Index( self.index, self.x - other.x, self.y - other.y, self.z - other.z )
166 def __truediv__(self, other):
167 'Get a new Vector3 by true dividing each component of this one.'
168 return Vector3Index( self.index, operator.truediv( self.x, other ), operator.truediv( self.y, other ), operator.truediv( self.z, other ) )
170 def _getAccessibleAttribute(self, attributeName):
171 'Get the accessible attribute.'
172 global globalGetAccessibleAttributeSet
173 if attributeName in globalGetAccessibleAttributeSet:
174 return getattr(self, attributeName, None)
177 def _setAccessibleAttribute(self, attributeName, value):
178 'Set the accessible attribute.'
179 if attributeName in globalSetAccessibleAttributeSet:
180 setattr(self, attributeName, value)
182 def cross(self, other):
183 'Calculate the cross product of this vector with other one.'
184 return Vector3Index( self.index, self.y * other.z - self.z * other.y, - self.x * other.z + self.z * other.x, self.x * other.y - self.y * other.x )
186 def distance(self, other):
187 'Get the Euclidean distance between this vector and other one.'
188 return math.sqrt( self.distanceSquared(other) )
190 def distanceSquared(self, other):
191 'Get the square of the Euclidean distance between this vector and other one.'
192 separationX = self.x - other.x
193 separationY = self.y - other.y
194 separationZ = self.z - other.z
195 return separationX * separationX + separationY * separationY + separationZ * separationZ
197 def dot(self, other):
198 'Calculate the dot product of this vector with other one.'
199 return self.x * other.x + self.y * other.y + self.z * other.z
201 def dropAxis( self, which = 2 ):
202 'Get a complex by removing one axis of the vector3.'
204 return complex( self.y, self.z )
206 return complex( self.x, self.z )
208 return complex( self.x, self.y )
210 def getFloatList(self):
211 'Get the vector as a list of floats.'
212 return [ float( self.x ), float( self.y ), float( self.z ) ]
214 def getIsDefault(self):
215 'Determine if this is the zero vector.'
222 def getNormalized(self):
223 'Get the normalized Vector3.'
224 magnitude = abs(self)
227 return self / magnitude
229 def magnitudeSquared(self):
230 'Get the square of the magnitude of the Vector3.'
231 return self.x * self.x + self.y * self.y + self.z * self.z
233 def maximize(self, other):
234 'Maximize the Vector3.'
235 self.x = max(other.x, self.x)
236 self.y = max(other.y, self.y)
237 self.z = max(other.z, self.z)
239 def minimize(self, other):
240 'Minimize the Vector3.'
241 self.x = min(other.x, self.x)
242 self.y = min(other.y, self.y)
243 self.z = min(other.z, self.z)
246 '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.'
247 magnitude = abs(self)
251 def reflect( self, normal ):
252 'Reflect the Vector3 across the normal, which is assumed to be normalized.'
253 distance = 2 * ( self.x * normal.x + self.y * normal.y + self.z * normal.z )
254 return Vector3Index( self.index, self.x - distance * normal.x, self.y - distance * normal.y, self.z - distance * normal.z )
256 def setToVector3(self, other):
257 'Set this Vector3 to be identical to other one.'
262 def setToXYZ( self, x, y, z ):
263 'Set the x, y, and z components of this Vector3.'
269 globalGetAccessibleAttributeSet = 'x y z'.split()
270 globalSetAccessibleAttributeSet = globalGetAccessibleAttributeSet