chiark / gitweb /
Add back the ultimaker platform, and made the platform mesh simpler.
[cura.git] / Cura / slice / cura_sf / fabmetheus_utilities / vector3index.py
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 __future__ import absolute_import
26
27 from fabmetheus_utilities import xml_simple_writer
28 import math
29 import operator
30
31
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'
36
37
38 class Vector3Index(object):
39         'A three dimensional vector index class.'
40         __slots__ = ['index', 'x', 'y', 'z']
41
42         def __init__( self, index, x = 0.0, y = 0.0, z = 0.0 ):
43                 self.index = index
44                 self.x = x
45                 self.y = y
46                 self.z = z
47
48         def __abs__(self):
49                 'Get the magnitude of the Vector3.'
50                 return math.sqrt( self.x * self.x + self.y * self.y + self.z * self.z )
51
52         magnitude = __abs__
53
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 )
57
58         def __copy__(self):
59                 'Get the copy of this Vector3.'
60                 return Vector3Index( self.index, self.x, self.y, self.z )
61
62         __pos__ = __copy__
63
64         copy = __copy__
65
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 )
69
70         def __eq__(self, other):
71                 'Determine whether this vector is identical to other one.'
72                 if other == None:
73                         return False
74                 if other.__class__ != self.__class__:
75                         return False
76                 return self.x == other.x and self.y == other.y and self.z == other.z
77
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 )
81
82         def __hash__(self):
83                 'Determine whether this vector is identical to other one.'
84                 return self.__repr__().__hash__()
85
86         def __iadd__(self, other):
87                 'Add other Vector3 to this one.'
88                 self.x += other.x
89                 self.y += other.y
90                 self.z += other.z
91                 return self
92
93         def __idiv__(self, other):
94                 'Divide each component of this Vector3.'
95                 self.x /= other
96                 self.y /= other
97                 self.z /= other
98                 return self
99
100         def __ifloordiv__(self, other):
101                 'Floor divide each component of this Vector3.'
102                 self.x //= other
103                 self.y //= other
104                 self.z //= other
105                 return self
106
107         def __imul__(self, other):
108                 'Multiply each component of this Vector3.'
109                 self.x *= other
110                 self.y *= other
111                 self.z *= other
112                 return self
113
114         def __isub__(self, other):
115                 'Subtract other Vector3 from this one.'
116                 self.x -= other.x
117                 self.y -= other.y
118                 self.z -= other.z
119                 return self
120
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 )
126                 return self
127
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 )
131
132         def __ne__(self, other):
133                 'Determine whether this vector is not identical to other one.'
134                 return not self.__eq__(other)
135
136         def __neg__(self):
137                 return Vector3Index( self.index, - self.x, - self.y, - self.z )
138
139         def __nonzero__(self):
140                 return self.x != 0 or self.y != 0 or self.z != 0
141
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 )
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 __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 )
153
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 )
157
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 ) )
161
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 )
165
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 ) )
169
170         def _getAccessibleAttribute(self, attributeName):
171                 'Get the accessible attribute.'
172                 global globalGetAccessibleAttributeSet
173                 if attributeName in globalGetAccessibleAttributeSet:
174                         return getattr(self, attributeName, None)
175                 return None
176
177         def _setAccessibleAttribute(self, attributeName, value):
178                 'Set the accessible attribute.'
179                 if attributeName in globalSetAccessibleAttributeSet:
180                         setattr(self, attributeName, value)
181
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 )
185
186         def distance(self, other):
187                 'Get the Euclidean distance between this vector and other one.'
188                 return math.sqrt( self.distanceSquared(other) )
189
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
196
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
200
201         def dropAxis( self, which = 2 ):
202                 'Get a complex by removing one axis of the vector3.'
203                 if which == 0:
204                         return complex( self.y, self.z )
205                 if which == 1:
206                         return complex( self.x, self.z )
207                 if which == 2:
208                         return complex( self.x, self.y )
209
210         def getFloatList(self):
211                 'Get the vector as a list of floats.'
212                 return [ float( self.x ), float( self.y ), float( self.z ) ]
213
214         def getIsDefault(self):
215                 'Determine if this is the zero vector.'
216                 if self.x != 0.0:
217                         return False
218                 if self.y != 0.0:
219                         return False
220                 return self.z == 0.0
221
222         def getNormalized(self):
223                 'Get the normalized Vector3.'
224                 magnitude = abs(self)
225                 if magnitude == 0.0:
226                         return self.copy()
227                 return self / magnitude
228
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
232
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)
238
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)
244
245         def normalize(self):
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)
248                 if magnitude != 0.0:
249                         self /= magnitude
250
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 )
255
256         def setToVector3(self, other):
257                 'Set this Vector3 to be identical to other one.'
258                 self.x = other.x
259                 self.y = other.y
260                 self.z = other.z
261
262         def setToXYZ( self, x, y, z ):
263                 'Set the x, y, and z components of this Vector3.'
264                 self.x = x
265                 self.y = y
266                 self.z = z
267
268
269 globalGetAccessibleAttributeSet = 'x y z'.split()
270 globalSetAccessibleAttributeSet = globalGetAccessibleAttributeSet