@lakuna/umath 1.3.5 → 1.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/dist/algorithms/combinations.d.ts +7 -0
- package/dist/algorithms/combinations.d.ts.map +1 -1
- package/dist/algorithms/combinations.js +7 -0
- package/dist/algorithms/combinations.js.map +1 -1
- package/dist/algorithms/degreesToRadians.d.ts +7 -0
- package/dist/algorithms/degreesToRadians.d.ts.map +1 -1
- package/dist/algorithms/degreesToRadians.js +7 -0
- package/dist/algorithms/degreesToRadians.js.map +1 -1
- package/dist/algorithms/factorial.d.ts +6 -0
- package/dist/algorithms/factorial.d.ts.map +1 -1
- package/dist/algorithms/factorial.js +8 -1
- package/dist/algorithms/factorial.js.map +1 -1
- package/dist/algorithms/fibonacci.d.ts +6 -1
- package/dist/algorithms/fibonacci.d.ts.map +1 -1
- package/dist/algorithms/fibonacci.js +5 -0
- package/dist/algorithms/fibonacci.js.map +1 -1
- package/dist/algorithms/greatestCommonDivisor.d.ts +14 -0
- package/dist/algorithms/greatestCommonDivisor.d.ts.map +1 -1
- package/dist/algorithms/greatestCommonDivisor.js +2 -0
- package/dist/algorithms/greatestCommonDivisor.js.map +1 -1
- package/dist/algorithms/hypergeometricPmf.d.ts +9 -0
- package/dist/algorithms/hypergeometricPmf.d.ts.map +1 -1
- package/dist/algorithms/hypergeometricPmf.js +9 -0
- package/dist/algorithms/hypergeometricPmf.js.map +1 -1
- package/dist/algorithms/isPrime.d.ts +6 -0
- package/dist/algorithms/isPrime.d.ts.map +1 -1
- package/dist/algorithms/isPrime.js +7 -0
- package/dist/algorithms/isPrime.js.map +1 -1
- package/dist/algorithms/permutations.d.ts +7 -0
- package/dist/algorithms/permutations.d.ts.map +1 -1
- package/dist/algorithms/permutations.js +7 -0
- package/dist/algorithms/permutations.js.map +1 -1
- package/dist/algorithms/primeFactorization.d.ts +6 -0
- package/dist/algorithms/primeFactorization.d.ts.map +1 -1
- package/dist/algorithms/primeFactorization.js +6 -0
- package/dist/algorithms/primeFactorization.js.map +1 -1
- package/dist/algorithms/radiansToDegrees.d.ts +7 -0
- package/dist/algorithms/radiansToDegrees.d.ts.map +1 -1
- package/dist/algorithms/radiansToDegrees.js +7 -0
- package/dist/algorithms/radiansToDegrees.js.map +1 -1
- package/dist/algorithms/summation.d.ts +8 -0
- package/dist/algorithms/summation.d.ts.map +1 -1
- package/dist/algorithms/summation.js +9 -0
- package/dist/algorithms/summation.js.map +1 -1
- package/dist/linalg/DualQuaternion.d.ts +410 -27
- package/dist/linalg/DualQuaternion.d.ts.map +1 -1
- package/dist/linalg/DualQuaternion.js +429 -54
- package/dist/linalg/DualQuaternion.js.map +1 -1
- package/dist/linalg/Matrix.d.ts +67 -0
- package/dist/linalg/Matrix.d.ts.map +1 -1
- package/dist/linalg/Matrix2.d.ts +309 -20
- package/dist/linalg/Matrix2.d.ts.map +1 -1
- package/dist/linalg/Matrix2.js +321 -40
- package/dist/linalg/Matrix2.js.map +1 -1
- package/dist/linalg/Matrix3.d.ts +424 -26
- package/dist/linalg/Matrix3.d.ts.map +1 -1
- package/dist/linalg/Matrix3.js +437 -52
- package/dist/linalg/Matrix3.js.map +1 -1
- package/dist/linalg/Matrix4.d.ts +766 -42
- package/dist/linalg/Matrix4.d.ts.map +1 -1
- package/dist/linalg/Matrix4.js +789 -84
- package/dist/linalg/Matrix4.js.map +1 -1
- package/dist/linalg/Quaternion.d.ts +359 -21
- package/dist/linalg/Quaternion.d.ts.map +1 -1
- package/dist/linalg/Quaternion.js +380 -42
- package/dist/linalg/Quaternion.js.map +1 -1
- package/dist/linalg/SlowMatrix.d.ts +70 -0
- package/dist/linalg/SlowMatrix.d.ts.map +1 -1
- package/dist/linalg/SlowMatrix.js +71 -0
- package/dist/linalg/SlowMatrix.js.map +1 -1
- package/dist/linalg/SlowSquareMatrix.d.ts +55 -0
- package/dist/linalg/SlowSquareMatrix.d.ts.map +1 -1
- package/dist/linalg/SlowSquareMatrix.js +58 -0
- package/dist/linalg/SlowSquareMatrix.js.map +1 -1
- package/dist/linalg/SquareMatrix.d.ts +23 -0
- package/dist/linalg/SquareMatrix.d.ts.map +1 -1
- package/dist/linalg/Vector.d.ts +123 -0
- package/dist/linalg/Vector.d.ts.map +1 -1
- package/dist/linalg/Vector2.d.ts +450 -33
- package/dist/linalg/Vector2.d.ts.map +1 -1
- package/dist/linalg/Vector2.js +480 -66
- package/dist/linalg/Vector2.js.map +1 -1
- package/dist/linalg/Vector3.d.ts +542 -38
- package/dist/linalg/Vector3.d.ts.map +1 -1
- package/dist/linalg/Vector3.js +579 -76
- package/dist/linalg/Vector3.js.map +1 -1
- package/dist/linalg/Vector4.d.ts +416 -30
- package/dist/linalg/Vector4.d.ts.map +1 -1
- package/dist/linalg/Vector4.js +441 -60
- package/dist/linalg/Vector4.js.map +1 -1
- package/dist/types/AxisAngle.d.ts +3 -0
- package/dist/types/AxisAngle.d.ts.map +1 -1
- package/dist/types/FieldOfView.d.ts +5 -0
- package/dist/types/FieldOfView.d.ts.map +1 -1
- package/dist/utility/BigNumber.d.ts +36 -0
- package/dist/utility/BigNumber.d.ts.map +1 -1
- package/dist/utility/BigNumber.js +42 -0
- package/dist/utility/BigNumber.js.map +1 -1
- package/dist/utility/MagnitudeError.d.ts +5 -0
- package/dist/utility/MagnitudeError.d.ts.map +1 -1
- package/dist/utility/MagnitudeError.js +5 -0
- package/dist/utility/MagnitudeError.js.map +1 -1
- package/dist/utility/MatrixSizeError.d.ts +5 -0
- package/dist/utility/MatrixSizeError.d.ts.map +1 -1
- package/dist/utility/MatrixSizeError.js +5 -0
- package/dist/utility/MatrixSizeError.js.map +1 -1
- package/dist/utility/PartialMatrixError.d.ts +5 -0
- package/dist/utility/PartialMatrixError.d.ts.map +1 -1
- package/dist/utility/PartialMatrixError.js +5 -0
- package/dist/utility/PartialMatrixError.js.map +1 -1
- package/dist/utility/SingularMatrixError.d.ts +8 -0
- package/dist/utility/SingularMatrixError.d.ts.map +1 -1
- package/dist/utility/SingularMatrixError.js +8 -0
- package/dist/utility/SingularMatrixError.js.map +1 -1
- package/dist/utility/epsilon.d.ts +1 -0
- package/dist/utility/epsilon.d.ts.map +1 -1
- package/dist/utility/epsilon.js +1 -0
- package/dist/utility/epsilon.js.map +1 -1
- package/package.json +1 -1
- package/src/algorithms/combinations.ts +1 -1
- package/src/algorithms/degreesToRadians.ts +1 -1
- package/src/algorithms/factorial.ts +1 -1
- package/src/algorithms/fibonacci.ts +1 -1
- package/src/algorithms/hypergeometricPmf.ts +1 -1
- package/src/algorithms/isPrime.ts +1 -1
- package/src/algorithms/permutations.ts +1 -1
- package/src/algorithms/primeFactorization.ts +1 -1
- package/src/algorithms/radiansToDegrees.ts +1 -1
- package/src/algorithms/summation.ts +1 -1
- package/src/linalg/DualQuaternion.ts +114 -108
- package/src/linalg/Matrix2.ts +82 -69
- package/src/linalg/Matrix3.ts +106 -93
- package/src/linalg/Matrix4.ts +170 -154
- package/src/linalg/Quaternion.ts +95 -91
- package/src/linalg/SlowMatrix.ts +13 -13
- package/src/linalg/SlowSquareMatrix.ts +11 -11
- package/src/linalg/Vector2.ts +129 -115
- package/src/linalg/Vector3.ts +151 -134
- package/src/linalg/Vector4.ts +120 -103
- package/src/utility/BigNumber.ts +7 -7
package/dist/linalg/Vector3.js
CHANGED
|
@@ -1,16 +1,34 @@
|
|
|
1
1
|
import Quaternion, {} from "./Quaternion.js";
|
|
2
2
|
import epsilon from "../utility/epsilon.js";
|
|
3
3
|
import { normalize as normalizeVector4 } from "./Vector4.js";
|
|
4
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Creates a 3x1 vector-like object.
|
|
6
|
+
* @returns A 3x1 vector-like object.
|
|
7
|
+
*/
|
|
8
|
+
export const createVector3Like = () => {
|
|
5
9
|
return new Float32Array(3);
|
|
6
|
-
}
|
|
7
|
-
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Create a vector with the given values.
|
|
13
|
+
* @param x - The first component.
|
|
14
|
+
* @param y - The second component.
|
|
15
|
+
* @param z - The third component.
|
|
16
|
+
* @param out - The vector to store the result in.
|
|
17
|
+
* @returns A new vector.
|
|
18
|
+
*/
|
|
19
|
+
export const fromValues = (x, y, z, out) => {
|
|
8
20
|
out[0] = x;
|
|
9
21
|
out[1] = y;
|
|
10
22
|
out[2] = z;
|
|
11
23
|
return out;
|
|
12
|
-
}
|
|
13
|
-
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Determine whether or not two vectors are roughly equivalent.
|
|
27
|
+
* @param a - The first vector.
|
|
28
|
+
* @param b - The second vector.
|
|
29
|
+
* @returns Whether or not the vectors are equivalent.
|
|
30
|
+
*/
|
|
31
|
+
export const equals = (a, b) => {
|
|
14
32
|
const a0 = a[0];
|
|
15
33
|
const a1 = a[1];
|
|
16
34
|
const a2 = a[2];
|
|
@@ -20,119 +38,249 @@ export function equals(a, b) {
|
|
|
20
38
|
return (Math.abs(a0 - b0) <= epsilon * Math.max(1, Math.abs(a0), Math.abs(b0)) &&
|
|
21
39
|
Math.abs(a1 - b1) <= epsilon * Math.max(1, Math.abs(a1), Math.abs(b1)) &&
|
|
22
40
|
Math.abs(a2 - b2) <= epsilon * Math.max(1, Math.abs(a2), Math.abs(b2)));
|
|
23
|
-
}
|
|
24
|
-
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Determine whether two vectors are exactly equivalent.
|
|
44
|
+
* @param a - The first vector.
|
|
45
|
+
* @param b - The second vector.
|
|
46
|
+
* @returns Whether the vectors are equivalent.
|
|
47
|
+
*/
|
|
48
|
+
export const exactEquals = (a, b) => {
|
|
25
49
|
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2];
|
|
26
|
-
}
|
|
27
|
-
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Add two vectors.
|
|
53
|
+
* @param a - The augend.
|
|
54
|
+
* @param b - The addend.
|
|
55
|
+
* @param out - The vector to store the result in.
|
|
56
|
+
* @returns The sum.
|
|
57
|
+
*/
|
|
58
|
+
export const add = (a, b, out) => {
|
|
28
59
|
out[0] = a[0] + b[0];
|
|
29
60
|
out[1] = a[1] + b[1];
|
|
30
61
|
out[2] = a[2] + b[2];
|
|
31
62
|
return out;
|
|
32
|
-
}
|
|
33
|
-
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Copy the values from one vector to another.
|
|
66
|
+
* @param vector - The vector to copy.
|
|
67
|
+
* @param out - The vector to store the result in.
|
|
68
|
+
* @returns The copy.
|
|
69
|
+
*/
|
|
70
|
+
export const copy = (vector, out) => {
|
|
34
71
|
out[0] = vector[0];
|
|
35
72
|
out[1] = vector[1];
|
|
36
73
|
out[2] = vector[2];
|
|
37
74
|
return out;
|
|
38
|
-
}
|
|
39
|
-
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Multiply two vectors.
|
|
78
|
+
* @param a - The multiplier.
|
|
79
|
+
* @param b - The multiplicand.
|
|
80
|
+
* @param out - The vector to store the result in.
|
|
81
|
+
* @returns The product.
|
|
82
|
+
*/
|
|
83
|
+
export const multiply = (a, b, out) => {
|
|
40
84
|
out[0] = a[0] * b[0];
|
|
41
85
|
out[1] = a[1] * b[1];
|
|
42
86
|
out[2] = a[2] * b[2];
|
|
43
87
|
return out;
|
|
44
|
-
}
|
|
45
|
-
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Divide two vectors.
|
|
91
|
+
* @param a - The dividend.
|
|
92
|
+
* @param b - The divisor.
|
|
93
|
+
* @param out - The vector to store the result in.
|
|
94
|
+
* @returns The quotient.
|
|
95
|
+
*/
|
|
96
|
+
export const divide = (a, b, out) => {
|
|
46
97
|
out[0] = a[0] / b[0];
|
|
47
98
|
out[1] = a[1] / b[1];
|
|
48
99
|
out[2] = a[2] / b[2];
|
|
49
100
|
return out;
|
|
50
|
-
}
|
|
51
|
-
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Subtract two vectors.
|
|
104
|
+
* @param a - The minuend.
|
|
105
|
+
* @param b - The subtrahend.
|
|
106
|
+
* @param out - The vector to store the result in.
|
|
107
|
+
* @returns The difference.
|
|
108
|
+
*/
|
|
109
|
+
export const subtract = (a, b, out) => {
|
|
52
110
|
out[0] = a[0] - b[0];
|
|
53
111
|
out[1] = a[1] - b[1];
|
|
54
112
|
out[2] = a[2] - b[2];
|
|
55
113
|
return out;
|
|
56
|
-
}
|
|
57
|
-
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* Round up the components of a vector.
|
|
117
|
+
* @param vector - The vector.
|
|
118
|
+
* @param out - The vector to store the result in.
|
|
119
|
+
* @returns The rounded vector.
|
|
120
|
+
*/
|
|
121
|
+
export const ceil = (vector, out) => {
|
|
58
122
|
out[0] = Math.ceil(vector[0]);
|
|
59
123
|
out[1] = Math.ceil(vector[1]);
|
|
60
124
|
out[2] = Math.ceil(vector[2]);
|
|
61
125
|
return out;
|
|
62
|
-
}
|
|
63
|
-
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* Round down the components of a vector.
|
|
129
|
+
* @param vector - The vector.
|
|
130
|
+
* @param out - The vector to store the result in.
|
|
131
|
+
* @returns The rounded vector.
|
|
132
|
+
*/
|
|
133
|
+
export const floor = (vector, out) => {
|
|
64
134
|
out[0] = Math.floor(vector[0]);
|
|
65
135
|
out[1] = Math.floor(vector[1]);
|
|
66
136
|
out[2] = Math.floor(vector[2]);
|
|
67
137
|
return out;
|
|
68
|
-
}
|
|
69
|
-
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* Round the components of a vector.
|
|
141
|
+
* @param vector - The vector.
|
|
142
|
+
* @param out - The vector to store the result in.
|
|
143
|
+
* @returns The rounded vector.
|
|
144
|
+
*/
|
|
145
|
+
export const round = (vector, out) => {
|
|
70
146
|
out[0] = Math.round(vector[0]);
|
|
71
147
|
out[1] = Math.round(vector[1]);
|
|
72
148
|
out[2] = Math.round(vector[2]);
|
|
73
149
|
return out;
|
|
74
|
-
}
|
|
75
|
-
|
|
150
|
+
};
|
|
151
|
+
/**
|
|
152
|
+
* Return the minimum of two vectors.
|
|
153
|
+
* @param a - The first vector.
|
|
154
|
+
* @param b - The second vector.
|
|
155
|
+
* @param out - The vector to store the result in.
|
|
156
|
+
* @returns The minimum.
|
|
157
|
+
*/
|
|
158
|
+
export const min = (a, b, out) => {
|
|
76
159
|
out[0] = Math.min(a[0], b[0]);
|
|
77
160
|
out[1] = Math.min(a[1], b[1]);
|
|
78
161
|
out[2] = Math.min(a[2], b[2]);
|
|
79
162
|
return out;
|
|
80
|
-
}
|
|
81
|
-
|
|
163
|
+
};
|
|
164
|
+
/**
|
|
165
|
+
* Return the maximum of two vectors.
|
|
166
|
+
* @param a - The first vector.
|
|
167
|
+
* @param b - The second vector.
|
|
168
|
+
* @param out - The vector to store the result in.
|
|
169
|
+
* @returns The maximum.
|
|
170
|
+
*/
|
|
171
|
+
export const max = (a, b, out) => {
|
|
82
172
|
out[0] = Math.max(a[0], b[0]);
|
|
83
173
|
out[1] = Math.max(a[1], b[1]);
|
|
84
174
|
out[2] = Math.max(a[2], b[2]);
|
|
85
175
|
return out;
|
|
86
|
-
}
|
|
87
|
-
|
|
176
|
+
};
|
|
177
|
+
/**
|
|
178
|
+
* Scale a vector by a scalar.
|
|
179
|
+
* @param vector - The multiplier.
|
|
180
|
+
* @param scalar - The multiplicand.
|
|
181
|
+
* @param out - The vector to store the result in.
|
|
182
|
+
* @returns The product.
|
|
183
|
+
*/
|
|
184
|
+
export const scale = (vector, scalar, out) => {
|
|
88
185
|
out[0] = vector[0] * scalar;
|
|
89
186
|
out[1] = vector[1] * scalar;
|
|
90
187
|
out[2] = vector[2] * scalar;
|
|
91
188
|
return out;
|
|
92
|
-
}
|
|
93
|
-
|
|
189
|
+
};
|
|
190
|
+
/**
|
|
191
|
+
* Add two vectors after scaling the second by a scalar.
|
|
192
|
+
* @param a - The augend.
|
|
193
|
+
* @param b - The addend.
|
|
194
|
+
* @param scalar - The multiplicand.
|
|
195
|
+
* @param out - The vector to store the result in.
|
|
196
|
+
* @returns The sum.
|
|
197
|
+
*/
|
|
198
|
+
export const scaleAndAdd = (a, b, scalar, out) => {
|
|
94
199
|
out[0] = a[0] + b[0] * scalar;
|
|
95
200
|
out[1] = a[1] + b[1] * scalar;
|
|
96
201
|
out[2] = a[2] + b[2] * scalar;
|
|
97
202
|
return out;
|
|
98
|
-
}
|
|
99
|
-
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* Calculate the Euclidean distance between two vectors.
|
|
206
|
+
* @param a - The first vector.
|
|
207
|
+
* @param b - The second vector.
|
|
208
|
+
* @returns The distance.
|
|
209
|
+
* @see [Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance)
|
|
210
|
+
*/
|
|
211
|
+
export const distance = (a, b) => {
|
|
100
212
|
const x = b[0] - a[0];
|
|
101
213
|
const y = b[1] - a[1];
|
|
102
214
|
const z = b[2] - a[2];
|
|
103
215
|
return Math.hypot(x, y, z);
|
|
104
|
-
}
|
|
105
|
-
|
|
216
|
+
};
|
|
217
|
+
/**
|
|
218
|
+
* Calculate the squared Euclidean distance between this vector and another.
|
|
219
|
+
* @param a - The first vector.
|
|
220
|
+
* @param b - The second vector.
|
|
221
|
+
* @returns The squared distance.
|
|
222
|
+
* @see [Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance)
|
|
223
|
+
*/
|
|
224
|
+
export const squaredDistance = (a, b) => {
|
|
106
225
|
const x = b[0] - a[0];
|
|
107
226
|
const y = b[1] - a[1];
|
|
108
227
|
const z = b[2] - a[2];
|
|
109
228
|
return x * x + y * y + z * z;
|
|
110
|
-
}
|
|
111
|
-
|
|
229
|
+
};
|
|
230
|
+
/**
|
|
231
|
+
* Calculate the magnitude (length) of a vector.
|
|
232
|
+
* @param vector - The vector.
|
|
233
|
+
* @returns The magnitude.
|
|
234
|
+
*/
|
|
235
|
+
export const getMagnitude = (vector) => {
|
|
112
236
|
const x = vector[0];
|
|
113
237
|
const y = vector[1];
|
|
114
238
|
const z = vector[2];
|
|
115
239
|
return Math.hypot(x, y, z);
|
|
116
|
-
}
|
|
117
|
-
|
|
240
|
+
};
|
|
241
|
+
/**
|
|
242
|
+
* Calculate the squared magnitude (length) of a vector.
|
|
243
|
+
* @param vector - The vector.
|
|
244
|
+
* @returns The squared magnitude.
|
|
245
|
+
*/
|
|
246
|
+
export const getSquaredMagnitude = (vector) => {
|
|
118
247
|
const x = vector[0];
|
|
119
248
|
const y = vector[1];
|
|
120
249
|
const z = vector[2];
|
|
121
250
|
return x * x + y * y + z * z;
|
|
122
|
-
}
|
|
123
|
-
|
|
251
|
+
};
|
|
252
|
+
/**
|
|
253
|
+
* Negate a vector.
|
|
254
|
+
* @param vector - The vector.
|
|
255
|
+
* @param out - The vector to store the result in.
|
|
256
|
+
* @returns The negated vector.
|
|
257
|
+
*/
|
|
258
|
+
export const negate = (vector, out) => {
|
|
124
259
|
out[0] = -vector[0];
|
|
125
260
|
out[1] = -vector[1];
|
|
126
261
|
out[2] = -vector[2];
|
|
127
262
|
return out;
|
|
128
|
-
}
|
|
129
|
-
|
|
263
|
+
};
|
|
264
|
+
/**
|
|
265
|
+
* Calculate the multiplicative inverse of the components of a vector.
|
|
266
|
+
* @param vector - The vector.
|
|
267
|
+
* @param out - The vector to store the result in.
|
|
268
|
+
* @returns The inverted vector.
|
|
269
|
+
*/
|
|
270
|
+
export const invert = (vector, out) => {
|
|
130
271
|
out[0] = 1 / vector[0];
|
|
131
272
|
out[1] = 1 / vector[1];
|
|
132
273
|
out[2] = 1 / vector[2];
|
|
133
274
|
return out;
|
|
134
|
-
}
|
|
135
|
-
|
|
275
|
+
};
|
|
276
|
+
/**
|
|
277
|
+
* Normalize a vector.
|
|
278
|
+
* @param vector - The vector.
|
|
279
|
+
* @param out - The vector to store the result in.
|
|
280
|
+
* @returns The normalized vector.
|
|
281
|
+
* @see [Unit vector](https://en.wikipedia.org/wiki/Unit_vector)
|
|
282
|
+
*/
|
|
283
|
+
export const normalize = (vector, out) => {
|
|
136
284
|
const x = vector[0];
|
|
137
285
|
const y = vector[1];
|
|
138
286
|
const z = vector[2];
|
|
@@ -144,11 +292,26 @@ export function normalize(vector, out) {
|
|
|
144
292
|
out[1] = y * len;
|
|
145
293
|
out[2] = z * len;
|
|
146
294
|
return out;
|
|
147
|
-
}
|
|
148
|
-
|
|
295
|
+
};
|
|
296
|
+
/**
|
|
297
|
+
* Calculate the dot product of two vectors.
|
|
298
|
+
* @param a - The multiplier.
|
|
299
|
+
* @param b - The multiplicand.
|
|
300
|
+
* @returns The dot product.
|
|
301
|
+
* @see [Dot product](https://en.wikipedia.org/wiki/Dot_product)
|
|
302
|
+
*/
|
|
303
|
+
export const dot = (a, b) => {
|
|
149
304
|
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
150
|
-
}
|
|
151
|
-
|
|
305
|
+
};
|
|
306
|
+
/**
|
|
307
|
+
* Calculate the cross product of two vectors.
|
|
308
|
+
* @param a - The multiplier.
|
|
309
|
+
* @param b - The mutliplicand.
|
|
310
|
+
* @param out - The vector to store the result in.
|
|
311
|
+
* @returns The cross product.
|
|
312
|
+
* @see [Cross product](https://en.wikipedia.org/wiki/Cross_product)
|
|
313
|
+
*/
|
|
314
|
+
export const cross = (a, b, out) => {
|
|
152
315
|
const ax = a[0];
|
|
153
316
|
const ay = a[1];
|
|
154
317
|
const az = a[2];
|
|
@@ -159,8 +322,17 @@ export function cross(a, b, out) {
|
|
|
159
322
|
out[1] = az * bx - ax * bz;
|
|
160
323
|
out[2] = ax * by - ay * bx;
|
|
161
324
|
return out;
|
|
162
|
-
}
|
|
163
|
-
|
|
325
|
+
};
|
|
326
|
+
/**
|
|
327
|
+
* Perform a linear interpolation between two vectors.
|
|
328
|
+
* @param a - The first vector.
|
|
329
|
+
* @param b - The second vector.
|
|
330
|
+
* @param t - The interpolation amount (in `[0,1]`).
|
|
331
|
+
* @param out - The vector to store the result in.
|
|
332
|
+
* @returns The interpolated vector.
|
|
333
|
+
* @see [Linear interpolation](https://en.wikipedia.org/wiki/Linear_interpolation)
|
|
334
|
+
*/
|
|
335
|
+
export const lerp = (a, b, t, out) => {
|
|
164
336
|
const ax = a[0];
|
|
165
337
|
const ay = a[1];
|
|
166
338
|
const az = a[2];
|
|
@@ -168,8 +340,14 @@ export function lerp(a, b, t, out) {
|
|
|
168
340
|
out[1] = ay + t * (b[1] - ay);
|
|
169
341
|
out[2] = az + t * (b[2] - az);
|
|
170
342
|
return out;
|
|
171
|
-
}
|
|
172
|
-
|
|
343
|
+
};
|
|
344
|
+
/**
|
|
345
|
+
* Set a vector to a random value with the given magnitude.
|
|
346
|
+
* @param magnitude - The magnitude.
|
|
347
|
+
* @param out - The vector to store the result in.
|
|
348
|
+
* @returns This vector.
|
|
349
|
+
*/
|
|
350
|
+
export const random = (magnitude, out) => {
|
|
173
351
|
const r = Math.random() * 2 * Math.PI;
|
|
174
352
|
const z = Math.random() * 2 - 1;
|
|
175
353
|
const zScale = Math.sqrt(1 - z * z) * magnitude;
|
|
@@ -177,8 +355,16 @@ export function random(magnitude, out) {
|
|
|
177
355
|
out[1] = Math.sin(r) * zScale;
|
|
178
356
|
out[2] = z * magnitude;
|
|
179
357
|
return out;
|
|
180
|
-
}
|
|
181
|
-
|
|
358
|
+
};
|
|
359
|
+
/**
|
|
360
|
+
* Transform a vector by a three-by-three matrix.
|
|
361
|
+
* @param vector - The vector (multiplier).
|
|
362
|
+
* @param matrix - The matrix (multiplicand).
|
|
363
|
+
* @param out - The vector to store the result in.
|
|
364
|
+
* @returns The transformed vector.
|
|
365
|
+
* @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
|
|
366
|
+
*/
|
|
367
|
+
export const transformMatrix3 = (vector, matrix, out) => {
|
|
182
368
|
const x = vector[0];
|
|
183
369
|
const y = vector[1];
|
|
184
370
|
const z = vector[2];
|
|
@@ -186,8 +372,16 @@ export function transformMatrix3(vector, matrix, out) {
|
|
|
186
372
|
out[1] = x * matrix[1] + y * matrix[4] + z * matrix[7];
|
|
187
373
|
out[2] = x * matrix[2] + y * matrix[5] + z * matrix[8];
|
|
188
374
|
return out;
|
|
189
|
-
}
|
|
190
|
-
|
|
375
|
+
};
|
|
376
|
+
/**
|
|
377
|
+
* Transform a vector by a four-by-four matrix.
|
|
378
|
+
* @param vector - The vector (multiplier).
|
|
379
|
+
* @param matrix - The matrix (multiplicand).
|
|
380
|
+
* @param out - The vector to store the result in.
|
|
381
|
+
* @returns The transformed vector.
|
|
382
|
+
* @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
|
|
383
|
+
*/
|
|
384
|
+
export const transformMatrix4 = (vector, matrix, out) => {
|
|
191
385
|
const x = vector[0];
|
|
192
386
|
const y = vector[1];
|
|
193
387
|
const z = vector[2];
|
|
@@ -196,8 +390,16 @@ export function transformMatrix4(vector, matrix, out) {
|
|
|
196
390
|
out[1] = (matrix[1] * x + matrix[5] * y + matrix[9] * z + matrix[13]) / w;
|
|
197
391
|
out[2] = (matrix[2] * x + matrix[6] * y + matrix[10] * z + matrix[14]) / w;
|
|
198
392
|
return out;
|
|
199
|
-
}
|
|
200
|
-
|
|
393
|
+
};
|
|
394
|
+
/**
|
|
395
|
+
* Rotate a vector around the X-axis.
|
|
396
|
+
* @param vector - The vector.
|
|
397
|
+
* @param origin - The origin of the rotation.
|
|
398
|
+
* @param r - The angle of rotation in radians.
|
|
399
|
+
* @param out - The vector to store the result in.
|
|
400
|
+
* @returns The rotated vector.
|
|
401
|
+
*/
|
|
402
|
+
export const rotateX = (vector, origin, r, out) => {
|
|
201
403
|
const p0 = vector[0] - origin[0];
|
|
202
404
|
const p1 = vector[1] - origin[1];
|
|
203
405
|
const p2 = vector[2] - origin[2];
|
|
@@ -207,8 +409,16 @@ export function rotateX(vector, origin, r, out) {
|
|
|
207
409
|
out[1] = r1 + origin[1];
|
|
208
410
|
out[2] = r2 + origin[2];
|
|
209
411
|
return out;
|
|
210
|
-
}
|
|
211
|
-
|
|
412
|
+
};
|
|
413
|
+
/**
|
|
414
|
+
* Rotate a vector around the Y-axis.
|
|
415
|
+
* @param vector - The vector.
|
|
416
|
+
* @param origin - The origin of the rotation.
|
|
417
|
+
* @param r - The angle of rotation in radians.
|
|
418
|
+
* @param out - The vector to store the result in.
|
|
419
|
+
* @returns The rotated vector.
|
|
420
|
+
*/
|
|
421
|
+
export const rotateY = (vector, origin, r, out) => {
|
|
212
422
|
const p0 = vector[0] - origin[0];
|
|
213
423
|
const p1 = vector[1] - origin[1];
|
|
214
424
|
const p2 = vector[2] - origin[2];
|
|
@@ -218,8 +428,16 @@ export function rotateY(vector, origin, r, out) {
|
|
|
218
428
|
out[1] = p1 + origin[1];
|
|
219
429
|
out[2] = r2 + origin[2];
|
|
220
430
|
return out;
|
|
221
|
-
}
|
|
222
|
-
|
|
431
|
+
};
|
|
432
|
+
/**
|
|
433
|
+
* Rotate a vector around the Z-axis.
|
|
434
|
+
* @param vector - The vector.
|
|
435
|
+
* @param origin - The origin of the rotation.
|
|
436
|
+
* @param r - The angle of rotation in radians.
|
|
437
|
+
* @param out - The vector to store the result in.
|
|
438
|
+
* @returns The rotated vector.
|
|
439
|
+
*/
|
|
440
|
+
export const rotateZ = (vector, origin, r, out) => {
|
|
223
441
|
const p0 = vector[0] - origin[0];
|
|
224
442
|
const p1 = vector[1] - origin[1];
|
|
225
443
|
const p2 = vector[2] - origin[2];
|
|
@@ -229,8 +447,14 @@ export function rotateZ(vector, origin, r, out) {
|
|
|
229
447
|
out[1] = r1 + origin[1];
|
|
230
448
|
out[2] = p2 + origin[2];
|
|
231
449
|
return out;
|
|
232
|
-
}
|
|
233
|
-
|
|
450
|
+
};
|
|
451
|
+
/**
|
|
452
|
+
* Get the angle between two vectors in radians.
|
|
453
|
+
* @param a - The first vector.
|
|
454
|
+
* @param b - The second vector.
|
|
455
|
+
* @returns The angular distance from the first vector to the second.
|
|
456
|
+
*/
|
|
457
|
+
export const angle = (a, b) => {
|
|
234
458
|
const ax = a[0];
|
|
235
459
|
const ay = a[1];
|
|
236
460
|
const az = a[2];
|
|
@@ -242,14 +466,30 @@ export function angle(a, b) {
|
|
|
242
466
|
const mag = mag1 * mag2;
|
|
243
467
|
const cosine = mag && dot(a, b) / mag;
|
|
244
468
|
return Math.acos(Math.min(Math.max(cosine, -1), 1));
|
|
245
|
-
}
|
|
246
|
-
|
|
469
|
+
};
|
|
470
|
+
/**
|
|
471
|
+
* Set a vector to the zero vector.
|
|
472
|
+
* @param out - The vector to store the result in.
|
|
473
|
+
* @returns This vector.
|
|
474
|
+
*/
|
|
475
|
+
export const zero = (out) => {
|
|
247
476
|
out[0] = 0;
|
|
248
477
|
out[1] = 0;
|
|
249
478
|
out[2] = 0;
|
|
250
479
|
return out;
|
|
251
|
-
}
|
|
252
|
-
|
|
480
|
+
};
|
|
481
|
+
/**
|
|
482
|
+
* Perform a Hermite interpolation with two control points between two vectors.
|
|
483
|
+
* @param a - The first vector.
|
|
484
|
+
* @param b - The first control point.
|
|
485
|
+
* @param c - The second control point.
|
|
486
|
+
* @param d - The second vector.
|
|
487
|
+
* @param t - The interpolation amount in the range `[0,1]`.
|
|
488
|
+
* @param out - The vector to store the result in.
|
|
489
|
+
* @returns The interpolated vector.
|
|
490
|
+
* @see [Hermite interpolation](https://en.wikipedia.org/wiki/Hermite_interpolation)
|
|
491
|
+
*/
|
|
492
|
+
export const hermite = (a, b, c, d, t, out) => {
|
|
253
493
|
const factorTimes2 = t * t;
|
|
254
494
|
const factor1 = factorTimes2 * (2 * t - 3) + 1;
|
|
255
495
|
const factor2 = factorTimes2 * (t - 2) + t;
|
|
@@ -259,8 +499,19 @@ export function hermite(a, b, c, d, t, out) {
|
|
|
259
499
|
out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4;
|
|
260
500
|
out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4;
|
|
261
501
|
return out;
|
|
262
|
-
}
|
|
263
|
-
|
|
502
|
+
};
|
|
503
|
+
/**
|
|
504
|
+
* Perform a Bézier interpolation with two control points between two vectors.
|
|
505
|
+
* @param a - The first vector.
|
|
506
|
+
* @param b - The first control point.
|
|
507
|
+
* @param c - The second control point.
|
|
508
|
+
* @param d - The second vector.
|
|
509
|
+
* @param t - The interpolation amount in the range `[0,1]`.
|
|
510
|
+
* @param out - The vector to store the result in.
|
|
511
|
+
* @returns The interpolated vector.
|
|
512
|
+
* @see [Bézier curve](https://en.wikipedia.org/wiki/B%C3%A9zier_curve)
|
|
513
|
+
*/
|
|
514
|
+
export const bezier = (a, b, c, d, t, out) => {
|
|
264
515
|
const inverseFactor = 1 - t;
|
|
265
516
|
const inverseFactorTimesTwo = inverseFactor * inverseFactor;
|
|
266
517
|
const factorSq = t * t;
|
|
@@ -272,8 +523,16 @@ export function bezier(a, b, c, d, t, out) {
|
|
|
272
523
|
out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4;
|
|
273
524
|
out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4;
|
|
274
525
|
return out;
|
|
275
|
-
}
|
|
276
|
-
|
|
526
|
+
};
|
|
527
|
+
/**
|
|
528
|
+
* Transform a vector by a quaternion.
|
|
529
|
+
* @param vector - The vector.
|
|
530
|
+
* @param quaternion - The quaternion.
|
|
531
|
+
* @param out - The vector to store the result in.
|
|
532
|
+
* @returns The transformed vector.
|
|
533
|
+
* @see [Quaternion](https://en.wikipedia.org/wiki/Quaternion)
|
|
534
|
+
*/
|
|
535
|
+
export const transformQuaternion = (vector, quaternion, out) => {
|
|
277
536
|
const qx = quaternion[0];
|
|
278
537
|
const qy = quaternion[1];
|
|
279
538
|
const qz = quaternion[2];
|
|
@@ -298,11 +557,21 @@ export function transformQuaternion(vector, quaternion, out) {
|
|
|
298
557
|
out[1] = y + uvy + uuvy;
|
|
299
558
|
out[2] = z + uvz + uuvz;
|
|
300
559
|
return out;
|
|
301
|
-
}
|
|
560
|
+
};
|
|
561
|
+
// The unit three-dimensional vector that represents the X-axis.
|
|
302
562
|
const xAxis = fromValues(1, 0, 0, createVector3Like());
|
|
563
|
+
// The unit three-dimensional vector that represents the Y-axis.
|
|
303
564
|
const yAxis = fromValues(0, 1, 0, createVector3Like());
|
|
565
|
+
// Used to store intermediary values for some functions.
|
|
304
566
|
const intermediary = createVector3Like();
|
|
305
|
-
|
|
567
|
+
/**
|
|
568
|
+
* Create a quaternion that represents the shortest rotation from one unit vector to another.
|
|
569
|
+
* @param a - The first vector.
|
|
570
|
+
* @param b - The second vector.
|
|
571
|
+
* @param out - The quaternion to store the result in.
|
|
572
|
+
* @returns The quaternion.
|
|
573
|
+
*/
|
|
574
|
+
export const rotationTo = (a, b, out) => {
|
|
306
575
|
const dp = dot(a, b);
|
|
307
576
|
if (dp < epsilon - 1) {
|
|
308
577
|
cross(xAxis, a, intermediary);
|
|
@@ -329,125 +598,359 @@ export function rotationTo(a, b, out) {
|
|
|
329
598
|
out[2] = intermediary[2];
|
|
330
599
|
out[3] = 1 + dp;
|
|
331
600
|
return normalizeVector4(out, out);
|
|
332
|
-
}
|
|
601
|
+
};
|
|
602
|
+
/**
|
|
603
|
+
* A quantity with magnitude and direction in three dimensions.
|
|
604
|
+
* @see [Euclidean vector](https://en.wikipedia.org/wiki/Euclidean_vector)
|
|
605
|
+
*/
|
|
333
606
|
export default class Vector3 extends Float32Array {
|
|
607
|
+
/**
|
|
608
|
+
* Create a vector with the given values.
|
|
609
|
+
* @param x - The first component.
|
|
610
|
+
* @param y - The second component.
|
|
611
|
+
* @param z - The third component.
|
|
612
|
+
* @param out - The vector to store the result in.
|
|
613
|
+
* @returns A new vector.
|
|
614
|
+
*/
|
|
334
615
|
static fromValues(x, y, z, out = new Vector3()) {
|
|
335
616
|
return fromValues(x, y, z, out);
|
|
336
617
|
}
|
|
618
|
+
/**
|
|
619
|
+
* Create a three-dimensional zero vector.
|
|
620
|
+
* @see [Euclidean vector](https://en.wikipedia.org/wiki/Euclidean_vector)
|
|
621
|
+
*/
|
|
337
622
|
constructor() {
|
|
338
623
|
super(3);
|
|
339
624
|
}
|
|
625
|
+
/** The first component of this vector. */
|
|
340
626
|
0;
|
|
627
|
+
/** The second component of this vector. */
|
|
341
628
|
1;
|
|
629
|
+
/** The third component of this vector. */
|
|
342
630
|
2;
|
|
631
|
+
/**
|
|
632
|
+
* Determine whether or not this vector is roughly equivalent to another.
|
|
633
|
+
* @param vector - The other vector.
|
|
634
|
+
* @returns Whether or not the vectors are equivalent.
|
|
635
|
+
*/
|
|
343
636
|
equals(vector) {
|
|
344
637
|
return equals(this, vector);
|
|
345
638
|
}
|
|
639
|
+
/**
|
|
640
|
+
* Determine whether or not this vector is exactly equivalent to another.
|
|
641
|
+
* @param vector - The other vector.
|
|
642
|
+
* @returns Whether or not the vectors are equivalent.
|
|
643
|
+
*/
|
|
346
644
|
exactEquals(vector) {
|
|
347
645
|
return exactEquals(this, vector);
|
|
348
646
|
}
|
|
647
|
+
/**
|
|
648
|
+
* Add two vectors of the same size.
|
|
649
|
+
* @param vector - The other vector.
|
|
650
|
+
* @param out - The vector to store the result in.
|
|
651
|
+
* @returns The sum of the vectors.
|
|
652
|
+
*/
|
|
349
653
|
add(vector, out = new Vector3()) {
|
|
350
654
|
return add(this, vector, out);
|
|
351
655
|
}
|
|
656
|
+
/**
|
|
657
|
+
* Copy the values from this vector to another one.
|
|
658
|
+
* @param out - The vector to store the result in.
|
|
659
|
+
* @returns The copy.
|
|
660
|
+
*/
|
|
352
661
|
clone(out = new Vector3()) {
|
|
353
662
|
return copy(this, out);
|
|
354
663
|
}
|
|
664
|
+
/**
|
|
665
|
+
* Copy the values of another vector into this one.
|
|
666
|
+
* @param vector - The vector to copy.
|
|
667
|
+
* @returns This vector.
|
|
668
|
+
*/
|
|
355
669
|
copy(vector) {
|
|
356
670
|
return copy(vector, this);
|
|
357
671
|
}
|
|
672
|
+
/**
|
|
673
|
+
* Multiply this vector by another.
|
|
674
|
+
* @param vector - The other vector.
|
|
675
|
+
* @param out - The vector to store the result in.
|
|
676
|
+
* @returns The product of the vectors.
|
|
677
|
+
*/
|
|
358
678
|
multiply(vector, out = new Vector3()) {
|
|
359
679
|
return multiply(this, vector, out);
|
|
360
680
|
}
|
|
681
|
+
/**
|
|
682
|
+
* Divide this vector by another.
|
|
683
|
+
* @param vector - The other vector.
|
|
684
|
+
* @param out - The vector to store the result in.
|
|
685
|
+
* @returns The quotient of the vectors.
|
|
686
|
+
*/
|
|
361
687
|
divide(vector, out = new Vector3()) {
|
|
362
688
|
return divide(this, vector, out);
|
|
363
689
|
}
|
|
690
|
+
/**
|
|
691
|
+
* Subtract another vector from this one.
|
|
692
|
+
* @param vector - The other vector.
|
|
693
|
+
* @param out - The vector to store the result in.
|
|
694
|
+
* @returns The difference between the vectors.
|
|
695
|
+
*/
|
|
364
696
|
subtract(vector, out = new Vector3()) {
|
|
365
697
|
return subtract(this, vector, out);
|
|
366
698
|
}
|
|
699
|
+
/**
|
|
700
|
+
* Round up the components of this vector.
|
|
701
|
+
* @param out - The vector to store the result in.
|
|
702
|
+
* @returns The rounded vector.
|
|
703
|
+
*/
|
|
367
704
|
ceil(out = new Vector3()) {
|
|
368
705
|
return ceil(this, out);
|
|
369
706
|
}
|
|
707
|
+
/**
|
|
708
|
+
* Round down the components of this vector.
|
|
709
|
+
* @param out - The vector to store the result in.
|
|
710
|
+
* @returns The rounded vector.
|
|
711
|
+
*/
|
|
370
712
|
floor(out = new Vector3()) {
|
|
371
713
|
return floor(this, out);
|
|
372
714
|
}
|
|
715
|
+
/**
|
|
716
|
+
* Round the components of this vector.
|
|
717
|
+
* @param out - The vector to store the result in.
|
|
718
|
+
* @returns The rounded vector.
|
|
719
|
+
*/
|
|
373
720
|
round(out = new Vector3()) {
|
|
374
721
|
return round(this, out);
|
|
375
722
|
}
|
|
723
|
+
/**
|
|
724
|
+
* Return the minimum of this and another vector.
|
|
725
|
+
* @param vector - The other vector.
|
|
726
|
+
* @param out - The vector to store the result in.
|
|
727
|
+
* @returns The minimum.
|
|
728
|
+
*/
|
|
376
729
|
min(vector, out = new Vector3()) {
|
|
377
730
|
return min(this, vector, out);
|
|
378
731
|
}
|
|
732
|
+
/**
|
|
733
|
+
* Return the maximum of this and another vector.
|
|
734
|
+
* @param vector - The other vector.
|
|
735
|
+
* @param out - The vector to store the result in.
|
|
736
|
+
* @returns The maximum.
|
|
737
|
+
*/
|
|
379
738
|
max(vector, out = new Vector3()) {
|
|
380
739
|
return max(this, vector, out);
|
|
381
740
|
}
|
|
741
|
+
/**
|
|
742
|
+
* Scale this vector by a scalar.
|
|
743
|
+
* @param scalar - The scalar.
|
|
744
|
+
* @param out - The vector to store the result in.
|
|
745
|
+
* @returns The scaled vector.
|
|
746
|
+
*/
|
|
382
747
|
scale(scalar, out = new Vector3()) {
|
|
383
748
|
return scale(this, scalar, out);
|
|
384
749
|
}
|
|
750
|
+
/**
|
|
751
|
+
* Add another vector to this one after scaling the other by a scalar.
|
|
752
|
+
* @param vector - The other vector.
|
|
753
|
+
* @param scalar - The scalar.
|
|
754
|
+
* @param out - The vector to store the result in.
|
|
755
|
+
* @returns The sum.
|
|
756
|
+
*/
|
|
385
757
|
scaleAndAdd(vector, scalar, out = new Vector3()) {
|
|
386
758
|
return scaleAndAdd(this, vector, scalar, out);
|
|
387
759
|
}
|
|
760
|
+
/**
|
|
761
|
+
* Calculate the Euclidean distance between this vector and another.
|
|
762
|
+
* @param vector - The other vector.
|
|
763
|
+
* @returns The distance.
|
|
764
|
+
* @see [Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance)
|
|
765
|
+
*/
|
|
388
766
|
distance(vector) {
|
|
389
767
|
return distance(this, vector);
|
|
390
768
|
}
|
|
769
|
+
/**
|
|
770
|
+
* Calculate the squared Euclidean distance between this vector and another.
|
|
771
|
+
* @param vector - The other vector.
|
|
772
|
+
* @returns The squared distance.
|
|
773
|
+
* @see [Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance)
|
|
774
|
+
*/
|
|
391
775
|
squaredDistance(vector) {
|
|
392
776
|
return squaredDistance(this, vector);
|
|
393
777
|
}
|
|
778
|
+
/** Get the magnitude (length) of this vector. */
|
|
394
779
|
get magnitude() {
|
|
395
780
|
return getMagnitude(this);
|
|
396
781
|
}
|
|
782
|
+
/** Get the squared magnitude (length) of this vector. */
|
|
397
783
|
get squaredMagnitude() {
|
|
398
784
|
return getSquaredMagnitude(this);
|
|
399
785
|
}
|
|
786
|
+
/**
|
|
787
|
+
* Negate this vector.
|
|
788
|
+
* @param out - The vector to store the result in.
|
|
789
|
+
* @returns The negated vector.
|
|
790
|
+
*/
|
|
400
791
|
negate(out = new Vector3()) {
|
|
401
792
|
return negate(this, out);
|
|
402
793
|
}
|
|
794
|
+
/**
|
|
795
|
+
* Calculate the multiplicative inverse of the components of this vector.
|
|
796
|
+
* @param out - The vector to store the result in.
|
|
797
|
+
* @returns The inverted vector.
|
|
798
|
+
*/
|
|
403
799
|
invert(out = new Vector3()) {
|
|
404
800
|
return invert(this, out);
|
|
405
801
|
}
|
|
802
|
+
/**
|
|
803
|
+
* Normalize this vector.
|
|
804
|
+
* @param out - The vector to store the result in.
|
|
805
|
+
* @returns The normalized vector.
|
|
806
|
+
* @see [Unit vector](https://en.wikipedia.org/wiki/Unit_vector)
|
|
807
|
+
*/
|
|
406
808
|
normalize(out = new Vector3()) {
|
|
407
809
|
return normalize(this, out);
|
|
408
810
|
}
|
|
811
|
+
/**
|
|
812
|
+
* Calculate the dot product of this and another vector.
|
|
813
|
+
* @param vector - The other vector.
|
|
814
|
+
* @returns The dot product.
|
|
815
|
+
* @see [Dot product](https://en.wikipedia.org/wiki/Dot_product)
|
|
816
|
+
*/
|
|
409
817
|
dot(vector) {
|
|
410
818
|
return dot(this, vector);
|
|
411
819
|
}
|
|
820
|
+
/**
|
|
821
|
+
* Calculate the cross product of this and another vector.
|
|
822
|
+
* @param vector - The other vector.
|
|
823
|
+
* @param out - The vector to store the result in.
|
|
824
|
+
* @returns The cross product.
|
|
825
|
+
* @see [Cross product](https://en.wikipedia.org/wiki/Cross_product)
|
|
826
|
+
*/
|
|
412
827
|
cross(vector, out = new Vector3()) {
|
|
413
828
|
return cross(this, vector, out);
|
|
414
829
|
}
|
|
830
|
+
/**
|
|
831
|
+
* Perform a linear interpolation between this and another vector.
|
|
832
|
+
* @param vector - The other vector.
|
|
833
|
+
* @param t - The interpolation amount (in `[0,1]`).
|
|
834
|
+
* @param out - The vector to store the result in.
|
|
835
|
+
* @returns The interpolated vector.
|
|
836
|
+
* @see [Linear interpolation](https://en.wikipedia.org/wiki/Linear_interpolation)
|
|
837
|
+
*/
|
|
415
838
|
lerp(vector, t, out = new Vector3()) {
|
|
416
839
|
return lerp(this, vector, t, out);
|
|
417
840
|
}
|
|
841
|
+
/**
|
|
842
|
+
* Set this vector to a random value with the given magnitude.
|
|
843
|
+
* @param magnitude - The magnitude.
|
|
844
|
+
* @returns This vector.
|
|
845
|
+
*/
|
|
418
846
|
random(magnitude = 1) {
|
|
419
847
|
return random(magnitude, this);
|
|
420
848
|
}
|
|
849
|
+
/**
|
|
850
|
+
* Transform this vector by a three-by-three matrix.
|
|
851
|
+
* @param matrix - The matrix.
|
|
852
|
+
* @param out - The vector to store the result in.
|
|
853
|
+
* @returns The transformed vector.
|
|
854
|
+
*/
|
|
421
855
|
transformMatrix3(matrix, out = new Vector3()) {
|
|
422
856
|
return transformMatrix3(this, matrix, out);
|
|
423
857
|
}
|
|
858
|
+
/**
|
|
859
|
+
* Transform this vector by a four-by-four matrix.
|
|
860
|
+
* @param matrix - The matrix.
|
|
861
|
+
* @param out - The vector to store the result in.
|
|
862
|
+
* @returns The transformed vector.
|
|
863
|
+
*/
|
|
424
864
|
transformMatrix4(matrix, out = new Vector3()) {
|
|
425
865
|
return transformMatrix4(this, matrix, out);
|
|
426
866
|
}
|
|
867
|
+
/**
|
|
868
|
+
* Rotate this vector around the X-axis.
|
|
869
|
+
* @param origin - The origin of the rotation.
|
|
870
|
+
* @param r - The angle of rotation in radians.
|
|
871
|
+
* @param out - The vector to store the result in.
|
|
872
|
+
* @returns The rotated vector.
|
|
873
|
+
*/
|
|
427
874
|
rotateX(origin, r, out = new Vector3()) {
|
|
428
875
|
return rotateX(this, origin, r, out);
|
|
429
876
|
}
|
|
877
|
+
/**
|
|
878
|
+
* Rotate this vector around the Y-axis.
|
|
879
|
+
* @param origin - The origin of the rotation.
|
|
880
|
+
* @param r - The angle of rotation in radians.
|
|
881
|
+
* @param out - The vector to store the result in.
|
|
882
|
+
* @returns The rotated vector.
|
|
883
|
+
*/
|
|
430
884
|
rotateY(origin, r, out = new Vector3()) {
|
|
431
885
|
return rotateY(this, origin, r, out);
|
|
432
886
|
}
|
|
887
|
+
/**
|
|
888
|
+
* Rotate this vector around the Z-axis.
|
|
889
|
+
* @param origin - The origin of the rotation.
|
|
890
|
+
* @param r - The angle of rotation in radians.
|
|
891
|
+
* @param out - The vector to store the result in.
|
|
892
|
+
* @returns The rotated vector.
|
|
893
|
+
*/
|
|
433
894
|
rotateZ(origin, r, out = new Vector3()) {
|
|
434
895
|
return rotateZ(this, origin, r, out);
|
|
435
896
|
}
|
|
897
|
+
/**
|
|
898
|
+
* Get the angle from this vector to another in radians.
|
|
899
|
+
* @param vector - The other vector.
|
|
900
|
+
* @returns The angular distance from this vector to the other.
|
|
901
|
+
*/
|
|
436
902
|
angle(vector) {
|
|
437
903
|
return angle(this, vector);
|
|
438
904
|
}
|
|
905
|
+
/**
|
|
906
|
+
* Set this to the zero vector.
|
|
907
|
+
* @returns This vector.
|
|
908
|
+
*/
|
|
439
909
|
zero() {
|
|
440
910
|
return zero(this);
|
|
441
911
|
}
|
|
912
|
+
/**
|
|
913
|
+
* Perform a Hermite interpolation with two control points between this vector and another.
|
|
914
|
+
* @param a - The first control point.
|
|
915
|
+
* @param b - The second control point.
|
|
916
|
+
* @param end - The other vector.
|
|
917
|
+
* @param t - The interpolation amount in the range `[0,1]`.
|
|
918
|
+
* @param out - The vector to store the result in.
|
|
919
|
+
* @returns The interpolated vector.
|
|
920
|
+
* @see [Hermite interpolation](https://en.wikipedia.org/wiki/Hermite_interpolation)
|
|
921
|
+
*/
|
|
442
922
|
hermite(a, b, end, t, out = new Vector3()) {
|
|
443
923
|
return hermite(this, a, b, end, t, out);
|
|
444
924
|
}
|
|
925
|
+
/**
|
|
926
|
+
* Perform a Bézier interpolation with two control points between this vector and another.
|
|
927
|
+
* @param a - The first control point.
|
|
928
|
+
* @param b - The second control point.
|
|
929
|
+
* @param end - The other vector.
|
|
930
|
+
* @param t - The interpolation amount in the range `[0,1]`.
|
|
931
|
+
* @param out - The vector to store the result in.
|
|
932
|
+
* @returns The interpolated vector.
|
|
933
|
+
* @see [Bézier curve](https://en.wikipedia.org/wiki/B%C3%A9zier_curve)
|
|
934
|
+
*/
|
|
445
935
|
bezier(a, b, end, t, out = new Vector3()) {
|
|
446
936
|
return bezier(this, a, b, end, t, out);
|
|
447
937
|
}
|
|
938
|
+
/**
|
|
939
|
+
* Transform this vector by a quaternion.
|
|
940
|
+
* @param quaternion - The quaternion.
|
|
941
|
+
* @param out - The vector to store the result in.
|
|
942
|
+
* @returns The transformed vector.
|
|
943
|
+
* @see [Quaternion](https://en.wikipedia.org/wiki/Quaternion)
|
|
944
|
+
*/
|
|
448
945
|
transformQuaternion(quaternion, out = new Vector3()) {
|
|
449
946
|
return transformQuaternion(this, quaternion, out);
|
|
450
947
|
}
|
|
948
|
+
/**
|
|
949
|
+
* Create a quaternion that represents the shortest rotation from this unit vector to another.
|
|
950
|
+
* @param vector - The other vector.
|
|
951
|
+
* @param out - The quaternion to store the result in.
|
|
952
|
+
* @returns The quaternion.
|
|
953
|
+
*/
|
|
451
954
|
rotationTo(vector, out = new Quaternion()) {
|
|
452
955
|
return rotationTo(this, vector, out);
|
|
453
956
|
}
|