@ac-kit/math-linear 0.2.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (178) hide show
  1. package/CHANGELOG.md +337 -0
  2. package/README.md +232 -16
  3. package/dist/_brand.d.ts +14 -0
  4. package/dist/_brand.d.ts.map +1 -0
  5. package/dist/_brand.js +2 -0
  6. package/dist/_brand.js.map +1 -0
  7. package/dist/_invert.d.ts +214 -0
  8. package/dist/_invert.d.ts.map +1 -0
  9. package/dist/_invert.js +884 -0
  10. package/dist/_invert.js.map +1 -0
  11. package/dist/_slerp.d.ts +29 -0
  12. package/dist/_slerp.d.ts.map +1 -1
  13. package/dist/_slerp.js +103 -0
  14. package/dist/_slerp.js.map +1 -1
  15. package/dist/_strided-range.d.ts +23 -0
  16. package/dist/_strided-range.d.ts.map +1 -0
  17. package/dist/_strided-range.js +75 -0
  18. package/dist/_strided-range.js.map +1 -0
  19. package/dist/_trs.d.ts +38 -0
  20. package/dist/_trs.d.ts.map +1 -0
  21. package/dist/_trs.js +132 -0
  22. package/dist/_trs.js.map +1 -0
  23. package/dist/affine2-array.d.ts +53 -0
  24. package/dist/affine2-array.d.ts.map +1 -0
  25. package/dist/affine2-array.js +107 -0
  26. package/dist/affine2-array.js.map +1 -0
  27. package/dist/affine2-componentwise.d.ts +28 -0
  28. package/dist/affine2-componentwise.d.ts.map +1 -0
  29. package/dist/affine2-componentwise.js +66 -0
  30. package/dist/affine2-componentwise.js.map +1 -0
  31. package/dist/affine2.d.ts +156 -0
  32. package/dist/affine2.d.ts.map +1 -0
  33. package/dist/affine2.js +437 -0
  34. package/dist/affine2.js.map +1 -0
  35. package/dist/affine3-array.d.ts +53 -0
  36. package/dist/affine3-array.d.ts.map +1 -0
  37. package/dist/affine3-array.js +137 -0
  38. package/dist/affine3-array.js.map +1 -0
  39. package/dist/affine3-componentwise.d.ts +32 -0
  40. package/dist/affine3-componentwise.d.ts.map +1 -0
  41. package/dist/affine3-componentwise.js +100 -0
  42. package/dist/affine3-componentwise.js.map +1 -0
  43. package/dist/affine3.d.ts +205 -0
  44. package/dist/affine3.d.ts.map +1 -0
  45. package/dist/affine3.js +792 -0
  46. package/dist/affine3.js.map +1 -0
  47. package/dist/checked/index.d.ts +35 -0
  48. package/dist/checked/index.d.ts.map +1 -0
  49. package/dist/checked/index.js +58 -0
  50. package/dist/checked/index.js.map +1 -0
  51. package/dist/euler.d.ts +65 -0
  52. package/dist/euler.d.ts.map +1 -0
  53. package/dist/euler.js +217 -0
  54. package/dist/euler.js.map +1 -0
  55. package/dist/index.d.ts +29 -0
  56. package/dist/index.d.ts.map +1 -1
  57. package/dist/index.js +29 -0
  58. package/dist/index.js.map +1 -1
  59. package/dist/kl-activations.d.ts.map +1 -1
  60. package/dist/kl-activations.js +4 -3
  61. package/dist/kl-activations.js.map +1 -1
  62. package/dist/mat2x2-array.d.ts +36 -0
  63. package/dist/mat2x2-array.d.ts.map +1 -0
  64. package/dist/mat2x2-array.js +71 -0
  65. package/dist/mat2x2-array.js.map +1 -0
  66. package/dist/mat2x2-componentwise.d.ts +86 -0
  67. package/dist/mat2x2-componentwise.d.ts.map +1 -0
  68. package/dist/mat2x2-componentwise.js +199 -0
  69. package/dist/mat2x2-componentwise.js.map +1 -0
  70. package/dist/mat2x2.d.ts +79 -25
  71. package/dist/mat2x2.d.ts.map +1 -1
  72. package/dist/mat2x2.js +192 -93
  73. package/dist/mat2x2.js.map +1 -1
  74. package/dist/mat3x3-array.d.ts +36 -0
  75. package/dist/mat3x3-array.d.ts.map +1 -0
  76. package/dist/mat3x3-array.js +91 -0
  77. package/dist/mat3x3-array.js.map +1 -0
  78. package/dist/mat3x3-componentwise.d.ts +86 -0
  79. package/dist/mat3x3-componentwise.d.ts.map +1 -0
  80. package/dist/mat3x3-componentwise.js +299 -0
  81. package/dist/mat3x3-componentwise.js.map +1 -0
  82. package/dist/mat3x3.d.ts +153 -48
  83. package/dist/mat3x3.d.ts.map +1 -1
  84. package/dist/mat3x3.js +470 -192
  85. package/dist/mat3x3.js.map +1 -1
  86. package/dist/mat4x4-array.d.ts +46 -0
  87. package/dist/mat4x4-array.d.ts.map +1 -0
  88. package/dist/mat4x4-array.js +133 -0
  89. package/dist/mat4x4-array.js.map +1 -0
  90. package/dist/mat4x4-componentwise.d.ts +87 -0
  91. package/dist/mat4x4-componentwise.d.ts.map +1 -0
  92. package/dist/mat4x4-componentwise.js +434 -0
  93. package/dist/mat4x4-componentwise.js.map +1 -0
  94. package/dist/mat4x4.d.ts +221 -67
  95. package/dist/mat4x4.d.ts.map +1 -1
  96. package/dist/mat4x4.js +972 -334
  97. package/dist/mat4x4.js.map +1 -1
  98. package/dist/projection.d.ts +113 -0
  99. package/dist/projection.d.ts.map +1 -0
  100. package/dist/projection.js +418 -0
  101. package/dist/projection.js.map +1 -0
  102. package/dist/quaternion-array.d.ts +47 -0
  103. package/dist/quaternion-array.d.ts.map +1 -0
  104. package/dist/quaternion-array.js +86 -0
  105. package/dist/quaternion-array.js.map +1 -0
  106. package/dist/quaternion.d.ts +106 -22
  107. package/dist/quaternion.d.ts.map +1 -1
  108. package/dist/quaternion.js +253 -182
  109. package/dist/quaternion.js.map +1 -1
  110. package/dist/transform.d.ts +220 -0
  111. package/dist/transform.d.ts.map +1 -0
  112. package/dist/transform.js +412 -0
  113. package/dist/transform.js.map +1 -0
  114. package/dist/tridiagonal.d.ts +52 -0
  115. package/dist/tridiagonal.d.ts.map +1 -0
  116. package/dist/tridiagonal.js +90 -0
  117. package/dist/tridiagonal.js.map +1 -0
  118. package/dist/upload.d.ts +55 -0
  119. package/dist/upload.d.ts.map +1 -0
  120. package/dist/upload.js +129 -0
  121. package/dist/upload.js.map +1 -0
  122. package/dist/vec2-array-componentwise.d.ts +207 -0
  123. package/dist/vec2-array-componentwise.d.ts.map +1 -0
  124. package/dist/vec2-array-componentwise.js +472 -0
  125. package/dist/vec2-array-componentwise.js.map +1 -0
  126. package/dist/vec2-array.d.ts +53 -0
  127. package/dist/vec2-array.d.ts.map +1 -0
  128. package/dist/vec2-array.js +123 -0
  129. package/dist/vec2-array.js.map +1 -0
  130. package/dist/vec2-componentwise.d.ts +311 -0
  131. package/dist/vec2-componentwise.d.ts.map +1 -0
  132. package/dist/vec2-componentwise.js +542 -0
  133. package/dist/vec2-componentwise.js.map +1 -0
  134. package/dist/vec2.d.ts +97 -82
  135. package/dist/vec2.d.ts.map +1 -1
  136. package/dist/vec2.js +121 -216
  137. package/dist/vec2.js.map +1 -1
  138. package/dist/vec3-array-componentwise.d.ts +207 -0
  139. package/dist/vec3-array-componentwise.d.ts.map +1 -0
  140. package/dist/vec3-array-componentwise.js +510 -0
  141. package/dist/vec3-array-componentwise.js.map +1 -0
  142. package/dist/vec3-array.d.ts +102 -0
  143. package/dist/vec3-array.d.ts.map +1 -0
  144. package/dist/vec3-array.js +299 -0
  145. package/dist/vec3-array.js.map +1 -0
  146. package/dist/vec3-componentwise.d.ts +310 -0
  147. package/dist/vec3-componentwise.d.ts.map +1 -0
  148. package/dist/vec3-componentwise.js +594 -0
  149. package/dist/vec3-componentwise.js.map +1 -0
  150. package/dist/vec3.d.ts +177 -87
  151. package/dist/vec3.d.ts.map +1 -1
  152. package/dist/vec3.js +391 -272
  153. package/dist/vec3.js.map +1 -1
  154. package/dist/vec4-array-componentwise.d.ts +198 -0
  155. package/dist/vec4-array-componentwise.d.ts.map +1 -0
  156. package/dist/vec4-array-componentwise.js +521 -0
  157. package/dist/vec4-array-componentwise.js.map +1 -0
  158. package/dist/vec4-array.d.ts +12 -0
  159. package/dist/vec4-array.d.ts.map +1 -0
  160. package/dist/vec4-array.js +43 -0
  161. package/dist/vec4-array.js.map +1 -0
  162. package/dist/vec4-componentwise.d.ts +299 -0
  163. package/dist/vec4-componentwise.d.ts.map +1 -0
  164. package/dist/vec4-componentwise.js +637 -0
  165. package/dist/vec4-componentwise.js.map +1 -0
  166. package/dist/vec4.d.ts +59 -53
  167. package/dist/vec4.d.ts.map +1 -1
  168. package/dist/vec4.js +74 -249
  169. package/dist/vec4.js.map +1 -1
  170. package/dist/view-basis.d.ts +15 -0
  171. package/dist/view-basis.d.ts.map +1 -0
  172. package/dist/view-basis.js +29 -0
  173. package/dist/view-basis.js.map +1 -0
  174. package/dist/viewport.d.ts +114 -0
  175. package/dist/viewport.d.ts.map +1 -0
  176. package/dist/viewport.js +250 -0
  177. package/dist/viewport.js.map +1 -0
  178. package/package.json +19 -9
@@ -1,23 +1,107 @@
1
- import { type Quaternion } from "@ac-kit/math-complex";
2
- import { Mat4x4 } from "./mat4x4.js";
3
- import { Vec3 } from "./vec3.js";
4
- export type { Quaternion } from "@ac-kit/math-complex";
5
- export declare function quaternionAngle(q: Quaternion): number;
6
- export declare function quaternionAxis(q: Quaternion): Vec3;
7
- export declare function quaternionFromAxisAngle(axis: Vec3, angle: number): Quaternion;
8
- /** Converts a rotation matrix to a quaternion. */
9
- export declare function quaternionFromRotationMatrix(m: Mat4x4): Quaternion;
10
- /**
11
- * Creates the shortest-arc quaternion that rotates `from` to `to`. Both vectors
12
- * must be non-zero; they do not need to be unit-length. Handles the
13
- * anti-parallel (180°) case by using an arbitrary perpendicular axis.
14
- */
15
- export declare function quaternionFromVec3ToVec3(from: Vec3, to: Vec3): Quaternion;
16
- /**
17
- * Non-normalized linear interpolation between two quaternions. Faster than
18
- * slerp but does not preserve unit length — normalize if needed.
19
- */
20
- export declare function quaternionLerp(q1: Quaternion, q2: Quaternion, t: number): Quaternion;
21
- export declare function quaternionSlerp(q1: Quaternion, q2: Quaternion, t: number): Quaternion;
22
- export declare function quaternionToRotationMatrix(q: Quaternion): Mat4x4;
1
+ import { type Quaternion, type ReadonlyQuaternion } from "@ac-kit/math-complex";
2
+ import type { ReadonlyMat3x3 } from "./mat3x3.js";
3
+ import type { ReadonlyMat4x4 } from "./mat4x4.js";
4
+ import { type ReadonlyVec3, type Vec3 } from "./vec3.js";
5
+ export { type Quaternion, quaternionAdd, quaternionClone, quaternionConjugate, quaternionCopy, quaternionCreate, quaternionDot, quaternionEquals, quaternionFromArray, quaternionIdentity, quaternionInvert, quaternionInvertScaled, quaternionIsClose, quaternionIsFinite, quaternionLength, quaternionLengthScaled, quaternionLengthSquared, quaternionMultiply, quaternionMultiplyScalar, quaternionNegate, quaternionNormalize, quaternionNormalizeScaled, quaternionOf, quaternionSet, quaternionSubtract, type ReadonlyQuaternion, } from "@ac-kit/math-complex";
6
+ /**
7
+ * The angle of the rotation of `q`, `2·atan2(|v|, |w|)` with `v` the vector
8
+ * part and `w` the scalar part: in `[0, π]` for `q` and `−q` alike (the same
9
+ * rotation), accurate for small angles where `2·acos(w)` is not, and right for
10
+ * a quaternion that is not unit length, as long as its components are between
11
+ * about `1e-150` and `1e150` (the vector part is squared): the zero quaternion
12
+ * gives `0`, and so does a rotation under about `1e-154` rad, while components
13
+ * beyond about `1e154` overflow to `π`. `NaN` gives `NaN`.
14
+ */
15
+ export declare function quaternionAngle(q: ReadonlyQuaternion): number;
16
+ /**
17
+ * Writes the axis of the rotation of `q` into `out`: the unit vector `v / |v|`
18
+ * of the vector part, negated when the scalar part is negative so that the pair
19
+ * of this axis and {@link quaternionAngle} (in `[0, π]`) is the rotation of `q`
20
+ * and of `−q` alike. A quaternion with no vector part, the identity, has no
21
+ * axis and gives `[1, 0, 0]`. Accurate at every angle, however small, and for a
22
+ * quaternion that is not unit length. `NaN` gives `NaN`.
23
+ */
24
+ export declare function quaternionAxis(out: Vec3, q: ReadonlyQuaternion): Vec3;
25
+ /**
26
+ * Writes the rotation by `angle` radians about the unit vector `axis`
27
+ * (right-hand rule) into `out`: `[axis · sin(angle/2), cos(angle/2)]`. `axis`
28
+ * is not normalised here.
29
+ */
30
+ export declare function quaternionFromAxisAngle(out: Quaternion, axis: ReadonlyVec3, angle: number): Quaternion;
31
+ /**
32
+ * Writes the unit quaternion of a rotation matrix into `out` (Shepperd's
33
+ * method: the largest of the trace and the diagonal entries picks the formula,
34
+ * so the divisor never vanishes). `m` must be orthonormal with a positive
35
+ * determinant; the result is not normalised, and `q` and `−q` are the same
36
+ * rotation.
37
+ */
38
+ export declare function quaternionFromMat3x3(out: Quaternion, m: ReadonlyMat3x3): Quaternion;
39
+ /**
40
+ * Writes the unit quaternion of the rotation in the upper-left 3 × 3 block of
41
+ * `m` into `out`, as {@link quaternionFromMat3x3}; the translation and the last
42
+ * row are ignored.
43
+ */
44
+ export declare function quaternionFromMat4x4(out: Quaternion, m: ReadonlyMat4x4): Quaternion;
45
+ /**
46
+ * Writes into `out` the shortest-arc rotation that turns the direction `from`
47
+ * onto the direction `to`. Neither needs to be unit length (they are normalised
48
+ * at any magnitude). Opposite directions, whose shortest arc is not unique,
49
+ * turn by `π` about {@link vec3Perpendicular} of `from`. A zero vector has no
50
+ * direction and gives the identity; `NaN` gives `NaN`. The rotation misses `to`
51
+ * by a few `1e-16` rad except near opposite directions, where no method does
52
+ * better than a few `1e-8` rad.
53
+ */
54
+ export declare function quaternionFromVec3ToVec3(out: Quaternion, from: ReadonlyVec3, to: ReadonlyVec3): Quaternion;
55
+ /**
56
+ * Writes `q · Rx(angle)` into `out`: the rotation by `angle` radians about the
57
+ * x axis applied first, in the local frame of `q`. `out` may be `q`.
58
+ */
59
+ export declare function quaternionRotateX(out: Quaternion, q: ReadonlyQuaternion, angle: number): Quaternion;
60
+ /**
61
+ * Writes `q · Ry(angle)` into `out`, as {@link quaternionRotateX} about the y
62
+ * axis. `out` may be `q`.
63
+ */
64
+ export declare function quaternionRotateY(out: Quaternion, q: ReadonlyQuaternion, angle: number): Quaternion;
65
+ /**
66
+ * Writes `q · Rz(angle)` into `out`, as {@link quaternionRotateX} about the z
67
+ * axis. `out` may be `q`.
68
+ */
69
+ export declare function quaternionRotateZ(out: Quaternion, q: ReadonlyQuaternion, angle: number): Quaternion;
70
+ /**
71
+ * Writes into `out` the normalised linear interpolation from `a` to `b`, along
72
+ * the shorter arc: `b` is negated first when the two lie in opposite
73
+ * hemispheres (`a · b < 0`, decided exactly where the plain dot product is
74
+ * within its own rounding error), then `a + t·(b − a)` is scaled to unit
75
+ * length. Cheaper than {@link quaternionSlerp} and the same path, but the angle
76
+ * does not grow evenly with `t`, and `t` outside `[0, 1]` extrapolates (the
77
+ * result is right for `|t|` up to about `1e150`, beyond which the normalisation
78
+ * overflows). `out` may be `a` or `b`.
79
+ */
80
+ export declare function quaternionNlerp(out: Quaternion, a: ReadonlyQuaternion, b: ReadonlyQuaternion, t: number): Quaternion;
81
+ /**
82
+ * Writes into `out` the spherical linear interpolation from `a` to `b` along
83
+ * the shorter arc, at constant angular speed: `t = 0` is `a`, `t = 1` is `b` or
84
+ * `−b` (the same rotation), and `t` outside `[0, 1]` extrapolates along the
85
+ * same arc at the same speed. The hemisphere is decided as in
86
+ * {@link quaternionNlerp}. Both must be unit length. Within `[0, 1]`, where the
87
+ * two are within about `1.8°` of each other, the interpolation is the
88
+ * normalised linear one, the same arc to within `1e-3` rad of parameterisation;
89
+ * elsewhere the arc is evaluated in an orthonormal basis of its plane, `a ·
90
+ * cos(t · θ) + tangent · sin(t · θ)`, whose weights never cancel: a rotation of
91
+ * `1e-16` rad extrapolated to `t = 1e16` lands on the unit quaternion half a
92
+ * radian on. The angle `t · θ` it turns is right to its last digits while `|t ·
93
+ * θ|` is below about `1e15` rad (a sine of a larger angle has none left), and
94
+ * `t` past about `1e150` overflows. `out` may be `a` or `b`.
95
+ */
96
+ export declare function quaternionSlerp(out: Quaternion, a: ReadonlyQuaternion, b: ReadonlyQuaternion, t: number): Quaternion;
97
+ /**
98
+ * The angle of the rotation that turns `a` into `b`, that of `a⁻¹ · b`, in `[0,
99
+ * π]`: `q` and `−q` count as the same rotation, and the angle is accurate for
100
+ * rotations that nearly coincide where `2·acos(a · b)` is not. Right for
101
+ * quaternions that are not unit length, as long as their components are between
102
+ * about `1e-150` and `1e150` (the products are squared): the angle reads `0`
103
+ * for a rotation under about `1e-154` rad and overflows to `π` beyond about
104
+ * `1e154`. `NaN` gives `NaN`.
105
+ */
106
+ export declare function quaternionAngleBetween(a: ReadonlyQuaternion, b: ReadonlyQuaternion): number;
23
107
  //# sourceMappingURL=quaternion.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"quaternion.d.ts","sourceRoot":"","sources":["../src/quaternion.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,UAAU,EAIf,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,IAAI,EAAiD,MAAM,WAAW,CAAC;AAEhF,YAAY,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAoEvD,wBAAgB,eAAe,CAAC,CAAC,EAAE,UAAU,GAAG,MAAM,CAErD;AAED,wBAAgB,cAAc,CAAC,CAAC,EAAE,UAAU,GAAG,IAAI,CAMlD;AAED,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,GAAG,UAAU,CAI7E;AAED,kDAAkD;AAClD,wBAAgB,4BAA4B,CAAC,CAAC,EAAE,MAAM,GAAG,UAAU,CAiClE;AAED;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,EAAE,IAAI,GAAG,UAAU,CAuBzE;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAC7B,EAAE,EAAE,UAAU,EACd,EAAE,EAAE,UAAU,EACd,CAAC,EAAE,MAAM,GACP,UAAU,CAUZ;AAED,wBAAgB,eAAe,CAC9B,EAAE,EAAE,UAAU,EACd,EAAE,EAAE,UAAU,EACd,CAAC,EAAE,MAAM,GACP,UAAU,CAmCZ;AAED,wBAAgB,0BAA0B,CAAC,CAAC,EAAE,UAAU,GAAG,MAAM,CAsBhE"}
1
+ {"version":3,"file":"quaternion.d.ts","sourceRoot":"","sources":["../src/quaternion.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,UAAU,EAEf,KAAK,kBAAkB,EACvB,MAAM,sBAAsB,CAAC;AAQ9B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AASlD,OAAO,EACN,KAAK,YAAY,EACjB,KAAK,IAAI,EAGT,MAAM,WAAW,CAAC;AAEnB,OAAO,EACN,KAAK,UAAU,EACf,aAAa,EACb,eAAe,EACf,mBAAmB,EACnB,cAAc,EACd,gBAAgB,EAChB,aAAa,EACb,gBAAgB,EAChB,mBAAmB,EACnB,kBAAkB,EAClB,gBAAgB,EAChB,sBAAsB,EACtB,iBAAiB,EACjB,kBAAkB,EAClB,gBAAgB,EAChB,sBAAsB,EACtB,uBAAuB,EACvB,kBAAkB,EAClB,wBAAwB,EACxB,gBAAgB,EAChB,mBAAmB,EACnB,yBAAyB,EACzB,YAAY,EACZ,aAAa,EACb,kBAAkB,EAClB,KAAK,kBAAkB,GACvB,MAAM,sBAAsB,CAAC;AA6B9B;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,kBAAkB,GAAG,MAAM,CAK7D;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC,EAAE,kBAAkB,GAAG,IAAI,CAYrE;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CACtC,GAAG,EAAE,UAAU,EACf,IAAI,EAAE,YAAY,EAClB,KAAK,EAAE,MAAM,GACX,UAAU,CAQZ;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CACnC,GAAG,EAAE,UAAU,EACf,CAAC,EAAE,cAAc,GACf,UAAU,CAaZ;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CACnC,GAAG,EAAE,UAAU,EACf,CAAC,EAAE,cAAc,GACf,UAAU,CAaZ;AAED;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CACvC,GAAG,EAAE,UAAU,EACf,IAAI,EAAE,YAAY,EAClB,EAAE,EAAE,YAAY,GACd,UAAU,CAoBZ;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAChC,GAAG,EAAE,UAAU,EACf,CAAC,EAAE,kBAAkB,EACrB,KAAK,EAAE,MAAM,GACX,UAAU,CAaZ;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAChC,GAAG,EAAE,UAAU,EACf,CAAC,EAAE,kBAAkB,EACrB,KAAK,EAAE,MAAM,GACX,UAAU,CAaZ;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAChC,GAAG,EAAE,UAAU,EACf,CAAC,EAAE,kBAAkB,EACrB,KAAK,EAAE,MAAM,GACX,UAAU,CAaZ;AAED;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAC9B,GAAG,EAAE,UAAU,EACf,CAAC,EAAE,kBAAkB,EACrB,CAAC,EAAE,kBAAkB,EACrB,CAAC,EAAE,MAAM,GACP,UAAU,CAqBZ;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,eAAe,CAC9B,GAAG,EAAE,UAAU,EACf,CAAC,EAAE,kBAAkB,EACrB,CAAC,EAAE,kBAAkB,EACrB,CAAC,EAAE,MAAM,GACP,UAAU,CAyBZ;AAED;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,CACrC,CAAC,EAAE,kBAAkB,EACrB,CAAC,EAAE,kBAAkB,GACnB,MAAM,CAgBR"}
@@ -1,208 +1,279 @@
1
- import { QUATERNION_IDENTITY, quaternionDot, quaternionNormalize, } from "@ac-kit/math-complex";
2
- import { SLERP_LINEAR_FALLBACK_DOT_ } from "./_slerp.js";
3
- import { vec3Cross, vec3Dot, vec3Length, vec3Normalize } from "./vec3.js";
1
+ import { quaternionNormalize, } from "@ac-kit/math-complex";
2
+ import { isOppositeHemisphere_, SLERP_LINEAR_FALLBACK_DOT_, slerpArc4_, } from "./_slerp.js";
3
+ import { quaternionFromRotationEntries_ } from "./_trs.js";
4
+ import { vec3Create, vec3Dot, vec3Length, vec3Negate, vec3NormalizeScaled, vec3Set, } from "./vec3-componentwise.js";
5
+ import { vec3Cross, vec3Perpendicular, } from "./vec3.js";
6
+ export { quaternionAdd, quaternionClone, quaternionConjugate, quaternionCopy, quaternionCreate, quaternionDot, quaternionEquals, quaternionFromArray, quaternionIdentity, quaternionInvert, quaternionInvertScaled, quaternionIsClose, quaternionIsFinite, quaternionLength, quaternionLengthScaled, quaternionLengthSquared, quaternionMultiply, quaternionMultiplyScalar, quaternionNegate, quaternionNormalize, quaternionNormalizeScaled, quaternionOf, quaternionSet, quaternionSubtract, } from "@ac-kit/math-complex";
4
7
  /**
5
- * How far `a·b` may sit from `±1` before two unit vectors are taken as
6
- * collinear, in {@link quaternionFromVec3ToVec3}.
8
+ * The sine of the angle between two directions below which they count as
9
+ * opposite in {@link quaternionFromVec3ToVec3}, which then turns by `π` about a
10
+ * perpendicular of the first instead of building the quaternion `[a×b, 1 +
11
+ * a·b]`.
7
12
  *
8
- * That function builds the unnormalised quaternion `[a×b, 1 + a·b]`, whose norm
9
- * has a closed form for unit `a` and `b`:
10
- *
11
- * ```text
12
- * ‖[a×b, 1 + a·b]‖² = |a×b|² + (1 + a·b)²
13
- * = (1 − dot²) + (1 + 2·dot + dot²)
14
- * = 2·(1 + dot)
15
- * ```
16
- *
17
- * So the norm is `√(2(1 + dot))`, which collapses to `0` as `dot → −1`: both
18
- * the axis and the scalar part vanish together, and normalising divides one
19
- * near-zero by another. At this margin the norm is still `√(2e-10) ≈ 1.4e-5`,
20
- * large enough to divide by; past it the anti-parallel branch takes over.
21
- *
22
- * In angle, `1 − cos θ ≈ θ²/2`, so the margin corresponds to `θ ≤ √(2e-10) ≈
23
- * 1.4e-5 rad` of the parallel case and `θ ≥ π − 1.4e-5 rad` of the
24
- * anti-parallel one.
25
- *
26
- * The parallel side is a shortcut rather than a rescue — there the norm tends
27
- * to `2` and the general formula would return the identity anyway.
13
+ * Near opposite directions the cross product `a×b` has the length `sin θ` but
14
+ * the absolute error of a rounded product, about `1e-16`, so its direction, the
15
+ * axis of the rotation, is off by `1e-16 / sin θ` and the rotation misses `b`
16
+ * by as much: the problem is that ill-conditioned (the shortest arc is not even
17
+ * continuous at `θ = π`). The `π` turn about a perpendicular misses by the
18
+ * angle to `π`, about `sin θ`. The two errors meet at `sin θ ≈ 1e-8`, which
19
+ * bounds the miss at a few `1e-8` rad everywhere; the quaternion is within a
20
+ * few `1e-16` of exact for every other pair, parallel included.
28
21
  *
29
22
  * Not a tolerance, and deliberately not configurable.
30
23
  */
31
- const COLLINEAR_DOT_MARGIN_ = 1e-10;
24
+ const OPPOSITE_MAX_SINE_ = 2e-8;
32
25
  /**
33
- * Shortest perpendicular accepted when picking an arbitrary rotation axis for
34
- * the anti-parallel (180°) case in {@link quaternionFromVec3ToVec3}.
35
- *
36
- * That branch tries `a × x̂` first. For unit `a` the length is a sine, `|a ×
37
- * x̂| = sin φ` with `φ` the angle between `a` and the x-axis, so this bounds `φ
38
- * ≥ 1e-6 rad`. Below it the cross product has lost its direction and the branch
39
- * retries against `ŷ`, which cannot also be near-parallel to `a`.
40
- *
41
- * Tighter in angle than {@link COLLINEAR_DOT_MARGIN_} despite the larger
42
- * literal: a sine is linear in the angle where `1 − cos` is quadratic, so
43
- * `1e-6` here means `1e-6 rad` against the other's `1.4e-5 rad`. The two bound
44
- * different quantities and their raw values are not comparable.
26
+ * Scratch for {@link quaternionFromVec3ToVec3}: one call at a time, no callback
27
+ * inside it.
45
28
  */
46
- const PERPENDICULAR_MIN_SINE_ = 1e-6;
29
+ const FROM_DIRECTION_ = vec3Create();
30
+ const TO_DIRECTION_ = vec3Create();
31
+ const ARC_AXIS_ = vec3Create();
47
32
  /**
48
- * Smallest `|sin(θ/2)|` at which a quaternion still has a well-defined rotation
49
- * axis, in {@link quaternionAxis}.
50
- *
51
- * For a unit quaternion `q = [v·sin(θ/2), cos(θ/2)]`, the axis is recovered as
52
- *
53
- * ```text
54
- * s = √(1 − qᵤ²) = |sin(θ/2)|
55
- * v = [qₓ, qᵧ, q_z] / s
56
- * ```
57
- *
58
- * And the numerator is itself of order `s`, so the quotient degenerates into
59
- * `0/0` as `θ → 0`. The identity rotation genuinely has no axis, so there is
60
- * nothing to recover and the function returns an arbitrary one.
61
- *
62
- * `θ ≈ 2·asin(1e-3) ≈ 2e-3 rad ≈ 0.115°` — far looser than the two margins
63
- * above because it protects a direction nobody can observe at that angle, not a
64
- * quantity fed into further arithmetic.
33
+ * The angle of the rotation of `q`, `2·atan2(|v|, |w|)` with `v` the vector
34
+ * part and `w` the scalar part: in `[0, π]` for `q` and `−q` alike (the same
35
+ * rotation), accurate for small angles where `2·acos(w)` is not, and right for
36
+ * a quaternion that is not unit length, as long as its components are between
37
+ * about `1e-150` and `1e150` (the vector part is squared): the zero quaternion
38
+ * gives `0`, and so does a rotation under about `1e-154` rad, while components
39
+ * beyond about `1e154` overflow to `π`. `NaN` gives `NaN`.
65
40
  */
66
- const AXIS_MIN_HALF_ANGLE_SINE_ = 0.001;
67
41
  export function quaternionAngle(q) {
68
- return 2 * Math.acos(q[3]);
42
+ const x = q[0];
43
+ const y = q[1];
44
+ const z = q[2];
45
+ return 2 * Math.atan2(Math.sqrt(x * x + y * y + z * z), Math.abs(q[3]));
69
46
  }
70
- export function quaternionAxis(q) {
71
- const s = Math.sqrt(1 - q[3] * q[3]);
72
- if (s < AXIS_MIN_HALF_ANGLE_SINE_) {
73
- return [1, 0, 0];
47
+ /**
48
+ * Writes the axis of the rotation of `q` into `out`: the unit vector `v / |v|`
49
+ * of the vector part, negated when the scalar part is negative so that the pair
50
+ * of this axis and {@link quaternionAngle} (in `[0, π]`) is the rotation of `q`
51
+ * and of `−q` alike. A quaternion with no vector part, the identity, has no
52
+ * axis and gives `[1, 0, 0]`. Accurate at every angle, however small, and for a
53
+ * quaternion that is not unit length. `NaN` gives `NaN`.
54
+ */
55
+ export function quaternionAxis(out, q) {
56
+ const x = q[0];
57
+ const y = q[1];
58
+ const z = q[2];
59
+ if (x === 0 && y === 0 && z === 0) {
60
+ out[0] = 1;
61
+ out[1] = 0;
62
+ out[2] = 0;
63
+ return out;
74
64
  }
75
- return [q[0] / s, q[1] / s, q[2] / s];
65
+ vec3NormalizeScaled(vec3Set(out, x, y, z), out);
66
+ return q[3] < 0 ? vec3Negate(out, out) : out;
76
67
  }
77
- export function quaternionFromAxisAngle(axis, angle) {
78
- const halfAngle = angle / 2;
79
- const s = Math.sin(halfAngle);
80
- return [axis[0] * s, axis[1] * s, axis[2] * s, Math.cos(halfAngle)];
68
+ /**
69
+ * Writes the rotation by `angle` radians about the unit vector `axis`
70
+ * (right-hand rule) into `out`: `[axis · sin(angle/2), cos(angle/2)]`. `axis`
71
+ * is not normalised here.
72
+ */
73
+ export function quaternionFromAxisAngle(out, axis, angle) {
74
+ const half = angle / 2;
75
+ const sin = Math.sin(half);
76
+ out[0] = axis[0] * sin;
77
+ out[1] = axis[1] * sin;
78
+ out[2] = axis[2] * sin;
79
+ out[3] = Math.cos(half);
80
+ return out;
81
81
  }
82
- /** Converts a rotation matrix to a quaternion. */
83
- export function quaternionFromRotationMatrix(m) {
84
- const trace = m[0][0] + m[1][1] + m[2][2];
85
- let x, y, z, w;
86
- if (trace > 0) {
87
- const s = 0.5 / Math.sqrt(trace + 1.0);
88
- w = 0.25 / s;
89
- x = (m[2][1] - m[1][2]) * s;
90
- y = (m[0][2] - m[2][0]) * s;
91
- z = (m[1][0] - m[0][1]) * s;
92
- }
93
- else {
94
- if (m[0][0] > m[1][1] && m[0][0] > m[2][2]) {
95
- const s = 2.0 * Math.sqrt(1.0 + m[0][0] - m[1][1] - m[2][2]);
96
- w = (m[2][1] - m[1][2]) / s;
97
- x = 0.25 * s;
98
- y = (m[0][1] + m[1][0]) / s;
99
- z = (m[0][2] + m[2][0]) / s;
100
- }
101
- else if (m[1][1] > m[2][2]) {
102
- const s = 2.0 * Math.sqrt(1.0 + m[1][1] - m[0][0] - m[2][2]);
103
- w = (m[0][2] - m[2][0]) / s;
104
- x = (m[0][1] + m[1][0]) / s;
105
- y = 0.25 * s;
106
- z = (m[1][2] + m[2][1]) / s;
107
- }
108
- else {
109
- const s = 2.0 * Math.sqrt(1.0 + m[2][2] - m[0][0] - m[1][1]);
110
- w = (m[1][0] - m[0][1]) / s;
111
- x = (m[0][2] + m[2][0]) / s;
112
- y = (m[1][2] + m[2][1]) / s;
113
- z = 0.25 * s;
114
- }
115
- }
116
- return [x, y, z, w];
82
+ /**
83
+ * Writes the unit quaternion of a rotation matrix into `out` (Shepperd's
84
+ * method: the largest of the trace and the diagonal entries picks the formula,
85
+ * so the divisor never vanishes). `m` must be orthonormal with a positive
86
+ * determinant; the result is not normalised, and `q` and `−q` are the same
87
+ * rotation.
88
+ */
89
+ export function quaternionFromMat3x3(out, m) {
90
+ return quaternionFromRotationEntries_(out, m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);
117
91
  }
118
92
  /**
119
- * Creates the shortest-arc quaternion that rotates `from` to `to`. Both vectors
120
- * must be non-zero; they do not need to be unit-length. Handles the
121
- * anti-parallel (180°) case by using an arbitrary perpendicular axis.
93
+ * Writes the unit quaternion of the rotation in the upper-left 3 × 3 block of
94
+ * `m` into `out`, as {@link quaternionFromMat3x3}; the translation and the last
95
+ * row are ignored.
122
96
  */
123
- export function quaternionFromVec3ToVec3(from, to) {
124
- const fromN = vec3Normalize(from);
125
- const toN = vec3Normalize(to);
126
- const dot = vec3Dot(fromN, toN);
127
- if (dot >= 1 - COLLINEAR_DOT_MARGIN_) {
128
- // Vectors are parallel — identity rotation
129
- return QUATERNION_IDENTITY;
130
- }
131
- if (dot <= -1 + COLLINEAR_DOT_MARGIN_) {
132
- // Vectors are anti-parallel — rotate 180° around an arbitrary perpendicular axis
133
- let perp = vec3Cross(fromN, [1, 0, 0]);
134
- if (vec3Length(perp) < PERPENDICULAR_MIN_SINE_) {
135
- perp = vec3Cross(fromN, [0, 1, 0]);
136
- }
137
- return quaternionFromAxisAngle(vec3Normalize(perp), Math.PI);
97
+ export function quaternionFromMat4x4(out, m) {
98
+ return quaternionFromRotationEntries_(out, m[0], m[1], m[2], m[4], m[5], m[6], m[8], m[9], m[10]);
99
+ }
100
+ /**
101
+ * Writes into `out` the shortest-arc rotation that turns the direction `from`
102
+ * onto the direction `to`. Neither needs to be unit length (they are normalised
103
+ * at any magnitude). Opposite directions, whose shortest arc is not unique,
104
+ * turn by `π` about {@link vec3Perpendicular} of `from`. A zero vector has no
105
+ * direction and gives the identity; `NaN` gives `NaN`. The rotation misses `to`
106
+ * by a few `1e-16` rad except near opposite directions, where no method does
107
+ * better than a few `1e-8` rad.
108
+ */
109
+ export function quaternionFromVec3ToVec3(out, from, to) {
110
+ vec3NormalizeScaled(FROM_DIRECTION_, from);
111
+ vec3NormalizeScaled(TO_DIRECTION_, to);
112
+ vec3Cross(ARC_AXIS_, FROM_DIRECTION_, TO_DIRECTION_);
113
+ const cosine = vec3Dot(FROM_DIRECTION_, TO_DIRECTION_);
114
+ if (cosine < 0 && vec3Length(ARC_AXIS_) <= OPPOSITE_MAX_SINE_) {
115
+ vec3Perpendicular(ARC_AXIS_, FROM_DIRECTION_);
116
+ out[0] = ARC_AXIS_[0];
117
+ out[1] = ARC_AXIS_[1];
118
+ out[2] = ARC_AXIS_[2];
119
+ out[3] = 0;
120
+ return out;
138
121
  }
139
- const axis = vec3Cross(fromN, toN);
140
- const w = 1 + dot;
141
- // axis × sin(θ/2), w = cos(θ/2) — unnormalized; quaternionNormalize ensures unit length
142
- return quaternionNormalize([axis[0], axis[1], axis[2], w]);
122
+ // [sin θ · n, 1 + cos θ] is 2·cos(θ/2) times the unit quaternion of the arc,
123
+ // for every θ but π.
124
+ out[0] = ARC_AXIS_[0];
125
+ out[1] = ARC_AXIS_[1];
126
+ out[2] = ARC_AXIS_[2];
127
+ out[3] = 1 + cosine;
128
+ return quaternionNormalize(out, out);
143
129
  }
144
130
  /**
145
- * Non-normalized linear interpolation between two quaternions. Faster than
146
- * slerp but does not preserve unit length — normalize if needed.
131
+ * Writes `q · Rx(angle)` into `out`: the rotation by `angle` radians about the
132
+ * x axis applied first, in the local frame of `q`. `out` may be `q`.
147
133
  */
148
- export function quaternionLerp(q1, q2, t) {
149
- const dot = quaternionDot(q1, q2);
150
- // Negate q2 if dot < 0 to take the shorter path
151
- const q2s = dot < 0 ? [-q2[0], -q2[1], -q2[2], -q2[3]] : q2;
152
- return [
153
- q1[0] + t * (q2s[0] - q1[0]),
154
- q1[1] + t * (q2s[1] - q1[1]),
155
- q1[2] + t * (q2s[2] - q1[2]),
156
- q1[3] + t * (q2s[3] - q1[3]),
157
- ];
134
+ export function quaternionRotateX(out, q, angle) {
135
+ const x = q[0];
136
+ const y = q[1];
137
+ const z = q[2];
138
+ const w = q[3];
139
+ const half = angle / 2;
140
+ const sin = Math.sin(half);
141
+ const cos = Math.cos(half);
142
+ out[0] = x * cos + w * sin;
143
+ out[1] = y * cos + z * sin;
144
+ out[2] = z * cos - y * sin;
145
+ out[3] = w * cos - x * sin;
146
+ return out;
158
147
  }
159
- export function quaternionSlerp(q1, q2, t) {
160
- // Compute the cosine of the angle between the two quaternions
161
- let dot = q1[0] * q2[0] + q1[1] * q2[1] + q1[2] * q2[2] + q1[3] * q2[3];
162
- // If the dot product is negative, negate one quaternion to take the shorter path
163
- if (dot < 0.0) {
164
- q2 = [-q2[0], -q2[1], -q2[2], -q2[3]];
165
- dot = -dot;
166
- }
167
- if (dot > SLERP_LINEAR_FALLBACK_DOT_) {
168
- const result = [
169
- q1[0] + t * (q2[0] - q1[0]),
170
- q1[1] + t * (q2[1] - q1[1]),
171
- q1[2] + t * (q2[2] - q1[2]),
172
- q1[3] + t * (q2[3] - q1[3]),
173
- ];
174
- return quaternionNormalize(result);
148
+ /**
149
+ * Writes `q · Ry(angle)` into `out`, as {@link quaternionRotateX} about the y
150
+ * axis. `out` may be `q`.
151
+ */
152
+ export function quaternionRotateY(out, q, angle) {
153
+ const x = q[0];
154
+ const y = q[1];
155
+ const z = q[2];
156
+ const w = q[3];
157
+ const half = angle / 2;
158
+ const sin = Math.sin(half);
159
+ const cos = Math.cos(half);
160
+ out[0] = x * cos - z * sin;
161
+ out[1] = y * cos + w * sin;
162
+ out[2] = z * cos + x * sin;
163
+ out[3] = w * cos - y * sin;
164
+ return out;
165
+ }
166
+ /**
167
+ * Writes `q · Rz(angle)` into `out`, as {@link quaternionRotateX} about the z
168
+ * axis. `out` may be `q`.
169
+ */
170
+ export function quaternionRotateZ(out, q, angle) {
171
+ const x = q[0];
172
+ const y = q[1];
173
+ const z = q[2];
174
+ const w = q[3];
175
+ const half = angle / 2;
176
+ const sin = Math.sin(half);
177
+ const cos = Math.cos(half);
178
+ out[0] = x * cos + y * sin;
179
+ out[1] = y * cos - x * sin;
180
+ out[2] = z * cos + w * sin;
181
+ out[3] = w * cos - z * sin;
182
+ return out;
183
+ }
184
+ /**
185
+ * Writes into `out` the normalised linear interpolation from `a` to `b`, along
186
+ * the shorter arc: `b` is negated first when the two lie in opposite
187
+ * hemispheres (`a · b < 0`, decided exactly where the plain dot product is
188
+ * within its own rounding error), then `a + t·(b − a)` is scaled to unit
189
+ * length. Cheaper than {@link quaternionSlerp} and the same path, but the angle
190
+ * does not grow evenly with `t`, and `t` outside `[0, 1]` extrapolates (the
191
+ * result is right for `|t|` up to about `1e150`, beyond which the normalisation
192
+ * overflows). `out` may be `a` or `b`.
193
+ */
194
+ export function quaternionNlerp(out, a, b, t) {
195
+ const ax = a[0];
196
+ const ay = a[1];
197
+ const az = a[2];
198
+ const aw = a[3];
199
+ const side = isOppositeHemisphere_(a, b, ax * b[0] + ay * b[1] + az * b[2] + aw * b[3])
200
+ ? -1
201
+ : 1;
202
+ const bx = side * b[0];
203
+ const by = side * b[1];
204
+ const bz = side * b[2];
205
+ const bw = side * b[3];
206
+ out[0] = ax + t * (bx - ax);
207
+ out[1] = ay + t * (by - ay);
208
+ out[2] = az + t * (bz - az);
209
+ out[3] = aw + t * (bw - aw);
210
+ return quaternionNormalize(out, out);
211
+ }
212
+ /**
213
+ * Writes into `out` the spherical linear interpolation from `a` to `b` along
214
+ * the shorter arc, at constant angular speed: `t = 0` is `a`, `t = 1` is `b` or
215
+ * `−b` (the same rotation), and `t` outside `[0, 1]` extrapolates along the
216
+ * same arc at the same speed. The hemisphere is decided as in
217
+ * {@link quaternionNlerp}. Both must be unit length. Within `[0, 1]`, where the
218
+ * two are within about `1.8°` of each other, the interpolation is the
219
+ * normalised linear one, the same arc to within `1e-3` rad of parameterisation;
220
+ * elsewhere the arc is evaluated in an orthonormal basis of its plane, `a ·
221
+ * cos(t · θ) + tangent · sin(t · θ)`, whose weights never cancel: a rotation of
222
+ * `1e-16` rad extrapolated to `t = 1e16` lands on the unit quaternion half a
223
+ * radian on. The angle `t · θ` it turns is right to its last digits while `|t ·
224
+ * θ|` is below about `1e15` rad (a sine of a larger angle has none left), and
225
+ * `t` past about `1e150` overflows. `out` may be `a` or `b`.
226
+ */
227
+ export function quaternionSlerp(out, a, b, t) {
228
+ const ax = a[0];
229
+ const ay = a[1];
230
+ const az = a[2];
231
+ const aw = a[3];
232
+ // The cosine of the angle between the two, plain: its magnitude picks the
233
+ // formula and the angle. Its sign picks the hemisphere, which is decided
234
+ // exactly where the plain sum is within its rounding error.
235
+ const plainDot = ax * b[0] + ay * b[1] + az * b[2] + aw * b[3];
236
+ const dot = Math.abs(plainDot);
237
+ const side = isOppositeHemisphere_(a, b, plainDot) ? -1 : 1;
238
+ const bx = side * b[0];
239
+ const by = side * b[1];
240
+ const bz = side * b[2];
241
+ const bw = side * b[3];
242
+ const interpolating = t >= 0 && t <= 1;
243
+ if (interpolating && dot > SLERP_LINEAR_FALLBACK_DOT_) {
244
+ out[0] = ax + t * (bx - ax);
245
+ out[1] = ay + t * (by - ay);
246
+ out[2] = az + t * (bz - az);
247
+ out[3] = aw + t * (bw - aw);
248
+ return quaternionNormalize(out, out);
175
249
  }
176
- // Calculate the angle between the quaternions
177
- const theta_0 = Math.acos(dot); // theta_0 = angle between input quaternions
178
- const theta = theta_0 * t; // theta = angle between q1 and result
179
- const sin_theta = Math.sin(theta);
180
- const sin_theta_0 = Math.sin(theta_0);
181
- const s1 = Math.cos(theta) - (dot * sin_theta) / sin_theta_0;
182
- const s2 = sin_theta / sin_theta_0;
183
- return [
184
- s1 * q1[0] + s2 * q2[0],
185
- s1 * q1[1] + s2 * q2[1],
186
- s1 * q1[2] + s2 * q2[2],
187
- s1 * q1[3] + s2 * q2[3],
188
- ];
250
+ slerpArc4_(out, ax, ay, az, aw, bx, by, bz, bw, t);
251
+ return out;
189
252
  }
190
- export function quaternionToRotationMatrix(q) {
191
- const x = q[0], y = q[1], z = q[2], w = q[3];
192
- const xx = x * x;
193
- const yy = y * y;
194
- const zz = z * z;
195
- const xy = x * y;
196
- const xz = x * z;
197
- const yz = y * z;
198
- const wx = w * x;
199
- const wy = w * y;
200
- const wz = w * z;
201
- return [
202
- [1 - 2 * (yy + zz), 2 * (xy - wz), 2 * (xz + wy), 0],
203
- [2 * (xy + wz), 1 - 2 * (xx + zz), 2 * (yz - wx), 0],
204
- [2 * (xz - wy), 2 * (yz + wx), 1 - 2 * (xx + yy), 0],
205
- [0, 0, 0, 1],
206
- ];
253
+ /**
254
+ * The angle of the rotation that turns `a` into `b`, that of `a⁻¹ · b`, in `[0,
255
+ * π]`: `q` and `−q` count as the same rotation, and the angle is accurate for
256
+ * rotations that nearly coincide where `2·acos(a · b)` is not. Right for
257
+ * quaternions that are not unit length, as long as their components are between
258
+ * about `1e-150` and `1e150` (the products are squared): the angle reads `0`
259
+ * for a rotation under about `1e-154` rad and overflows to `π` beyond about
260
+ * `1e154`. `NaN` gives `NaN`.
261
+ */
262
+ export function quaternionAngleBetween(a, b) {
263
+ const ax = a[0];
264
+ const ay = a[1];
265
+ const az = a[2];
266
+ const aw = a[3];
267
+ const bx = b[0];
268
+ const by = b[1];
269
+ const bz = b[2];
270
+ const bw = b[3];
271
+ // The conjugate of a times b: the scalar part is a·b, the vector part
272
+ // aw·bv − bw·av − av × bv.
273
+ const w = aw * bw + ax * bx + ay * by + az * bz;
274
+ const x = aw * bx - bw * ax - (ay * bz - az * by);
275
+ const y = aw * by - bw * ay - (az * bx - ax * bz);
276
+ const z = aw * bz - bw * az - (ax * by - ay * bx);
277
+ return 2 * Math.atan2(Math.sqrt(x * x + y * y + z * z), Math.abs(w));
207
278
  }
208
279
  //# sourceMappingURL=quaternion.js.map