@lakuna/umath 0.0.8 → 1.0.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 (37) hide show
  1. package/README.md +4 -0
  2. package/dist/linear/DualQuaternion.d.ts.map +1 -1
  3. package/dist/linear/DualQuaternion.js +1 -1
  4. package/dist/linear/DualQuaternion.js.map +1 -1
  5. package/dist/linear/Quaternion.d.ts +2 -2
  6. package/dist/linear/Quaternion.d.ts.map +1 -1
  7. package/dist/linear/Quaternion.js +7 -6
  8. package/dist/linear/Quaternion.js.map +1 -1
  9. package/dist/linear/Vector.d.ts +1 -1
  10. package/dist/linear/Vector.d.ts.map +1 -1
  11. package/dist/linear/Vector2.d.ts +3 -3
  12. package/dist/linear/Vector2.d.ts.map +1 -1
  13. package/dist/linear/Vector2.js +5 -4
  14. package/dist/linear/Vector2.js.map +1 -1
  15. package/dist/linear/Vector3.d.ts +3 -3
  16. package/dist/linear/Vector3.d.ts.map +1 -1
  17. package/dist/linear/Vector3.js +3 -3
  18. package/dist/linear/Vector3.js.map +1 -1
  19. package/dist/linear/Vector4.d.ts +3 -3
  20. package/dist/linear/Vector4.d.ts.map +1 -1
  21. package/dist/linear/Vector4.js +14 -14
  22. package/dist/linear/Vector4.js.map +1 -1
  23. package/dist/types/AxisAngle.d.ts.map +1 -1
  24. package/dist/utility/MagnitudeError.d.ts.map +1 -1
  25. package/dist/utility/MagnitudeError.js.map +1 -1
  26. package/dist/utility/SingularMatrixError.d.ts.map +1 -1
  27. package/dist/utility/SingularMatrixError.js.map +1 -1
  28. package/package.json +1 -1
  29. package/src/linear/DualQuaternion.ts +942 -942
  30. package/src/linear/Quaternion.ts +766 -763
  31. package/src/linear/Vector.ts +1 -1
  32. package/src/linear/Vector2.ts +13 -12
  33. package/src/linear/Vector3.ts +605 -605
  34. package/src/linear/Vector4.ts +689 -689
  35. package/src/types/AxisAngle.ts +4 -4
  36. package/src/utility/MagnitudeError.ts +7 -7
  37. package/src/utility/SingularMatrixError.ts +7 -7
@@ -14,30 +14,30 @@ export type QuaternionLike = Quaternion | [number, number, number, number];
14
14
  * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
15
15
  */
16
16
  export function fromMatrix3<T extends QuaternionLike>(matrix: Matrix3Like, out: T): T {
17
- const fTrace: number = matrix[0] + matrix[4] + matrix[8];
18
- if (fTrace > 0) {
19
- let fRoot: number = Math.sqrt(fTrace + 1);
20
- out[3] = 0.5 * fRoot;
21
- fRoot = 0.5 / fRoot;
22
- out[0] = (matrix[5] - matrix[7]) * fRoot;
23
- out[1] = (matrix[6] - matrix[2]) * fRoot;
24
- out[2] = (matrix[1] - matrix[3]) * fRoot;
25
- return out;
26
- }
27
-
28
- let i = 0;
29
- if (matrix[4] > matrix[0]) { i = 1; }
30
- if (matrix[8] > (matrix[i * 3 + i] as number)) { i = 2; }
31
- const j: number = (i + 1) % 3;
32
- const k: number = (i + 2) % 3;
33
-
34
- let fRoot: number = Math.sqrt((matrix[i * 3 + i] as number) - (matrix[j * 3 + j] as number) - (matrix[k * 3 + k] as number) + 1);
35
- out[i] = 0.5 * fRoot;
36
- fRoot = 0.5 / fRoot;
37
- out[3] = ((matrix[j * 3 + k] as number) - (matrix[k * 3 + j] as number)) * fRoot;
38
- out[j] = ((matrix[j * 3 + i] as number) + (matrix[i * 3 + j] as number)) * fRoot;
39
- out[k] = ((matrix[k * 3 + i] as number) + (matrix[i * 3 + k] as number)) * fRoot;
40
- return out;
17
+ const fTrace: number = matrix[0] + matrix[4] + matrix[8];
18
+ if (fTrace > 0) {
19
+ let fRoot: number = Math.sqrt(fTrace + 1);
20
+ out[3] = 0.5 * fRoot;
21
+ fRoot = 0.5 / fRoot;
22
+ out[0] = (matrix[5] - matrix[7]) * fRoot;
23
+ out[1] = (matrix[6] - matrix[2]) * fRoot;
24
+ out[2] = (matrix[1] - matrix[3]) * fRoot;
25
+ return out;
26
+ }
27
+
28
+ let i = 0;
29
+ if (matrix[4] > matrix[0]) { i = 1; }
30
+ if (matrix[8] > (matrix[i * 3 + i] as number)) { i = 2; }
31
+ const j: number = (i + 1) % 3;
32
+ const k: number = (i + 2) % 3;
33
+
34
+ let fRoot: number = Math.sqrt((matrix[i * 3 + i] as number) - (matrix[j * 3 + j] as number) - (matrix[k * 3 + k] as number) + 1);
35
+ out[i] = 0.5 * fRoot;
36
+ fRoot = 0.5 / fRoot;
37
+ out[3] = ((matrix[j * 3 + k] as number) - (matrix[k * 3 + j] as number)) * fRoot;
38
+ out[j] = ((matrix[j * 3 + i] as number) + (matrix[i * 3 + j] as number)) * fRoot;
39
+ out[k] = ((matrix[k * 3 + i] as number) + (matrix[i * 3 + k] as number)) * fRoot;
40
+ return out;
41
41
  }
42
42
 
43
43
  /**
@@ -50,24 +50,24 @@ export function fromMatrix3<T extends QuaternionLike>(matrix: Matrix3Like, out:
50
50
  * @see [Euler angles](https://en.wikipedia.org/wiki/Euler_angles)
51
51
  */
52
52
  export function fromEuler<T extends QuaternionLike>(x: number, y: number, z: number, out: T): T {
53
- const r: number = (0.5 * Math.PI) / 180;
54
-
55
- const x2: number = x * r;
56
- const y2: number = y * r;
57
- const z2: number = z * r;
58
-
59
- const sx: number = Math.sin(x2);
60
- const cx: number = Math.cos(x2);
61
- const sy: number = Math.sin(y2);
62
- const cy: number = Math.cos(y2);
63
- const sz: number = Math.sin(z2);
64
- const cz: number = Math.cos(z2);
65
-
66
- out[0] = sx * cy * cz - cx * sy * sz;
67
- out[1] = cx * sy * cz + sx * cy * sz;
68
- out[2] = cx * cy * sz - sx * sy * cz;
69
- out[3] = cx * cy * cz + sx * sy * sz;
70
- return out;
53
+ const r: number = (0.5 * Math.PI) / 180;
54
+
55
+ const x2: number = x * r;
56
+ const y2: number = y * r;
57
+ const z2: number = z * r;
58
+
59
+ const sx: number = Math.sin(x2);
60
+ const cx: number = Math.cos(x2);
61
+ const sy: number = Math.sin(y2);
62
+ const cy: number = Math.cos(y2);
63
+ const sz: number = Math.sin(z2);
64
+ const cz: number = Math.cos(z2);
65
+
66
+ out[0] = sx * cy * cz - cx * sy * sz;
67
+ out[1] = cx * sy * cz + sx * cy * sz;
68
+ out[2] = cx * cy * sz - sx * sy * cz;
69
+ out[3] = cx * cy * cz + sx * sy * sz;
70
+ return out;
71
71
  }
72
72
 
73
73
  /** A three-by-three matrix that is used to store intermediary values for some functions. */
@@ -82,16 +82,16 @@ const intermediary: Matrix3Like = new Float32Array(9) as Matrix3Like;
82
82
  * @returns The quaternion.
83
83
  */
84
84
  export function fromAxes<T extends QuaternionLike>(view: Vector3Like, right: Vector3Like, up: Vector3Like, out: T): T {
85
- intermediary[0] = right[0];
86
- intermediary[3] = right[1];
87
- intermediary[6] = right[2];
88
- intermediary[1] = up[0];
89
- intermediary[4] = up[1];
90
- intermediary[7] = up[2];
91
- intermediary[2] = -view[0];
92
- intermediary[5] = -view[1];
93
- intermediary[8] = -view[2];
94
- return normalize(fromMatrix3(intermediary, out) as Vector4Like, out as Vector4Like) as T;
85
+ intermediary[0] = right[0];
86
+ intermediary[3] = right[1];
87
+ intermediary[6] = right[2];
88
+ intermediary[1] = up[0];
89
+ intermediary[4] = up[1];
90
+ intermediary[7] = up[2];
91
+ intermediary[2] = -view[0];
92
+ intermediary[5] = -view[1];
93
+ intermediary[8] = -view[2];
94
+ return normalize(fromMatrix3(intermediary, out) as Vector4Like, out as Vector4Like) as T;
95
95
  }
96
96
 
97
97
  /**
@@ -100,11 +100,11 @@ export function fromAxes<T extends QuaternionLike>(view: Vector3Like, right: Vec
100
100
  * @returns This quaternion.
101
101
  */
102
102
  export function identity<T extends QuaternionLike>(out: T): T {
103
- out[0] = 0;
104
- out[1] = 0;
105
- out[2] = 0;
106
- out[3] = 1;
107
- return out;
103
+ out[0] = 0;
104
+ out[1] = 0;
105
+ out[2] = 0;
106
+ out[3] = 1;
107
+ return out;
108
108
  }
109
109
 
110
110
  /**
@@ -114,29 +114,31 @@ export function identity<T extends QuaternionLike>(out: T): T {
114
114
  * @returns The axis and angle.
115
115
  */
116
116
  export function getAxisAngle<T extends AxisAngle>(quaternion: QuaternionLike, out: T): T {
117
- const r: number = Math.acos(quaternion[3] as number) * 2;
118
- const s: number = Math.sin(r / 2);
119
-
120
- out.axis = s > epsilon
121
- ? [quaternion[0] / s, quaternion[1] / s, quaternion[2] / s]
122
- : [1, 0, 0];
123
- out.angle = r;
124
- return out;
117
+ const r: number = Math.acos(quaternion[3] as number) * 2;
118
+ const s: number = Math.sin(r / 2);
119
+
120
+ out.axis = s > epsilon
121
+ ? [quaternion[0] / s, quaternion[1] / s, quaternion[2] / s]
122
+ : [1, 0, 0];
123
+ out.angle = r;
124
+ return out;
125
125
  }
126
126
 
127
127
  /**
128
128
  * Sets the axis and angle that represent a quaternion.
129
- * @param quaternion The quaternion.
130
129
  * @param axisAngle The axis and angle.
130
+ * @param out The quaternion to store the result in.
131
+ * @returns The quaternion.
131
132
  */
132
- export function setAxisAngle(quaternion: QuaternionLike, axisAngle: AxisAngle): void {
133
- const r: number = axisAngle.angle * 0.5;
134
- const s: number = Math.sin(r);
135
-
136
- quaternion[0] = s * axisAngle.axis[0];
137
- quaternion[1] = s * axisAngle.axis[1];
138
- quaternion[2] = s * axisAngle.axis[2];
139
- quaternion[3] = Math.cos(r);
133
+ export function setAxisAngle<T extends QuaternionLike>(axisAngle: AxisAngle, out: T): T {
134
+ const r: number = axisAngle.angle * 0.5;
135
+ const s: number = Math.sin(r);
136
+
137
+ out[0] = s * axisAngle.axis[0];
138
+ out[1] = s * axisAngle.axis[1];
139
+ out[2] = s * axisAngle.axis[2];
140
+ out[3] = Math.cos(r);
141
+ return out;
140
142
  }
141
143
 
142
144
  /**
@@ -146,8 +148,8 @@ export function setAxisAngle(quaternion: QuaternionLike, axisAngle: AxisAngle):
146
148
  * @returns The angular distance in radians.
147
149
  */
148
150
  export function getAngle(a: QuaternionLike, b: QuaternionLike): number {
149
- const dp = dot(a as Vector4Like, b as Vector4Like);
150
- return Math.acos(2 * dp * dp - 1);
151
+ const dp = dot(a as Vector4Like, b as Vector4Like);
152
+ return Math.acos(2 * dp * dp - 1);
151
153
  }
152
154
 
153
155
  /**
@@ -158,21 +160,21 @@ export function getAngle(a: QuaternionLike, b: QuaternionLike): number {
158
160
  * @returns The product.
159
161
  */
160
162
  export function multiply<T extends QuaternionLike>(a: QuaternionLike, b: QuaternionLike, out: T): T {
161
- const ax: number = a[0];
162
- const ay: number = a[1];
163
- const az: number = a[2];
164
- const aw: number = a[3];
165
-
166
- const bx: number = b[0];
167
- const by: number = b[1];
168
- const bz: number = b[2];
169
- const bw: number = b[3];
170
-
171
- out[0] = ax * bw + aw * bx + ay * bz - az * by;
172
- out[1] = ay * bw + aw * by + az * bx - ax * bz;
173
- out[2] = az * bw + aw * bz + ax * by - ay * bx;
174
- out[3] = aw * bw - ax * bx - ay * by - az * bz;
175
- return out;
163
+ const ax: number = a[0];
164
+ const ay: number = a[1];
165
+ const az: number = a[2];
166
+ const aw: number = a[3];
167
+
168
+ const bx: number = b[0];
169
+ const by: number = b[1];
170
+ const bz: number = b[2];
171
+ const bw: number = b[3];
172
+
173
+ out[0] = ax * bw + aw * bx + ay * bz - az * by;
174
+ out[1] = ay * bw + aw * by + az * bx - ax * bz;
175
+ out[2] = az * bw + aw * bz + ax * by - ay * bx;
176
+ out[3] = aw * bw - ax * bx - ay * by - az * bz;
177
+ return out;
176
178
  }
177
179
 
178
180
  /**
@@ -183,21 +185,21 @@ export function multiply<T extends QuaternionLike>(a: QuaternionLike, b: Quatern
183
185
  * @returns The rotated quaternion.
184
186
  */
185
187
  export function rotateX<T extends QuaternionLike>(quaternion: QuaternionLike, radians: number, out: T): T {
186
- const r: number = radians * 0.5;
188
+ const r: number = radians * 0.5;
187
189
 
188
- const ax: number = quaternion[0];
189
- const ay: number = quaternion[1];
190
- const az: number = quaternion[2];
191
- const aw: number = quaternion[3];
190
+ const ax: number = quaternion[0];
191
+ const ay: number = quaternion[1];
192
+ const az: number = quaternion[2];
193
+ const aw: number = quaternion[3];
192
194
 
193
- const bx: number = Math.sin(r);
194
- const bw: number = Math.cos(r);
195
+ const bx: number = Math.sin(r);
196
+ const bw: number = Math.cos(r);
195
197
 
196
- out[0] = ax * bw + aw * bx;
197
- out[1] = ay * bw + az * bx;
198
- out[2] = az * bw - ay * bx;
199
- out[3] = aw * bw - ax * bx;
200
- return out;
198
+ out[0] = ax * bw + aw * bx;
199
+ out[1] = ay * bw + az * bx;
200
+ out[2] = az * bw - ay * bx;
201
+ out[3] = aw * bw - ax * bx;
202
+ return out;
201
203
  }
202
204
 
203
205
  /**
@@ -208,21 +210,21 @@ export function rotateX<T extends QuaternionLike>(quaternion: QuaternionLike, ra
208
210
  * @returns The rotated quaternion.
209
211
  */
210
212
  export function rotateY<T extends QuaternionLike>(quaternion: QuaternionLike, radians: number, out: T): T {
211
- const r: number = radians * 0.5;
213
+ const r: number = radians * 0.5;
212
214
 
213
- const ax: number = quaternion[0];
214
- const ay: number = quaternion[1];
215
- const az: number = quaternion[2];
216
- const aw: number = quaternion[3];
215
+ const ax: number = quaternion[0];
216
+ const ay: number = quaternion[1];
217
+ const az: number = quaternion[2];
218
+ const aw: number = quaternion[3];
217
219
 
218
- const by: number = Math.sin(r);
219
- const bw: number = Math.cos(r);
220
+ const by: number = Math.sin(r);
221
+ const bw: number = Math.cos(r);
220
222
 
221
- out[0] = ax * bw - az * by;
222
- out[1] = ay * bw + aw * by;
223
- out[2] = az * bw + ax * by;
224
- out[3] = aw * bw - ay * by;
225
- return out;
223
+ out[0] = ax * bw - az * by;
224
+ out[1] = ay * bw + aw * by;
225
+ out[2] = az * bw + ax * by;
226
+ out[3] = aw * bw - ay * by;
227
+ return out;
226
228
  }
227
229
 
228
230
  /**
@@ -233,21 +235,21 @@ export function rotateY<T extends QuaternionLike>(quaternion: QuaternionLike, ra
233
235
  * @returns The rotated quaternion.
234
236
  */
235
237
  export function rotateZ<T extends QuaternionLike>(quaternion: QuaternionLike, radians: number, out: T): T {
236
- const r: number = radians * 0.5;
238
+ const r: number = radians * 0.5;
237
239
 
238
- const ax: number = quaternion[0];
239
- const ay: number = quaternion[1];
240
- const az: number = quaternion[2];
241
- const aw: number = quaternion[3];
240
+ const ax: number = quaternion[0];
241
+ const ay: number = quaternion[1];
242
+ const az: number = quaternion[2];
243
+ const aw: number = quaternion[3];
242
244
 
243
- const bz: number = Math.sin(r);
244
- const bw: number = Math.cos(r);
245
+ const bz: number = Math.sin(r);
246
+ const bw: number = Math.cos(r);
245
247
 
246
- out[0] = ax * bw + ay * bz;
247
- out[1] = ay * bw - ax * bz;
248
- out[2] = az * bw + aw * bz;
249
- out[3] = aw * bw - az * bz;
250
- return out;
248
+ out[0] = ax * bw + ay * bz;
249
+ out[1] = ay * bw - ax * bz;
250
+ out[2] = az * bw + aw * bz;
251
+ out[3] = aw * bw - az * bz;
252
+ return out;
251
253
  }
252
254
 
253
255
  /**
@@ -257,15 +259,15 @@ export function rotateZ<T extends QuaternionLike>(quaternion: QuaternionLike, ra
257
259
  * @returns The quaternion.
258
260
  */
259
261
  export function calculateW<T extends QuaternionLike>(quaternion: QuaternionLike, out: T): T {
260
- const x: number = quaternion[0];
261
- const y: number = quaternion[1];
262
- const z: number = quaternion[2];
263
-
264
- out[0] = x;
265
- out[1] = y;
266
- out[2] = z;
267
- out[3] = Math.sqrt(Math.abs(1 - x * x - y * y - z * z));
268
- return out;
262
+ const x: number = quaternion[0];
263
+ const y: number = quaternion[1];
264
+ const z: number = quaternion[2];
265
+
266
+ out[0] = x;
267
+ out[1] = y;
268
+ out[2] = z;
269
+ out[3] = Math.sqrt(Math.abs(1 - x * x - y * y - z * z));
270
+ return out;
269
271
  }
270
272
 
271
273
  /**
@@ -275,20 +277,20 @@ export function calculateW<T extends QuaternionLike>(quaternion: QuaternionLike,
275
277
  * @returns The exponential.
276
278
  */
277
279
  export function exp<T extends QuaternionLike>(quaternion: QuaternionLike, out: T): T {
278
- const x: number = quaternion[0];
279
- const y: number = quaternion[1];
280
- const z: number = quaternion[2];
281
- const w: number = quaternion[3];
282
-
283
- const r: number = Math.sqrt(x * x + y * y + z * z);
284
- const et: number = Math.exp(w);
285
- const s: number = r > 0 ? (et * Math.sin(r)) / r : 0;
286
-
287
- out[0] = x * s;
288
- out[1] = y * s;
289
- out[2] = z * s;
290
- out[3] = et * Math.cos(r);
291
- return out;
280
+ const x: number = quaternion[0];
281
+ const y: number = quaternion[1];
282
+ const z: number = quaternion[2];
283
+ const w: number = quaternion[3];
284
+
285
+ const r: number = Math.sqrt(x * x + y * y + z * z);
286
+ const et: number = Math.exp(w);
287
+ const s: number = r > 0 ? (et * Math.sin(r)) / r : 0;
288
+
289
+ out[0] = x * s;
290
+ out[1] = y * s;
291
+ out[2] = z * s;
292
+ out[3] = et * Math.cos(r);
293
+ return out;
292
294
  }
293
295
 
294
296
  /**
@@ -298,19 +300,19 @@ export function exp<T extends QuaternionLike>(quaternion: QuaternionLike, out: T
298
300
  * @returns The natural logarithm.
299
301
  */
300
302
  export function ln<T extends QuaternionLike>(quaternion: QuaternionLike, out: T): T {
301
- const x: number = quaternion[0];
302
- const y: number = quaternion[1];
303
- const z: number = quaternion[2];
304
- const w: number = quaternion[3];
305
-
306
- const r: number = Math.sqrt(x * x + y * y + z * z);
307
- const t: number = r > 0 ? Math.atan2(r, w) / r : 0;
308
-
309
- out[0] = x * t;
310
- out[1] = y * t;
311
- out[2] = z * t;
312
- out[3] = 0.5 * Math.log(x * x + y * y + z * z + w * w);
313
- return out;
303
+ const x: number = quaternion[0];
304
+ const y: number = quaternion[1];
305
+ const z: number = quaternion[2];
306
+ const w: number = quaternion[3];
307
+
308
+ const r: number = Math.sqrt(x * x + y * y + z * z);
309
+ const t: number = r > 0 ? Math.atan2(r, w) / r : 0;
310
+
311
+ out[0] = x * t;
312
+ out[1] = y * t;
313
+ out[2] = z * t;
314
+ out[3] = 0.5 * Math.log(x * x + y * y + z * z + w * w);
315
+ return out;
314
316
  }
315
317
 
316
318
  /**
@@ -321,7 +323,7 @@ export function ln<T extends QuaternionLike>(quaternion: QuaternionLike, out: T)
321
323
  * @returns The power.
322
324
  */
323
325
  export function pow<T extends QuaternionLike>(quaternion: QuaternionLike, scalar: number, out: T): T {
324
- return exp(scale(ln(quaternion, out) as Vector4Like, scalar, out as Vector4Like) as QuaternionLike, out);
326
+ return exp(scale(ln(quaternion, out) as Vector4Like, scalar, out as Vector4Like) as QuaternionLike, out);
325
327
  }
326
328
 
327
329
  /**
@@ -334,43 +336,43 @@ export function pow<T extends QuaternionLike>(quaternion: QuaternionLike, scalar
334
336
  * @see [Slerp](https://en.wikipedia.org/wiki/Slerp)
335
337
  */
336
338
  export function slerp<T extends QuaternionLike>(a: QuaternionLike, b: QuaternionLike, t: number, out: T): T {
337
- const ax: number = a[0];
338
- const ay: number = a[1];
339
- const az: number = a[2];
340
- const aw: number = a[3];
341
-
342
- let bx: number = b[0];
343
- let by: number = b[1];
344
- let bz: number = b[2];
345
- let bw: number = b[3];
346
-
347
- let cosom: number = ax * bx + ay * by + az * bz + aw * bw;
348
- if (cosom < 0) {
349
- cosom = -cosom;
350
- bx = -bx;
351
- by = -by;
352
- bz = -bz;
353
- bw = -bw;
354
- }
355
-
356
- let scale0 = 0;
357
- let scale1 = 0;
358
- if (1 - cosom > epsilon) {
359
- const omega: number = Math.acos(cosom);
360
- const sinom: number = Math.sin(omega);
361
- scale0 = Math.sin((1 - t) * omega) / sinom;
362
- scale1 = Math.sin(t * omega) / sinom;
363
- } else {
364
- // Close enough to do a linear interpolation.
365
- scale0 = 1 - t;
366
- scale1 = t;
367
- }
368
-
369
- out[0] = scale0 * ax + scale1 * bx;
370
- out[1] = scale0 * ay + scale1 * by;
371
- out[2] = scale0 * az + scale1 * bz;
372
- out[3] = scale0 * aw + scale1 * bw;
373
- return out;
339
+ const ax: number = a[0];
340
+ const ay: number = a[1];
341
+ const az: number = a[2];
342
+ const aw: number = a[3];
343
+
344
+ let bx: number = b[0];
345
+ let by: number = b[1];
346
+ let bz: number = b[2];
347
+ let bw: number = b[3];
348
+
349
+ let cosom: number = ax * bx + ay * by + az * bz + aw * bw;
350
+ if (cosom < 0) {
351
+ cosom = -cosom;
352
+ bx = -bx;
353
+ by = -by;
354
+ bz = -bz;
355
+ bw = -bw;
356
+ }
357
+
358
+ let scale0 = 0;
359
+ let scale1 = 0;
360
+ if (1 - cosom > epsilon) {
361
+ const omega: number = Math.acos(cosom);
362
+ const sinom: number = Math.sin(omega);
363
+ scale0 = Math.sin((1 - t) * omega) / sinom;
364
+ scale1 = Math.sin(t * omega) / sinom;
365
+ } else {
366
+ // Close enough to do a linear interpolation.
367
+ scale0 = 1 - t;
368
+ scale1 = t;
369
+ }
370
+
371
+ out[0] = scale0 * ax + scale1 * bx;
372
+ out[1] = scale0 * ay + scale1 * by;
373
+ out[2] = scale0 * az + scale1 * bz;
374
+ out[3] = scale0 * aw + scale1 * bw;
375
+ return out;
374
376
  }
375
377
 
376
378
  /**
@@ -379,18 +381,18 @@ export function slerp<T extends QuaternionLike>(a: QuaternionLike, b: Quaternion
379
381
  * @returns The quaternion.
380
382
  */
381
383
  export function random<T extends QuaternionLike>(out: T): T {
382
- const u1: number = Math.random();
383
- const u2: number = Math.random();
384
- const u3: number = Math.random();
385
-
386
- const sqrt1MinusU1: number = Math.sqrt(1 - u1);
387
- const sqrtU1: number = Math.sqrt(u1);
388
-
389
- out[0] = sqrt1MinusU1 * Math.sin(2 * Math.PI * u2);
390
- out[1] = sqrt1MinusU1 * Math.cos(2 * Math.PI * u2);
391
- out[2] = sqrtU1 * Math.sin(2 * Math.PI * u3);
392
- out[3] = sqrtU1 * Math.cos(2 * Math.PI * u3);
393
- return out;
384
+ const u1: number = Math.random();
385
+ const u2: number = Math.random();
386
+ const u3: number = Math.random();
387
+
388
+ const sqrt1MinusU1: number = Math.sqrt(1 - u1);
389
+ const sqrtU1: number = Math.sqrt(u1);
390
+
391
+ out[0] = sqrt1MinusU1 * Math.sin(2 * Math.PI * u2);
392
+ out[1] = sqrt1MinusU1 * Math.cos(2 * Math.PI * u2);
393
+ out[2] = sqrtU1 * Math.sin(2 * Math.PI * u3);
394
+ out[3] = sqrtU1 * Math.cos(2 * Math.PI * u3);
395
+ return out;
394
396
  }
395
397
 
396
398
  /**
@@ -400,27 +402,27 @@ export function random<T extends QuaternionLike>(out: T): T {
400
402
  * @returns The inverse.
401
403
  */
402
404
  export function invert<T extends QuaternionLike>(quaternion: QuaternionLike, out: T): T {
403
- const a0: number = quaternion[0];
404
- const a1: number = quaternion[1];
405
- const a2: number = quaternion[2];
406
- const a3: number = quaternion[3];
407
-
408
- const dot: number = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3;
409
- const invDot: number = dot ? 1 / dot : 0;
410
-
411
- if (dot == 0) {
412
- out[0] = 0;
413
- out[1] = 0;
414
- out[2] = 0;
415
- out[3] = 0;
416
- return out;
417
- }
418
-
419
- out[0] = -a0 * invDot;
420
- out[1] = -a1 * invDot;
421
- out[2] = -a2 * invDot;
422
- out[3] = a3 * invDot;
423
- return out;
405
+ const a0: number = quaternion[0];
406
+ const a1: number = quaternion[1];
407
+ const a2: number = quaternion[2];
408
+ const a3: number = quaternion[3];
409
+
410
+ const dot: number = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3;
411
+ const invDot: number = dot ? 1 / dot : 0;
412
+
413
+ if (dot == 0) {
414
+ out[0] = 0;
415
+ out[1] = 0;
416
+ out[2] = 0;
417
+ out[3] = 0;
418
+ return out;
419
+ }
420
+
421
+ out[0] = -a0 * invDot;
422
+ out[1] = -a1 * invDot;
423
+ out[2] = -a2 * invDot;
424
+ out[3] = a3 * invDot;
425
+ return out;
424
426
  }
425
427
 
426
428
  /**
@@ -430,11 +432,11 @@ export function invert<T extends QuaternionLike>(quaternion: QuaternionLike, out
430
432
  * @returns The conjugate.
431
433
  */
432
434
  export function conjugate<T extends QuaternionLike>(quaternion: QuaternionLike, out: T): T {
433
- out[0] = -quaternion[0];
434
- out[1] = -quaternion[1];
435
- out[2] = -quaternion[2];
436
- out[3] = quaternion[3];
437
- return out;
435
+ out[0] = -quaternion[0];
436
+ out[1] = -quaternion[1];
437
+ out[2] = -quaternion[2];
438
+ out[3] = quaternion[3];
439
+ return out;
438
440
  }
439
441
 
440
442
  /** A quaternion that is used to store intermediary values for some functions. */
@@ -455,9 +457,9 @@ const controlPointTwo: QuaternionLike = new Float32Array(4) as QuaternionLike;
455
457
  * @see [Slerp](https://en.wikipedia.org/wiki/Slerp)
456
458
  */
457
459
  export function sqlerp<T extends QuaternionLike>(a: QuaternionLike, b: QuaternionLike, c: QuaternionLike, d: QuaternionLike, t: number, out: T): T {
458
- slerp(a, d, t, controlPointOne);
459
- slerp(b, c, t, controlPointTwo);
460
- return slerp(controlPointOne, controlPointTwo, 2 * t * (1 - t), out);
460
+ slerp(a, d, t, controlPointOne);
461
+ slerp(b, c, t, controlPointTwo);
462
+ return slerp(controlPointOne, controlPointTwo, 2 * t * (1 - t), out);
461
463
  }
462
464
 
463
465
  /**
@@ -465,525 +467,526 @@ export function sqlerp<T extends QuaternionLike>(a: QuaternionLike, b: Quaternio
465
467
  * @see [Quaternion](https://en.wikipedia.org/wiki/Quaternion)
466
468
  */
467
469
  export default class Quaternion extends Float32Array {
468
- /**
469
- * Creates a quaternion from a three-by-three rotation matrix.
470
- * @param matrix The matrix.
471
- * @returns The quaternion.
472
- * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
473
- */
474
- public static fromMatrix3(matrix: Matrix3Like): Quaternion;
475
-
476
- /**
477
- * Creates a quaternion from a three-by-three rotation matrix.
478
- * @param matrix The matrix.
479
- * @param out The quaternion to store the result in.
480
- * @returns The quaternion.
481
- * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
482
- */
483
- public static fromMatrix3<T extends QuaternionLike>(matrix: Matrix3Like, out: T): T;
484
-
485
- public static fromMatrix3<T extends QuaternionLike>(matrix: Matrix3Like, out: T = new Quaternion() as T): T {
486
- return fromMatrix3(matrix, out);
487
- }
488
-
489
- /**
490
- * Creates a quaternion from equivalent x-y-z Tait-Bryan angles
491
- * @param x The x angle.
492
- * @param y The y angle.
493
- * @param z The z angle.
494
- * @returns The quaternion.
495
- * @see [Euler angles](https://en.wikipedia.org/wiki/Euler_angles)
496
- */
497
- public static fromEuler(x: number, y: number, z: number): Quaternion;
498
-
499
- /**
500
- * Creates a quaternion from equivalent x-y-z Tait-Bryan angles
501
- * @param x The x angle.
502
- * @param y The y angle.
503
- * @param z The z angle.
504
- * @param out The quaternion to store the result in.
505
- * @returns The quaternion.
506
- * @see [Euler angles](https://en.wikipedia.org/wiki/Euler_angles)
507
- */
508
- public static fromEuler<T extends QuaternionLike>(x: number, y: number, z: number, out: T): T;
509
-
510
- public static fromEuler<T extends QuaternionLike>(x: number, y: number, z: number, out: T = new Quaternion() as T): T {
511
- return fromEuler(x, y, z, out);
512
- }
513
-
514
- /**
515
- * Creates a quaternion with the given values.
516
- * @param x The first component.
517
- * @param y The second component.
518
- * @param z The third component.
519
- * @param w The fourth component.
520
- * @returns A new quaternion.
521
- */
522
- public static fromValues(x: number, y: number, z: number, w: number): Quaternion;
523
-
524
- /**
525
- * Creates a quaternion with the given values.
526
- * @param x The first component.
527
- * @param y The second component.
528
- * @param z The third component.
529
- * @param w The fourth component.
530
- * @param out The quaternion to store the result in.
531
- * @returns A new quaternion.
532
- */
533
- public static fromValues<T extends QuaternionLike>(x: number, y: number, z: number, w: number, out: T): T;
534
-
535
- public static fromValues<T extends QuaternionLike>(x: number, y: number, z: number, w: number, out: T = new Quaternion() as T): T {
536
- return fromValues(x, y, z, w, out as Vector4Like) as T;
537
- }
538
-
539
- /**
540
- * Creates a quaternion with values corresponding to the given orthonormal set of vectors.
541
- * @param view The vector representing the viewing direction.
542
- * @param right The vector representing the local "right" direction.
543
- * @param up The vector representing the local "up" direction.
544
- * @returns The quaternion.
545
- */
546
- public static fromAxes(view: Vector3Like, right: Vector3Like, up: Vector3Like): Quaternion;
547
-
548
- /**
549
- * Creates a quaternion with values corresponding to the given orthonormal set of vectors.
550
- * @param view The vector representing the viewing direction.
551
- * @param right The vector representing the local "right" direction.
552
- * @param up The vector representing the local "up" direction.
553
- * @param out The quaternion to store the result in.
554
- * @returns The quaternion.
555
- */
556
- public static fromAxes<T extends QuaternionLike>(view: Vector3Like, right: Vector3Like, up: Vector3Like, out: T): T;
557
-
558
- public static fromAxes<T extends QuaternionLike>(view: Vector3Like, right: Vector3Like, up: Vector3Like, out: T = new Quaternion() as T): T {
559
- return fromAxes(view, right, up, out);
560
- }
561
-
562
- /**
563
- * Creates an identity quaternion.
564
- * @see [Quaternion](https://en.wikipedia.org/wiki/Quaternion)
565
- */
566
- public constructor() {
567
- super(4);
568
- this[3] = 1;
569
- }
570
-
571
- /**
572
- * Sets this quaternion to the identity.
573
- * @returns This quaternion.
574
- */
575
- public identity(): this {
576
- return identity(this);
577
- }
578
-
579
- /**
580
- * Gets the axis and angle that represent this quaternion.
581
- * @returns The axis and angle.
582
- */
583
- public getAxisAngle(): AxisAngle;
584
-
585
- /**
586
- * Gets the axis and angle that represent this quaternion.
587
- * @param out The axis and angle to store the result in.
588
- * @returns The axis and angle.
589
- */
590
- public getAxisAngle<T extends AxisAngle>(out: T): T;
591
-
592
- public getAxisAngle<T extends AxisAngle>(out: T = {} as T): T {
593
- return getAxisAngle(this, out);
594
- }
595
-
596
- /**
597
- * Sets the axis and angle that represent this quaternion.
598
- * @param axisAngle The axis and angle.
599
- */
600
- public setAxisAngle(axisAngle: AxisAngle): void {
601
- setAxisAngle(this, axisAngle);
602
- }
603
-
604
- /**
605
- * Gets the angular distance between this unit quaternion and another.
606
- * @param quaternion The other unit quaternion.
607
- * @returns The angular distance in radians.
608
- */
609
- public getAngle(quaternion: QuaternionLike): number {
610
- return getAngle(this, quaternion);
611
- }
612
-
613
- /**
614
- * Multiplies this and another quaternion.
615
- * @param quaternion The other quaternion.
616
- * @returns The product.
617
- */
618
- public multiply(quaternion: QuaternionLike): Quaternion;
619
-
620
- /**
621
- * Multiplies this and another quaternion.
622
- * @param quaternion The other quaternion.
623
- * @param out The quaternion to store the result in.
624
- * @returns The product.
625
- */
626
- public multiply<T extends QuaternionLike>(quaternion: QuaternionLike, out: T): T;
627
-
628
- public multiply<T extends QuaternionLike>(quaternion: QuaternionLike, out: T = new Quaternion() as T): T {
629
- return multiply(this, quaternion, out);
630
- }
631
-
632
- /**
633
- * Rotates this quaternion by the given angle around the X-axis.
634
- * @param radians The angle in radians.
635
- * @returns The rotated quaternion.
636
- */
637
- public rotateX(radians: number): Quaternion;
638
-
639
- /**
640
- * Rotates this quaternion by the given angle around the X-axis.
641
- * @param radians The angle in radians.
642
- * @param out The quaternion to store the result in.
643
- * @returns The rotated quaternion.
644
- */
645
- public rotateX<T extends QuaternionLike>(radians: number, out: T): T;
646
-
647
- public rotateX<T extends QuaternionLike>(radians: number, out: T = new Quaternion() as T): T {
648
- return rotateX(this, radians, out);
649
- }
650
-
651
- /**
652
- * Rotates this quaternion by the given angle around the Y-axis.
653
- * @param radians The angle in radians.
654
- * @returns The rotated quaternion.
655
- */
656
- public rotateY(radians: number): Quaternion;
657
-
658
- /**
659
- * Rotates this quaternion by the given angle around the Y-axis.
660
- * @param radians The angle in radians.
661
- * @param out The quaternion to store the result in.
662
- * @returns The rotated quaternion.
663
- */
664
- public rotateY<T extends QuaternionLike>(radians: number, out: T): T;
665
-
666
- public rotateY<T extends QuaternionLike>(radians: number, out: T = new Quaternion() as T): T {
667
- return rotateY(this, radians, out);
668
- }
669
-
670
- /**
671
- * Rotates this quaternion by the given angle around the Z-axis.
672
- * @param radians The angle in radians.
673
- * @returns The rotated quaternion.
674
- */
675
- public rotateZ(radians: number): Quaternion;
676
-
677
- /**
678
- * Rotates this quaternion by the given angle around the Z-axis.
679
- * @param radians The angle in radians.
680
- * @param out The quaternion to store the result in.
681
- * @returns The rotated quaternion.
682
- */
683
- public rotateZ<T extends QuaternionLike>(radians: number, out: T): T;
684
-
685
- public rotateZ<T extends QuaternionLike>(radians: number, out: T = new Quaternion() as T): T {
686
- return rotateZ(this, radians, out);
687
- }
688
-
689
- /**
690
- * Calculates the fourth component of this unit quaternion from the first three, ignoring the existing fourth component.
691
- * @returns The quaternion.
692
- */
693
- public calculateW(): Quaternion;
694
-
695
- /**
696
- * Calculates the fourth component of this unit quaternion from the first three, ignoring the existing fourth component.
697
- * @param out The quaternion to store the result in.
698
- * @returns The quaternion.
699
- */
700
- public calculateW<T extends QuaternionLike>(out: T): T;
701
-
702
- public calculateW<T extends QuaternionLike>(out: T = new Quaternion() as T): T {
703
- return calculateW(this, out);
704
- }
705
-
706
- /**
707
- * Calculates the exponential of this unit quaternion.
708
- * @returns The exponential.
709
- */
710
- public exp(): Quaternion;
711
-
712
- /**
713
- * Calculates the exponential of this unit quaternion.
714
- * @param out The quaternion to store the result in.
715
- * @returns The exponential.
716
- */
717
- public exp<T extends QuaternionLike>(out: T): T;
718
-
719
- public exp<T extends QuaternionLike>(out: T = new Quaternion() as T): T {
720
- return exp(this, out);
721
- }
722
-
723
- /**
724
- * Calculates the natural logarithm of this unit quaternion.
725
- * @returns The natural logarithm.
726
- */
727
- public ln(): Quaternion;
728
-
729
- /**
730
- * Calculates the natural logarithm of this unit quaternion.
731
- * @param out The quaternion to store the result in.
732
- * @returns The natural logarithm.
733
- */
734
- public ln<T extends QuaternionLike>(out: T): T;
735
-
736
- public ln<T extends QuaternionLike>(out: T = new Quaternion() as T): T {
737
- return ln(this, out);
738
- }
739
-
740
- /**
741
- * Calculates a power of this unit quaternion.
742
- * @param scalar The amount to scale the quaternion by.
743
- * @returns The power.
744
- */
745
- public pow(scalar: number): Quaternion;
746
-
747
- /**
748
- * Calculates a power of this unit quaternion.
749
- * @param scalar The amount to scale the quaternion by.
750
- * @param out The quaternion to store the result in.
751
- * @returns The power.
752
- */
753
- public pow<T extends QuaternionLike>(scalar: number, out: T): T;
754
-
755
- public pow<T extends QuaternionLike>(scalar: number, out: T = new Quaternion() as T): T {
756
- return pow(this, scalar, out);
757
- }
758
-
759
- /**
760
- * Performs a spherical linear interpolation between this and another quaternion.
761
- * @param quaternion The other quaternion.
762
- * @param t The interpolation amount in `[0,1]`.
763
- * @returns The interpolated quaternion.
764
- * @see [Slerp](https://en.wikipedia.org/wiki/Slerp)
765
- */
766
- public slerp(quaternion: QuaternionLike, t: number): Quaternion;
767
-
768
- /**
769
- * Performs a spherical linear interpolation between this and another quaternion.
770
- * @param quaternion The other quaternion.
771
- * @param t The interpolation amount in `[0,1]`.
772
- * @param out The quaternion to store the result in.
773
- * @returns The interpolated quaternion.
774
- * @see [Slerp](https://en.wikipedia.org/wiki/Slerp)
775
- */
776
- public slerp<T extends QuaternionLike>(quaternion: QuaternionLike, t: number, out: T): T;
777
-
778
- public slerp<T extends QuaternionLike>(quaternion: QuaternionLike, t: number, out: T = new Quaternion() as T): T {
779
- return slerp(this, quaternion, t, out);
780
- }
781
-
782
- /**
783
- * Sets this to a random unit quaternion.
784
- * @returns A random unit quaternion.
785
- */
786
- public random(): this {
787
- return random(this);
788
- }
789
-
790
- /**
791
- * Calculates the inverse of this quaternion. If the quaternion is normalized, the conjugate is the same but faster to calculate.
792
- * @returns The inverse.
793
- */
794
- public invert(): Quaternion;
795
-
796
- /**
797
- * Calculates the inverse of this quaternion. If the quaternion is normalized, the conjugate is the same but faster to calculate.
798
- * @param out The quaternion to store the result in.
799
- * @returns The inverse.
800
- */
801
- public invert<T extends QuaternionLike>(out: T): T;
802
-
803
- public invert<T extends QuaternionLike>(out: T = new Quaternion() as T): T {
804
- return invert(this, out);
805
- }
806
-
807
- /**
808
- * Calculates the conjugate of this quaternion. If the quaternion is normalized, this is the same as the inverse but faster to calculate.
809
- * @returns The conjugate.
810
- */
811
- public conjugate(): Quaternion;
812
-
813
- /**
814
- * Calculates the conjugate of this quaternion. If the quaternion is normalized, this is the same as the inverse but faster to calculate.
815
- * @param out The quaternion to store the result in.
816
- * @returns The conjugate.
817
- */
818
- public conjugate<T extends QuaternionLike>(out: T): T;
819
-
820
- public conjugate<T extends QuaternionLike>(out: T = new Quaternion() as T): T {
821
- return conjugate(this, out);
822
- }
823
-
824
- /**
825
- * Creates a copy of this quaternion.
826
- * @returns The copy.
827
- */
828
- public clone(): Quaternion;
829
-
830
- /**
831
- * Copies the values from this quaternion to another one.
832
- * @param out The quaternion to store the result in.
833
- * @returns The copy.
834
- */
835
- public clone<T extends QuaternionLike>(out: T): T;
836
-
837
- public clone<T extends QuaternionLike>(out: T = new Quaternion() as T): T {
838
- return copy(this as unknown as Vector4Like, out as Vector4Like) as T;
839
- }
840
-
841
- /**
842
- * Copies the values of another quaternion into this one.
843
- * @param quaternion The quaternion to copy.
844
- * @returns This quaternion.
845
- */
846
- public copy(quaternion: QuaternionLike): this {
847
- return copy(quaternion as Vector4Like, this as unknown as Vector4Like) as unknown as this;
848
- }
849
-
850
- /**
851
- * Adds two quaternions of the same size.
852
- * @param quaternion The other quaternion.
853
- * @returns The sum of the quaternions.
854
- */
855
- public add(quaternion: QuaternionLike): Quaternion;
856
-
857
- /**
858
- * Adds two quaternions of the same size.
859
- * @param quaternion The other quaternion.
860
- * @param out The quaternion to store the result in.
861
- * @returns The sum of the quaternions.
862
- */
863
- public add<T extends QuaternionLike>(quaternion: QuaternionLike, out: T): T;
864
-
865
- public add<T extends QuaternionLike>(quaternion: QuaternionLike, out: T = new Quaternion() as T): T {
866
- return add(this as unknown as Vector4Like, quaternion as Vector4Like, out as Vector4Like) as T;
867
- }
868
-
869
- /**
870
- * Scales this quaternion by a scalar.
871
- * @param scalar The scalar.
872
- * @returns The scaled quaternion.
873
- */
874
- public scale(scalar: number): Quaternion;
875
-
876
- /**
877
- * Scales this quaternion by a scalar.
878
- * @param scalar The scalar.
879
- * @param out The quaternion to store the result in.
880
- * @returns The scaled quaternion.
881
- */
882
- public scale<T extends QuaternionLike>(scalar: number, out: T): T;
883
-
884
- public scale<T extends QuaternionLike>(scalar: number, out: T = new Quaternion() as T): T {
885
- return scale(this as unknown as Vector4Like, scalar, out as Vector4Like) as T;
886
- }
887
-
888
- /**
889
- * Calculates the dot product of this and another quaternion.
890
- * @param quaternion The other quaternion.
891
- * @returns The dot product.
892
- */
893
- public dot(quaternion: QuaternionLike): number {
894
- return dot(this as unknown as Vector4Like, quaternion as Vector4Like);
895
- }
896
-
897
- /**
898
- * Performs a linear interpolation between this and another quaternion.
899
- * @param quaternion The other quaternion.
900
- * @param t The interpolation amount (in `[0,1]`).
901
- * @returns The interpolated quaternion.
902
- */
903
- public lerp(quaternion: QuaternionLike, t: number): Quaternion;
904
-
905
- /**
906
- * Performs a linear interpolation between this and another quaternion.
907
- * @param quaternion The other quaternion.
908
- * @param t The interpolation amount (in `[0,1]`).
909
- * @param out The quaternion to store the result in.
910
- * @returns The interpolated quaternion.
911
- */
912
- public lerp<T extends QuaternionLike>(quaternion: QuaternionLike, t: number, out: T): T;
913
-
914
- public lerp<T extends QuaternionLike>(quaternion: QuaternionLike, t: number, out: T = new Quaternion() as T): T {
915
- return lerp(this as unknown as Vector4Like, quaternion as Vector4Like, t, out as Vector4Like) as T;
916
- }
917
-
918
- /** The magnitude (length) of this quaternion. */
919
- public get magnitude(): number {
920
- return getMagnitude(this as unknown as Vector4Like);
921
- }
922
-
923
- /** The squared magnitude (length) of this quaternion. */
924
- public get squaredMagnitude(): number {
925
- return getSquaredMagnitude(this as unknown as Vector4Like);
926
- }
927
-
928
- /**
929
- * Normalizes this quaternion.
930
- * @returns The normalized quaternion.
931
- */
932
- public normalize(): Quaternion;
933
-
934
- /**
935
- * Normalizes this quaternion.
936
- * @param out The quaternion to store the result in.
937
- * @returns The normalized quaternion.
938
- */
939
- public normalize<T extends QuaternionLike>(out: T): T;
940
-
941
- public normalize<T extends QuaternionLike>(out: T = new Quaternion() as T): T {
942
- return normalize(this as unknown as Vector4Like, out as Vector4Like) as T;
943
- }
944
-
945
- /**
946
- * Determines whether this quaternion is roughly equivalent to another.
947
- * @param quaternion The other quaternion.
948
- * @returns Whether the quaternions are equivalent.
949
- */
950
- public equals(quaternion: QuaternionLike): boolean {
951
- return equals(this as unknown as Vector4, quaternion as Vector4Like);
952
- }
953
-
954
- /**
955
- * Determines whether this quaternion is exactly equivalent to another.
956
- * @param quaternion The other quaternion.
957
- * @returns Whether the quaternions are equivalent.
958
- */
959
- public exactEquals(quaternion: QuaternionLike): boolean {
960
- return exactEquals(this as unknown as Vector4Like, quaternion as Vector4Like);
961
- }
962
-
963
- /**
964
- * Performs a spherical linear interpolation with two control points between this and another quaternion.
965
- * @param a The first control point.
966
- * @param b The second control point.
967
- * @param quaternion The other quaternion.
968
- * @param t The interpolation amount in `[0,1]`.
969
- * @returns The interpolated value.
970
- * @see [Slerp](https://en.wikipedia.org/wiki/Slerp)
971
- */
972
- public sqlerp(a: QuaternionLike, b: QuaternionLike, quaternion: QuaternionLike, t: number): Quaternion;
973
-
974
- /**
975
- * Performs a spherical linear interpolation with two control points between this and another quaternion.
976
- * @param a The first control point.
977
- * @param b The second control point.
978
- * @param quaternion The other quaternion.
979
- * @param t The interpolation amount in `[0,1]`.
980
- * @param out The quaternion to store the result in.
981
- * @returns The interpolated value.
982
- * @see [Slerp](https://en.wikipedia.org/wiki/Slerp)
983
- */
984
- public sqlerp<T extends QuaternionLike>(a: QuaternionLike, b: QuaternionLike, quaternion: QuaternionLike, t: number, out: T): T;
985
-
986
- public sqlerp<T extends QuaternionLike>(a: QuaternionLike, b: QuaternionLike, quaternion: QuaternionLike, t: number, out: T = new Quaternion() as T): T {
987
- return sqlerp(this, a, b, quaternion, t, out);
988
- }
470
+ /**
471
+ * Creates a quaternion from a three-by-three rotation matrix.
472
+ * @param matrix The matrix.
473
+ * @returns The quaternion.
474
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
475
+ */
476
+ public static fromMatrix3(matrix: Matrix3Like): Quaternion;
477
+
478
+ /**
479
+ * Creates a quaternion from a three-by-three rotation matrix.
480
+ * @param matrix The matrix.
481
+ * @param out The quaternion to store the result in.
482
+ * @returns The quaternion.
483
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
484
+ */
485
+ public static fromMatrix3<T extends QuaternionLike>(matrix: Matrix3Like, out: T): T;
486
+
487
+ public static fromMatrix3<T extends QuaternionLike>(matrix: Matrix3Like, out: T = new Quaternion() as T): T {
488
+ return fromMatrix3(matrix, out);
489
+ }
490
+
491
+ /**
492
+ * Creates a quaternion from equivalent x-y-z Tait-Bryan angles
493
+ * @param x The x angle.
494
+ * @param y The y angle.
495
+ * @param z The z angle.
496
+ * @returns The quaternion.
497
+ * @see [Euler angles](https://en.wikipedia.org/wiki/Euler_angles)
498
+ */
499
+ public static fromEuler(x: number, y: number, z: number): Quaternion;
500
+
501
+ /**
502
+ * Creates a quaternion from equivalent x-y-z Tait-Bryan angles
503
+ * @param x The x angle.
504
+ * @param y The y angle.
505
+ * @param z The z angle.
506
+ * @param out The quaternion to store the result in.
507
+ * @returns The quaternion.
508
+ * @see [Euler angles](https://en.wikipedia.org/wiki/Euler_angles)
509
+ */
510
+ public static fromEuler<T extends QuaternionLike>(x: number, y: number, z: number, out: T): T;
511
+
512
+ public static fromEuler<T extends QuaternionLike>(x: number, y: number, z: number, out: T = new Quaternion() as T): T {
513
+ return fromEuler(x, y, z, out);
514
+ }
515
+
516
+ /**
517
+ * Creates a quaternion with the given values.
518
+ * @param x The first component.
519
+ * @param y The second component.
520
+ * @param z The third component.
521
+ * @param w The fourth component.
522
+ * @returns A new quaternion.
523
+ */
524
+ public static fromValues(x: number, y: number, z: number, w: number): Quaternion;
525
+
526
+ /**
527
+ * Creates a quaternion with the given values.
528
+ * @param x The first component.
529
+ * @param y The second component.
530
+ * @param z The third component.
531
+ * @param w The fourth component.
532
+ * @param out The quaternion to store the result in.
533
+ * @returns A new quaternion.
534
+ */
535
+ public static fromValues<T extends QuaternionLike>(x: number, y: number, z: number, w: number, out: T): T;
536
+
537
+ public static fromValues<T extends QuaternionLike>(x: number, y: number, z: number, w: number, out: T = new Quaternion() as T): T {
538
+ return fromValues(x, y, z, w, out as Vector4Like) as T;
539
+ }
540
+
541
+ /**
542
+ * Creates a quaternion with values corresponding to the given orthonormal set of vectors.
543
+ * @param view The vector representing the viewing direction.
544
+ * @param right The vector representing the local "right" direction.
545
+ * @param up The vector representing the local "up" direction.
546
+ * @returns The quaternion.
547
+ */
548
+ public static fromAxes(view: Vector3Like, right: Vector3Like, up: Vector3Like): Quaternion;
549
+
550
+ /**
551
+ * Creates a quaternion with values corresponding to the given orthonormal set of vectors.
552
+ * @param view The vector representing the viewing direction.
553
+ * @param right The vector representing the local "right" direction.
554
+ * @param up The vector representing the local "up" direction.
555
+ * @param out The quaternion to store the result in.
556
+ * @returns The quaternion.
557
+ */
558
+ public static fromAxes<T extends QuaternionLike>(view: Vector3Like, right: Vector3Like, up: Vector3Like, out: T): T;
559
+
560
+ public static fromAxes<T extends QuaternionLike>(view: Vector3Like, right: Vector3Like, up: Vector3Like, out: T = new Quaternion() as T): T {
561
+ return fromAxes(view, right, up, out);
562
+ }
563
+
564
+ /**
565
+ * Creates an identity quaternion.
566
+ * @see [Quaternion](https://en.wikipedia.org/wiki/Quaternion)
567
+ */
568
+ public constructor() {
569
+ super(4);
570
+ this[3] = 1;
571
+ }
572
+
573
+ /**
574
+ * Sets this quaternion to the identity.
575
+ * @returns This quaternion.
576
+ */
577
+ public identity(): this {
578
+ return identity(this);
579
+ }
580
+
581
+ /**
582
+ * Gets the axis and angle that represent this quaternion.
583
+ * @returns The axis and angle.
584
+ */
585
+ public getAxisAngle(): AxisAngle;
586
+
587
+ /**
588
+ * Gets the axis and angle that represent this quaternion.
589
+ * @param out The axis and angle to store the result in.
590
+ * @returns The axis and angle.
591
+ */
592
+ public getAxisAngle<T extends AxisAngle>(out: T): T;
593
+
594
+ public getAxisAngle<T extends AxisAngle>(out: T = {} as T): T {
595
+ return getAxisAngle(this, out);
596
+ }
597
+
598
+ /**
599
+ * Sets the axis and angle that represent this quaternion.
600
+ * @param axisAngle The axis and angle.
601
+ * @returns This quaternion.
602
+ */
603
+ public setAxisAngle(axisAngle: AxisAngle): this {
604
+ return setAxisAngle(axisAngle, this);
605
+ }
606
+
607
+ /**
608
+ * Gets the angular distance between this unit quaternion and another.
609
+ * @param quaternion The other unit quaternion.
610
+ * @returns The angular distance in radians.
611
+ */
612
+ public getAngle(quaternion: QuaternionLike): number {
613
+ return getAngle(this, quaternion);
614
+ }
615
+
616
+ /**
617
+ * Multiplies this and another quaternion.
618
+ * @param quaternion The other quaternion.
619
+ * @returns The product.
620
+ */
621
+ public multiply(quaternion: QuaternionLike): Quaternion;
622
+
623
+ /**
624
+ * Multiplies this and another quaternion.
625
+ * @param quaternion The other quaternion.
626
+ * @param out The quaternion to store the result in.
627
+ * @returns The product.
628
+ */
629
+ public multiply<T extends QuaternionLike>(quaternion: QuaternionLike, out: T): T;
630
+
631
+ public multiply<T extends QuaternionLike>(quaternion: QuaternionLike, out: T = new Quaternion() as T): T {
632
+ return multiply(this, quaternion, out);
633
+ }
634
+
635
+ /**
636
+ * Rotates this quaternion by the given angle around the X-axis.
637
+ * @param radians The angle in radians.
638
+ * @returns The rotated quaternion.
639
+ */
640
+ public rotateX(radians: number): Quaternion;
641
+
642
+ /**
643
+ * Rotates this quaternion by the given angle around the X-axis.
644
+ * @param radians The angle in radians.
645
+ * @param out The quaternion to store the result in.
646
+ * @returns The rotated quaternion.
647
+ */
648
+ public rotateX<T extends QuaternionLike>(radians: number, out: T): T;
649
+
650
+ public rotateX<T extends QuaternionLike>(radians: number, out: T = new Quaternion() as T): T {
651
+ return rotateX(this, radians, out);
652
+ }
653
+
654
+ /**
655
+ * Rotates this quaternion by the given angle around the Y-axis.
656
+ * @param radians The angle in radians.
657
+ * @returns The rotated quaternion.
658
+ */
659
+ public rotateY(radians: number): Quaternion;
660
+
661
+ /**
662
+ * Rotates this quaternion by the given angle around the Y-axis.
663
+ * @param radians The angle in radians.
664
+ * @param out The quaternion to store the result in.
665
+ * @returns The rotated quaternion.
666
+ */
667
+ public rotateY<T extends QuaternionLike>(radians: number, out: T): T;
668
+
669
+ public rotateY<T extends QuaternionLike>(radians: number, out: T = new Quaternion() as T): T {
670
+ return rotateY(this, radians, out);
671
+ }
672
+
673
+ /**
674
+ * Rotates this quaternion by the given angle around the Z-axis.
675
+ * @param radians The angle in radians.
676
+ * @returns The rotated quaternion.
677
+ */
678
+ public rotateZ(radians: number): Quaternion;
679
+
680
+ /**
681
+ * Rotates this quaternion by the given angle around the Z-axis.
682
+ * @param radians The angle in radians.
683
+ * @param out The quaternion to store the result in.
684
+ * @returns The rotated quaternion.
685
+ */
686
+ public rotateZ<T extends QuaternionLike>(radians: number, out: T): T;
687
+
688
+ public rotateZ<T extends QuaternionLike>(radians: number, out: T = new Quaternion() as T): T {
689
+ return rotateZ(this, radians, out);
690
+ }
691
+
692
+ /**
693
+ * Calculates the fourth component of this unit quaternion from the first three, ignoring the existing fourth component.
694
+ * @returns The quaternion.
695
+ */
696
+ public calculateW(): Quaternion;
697
+
698
+ /**
699
+ * Calculates the fourth component of this unit quaternion from the first three, ignoring the existing fourth component.
700
+ * @param out The quaternion to store the result in.
701
+ * @returns The quaternion.
702
+ */
703
+ public calculateW<T extends QuaternionLike>(out: T): T;
704
+
705
+ public calculateW<T extends QuaternionLike>(out: T = new Quaternion() as T): T {
706
+ return calculateW(this, out);
707
+ }
708
+
709
+ /**
710
+ * Calculates the exponential of this unit quaternion.
711
+ * @returns The exponential.
712
+ */
713
+ public exp(): Quaternion;
714
+
715
+ /**
716
+ * Calculates the exponential of this unit quaternion.
717
+ * @param out The quaternion to store the result in.
718
+ * @returns The exponential.
719
+ */
720
+ public exp<T extends QuaternionLike>(out: T): T;
721
+
722
+ public exp<T extends QuaternionLike>(out: T = new Quaternion() as T): T {
723
+ return exp(this, out);
724
+ }
725
+
726
+ /**
727
+ * Calculates the natural logarithm of this unit quaternion.
728
+ * @returns The natural logarithm.
729
+ */
730
+ public ln(): Quaternion;
731
+
732
+ /**
733
+ * Calculates the natural logarithm of this unit quaternion.
734
+ * @param out The quaternion to store the result in.
735
+ * @returns The natural logarithm.
736
+ */
737
+ public ln<T extends QuaternionLike>(out: T): T;
738
+
739
+ public ln<T extends QuaternionLike>(out: T = new Quaternion() as T): T {
740
+ return ln(this, out);
741
+ }
742
+
743
+ /**
744
+ * Calculates a power of this unit quaternion.
745
+ * @param scalar The amount to scale the quaternion by.
746
+ * @returns The power.
747
+ */
748
+ public pow(scalar: number): Quaternion;
749
+
750
+ /**
751
+ * Calculates a power of this unit quaternion.
752
+ * @param scalar The amount to scale the quaternion by.
753
+ * @param out The quaternion to store the result in.
754
+ * @returns The power.
755
+ */
756
+ public pow<T extends QuaternionLike>(scalar: number, out: T): T;
757
+
758
+ public pow<T extends QuaternionLike>(scalar: number, out: T = new Quaternion() as T): T {
759
+ return pow(this, scalar, out);
760
+ }
761
+
762
+ /**
763
+ * Performs a spherical linear interpolation between this and another quaternion.
764
+ * @param quaternion The other quaternion.
765
+ * @param t The interpolation amount in `[0,1]`.
766
+ * @returns The interpolated quaternion.
767
+ * @see [Slerp](https://en.wikipedia.org/wiki/Slerp)
768
+ */
769
+ public slerp(quaternion: QuaternionLike, t: number): Quaternion;
770
+
771
+ /**
772
+ * Performs a spherical linear interpolation between this and another quaternion.
773
+ * @param quaternion The other quaternion.
774
+ * @param t The interpolation amount in `[0,1]`.
775
+ * @param out The quaternion to store the result in.
776
+ * @returns The interpolated quaternion.
777
+ * @see [Slerp](https://en.wikipedia.org/wiki/Slerp)
778
+ */
779
+ public slerp<T extends QuaternionLike>(quaternion: QuaternionLike, t: number, out: T): T;
780
+
781
+ public slerp<T extends QuaternionLike>(quaternion: QuaternionLike, t: number, out: T = new Quaternion() as T): T {
782
+ return slerp(this, quaternion, t, out);
783
+ }
784
+
785
+ /**
786
+ * Sets this to a random unit quaternion.
787
+ * @returns A random unit quaternion.
788
+ */
789
+ public random(): this {
790
+ return random(this);
791
+ }
792
+
793
+ /**
794
+ * Calculates the inverse of this quaternion. If the quaternion is normalized, the conjugate is the same but faster to calculate.
795
+ * @returns The inverse.
796
+ */
797
+ public invert(): Quaternion;
798
+
799
+ /**
800
+ * Calculates the inverse of this quaternion. If the quaternion is normalized, the conjugate is the same but faster to calculate.
801
+ * @param out The quaternion to store the result in.
802
+ * @returns The inverse.
803
+ */
804
+ public invert<T extends QuaternionLike>(out: T): T;
805
+
806
+ public invert<T extends QuaternionLike>(out: T = new Quaternion() as T): T {
807
+ return invert(this, out);
808
+ }
809
+
810
+ /**
811
+ * Calculates the conjugate of this quaternion. If the quaternion is normalized, this is the same as the inverse but faster to calculate.
812
+ * @returns The conjugate.
813
+ */
814
+ public conjugate(): Quaternion;
815
+
816
+ /**
817
+ * Calculates the conjugate of this quaternion. If the quaternion is normalized, this is the same as the inverse but faster to calculate.
818
+ * @param out The quaternion to store the result in.
819
+ * @returns The conjugate.
820
+ */
821
+ public conjugate<T extends QuaternionLike>(out: T): T;
822
+
823
+ public conjugate<T extends QuaternionLike>(out: T = new Quaternion() as T): T {
824
+ return conjugate(this, out);
825
+ }
826
+
827
+ /**
828
+ * Creates a copy of this quaternion.
829
+ * @returns The copy.
830
+ */
831
+ public clone(): Quaternion;
832
+
833
+ /**
834
+ * Copies the values from this quaternion to another one.
835
+ * @param out The quaternion to store the result in.
836
+ * @returns The copy.
837
+ */
838
+ public clone<T extends QuaternionLike>(out: T): T;
839
+
840
+ public clone<T extends QuaternionLike>(out: T = new Quaternion() as T): T {
841
+ return copy(this as unknown as Vector4Like, out as Vector4Like) as T;
842
+ }
843
+
844
+ /**
845
+ * Copies the values of another quaternion into this one.
846
+ * @param quaternion The quaternion to copy.
847
+ * @returns This quaternion.
848
+ */
849
+ public copy(quaternion: QuaternionLike): this {
850
+ return copy(quaternion as Vector4Like, this as unknown as Vector4Like) as unknown as this;
851
+ }
852
+
853
+ /**
854
+ * Adds two quaternions of the same size.
855
+ * @param quaternion The other quaternion.
856
+ * @returns The sum of the quaternions.
857
+ */
858
+ public add(quaternion: QuaternionLike): Quaternion;
859
+
860
+ /**
861
+ * Adds two quaternions of the same size.
862
+ * @param quaternion The other quaternion.
863
+ * @param out The quaternion to store the result in.
864
+ * @returns The sum of the quaternions.
865
+ */
866
+ public add<T extends QuaternionLike>(quaternion: QuaternionLike, out: T): T;
867
+
868
+ public add<T extends QuaternionLike>(quaternion: QuaternionLike, out: T = new Quaternion() as T): T {
869
+ return add(this as unknown as Vector4Like, quaternion as Vector4Like, out as Vector4Like) as T;
870
+ }
871
+
872
+ /**
873
+ * Scales this quaternion by a scalar.
874
+ * @param scalar The scalar.
875
+ * @returns The scaled quaternion.
876
+ */
877
+ public scale(scalar: number): Quaternion;
878
+
879
+ /**
880
+ * Scales this quaternion by a scalar.
881
+ * @param scalar The scalar.
882
+ * @param out The quaternion to store the result in.
883
+ * @returns The scaled quaternion.
884
+ */
885
+ public scale<T extends QuaternionLike>(scalar: number, out: T): T;
886
+
887
+ public scale<T extends QuaternionLike>(scalar: number, out: T = new Quaternion() as T): T {
888
+ return scale(this as unknown as Vector4Like, scalar, out as Vector4Like) as T;
889
+ }
890
+
891
+ /**
892
+ * Calculates the dot product of this and another quaternion.
893
+ * @param quaternion The other quaternion.
894
+ * @returns The dot product.
895
+ */
896
+ public dot(quaternion: QuaternionLike): number {
897
+ return dot(this as unknown as Vector4Like, quaternion as Vector4Like);
898
+ }
899
+
900
+ /**
901
+ * Performs a linear interpolation between this and another quaternion.
902
+ * @param quaternion The other quaternion.
903
+ * @param t The interpolation amount (in `[0,1]`).
904
+ * @returns The interpolated quaternion.
905
+ */
906
+ public lerp(quaternion: QuaternionLike, t: number): Quaternion;
907
+
908
+ /**
909
+ * Performs a linear interpolation between this and another quaternion.
910
+ * @param quaternion The other quaternion.
911
+ * @param t The interpolation amount (in `[0,1]`).
912
+ * @param out The quaternion to store the result in.
913
+ * @returns The interpolated quaternion.
914
+ */
915
+ public lerp<T extends QuaternionLike>(quaternion: QuaternionLike, t: number, out: T): T;
916
+
917
+ public lerp<T extends QuaternionLike>(quaternion: QuaternionLike, t: number, out: T = new Quaternion() as T): T {
918
+ return lerp(this as unknown as Vector4Like, quaternion as Vector4Like, t, out as Vector4Like) as T;
919
+ }
920
+
921
+ /** The magnitude (length) of this quaternion. */
922
+ public get magnitude(): number {
923
+ return getMagnitude(this as unknown as Vector4Like);
924
+ }
925
+
926
+ /** The squared magnitude (length) of this quaternion. */
927
+ public get squaredMagnitude(): number {
928
+ return getSquaredMagnitude(this as unknown as Vector4Like);
929
+ }
930
+
931
+ /**
932
+ * Normalizes this quaternion.
933
+ * @returns The normalized quaternion.
934
+ */
935
+ public normalize(): Quaternion;
936
+
937
+ /**
938
+ * Normalizes this quaternion.
939
+ * @param out The quaternion to store the result in.
940
+ * @returns The normalized quaternion.
941
+ */
942
+ public normalize<T extends QuaternionLike>(out: T): T;
943
+
944
+ public normalize<T extends QuaternionLike>(out: T = new Quaternion() as T): T {
945
+ return normalize(this as unknown as Vector4Like, out as Vector4Like) as T;
946
+ }
947
+
948
+ /**
949
+ * Determines whether this quaternion is roughly equivalent to another.
950
+ * @param quaternion The other quaternion.
951
+ * @returns Whether the quaternions are equivalent.
952
+ */
953
+ public equals(quaternion: QuaternionLike): boolean {
954
+ return equals(this as unknown as Vector4, quaternion as Vector4Like);
955
+ }
956
+
957
+ /**
958
+ * Determines whether this quaternion is exactly equivalent to another.
959
+ * @param quaternion The other quaternion.
960
+ * @returns Whether the quaternions are equivalent.
961
+ */
962
+ public exactEquals(quaternion: QuaternionLike): boolean {
963
+ return exactEquals(this as unknown as Vector4Like, quaternion as Vector4Like);
964
+ }
965
+
966
+ /**
967
+ * Performs a spherical linear interpolation with two control points between this and another quaternion.
968
+ * @param a The first control point.
969
+ * @param b The second control point.
970
+ * @param quaternion The other quaternion.
971
+ * @param t The interpolation amount in `[0,1]`.
972
+ * @returns The interpolated value.
973
+ * @see [Slerp](https://en.wikipedia.org/wiki/Slerp)
974
+ */
975
+ public sqlerp(a: QuaternionLike, b: QuaternionLike, quaternion: QuaternionLike, t: number): Quaternion;
976
+
977
+ /**
978
+ * Performs a spherical linear interpolation with two control points between this and another quaternion.
979
+ * @param a The first control point.
980
+ * @param b The second control point.
981
+ * @param quaternion The other quaternion.
982
+ * @param t The interpolation amount in `[0,1]`.
983
+ * @param out The quaternion to store the result in.
984
+ * @returns The interpolated value.
985
+ * @see [Slerp](https://en.wikipedia.org/wiki/Slerp)
986
+ */
987
+ public sqlerp<T extends QuaternionLike>(a: QuaternionLike, b: QuaternionLike, quaternion: QuaternionLike, t: number, out: T): T;
988
+
989
+ public sqlerp<T extends QuaternionLike>(a: QuaternionLike, b: QuaternionLike, quaternion: QuaternionLike, t: number, out: T = new Quaternion() as T): T {
990
+ return sqlerp(this, a, b, quaternion, t, out);
991
+ }
989
992
  }