Quaternion.html 11 KB

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