| Trees | Indices | Help |
|
|---|
|
|
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 3537 'A three dimensional vector class.' 38 __slots__ = ['x', 'y', 'z'] 39 44263 264 265 globalGetAccessibleAttributeSet = 'x y z'.split() 266 globalSetAccessibleAttributeSet = globalGetAccessibleAttributeSet 26746 '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__ 5052 '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 58 59 __pos__ = __copy__ 60 61 copy = __copy__ 6264 'Get a new Vector3 by dividing each component of this one.' 65 return Vector3( self.x / other, self.y / other, self.z / other )6668 '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.z7476 'Get a new Vector3 by floor dividing each component of this one.' 77 return Vector3( self.x // other, self.y // other, self.z // other )7880 'Get the accessible attribute.' 81 if attributeName in globalGetAccessibleAttributeSet: 82 return getattr(self, attributeName, None) 83 return None84 8890 'Add other Vector3 to this one.' 91 self.x += other.x 92 self.y += other.y 93 self.z += other.z 94 return self9597 'Divide each component of this Vector3.' 98 self.x /= other 99 self.y /= other 100 self.z /= other 101 return self102104 'Floor divide each component of this Vector3.' 105 self.x //= other 106 self.y //= other 107 self.z //= other 108 return self109111 'Multiply each component of this Vector3.' 112 self.x *= other 113 self.y *= other 114 self.z *= other 115 return self116118 'Subtract other Vector3 from this one.' 119 self.x -= other.x 120 self.y -= other.y 121 self.z -= other.z 122 return self123125 '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 self130132 'Get a new Vector3 by multiplying each component of this one.' 133 return Vector3( self.x * other, self.y * other, self.z * other )134136 'Determine whether this vector is not identical to other one.' 137 return not self.__eq__(other)138140 return Vector3( - self.x, - self.y, - self.z )141 144146 'Get the string representation of this Vector3.' 147 return '(%s, %s, %s)' % ( self.x, self.y, self.z )148150 'Get a new Vector3 by dividing each component of this one.' 151 return Vector3( other / self.x, other / self.y, other / self.z )152154 'Get a new Vector3 by floor dividing each component of this one.' 155 return Vector3( other // self.x, other // self.y, other // self.z )156158 'Get a new Vector3 by multiplying each component of this one.' 159 return Vector3( self.x * other, self.y * other, self.z * other )160162 '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 ) )164166 'Set the accessible attribute.' 167 if attributeName in globalSetAccessibleAttributeSet: 168 setattr(self, attributeName, value)169171 'Get the difference between the Vector3 and other one.' 172 return Vector3( self.x - other.x, self.y - other.y, self.z - other.z )173175 '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 ) )177179 '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)181183 'Get the Euclidean distance between this vector and other one.' 184 return math.sqrt( self.distanceSquared(other) )185187 '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 * separationZ192194 'Calculate the dot product of this vector with other one.' 195 return self.x * other.x + self.y * other.y + self.z * other.z196198 '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 )205207 'Get the vector as a list of floats.' 208 return [ float( self.x ), float( self.y ), float( self.z ) ]209211 '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.0217219 'Get the normalized Vector3.' 220 magnitude = abs(self) 221 if magnitude == 0.0: 222 return self.copy() 223 return self / magnitude224226 'Get the square of the magnitude of the Vector3.' 227 return self.x * self.x + self.y * self.y + self.z * self.z228230 '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)234236 '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)240242 '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 /= magnitude246248 '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 )251253 'Set this Vector3 to be identical to other one.' 254 self.x = other.x 255 self.y = other.y 256 self.z = other.z257
| Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1 on Mon Nov 21 00:22:49 2011 | http://epydoc.sourceforge.net |