Quaternion.html 10 KB

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