@lakuna/umath 1.3.4 → 1.3.6

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 (162) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +7 -9
  3. package/dist/algorithms/combinations.d.ts +7 -0
  4. package/dist/algorithms/combinations.d.ts.map +1 -1
  5. package/dist/algorithms/combinations.js +8 -1
  6. package/dist/algorithms/combinations.js.map +1 -1
  7. package/dist/algorithms/degreesToRadians.d.ts +7 -0
  8. package/dist/algorithms/degreesToRadians.d.ts.map +1 -1
  9. package/dist/algorithms/degreesToRadians.js +7 -0
  10. package/dist/algorithms/degreesToRadians.js.map +1 -1
  11. package/dist/algorithms/factorial.d.ts +6 -0
  12. package/dist/algorithms/factorial.d.ts.map +1 -1
  13. package/dist/algorithms/factorial.js +15 -5
  14. package/dist/algorithms/factorial.js.map +1 -1
  15. package/dist/algorithms/fibonacci.d.ts +6 -1
  16. package/dist/algorithms/fibonacci.d.ts.map +1 -1
  17. package/dist/algorithms/fibonacci.js +6 -1
  18. package/dist/algorithms/fibonacci.js.map +1 -1
  19. package/dist/algorithms/greatestCommonDivisor.d.ts +14 -0
  20. package/dist/algorithms/greatestCommonDivisor.d.ts.map +1 -1
  21. package/dist/algorithms/greatestCommonDivisor.js +7 -9
  22. package/dist/algorithms/greatestCommonDivisor.js.map +1 -1
  23. package/dist/algorithms/hypergeometricPmf.d.ts +9 -0
  24. package/dist/algorithms/hypergeometricPmf.d.ts.map +1 -1
  25. package/dist/algorithms/hypergeometricPmf.js +10 -1
  26. package/dist/algorithms/hypergeometricPmf.js.map +1 -1
  27. package/dist/algorithms/isPrime.d.ts +6 -0
  28. package/dist/algorithms/isPrime.d.ts.map +1 -1
  29. package/dist/algorithms/isPrime.js +8 -1
  30. package/dist/algorithms/isPrime.js.map +1 -1
  31. package/dist/algorithms/permutations.d.ts +7 -0
  32. package/dist/algorithms/permutations.d.ts.map +1 -1
  33. package/dist/algorithms/permutations.js +8 -1
  34. package/dist/algorithms/permutations.js.map +1 -1
  35. package/dist/algorithms/primeFactorization.d.ts +7 -1
  36. package/dist/algorithms/primeFactorization.d.ts.map +1 -1
  37. package/dist/algorithms/primeFactorization.js +10 -3
  38. package/dist/algorithms/primeFactorization.js.map +1 -1
  39. package/dist/algorithms/radiansToDegrees.d.ts +7 -0
  40. package/dist/algorithms/radiansToDegrees.d.ts.map +1 -1
  41. package/dist/algorithms/radiansToDegrees.js +7 -0
  42. package/dist/algorithms/radiansToDegrees.js.map +1 -1
  43. package/dist/algorithms/summation.d.ts +8 -0
  44. package/dist/algorithms/summation.d.ts.map +1 -1
  45. package/dist/algorithms/summation.js +9 -0
  46. package/dist/algorithms/summation.js.map +1 -1
  47. package/dist/index.d.ts +32 -33
  48. package/dist/index.d.ts.map +1 -1
  49. package/dist/index.js +27 -27
  50. package/dist/index.js.map +1 -1
  51. package/dist/linalg/DualQuaternion.d.ts +460 -83
  52. package/dist/linalg/DualQuaternion.d.ts.map +1 -1
  53. package/dist/linalg/DualQuaternion.js +514 -133
  54. package/dist/linalg/DualQuaternion.js.map +1 -1
  55. package/dist/linalg/Matrix.d.ts +80 -12
  56. package/dist/linalg/Matrix.d.ts.map +1 -1
  57. package/dist/linalg/Matrix2.d.ts +337 -52
  58. package/dist/linalg/Matrix2.d.ts.map +1 -1
  59. package/dist/linalg/Matrix2.js +345 -61
  60. package/dist/linalg/Matrix2.js.map +1 -1
  61. package/dist/linalg/Matrix3.d.ts +470 -72
  62. package/dist/linalg/Matrix3.d.ts.map +1 -1
  63. package/dist/linalg/Matrix3.js +488 -94
  64. package/dist/linalg/Matrix3.js.map +1 -1
  65. package/dist/linalg/Matrix4.d.ts +842 -137
  66. package/dist/linalg/Matrix4.d.ts.map +1 -1
  67. package/dist/linalg/Matrix4.js +896 -175
  68. package/dist/linalg/Matrix4.js.map +1 -1
  69. package/dist/linalg/Quaternion.d.ts +395 -70
  70. package/dist/linalg/Quaternion.d.ts.map +1 -1
  71. package/dist/linalg/Quaternion.js +416 -70
  72. package/dist/linalg/Quaternion.js.map +1 -1
  73. package/dist/linalg/SlowMatrix.d.ts +79 -9
  74. package/dist/linalg/SlowMatrix.d.ts.map +1 -1
  75. package/dist/linalg/SlowMatrix.js +147 -27
  76. package/dist/linalg/SlowMatrix.js.map +1 -1
  77. package/dist/linalg/SlowSquareMatrix.d.ts +58 -3
  78. package/dist/linalg/SlowSquareMatrix.d.ts.map +1 -1
  79. package/dist/linalg/SlowSquareMatrix.js +111 -21
  80. package/dist/linalg/SlowSquareMatrix.js.map +1 -1
  81. package/dist/linalg/SquareMatrix.d.ts +26 -3
  82. package/dist/linalg/SquareMatrix.d.ts.map +1 -1
  83. package/dist/linalg/Vector.d.ts +148 -24
  84. package/dist/linalg/Vector.d.ts.map +1 -1
  85. package/dist/linalg/Vector2.d.ts +484 -84
  86. package/dist/linalg/Vector2.d.ts.map +1 -1
  87. package/dist/linalg/Vector2.js +487 -71
  88. package/dist/linalg/Vector2.js.map +1 -1
  89. package/dist/linalg/Vector3.d.ts +582 -98
  90. package/dist/linalg/Vector3.d.ts.map +1 -1
  91. package/dist/linalg/Vector3.js +625 -134
  92. package/dist/linalg/Vector3.js.map +1 -1
  93. package/dist/linalg/Vector4.d.ts +450 -75
  94. package/dist/linalg/Vector4.d.ts.map +1 -1
  95. package/dist/linalg/Vector4.js +447 -62
  96. package/dist/linalg/Vector4.js.map +1 -1
  97. package/dist/types/AxisAngle.d.ts +4 -1
  98. package/dist/types/AxisAngle.d.ts.map +1 -1
  99. package/dist/types/FieldOfView.d.ts +5 -0
  100. package/dist/types/FieldOfView.d.ts.map +1 -1
  101. package/dist/utility/BigNumber.d.ts +41 -8
  102. package/dist/utility/BigNumber.d.ts.map +1 -1
  103. package/dist/utility/BigNumber.js +47 -5
  104. package/dist/utility/BigNumber.js.map +1 -1
  105. package/dist/utility/MagnitudeError.d.ts +5 -0
  106. package/dist/utility/MagnitudeError.d.ts.map +1 -1
  107. package/dist/utility/MagnitudeError.js +6 -1
  108. package/dist/utility/MagnitudeError.js.map +1 -1
  109. package/dist/utility/MatrixSizeError.d.ts +5 -0
  110. package/dist/utility/MatrixSizeError.d.ts.map +1 -1
  111. package/dist/utility/MatrixSizeError.js +5 -0
  112. package/dist/utility/MatrixSizeError.js.map +1 -1
  113. package/dist/utility/PartialMatrixError.d.ts +5 -0
  114. package/dist/utility/PartialMatrixError.d.ts.map +1 -1
  115. package/dist/utility/PartialMatrixError.js +5 -0
  116. package/dist/utility/PartialMatrixError.js.map +1 -1
  117. package/dist/utility/SingularMatrixError.d.ts +8 -0
  118. package/dist/utility/SingularMatrixError.d.ts.map +1 -1
  119. package/dist/utility/SingularMatrixError.js +8 -0
  120. package/dist/utility/SingularMatrixError.js.map +1 -1
  121. package/dist/utility/epsilon.d.ts +1 -0
  122. package/dist/utility/epsilon.d.ts.map +1 -1
  123. package/dist/utility/epsilon.js +1 -0
  124. package/dist/utility/epsilon.js.map +1 -1
  125. package/package.json +20 -51
  126. package/src/algorithms/combinations.ts +4 -4
  127. package/src/algorithms/degreesToRadians.ts +3 -3
  128. package/src/algorithms/factorial.ts +13 -8
  129. package/src/algorithms/fibonacci.ts +3 -3
  130. package/src/algorithms/greatestCommonDivisor.ts +15 -19
  131. package/src/algorithms/hypergeometricPmf.ts +5 -6
  132. package/src/algorithms/isPrime.ts +4 -4
  133. package/src/algorithms/permutations.ts +4 -4
  134. package/src/algorithms/primeFactorization.ts +9 -9
  135. package/src/algorithms/radiansToDegrees.ts +3 -3
  136. package/src/algorithms/summation.ts +5 -5
  137. package/src/index.ts +35 -33
  138. package/src/linalg/DualQuaternion.ts +558 -804
  139. package/src/linalg/Matrix.ts +35 -36
  140. package/src/linalg/Matrix2.ts +277 -413
  141. package/src/linalg/Matrix3.ts +532 -737
  142. package/src/linalg/Matrix4.ts +1078 -1604
  143. package/src/linalg/Quaternion.ts +415 -690
  144. package/src/linalg/SlowMatrix.ts +118 -69
  145. package/src/linalg/SlowSquareMatrix.ts +85 -56
  146. package/src/linalg/SquareMatrix.ts +7 -7
  147. package/src/linalg/Vector.ts +71 -70
  148. package/src/linalg/Vector2.ts +380 -576
  149. package/src/linalg/Vector3.ts +544 -840
  150. package/src/linalg/Vector4.ts +383 -549
  151. package/src/types/AxisAngle.ts +1 -1
  152. package/src/utility/BigNumber.ts +33 -42
  153. package/src/utility/MagnitudeError.ts +4 -4
  154. package/src/utility/MatrixSizeError.ts +2 -3
  155. package/src/utility/PartialMatrixError.ts +2 -3
  156. package/src/utility/SingularMatrixError.ts +2 -2
  157. package/src/utility/epsilon.ts +1 -4
  158. package/dist/types/IntegerRepresentation.d.ts +0 -2
  159. package/dist/types/IntegerRepresentation.d.ts.map +0 -1
  160. package/dist/types/IntegerRepresentation.js +0 -2
  161. package/dist/types/IntegerRepresentation.js.map +0 -1
  162. package/src/types/IntegerRepresentation.ts +0 -2
@@ -1,9 +1,21 @@
1
- import Vector4, { add, copy, dot, equals, exactEquals, fromValues, getMagnitude, getSquaredMagnitude, lerp, normalize, scale } from "#Vector4";
2
- import epsilon from "#epsilon";
3
- export function createQuaternionLike() {
1
+ import { createMatrix3Like } from "./Matrix3.js";
2
+ import { add, copy, dot, equals, exactEquals, fromValues, getMagnitude, getSquaredMagnitude, lerp, normalize, scale } from "./Vector4.js";
3
+ import epsilon from "../utility/epsilon.js";
4
+ /**
5
+ * Create a quaternion-like object.
6
+ * @returns A quaternion-like object.
7
+ */
8
+ export const createQuaternionLike = () => {
4
9
  return new Float32Array(4);
5
- }
6
- export function fromMatrix3(matrix, out) {
10
+ };
11
+ /**
12
+ * Create a quaternion from a three-by-three rotation matrix.
13
+ * @param matrix - The matrix.
14
+ * @param out - The quaternion to store the result in.
15
+ * @returns The quaternion.
16
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
17
+ */
18
+ export const fromMatrix3 = (matrix, out) => {
7
19
  const fTrace = matrix[0] + matrix[4] + matrix[8];
8
20
  if (fTrace > 0) {
9
21
  let fRoot = Math.sqrt(fTrace + 1);
@@ -18,26 +30,38 @@ export function fromMatrix3(matrix, out) {
18
30
  if (matrix[4] > matrix[0]) {
19
31
  i = 1;
20
32
  }
21
- if (matrix[8] > matrix[i * 3 + i]) {
33
+ if (matrix[8] > matrix[(i * 3 + i)]) {
22
34
  i = 2;
23
35
  }
24
- const j = (i + 1) % 3;
25
- const k = (i + 2) % 3;
26
- let fRoot = Math.sqrt(matrix[i * 3 + i] -
27
- matrix[j * 3 + j] -
28
- matrix[k * 3 + k] +
36
+ const j = ((i + 1) % 3);
37
+ const k = ((i + 2) % 3);
38
+ let fRoot = Math.sqrt(matrix[(i * 3 + i)] -
39
+ matrix[(j * 3 + j)] -
40
+ matrix[(k * 3 + k)] +
29
41
  1);
30
42
  out[i] = 0.5 * fRoot;
31
43
  fRoot = 0.5 / fRoot;
32
44
  out[3] =
33
- (matrix[j * 3 + k] - matrix[k * 3 + j]) * fRoot;
45
+ (matrix[(j * 3 + k)] - matrix[(k * 3 + j)]) *
46
+ fRoot;
34
47
  out[j] =
35
- (matrix[j * 3 + i] + matrix[i * 3 + j]) * fRoot;
48
+ (matrix[(j * 3 + i)] + matrix[(i * 3 + j)]) *
49
+ fRoot;
36
50
  out[k] =
37
- (matrix[k * 3 + i] + matrix[i * 3 + k]) * fRoot;
51
+ (matrix[(k * 3 + i)] + matrix[(i * 3 + k)]) *
52
+ fRoot;
38
53
  return out;
39
- }
40
- export function fromEuler(x, y, z, out) {
54
+ };
55
+ /**
56
+ * Create a quaternion from equivalent X-Y-Z Tait-Bryan angles
57
+ * @param x - The X angle.
58
+ * @param y - The Y angle.
59
+ * @param z - The Z angle.
60
+ * @param out - The quaternion to store the result in.
61
+ * @returns The quaternion.
62
+ * @see [Euler angles](https://en.wikipedia.org/wiki/Euler_angles)
63
+ */
64
+ export const fromEuler = (x, y, z, out) => {
41
65
  const r = (0.5 * Math.PI) / 180;
42
66
  const x2 = x * r;
43
67
  const y2 = y * r;
@@ -53,9 +77,18 @@ export function fromEuler(x, y, z, out) {
53
77
  out[2] = cx * cy * sz - sx * sy * cz;
54
78
  out[3] = cx * cy * cz + sx * sy * sz;
55
79
  return out;
56
- }
57
- const intermediary = new Float32Array(9);
58
- export function fromAxes(view, right, up, out) {
80
+ };
81
+ // Stores intermediary values for some functions.
82
+ const intermediary = createMatrix3Like();
83
+ /**
84
+ * Create a quaternion with values corresponding to the given orthonormal set of vectors.
85
+ * @param view - The vector representing the viewing direction.
86
+ * @param right - The vector representing the local "right" direction.
87
+ * @param up - The vector representing the local "up" direction.
88
+ * @param out - The quaternion to store the result in.
89
+ * @returns The quaternion.
90
+ */
91
+ export const fromAxes = (view, right, up, out) => {
59
92
  intermediary[0] = right[0];
60
93
  intermediary[3] = right[1];
61
94
  intermediary[6] = right[2];
@@ -66,15 +99,26 @@ export function fromAxes(view, right, up, out) {
66
99
  intermediary[5] = -view[1];
67
100
  intermediary[8] = -view[2];
68
101
  return normalize(fromMatrix3(intermediary, out), out);
69
- }
70
- export function identity(out) {
102
+ };
103
+ /**
104
+ * Set a quaternion to the identity.
105
+ * @param out - The quaternion to store the result in.
106
+ * @returns This quaternion.
107
+ */
108
+ export const identity = (out) => {
71
109
  out[0] = 0;
72
110
  out[1] = 0;
73
111
  out[2] = 0;
74
112
  out[3] = 1;
75
113
  return out;
76
- }
77
- export function getAxisAngle(quaternion, out) {
114
+ };
115
+ /**
116
+ * Calculate the axis and angle that represent a quaternion.
117
+ * @param quaternion - The quaternion.
118
+ * @param out - The axis and angle to store the result in.
119
+ * @returns The axis and angle.
120
+ */
121
+ export const getAxisAngle = (quaternion, out) => {
78
122
  const r = Math.acos(quaternion[3]) * 2;
79
123
  const s = Math.sin(r / 2);
80
124
  out.axis =
@@ -83,8 +127,14 @@ export function getAxisAngle(quaternion, out) {
83
127
  : [1, 0, 0];
84
128
  out.angle = r;
85
129
  return out;
86
- }
87
- export function setAxisAngle(axisAngle, out) {
130
+ };
131
+ /**
132
+ * Set the axis and angle that represent a quaternion.
133
+ * @param axisAngle - The axis and angle.
134
+ * @param out - The quaternion to store the result in.
135
+ * @returns The quaternion.
136
+ */
137
+ export const setAxisAngle = (axisAngle, out) => {
88
138
  const r = axisAngle.angle * 0.5;
89
139
  const s = Math.sin(r);
90
140
  out[0] = s * axisAngle.axis[0];
@@ -92,12 +142,25 @@ export function setAxisAngle(axisAngle, out) {
92
142
  out[2] = s * axisAngle.axis[2];
93
143
  out[3] = Math.cos(r);
94
144
  return out;
95
- }
96
- export function getAngle(a, b) {
145
+ };
146
+ /**
147
+ * Get the angular distance between two unit quaternions.
148
+ * @param a - The first unit quaternion.
149
+ * @param b - The second unit quaternion.
150
+ * @returns The angular distance in radians.
151
+ */
152
+ export const getAngle = (a, b) => {
97
153
  const dp = dot(a, b);
98
154
  return Math.acos(2 * dp * dp - 1);
99
- }
100
- export function multiply(a, b, out) {
155
+ };
156
+ /**
157
+ * Multiply two quaternions.
158
+ * @param a - The multiplier.
159
+ * @param b - The multiplicand.
160
+ * @param out - The quaternion to store the result in.
161
+ * @returns The product.
162
+ */
163
+ export const multiply = (a, b, out) => {
101
164
  const ax = a[0];
102
165
  const ay = a[1];
103
166
  const az = a[2];
@@ -111,8 +174,15 @@ export function multiply(a, b, out) {
111
174
  out[2] = az * bw + aw * bz + ax * by - ay * bx;
112
175
  out[3] = aw * bw - ax * bx - ay * by - az * bz;
113
176
  return out;
114
- }
115
- export function rotateX(quaternion, radians, out) {
177
+ };
178
+ /**
179
+ * Rotate a quaternion by the given angle around the X-axis.
180
+ * @param quaternion - The quaternion.
181
+ * @param radians - The angle in radians.
182
+ * @param out - The quaternion to store the result in.
183
+ * @returns The rotated quaternion.
184
+ */
185
+ export const rotateX = (quaternion, radians, out) => {
116
186
  const r = radians * 0.5;
117
187
  const ax = quaternion[0];
118
188
  const ay = quaternion[1];
@@ -125,8 +195,15 @@ export function rotateX(quaternion, radians, out) {
125
195
  out[2] = az * bw - ay * bx;
126
196
  out[3] = aw * bw - ax * bx;
127
197
  return out;
128
- }
129
- export function rotateY(quaternion, radians, out) {
198
+ };
199
+ /**
200
+ * Rotate a quaternion by the given angle around the Y-axis.
201
+ * @param quaternion - The quaternion.
202
+ * @param radians - The angle in radians.
203
+ * @param out - The quaternion to store the result in.
204
+ * @returns The rotated quaternion.
205
+ */
206
+ export const rotateY = (quaternion, radians, out) => {
130
207
  const r = radians * 0.5;
131
208
  const ax = quaternion[0];
132
209
  const ay = quaternion[1];
@@ -139,8 +216,15 @@ export function rotateY(quaternion, radians, out) {
139
216
  out[2] = az * bw + ax * by;
140
217
  out[3] = aw * bw - ay * by;
141
218
  return out;
142
- }
143
- export function rotateZ(quaternion, radians, out) {
219
+ };
220
+ /**
221
+ * Rotate a quaternion by the given angle around the Z-axis.
222
+ * @param quaternion - The quaternion.
223
+ * @param radians - The angle in radians.
224
+ * @param out - The quaternion to store the result in.
225
+ * @returns The rotated quaternion.
226
+ */
227
+ export const rotateZ = (quaternion, radians, out) => {
144
228
  const r = radians * 0.5;
145
229
  const ax = quaternion[0];
146
230
  const ay = quaternion[1];
@@ -153,8 +237,14 @@ export function rotateZ(quaternion, radians, out) {
153
237
  out[2] = az * bw + aw * bz;
154
238
  out[3] = aw * bw - az * bz;
155
239
  return out;
156
- }
157
- export function calculateW(quaternion, out) {
240
+ };
241
+ /**
242
+ * Calculate the fourth component of a unit quaternion from the first three, ignoring the existing fourth component.
243
+ * @param quaternion - The quaternion.
244
+ * @param out - The quaternion to store the result in.
245
+ * @returns The quaternion.
246
+ */
247
+ export const calculateW = (quaternion, out) => {
158
248
  const x = quaternion[0];
159
249
  const y = quaternion[1];
160
250
  const z = quaternion[2];
@@ -163,8 +253,14 @@ export function calculateW(quaternion, out) {
163
253
  out[2] = z;
164
254
  out[3] = Math.sqrt(Math.abs(1 - x * x - y * y - z * z));
165
255
  return out;
166
- }
167
- export function exp(quaternion, out) {
256
+ };
257
+ /**
258
+ * Calculate the exponential of a unit quaternion.
259
+ * @param quaternion - The quaternion.
260
+ * @param out - The quaternion to store the result in.
261
+ * @returns The exponential.
262
+ */
263
+ export const exp = (quaternion, out) => {
168
264
  const x = quaternion[0];
169
265
  const y = quaternion[1];
170
266
  const z = quaternion[2];
@@ -177,8 +273,14 @@ export function exp(quaternion, out) {
177
273
  out[2] = z * s;
178
274
  out[3] = et * Math.cos(r);
179
275
  return out;
180
- }
181
- export function ln(quaternion, out) {
276
+ };
277
+ /**
278
+ * Calculate the natural logarithm of a unit quaternion.
279
+ * @param quaternion - The quaternion.
280
+ * @param out - The quaternion to store the result in.
281
+ * @returns The natural logarithm.
282
+ */
283
+ export const ln = (quaternion, out) => {
182
284
  const x = quaternion[0];
183
285
  const y = quaternion[1];
184
286
  const z = quaternion[2];
@@ -190,11 +292,27 @@ export function ln(quaternion, out) {
190
292
  out[2] = z * t;
191
293
  out[3] = 0.5 * Math.log(x * x + y * y + z * z + w * w);
192
294
  return out;
193
- }
194
- export function pow(quaternion, scalar, out) {
295
+ };
296
+ /**
297
+ * Calculate a power of a unit quaternion.
298
+ * @param quaternion - The quaternion.
299
+ * @param scalar - The amount to scale the quaternion by.
300
+ * @param out - The quaternion to store the result in.
301
+ * @returns The power.
302
+ */
303
+ export const pow = (quaternion, scalar, out) => {
195
304
  return exp(scale(ln(quaternion, out), scalar, out), out);
196
- }
197
- export function slerp(a, b, t, out) {
305
+ };
306
+ /**
307
+ * Perform a spherical linear interpolation between two quaternions.
308
+ * @param a - The first quaternion.
309
+ * @param b - The second quaternion.
310
+ * @param t - The interpolation amount in `[0,1]`.
311
+ * @param out - The quaternion to store the result in.
312
+ * @returns The interpolated quaternion.
313
+ * @see [Slerp](https://en.wikipedia.org/wiki/Slerp)
314
+ */
315
+ export const slerp = (a, b, t, out) => {
198
316
  const ax = a[0];
199
317
  const ay = a[1];
200
318
  const az = a[2];
@@ -211,8 +329,10 @@ export function slerp(a, b, t, out) {
211
329
  bz = -bz;
212
330
  bw = -bw;
213
331
  }
214
- let scale0 = 0;
215
- let scale1 = 0;
332
+ // eslint-disable-next-line init-declarations
333
+ let scale0;
334
+ // eslint-disable-next-line init-declarations
335
+ let scale1;
216
336
  if (1 - cosom > epsilon) {
217
337
  const omega = Math.acos(cosom);
218
338
  const sinom = Math.sin(omega);
@@ -220,6 +340,7 @@ export function slerp(a, b, t, out) {
220
340
  scale1 = Math.sin(t * omega) / sinom;
221
341
  }
222
342
  else {
343
+ // Close enough to do a linear interpolation.
223
344
  scale0 = 1 - t;
224
345
  scale1 = t;
225
346
  }
@@ -228,8 +349,13 @@ export function slerp(a, b, t, out) {
228
349
  out[2] = scale0 * az + scale1 * bz;
229
350
  out[3] = scale0 * aw + scale1 * bw;
230
351
  return out;
231
- }
232
- export function random(out) {
352
+ };
353
+ /**
354
+ * Set a quaternion to a random unit quaternion.
355
+ * @param out - The quaternion to store the result in.
356
+ * @returns The quaternion.
357
+ */
358
+ export const random = (out) => {
233
359
  const u1 = Math.random();
234
360
  const u2 = Math.random();
235
361
  const u3 = Math.random();
@@ -240,15 +366,21 @@ export function random(out) {
240
366
  out[2] = sqrtU1 * Math.sin(2 * Math.PI * u3);
241
367
  out[3] = sqrtU1 * Math.cos(2 * Math.PI * u3);
242
368
  return out;
243
- }
244
- export function invert(quaternion, out) {
369
+ };
370
+ /**
371
+ * Calculate the inverse of a quaternion. If the quaternion is normalized, the conjugate is the same but faster to calculate.
372
+ * @param quaternion - The quaternion.
373
+ * @param out - The quaternion to store the result in.
374
+ * @returns The inverse.
375
+ */
376
+ export const invert = (quaternion, out) => {
245
377
  const a0 = quaternion[0];
246
378
  const a1 = quaternion[1];
247
379
  const a2 = quaternion[2];
248
380
  const a3 = quaternion[3];
249
- const dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3;
250
- const invDot = dot ? 1 / dot : 0;
251
- if (dot == 0) {
381
+ const aDotA = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3;
382
+ const invDot = aDotA ? 1 / aDotA : 0;
383
+ if (aDotA === 0) {
252
384
  out[0] = 0;
253
385
  out[1] = 0;
254
386
  out[2] = 0;
@@ -260,119 +392,333 @@ export function invert(quaternion, out) {
260
392
  out[2] = -a2 * invDot;
261
393
  out[3] = a3 * invDot;
262
394
  return out;
263
- }
264
- export function conjugate(quaternion, out) {
395
+ };
396
+ /**
397
+ * Calculate the conjugate of a quaternion. If the quaternion is normalized, this is the same as the inverse but faster to calculate.
398
+ * @param quaternion - The quaternion.
399
+ * @param out - The quaternion to store the result in.
400
+ * @returns The conjugate.
401
+ */
402
+ export const conjugate = (quaternion, out) => {
265
403
  out[0] = -quaternion[0];
266
404
  out[1] = -quaternion[1];
267
405
  out[2] = -quaternion[2];
268
406
  out[3] = quaternion[3];
269
407
  return out;
270
- }
271
- const controlPointOne = new Float32Array(4);
272
- const controlPointTwo = new Float32Array(4);
273
- export function sqlerp(a, b, c, d, t, out) {
408
+ };
409
+ // Used to store intermediate values for some functions.
410
+ const controlPointOne = createQuaternionLike();
411
+ const controlPointTwo = createQuaternionLike();
412
+ /**
413
+ * Perform a spherical linear interpolation with two control points between two quaternions.
414
+ * @param a - The first quaternion.
415
+ * @param b - The first control point.
416
+ * @param c - The second control point.
417
+ * @param d - The second quaternion.
418
+ * @param t - The interpolation amount in `[0,1]`.
419
+ * @param out - The quaternion to store the result in.
420
+ * @returns The interpolated value.
421
+ * @see [Slerp](https://en.wikipedia.org/wiki/Slerp)
422
+ */
423
+ export const sqlerp = (a, b, c, d, t, out) => {
274
424
  slerp(a, d, t, controlPointOne);
275
425
  slerp(b, c, t, controlPointTwo);
276
426
  return slerp(controlPointOne, controlPointTwo, 2 * t * (1 - t), out);
277
- }
427
+ };
428
+ /**
429
+ * A complex number that is commonly used to describe rotations.
430
+ * @see [Quaternion](https://en.wikipedia.org/wiki/Quaternion)
431
+ */
278
432
  export default class Quaternion extends Float32Array {
433
+ /**
434
+ * Create a quaternion from a three-by-three rotation matrix.
435
+ * @param matrix - The matrix.
436
+ * @param out - The quaternion to store the result in.
437
+ * @returns The quaternion.
438
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
439
+ */
279
440
  static fromMatrix3(matrix, out = new Quaternion()) {
280
441
  return fromMatrix3(matrix, out);
281
442
  }
443
+ /**
444
+ * Create a quaternion from equivalent x-y-z Tait-Bryan angles
445
+ * @param x - The x angle.
446
+ * @param y - The y angle.
447
+ * @param z - The z angle.
448
+ * @param out - The quaternion to store the result in.
449
+ * @returns The quaternion.
450
+ * @see [Euler angles](https://en.wikipedia.org/wiki/Euler_angles)
451
+ */
282
452
  static fromEuler(x, y, z, out = new Quaternion()) {
283
453
  return fromEuler(x, y, z, out);
284
454
  }
455
+ /**
456
+ * Create a quaternion with the given values.
457
+ * @param x - The first component.
458
+ * @param y - The second component.
459
+ * @param z - The third component.
460
+ * @param w - The fourth component.
461
+ * @param out - The quaternion to store the result in.
462
+ * @returns A new quaternion.
463
+ */
285
464
  static fromValues(x, y, z, w, out = new Quaternion()) {
286
465
  return fromValues(x, y, z, w, out);
287
466
  }
467
+ /**
468
+ * Create a quaternion with values corresponding to the given orthonormal set of vectors.
469
+ * @param view - The vector representing the viewing direction.
470
+ * @param right - The vector representing the local "right" direction.
471
+ * @param up - The vector representing the local "up" direction.
472
+ * @param out - The quaternion to store the result in.
473
+ * @returns The quaternion.
474
+ */
288
475
  static fromAxes(view, right, up, out = new Quaternion()) {
289
476
  return fromAxes(view, right, up, out);
290
477
  }
478
+ /**
479
+ * Create an identity quaternion.
480
+ * @see [Quaternion](https://en.wikipedia.org/wiki/Quaternion)
481
+ */
291
482
  constructor() {
292
483
  super(4);
293
484
  this[3] = 1;
294
485
  }
486
+ /** The first component of this quaternion. */
487
+ 0;
488
+ /** The second component of this quaternion. */
489
+ 1;
490
+ /** The third component of this quaternion. */
491
+ 2;
492
+ /** The fourth component of this quaternion. */
493
+ 3;
494
+ /**
495
+ * Set this quaternion to the identity.
496
+ * @returns This quaternion.
497
+ */
295
498
  identity() {
296
499
  return identity(this);
297
500
  }
501
+ /**
502
+ * Get the axis and angle that represent this quaternion.
503
+ * @param out - The axis and angle to store the result in.
504
+ * @returns The axis and angle.
505
+ */
298
506
  getAxisAngle(out = {}) {
299
507
  return getAxisAngle(this, out);
300
508
  }
509
+ /**
510
+ * Set the axis and angle that represent this quaternion.
511
+ * @param axisAngle - The axis and angle.
512
+ * @returns This quaternion.
513
+ */
301
514
  setAxisAngle(axisAngle) {
302
515
  return setAxisAngle(axisAngle, this);
303
516
  }
517
+ /**
518
+ * Get the angular distance between this unit quaternion and another.
519
+ * @param quaternion - The other unit quaternion.
520
+ * @returns The angular distance in radians.
521
+ */
304
522
  getAngle(quaternion) {
305
523
  return getAngle(this, quaternion);
306
524
  }
525
+ /**
526
+ * Multiply this and another quaternion.
527
+ * @param quaternion - The other quaternion.
528
+ * @param out - The quaternion to store the result in.
529
+ * @returns The product.
530
+ */
307
531
  multiply(quaternion, out = new Quaternion()) {
308
532
  return multiply(this, quaternion, out);
309
533
  }
310
- rotateX(radians, out = new Quaternion()) {
311
- return rotateX(this, radians, out);
312
- }
313
- rotateY(radians, out = new Quaternion()) {
314
- return rotateY(this, radians, out);
315
- }
316
- rotateZ(radians, out = new Quaternion()) {
317
- return rotateZ(this, radians, out);
318
- }
534
+ /**
535
+ * Rotate this quaternion by the given angle around the X-axis.
536
+ * @param r - The angle in radians.
537
+ * @param out - The quaternion to store the result in.
538
+ * @returns The rotated quaternion.
539
+ */
540
+ rotateX(r, out = new Quaternion()) {
541
+ return rotateX(this, r, out);
542
+ }
543
+ /**
544
+ * Rotate this quaternion by the given angle around the Y-axis.
545
+ * @param r - The angle in radians.
546
+ * @param out - The quaternion to store the result in.
547
+ * @returns The rotated quaternion.
548
+ */
549
+ rotateY(r, out = new Quaternion()) {
550
+ return rotateY(this, r, out);
551
+ }
552
+ /**
553
+ * Rotate this quaternion by the given angle around the Z-axis.
554
+ * @param r - The angle in radians.
555
+ * @param out - The quaternion to store the result in.
556
+ * @returns The rotated quaternion.
557
+ */
558
+ rotateZ(r, out = new Quaternion()) {
559
+ return rotateZ(this, r, out);
560
+ }
561
+ /**
562
+ * Calculate the fourth component of this unit quaternion from the first three, ignoring the existing fourth component.
563
+ * @param out - The quaternion to store the result in.
564
+ * @returns The quaternion.
565
+ */
319
566
  calculateW(out = new Quaternion()) {
320
567
  return calculateW(this, out);
321
568
  }
569
+ /**
570
+ * Calculate the exponential of this unit quaternion.
571
+ * @param out - The quaternion to store the result in.
572
+ * @returns The exponential.
573
+ */
322
574
  exp(out = new Quaternion()) {
323
575
  return exp(this, out);
324
576
  }
577
+ /**
578
+ * Calculate the natural logarithm of this unit quaternion.
579
+ * @param out - The quaternion to store the result in.
580
+ * @returns The natural logarithm.
581
+ */
325
582
  ln(out = new Quaternion()) {
326
583
  return ln(this, out);
327
584
  }
585
+ /**
586
+ * Calculate a power of this unit quaternion.
587
+ * @param scalar - The amount to scale the quaternion by.
588
+ * @param out - The quaternion to store the result in.
589
+ * @returns The power.
590
+ */
328
591
  pow(scalar, out = new Quaternion()) {
329
592
  return pow(this, scalar, out);
330
593
  }
594
+ /**
595
+ * Perform a spherical linear interpolation between this and another quaternion.
596
+ * @param quaternion - The other quaternion.
597
+ * @param t - The interpolation amount in `[0,1]`.
598
+ * @param out - The quaternion to store the result in.
599
+ * @returns The interpolated quaternion.
600
+ * @see [Slerp](https://en.wikipedia.org/wiki/Slerp)
601
+ */
331
602
  slerp(quaternion, t, out = new Quaternion()) {
332
603
  return slerp(this, quaternion, t, out);
333
604
  }
605
+ /**
606
+ * Set this to a random unit quaternion.
607
+ * @returns A random unit quaternion.
608
+ */
334
609
  random() {
335
610
  return random(this);
336
611
  }
612
+ /**
613
+ * Calculate the inverse of this quaternion. If the quaternion is normalized, the conjugate is the same but faster to calculate.
614
+ * @param out - The quaternion to store the result in.
615
+ * @returns The inverse.
616
+ */
337
617
  invert(out = new Quaternion()) {
338
618
  return invert(this, out);
339
619
  }
620
+ /**
621
+ * Calculate the conjugate of this quaternion. If the quaternion is normalized, this is the same as the inverse but faster to calculate.
622
+ * @param out - The quaternion to store the result in.
623
+ * @returns The conjugate.
624
+ */
340
625
  conjugate(out = new Quaternion()) {
341
626
  return conjugate(this, out);
342
627
  }
628
+ /**
629
+ * Copy the values from this quaternion to another one.
630
+ * @param out - The quaternion to store the result in.
631
+ * @returns The copy.
632
+ */
343
633
  clone(out = new Quaternion()) {
344
634
  return copy(this, out);
345
635
  }
636
+ /**
637
+ * Copy the values of another quaternion into this one.
638
+ * @param quaternion - The quaternion to copy.
639
+ * @returns This quaternion.
640
+ */
346
641
  copy(quaternion) {
347
642
  return copy(quaternion, this);
348
643
  }
644
+ /**
645
+ * Add two quaternions of the same size.
646
+ * @param quaternion - The other quaternion.
647
+ * @param out - The quaternion to store the result in.
648
+ * @returns The sum of the quaternions.
649
+ */
349
650
  add(quaternion, out = new Quaternion()) {
350
651
  return add(this, quaternion, out);
351
652
  }
653
+ /**
654
+ * Scale this quaternion by a scalar.
655
+ * @param scalar - The scalar.
656
+ * @param out - The quaternion to store the result in.
657
+ * @returns The scaled quaternion.
658
+ */
352
659
  scale(scalar, out = new Quaternion()) {
353
660
  return scale(this, scalar, out);
354
661
  }
662
+ /**
663
+ * Calculate the dot product of this and another quaternion.
664
+ * @param quaternion - The other quaternion.
665
+ * @returns The dot product.
666
+ */
355
667
  dot(quaternion) {
356
668
  return dot(this, quaternion);
357
669
  }
670
+ /**
671
+ * Perform a linear interpolation between this and another quaternion.
672
+ * @param quaternion - The other quaternion.
673
+ * @param t - The interpolation amount (in `[0,1]`).
674
+ * @param out - The quaternion to store the result in.
675
+ * @returns The interpolated quaternion.
676
+ */
358
677
  lerp(quaternion, t, out = new Quaternion()) {
359
678
  return lerp(this, quaternion, t, out);
360
679
  }
680
+ /** Get the magnitude (length) of this quaternion. */
361
681
  get magnitude() {
362
682
  return getMagnitude(this);
363
683
  }
684
+ /** Get the squared magnitude (length) of this quaternion. */
364
685
  get squaredMagnitude() {
365
686
  return getSquaredMagnitude(this);
366
687
  }
688
+ /**
689
+ * Normalize this quaternion.
690
+ * @param out - The quaternion to store the result in.
691
+ * @returns The normalized quaternion.
692
+ */
367
693
  normalize(out = new Quaternion()) {
368
694
  return normalize(this, out);
369
695
  }
696
+ /**
697
+ * Determine whether or not this quaternion is roughly equivalent to another.
698
+ * @param quaternion - The other quaternion.
699
+ * @returns Whether or not the quaternions are equivalent.
700
+ */
370
701
  equals(quaternion) {
371
702
  return equals(this, quaternion);
372
703
  }
704
+ /**
705
+ * Determine whether or not this quaternion is exactly equivalent to another.
706
+ * @param quaternion - The other quaternion.
707
+ * @returns Whether or not the quaternions are equivalent.
708
+ */
373
709
  exactEquals(quaternion) {
374
710
  return exactEquals(this, quaternion);
375
711
  }
712
+ /**
713
+ * Perform a spherical linear interpolation with two control points between this and another quaternion.
714
+ * @param a - The first control point.
715
+ * @param b - The second control point.
716
+ * @param quaternion - The other quaternion.
717
+ * @param t - The interpolation amount in `[0,1]`.
718
+ * @param out - The quaternion to store the result in.
719
+ * @returns The interpolated value.
720
+ * @see [Slerp](https://en.wikipedia.org/wiki/Slerp)
721
+ */
376
722
  sqlerp(a, b, quaternion, t, out = new Quaternion()) {
377
723
  return sqlerp(this, a, b, quaternion, t, out);
378
724
  }