@lakuna/umath 0.0.2 → 0.0.4
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.
- package/README.md +41 -1
- package/dist/algorithms/combinations.d.ts +2 -0
- package/dist/algorithms/combinations.d.ts.map +1 -0
- package/dist/algorithms/combinations.js +5 -0
- package/dist/algorithms/combinations.js.map +1 -0
- package/dist/algorithms/degreesToRadians.d.ts +2 -0
- package/dist/algorithms/degreesToRadians.d.ts.map +1 -0
- package/dist/algorithms/degreesToRadians.js +4 -0
- package/dist/algorithms/degreesToRadians.js.map +1 -0
- package/dist/algorithms/factorial.d.ts +2 -0
- package/dist/algorithms/factorial.d.ts.map +1 -0
- package/dist/algorithms/factorial.js +4 -0
- package/dist/algorithms/factorial.js.map +1 -0
- package/dist/algorithms/fibonacci.d.ts +2 -0
- package/dist/algorithms/fibonacci.d.ts.map +1 -0
- package/dist/algorithms/fibonacci.js +9 -0
- package/dist/algorithms/fibonacci.js.map +1 -0
- package/dist/algorithms/greatestCommonDivisor.d.ts +3 -0
- package/dist/algorithms/greatestCommonDivisor.d.ts.map +1 -0
- package/dist/algorithms/greatestCommonDivisor.js +13 -0
- package/dist/algorithms/greatestCommonDivisor.js.map +1 -0
- package/dist/algorithms/isPrime.d.ts +2 -0
- package/dist/algorithms/isPrime.d.ts.map +1 -0
- package/dist/algorithms/isPrime.js +12 -0
- package/dist/algorithms/isPrime.js.map +1 -0
- package/dist/algorithms/permutations.d.ts +2 -0
- package/dist/algorithms/permutations.d.ts.map +1 -0
- package/dist/algorithms/permutations.js +5 -0
- package/dist/algorithms/permutations.js.map +1 -0
- package/dist/algorithms/primeFactorization.d.ts +2 -0
- package/dist/algorithms/primeFactorization.d.ts.map +1 -0
- package/dist/algorithms/primeFactorization.js +15 -0
- package/dist/algorithms/primeFactorization.js.map +1 -0
- package/dist/algorithms/radiansToDegrees.d.ts +2 -0
- package/dist/algorithms/radiansToDegrees.d.ts.map +1 -0
- package/dist/algorithms/radiansToDegrees.js +4 -0
- package/dist/algorithms/radiansToDegrees.js.map +1 -0
- package/dist/algorithms/summation.d.ts +2 -0
- package/dist/algorithms/summation.d.ts.map +1 -0
- package/dist/algorithms/summation.js +7 -0
- package/dist/algorithms/summation.js.map +1 -0
- package/dist/index.d.ts +28 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +22 -1
- package/dist/index.js.map +1 -1
- package/dist/linear/DualQuaternion.d.ts +89 -0
- package/dist/linear/DualQuaternion.d.ts.map +1 -0
- package/dist/linear/DualQuaternion.js +530 -0
- package/dist/linear/DualQuaternion.js.map +1 -0
- package/dist/linear/Matrix.d.ts +18 -0
- package/dist/linear/Matrix.d.ts.map +1 -0
- package/dist/linear/Matrix.js +2 -0
- package/dist/linear/Matrix.js.map +1 -0
- package/dist/linear/Matrix2.d.ts +66 -0
- package/dist/linear/Matrix2.d.ts.map +1 -0
- package/dist/linear/Matrix2.js +240 -0
- package/dist/linear/Matrix2.js.map +1 -0
- package/dist/linear/Matrix3.d.ts +91 -0
- package/dist/linear/Matrix3.d.ts.map +1 -0
- package/dist/linear/Matrix3.js +533 -0
- package/dist/linear/Matrix3.js.map +1 -0
- package/dist/linear/Matrix4.d.ts +140 -0
- package/dist/linear/Matrix4.d.ts.map +1 -0
- package/dist/linear/Matrix4.js +1363 -0
- package/dist/linear/Matrix4.js.map +1 -0
- package/dist/linear/Quaternion.d.ts +80 -0
- package/dist/linear/Quaternion.d.ts.map +1 -0
- package/dist/linear/Quaternion.js +371 -0
- package/dist/linear/Quaternion.js.map +1 -0
- package/dist/linear/SquareMatrix.d.ts +7 -0
- package/dist/linear/SquareMatrix.d.ts.map +1 -0
- package/dist/linear/SquareMatrix.js +2 -0
- package/dist/linear/SquareMatrix.js.map +1 -0
- package/dist/linear/Vector.d.ts +30 -0
- package/dist/linear/Vector.d.ts.map +1 -0
- package/dist/linear/Vector.js +2 -0
- package/dist/linear/Vector.js.map +1 -0
- package/dist/linear/Vector2.d.ts +96 -0
- package/dist/linear/Vector2.d.ts.map +1 -0
- package/dist/linear/Vector2.js +293 -0
- package/dist/linear/Vector2.js.map +1 -0
- package/dist/linear/Vector3.d.ts +111 -0
- package/dist/linear/Vector3.d.ts.map +1 -0
- package/dist/linear/Vector3.js +466 -0
- package/dist/linear/Vector3.js.map +1 -0
- package/dist/linear/Vector4.d.ts +86 -0
- package/dist/linear/Vector4.d.ts.map +1 -0
- package/dist/linear/Vector4.js +348 -0
- package/dist/linear/Vector4.js.map +1 -0
- package/dist/types/AxisAngle.d.ts +6 -0
- package/dist/types/AxisAngle.d.ts.map +1 -0
- package/dist/types/AxisAngle.js +2 -0
- package/dist/types/AxisAngle.js.map +1 -0
- package/dist/types/FieldOfView.d.ts +7 -0
- package/dist/types/FieldOfView.d.ts.map +1 -0
- package/dist/types/FieldOfView.js +2 -0
- package/dist/types/FieldOfView.js.map +1 -0
- package/dist/types/IntegerRepresentation.d.ts +2 -0
- package/dist/types/IntegerRepresentation.d.ts.map +1 -0
- package/dist/types/IntegerRepresentation.js +2 -0
- package/dist/types/IntegerRepresentation.js.map +1 -0
- package/dist/utility/BigNumber.d.ts +15 -0
- package/dist/utility/BigNumber.d.ts.map +1 -0
- package/dist/utility/BigNumber.js +75 -0
- package/dist/utility/BigNumber.js.map +1 -0
- package/dist/utility/MagnitudeError.d.ts +4 -0
- package/dist/utility/MagnitudeError.d.ts.map +1 -0
- package/dist/utility/MagnitudeError.js +6 -0
- package/dist/utility/MagnitudeError.js.map +1 -0
- package/dist/utility/SingularMatrixError.d.ts +4 -0
- package/dist/utility/SingularMatrixError.d.ts.map +1 -0
- package/dist/utility/SingularMatrixError.js +6 -0
- package/dist/utility/SingularMatrixError.js.map +1 -0
- package/dist/utility/epsilon.d.ts +3 -0
- package/dist/utility/epsilon.d.ts.map +1 -0
- package/dist/utility/epsilon.js +2 -0
- package/dist/utility/epsilon.js.map +1 -0
- package/package.json +17 -9
- package/src/algorithms/combinations.ts +12 -0
- package/src/algorithms/degreesToRadians.ts +10 -0
- package/src/algorithms/factorial.ts +9 -0
- package/src/algorithms/fibonacci.ts +14 -0
- package/src/algorithms/greatestCommonDivisor.ts +30 -0
- package/src/algorithms/isPrime.ts +20 -0
- package/src/algorithms/permutations.ts +12 -0
- package/src/algorithms/primeFactorization.ts +21 -0
- package/src/algorithms/radiansToDegrees.ts +10 -0
- package/src/algorithms/summation.ts +23 -0
- package/src/index.ts +28 -1
- package/src/linear/DualQuaternion.ts +1213 -0
- package/src/linear/Matrix.ts +106 -0
- package/src/linear/Matrix2.ts +709 -0
- package/src/linear/Matrix3.ts +1194 -0
- package/src/linear/Matrix4.ts +2569 -0
- package/src/linear/Quaternion.ts +967 -0
- package/src/linear/SquareMatrix.ts +27 -0
- package/src/linear/Vector.ts +178 -0
- package/src/linear/Vector2.ts +979 -0
- package/src/linear/Vector3.ts +1320 -0
- package/src/linear/Vector4.ts +975 -0
- package/src/types/AxisAngle.ts +10 -0
- package/src/types/FieldOfView.ts +14 -0
- package/src/types/IntegerRepresentation.ts +2 -0
- package/src/utility/BigNumber.ts +130 -0
- package/src/utility/MagnitudeError.ts +10 -0
- package/src/utility/SingularMatrixError.ts +13 -0
- package/src/utility/epsilon.ts +2 -0
|
@@ -0,0 +1,967 @@
|
|
|
1
|
+
import { epsilon, type AxisAngle } from "@lakuna/umath";
|
|
2
|
+
import { type Matrix3Like } from "@lakuna/umath/Matrix3";
|
|
3
|
+
import type { Vector3Like } from "@lakuna/umath/Vector3";
|
|
4
|
+
import Vector4, { fromValues, copy, add, scale, dot, lerp, getMagnitude, getSquaredMagnitude, normalize, equals, exactEquals, type Vector4Like } from "./Vector4.js";
|
|
5
|
+
|
|
6
|
+
/** A complex number that is commonly used to describe rotations. */
|
|
7
|
+
export type QuaternionLike = Quaternion | [number, number, number, number];
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Creates a quaternion from a three-by-three rotation matrix.
|
|
11
|
+
* @param matrix The matrix.
|
|
12
|
+
* @param out The quaternion to store the result in.
|
|
13
|
+
* @returns The quaternion.
|
|
14
|
+
* @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
|
|
15
|
+
*/
|
|
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;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Creates a quaternion from equivalent x-y-z Tait-Bryan angles
|
|
45
|
+
* @param x The x angle.
|
|
46
|
+
* @param y The y angle.
|
|
47
|
+
* @param z The z angle.
|
|
48
|
+
* @param out The quaternion to store the result in.
|
|
49
|
+
* @returns The quaternion.
|
|
50
|
+
* @see [Euler angles](https://en.wikipedia.org/wiki/Euler_angles)
|
|
51
|
+
*/
|
|
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;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** A three-by-three matrix that is used to store intermediary values for some functions. */
|
|
74
|
+
const intermediary: Matrix3Like = new Float32Array(9) as Matrix3Like;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Creates a quaternion with values corresponding to the given orthonormal set of vectors.
|
|
78
|
+
* @param view The vector representing the viewing direction.
|
|
79
|
+
* @param right The vector representing the local "right" direction.
|
|
80
|
+
* @param up The vector representing the local "up" direction.
|
|
81
|
+
* @param out The quaternion to store the result in.
|
|
82
|
+
* @returns The quaternion.
|
|
83
|
+
*/
|
|
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;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Sets a quaternion to the identity.
|
|
99
|
+
* @param out The quaternion to store the result in.
|
|
100
|
+
* @returns This quaternion.
|
|
101
|
+
*/
|
|
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;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Calculates the axis and angle that represent a quaternion.
|
|
112
|
+
* @param quaternion The quaternion.
|
|
113
|
+
* @returns The axis and angle.
|
|
114
|
+
*/
|
|
115
|
+
export function getAxisAngle(quaternion: QuaternionLike): AxisAngle {
|
|
116
|
+
const r: number = Math.acos(quaternion[3] as number) * 2;
|
|
117
|
+
const s: number = Math.sin(r / 2);
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
axis: s > epsilon
|
|
121
|
+
? [quaternion[0] / s, quaternion[1] / s, quaternion[2] / s]
|
|
122
|
+
: [1, 0, 0],
|
|
123
|
+
angle: r
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Sets the axis and angle that represent a quaternion.
|
|
129
|
+
* @param quaternion The quaternion.
|
|
130
|
+
* @param axisAngle The axis and angle.
|
|
131
|
+
*/
|
|
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);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Gets the angular distance between two unit quaternions.
|
|
144
|
+
* @param a The first unit quaternion.
|
|
145
|
+
* @param b The second unit quaternion.
|
|
146
|
+
* @returns The angular distance in radians.
|
|
147
|
+
*/
|
|
148
|
+
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
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Multiplies two quaternions.
|
|
155
|
+
* @param a The multiplier.
|
|
156
|
+
* @param b The multiplicand.
|
|
157
|
+
* @param out The quaternion to store the result in.
|
|
158
|
+
* @returns The product.
|
|
159
|
+
*/
|
|
160
|
+
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;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Rotates 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 function rotateX<T extends QuaternionLike>(quaternion: QuaternionLike, radians: number, out: T): T {
|
|
186
|
+
const r: number = radians * 0.5;
|
|
187
|
+
|
|
188
|
+
const ax: number = quaternion[0];
|
|
189
|
+
const ay: number = quaternion[1];
|
|
190
|
+
const az: number = quaternion[2];
|
|
191
|
+
const aw: number = quaternion[3];
|
|
192
|
+
|
|
193
|
+
const bx: number = Math.sin(r);
|
|
194
|
+
const bw: number = Math.cos(r);
|
|
195
|
+
|
|
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;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Rotates a quaternion by the given angle around the Y-axis.
|
|
205
|
+
* @param quaternion The quaternion.
|
|
206
|
+
* @param radians The angle in radians.
|
|
207
|
+
* @param out The quaternion to store the result in.
|
|
208
|
+
* @returns The rotated quaternion.
|
|
209
|
+
*/
|
|
210
|
+
export function rotateY<T extends QuaternionLike>(quaternion: QuaternionLike, radians: number, out: T): T {
|
|
211
|
+
const r: number = radians * 0.5;
|
|
212
|
+
|
|
213
|
+
const ax: number = quaternion[0];
|
|
214
|
+
const ay: number = quaternion[1];
|
|
215
|
+
const az: number = quaternion[2];
|
|
216
|
+
const aw: number = quaternion[3];
|
|
217
|
+
|
|
218
|
+
const by: number = Math.sin(r);
|
|
219
|
+
const bw: number = Math.cos(r);
|
|
220
|
+
|
|
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;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Rotates a quaternion by the given angle around the Z-axis.
|
|
230
|
+
* @param quaternion The quaternion.
|
|
231
|
+
* @param radians The angle in radians.
|
|
232
|
+
* @param out The quaternion to store the result in.
|
|
233
|
+
* @returns The rotated quaternion.
|
|
234
|
+
*/
|
|
235
|
+
export function rotateZ<T extends QuaternionLike>(quaternion: QuaternionLike, radians: number, out: T): T {
|
|
236
|
+
const r: number = radians * 0.5;
|
|
237
|
+
|
|
238
|
+
const ax: number = quaternion[0];
|
|
239
|
+
const ay: number = quaternion[1];
|
|
240
|
+
const az: number = quaternion[2];
|
|
241
|
+
const aw: number = quaternion[3];
|
|
242
|
+
|
|
243
|
+
const bz: number = Math.sin(r);
|
|
244
|
+
const bw: number = Math.cos(r);
|
|
245
|
+
|
|
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;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Calculates the fourth component of a unit quaternion from the first three, ignoring the existing fourth component.
|
|
255
|
+
* @param quaternion The quaternion.
|
|
256
|
+
* @param out The quaternion to store the result in.
|
|
257
|
+
* @returns The quaternion.
|
|
258
|
+
*/
|
|
259
|
+
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;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Calculates the exponential of a unit quaternion.
|
|
273
|
+
* @param quaternion The quaternion.
|
|
274
|
+
* @param out The quaternion to store the result in.
|
|
275
|
+
* @returns The exponential.
|
|
276
|
+
*/
|
|
277
|
+
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;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Calculates the natural logarithm of a unit quaternion.
|
|
296
|
+
* @param quaternion The quaternion.
|
|
297
|
+
* @param out The quaternion to store the result in.
|
|
298
|
+
* @returns The natural logarithm.
|
|
299
|
+
*/
|
|
300
|
+
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;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Calculates a power of a unit quaternion.
|
|
318
|
+
* @param quaternion The quaternion.
|
|
319
|
+
* @param scalar The amount to scale the quaternion by.
|
|
320
|
+
* @param out The quaternion to store the result in.
|
|
321
|
+
* @returns The power.
|
|
322
|
+
*/
|
|
323
|
+
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);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Performs a spherical linear interpolation between two quaternions.
|
|
329
|
+
* @param a The first quaternion.
|
|
330
|
+
* @param b The second quaternion.
|
|
331
|
+
* @param t The interpolation amount in `[0,1]`.
|
|
332
|
+
* @param out The quaternion to store the result in.
|
|
333
|
+
* @returns The interpolated quaternion.
|
|
334
|
+
* @see [Slerp](https://en.wikipedia.org/wiki/Slerp)
|
|
335
|
+
*/
|
|
336
|
+
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;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Sets a quaternion to a random unit quaternion.
|
|
378
|
+
* @param out The quaternion to store the result in.
|
|
379
|
+
* @returns The quaternion.
|
|
380
|
+
*/
|
|
381
|
+
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;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Calculates the inverse of a quaternion. If the quaternion is normalized, the conjugate is the same but faster to calculate.
|
|
398
|
+
* @param quaternion The quaternion.
|
|
399
|
+
* @param out The quaternion to store the result in.
|
|
400
|
+
* @returns The inverse.
|
|
401
|
+
*/
|
|
402
|
+
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;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Calculates the conjugate of a quaternion. If the quaternion is normalized, this is the same as the inverse but faster to calculate.
|
|
428
|
+
* @param quaternion The quaternion.
|
|
429
|
+
* @param out The quaternion to store the result in.
|
|
430
|
+
* @returns The conjugate.
|
|
431
|
+
*/
|
|
432
|
+
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;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/** A quaternion that is used to store intermediary values for some functions. */
|
|
441
|
+
const controlPointOne: QuaternionLike = new Float32Array(4) as QuaternionLike;
|
|
442
|
+
|
|
443
|
+
/** A quaternion that is used to store intermediary values for some functions. */
|
|
444
|
+
const controlPointTwo: QuaternionLike = new Float32Array(4) as QuaternionLike;
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Performs a spherical linear interpolation with two control points between two quaternions.
|
|
448
|
+
* @param a The first quaternion.
|
|
449
|
+
* @param b The first control point.
|
|
450
|
+
* @param c The second control point.
|
|
451
|
+
* @param d The second quaternion.
|
|
452
|
+
* @param t The interpolation amount in `[0,1]`.
|
|
453
|
+
* @param out The quaternion to store the result in.
|
|
454
|
+
* @returns The interpolated value.
|
|
455
|
+
* @see [Slerp](https://en.wikipedia.org/wiki/Slerp)
|
|
456
|
+
*/
|
|
457
|
+
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);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* A complex number that is commonly used to describe rotations.
|
|
465
|
+
* @see [Quaternion](https://en.wikipedia.org/wiki/Quaternion)
|
|
466
|
+
*/
|
|
467
|
+
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
|
+
/** The axis and angle that represent this quaternion. */
|
|
580
|
+
public get axisAngle(): AxisAngle {
|
|
581
|
+
// TODO: `out` parameter.
|
|
582
|
+
return getAxisAngle(this);
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/** The axis and angle that represent this quaternion. */
|
|
586
|
+
public set axisAngle(value: AxisAngle) {
|
|
587
|
+
setAxisAngle(this, value);
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Gets the angular distance between this unit quaternion and another.
|
|
592
|
+
* @param quaternion The other unit quaternion.
|
|
593
|
+
* @returns The angular distance in radians.
|
|
594
|
+
*/
|
|
595
|
+
public getAngle(quaternion: QuaternionLike): number {
|
|
596
|
+
return getAngle(this, quaternion);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* Multiplies this and another quaternion.
|
|
601
|
+
* @param quaternion The other quaternion.
|
|
602
|
+
* @returns The product.
|
|
603
|
+
*/
|
|
604
|
+
public multiply(quaternion: QuaternionLike): Quaternion;
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* Multiplies this and another quaternion.
|
|
608
|
+
* @param quaternion The other quaternion.
|
|
609
|
+
* @param out The quaternion to store the result in.
|
|
610
|
+
* @returns The product.
|
|
611
|
+
*/
|
|
612
|
+
public multiply<T extends QuaternionLike>(quaternion: QuaternionLike, out: T): T;
|
|
613
|
+
|
|
614
|
+
public multiply<T extends QuaternionLike>(quaternion: QuaternionLike, out: T = new Quaternion() as T): T {
|
|
615
|
+
return multiply(this, quaternion, out);
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* Rotates this quaternion by the given angle around the X-axis.
|
|
620
|
+
* @param radians The angle in radians.
|
|
621
|
+
* @returns The rotated quaternion.
|
|
622
|
+
*/
|
|
623
|
+
public rotateX(radians: number): Quaternion;
|
|
624
|
+
|
|
625
|
+
/**
|
|
626
|
+
* Rotates this quaternion by the given angle around the X-axis.
|
|
627
|
+
* @param radians The angle in radians.
|
|
628
|
+
* @param out The quaternion to store the result in.
|
|
629
|
+
* @returns The rotated quaternion.
|
|
630
|
+
*/
|
|
631
|
+
public rotateX<T extends QuaternionLike>(radians: number, out: T): T;
|
|
632
|
+
|
|
633
|
+
public rotateX<T extends QuaternionLike>(radians: number, out: T = new Quaternion() as T): T {
|
|
634
|
+
return rotateX(this, radians, out);
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* Rotates this quaternion by the given angle around the Y-axis.
|
|
639
|
+
* @param radians The angle in radians.
|
|
640
|
+
* @returns The rotated quaternion.
|
|
641
|
+
*/
|
|
642
|
+
public rotateY(radians: number): Quaternion;
|
|
643
|
+
|
|
644
|
+
/**
|
|
645
|
+
* Rotates this quaternion by the given angle around the Y-axis.
|
|
646
|
+
* @param radians The angle in radians.
|
|
647
|
+
* @param out The quaternion to store the result in.
|
|
648
|
+
* @returns The rotated quaternion.
|
|
649
|
+
*/
|
|
650
|
+
public rotateY<T extends QuaternionLike>(radians: number, out: T): T;
|
|
651
|
+
|
|
652
|
+
public rotateY<T extends QuaternionLike>(radians: number, out: T = new Quaternion() as T): T {
|
|
653
|
+
return rotateY(this, radians, out);
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/**
|
|
657
|
+
* Rotates this quaternion by the given angle around the Z-axis.
|
|
658
|
+
* @param radians The angle in radians.
|
|
659
|
+
* @returns The rotated quaternion.
|
|
660
|
+
*/
|
|
661
|
+
public rotateZ(radians: number): Quaternion;
|
|
662
|
+
|
|
663
|
+
/**
|
|
664
|
+
* Rotates this quaternion by the given angle around the Z-axis.
|
|
665
|
+
* @param radians The angle in radians.
|
|
666
|
+
* @param out The quaternion to store the result in.
|
|
667
|
+
* @returns The rotated quaternion.
|
|
668
|
+
*/
|
|
669
|
+
public rotateZ<T extends QuaternionLike>(radians: number, out: T): T;
|
|
670
|
+
|
|
671
|
+
public rotateZ<T extends QuaternionLike>(radians: number, out: T = new Quaternion() as T): T {
|
|
672
|
+
return rotateZ(this, radians, out);
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/**
|
|
676
|
+
* Calculates the fourth component of this unit quaternion from the first three, ignoring the existing fourth component.
|
|
677
|
+
* @returns The quaternion.
|
|
678
|
+
*/
|
|
679
|
+
public calculateW(): Quaternion;
|
|
680
|
+
|
|
681
|
+
/**
|
|
682
|
+
* Calculates the fourth component of this unit quaternion from the first three, ignoring the existing fourth component.
|
|
683
|
+
* @param out The quaternion to store the result in.
|
|
684
|
+
* @returns The quaternion.
|
|
685
|
+
*/
|
|
686
|
+
public calculateW<T extends QuaternionLike>(out: T): T;
|
|
687
|
+
|
|
688
|
+
public calculateW<T extends QuaternionLike>(out: T = new Quaternion() as T): T {
|
|
689
|
+
return calculateW(this, out);
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* Calculates the exponential of this unit quaternion.
|
|
694
|
+
* @returns The exponential.
|
|
695
|
+
*/
|
|
696
|
+
public exp(): Quaternion;
|
|
697
|
+
|
|
698
|
+
/**
|
|
699
|
+
* Calculates the exponential of this unit quaternion.
|
|
700
|
+
* @param out The quaternion to store the result in.
|
|
701
|
+
* @returns The exponential.
|
|
702
|
+
*/
|
|
703
|
+
public exp<T extends QuaternionLike>(out: T): T;
|
|
704
|
+
|
|
705
|
+
public exp<T extends QuaternionLike>(out: T = new Quaternion() as T): T {
|
|
706
|
+
return exp(this, out);
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
/**
|
|
710
|
+
* Calculates the natural logarithm of this unit quaternion.
|
|
711
|
+
* @returns The natural logarithm.
|
|
712
|
+
*/
|
|
713
|
+
public ln(): Quaternion;
|
|
714
|
+
|
|
715
|
+
/**
|
|
716
|
+
* Calculates the natural logarithm of this unit quaternion.
|
|
717
|
+
* @param out The quaternion to store the result in.
|
|
718
|
+
* @returns The natural logarithm.
|
|
719
|
+
*/
|
|
720
|
+
public ln<T extends QuaternionLike>(out: T): T;
|
|
721
|
+
|
|
722
|
+
public ln<T extends QuaternionLike>(out: T = new Quaternion() as T): T {
|
|
723
|
+
return ln(this, out);
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
/**
|
|
727
|
+
* Calculates a power of this unit quaternion.
|
|
728
|
+
* @param scalar The amount to scale the quaternion by.
|
|
729
|
+
* @returns The power.
|
|
730
|
+
*/
|
|
731
|
+
public pow(scalar: number): Quaternion;
|
|
732
|
+
|
|
733
|
+
/**
|
|
734
|
+
* Calculates a power of this unit quaternion.
|
|
735
|
+
* @param scalar The amount to scale the quaternion by.
|
|
736
|
+
* @param out The quaternion to store the result in.
|
|
737
|
+
* @returns The power.
|
|
738
|
+
*/
|
|
739
|
+
public pow<T extends QuaternionLike>(scalar: number, out: T): T;
|
|
740
|
+
|
|
741
|
+
public pow<T extends QuaternionLike>(scalar: number, out: T = new Quaternion() as T): T {
|
|
742
|
+
return pow(this, scalar, out);
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
/**
|
|
746
|
+
* Performs a spherical linear interpolation between this and another quaternion.
|
|
747
|
+
* @param quaternion The other quaternion.
|
|
748
|
+
* @param t The interpolation amount in `[0,1]`.
|
|
749
|
+
* @returns The interpolated quaternion.
|
|
750
|
+
* @see [Slerp](https://en.wikipedia.org/wiki/Slerp)
|
|
751
|
+
*/
|
|
752
|
+
public slerp(quaternion: QuaternionLike, t: number): Quaternion;
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* Performs a spherical linear interpolation between this and another quaternion.
|
|
756
|
+
* @param quaternion The other quaternion.
|
|
757
|
+
* @param t The interpolation amount in `[0,1]`.
|
|
758
|
+
* @param out The quaternion to store the result in.
|
|
759
|
+
* @returns The interpolated quaternion.
|
|
760
|
+
* @see [Slerp](https://en.wikipedia.org/wiki/Slerp)
|
|
761
|
+
*/
|
|
762
|
+
public slerp<T extends QuaternionLike>(quaternion: QuaternionLike, t: number, out: T): T;
|
|
763
|
+
|
|
764
|
+
public slerp<T extends QuaternionLike>(quaternion: QuaternionLike, t: number, out: T = new Quaternion() as T): T {
|
|
765
|
+
return slerp(this, quaternion, t, out);
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
/**
|
|
769
|
+
* Sets this to a random unit quaternion.
|
|
770
|
+
* @returns A random unit quaternion.
|
|
771
|
+
*/
|
|
772
|
+
public random(): this {
|
|
773
|
+
return random(this);
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
/**
|
|
777
|
+
* Calculates the inverse of this quaternion. If the quaternion is normalized, the conjugate is the same but faster to calculate.
|
|
778
|
+
* @returns The inverse.
|
|
779
|
+
*/
|
|
780
|
+
public invert(): Quaternion;
|
|
781
|
+
|
|
782
|
+
/**
|
|
783
|
+
* Calculates the inverse of this quaternion. If the quaternion is normalized, the conjugate is the same but faster to calculate.
|
|
784
|
+
* @param out The quaternion to store the result in.
|
|
785
|
+
* @returns The inverse.
|
|
786
|
+
*/
|
|
787
|
+
public invert<T extends QuaternionLike>(out: T): T;
|
|
788
|
+
|
|
789
|
+
public invert<T extends QuaternionLike>(out: T = new Quaternion() as T): T {
|
|
790
|
+
return invert(this, out);
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
/**
|
|
794
|
+
* Calculates the conjugate of this quaternion. If the quaternion is normalized, this is the same as the inverse but faster to calculate.
|
|
795
|
+
* @returns The conjugate.
|
|
796
|
+
*/
|
|
797
|
+
public conjugate(): Quaternion;
|
|
798
|
+
|
|
799
|
+
/**
|
|
800
|
+
* Calculates the conjugate of this quaternion. If the quaternion is normalized, this is the same as the inverse but faster to calculate.
|
|
801
|
+
* @param out The quaternion to store the result in.
|
|
802
|
+
* @returns The conjugate.
|
|
803
|
+
*/
|
|
804
|
+
public conjugate<T extends QuaternionLike>(out: T): T;
|
|
805
|
+
|
|
806
|
+
public conjugate<T extends QuaternionLike>(out: T = new Quaternion() as T): T {
|
|
807
|
+
return conjugate(this, out);
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
/**
|
|
811
|
+
* Creates a copy of this quaternion.
|
|
812
|
+
* @returns A copy of this quaternion.
|
|
813
|
+
*/
|
|
814
|
+
public clone(): Quaternion {
|
|
815
|
+
// TODO: `out` parameter.
|
|
816
|
+
return copy(this as unknown as Vector4Like, new Quaternion() as unknown as Vector4Like) as unknown as Quaternion;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
/**
|
|
820
|
+
* Copies the values of another quaternion into this one.
|
|
821
|
+
* @param quaternion The quaternion to copy.
|
|
822
|
+
* @returns This quaternion.
|
|
823
|
+
*/
|
|
824
|
+
public copy(quaternion: QuaternionLike): this {
|
|
825
|
+
return copy(quaternion as Vector4Like, this as unknown as Vector4Like) as unknown as this;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
/**
|
|
829
|
+
* Adds two quaternions of the same size.
|
|
830
|
+
* @param quaternion The other quaternion.
|
|
831
|
+
* @returns The sum of the quaternions.
|
|
832
|
+
*/
|
|
833
|
+
public add(quaternion: QuaternionLike): Quaternion;
|
|
834
|
+
|
|
835
|
+
/**
|
|
836
|
+
* Adds two quaternions of the same size.
|
|
837
|
+
* @param quaternion The other quaternion.
|
|
838
|
+
* @param out The quaternion to store the result in.
|
|
839
|
+
* @returns The sum of the quaternions.
|
|
840
|
+
*/
|
|
841
|
+
public add<T extends QuaternionLike>(quaternion: QuaternionLike, out: T): T;
|
|
842
|
+
|
|
843
|
+
public add<T extends QuaternionLike>(quaternion: QuaternionLike, out: T = new Quaternion() as T): T {
|
|
844
|
+
return add(this as unknown as Vector4Like, quaternion as Vector4Like, out as Vector4Like) as T;
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
/**
|
|
848
|
+
* Scales this quaternion by a scalar.
|
|
849
|
+
* @param scalar The scalar.
|
|
850
|
+
* @returns The scaled quaternion.
|
|
851
|
+
*/
|
|
852
|
+
public scale(scalar: number): Quaternion;
|
|
853
|
+
|
|
854
|
+
/**
|
|
855
|
+
* Scales this quaternion by a scalar.
|
|
856
|
+
* @param scalar The scalar.
|
|
857
|
+
* @param out The quaternion to store the result in.
|
|
858
|
+
* @returns The scaled quaternion.
|
|
859
|
+
*/
|
|
860
|
+
public scale<T extends QuaternionLike>(scalar: number, out: T): T;
|
|
861
|
+
|
|
862
|
+
public scale<T extends QuaternionLike>(scalar: number, out: T = new Quaternion() as T): T {
|
|
863
|
+
return scale(this as unknown as Vector4Like, scalar, out as Vector4Like) as T;
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
/**
|
|
867
|
+
* Calculates the dot product of this and another quaternion.
|
|
868
|
+
* @param quaternion The other quaternion.
|
|
869
|
+
* @returns The dot product.
|
|
870
|
+
*/
|
|
871
|
+
public dot(quaternion: QuaternionLike): number {
|
|
872
|
+
return dot(this as unknown as Vector4Like, quaternion as Vector4Like);
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
/**
|
|
876
|
+
* Performs a linear interpolation between this and another quaternion.
|
|
877
|
+
* @param quaternion The other quaternion.
|
|
878
|
+
* @param t The interpolation amount (in `[0,1]`).
|
|
879
|
+
* @returns The interpolated quaternion.
|
|
880
|
+
*/
|
|
881
|
+
public lerp(quaternion: QuaternionLike, t: number): Quaternion;
|
|
882
|
+
|
|
883
|
+
/**
|
|
884
|
+
* Performs a linear interpolation between this and another quaternion.
|
|
885
|
+
* @param quaternion The other quaternion.
|
|
886
|
+
* @param t The interpolation amount (in `[0,1]`).
|
|
887
|
+
* @param out The quaternion to store the result in.
|
|
888
|
+
* @returns The interpolated quaternion.
|
|
889
|
+
*/
|
|
890
|
+
public lerp<T extends QuaternionLike>(quaternion: QuaternionLike, t: number, out: T): T;
|
|
891
|
+
|
|
892
|
+
public lerp<T extends QuaternionLike>(quaternion: QuaternionLike, t: number, out: T = new Quaternion() as T): T {
|
|
893
|
+
return lerp(this as unknown as Vector4Like, quaternion as Vector4Like, t, out as Vector4Like) as T;
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
/** The magnitude (length) of this quaternion. */
|
|
897
|
+
public get magnitude(): number {
|
|
898
|
+
return getMagnitude(this as unknown as Vector4Like);
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
/** The squared magnitude (length) of this quaternion. */
|
|
902
|
+
public get squaredMagnitude(): number {
|
|
903
|
+
return getSquaredMagnitude(this as unknown as Vector4Like);
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
/**
|
|
907
|
+
* Normalizes this quaternion.
|
|
908
|
+
* @returns The normalized quaternion.
|
|
909
|
+
*/
|
|
910
|
+
public normalize(): Quaternion;
|
|
911
|
+
|
|
912
|
+
/**
|
|
913
|
+
* Normalizes this quaternion.
|
|
914
|
+
* @param out The quaternion to store the result in.
|
|
915
|
+
* @returns The normalized quaternion.
|
|
916
|
+
*/
|
|
917
|
+
public normalize<T extends QuaternionLike>(out: T): T;
|
|
918
|
+
|
|
919
|
+
public normalize<T extends QuaternionLike>(out: T = new Quaternion() as T): T {
|
|
920
|
+
return normalize(this as unknown as Vector4Like, out as Vector4Like) as T;
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
/**
|
|
924
|
+
* Determines whether this quaternion is roughly equivalent to another.
|
|
925
|
+
* @param quaternion The other quaternion.
|
|
926
|
+
* @returns Whether the quaternions are equivalent.
|
|
927
|
+
*/
|
|
928
|
+
public equals(quaternion: QuaternionLike): boolean {
|
|
929
|
+
return equals(this as unknown as Vector4, quaternion as Vector4Like);
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
/**
|
|
933
|
+
* Determines whether this quaternion is exactly equivalent to another.
|
|
934
|
+
* @param quaternion The other quaternion.
|
|
935
|
+
* @returns Whether the quaternions are equivalent.
|
|
936
|
+
*/
|
|
937
|
+
public exactEquals(quaternion: QuaternionLike): boolean {
|
|
938
|
+
return exactEquals(this as unknown as Vector4Like, quaternion as Vector4Like);
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
/**
|
|
942
|
+
* Performs a spherical linear interpolation with two control points between this and another quaternion.
|
|
943
|
+
* @param a The first control point.
|
|
944
|
+
* @param b The second control point.
|
|
945
|
+
* @param quaternion The other quaternion.
|
|
946
|
+
* @param t The interpolation amount in `[0,1]`.
|
|
947
|
+
* @returns The interpolated value.
|
|
948
|
+
* @see [Slerp](https://en.wikipedia.org/wiki/Slerp)
|
|
949
|
+
*/
|
|
950
|
+
public sqlerp(a: QuaternionLike, b: QuaternionLike, quaternion: QuaternionLike, t: number): Quaternion;
|
|
951
|
+
|
|
952
|
+
/**
|
|
953
|
+
* Performs a spherical linear interpolation with two control points between this and another quaternion.
|
|
954
|
+
* @param a The first control point.
|
|
955
|
+
* @param b The second control point.
|
|
956
|
+
* @param quaternion The other quaternion.
|
|
957
|
+
* @param t The interpolation amount in `[0,1]`.
|
|
958
|
+
* @param out The quaternion to store the result in.
|
|
959
|
+
* @returns The interpolated value.
|
|
960
|
+
* @see [Slerp](https://en.wikipedia.org/wiki/Slerp)
|
|
961
|
+
*/
|
|
962
|
+
public sqlerp<T extends QuaternionLike>(a: QuaternionLike, b: QuaternionLike, quaternion: QuaternionLike, t: number, out: T): T;
|
|
963
|
+
|
|
964
|
+
public sqlerp<T extends QuaternionLike>(a: QuaternionLike, b: QuaternionLike, quaternion: QuaternionLike, t: number, out: T = new Quaternion() as T): T {
|
|
965
|
+
return sqlerp(this, a, b, quaternion, t, out);
|
|
966
|
+
}
|
|
967
|
+
}
|