@lakuna/umath 0.0.3 → 0.0.4

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