Quaternion.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <base href="../../../" />
  6. <script src="page.js"></script>
  7. <link type="text/css" rel="stylesheet" href="page.css" />
  8. </head>
  9. <body>
  10. <h1>[name]</h1>
  11. <p class="desc">
  12. Implementation of a [link:http://en.wikipedia.org/wiki/Quaternion quaternion].<br />
  13. Quaternions are used in three.js to represent
  14. [link:https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation rotations].
  15. </p>
  16. <p>
  17. Iterating through a [name] instance will yield its components (x, y, z, w)
  18. in the corresponding order.
  19. </p>
  20. <h2>Code Example</h2>
  21. <code>
  22. const quaternion = new THREE.Quaternion();
  23. quaternion.setFromAxisAngle( new THREE.Vector3( 0, 1, 0 ), Math.PI / 2 );
  24. const vector = new THREE.Vector3( 1, 0, 0 );
  25. vector.applyQuaternion( quaternion );
  26. </code>
  27. <h2>Constructor</h2>
  28. <h3>
  29. [name]( [param:Float x], [param:Float y], [param:Float z], [param:Float w] )
  30. </h3>
  31. <p>
  32. [page:Float x] - x coordinate<br />
  33. [page:Float y] - y coordinate<br />
  34. [page:Float z] - z coordinate<br />
  35. [page:Float w] - w coordinate
  36. </p>
  37. <h2>Properties</h2>
  38. <h3>[property:Boolean isQuaternion]</h3>
  39. <p>Read-only flag to check if a given object is of type [name].</p>
  40. <h3>[property:Float x]</h3>
  41. <h3>[property:Float y]</h3>
  42. <h3>[property:Float z]</h3>
  43. <h3>[property:Float w]</h3>
  44. <h2>Methods</h2>
  45. <h3>[method:Float angleTo]( [param:Quaternion q] )</h3>
  46. <p>
  47. Returns the angle between this quaternion and quaternion [page:Quaternion q] in radians.
  48. </p>
  49. <h3>[method:Quaternion clone]()</h3>
  50. <p>
  51. Creates a new Quaternion with identical [page:.x x], [page:.y y], [page:.z z]
  52. and [page:.w w] properties to this one.
  53. </p>
  54. <h3>[method:this conjugate]()</h3>
  55. <p>
  56. Returns the rotational conjugate of this quaternion. The conjugate of a
  57. quaternion represents the same rotation in the opposite direction about
  58. the rotational axis.
  59. </p>
  60. <h3>[method:this copy]( [param:Quaternion q] )</h3>
  61. <p>
  62. Copies the [page:.x x], [page:.y y], [page:.z z] and [page:.w w]
  63. properties of [page:Quaternion q] into this quaternion.
  64. </p>
  65. <h3>[method:Boolean equals]( [param:Quaternion v] )</h3>
  66. <p>
  67. [page:Quaternion v] - Quaternion that this quaternion will be compared
  68. to.<br /><br />
  69. Compares the [page:.x x], [page:.y y], [page:.z z] and [page:.w w]
  70. properties of [page:Quaternion v] to the equivalent properties of this
  71. quaternion to determine if they represent the same rotation.
  72. </p>
  73. <h3>[method:Float dot]( [param:Quaternion v] )</h3>
  74. <p>
  75. Calculates the [link:https://en.wikipedia.org/wiki/Dot_product dot product]
  76. of quaternions [page:Quaternion v] and this one.
  77. </p>
  78. <h3>
  79. [method:this fromArray]( [param:Array array], [param:Integer offset] )
  80. </h3>
  81. <p>
  82. [page:Array array] - array of format (x, y, z, w) used to construct the
  83. quaternion.<br />
  84. [page:Integer offset] - (optional) an offset into the array.<br /><br />
  85. Sets this quaternion's [page:.x x], [page:.y y], [page:.z z] and [page:.w w]
  86. properties from an array.
  87. </p>
  88. <h3>[method:this identity]()</h3>
  89. <p>
  90. Sets this quaternion to the identity quaternion; that is, to the
  91. quaternion that represents "no rotation".
  92. </p>
  93. <h3>[method:this invert]()</h3>
  94. <p>
  95. Inverts this quaternion - calculates the [page:.conjugate conjugate]. The
  96. quaternion is assumed to have unit length.
  97. </p>
  98. <h3>[method:Float length]()</h3>
  99. <p>
  100. Computes the [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean length]
  101. (straight-line length) of this quaternion, considered as
  102. a 4 dimensional vector.
  103. </p>
  104. <h3>[method:Float lengthSq]()</h3>
  105. <p>
  106. Computes the squared
  107. [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean length]
  108. (straight-line length) of this quaternion, considered as a 4 dimensional
  109. vector. This can be useful if you are comparing the lengths of two
  110. quaternions, as this is a slightly more efficient calculation than
  111. [page:.length length]().
  112. </p>
  113. <h3>[method:this normalize]()</h3>
  114. <p>
  115. [link:https://en.wikipedia.org/wiki/Normalized_vector Normalizes] this
  116. quaternion - that is, calculated the quaternion that performs the same
  117. rotation as this one, but has [page:.length length] equal to `1`.
  118. </p>
  119. <h3>[method:this multiply]( [param:Quaternion q] )</h3>
  120. <p>Multiplies this quaternion by [page:Quaternion q].</p>
  121. <h3>
  122. [method:this multiplyQuaternions]( [param:Quaternion a], [param:Quaternion b] )
  123. </h3>
  124. <p>
  125. Sets this quaternion to [page:Quaternion a] x [page:Quaternion b].<br />
  126. Adapted from the method outlined
  127. [link:http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.html here].
  128. </p>
  129. <h3>[method:this premultiply]( [param:Quaternion q] )</h3>
  130. <p>Pre-multiplies this quaternion by [page:Quaternion q].</p>
  131. <h3>[method:this random]()</h3>
  132. <p>Sets this quaternion to a uniformly random, normalized quaternion.</p>
  133. <h3>
  134. [method:this rotateTowards]( [param:Quaternion q], [param:Float step] )
  135. </h3>
  136. <p>
  137. [page:Quaternion q] - The target quaternion.<br />
  138. [page:Float step] - The angular step in radians.<br /><br />
  139. Rotates this quaternion by a given angular step to the defined quaternion
  140. *q*. The method ensures that the final quaternion will not overshoot *q*.
  141. </p>
  142. <h3>[method:this slerp]( [param:Quaternion qb], [param:Float t] )</h3>
  143. <p>
  144. [page:Quaternion qb] - The other quaternion rotation<br />
  145. [page:Float t] - interpolation factor in the closed interval `[0, 1]`.<br /><br />
  146. Handles the spherical linear interpolation between quaternions.
  147. [page:Float t] represents the amount of rotation between this quaternion
  148. (where [page:Float t] is 0) and [page:Quaternion qb] (where [page:Float t]
  149. is 1). This quaternion is set to the result. Also see the static version
  150. of the `slerp` below.
  151. <code>
  152. // rotate a mesh towards a target quaternion
  153. mesh.quaternion.slerp( endQuaternion, 0.01 );
  154. </code>
  155. </p>
  156. <h3>
  157. [method:this slerpQuaternions]( [param:Quaternion qa], [param:Quaternion qb], [param:Float t] )
  158. </h3>
  159. <p>
  160. Performs a spherical linear interpolation between the given quaternions
  161. and stores the result in this quaternion.
  162. </p>
  163. <h3>
  164. [method:this set]( [param:Float x], [param:Float y], [param:Float z], [param:Float w] )
  165. </h3>
  166. <p>
  167. Sets [page:.x x], [page:.y y], [page:.z z], [page:.w w] properties of this
  168. quaternion.
  169. </p>
  170. <h3>
  171. [method:this setFromAxisAngle]( [param:Vector3 axis], [param:Float angle] )
  172. </h3>
  173. <p>
  174. Sets this quaternion from rotation specified by [page:Vector3 axis] and
  175. [page:Float angle].<br />
  176. Adapted from the method
  177. [link:http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.html here].<br />
  178. `Axis` is assumed to be normalized, `angle` is in radians.
  179. </p>
  180. <h3>[method:this setFromEuler]( [param:Euler euler] )</h3>
  181. <p>
  182. Sets this quaternion from the rotation specified by [page:Euler] angle.
  183. </p>
  184. <h3>[method:this setFromRotationMatrix]( [param:Matrix4 m] )</h3>
  185. <p>
  186. [page:Matrix4 m] - a [page:Matrix4] of which the upper 3x3 of matrix is a
  187. pure [link:https://en.wikipedia.org/wiki/Rotation_matrix rotation matrix]
  188. (i.e. unscaled).<br />
  189. Sets this quaternion from rotation component of [page:Matrix4 m].<br />
  190. Adapted from the method
  191. [link:http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.html here].
  192. </p>
  193. <h3>
  194. [method:this setFromUnitVectors]( [param:Vector3 vFrom], [param:Vector3 vTo] )
  195. </h3>
  196. <p>
  197. Sets this quaternion to the rotation required to rotate direction vector
  198. [page:Vector3 vFrom] to direction vector [page:Vector3 vTo].<br />
  199. Adapted from the method
  200. [link:http://lolengine.net/blog/2013/09/18/beautiful-maths-quaternion-from-vectors here].<br />
  201. [page:Vector3 vFrom] and [page:Vector3 vTo] are assumed to be normalized.
  202. </p>
  203. <h3>
  204. [method:Array toArray]( [param:Array array], [param:Integer offset] )
  205. </h3>
  206. <p>
  207. [page:Array array] - An optional array to store the quaternion. If not
  208. specified, a new array will be created.<br />
  209. [page:Integer offset] - (optional) if specified, the result will be copied
  210. into this [page:Array].<br /><br />
  211. Returns the numerical elements of this quaternion in an array of format
  212. [x, y, z, w].
  213. </p>
  214. <h3>[method:Array toJSON]()</h3>
  215. <p>
  216. This methods defines the serialization result of [name]. Returns the
  217. numerical elements of this quaternion in an array of format [x, y, z, w].
  218. </p>
  219. <h3>
  220. [method:this fromBufferAttribute]( [param:BufferAttribute attribute], [param:Integer index] )
  221. </h3>
  222. <p>
  223. [page:BufferAttribute attribute] - the source attribute.<br />
  224. [page:Integer index] - index in the attribute.<br /><br />
  225. Sets [page:.x x], [page:.y y], [page:.z z], [page:.w w] properties of this
  226. quaternion from the [page:BufferAttribute attribute].
  227. </p>
  228. <h2>Static Methods</h2>
  229. <h3>
  230. [method:undefined slerpFlat]( [param:Array dst], [param:Integer dstOffset],
  231. [param:Array src0], [param:Integer srcOffset0], [param:Array src1],
  232. [param:Integer srcOffset1], [param:Float t] )
  233. </h3>
  234. <p>
  235. [page:Array dst] - The output array.<br />
  236. [page:Integer dstOffset] - An offset into the output array.<br />
  237. [page:Array src0] - The source array of the starting quaternion.<br />
  238. [page:Integer srcOffset0] - An offset into the array `src0`.<br />
  239. [page:Array src1] - The source array of the target quaternion.<br />
  240. [page:Integer srcOffset1] - An offset into the array `src1`.<br />
  241. [page:Float t] - Normalized interpolation factor (between `0` and `1`).<br /><br />
  242. This SLERP implementation assumes the quaternion data are managed in flat
  243. arrays.
  244. </p>
  245. <h3>
  246. [method:Array multiplyQuaternionsFlat]( [param:Array dst], [param:Integer dstOffset],
  247. [param:Array src0], [param:Integer srcOffset0], [param:Array src1], [param:Integer srcOffset1] )
  248. </h3>
  249. <p>
  250. [page:Array dst] - The output array.<br />
  251. [page:Integer dstOffset] - An offset into the output array.<br />
  252. [page:Array src0] - The source array of the starting quaternion.<br />
  253. [page:Integer srcOffset0] - An offset into the array `src0`.<br />
  254. [page:Array src1] - The source array of the target quaternion.<br />
  255. [page:Integer srcOffset1] - An offset into the array `src1`.<br /><br />
  256. This multiplication implementation assumes the quaternion data are managed
  257. in flat arrays.
  258. </p>
  259. <!-- Note: Do not add non-static methods to the bottom of this page. Put them above the <h2>Static Methods</h2> -->
  260. <h2>Source</h2>
  261. <p>
  262. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  263. </p>
  264. </body>
  265. </html>