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

Source Code for Module fabmetheus_utilities.vector3index

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