/Program_Files/replicatorg-0025/skein_engines/skeinforge-35/fabmetheus_utilities/vector3index.py

https://github.com/sialan/autonomous-sprayer · Python · 262 lines · 184 code · 50 blank · 28 comment · 15 complexity · 92b6e226af553ec84eac007cbcc85864 MD5 · raw file

  1. """
  2. Vector3 is a three dimensional vector class.
  3. Below are examples of Vector3 use.
  4. >>> from vector3 import Vector3
  5. >>> origin = Vector3()
  6. >>> origin
  7. 0.0, 0.0, 0.0
  8. >>> pythagoras = Vector3( 3, 4, 0 )
  9. >>> pythagoras
  10. 3.0, 4.0, 0.0
  11. >>> pythagoras.magnitude()
  12. 5.0
  13. >>> pythagoras.magnitudeSquared()
  14. 25
  15. >>> triplePythagoras = pythagoras * 3.0
  16. >>> triplePythagoras
  17. 9.0, 12.0, 0.0
  18. >>> plane = pythagoras.dropAxis(2)
  19. >>> plane
  20. (3+4j)
  21. """
  22. from __future__ import absolute_import
  23. try:
  24. import psyco
  25. psyco.full()
  26. except:
  27. pass
  28. #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.
  29. import __init__
  30. from fabmetheus_utilities import xml_simple_writer
  31. import math
  32. import operator
  33. __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
  34. __credits__ = 'Nophead <http://forums.reprap.org/profile.php?12,28>\nArt of Illusion <http://www.artofillusion.org/>'
  35. __date__ = '$Date: 2008/21/04 $'
  36. __license__ = 'GPL 3.0'
  37. class Vector3Index:
  38. "A three dimensional vector index class."
  39. __slots__ = ['index', 'x', 'y', 'z']
  40. def __init__( self, index, x = 0.0, y = 0.0, z = 0.0 ):
  41. self.index = index
  42. self.x = x
  43. self.y = y
  44. self.z = z
  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. magnitude = __abs__
  49. def __add__(self, other):
  50. "Get the sum of this Vector3 and other one."
  51. return Vector3Index( self.index, self.x + other.x, self.y + other.y, self.z + other.z )
  52. def __copy__(self):
  53. "Get the copy of this Vector3."
  54. return Vector3Index( self.index, self.x, self.y, self.z )
  55. __pos__ = __copy__
  56. copy = __copy__
  57. def __div__(self, other):
  58. "Get a new Vector3 by dividing each component of this one."
  59. return Vector3Index( self.index, self.x / other, self.y / other, self.z / other )
  60. def __eq__(self, other):
  61. "Determine whether this vector is identical to other one."
  62. if other == None:
  63. return False
  64. if other.__class__ != self.__class__:
  65. return False
  66. return self.x == other.x and self.y == other.y and self.z == other.z
  67. def __floordiv__(self, other):
  68. "Get a new Vector3 by floor dividing each component of this one."
  69. return Vector3Index( self.index, self.x // other, self.y // other, self.z // other )
  70. def _getAccessibleAttribute(self, attributeName):
  71. "Get the accessible attribute."
  72. global globalAccessibleAttributeSet
  73. if attributeName in globalAccessibleAttributeSet:
  74. return getattr(self, attributeName, None)
  75. return None
  76. def __hash__(self):
  77. "Determine whether this vector is identical to other one."
  78. return self.__repr__().__hash__()
  79. def __iadd__(self, other):
  80. "Add other Vector3 to this one."
  81. self.x += other.x
  82. self.y += other.y
  83. self.z += other.z
  84. return self
  85. def __idiv__(self, other):
  86. "Divide each component of this Vector3."
  87. self.x /= other
  88. self.y /= other
  89. self.z /= other
  90. return self
  91. def __ifloordiv__(self, other):
  92. "Floor divide each component of this Vector3."
  93. self.x //= other
  94. self.y //= other
  95. self.z //= other
  96. return self
  97. def __imul__(self, other):
  98. "Multiply each component of this Vector3."
  99. self.x *= other
  100. self.y *= other
  101. self.z *= other
  102. return self
  103. def __isub__(self, other):
  104. "Subtract other Vector3 from this one."
  105. self.x -= other.x
  106. self.y -= other.y
  107. self.z -= other.z
  108. return self
  109. def __itruediv__(self, other):
  110. "True divide each component of this Vector3."
  111. self.x = operator.truediv( self.x, other )
  112. self.y = operator.truediv( self.y, other )
  113. self.z = operator.truediv( self.z, other )
  114. return self
  115. def __mul__(self, other):
  116. "Get a new Vector3 by multiplying each component of this one."
  117. return Vector3Index( self.index, self.x * other, self.y * other, self.z * other )
  118. def __ne__(self, other):
  119. "Determine whether this vector is not identical to other one."
  120. return not self.__eq__( other )
  121. def __neg__(self):
  122. return Vector3Index( self.index, - self.x, - self.y, - self.z )
  123. def __nonzero__(self):
  124. return self.x != 0 or self.y != 0 or self.z != 0
  125. def __repr__(self):
  126. "Get the string representation of this Vector3 index."
  127. return '(%s, %s, %s, %s)' % (self.index, self.x, self.y, self.z)
  128. def __rdiv__(self, other):
  129. "Get a new Vector3 by dividing each component of this one."
  130. return Vector3Index( self.index, other / self.x, other / self.y, other / self.z )
  131. def __rfloordiv__(self, other):
  132. "Get a new Vector3 by floor dividing each component of this one."
  133. return Vector3Index( self.index, other // self.x, other // self.y, other // self.z )
  134. def __rmul__(self, other):
  135. "Get a new Vector3 by multiplying each component of this one."
  136. return Vector3Index( self.index, self.x * other, self.y * other, self.z * other )
  137. def __rtruediv__(self, other):
  138. "Get a new Vector3 by true dividing each component of this one."
  139. return Vector3Index( self.index, operator.truediv( other , self.x ), operator.truediv( other, self.y ), operator.truediv( other, self.z ) )
  140. def __sub__(self, other):
  141. "Get the difference between the Vector3 and other one."
  142. return Vector3Index( self.index, self.x - other.x, self.y - other.y, self.z - other.z )
  143. def __truediv__(self, other):
  144. "Get a new Vector3 by true dividing each component of this one."
  145. return Vector3Index( self.index, operator.truediv( self.x, other ), operator.truediv( self.y, other ), operator.truediv( self.z, other ) )
  146. def cross(self, other):
  147. "Calculate the cross product of this vector with other one."
  148. 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 )
  149. def distance(self, other):
  150. "Get the Euclidean distance between this vector and other one."
  151. return math.sqrt( self.distanceSquared( other ) )
  152. def distanceSquared(self, other):
  153. "Get the square of the Euclidean distance between this vector and other one."
  154. separationX = self.x - other.x
  155. separationY = self.y - other.y
  156. separationZ = self.z - other.z
  157. return separationX * separationX + separationY * separationY + separationZ * separationZ
  158. def dot(self, other):
  159. "Calculate the dot product of this vector with other one."
  160. return self.x * other.x + self.y * other.y + self.z * other.z
  161. def dropAxis( self, which = 2 ):
  162. """Get a complex by removing one axis of this one.
  163. Keyword arguments:
  164. which -- the axis to drop (0=X, 1=Y, 2=Z)"""
  165. if which == 0:
  166. return complex( self.y, self.z )
  167. if which == 1:
  168. return complex( self.x, self.z )
  169. if which == 2:
  170. return complex( self.x, self.y )
  171. def getFloatList(self):
  172. "Get the vector as a list of floats."
  173. return [ float( self.x ), float( self.y ), float( self.z ) ]
  174. def getIsDefault(self):
  175. "Determine if this is the zero vector."
  176. if self.x != 0.0:
  177. return False
  178. if self.y != 0.0:
  179. return False
  180. return self.z == 0.0
  181. def getNormalized(self):
  182. "Get the normalized Vector3."
  183. magnitude = abs(self)
  184. if magnitude == 0.0:
  185. return self.copy()
  186. return self / magnitude
  187. def magnitudeSquared(self):
  188. "Get the square of the magnitude of the Vector3."
  189. return self.x * self.x + self.y * self.y + self.z * self.z
  190. def normalize(self):
  191. "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."
  192. magnitude = abs(self)
  193. if magnitude != 0.0:
  194. self /= magnitude
  195. def reflect( self, normal ):
  196. "Reflect the Vector3 across the normal, which is assumed to be normalized."
  197. distance = 2 * ( self.x * normal.x + self.y * normal.y + self.z * normal.z )
  198. return Vector3Index( self.index, self.x - distance * normal.x, self.y - distance * normal.y, self.z - distance * normal.z )
  199. def setToVector3(self, other):
  200. "Set this Vector3 to be identical to other one."
  201. self.x = other.x
  202. self.y = other.y
  203. self.z = other.z
  204. def setToXYZ( self, x, y, z ):
  205. "Set the x, y, and z components of this Vector3."
  206. self.x = x
  207. self.y = y
  208. self.z = z
  209. globalAccessibleAttributeSet = 'x y z'.split()