summaryrefslogtreecommitdiff
path: root/trunk/reprap/miscellaneous/python-beanshell-scripts/skeinforge_tools/skeinforge_utilities/vector3.py
blob: 52acd0e385745e03d8fa8f5000f04a0daab81b53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
"""
Vector3 is a three dimensional vector class.

Below are examples of Vector3 use.

>>> from vector3 import Vector3
>>> origin = Vector3()
>>> origin
0.0, 0.0, 0.0
>>> pythagoras = Vector3( 3, 4, 0 )
>>> pythagoras
3.0, 4.0, 0.0
>>> pythagoras.magnitude()
5.0
>>> pythagoras.magnitudeSquared()
25
>>> triplePythagoras = pythagoras * 3.0
>>> triplePythagoras
9.0, 12.0, 0.0
>>> plane = pythagoras.dropAxis( 2 )
>>> plane
(3+4j)
"""

from __future__ import absolute_import
try:
	import psyco
	psyco.full()
except:
	pass
#Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
import __init__

import math
import operator


__author__ = "Enrique Perez (perez_enrique@yahoo.com)"
__credits__ = 'Nophead <http://forums.reprap.org/profile.php?12,28>\nArt of Illusion <http://www.artofillusion.org/>'
__date__ = "$Date: 2008/21/04 $"
__license__ = "GPL 3.0"


class Vector3:
	"A three dimensional vector class."
	__slots__ = [ 'x', 'y', 'z' ]

	def __init__( self, x = 0.0, y = 0.0, z = 0.0 ):
		self.x = x
		self.y = y
		self.z = z

	def __abs__( self ):
		"Get the magnitude of the Vector3."
		return math.sqrt( self.x * self.x + self.y * self.y + self.z * self.z )

	magnitude = __abs__

	def __add__( self, other ):
		"Get the sum of this Vector3 and other one."
		return Vector3( self.x + other.x, self.y + other.y, self.z + other.z )

	def __copy__( self ):
		"Get the copy of this Vector3."
		return Vector3( self.x, self.y, self.z )

	__pos__ = __copy__

	copy = __copy__

	def __div__( self, other ):
		"Get a new Vector3 by dividing each component of this one."
		return Vector3( self.x / other, self.y / other, self.z / other )

	def __eq__( self, other ):
		"Determine whether this vector is identical to other one."
		if other == None:
			return False
		return self.x == other.x and self.y == other.y and self.z == other.z

	def __floordiv__( self, other ):
		"Get a new Vector3 by floor dividing each component of this one."
		return Vector3( self.x // other, self.y // other, self.z // other )

	def __hash__( self ):
		"Determine whether this vector is identical to other one."
		return self.__repr__().__hash__()

	def __iadd__( self, other ):
		"Add other Vector3 to this one."
		self.x += other.x
		self.y += other.y
		self.z += other.z
		return self

	def __idiv__( self, other ):
		"Divide each component of this Vector3."
		self.x /= other
		self.y /= other
		self.z /= other
		return self

	def __ifloordiv__( self, other ):
		"Floor divide each component of this Vector3."
		self.x //= other
		self.y //= other
		self.z //= other
		return self

	def __imul__( self, other ):
		"Multiply each component of this Vector3."
		self.x *= other
		self.y *= other
		self.z *= other
		return self

	def __isub__( self, other ):
		"Subtract other Vector3 from this one."
		self.x -= other.x
		self.y -= other.y
		self.z -= other.z
		return self

	def __itruediv__( self, other ):
		"True divide each component of this Vector3."
		self.x = operator.truediv( self.x, other )
		self.y = operator.truediv( self.y, other )
		self.z = operator.truediv( self.z, other )
		return self

	def __mul__( self, other ):
		"Get a new Vector3 by multiplying each component of this one."
		return Vector3( self.x * other, self.y * other, self.z * other )

	def __ne__( self, other ):
		"Determine whether this vector is not identical to other one."
		return not self.__eq__( other )

	def __neg__( self ):
		return Vector3( - self.x, - self.y, - self.z )

	def __nonzero__( self ):
		return self.x != 0 or self.y != 0 or self.z != 0

	def __repr__( self ):
		"Get the string representation of this Vector3."
		return '%s, %s, %s' % ( self.x, self.y, self.z )

	def __rdiv__( self, other ):
		"Get a new Vector3 by dividing each component of this one."
		return Vector3( other / self.x, other / self.y, other / self.z )

	def __rfloordiv__( self, other ):
		"Get a new Vector3 by floor dividing each component of this one."
		return Vector3( other // self.x, other // self.y, other // self.z )

	def __rmul__( self, other ):
		"Get a new Vector3 by multiplying each component of this one."
		return Vector3( self.x * other, self.y * other, self.z * other )

	def __rtruediv__( self, other ):
		"Get a new Vector3 by true dividing each component of this one."
		return Vector3( operator.truediv( other , self.x ), operator.truediv( other, self.y ), operator.truediv( other, self.z ) )

	def __sub__( self, other ):
		"Get the difference between the Vector3 and other one."
		return Vector3( self.x - other.x, self.y - other.y, self.z - other.z )

	def __truediv__( self, other ):
		"Get a new Vector3 by true dividing each component of this one."
		return Vector3( operator.truediv( self.x, other ), operator.truediv( self.y, other ), operator.truediv( self.z, other ) )

	def cross( self, other ):
		"Calculate the cross product of this vector with other one."
		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 )

	def distance( self, other ):
		"Get the Euclidean distance between this vector and other one."
		return math.sqrt( self.distanceSquared( other ) )

	def distanceSquared( self, other ):
		"Get the square of the Euclidean distance between this vector and other one."
		separationX = self.x - other.x
		separationY = self.y - other.y
		separationZ = self.z - other.z
		return separationX * separationX + separationY * separationY + separationZ * separationZ

	def dot( self, other ):
		"Calculate the dot product of this vector with other one."
		return self.x * other.x + self.y * other.y + self.z * other.z

	def dropAxis( self, which ):
		"""Get a complex by removing one axis of this one.

		Keyword arguments:
		which -- the axis to drop (0=X, 1=Y, 2=Z)"""
		if which == 0:
			return complex( self.y, self.z )
		if which == 1:
			return complex( self.x, self.z )
		if which == 2:
			return complex( self.x, self.y )

	def getNormalized( self, other ):
		"Get the normalized Vector3."
		magnitude = abs( self )
		if magnitude == 0.0:
			return self.copy()
		return self / magnitude

	def magnitudeSquared( self ):
		"Get the square of the magnitude of the Vector3."
		return self.x * self.x + self.y * self.y + self.z * self.z

	def normalize( self ):
		"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."
		magnitude = abs( self )
		if magnitude != 0.0:
			self /= magnitude

	def reflect( self, normal ):
		"Reflect the Vector3 across the normal, which is assumed to be normalized."
		distance = 2 * ( self.x * normal.x + self.y * normal.y + self.z * normal.z )
		return Vector3( self.x - distance * normal.x, self.y - distance * normal.y, self.z - distance * normal.z )

	def setToVector3( self, other ):
		"Set this Vector3 to be identical to other one."
		self.x = other.x
		self.y = other.y
		self.z = other.z

	def setToXYZ( self, x, y, z ):
		"Set the x, y, and z components of this Vector3."
		self.x = x
		self.y = y
		self.z = z

"""
class Vector3:
	__slots__ = ['x', 'y', 'z']

	def __init__(self, x, y, z):
		self.x = x
		self.y = y
		self.z = z

	def __copy__(self):
		return self.__class__(self.x, self.y, self.z)

	copy = __copy__

	def __repr__(self):
		return 'Vector3(%.2f, %.2f, %.2f)' % (self.x,
											  self.y,
											  self.z)

	def __eq__(self, other):
		if isinstance(other, Vector3):
			return self.x == other.x and \
				   self.y == other.y and \
				   self.z == other.z
		else:
			assert hasattr(other, '__len__') and len(other) == 3
			return self.x == other[0] and \
				   self.y == other[1] and \
				   self.z == other[2]

	def __ne__(self, other):
		return not self.__eq__(other)

	def __nonzero__(self):
		return self.x != 0 or self.y != 0 or self.z != 0

	def __len__(self):
		return 3

	def __getitem__(self, key):
		return (self.x, self.y, self.z)[key]

	def __setitem__(self, key, value):
		l = [self.x, self.y, self.z]
		l[key] = value
		self.x, self.y, self.z = l

	def __iter__(self):
		return iter((self.x, self.y, self.z))

	def __getattr__(self, name):
		try:
			return tuple([(self.x, self.y, self.z)['xyz'.index(c)] \
						  for c in name])
		except ValueError:
			raise AttributeError, name

	if _enable_swizzle_set:
		# This has detrimental performance on ordinary setattr as well
		# if enabled
		def __setattr__(self, name, value):
			if len(name) == 1:
				object.__setattr__(self, name, value)
			else:
				try:
					l = [self.x, self.y, self.z]
					for c, v in map(None, name, value):
						l['xyz'.index(c)] = v
					self.x, self.y, self.z = l
				except ValueError:
					raise AttributeError, name


	def __add__(self, other):
		if isinstance(other, Vector3):
			# Vector + Vector -> Vector
			# Vector + Point -> Point
			# Point + Point -> Vector
			if self.__class__ is other.__class__:
				_class = Vector3
			else:
				_class = Point3
			return _class(self.x + other.x,
						  self.y + other.y,
						  self.z + other.z)
		else:
			assert hasattr(other, '__len__') and len(other) == 3
			return Vector3(self.x + other[0],
						   self.y + other[1],
						   self.z + other[2])
	__radd__ = __add__

	def __iadd__(self, other):
		if isinstance(other, Vector3):
			self.x += other.x
			self.y += other.y
			self.z += other.z
		else:
			self.x += other[0]
			self.y += other[1]
			self.z += other[2]
		return self

	def __sub__(self, other):
		if isinstance(other, Vector3):
			# Vector - Vector -> Vector
			# Vector - Point -> Point
			# Point - Point -> Vector
			if self.__class__ is other.__class__:
				_class = Vector3
			else:
				_class = Point3
			return Vector3(self.x - other.x,
						   self.y - other.y,
						   self.z - other.z)
		else:
			assert hasattr(other, '__len__') and len(other) == 3
			return Vector3(self.x - other[0],
						   self.y - other[1],
						   self.z - other[2])

   
	def __rsub__(self, other):
		if isinstance(other, Vector3):
			return Vector3(other.x - self.x,
						   other.y - self.y,
						   other.z - self.z)
		else:
			assert hasattr(other, '__len__') and len(other) == 3
			return Vector3(other.x - self[0],
						   other.y - self[1],
						   other.z - self[2])

	def __mul__(self, other):
		if isinstance(other, Vector3):
			# TODO component-wise mul/div in-place and on Vector2; docs.
			if self.__class__ is Point3 or other.__class__ is Point3:
				_class = Point3
			else:
				_class = Vector3
			return _class(self.x * other.x,
						  self.y * other.y,
						  self.z * other.z)
		else: 
			assert type(other) in (int, long, float)
			return Vector3(self.x * other,
						   self.y * other,
						   self.z * other)

	__rmul__ = __mul__

	def __imul__(self, other):
		assert type(other) in (int, long, float)
		self.x *= other
		self.y *= other
		self.z *= other
		return self

	def __div__(self, other):
		assert type(other) in (int, long, float)
		return Vector3(operator.div(self.x, other),
					   operator.div(self.y, other),
					   operator.div(self.z, other))


	def __rdiv__(self, other):
		assert type(other) in (int, long, float)
		return Vector3(operator.div(other, self.x),
					   operator.div(other, self.y),
					   operator.div(other, self.z))

	def __floordiv__(self, other):
		assert type(other) in (int, long, float)
		return Vector3(operator.floordiv(self.x, other),
					   operator.floordiv(self.y, other),
					   operator.floordiv(self.z, other))


	def __rfloordiv__(self, other):
		assert type(other) in (int, long, float)
		return Vector3(operator.floordiv(other, self.x),
					   operator.floordiv(other, self.y),
					   operator.floordiv(other, self.z))

	def __truediv__(self, other):
		assert type(other) in (int, long, float)
		return Vector3(operator.truediv(self.x, other),
					   operator.truediv(self.y, other),
					   operator.truediv(self.z, other))


	def __rtruediv__(self, other):
		assert type(other) in (int, long, float)
		return Vector3(operator.truediv(other, self.x),
					   operator.truediv(other, self.y),
					   operator.truediv(other, self.z))
	
	def __neg__(self):
		return Vector3(-self.x,
						-self.y,
						-self.z)

	__pos__ = __copy__
	
	def __abs__(self):
		return math.sqrt(self.x ** 2 + \
						 self.y ** 2 + \
						 self.z ** 2)

	magnitude = __abs__

	def magnitude_squared(self):
		return self.x ** 2 + \
			   self.y ** 2 + \
			   self.z ** 2

	def normalize(self):
		d = self.magnitude()
		if d:
			self.x /= d
			self.y /= d
			self.z /= d
		return self

	def normalized(self):
		d = self.magnitude()
		if d:
			return Vector3(self.x / d, 
						   self.y / d, 
						   self.z / d)
		return self.copy()

	def dot(self, other):
		assert isinstance(other, Vector3)
		return self.x * other.x + \
			   self.y * other.y + \
			   self.z * other.z

	def cross(self, other):
		assert isinstance(other, Vector3)
		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)

	def reflect(self, normal):
		# assume normal is normalized
		assert isinstance(normal, Vector3)
		d = 2 * (self.x * normal.x + self.y * normal.y + self.z * normal.z)
		return Vector3(self.x - d * normal.x,
					   self.y - d * normal.y,
					   self.z - d * normal.z)
"""