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
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
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
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
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
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
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
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
84
86 'Determine whether this vector is identical to other one.'
87 return self.__repr__().__hash__()
88
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
97 'Divide each component of this Vector3.'
98 self.x /= other
99 self.y /= other
100 self.z /= other
101 return self
102
104 'Floor divide each component of this Vector3.'
105 self.x //= other
106 self.y //= other
107 self.z //= other
108 return self
109
111 'Multiply each component of this Vector3.'
112 self.x *= other
113 self.y *= other
114 self.z *= other
115 return self
116
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
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
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
136 'Determine whether this vector is not identical to other one.'
137 return not self.__eq__(other)
138
140 return Vector3( - self.x, - self.y, - self.z )
141
143 return self.x != 0 or self.y != 0 or self.z != 0
144
146 'Get the string representation of this Vector3.'
147 return '(%s, %s, %s)' % ( self.x, self.y, self.z )
148
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
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
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
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
169
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
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
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
183 'Get the Euclidean distance between this vector and other one.'
184 return math.sqrt( self.distanceSquared(other) )
185
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
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
207 'Get the vector as a list of floats.'
208 return [ float( self.x ), float( self.y ), float( self.z ) ]
209
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
219 'Get the normalized Vector3.'
220 magnitude = abs(self)
221 if magnitude == 0.0:
222 return self.copy()
223 return self / magnitude
224
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
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
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
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
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
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
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