@lakuna/umath 1.3.9 → 1.4.2
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 +1 -1
- package/dist/algorithms/approx.d.ts +9 -0
- package/dist/algorithms/approx.d.ts.map +1 -0
- package/dist/algorithms/approx.js +12 -0
- package/dist/algorithms/approx.js.map +1 -0
- package/dist/algorithms/approxRelative.d.ts +9 -0
- package/dist/algorithms/approxRelative.d.ts.map +1 -0
- package/dist/algorithms/approxRelative.js +12 -0
- package/dist/algorithms/approxRelative.js.map +1 -0
- package/dist/algorithms/combinations.d.ts +2 -1
- package/dist/algorithms/combinations.d.ts.map +1 -1
- package/dist/algorithms/combinations.js +2 -1
- package/dist/algorithms/combinations.js.map +1 -1
- package/dist/algorithms/degreesToRadians.d.ts +3 -2
- package/dist/algorithms/degreesToRadians.d.ts.map +1 -1
- package/dist/algorithms/degreesToRadians.js +5 -3
- package/dist/algorithms/degreesToRadians.js.map +1 -1
- package/dist/algorithms/factorial.d.ts +2 -1
- package/dist/algorithms/factorial.d.ts.map +1 -1
- package/dist/algorithms/factorial.js +2 -1
- package/dist/algorithms/factorial.js.map +1 -1
- package/dist/algorithms/fibonacci.d.ts +2 -1
- package/dist/algorithms/fibonacci.d.ts.map +1 -1
- package/dist/algorithms/fibonacci.js +6 -2
- package/dist/algorithms/fibonacci.js.map +1 -1
- package/dist/algorithms/greatestCommonDivisor.d.ts +4 -2
- package/dist/algorithms/greatestCommonDivisor.d.ts.map +1 -1
- package/dist/algorithms/greatestCommonDivisor.js +5 -2
- package/dist/algorithms/greatestCommonDivisor.js.map +1 -1
- package/dist/algorithms/hypergeometricPmf.d.ts +3 -2
- package/dist/algorithms/hypergeometricPmf.d.ts.map +1 -1
- package/dist/algorithms/hypergeometricPmf.js +3 -2
- package/dist/algorithms/hypergeometricPmf.js.map +1 -1
- package/dist/algorithms/isPrime.d.ts +2 -1
- package/dist/algorithms/isPrime.d.ts.map +1 -1
- package/dist/algorithms/isPrime.js +2 -1
- package/dist/algorithms/isPrime.js.map +1 -1
- package/dist/algorithms/permutations.d.ts +2 -1
- package/dist/algorithms/permutations.d.ts.map +1 -1
- package/dist/algorithms/permutations.js +2 -1
- package/dist/algorithms/permutations.js.map +1 -1
- package/dist/algorithms/primeFactorization.d.ts +2 -1
- package/dist/algorithms/primeFactorization.d.ts.map +1 -1
- package/dist/algorithms/primeFactorization.js +2 -1
- package/dist/algorithms/primeFactorization.js.map +1 -1
- package/dist/algorithms/radiansToDegrees.d.ts +3 -2
- package/dist/algorithms/radiansToDegrees.d.ts.map +1 -1
- package/dist/algorithms/radiansToDegrees.js +5 -3
- package/dist/algorithms/radiansToDegrees.js.map +1 -1
- package/dist/algorithms/summation.d.ts +2 -1
- package/dist/algorithms/summation.d.ts.map +1 -1
- package/dist/algorithms/summation.js +2 -1
- package/dist/algorithms/summation.js.map +1 -1
- package/dist/linalg/DualQuaternion.d.ts +102 -32
- package/dist/linalg/DualQuaternion.d.ts.map +1 -1
- package/dist/linalg/DualQuaternion.js +243 -272
- package/dist/linalg/DualQuaternion.js.map +1 -1
- package/dist/linalg/Matrix.d.ts +14 -10
- package/dist/linalg/Matrix.d.ts.map +1 -1
- package/dist/linalg/Matrix2.d.ts +74 -51
- package/dist/linalg/Matrix2.d.ts.map +1 -1
- package/dist/linalg/Matrix2.js +95 -171
- package/dist/linalg/Matrix2.js.map +1 -1
- package/dist/linalg/Matrix3.d.ts +122 -72
- package/dist/linalg/Matrix3.d.ts.map +1 -1
- package/dist/linalg/Matrix3.js +186 -355
- package/dist/linalg/Matrix3.js.map +1 -1
- package/dist/linalg/Matrix4.d.ts +266 -149
- package/dist/linalg/Matrix4.d.ts.map +1 -1
- package/dist/linalg/Matrix4.js +512 -852
- package/dist/linalg/Matrix4.js.map +1 -1
- package/dist/linalg/Quaternion.d.ts +252 -34
- package/dist/linalg/Quaternion.d.ts.map +1 -1
- package/dist/linalg/Quaternion.js +436 -166
- package/dist/linalg/Quaternion.js.map +1 -1
- package/dist/linalg/SlowMatrix.d.ts +10 -9
- package/dist/linalg/SlowMatrix.d.ts.map +1 -1
- package/dist/linalg/SlowMatrix.js +10 -9
- package/dist/linalg/SlowMatrix.js.map +1 -1
- package/dist/linalg/SlowSquareMatrix.d.ts +10 -9
- package/dist/linalg/SlowSquareMatrix.d.ts.map +1 -1
- package/dist/linalg/SlowSquareMatrix.js +10 -9
- package/dist/linalg/SlowSquareMatrix.js.map +1 -1
- package/dist/linalg/SquareMatrix.d.ts +6 -5
- package/dist/linalg/SquareMatrix.d.ts.map +1 -1
- package/dist/linalg/Vector.d.ts +8 -4
- package/dist/linalg/Vector.d.ts.map +1 -1
- package/dist/linalg/Vector2.d.ts +82 -31
- package/dist/linalg/Vector2.d.ts.map +1 -1
- package/dist/linalg/Vector2.js +112 -154
- package/dist/linalg/Vector2.js.map +1 -1
- package/dist/linalg/Vector3.d.ts +93 -41
- package/dist/linalg/Vector3.d.ts.map +1 -1
- package/dist/linalg/Vector3.js +171 -282
- package/dist/linalg/Vector3.js.map +1 -1
- package/dist/linalg/Vector4.d.ts +71 -21
- package/dist/linalg/Vector4.d.ts.map +1 -1
- package/dist/linalg/Vector4.js +121 -195
- package/dist/linalg/Vector4.js.map +1 -1
- package/dist/types/AxisAngle.d.ts +4 -1
- package/dist/types/AxisAngle.d.ts.map +1 -1
- package/dist/types/FieldOfView.d.ts +4 -1
- package/dist/types/FieldOfView.d.ts.map +1 -1
- package/dist/utility/BigNumber.d.ts +4 -1
- package/dist/utility/BigNumber.d.ts.map +1 -1
- package/dist/utility/BigNumber.js +4 -1
- package/dist/utility/BigNumber.js.map +1 -1
- package/dist/utility/MagnitudeError.d.ts +4 -1
- package/dist/utility/MagnitudeError.d.ts.map +1 -1
- package/dist/utility/MagnitudeError.js +4 -1
- package/dist/utility/MagnitudeError.js.map +1 -1
- package/dist/utility/MatrixSizeError.d.ts +4 -1
- package/dist/utility/MatrixSizeError.d.ts.map +1 -1
- package/dist/utility/MatrixSizeError.js +4 -1
- package/dist/utility/MatrixSizeError.js.map +1 -1
- package/dist/utility/PartialMatrixError.d.ts +4 -1
- package/dist/utility/PartialMatrixError.d.ts.map +1 -1
- package/dist/utility/PartialMatrixError.js +4 -1
- package/dist/utility/PartialMatrixError.js.map +1 -1
- package/dist/utility/SingularMatrixError.d.ts +2 -1
- package/dist/utility/SingularMatrixError.d.ts.map +1 -1
- package/dist/utility/SingularMatrixError.js +2 -1
- package/dist/utility/SingularMatrixError.js.map +1 -1
- package/dist/utility/epsilon.d.ts +4 -1
- package/dist/utility/epsilon.d.ts.map +1 -1
- package/dist/utility/epsilon.js +4 -1
- package/dist/utility/epsilon.js.map +1 -1
- package/package.json +11 -11
- package/src/algorithms/approx.ts +12 -0
- package/src/algorithms/approxRelative.ts +12 -0
- package/src/algorithms/combinations.ts +2 -1
- package/src/algorithms/degreesToRadians.ts +6 -3
- package/src/algorithms/factorial.ts +3 -1
- package/src/algorithms/fibonacci.ts +7 -2
- package/src/algorithms/greatestCommonDivisor.ts +9 -4
- package/src/algorithms/hypergeometricPmf.ts +3 -2
- package/src/algorithms/isPrime.ts +2 -1
- package/src/algorithms/permutations.ts +2 -1
- package/src/algorithms/primeFactorization.ts +2 -1
- package/src/algorithms/radiansToDegrees.ts +6 -3
- package/src/algorithms/summation.ts +2 -1
- package/src/linalg/DualQuaternion.ts +424 -289
- package/src/linalg/Matrix.ts +14 -10
- package/src/linalg/Matrix2.ts +141 -188
- package/src/linalg/Matrix3.ts +400 -375
- package/src/linalg/Matrix4.ts +1083 -905
- package/src/linalg/Quaternion.ts +706 -188
- package/src/linalg/SlowMatrix.ts +10 -9
- package/src/linalg/SlowSquareMatrix.ts +10 -9
- package/src/linalg/SquareMatrix.ts +6 -5
- package/src/linalg/Vector.ts +8 -4
- package/src/linalg/Vector2.ts +146 -173
- package/src/linalg/Vector3.ts +293 -326
- package/src/linalg/Vector4.ts +227 -215
- package/src/types/AxisAngle.ts +4 -1
- package/src/types/FieldOfView.ts +4 -1
- package/src/utility/BigNumber.ts +6 -3
- package/src/utility/MagnitudeError.ts +4 -1
- package/src/utility/MatrixSizeError.ts +4 -1
- package/src/utility/PartialMatrixError.ts +4 -1
- package/src/utility/SingularMatrixError.ts +2 -1
- package/src/utility/epsilon.ts +4 -1
package/src/linalg/Vector3.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import Quaternion, { type QuaternionLike } from "./Quaternion.js";
|
|
2
1
|
import type { default as Vector, VectorLike } from "./Vector.js";
|
|
3
2
|
import type { Matrix3Like } from "./Matrix3.js";
|
|
4
3
|
import type { Matrix4Like } from "./Matrix4.js";
|
|
5
|
-
import
|
|
6
|
-
import
|
|
4
|
+
import type { QuaternionLike } from "./Quaternion.js";
|
|
5
|
+
import approxRelative from "../algorithms/approxRelative.js";
|
|
7
6
|
|
|
8
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* A quantity with magnitude and direction in three dimensions.
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
9
11
|
export interface Vector3Like extends VectorLike {
|
|
10
12
|
/** The first component of this vector. */
|
|
11
13
|
0: number;
|
|
@@ -20,8 +22,9 @@ export interface Vector3Like extends VectorLike {
|
|
|
20
22
|
/**
|
|
21
23
|
* Creates a 3x1 vector-like object.
|
|
22
24
|
* @returns A 3x1 vector-like object.
|
|
25
|
+
* @public
|
|
23
26
|
*/
|
|
24
|
-
export const createVector3Like = () => {
|
|
27
|
+
export const createVector3Like = (): Float32Array & Vector3Like => {
|
|
25
28
|
return new Float32Array(3) as Float32Array & Vector3Like;
|
|
26
29
|
};
|
|
27
30
|
|
|
@@ -32,6 +35,7 @@ export const createVector3Like = () => {
|
|
|
32
35
|
* @param z - The third component.
|
|
33
36
|
* @param out - The vector to store the result in.
|
|
34
37
|
* @returns A new vector.
|
|
38
|
+
* @public
|
|
35
39
|
*/
|
|
36
40
|
export const fromValues = <T extends Vector3Like>(
|
|
37
41
|
x: number,
|
|
@@ -50,32 +54,22 @@ export const fromValues = <T extends Vector3Like>(
|
|
|
50
54
|
* @param a - The first vector.
|
|
51
55
|
* @param b - The second vector.
|
|
52
56
|
* @returns Whether or not the vectors are equivalent.
|
|
57
|
+
* @public
|
|
53
58
|
*/
|
|
54
|
-
export const equals = (a: Vector3Like, b: Vector3Like): boolean =>
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const b0 = b[0];
|
|
60
|
-
const b1 = b[1];
|
|
61
|
-
const b2 = b[2];
|
|
62
|
-
|
|
63
|
-
return (
|
|
64
|
-
Math.abs(a0 - b0) <= epsilon * Math.max(1, Math.abs(a0), Math.abs(b0)) &&
|
|
65
|
-
Math.abs(a1 - b1) <= epsilon * Math.max(1, Math.abs(a1), Math.abs(b1)) &&
|
|
66
|
-
Math.abs(a2 - b2) <= epsilon * Math.max(1, Math.abs(a2), Math.abs(b2))
|
|
67
|
-
);
|
|
68
|
-
};
|
|
59
|
+
export const equals = (a: Vector3Like, b: Vector3Like): boolean =>
|
|
60
|
+
approxRelative(a[0], b[0]) &&
|
|
61
|
+
approxRelative(a[1], b[1]) &&
|
|
62
|
+
approxRelative(a[2], b[2]);
|
|
69
63
|
|
|
70
64
|
/**
|
|
71
65
|
* Determine whether two vectors are exactly equivalent.
|
|
72
66
|
* @param a - The first vector.
|
|
73
67
|
* @param b - The second vector.
|
|
74
68
|
* @returns Whether the vectors are equivalent.
|
|
69
|
+
* @public
|
|
75
70
|
*/
|
|
76
|
-
export const exactEquals = (a: Vector3Like, b: Vector3Like): boolean =>
|
|
77
|
-
|
|
78
|
-
};
|
|
71
|
+
export const exactEquals = (a: Vector3Like, b: Vector3Like): boolean =>
|
|
72
|
+
a[0] === b[0] && a[1] === b[1] && a[2] === b[2];
|
|
79
73
|
|
|
80
74
|
/**
|
|
81
75
|
* Add two vectors.
|
|
@@ -83,30 +77,23 @@ export const exactEquals = (a: Vector3Like, b: Vector3Like): boolean => {
|
|
|
83
77
|
* @param b - The addend.
|
|
84
78
|
* @param out - The vector to store the result in.
|
|
85
79
|
* @returns The sum.
|
|
80
|
+
* @public
|
|
86
81
|
*/
|
|
87
82
|
export const add = <T extends Vector3Like>(
|
|
88
83
|
a: Vector3Like,
|
|
89
84
|
b: Vector3Like,
|
|
90
85
|
out: T
|
|
91
|
-
): T =>
|
|
92
|
-
out[0] = a[0] + b[0];
|
|
93
|
-
out[1] = a[1] + b[1];
|
|
94
|
-
out[2] = a[2] + b[2];
|
|
95
|
-
return out;
|
|
96
|
-
};
|
|
86
|
+
): T => fromValues(a[0] + b[0], a[1] + b[1], a[2] + b[2], out);
|
|
97
87
|
|
|
98
88
|
/**
|
|
99
89
|
* Copy the values from one vector to another.
|
|
100
90
|
* @param vector - The vector to copy.
|
|
101
91
|
* @param out - The vector to store the result in.
|
|
102
92
|
* @returns The copy.
|
|
93
|
+
* @public
|
|
103
94
|
*/
|
|
104
|
-
export const copy = <T extends Vector3Like>(vector: Vector3Like, out: T): T =>
|
|
105
|
-
|
|
106
|
-
out[1] = vector[1];
|
|
107
|
-
out[2] = vector[2];
|
|
108
|
-
return out;
|
|
109
|
-
};
|
|
95
|
+
export const copy = <T extends Vector3Like>(vector: Vector3Like, out: T): T =>
|
|
96
|
+
fromValues(vector[0], vector[1], vector[2], out);
|
|
110
97
|
|
|
111
98
|
/**
|
|
112
99
|
* Multiply two vectors.
|
|
@@ -114,17 +101,13 @@ export const copy = <T extends Vector3Like>(vector: Vector3Like, out: T): T => {
|
|
|
114
101
|
* @param b - The multiplicand.
|
|
115
102
|
* @param out - The vector to store the result in.
|
|
116
103
|
* @returns The product.
|
|
104
|
+
* @public
|
|
117
105
|
*/
|
|
118
106
|
export const multiply = <T extends Vector3Like>(
|
|
119
107
|
a: Vector3Like,
|
|
120
108
|
b: Vector3Like,
|
|
121
109
|
out: T
|
|
122
|
-
): T =>
|
|
123
|
-
out[0] = a[0] * b[0];
|
|
124
|
-
out[1] = a[1] * b[1];
|
|
125
|
-
out[2] = a[2] * b[2];
|
|
126
|
-
return out;
|
|
127
|
-
};
|
|
110
|
+
): T => fromValues(a[0] * b[0], a[1] * b[1], a[2] * b[2], out);
|
|
128
111
|
|
|
129
112
|
/**
|
|
130
113
|
* Divide two vectors.
|
|
@@ -132,17 +115,13 @@ export const multiply = <T extends Vector3Like>(
|
|
|
132
115
|
* @param b - The divisor.
|
|
133
116
|
* @param out - The vector to store the result in.
|
|
134
117
|
* @returns The quotient.
|
|
118
|
+
* @public
|
|
135
119
|
*/
|
|
136
120
|
export const divide = <T extends Vector3Like>(
|
|
137
121
|
a: Vector3Like,
|
|
138
122
|
b: Vector3Like,
|
|
139
123
|
out: T
|
|
140
|
-
): T =>
|
|
141
|
-
out[0] = a[0] / b[0];
|
|
142
|
-
out[1] = a[1] / b[1];
|
|
143
|
-
out[2] = a[2] / b[2];
|
|
144
|
-
return out;
|
|
145
|
-
};
|
|
124
|
+
): T => fromValues(a[0] / b[0], a[1] / b[1], a[2] / b[2], out);
|
|
146
125
|
|
|
147
126
|
/**
|
|
148
127
|
* Subtract two vectors.
|
|
@@ -150,62 +129,73 @@ export const divide = <T extends Vector3Like>(
|
|
|
150
129
|
* @param b - The subtrahend.
|
|
151
130
|
* @param out - The vector to store the result in.
|
|
152
131
|
* @returns The difference.
|
|
132
|
+
* @public
|
|
153
133
|
*/
|
|
154
134
|
export const subtract = <T extends Vector3Like>(
|
|
155
135
|
a: Vector3Like,
|
|
156
136
|
b: Vector3Like,
|
|
157
137
|
out: T
|
|
158
|
-
): T =>
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
138
|
+
): T => fromValues(a[0] - b[0], a[1] - b[1], a[2] - b[2], out);
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Absolutize the components of a vector.
|
|
142
|
+
* @param vector - The vector.
|
|
143
|
+
* @param out - The vector to store the result in.
|
|
144
|
+
* @returns The absolutized vector.
|
|
145
|
+
* @public
|
|
146
|
+
*/
|
|
147
|
+
export const abs = <T extends Vector3Like>(vector: Vector3Like, out: T): T =>
|
|
148
|
+
fromValues(
|
|
149
|
+
Math.abs(vector[0]),
|
|
150
|
+
Math.abs(vector[1]),
|
|
151
|
+
Math.abs(vector[2]),
|
|
152
|
+
out
|
|
153
|
+
);
|
|
164
154
|
|
|
165
155
|
/**
|
|
166
156
|
* Round up the components of a vector.
|
|
167
157
|
* @param vector - The vector.
|
|
168
158
|
* @param out - The vector to store the result in.
|
|
169
159
|
* @returns The rounded vector.
|
|
160
|
+
* @public
|
|
170
161
|
*/
|
|
171
|
-
export const ceil = <T extends Vector3Like>(vector: Vector3Like, out: T): T =>
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
162
|
+
export const ceil = <T extends Vector3Like>(vector: Vector3Like, out: T): T =>
|
|
163
|
+
fromValues(
|
|
164
|
+
Math.ceil(vector[0]),
|
|
165
|
+
Math.ceil(vector[1]),
|
|
166
|
+
Math.ceil(vector[2]),
|
|
167
|
+
out
|
|
168
|
+
);
|
|
177
169
|
|
|
178
170
|
/**
|
|
179
171
|
* Round down the components of a vector.
|
|
180
172
|
* @param vector - The vector.
|
|
181
173
|
* @param out - The vector to store the result in.
|
|
182
174
|
* @returns The rounded vector.
|
|
175
|
+
* @public
|
|
183
176
|
*/
|
|
184
|
-
export const floor = <T extends Vector3Like>(
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
)
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
return out;
|
|
192
|
-
};
|
|
177
|
+
export const floor = <T extends Vector3Like>(vector: Vector3Like, out: T): T =>
|
|
178
|
+
fromValues(
|
|
179
|
+
Math.floor(vector[0]),
|
|
180
|
+
Math.floor(vector[1]),
|
|
181
|
+
Math.floor(vector[2]),
|
|
182
|
+
out
|
|
183
|
+
);
|
|
193
184
|
|
|
194
185
|
/**
|
|
195
186
|
* Round the components of a vector.
|
|
196
187
|
* @param vector - The vector.
|
|
197
188
|
* @param out - The vector to store the result in.
|
|
198
189
|
* @returns The rounded vector.
|
|
190
|
+
* @public
|
|
199
191
|
*/
|
|
200
|
-
export const round = <T extends Vector3Like>(
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
)
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
return out;
|
|
208
|
-
};
|
|
192
|
+
export const round = <T extends Vector3Like>(vector: Vector3Like, out: T): T =>
|
|
193
|
+
fromValues(
|
|
194
|
+
Math.round(vector[0]),
|
|
195
|
+
Math.round(vector[1]),
|
|
196
|
+
Math.round(vector[2]),
|
|
197
|
+
out
|
|
198
|
+
);
|
|
209
199
|
|
|
210
200
|
/**
|
|
211
201
|
* Return the minimum of two vectors.
|
|
@@ -213,17 +203,19 @@ export const round = <T extends Vector3Like>(
|
|
|
213
203
|
* @param b - The second vector.
|
|
214
204
|
* @param out - The vector to store the result in.
|
|
215
205
|
* @returns The minimum.
|
|
206
|
+
* @public
|
|
216
207
|
*/
|
|
217
208
|
export const min = <T extends Vector3Like>(
|
|
218
209
|
a: Vector3Like,
|
|
219
210
|
b: Vector3Like,
|
|
220
211
|
out: T
|
|
221
|
-
): T =>
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
212
|
+
): T =>
|
|
213
|
+
fromValues(
|
|
214
|
+
Math.min(a[0], b[0]),
|
|
215
|
+
Math.min(a[1], b[1]),
|
|
216
|
+
Math.min(a[2], b[2]),
|
|
217
|
+
out
|
|
218
|
+
);
|
|
227
219
|
|
|
228
220
|
/**
|
|
229
221
|
* Return the maximum of two vectors.
|
|
@@ -231,17 +223,19 @@ export const min = <T extends Vector3Like>(
|
|
|
231
223
|
* @param b - The second vector.
|
|
232
224
|
* @param out - The vector to store the result in.
|
|
233
225
|
* @returns The maximum.
|
|
226
|
+
* @public
|
|
234
227
|
*/
|
|
235
228
|
export const max = <T extends Vector3Like>(
|
|
236
229
|
a: Vector3Like,
|
|
237
230
|
b: Vector3Like,
|
|
238
231
|
out: T
|
|
239
|
-
): T =>
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
232
|
+
): T =>
|
|
233
|
+
fromValues(
|
|
234
|
+
Math.max(a[0], b[0]),
|
|
235
|
+
Math.max(a[1], b[1]),
|
|
236
|
+
Math.max(a[2], b[2]),
|
|
237
|
+
out
|
|
238
|
+
);
|
|
245
239
|
|
|
246
240
|
/**
|
|
247
241
|
* Scale a vector by a scalar.
|
|
@@ -249,17 +243,14 @@ export const max = <T extends Vector3Like>(
|
|
|
249
243
|
* @param scalar - The multiplicand.
|
|
250
244
|
* @param out - The vector to store the result in.
|
|
251
245
|
* @returns The product.
|
|
246
|
+
* @public
|
|
252
247
|
*/
|
|
253
248
|
export const scale = <T extends Vector3Like>(
|
|
254
249
|
vector: Vector3Like,
|
|
255
250
|
scalar: number,
|
|
256
251
|
out: T
|
|
257
|
-
): T =>
|
|
258
|
-
|
|
259
|
-
out[1] = vector[1] * scalar;
|
|
260
|
-
out[2] = vector[2] * scalar;
|
|
261
|
-
return out;
|
|
262
|
-
};
|
|
252
|
+
): T =>
|
|
253
|
+
fromValues(vector[0] * scalar, vector[1] * scalar, vector[2] * scalar, out);
|
|
263
254
|
|
|
264
255
|
/**
|
|
265
256
|
* Add two vectors after scaling the second by a scalar.
|
|
@@ -268,31 +259,34 @@ export const scale = <T extends Vector3Like>(
|
|
|
268
259
|
* @param scalar - The multiplicand.
|
|
269
260
|
* @param out - The vector to store the result in.
|
|
270
261
|
* @returns The sum.
|
|
262
|
+
* @public
|
|
271
263
|
*/
|
|
272
264
|
export const scaleAndAdd = <T extends Vector3Like>(
|
|
273
265
|
a: Vector3Like,
|
|
274
266
|
b: Vector3Like,
|
|
275
267
|
scalar: number,
|
|
276
268
|
out: T
|
|
277
|
-
): T =>
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
269
|
+
): T =>
|
|
270
|
+
fromValues(
|
|
271
|
+
a[0] + b[0] * scalar,
|
|
272
|
+
a[1] + b[1] * scalar,
|
|
273
|
+
a[2] + b[2] * scalar,
|
|
274
|
+
out
|
|
275
|
+
);
|
|
283
276
|
|
|
284
277
|
/**
|
|
285
278
|
* Calculate the Euclidean distance between two vectors.
|
|
286
279
|
* @param a - The first vector.
|
|
287
280
|
* @param b - The second vector.
|
|
288
281
|
* @returns The distance.
|
|
289
|
-
* @see
|
|
282
|
+
* @see {@link https://en.wikipedia.org/wiki/Euclidean_distance | Euclidean distance}
|
|
283
|
+
* @public
|
|
290
284
|
*/
|
|
291
285
|
export const distance = (a: Vector3Like, b: Vector3Like): number => {
|
|
292
286
|
const x = b[0] - a[0];
|
|
293
287
|
const y = b[1] - a[1];
|
|
294
288
|
const z = b[2] - a[2];
|
|
295
|
-
return Math.sqrt(x * x + y * y + z * z);
|
|
289
|
+
return Math.sqrt(x * x + y * y + z * z); // `Math.hypot` is slower.
|
|
296
290
|
};
|
|
297
291
|
|
|
298
292
|
/**
|
|
@@ -300,7 +294,8 @@ export const distance = (a: Vector3Like, b: Vector3Like): number => {
|
|
|
300
294
|
* @param a - The first vector.
|
|
301
295
|
* @param b - The second vector.
|
|
302
296
|
* @returns The squared distance.
|
|
303
|
-
* @see
|
|
297
|
+
* @see {@link https://en.wikipedia.org/wiki/Euclidean_distance | Euclidean distance}
|
|
298
|
+
* @public
|
|
304
299
|
*/
|
|
305
300
|
export const squaredDistance = (a: Vector3Like, b: Vector3Like): number => {
|
|
306
301
|
const x = b[0] - a[0];
|
|
@@ -313,18 +308,20 @@ export const squaredDistance = (a: Vector3Like, b: Vector3Like): number => {
|
|
|
313
308
|
* Calculate the magnitude (length) of a vector.
|
|
314
309
|
* @param vector - The vector.
|
|
315
310
|
* @returns The magnitude.
|
|
311
|
+
* @public
|
|
316
312
|
*/
|
|
317
313
|
export const getMagnitude = (vector: Vector3Like): number => {
|
|
318
314
|
const x = vector[0];
|
|
319
315
|
const y = vector[1];
|
|
320
316
|
const z = vector[2];
|
|
321
|
-
return Math.sqrt(x * x + y * y + z * z);
|
|
317
|
+
return Math.sqrt(x * x + y * y + z * z); // `Math.hypot` is slower.
|
|
322
318
|
};
|
|
323
319
|
|
|
324
320
|
/**
|
|
325
321
|
* Calculate the squared magnitude (length) of a vector.
|
|
326
322
|
* @param vector - The vector.
|
|
327
323
|
* @returns The squared magnitude.
|
|
324
|
+
* @public
|
|
328
325
|
*/
|
|
329
326
|
export const getSquaredMagnitude = (vector: Vector3Like): number => {
|
|
330
327
|
const x = vector[0];
|
|
@@ -338,39 +335,28 @@ export const getSquaredMagnitude = (vector: Vector3Like): number => {
|
|
|
338
335
|
* @param vector - The vector.
|
|
339
336
|
* @param out - The vector to store the result in.
|
|
340
337
|
* @returns The negated vector.
|
|
338
|
+
* @public
|
|
341
339
|
*/
|
|
342
|
-
export const negate = <T extends Vector3Like>(
|
|
343
|
-
vector
|
|
344
|
-
out: T
|
|
345
|
-
): T => {
|
|
346
|
-
out[0] = -vector[0];
|
|
347
|
-
out[1] = -vector[1];
|
|
348
|
-
out[2] = -vector[2];
|
|
349
|
-
return out;
|
|
350
|
-
};
|
|
340
|
+
export const negate = <T extends Vector3Like>(vector: Vector3Like, out: T): T =>
|
|
341
|
+
fromValues(-vector[0], -vector[1], -vector[2], out);
|
|
351
342
|
|
|
352
343
|
/**
|
|
353
344
|
* Calculate the multiplicative inverse of the components of a vector.
|
|
354
345
|
* @param vector - The vector.
|
|
355
346
|
* @param out - The vector to store the result in.
|
|
356
347
|
* @returns The inverted vector.
|
|
348
|
+
* @public
|
|
357
349
|
*/
|
|
358
|
-
export const invert = <T extends Vector3Like>(
|
|
359
|
-
vector
|
|
360
|
-
out: T
|
|
361
|
-
): T => {
|
|
362
|
-
out[0] = 1 / vector[0];
|
|
363
|
-
out[1] = 1 / vector[1];
|
|
364
|
-
out[2] = 1 / vector[2];
|
|
365
|
-
return out;
|
|
366
|
-
};
|
|
350
|
+
export const invert = <T extends Vector3Like>(vector: Vector3Like, out: T): T =>
|
|
351
|
+
fromValues(1 / vector[0], 1 / vector[1], 1 / vector[2], out);
|
|
367
352
|
|
|
368
353
|
/**
|
|
369
354
|
* Normalize a vector.
|
|
370
355
|
* @param vector - The vector.
|
|
371
356
|
* @param out - The vector to store the result in.
|
|
372
357
|
* @returns The normalized vector.
|
|
373
|
-
* @see
|
|
358
|
+
* @see {@link https://en.wikipedia.org/wiki/Unit_vector | Unit vector}
|
|
359
|
+
* @public
|
|
374
360
|
*/
|
|
375
361
|
export const normalize = <T extends Vector3Like>(
|
|
376
362
|
vector: Vector3Like,
|
|
@@ -385,10 +371,7 @@ export const normalize = <T extends Vector3Like>(
|
|
|
385
371
|
len = 1 / Math.sqrt(len);
|
|
386
372
|
}
|
|
387
373
|
|
|
388
|
-
|
|
389
|
-
out[1] = y * len;
|
|
390
|
-
out[2] = z * len;
|
|
391
|
-
return out;
|
|
374
|
+
return fromValues(x * len, y * len, z * len, out);
|
|
392
375
|
};
|
|
393
376
|
|
|
394
377
|
/**
|
|
@@ -396,11 +379,11 @@ export const normalize = <T extends Vector3Like>(
|
|
|
396
379
|
* @param a - The multiplier.
|
|
397
380
|
* @param b - The multiplicand.
|
|
398
381
|
* @returns The dot product.
|
|
399
|
-
* @see
|
|
382
|
+
* @see {@link https://en.wikipedia.org/wiki/Dot_product | Dot product}
|
|
383
|
+
* @public
|
|
400
384
|
*/
|
|
401
|
-
export const dot = (a: Vector3Like, b: Vector3Like): number =>
|
|
402
|
-
|
|
403
|
-
};
|
|
385
|
+
export const dot = (a: Vector3Like, b: Vector3Like): number =>
|
|
386
|
+
a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
404
387
|
|
|
405
388
|
/**
|
|
406
389
|
* Calculate the cross product of two vectors.
|
|
@@ -408,7 +391,8 @@ export const dot = (a: Vector3Like, b: Vector3Like): number => {
|
|
|
408
391
|
* @param b - The mutliplicand.
|
|
409
392
|
* @param out - The vector to store the result in.
|
|
410
393
|
* @returns The cross product.
|
|
411
|
-
* @see
|
|
394
|
+
* @see {@link https://en.wikipedia.org/wiki/Cross_product | Cross product}
|
|
395
|
+
* @public
|
|
412
396
|
*/
|
|
413
397
|
export const cross = <T extends Vector3Like>(
|
|
414
398
|
a: Vector3Like,
|
|
@@ -423,10 +407,12 @@ export const cross = <T extends Vector3Like>(
|
|
|
423
407
|
const by = b[1];
|
|
424
408
|
const bz = b[2];
|
|
425
409
|
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
410
|
+
return fromValues(
|
|
411
|
+
ay * bz - az * by,
|
|
412
|
+
az * bx - ax * bz,
|
|
413
|
+
ax * by - ay * bx,
|
|
414
|
+
out
|
|
415
|
+
);
|
|
430
416
|
};
|
|
431
417
|
|
|
432
418
|
/**
|
|
@@ -436,7 +422,8 @@ export const cross = <T extends Vector3Like>(
|
|
|
436
422
|
* @param t - The interpolation amount (in `[0,1]`).
|
|
437
423
|
* @param out - The vector to store the result in.
|
|
438
424
|
* @returns The interpolated vector.
|
|
439
|
-
* @see
|
|
425
|
+
* @see {@link https://en.wikipedia.org/wiki/Linear_interpolation | Linear interpolation}
|
|
426
|
+
* @public
|
|
440
427
|
*/
|
|
441
428
|
export const lerp = <T extends Vector3Like>(
|
|
442
429
|
a: Vector3Like,
|
|
@@ -448,10 +435,42 @@ export const lerp = <T extends Vector3Like>(
|
|
|
448
435
|
const ay = a[1];
|
|
449
436
|
const az = a[2];
|
|
450
437
|
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
438
|
+
return fromValues(
|
|
439
|
+
ax + t * (b[0] - ax),
|
|
440
|
+
ay + t * (b[1] - ay),
|
|
441
|
+
az + t * (b[2] - az),
|
|
442
|
+
out
|
|
443
|
+
);
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Perform a spherical linear interpolation between two vectors.
|
|
448
|
+
* @param a - The first vector.
|
|
449
|
+
* @param b - The second vector.
|
|
450
|
+
* @param t - The interpolation amount (in `[0,1]`).
|
|
451
|
+
* @param out - The vector to store the result in.
|
|
452
|
+
* @returns The interpolated vector.
|
|
453
|
+
* @see {@link https://en.wikipedia.org/wiki/Slerp | Slerp}
|
|
454
|
+
* @public
|
|
455
|
+
*/
|
|
456
|
+
export const slerp = <T extends Vector3Like>(
|
|
457
|
+
a: Vector3Like,
|
|
458
|
+
b: Vector3Like,
|
|
459
|
+
t: number,
|
|
460
|
+
out: T
|
|
461
|
+
): T => {
|
|
462
|
+
const angle = Math.acos(Math.min(Math.max(dot(a, b), -1), 1));
|
|
463
|
+
const s = Math.sin(angle);
|
|
464
|
+
|
|
465
|
+
const ratioA = Math.sin((1 - t) * angle) / s;
|
|
466
|
+
const ratioB = Math.sin(t * angle) / s;
|
|
467
|
+
|
|
468
|
+
return fromValues(
|
|
469
|
+
ratioA * a[0] + ratioB * b[0],
|
|
470
|
+
ratioA * a[1] + ratioB * b[1],
|
|
471
|
+
ratioA * a[2] + ratioB * b[2],
|
|
472
|
+
out
|
|
473
|
+
);
|
|
455
474
|
};
|
|
456
475
|
|
|
457
476
|
/**
|
|
@@ -459,16 +478,19 @@ export const lerp = <T extends Vector3Like>(
|
|
|
459
478
|
* @param magnitude - The magnitude.
|
|
460
479
|
* @param out - The vector to store the result in.
|
|
461
480
|
* @returns This vector.
|
|
481
|
+
* @public
|
|
462
482
|
*/
|
|
463
483
|
export const random = <T extends Vector3Like>(magnitude: number, out: T): T => {
|
|
464
484
|
const r = Math.random() * 2 * Math.PI;
|
|
465
485
|
const z = Math.random() * 2 - 1;
|
|
466
486
|
const zScale = Math.sqrt(1 - z * z) * magnitude;
|
|
467
487
|
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
488
|
+
return fromValues(
|
|
489
|
+
Math.cos(r) * zScale,
|
|
490
|
+
Math.sin(r) * zScale,
|
|
491
|
+
z * magnitude,
|
|
492
|
+
out
|
|
493
|
+
);
|
|
472
494
|
};
|
|
473
495
|
|
|
474
496
|
/**
|
|
@@ -477,7 +499,8 @@ export const random = <T extends Vector3Like>(magnitude: number, out: T): T => {
|
|
|
477
499
|
* @param matrix - The matrix (multiplicand).
|
|
478
500
|
* @param out - The vector to store the result in.
|
|
479
501
|
* @returns The transformed vector.
|
|
480
|
-
* @see
|
|
502
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
503
|
+
* @public
|
|
481
504
|
*/
|
|
482
505
|
export const transformMatrix3 = <T extends Vector3Like>(
|
|
483
506
|
vector: Vector3Like,
|
|
@@ -488,10 +511,12 @@ export const transformMatrix3 = <T extends Vector3Like>(
|
|
|
488
511
|
const y = vector[1];
|
|
489
512
|
const z = vector[2];
|
|
490
513
|
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
514
|
+
return fromValues(
|
|
515
|
+
x * matrix[0] + y * matrix[3] + z * matrix[6],
|
|
516
|
+
x * matrix[1] + y * matrix[4] + z * matrix[7],
|
|
517
|
+
x * matrix[2] + y * matrix[5] + z * matrix[8],
|
|
518
|
+
out
|
|
519
|
+
);
|
|
495
520
|
};
|
|
496
521
|
|
|
497
522
|
/**
|
|
@@ -500,7 +525,8 @@ export const transformMatrix3 = <T extends Vector3Like>(
|
|
|
500
525
|
* @param matrix - The matrix (multiplicand).
|
|
501
526
|
* @param out - The vector to store the result in.
|
|
502
527
|
* @returns The transformed vector.
|
|
503
|
-
* @see
|
|
528
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
529
|
+
* @public
|
|
504
530
|
*/
|
|
505
531
|
export const transformMatrix4 = <T extends Vector3Like>(
|
|
506
532
|
vector: Vector3Like,
|
|
@@ -510,13 +536,14 @@ export const transformMatrix4 = <T extends Vector3Like>(
|
|
|
510
536
|
const x = vector[0];
|
|
511
537
|
const y = vector[1];
|
|
512
538
|
const z = vector[2];
|
|
513
|
-
|
|
514
539
|
const w = matrix[3] * x + matrix[7] * y + matrix[11] * z + matrix[15] || 1;
|
|
515
540
|
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
541
|
+
return fromValues(
|
|
542
|
+
(matrix[0] * x + matrix[4] * y + matrix[8] * z + matrix[12]) / w,
|
|
543
|
+
(matrix[1] * x + matrix[5] * y + matrix[9] * z + matrix[13]) / w,
|
|
544
|
+
(matrix[2] * x + matrix[6] * y + matrix[10] * z + matrix[14]) / w,
|
|
545
|
+
out
|
|
546
|
+
);
|
|
520
547
|
};
|
|
521
548
|
|
|
522
549
|
/**
|
|
@@ -526,6 +553,7 @@ export const transformMatrix4 = <T extends Vector3Like>(
|
|
|
526
553
|
* @param r - The angle of rotation in radians.
|
|
527
554
|
* @param out - The vector to store the result in.
|
|
528
555
|
* @returns The rotated vector.
|
|
556
|
+
* @public
|
|
529
557
|
*/
|
|
530
558
|
export const rotateX = <T extends Vector3Like>(
|
|
531
559
|
vector: Vector3Like,
|
|
@@ -533,17 +561,16 @@ export const rotateX = <T extends Vector3Like>(
|
|
|
533
561
|
r: number,
|
|
534
562
|
out: T
|
|
535
563
|
): T => {
|
|
536
|
-
const
|
|
537
|
-
const
|
|
538
|
-
const p2 = vector[2] - origin[2];
|
|
564
|
+
const o1 = origin[1];
|
|
565
|
+
const o2 = origin[2];
|
|
539
566
|
|
|
540
|
-
const
|
|
541
|
-
const
|
|
567
|
+
const p1 = vector[1] - o1;
|
|
568
|
+
const p2 = vector[2] - o2;
|
|
542
569
|
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
return out;
|
|
570
|
+
const c = Math.cos(r);
|
|
571
|
+
const s = Math.sin(r);
|
|
572
|
+
|
|
573
|
+
return fromValues(vector[0], p1 * c - p2 * s + o1, p1 * s + p2 * c + o2, out);
|
|
547
574
|
};
|
|
548
575
|
|
|
549
576
|
/**
|
|
@@ -553,6 +580,7 @@ export const rotateX = <T extends Vector3Like>(
|
|
|
553
580
|
* @param r - The angle of rotation in radians.
|
|
554
581
|
* @param out - The vector to store the result in.
|
|
555
582
|
* @returns The rotated vector.
|
|
583
|
+
* @public
|
|
556
584
|
*/
|
|
557
585
|
export const rotateY = <T extends Vector3Like>(
|
|
558
586
|
vector: Vector3Like,
|
|
@@ -560,17 +588,16 @@ export const rotateY = <T extends Vector3Like>(
|
|
|
560
588
|
r: number,
|
|
561
589
|
out: T
|
|
562
590
|
): T => {
|
|
563
|
-
const
|
|
564
|
-
const
|
|
565
|
-
const p2 = vector[2] - origin[2];
|
|
591
|
+
const o0 = origin[0];
|
|
592
|
+
const o2 = origin[2];
|
|
566
593
|
|
|
567
|
-
const
|
|
568
|
-
const
|
|
594
|
+
const p0 = vector[0] - o0;
|
|
595
|
+
const p2 = vector[2] - o2;
|
|
569
596
|
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
return out;
|
|
597
|
+
const c = Math.cos(r);
|
|
598
|
+
const s = Math.sin(r);
|
|
599
|
+
|
|
600
|
+
return fromValues(p2 * s + p0 * c + o0, vector[1], p2 * c - p0 * s + o2, out);
|
|
574
601
|
};
|
|
575
602
|
|
|
576
603
|
/**
|
|
@@ -580,6 +607,7 @@ export const rotateY = <T extends Vector3Like>(
|
|
|
580
607
|
* @param r - The angle of rotation in radians.
|
|
581
608
|
* @param out - The vector to store the result in.
|
|
582
609
|
* @returns The rotated vector.
|
|
610
|
+
* @public
|
|
583
611
|
*/
|
|
584
612
|
export const rotateZ = <T extends Vector3Like>(
|
|
585
613
|
vector: Vector3Like,
|
|
@@ -587,17 +615,16 @@ export const rotateZ = <T extends Vector3Like>(
|
|
|
587
615
|
r: number,
|
|
588
616
|
out: T
|
|
589
617
|
): T => {
|
|
590
|
-
const
|
|
591
|
-
const
|
|
592
|
-
const p2 = vector[2] - origin[2];
|
|
618
|
+
const o0 = origin[0];
|
|
619
|
+
const o1 = origin[1];
|
|
593
620
|
|
|
594
|
-
const
|
|
595
|
-
const
|
|
621
|
+
const p0 = vector[0] - o0;
|
|
622
|
+
const p1 = vector[1] - o1;
|
|
596
623
|
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
return out;
|
|
624
|
+
const c = Math.cos(r);
|
|
625
|
+
const s = Math.sin(r);
|
|
626
|
+
|
|
627
|
+
return fromValues(p0 * c - p1 * s + o0, p0 * s + p1 * c + o1, vector[2], out);
|
|
601
628
|
};
|
|
602
629
|
|
|
603
630
|
/**
|
|
@@ -605,6 +632,7 @@ export const rotateZ = <T extends Vector3Like>(
|
|
|
605
632
|
* @param a - The first vector.
|
|
606
633
|
* @param b - The second vector.
|
|
607
634
|
* @returns The angular distance from the first vector to the second.
|
|
635
|
+
* @public
|
|
608
636
|
*/
|
|
609
637
|
export const angle = (a: Vector3Like, b: Vector3Like): number => {
|
|
610
638
|
const ax = a[0];
|
|
@@ -615,27 +643,22 @@ export const angle = (a: Vector3Like, b: Vector3Like): number => {
|
|
|
615
643
|
const by = b[1];
|
|
616
644
|
const bz = b[2];
|
|
617
645
|
|
|
618
|
-
|
|
619
|
-
const
|
|
620
|
-
|
|
621
|
-
|
|
646
|
+
// `Math.hypot` is slower.
|
|
647
|
+
const mag =
|
|
648
|
+
Math.sqrt(ax * ax + ay * ay + az * az) *
|
|
649
|
+
Math.sqrt(bx * bx + by * by + bz * bz);
|
|
622
650
|
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
return Math.acos(Math.min(Math.max(cosine, -1), 1));
|
|
651
|
+
return Math.acos(Math.min(Math.max(mag && dot(a, b) / mag, -1), 1));
|
|
626
652
|
};
|
|
627
653
|
|
|
628
654
|
/**
|
|
629
655
|
* Set a vector to the zero vector.
|
|
630
656
|
* @param out - The vector to store the result in.
|
|
631
657
|
* @returns This vector.
|
|
658
|
+
* @public
|
|
632
659
|
*/
|
|
633
|
-
export const zero = <T extends Vector3Like>(out: T): T =>
|
|
634
|
-
|
|
635
|
-
out[1] = 0;
|
|
636
|
-
out[2] = 0;
|
|
637
|
-
return out;
|
|
638
|
-
};
|
|
660
|
+
export const zero = <T extends Vector3Like>(out: T): T =>
|
|
661
|
+
fromValues(0, 0, 0, out);
|
|
639
662
|
|
|
640
663
|
/**
|
|
641
664
|
* Perform a Hermite interpolation with two control points between two vectors.
|
|
@@ -646,7 +669,8 @@ export const zero = <T extends Vector3Like>(out: T): T => {
|
|
|
646
669
|
* @param t - The interpolation amount in the range `[0,1]`.
|
|
647
670
|
* @param out - The vector to store the result in.
|
|
648
671
|
* @returns The interpolated vector.
|
|
649
|
-
* @see
|
|
672
|
+
* @see {@link https://en.wikipedia.org/wiki/Hermite_interpolation | Hermite interpolation}
|
|
673
|
+
* @public
|
|
650
674
|
*/
|
|
651
675
|
export const hermite = <T extends Vector3Like>(
|
|
652
676
|
a: Vector3Like,
|
|
@@ -656,17 +680,19 @@ export const hermite = <T extends Vector3Like>(
|
|
|
656
680
|
t: number,
|
|
657
681
|
out: T
|
|
658
682
|
): T => {
|
|
659
|
-
const
|
|
660
|
-
|
|
661
|
-
const
|
|
662
|
-
const
|
|
663
|
-
const
|
|
664
|
-
const
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
683
|
+
const t2 = t * t;
|
|
684
|
+
|
|
685
|
+
const f1 = t2 * (2 * t - 3) + 1;
|
|
686
|
+
const f2 = t2 * (t - 2) + t;
|
|
687
|
+
const f3 = t2 * (t - 1);
|
|
688
|
+
const f4 = t2 * (3 - 2 * t);
|
|
689
|
+
|
|
690
|
+
return fromValues(
|
|
691
|
+
a[0] * f1 + b[0] * f2 + c[0] * f3 + d[0] * f4,
|
|
692
|
+
a[1] * f1 + b[1] * f2 + c[1] * f3 + d[1] * f4,
|
|
693
|
+
a[2] * f1 + b[2] * f2 + c[2] * f3 + d[2] * f4,
|
|
694
|
+
out
|
|
695
|
+
);
|
|
670
696
|
};
|
|
671
697
|
|
|
672
698
|
/**
|
|
@@ -678,7 +704,8 @@ export const hermite = <T extends Vector3Like>(
|
|
|
678
704
|
* @param t - The interpolation amount in the range `[0,1]`.
|
|
679
705
|
* @param out - The vector to store the result in.
|
|
680
706
|
* @returns The interpolated vector.
|
|
681
|
-
* @see
|
|
707
|
+
* @see {@link https://en.wikipedia.org/wiki/B%C3%A9zier_curve | Bézier curve}
|
|
708
|
+
* @public
|
|
682
709
|
*/
|
|
683
710
|
export const bezier = <T extends Vector3Like>(
|
|
684
711
|
a: Vector3Like,
|
|
@@ -688,121 +715,63 @@ export const bezier = <T extends Vector3Like>(
|
|
|
688
715
|
t: number,
|
|
689
716
|
out: T
|
|
690
717
|
): T => {
|
|
691
|
-
const
|
|
692
|
-
const
|
|
693
|
-
const
|
|
694
|
-
|
|
695
|
-
const
|
|
696
|
-
const
|
|
697
|
-
const
|
|
698
|
-
const
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
718
|
+
const invf = 1 - t;
|
|
719
|
+
const invf2 = invf * invf;
|
|
720
|
+
const sqf = t * t;
|
|
721
|
+
|
|
722
|
+
const f1 = invf2 * invf;
|
|
723
|
+
const f2 = 3 * t * invf2;
|
|
724
|
+
const f3 = 3 * sqf * invf;
|
|
725
|
+
const f4 = sqf * t;
|
|
726
|
+
|
|
727
|
+
return fromValues(
|
|
728
|
+
a[0] * f1 + b[0] * f2 + c[0] * f3 + d[0] * f4,
|
|
729
|
+
a[1] * f1 + b[1] * f2 + c[1] * f3 + d[1] * f4,
|
|
730
|
+
a[2] * f1 + b[2] * f2 + c[2] * f3 + d[2] * f4,
|
|
731
|
+
out
|
|
732
|
+
);
|
|
704
733
|
};
|
|
705
734
|
|
|
706
735
|
/**
|
|
707
|
-
* Transform a vector by a quaternion.
|
|
736
|
+
* Transform a vector by a unit quaternion.
|
|
708
737
|
* @param vector - The vector.
|
|
709
|
-
* @param quaternion - The quaternion.
|
|
738
|
+
* @param quaternion - The unit quaternion.
|
|
710
739
|
* @param out - The vector to store the result in.
|
|
711
740
|
* @returns The transformed vector.
|
|
712
|
-
* @see
|
|
741
|
+
* @see {@link https://en.wikipedia.org/wiki/Quaternion | Quaternion}
|
|
742
|
+
* @see {@link https://raw.org/proof/vector-rotation-using-quaternions/ | Fast Vector Rotation using Quaternions}
|
|
743
|
+
* @public
|
|
713
744
|
*/
|
|
714
745
|
export const transformQuaternion = <T extends Vector3Like>(
|
|
715
746
|
vector: Vector3Like,
|
|
716
747
|
quaternion: QuaternionLike,
|
|
717
748
|
out: T
|
|
718
749
|
): T => {
|
|
719
|
-
const qx = quaternion[0];
|
|
720
|
-
const qy = quaternion[1];
|
|
721
|
-
const qz = quaternion[2];
|
|
722
|
-
const qw = quaternion[3];
|
|
723
|
-
|
|
724
750
|
const x = vector[0];
|
|
725
751
|
const y = vector[1];
|
|
726
752
|
const z = vector[2];
|
|
727
753
|
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
let uuvx = qy * uvz - qz * uvy;
|
|
733
|
-
let uuvy = qz * uvx - qx * uvz;
|
|
734
|
-
let uuvz = qx * uvy - qy * uvx;
|
|
735
|
-
|
|
736
|
-
const w2 = qw * 2;
|
|
737
|
-
uvx *= w2;
|
|
738
|
-
uvy *= w2;
|
|
739
|
-
uvz *= w2;
|
|
740
|
-
|
|
741
|
-
uuvx *= 2;
|
|
742
|
-
uuvy *= 2;
|
|
743
|
-
uuvz *= 2;
|
|
744
|
-
|
|
745
|
-
out[0] = x + uvx + uuvx;
|
|
746
|
-
out[1] = y + uvy + uuvy;
|
|
747
|
-
out[2] = z + uvz + uuvz;
|
|
748
|
-
return out;
|
|
749
|
-
};
|
|
750
|
-
|
|
751
|
-
// The unit three-dimensional vector that represents the X-axis.
|
|
752
|
-
const xAxis = fromValues(1, 0, 0, createVector3Like());
|
|
753
|
-
|
|
754
|
-
// The unit three-dimensional vector that represents the Y-axis.
|
|
755
|
-
const yAxis = fromValues(0, 1, 0, createVector3Like());
|
|
756
|
-
|
|
757
|
-
// Used to store intermediary values for some functions.
|
|
758
|
-
const intermediary = createVector3Like();
|
|
759
|
-
|
|
760
|
-
/**
|
|
761
|
-
* Create a quaternion that represents the shortest rotation from one unit vector to another.
|
|
762
|
-
* @param a - The first vector.
|
|
763
|
-
* @param b - The second vector.
|
|
764
|
-
* @param out - The quaternion to store the result in.
|
|
765
|
-
* @returns The quaternion.
|
|
766
|
-
*/
|
|
767
|
-
export const rotationTo = <T extends QuaternionLike>(
|
|
768
|
-
a: Vector3Like,
|
|
769
|
-
b: Vector3Like,
|
|
770
|
-
out: T
|
|
771
|
-
): T => {
|
|
772
|
-
const dp = dot(a, b);
|
|
773
|
-
|
|
774
|
-
if (dp < epsilon - 1) {
|
|
775
|
-
cross(xAxis, a, intermediary);
|
|
776
|
-
if (getMagnitude(intermediary) < epsilon) {
|
|
777
|
-
cross(yAxis, a, intermediary);
|
|
778
|
-
}
|
|
779
|
-
normalize(intermediary, intermediary);
|
|
780
|
-
out[0] = intermediary[0];
|
|
781
|
-
out[1] = intermediary[1];
|
|
782
|
-
out[2] = intermediary[2];
|
|
783
|
-
out[3] = 0;
|
|
784
|
-
return out;
|
|
785
|
-
}
|
|
754
|
+
const qx = quaternion[0];
|
|
755
|
+
const qy = quaternion[1];
|
|
756
|
+
const qz = quaternion[2];
|
|
757
|
+
const qw = quaternion[3];
|
|
786
758
|
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
out[2] = 0;
|
|
791
|
-
out[3] = 1;
|
|
792
|
-
return out;
|
|
793
|
-
}
|
|
759
|
+
const tx = (qy * z - qz * y) * 2;
|
|
760
|
+
const ty = (qz * x - qx * z) * 2;
|
|
761
|
+
const tz = (qx * y - qy * x) * 2;
|
|
794
762
|
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
763
|
+
return fromValues(
|
|
764
|
+
x + qw * tx + qy * tz - qz * ty,
|
|
765
|
+
y + qw * ty + qz * tx - qx * tz,
|
|
766
|
+
z + qw * tz + qx * ty - qy * tx,
|
|
767
|
+
out
|
|
768
|
+
);
|
|
801
769
|
};
|
|
802
770
|
|
|
803
771
|
/**
|
|
804
772
|
* A quantity with magnitude and direction in three dimensions.
|
|
805
|
-
* @see
|
|
773
|
+
* @see {@link https://en.wikipedia.org/wiki/Euclidean_vector | Euclidean vector}
|
|
774
|
+
* @public
|
|
806
775
|
*/
|
|
807
776
|
export default class Vector3
|
|
808
777
|
extends Float32Array
|
|
@@ -827,7 +796,7 @@ export default class Vector3
|
|
|
827
796
|
|
|
828
797
|
/**
|
|
829
798
|
* Create a three-dimensional zero vector.
|
|
830
|
-
* @see
|
|
799
|
+
* @see {@link https://en.wikipedia.org/wiki/Euclidean_vector | Euclidean vector}
|
|
831
800
|
*/
|
|
832
801
|
public constructor() {
|
|
833
802
|
super(3);
|
|
@@ -932,6 +901,17 @@ export default class Vector3
|
|
|
932
901
|
return subtract(this, vector, out);
|
|
933
902
|
}
|
|
934
903
|
|
|
904
|
+
/**
|
|
905
|
+
* Absolutize the components of this vector.
|
|
906
|
+
* @param out - The vector to store the result in.
|
|
907
|
+
* @returns The absolutized vector.
|
|
908
|
+
*/
|
|
909
|
+
public abs<T extends Vector3Like = Vector3>(
|
|
910
|
+
out: T = new Vector3() as Vector3 & T
|
|
911
|
+
): T {
|
|
912
|
+
return abs(this, out);
|
|
913
|
+
}
|
|
914
|
+
|
|
935
915
|
/**
|
|
936
916
|
* Round up the components of this vector.
|
|
937
917
|
* @param out - The vector to store the result in.
|
|
@@ -1023,7 +1003,7 @@ export default class Vector3
|
|
|
1023
1003
|
* Calculate the Euclidean distance between this vector and another.
|
|
1024
1004
|
* @param vector - The other vector.
|
|
1025
1005
|
* @returns The distance.
|
|
1026
|
-
* @see
|
|
1006
|
+
* @see {@link https://en.wikipedia.org/wiki/Euclidean_distance | Euclidean distance}
|
|
1027
1007
|
*/
|
|
1028
1008
|
public distance(vector: Vector3Like): number {
|
|
1029
1009
|
return distance(this, vector);
|
|
@@ -1033,7 +1013,7 @@ export default class Vector3
|
|
|
1033
1013
|
* Calculate the squared Euclidean distance between this vector and another.
|
|
1034
1014
|
* @param vector - The other vector.
|
|
1035
1015
|
* @returns The squared distance.
|
|
1036
|
-
* @see
|
|
1016
|
+
* @see {@link https://en.wikipedia.org/wiki/Euclidean_distance | Euclidean distance}
|
|
1037
1017
|
*/
|
|
1038
1018
|
public squaredDistance(vector: Vector3Like): number {
|
|
1039
1019
|
return squaredDistance(this, vector);
|
|
@@ -1075,7 +1055,7 @@ export default class Vector3
|
|
|
1075
1055
|
* Normalize this vector.
|
|
1076
1056
|
* @param out - The vector to store the result in.
|
|
1077
1057
|
* @returns The normalized vector.
|
|
1078
|
-
* @see
|
|
1058
|
+
* @see {@link https://en.wikipedia.org/wiki/Unit_vector | Unit vector}
|
|
1079
1059
|
*/
|
|
1080
1060
|
public normalize<T extends Vector3Like = Vector3>(
|
|
1081
1061
|
out: T = new Vector3() as Vector3 & T
|
|
@@ -1087,7 +1067,7 @@ export default class Vector3
|
|
|
1087
1067
|
* Calculate the dot product of this and another vector.
|
|
1088
1068
|
* @param vector - The other vector.
|
|
1089
1069
|
* @returns The dot product.
|
|
1090
|
-
* @see
|
|
1070
|
+
* @see {@link https://en.wikipedia.org/wiki/Dot_product | Dot product}
|
|
1091
1071
|
*/
|
|
1092
1072
|
public dot(vector: Vector3Like): number {
|
|
1093
1073
|
return dot(this, vector);
|
|
@@ -1098,7 +1078,7 @@ export default class Vector3
|
|
|
1098
1078
|
* @param vector - The other vector.
|
|
1099
1079
|
* @param out - The vector to store the result in.
|
|
1100
1080
|
* @returns The cross product.
|
|
1101
|
-
* @see
|
|
1081
|
+
* @see {@link https://en.wikipedia.org/wiki/Cross_product | Cross product}
|
|
1102
1082
|
*/
|
|
1103
1083
|
public cross<T extends Vector3Like = Vector3>(
|
|
1104
1084
|
vector: Vector3Like,
|
|
@@ -1113,7 +1093,7 @@ export default class Vector3
|
|
|
1113
1093
|
* @param t - The interpolation amount (in `[0,1]`).
|
|
1114
1094
|
* @param out - The vector to store the result in.
|
|
1115
1095
|
* @returns The interpolated vector.
|
|
1116
|
-
* @see
|
|
1096
|
+
* @see {@link https://en.wikipedia.org/wiki/Linear_interpolation | Linear interpolation}
|
|
1117
1097
|
*/
|
|
1118
1098
|
public lerp<T extends Vector3Like = Vector3>(
|
|
1119
1099
|
vector: Vector3Like,
|
|
@@ -1228,7 +1208,7 @@ export default class Vector3
|
|
|
1228
1208
|
* @param t - The interpolation amount in the range `[0,1]`.
|
|
1229
1209
|
* @param out - The vector to store the result in.
|
|
1230
1210
|
* @returns The interpolated vector.
|
|
1231
|
-
* @see
|
|
1211
|
+
* @see {@link https://en.wikipedia.org/wiki/Hermite_interpolation | Hermite interpolation}
|
|
1232
1212
|
*/
|
|
1233
1213
|
public hermite<T extends Vector3Like = Vector3>(
|
|
1234
1214
|
a: Vector3Like,
|
|
@@ -1248,7 +1228,7 @@ export default class Vector3
|
|
|
1248
1228
|
* @param t - The interpolation amount in the range `[0,1]`.
|
|
1249
1229
|
* @param out - The vector to store the result in.
|
|
1250
1230
|
* @returns The interpolated vector.
|
|
1251
|
-
* @see
|
|
1231
|
+
* @see {@link https://en.wikipedia.org/wiki/B%C3%A9zier_curve | Bézier curve}
|
|
1252
1232
|
*/
|
|
1253
1233
|
public bezier<T extends Vector3Like = Vector3>(
|
|
1254
1234
|
a: Vector3Like,
|
|
@@ -1265,7 +1245,7 @@ export default class Vector3
|
|
|
1265
1245
|
* @param quaternion - The quaternion.
|
|
1266
1246
|
* @param out - The vector to store the result in.
|
|
1267
1247
|
* @returns The transformed vector.
|
|
1268
|
-
* @see
|
|
1248
|
+
* @see {@link https://en.wikipedia.org/wiki/Quaternion | Quaternion}
|
|
1269
1249
|
*/
|
|
1270
1250
|
public transformQuaternion<T extends Vector3Like = Vector3>(
|
|
1271
1251
|
quaternion: QuaternionLike,
|
|
@@ -1273,17 +1253,4 @@ export default class Vector3
|
|
|
1273
1253
|
): T {
|
|
1274
1254
|
return transformQuaternion(this, quaternion, out);
|
|
1275
1255
|
}
|
|
1276
|
-
|
|
1277
|
-
/**
|
|
1278
|
-
* Create a quaternion that represents the shortest rotation from this unit vector to another.
|
|
1279
|
-
* @param vector - The other vector.
|
|
1280
|
-
* @param out - The quaternion to store the result in.
|
|
1281
|
-
* @returns The quaternion.
|
|
1282
|
-
*/
|
|
1283
|
-
public rotationTo<T extends QuaternionLike = Quaternion>(
|
|
1284
|
-
vector: Vector3Like,
|
|
1285
|
-
out: T = new Quaternion() as Quaternion & T
|
|
1286
|
-
): T {
|
|
1287
|
-
return rotationTo(this, vector, out);
|
|
1288
|
-
}
|
|
1289
1256
|
}
|