@lakuna/umath 1.4.3 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/dist/index.d.ts +6 -0
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +11 -0
  4. package/dist/index.js.map +1 -1
  5. package/dist/linalg/DualQuaternion.d.ts +23 -46
  6. package/dist/linalg/DualQuaternion.d.ts.map +1 -1
  7. package/dist/linalg/DualQuaternion.js +47 -70
  8. package/dist/linalg/DualQuaternion.js.map +1 -1
  9. package/dist/linalg/Matrix2.d.ts +16 -30
  10. package/dist/linalg/Matrix2.d.ts.map +1 -1
  11. package/dist/linalg/Matrix2.js +30 -44
  12. package/dist/linalg/Matrix2.js.map +1 -1
  13. package/dist/linalg/Matrix3.d.ts +21 -42
  14. package/dist/linalg/Matrix3.d.ts.map +1 -1
  15. package/dist/linalg/Matrix3.js +43 -84
  16. package/dist/linalg/Matrix3.js.map +1 -1
  17. package/dist/linalg/Matrix4.d.ts +40 -79
  18. package/dist/linalg/Matrix4.d.ts.map +1 -1
  19. package/dist/linalg/Matrix4.js +86 -171
  20. package/dist/linalg/Matrix4.js.map +1 -1
  21. package/dist/linalg/Quaternion.d.ts +29 -60
  22. package/dist/linalg/Quaternion.d.ts.map +1 -1
  23. package/dist/linalg/Quaternion.js +54 -85
  24. package/dist/linalg/Quaternion.js.map +1 -1
  25. package/dist/linalg/SlowMatrix.d.ts.map +1 -1
  26. package/dist/linalg/SlowMatrix.js +52 -78
  27. package/dist/linalg/SlowMatrix.js.map +1 -1
  28. package/dist/linalg/SlowSquareMatrix.js +1 -1
  29. package/dist/linalg/SlowSquareMatrix.js.map +1 -1
  30. package/dist/linalg/SlowVector.d.ts +165 -0
  31. package/dist/linalg/SlowVector.d.ts.map +1 -0
  32. package/dist/linalg/SlowVector.js +369 -0
  33. package/dist/linalg/SlowVector.js.map +1 -0
  34. package/dist/linalg/Vector.d.ts +20 -15
  35. package/dist/linalg/Vector.d.ts.map +1 -1
  36. package/dist/linalg/Vector2.d.ts +42 -48
  37. package/dist/linalg/Vector2.d.ts.map +1 -1
  38. package/dist/linalg/Vector2.js +74 -82
  39. package/dist/linalg/Vector2.js.map +1 -1
  40. package/dist/linalg/Vector3.d.ts +46 -56
  41. package/dist/linalg/Vector3.d.ts.map +1 -1
  42. package/dist/linalg/Vector3.js +82 -104
  43. package/dist/linalg/Vector3.js.map +1 -1
  44. package/dist/linalg/Vector4.d.ts +40 -44
  45. package/dist/linalg/Vector4.d.ts.map +1 -1
  46. package/dist/linalg/Vector4.js +69 -79
  47. package/dist/linalg/Vector4.js.map +1 -1
  48. package/dist/utility/MatrixSizeError.d.ts +1 -1
  49. package/dist/utility/MatrixSizeError.d.ts.map +1 -1
  50. package/dist/utility/MatrixSizeError.js +1 -1
  51. package/dist/utility/MatrixSizeError.js.map +1 -1
  52. package/dist/utility/VectorSizeError.d.ts +12 -0
  53. package/dist/utility/VectorSizeError.d.ts.map +1 -0
  54. package/dist/utility/VectorSizeError.js +15 -0
  55. package/dist/utility/VectorSizeError.js.map +1 -0
  56. package/dist/utility/createAxisAngleLike.d.ts +10 -0
  57. package/dist/utility/createAxisAngleLike.d.ts.map +1 -0
  58. package/dist/utility/createAxisAngleLike.js +10 -0
  59. package/dist/utility/createAxisAngleLike.js.map +1 -0
  60. package/package.json +8 -10
  61. package/src/index.ts +16 -0
  62. package/src/linalg/DualQuaternion.ts +51 -134
  63. package/src/linalg/Matrix2.ts +32 -83
  64. package/src/linalg/Matrix3.ts +55 -149
  65. package/src/linalg/Matrix4.ts +136 -292
  66. package/src/linalg/Quaternion.ts +62 -153
  67. package/src/linalg/SlowMatrix.ts +82 -81
  68. package/src/linalg/SlowSquareMatrix.ts +1 -1
  69. package/src/linalg/SlowVector.ts +449 -0
  70. package/src/linalg/Vector.ts +21 -16
  71. package/src/linalg/Vector2.ts +84 -147
  72. package/src/linalg/Vector3.ts +102 -188
  73. package/src/linalg/Vector4.ts +88 -137
  74. package/src/utility/MatrixSizeError.ts +1 -1
  75. package/src/utility/VectorSizeError.ts +14 -0
  76. package/src/utility/createAxisAngleLike.ts +13 -0
@@ -0,0 +1,449 @@
1
+ import type Vector from "./Vector.js";
2
+ import type { VectorLike } from "./Vector.js";
3
+ import VectorSizeError from "../utility/VectorSizeError.js";
4
+ import approx from "../algorithms/approx.js";
5
+
6
+ /**
7
+ * A vector with size information.
8
+ * @internal
9
+ */
10
+ interface SizedVectorLike extends VectorLike {
11
+ /** The length (number of elements) of the vector. */
12
+ length: number;
13
+ }
14
+
15
+ /**
16
+ * Determine whether the given `VectorLike` has size information.
17
+ * @internal
18
+ */
19
+ const isSized = (vector: VectorLike): vector is SizedVectorLike =>
20
+ "length" in vector && typeof vector.length === "number";
21
+
22
+ /**
23
+ * A variable-size vector.
24
+ * @see {@link https://en.wikipedia.org/wiki/Euclidean_vector | Euclidean vector}
25
+ * @public
26
+ */
27
+ export default class SlowVector extends Float32Array implements Vector {
28
+ /**
29
+ * Create a variable-size vector from the given values.
30
+ * @param values - The values in the vector.
31
+ */
32
+ public constructor(...values: number[]) {
33
+ super(values);
34
+ }
35
+
36
+ /**
37
+ * Determine whether this vector is roughly equivalent to another.
38
+ * @param vector - The other vector.
39
+ * @returns Whether the vectors are equivalent.
40
+ */
41
+ public equals(vector: VectorLike): boolean {
42
+ if (!isSized(vector) || this.length !== vector.length) {
43
+ throw new VectorSizeError();
44
+ }
45
+
46
+ for (let i = 0; i < this.length; i++) {
47
+ const a = this[i];
48
+ const b = vector[i];
49
+ if (
50
+ typeof a === "undefined" ||
51
+ typeof b === "undefined" ||
52
+ !approx(a, b)
53
+ ) {
54
+ return false;
55
+ }
56
+ }
57
+
58
+ return true;
59
+ }
60
+
61
+ /**
62
+ * Determine whether this vector is exactly equivalent to another.
63
+ * @param vector - The other vector.
64
+ * @returns Whether the vectors are equivalent.
65
+ */
66
+ public exactEquals(vector: VectorLike): boolean {
67
+ if (!isSized(vector) || this.length !== vector.length) {
68
+ throw new VectorSizeError();
69
+ }
70
+
71
+ for (let i = 0; i < this.length; i++) {
72
+ if (this[i] !== vector[i]) {
73
+ return false;
74
+ }
75
+ }
76
+
77
+ return true;
78
+ }
79
+
80
+ /**
81
+ * Add two vectors of the same size.
82
+ * @param vector - The other vector.
83
+ * @returns The sum of the vectors.
84
+ */
85
+ public add(vector: VectorLike): SlowVector {
86
+ if (!isSized(vector) || this.length !== vector.length) {
87
+ throw new VectorSizeError();
88
+ }
89
+
90
+ const out = [];
91
+ for (let i = 0; i < this.length; i++) {
92
+ out[i] = (this[i] ?? 0) + (vector[i] ?? 0);
93
+ }
94
+
95
+ return new SlowVector(...out);
96
+ }
97
+
98
+ /**
99
+ * Create a copy of this vector.
100
+ * @returns A copy of this vector.
101
+ */
102
+ public clone(): SlowVector {
103
+ return new SlowVector(...this);
104
+ }
105
+
106
+ /**
107
+ * Copy the values of another vector into this one.
108
+ * @param vector - The vector to copy.
109
+ * @returns This vector.
110
+ */
111
+ public copy(vector: VectorLike): this {
112
+ if (!isSized(vector) || this.length !== vector.length) {
113
+ throw new VectorSizeError();
114
+ }
115
+
116
+ for (let i = 0; i < vector.length; i++) {
117
+ this[i] = vector[i] ?? 0;
118
+ }
119
+
120
+ return this;
121
+ }
122
+
123
+ /**
124
+ * Multiply this vector by another.
125
+ * @param vector - The other vector.
126
+ * @returns The product of the vectors.
127
+ */
128
+ public multiply(vector: VectorLike): SlowVector {
129
+ if (!isSized(vector) || this.length !== vector.length) {
130
+ throw new VectorSizeError();
131
+ }
132
+
133
+ const out = [];
134
+ for (let i = 0; i < this.length; i++) {
135
+ out[i] = (this[i] ?? 0) * (vector[i] ?? 0);
136
+ }
137
+
138
+ return new SlowVector(...out);
139
+ }
140
+
141
+ /**
142
+ * Divide this vector by another.
143
+ * @param vector - The other vector.
144
+ * @returns The quotient of the vectors.
145
+ */
146
+ public divide(vector: VectorLike): SlowVector {
147
+ if (!isSized(vector) || this.length !== vector.length) {
148
+ throw new VectorSizeError();
149
+ }
150
+
151
+ const out = [];
152
+ for (let i = 0; i < this.length; i++) {
153
+ out[i] = (this[i] ?? 0) / (vector[i] ?? 0);
154
+ }
155
+
156
+ return new SlowVector(...out);
157
+ }
158
+
159
+ /**
160
+ * Subtract another vector from this one.
161
+ * @param vector - The other vector.
162
+ * @returns The difference between the vectors.
163
+ */
164
+ public subtract(vector: VectorLike): SlowVector {
165
+ if (!isSized(vector) || this.length !== vector.length) {
166
+ throw new VectorSizeError();
167
+ }
168
+
169
+ const out = [];
170
+ for (let i = 0; i < this.length; i++) {
171
+ out[i] = (this[i] ?? 0) - (vector[i] ?? 0);
172
+ }
173
+
174
+ return new SlowVector(...out);
175
+ }
176
+
177
+ /**
178
+ * Round up the components of this vector.
179
+ * @returns The rounded vector.
180
+ */
181
+ public ceil(): SlowVector {
182
+ const out = [];
183
+ for (let i = 0; i < this.length; i++) {
184
+ out[i] = Math.ceil(this[i] ?? 0);
185
+ }
186
+
187
+ return new SlowVector(...out);
188
+ }
189
+
190
+ /**
191
+ * Round down the components of this vector.
192
+ * @returns The rounded vector.
193
+ */
194
+ public floor(): SlowVector {
195
+ const out = [];
196
+ for (let i = 0; i < this.length; i++) {
197
+ out[i] = Math.floor(this[i] ?? 0);
198
+ }
199
+
200
+ return new SlowVector(...out);
201
+ }
202
+
203
+ /**
204
+ * Round the components of this vector.
205
+ * @returns The rounded vector.
206
+ */
207
+ public round(): SlowVector {
208
+ const out = [];
209
+ for (let i = 0; i < this.length; i++) {
210
+ out[i] = Math.round(this[i] ?? 0);
211
+ }
212
+
213
+ return new SlowVector(...out);
214
+ }
215
+
216
+ /**
217
+ * Return the minimum of this and another vector.
218
+ * @param vector - The other vector.
219
+ * @returns The minimum.
220
+ */
221
+ public min(vector: VectorLike): SlowVector {
222
+ if (!isSized(vector) || this.length !== vector.length) {
223
+ throw new VectorSizeError();
224
+ }
225
+
226
+ const out = [];
227
+ for (let i = 0; i < this.length; i++) {
228
+ out[i] = Math.min(this[i] ?? 0, vector[i] ?? 0);
229
+ }
230
+
231
+ return new SlowVector(...out);
232
+ }
233
+
234
+ /**
235
+ * Return the maximum of this and another vector.
236
+ * @param vector - The other vector.
237
+ * @returns The maximum.
238
+ */
239
+ public max(vector: VectorLike): SlowVector {
240
+ if (!isSized(vector) || this.length !== vector.length) {
241
+ throw new VectorSizeError();
242
+ }
243
+
244
+ const out = [];
245
+ for (let i = 0; i < this.length; i++) {
246
+ out[i] = Math.max(this[i] ?? 0, vector[i] ?? 0);
247
+ }
248
+
249
+ return new SlowVector(...out);
250
+ }
251
+
252
+ /**
253
+ * Raise each component of this vector to the given power.
254
+ * @param exponent - The exponent (power) to raise each component to.
255
+ * @returns The power (result of the exponentiation).
256
+ */
257
+ public pow(exponent: number): SlowVector {
258
+ const out = [];
259
+ for (let i = 0; i < this.length; i++) {
260
+ out[i] = (this[i] ?? 0) ** exponent;
261
+ }
262
+
263
+ return new SlowVector(...out);
264
+ }
265
+
266
+ /**
267
+ * Scale this vector by a scalar.
268
+ * @param scalar - The scalar.
269
+ * @returns The scaled vector.
270
+ */
271
+ public scale(scalar: number): SlowVector {
272
+ const out = [];
273
+ for (let i = 0; i < this.length; i++) {
274
+ out[i] = (this[i] ?? 0) * scalar;
275
+ }
276
+
277
+ return new SlowVector(...out);
278
+ }
279
+
280
+ /**
281
+ * Add another vector to this one after scaling the other by a scalar.
282
+ * @param vector - The other vector.
283
+ * @param scalar - The scalar.
284
+ * @returns The sum.
285
+ */
286
+ public scaleAndAdd(vector: VectorLike, scalar: number): SlowVector {
287
+ if (!isSized(vector)) {
288
+ throw new VectorSizeError();
289
+ }
290
+
291
+ const out = [];
292
+ for (let i = 0; i < vector.length; i++) {
293
+ out[i] = (vector[i] ?? 0) * scalar;
294
+ }
295
+
296
+ return this.add(out);
297
+ }
298
+
299
+ /**
300
+ * Calculate the Euclidean distance between this vector and another.
301
+ * @param vector - The other vector.
302
+ * @returns The distance.
303
+ */
304
+ public distance(vector: VectorLike): number {
305
+ if (!isSized(vector) || this.length !== vector.length) {
306
+ throw new VectorSizeError();
307
+ }
308
+
309
+ const temp = [];
310
+ for (let i = 0; i < this.length; i++) {
311
+ temp[i] = (vector[i] ?? 0) - (this[i] ?? 0);
312
+ }
313
+
314
+ return Math.hypot(...temp);
315
+ }
316
+
317
+ /**
318
+ * Calculate the squared Euclidean distance between this vector and another.
319
+ * @param vector - The other vector.
320
+ * @returns The squared distance.
321
+ */
322
+ public squaredDistance(vector: VectorLike): number {
323
+ if (!isSized(vector) || this.length !== vector.length) {
324
+ throw new VectorSizeError();
325
+ }
326
+
327
+ let out = 0;
328
+ for (let i = 0; i < this.length; i++) {
329
+ out += ((vector[i] ?? 0) - (this[i] ?? 0)) ** 2;
330
+ }
331
+
332
+ return out;
333
+ }
334
+
335
+ /** Get the magnitude (length) of this vector. */
336
+ public get magnitude(): number {
337
+ return Math.hypot(...this);
338
+ }
339
+
340
+ /** Get the squared magnitude (length) of this vector. */
341
+ public get squaredMagnitude(): number {
342
+ let out = 0;
343
+ for (let i = 0; i < this.length; i++) {
344
+ out += (this[i] ?? 0) ** 2;
345
+ }
346
+
347
+ return out;
348
+ }
349
+
350
+ /**
351
+ * Negate this vector.
352
+ * @returns The negated vector.
353
+ */
354
+ public negate(): SlowVector {
355
+ return this.scale(-1);
356
+ }
357
+
358
+ /**
359
+ * Calculate the multiplicative inverse of the components of this vector.
360
+ * @returns The inverted vector.
361
+ */
362
+ public invert(): SlowVector {
363
+ const out = [];
364
+ for (let i = 0; i < this.length; i++) {
365
+ out[i] = 1 / (this[i] ?? 0);
366
+ }
367
+
368
+ return new SlowVector(...out);
369
+ }
370
+
371
+ /**
372
+ * Normalize this vector.
373
+ * @returns The normalized vector.
374
+ * @see {@link https://en.wikipedia.org/wiki/Unit_vector | Unit vector}
375
+ */
376
+ public normalize(): SlowVector {
377
+ return this.scale(1 / this.magnitude);
378
+ }
379
+
380
+ /**
381
+ * Calculate the dot product of this and another vector.
382
+ * @param vector - The other vector.
383
+ * @returns The dot product.
384
+ * @see {@link https://en.wikipedia.org/wiki/Dot_product | Dot product}
385
+ */
386
+ public dot(vector: VectorLike): number {
387
+ if (!isSized(vector) || this.length !== vector.length) {
388
+ throw new VectorSizeError();
389
+ }
390
+
391
+ let out = 0;
392
+ for (let i = 0; i < this.length; i++) {
393
+ out += (this[i] ?? 0) * (vector[i] ?? 0);
394
+ }
395
+
396
+ return out;
397
+ }
398
+
399
+ /**
400
+ * Perform a linear interpolation between this and another vector.
401
+ * @param vector - The other vector.
402
+ * @param t - The interpolation amount (in `[0,1]`).
403
+ * @returns The interpolated vector.
404
+ */
405
+ public lerp(vector: VectorLike, t: number): SlowVector {
406
+ if (!isSized(vector) || this.length !== vector.length) {
407
+ throw new VectorSizeError();
408
+ }
409
+
410
+ const out = [];
411
+ for (let i = 0; i < this.length; i++) {
412
+ const ti = this[i] ?? 0;
413
+ out[i] = ti + t * ((vector[i] ?? 0) - ti);
414
+ }
415
+
416
+ return new SlowVector(...out);
417
+ }
418
+
419
+ /**
420
+ * Set this vector to a random value with the given magnitude.
421
+ * @param magnitude - The magnitude.
422
+ * @returns This vector.
423
+ */
424
+ public random(magnitude: number): this {
425
+ const temp = [];
426
+ for (let i = 0; i < this.length; i++) {
427
+ temp[i] = Math.random();
428
+ }
429
+
430
+ const randomized = new SlowVector(...temp).normalize().scale(magnitude);
431
+ for (let i = 0; i < this.length; i++) {
432
+ this[i] = randomized[i] ?? 0;
433
+ }
434
+
435
+ return this;
436
+ }
437
+
438
+ /**
439
+ * Set this to the zero vector.
440
+ * @returns This vector.
441
+ */
442
+ public zero(): this {
443
+ for (let i = 0; i < this.length; i++) {
444
+ this[i] = 0;
445
+ }
446
+
447
+ return this;
448
+ }
449
+ }
@@ -1,5 +1,3 @@
1
- import type { Vector4Like } from "./Vector4.js";
2
-
3
1
  /**
4
2
  * An object that could be interpreted as a vector.
5
3
  * @public
@@ -17,21 +15,21 @@ export default interface Vector extends VectorLike {
17
15
  * @param vector - The other vector.
18
16
  * @returns Whether the vectors are equivalent.
19
17
  */
20
- equals(vector: Vector4Like): boolean;
18
+ equals(vector: VectorLike): boolean;
21
19
 
22
20
  /**
23
21
  * Determine whether this vector is exactly equivalent to another.
24
22
  * @param vector - The other vector.
25
23
  * @returns Whether the vectors are equivalent.
26
24
  */
27
- exactEquals(vector: Vector4Like): boolean;
25
+ exactEquals(vector: VectorLike): boolean;
28
26
 
29
27
  /**
30
28
  * Add two vectors of the same size.
31
29
  * @param vector - The other vector.
32
30
  * @returns The sum of the vectors.
33
31
  */
34
- add(vector: Vector4Like): VectorLike;
32
+ add(vector: VectorLike): VectorLike;
35
33
 
36
34
  /**
37
35
  * Create a copy of this vector.
@@ -44,28 +42,28 @@ export default interface Vector extends VectorLike {
44
42
  * @param vector - The vector to copy.
45
43
  * @returns This vector.
46
44
  */
47
- copy(vector: Vector4Like): this;
45
+ copy(vector: VectorLike): this;
48
46
 
49
47
  /**
50
48
  * Multiply this vector by another.
51
49
  * @param vector - The other vector.
52
50
  * @returns The product of the vectors.
53
51
  */
54
- multiply(vector: Vector4Like): VectorLike;
52
+ multiply(vector: VectorLike): VectorLike;
55
53
 
56
54
  /**
57
55
  * Divide this vector by another.
58
56
  * @param vector - The other vector.
59
57
  * @returns The quotient of the vectors.
60
58
  */
61
- divide(vector: Vector4Like): VectorLike;
59
+ divide(vector: VectorLike): VectorLike;
62
60
 
63
61
  /**
64
62
  * Subtract another vector from this one.
65
63
  * @param vector - The other vector.
66
64
  * @returns The difference between the vectors.
67
65
  */
68
- subtract(vector: Vector4Like): VectorLike;
66
+ subtract(vector: VectorLike): VectorLike;
69
67
 
70
68
  /**
71
69
  * Round up the components of this vector.
@@ -90,14 +88,21 @@ export default interface Vector extends VectorLike {
90
88
  * @param vector - The other vector.
91
89
  * @returns The minimum.
92
90
  */
93
- min(vector: Vector4Like): VectorLike;
91
+ min(vector: VectorLike): VectorLike;
94
92
 
95
93
  /**
96
94
  * Return the maximum of this and another vector.
97
95
  * @param vector - The other vector.
98
96
  * @returns The maximum.
99
97
  */
100
- max(vector: Vector4Like): VectorLike;
98
+ max(vector: VectorLike): VectorLike;
99
+
100
+ /**
101
+ * Raise each component of this vector to the given power.
102
+ * @param exponent - The exponent (power) to raise each component to.
103
+ * @returns The power (result of the exponentiation).
104
+ */
105
+ pow(exponent: number): VectorLike;
101
106
 
102
107
  /**
103
108
  * Scale this vector by a scalar.
@@ -112,21 +117,21 @@ export default interface Vector extends VectorLike {
112
117
  * @param scalar - The scalar.
113
118
  * @returns The sum.
114
119
  */
115
- scaleAndAdd(vector: Vector4Like, scalar: number): VectorLike;
120
+ scaleAndAdd(vector: VectorLike, scalar: number): VectorLike;
116
121
 
117
122
  /**
118
123
  * Calculate the Euclidean distance between this vector and another.
119
124
  * @param vector - The other vector.
120
125
  * @returns The distance.
121
126
  */
122
- distance(vector: Vector4Like): number;
127
+ distance(vector: VectorLike): number;
123
128
 
124
129
  /**
125
130
  * Calculate the squared Euclidean distance between this vector and another.
126
131
  * @param vector - The other vector.
127
132
  * @returns The squared distance.
128
133
  */
129
- squaredDistance(vector: Vector4Like): number;
134
+ squaredDistance(vector: VectorLike): number;
130
135
 
131
136
  /** Get the magnitude (length) of this vector. */
132
137
  get magnitude(): number;
@@ -159,7 +164,7 @@ export default interface Vector extends VectorLike {
159
164
  * @returns The dot product.
160
165
  * @see {@link https://en.wikipedia.org/wiki/Dot_product | Dot product}
161
166
  */
162
- dot(vector: Vector4Like): number;
167
+ dot(vector: VectorLike): number;
163
168
 
164
169
  /**
165
170
  * Perform a linear interpolation between this and another vector.
@@ -167,7 +172,7 @@ export default interface Vector extends VectorLike {
167
172
  * @param t - The interpolation amount (in `[0,1]`).
168
173
  * @returns The interpolated vector.
169
174
  */
170
- lerp(vector: Vector4Like, t: number): VectorLike;
175
+ lerp(vector: VectorLike, t: number): VectorLike;
171
176
 
172
177
  /**
173
178
  * Set this vector to a random value with the given magnitude.