@lakuna/umath 1.3.9 → 1.4.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 (162) hide show
  1. package/README.md +1 -1
  2. package/dist/algorithms/approx.d.ts +9 -0
  3. package/dist/algorithms/approx.d.ts.map +1 -0
  4. package/dist/algorithms/approx.js +12 -0
  5. package/dist/algorithms/approx.js.map +1 -0
  6. package/dist/algorithms/approxRelative.d.ts +9 -0
  7. package/dist/algorithms/approxRelative.d.ts.map +1 -0
  8. package/dist/algorithms/approxRelative.js +12 -0
  9. package/dist/algorithms/approxRelative.js.map +1 -0
  10. package/dist/algorithms/combinations.d.ts +2 -1
  11. package/dist/algorithms/combinations.d.ts.map +1 -1
  12. package/dist/algorithms/combinations.js +2 -1
  13. package/dist/algorithms/combinations.js.map +1 -1
  14. package/dist/algorithms/degreesToRadians.d.ts +3 -2
  15. package/dist/algorithms/degreesToRadians.d.ts.map +1 -1
  16. package/dist/algorithms/degreesToRadians.js +5 -3
  17. package/dist/algorithms/degreesToRadians.js.map +1 -1
  18. package/dist/algorithms/factorial.d.ts +2 -1
  19. package/dist/algorithms/factorial.d.ts.map +1 -1
  20. package/dist/algorithms/factorial.js +2 -1
  21. package/dist/algorithms/factorial.js.map +1 -1
  22. package/dist/algorithms/fibonacci.d.ts +2 -1
  23. package/dist/algorithms/fibonacci.d.ts.map +1 -1
  24. package/dist/algorithms/fibonacci.js +6 -2
  25. package/dist/algorithms/fibonacci.js.map +1 -1
  26. package/dist/algorithms/greatestCommonDivisor.d.ts +4 -2
  27. package/dist/algorithms/greatestCommonDivisor.d.ts.map +1 -1
  28. package/dist/algorithms/greatestCommonDivisor.js +5 -2
  29. package/dist/algorithms/greatestCommonDivisor.js.map +1 -1
  30. package/dist/algorithms/hypergeometricPmf.d.ts +3 -2
  31. package/dist/algorithms/hypergeometricPmf.d.ts.map +1 -1
  32. package/dist/algorithms/hypergeometricPmf.js +3 -2
  33. package/dist/algorithms/hypergeometricPmf.js.map +1 -1
  34. package/dist/algorithms/isPrime.d.ts +2 -1
  35. package/dist/algorithms/isPrime.d.ts.map +1 -1
  36. package/dist/algorithms/isPrime.js +2 -1
  37. package/dist/algorithms/isPrime.js.map +1 -1
  38. package/dist/algorithms/permutations.d.ts +2 -1
  39. package/dist/algorithms/permutations.d.ts.map +1 -1
  40. package/dist/algorithms/permutations.js +2 -1
  41. package/dist/algorithms/permutations.js.map +1 -1
  42. package/dist/algorithms/primeFactorization.d.ts +2 -1
  43. package/dist/algorithms/primeFactorization.d.ts.map +1 -1
  44. package/dist/algorithms/primeFactorization.js +2 -1
  45. package/dist/algorithms/primeFactorization.js.map +1 -1
  46. package/dist/algorithms/radiansToDegrees.d.ts +3 -2
  47. package/dist/algorithms/radiansToDegrees.d.ts.map +1 -1
  48. package/dist/algorithms/radiansToDegrees.js +5 -3
  49. package/dist/algorithms/radiansToDegrees.js.map +1 -1
  50. package/dist/algorithms/summation.d.ts +2 -1
  51. package/dist/algorithms/summation.d.ts.map +1 -1
  52. package/dist/algorithms/summation.js +2 -1
  53. package/dist/algorithms/summation.js.map +1 -1
  54. package/dist/linalg/DualQuaternion.d.ts +102 -32
  55. package/dist/linalg/DualQuaternion.d.ts.map +1 -1
  56. package/dist/linalg/DualQuaternion.js +243 -272
  57. package/dist/linalg/DualQuaternion.js.map +1 -1
  58. package/dist/linalg/Matrix.d.ts +14 -10
  59. package/dist/linalg/Matrix.d.ts.map +1 -1
  60. package/dist/linalg/Matrix2.d.ts +74 -51
  61. package/dist/linalg/Matrix2.d.ts.map +1 -1
  62. package/dist/linalg/Matrix2.js +95 -171
  63. package/dist/linalg/Matrix2.js.map +1 -1
  64. package/dist/linalg/Matrix3.d.ts +122 -72
  65. package/dist/linalg/Matrix3.d.ts.map +1 -1
  66. package/dist/linalg/Matrix3.js +186 -355
  67. package/dist/linalg/Matrix3.js.map +1 -1
  68. package/dist/linalg/Matrix4.d.ts +266 -149
  69. package/dist/linalg/Matrix4.d.ts.map +1 -1
  70. package/dist/linalg/Matrix4.js +512 -852
  71. package/dist/linalg/Matrix4.js.map +1 -1
  72. package/dist/linalg/Quaternion.d.ts +252 -34
  73. package/dist/linalg/Quaternion.d.ts.map +1 -1
  74. package/dist/linalg/Quaternion.js +436 -166
  75. package/dist/linalg/Quaternion.js.map +1 -1
  76. package/dist/linalg/SlowMatrix.d.ts +10 -9
  77. package/dist/linalg/SlowMatrix.d.ts.map +1 -1
  78. package/dist/linalg/SlowMatrix.js +10 -9
  79. package/dist/linalg/SlowMatrix.js.map +1 -1
  80. package/dist/linalg/SlowSquareMatrix.d.ts +10 -9
  81. package/dist/linalg/SlowSquareMatrix.d.ts.map +1 -1
  82. package/dist/linalg/SlowSquareMatrix.js +10 -9
  83. package/dist/linalg/SlowSquareMatrix.js.map +1 -1
  84. package/dist/linalg/SquareMatrix.d.ts +6 -5
  85. package/dist/linalg/SquareMatrix.d.ts.map +1 -1
  86. package/dist/linalg/Vector.d.ts +8 -4
  87. package/dist/linalg/Vector.d.ts.map +1 -1
  88. package/dist/linalg/Vector2.d.ts +82 -31
  89. package/dist/linalg/Vector2.d.ts.map +1 -1
  90. package/dist/linalg/Vector2.js +112 -154
  91. package/dist/linalg/Vector2.js.map +1 -1
  92. package/dist/linalg/Vector3.d.ts +93 -41
  93. package/dist/linalg/Vector3.d.ts.map +1 -1
  94. package/dist/linalg/Vector3.js +171 -282
  95. package/dist/linalg/Vector3.js.map +1 -1
  96. package/dist/linalg/Vector4.d.ts +71 -21
  97. package/dist/linalg/Vector4.d.ts.map +1 -1
  98. package/dist/linalg/Vector4.js +121 -195
  99. package/dist/linalg/Vector4.js.map +1 -1
  100. package/dist/types/AxisAngle.d.ts +4 -1
  101. package/dist/types/AxisAngle.d.ts.map +1 -1
  102. package/dist/types/FieldOfView.d.ts +4 -1
  103. package/dist/types/FieldOfView.d.ts.map +1 -1
  104. package/dist/utility/BigNumber.d.ts +4 -1
  105. package/dist/utility/BigNumber.d.ts.map +1 -1
  106. package/dist/utility/BigNumber.js +4 -1
  107. package/dist/utility/BigNumber.js.map +1 -1
  108. package/dist/utility/MagnitudeError.d.ts +4 -1
  109. package/dist/utility/MagnitudeError.d.ts.map +1 -1
  110. package/dist/utility/MagnitudeError.js +4 -1
  111. package/dist/utility/MagnitudeError.js.map +1 -1
  112. package/dist/utility/MatrixSizeError.d.ts +4 -1
  113. package/dist/utility/MatrixSizeError.d.ts.map +1 -1
  114. package/dist/utility/MatrixSizeError.js +4 -1
  115. package/dist/utility/MatrixSizeError.js.map +1 -1
  116. package/dist/utility/PartialMatrixError.d.ts +4 -1
  117. package/dist/utility/PartialMatrixError.d.ts.map +1 -1
  118. package/dist/utility/PartialMatrixError.js +4 -1
  119. package/dist/utility/PartialMatrixError.js.map +1 -1
  120. package/dist/utility/SingularMatrixError.d.ts +2 -1
  121. package/dist/utility/SingularMatrixError.d.ts.map +1 -1
  122. package/dist/utility/SingularMatrixError.js +2 -1
  123. package/dist/utility/SingularMatrixError.js.map +1 -1
  124. package/dist/utility/epsilon.d.ts +4 -1
  125. package/dist/utility/epsilon.d.ts.map +1 -1
  126. package/dist/utility/epsilon.js +4 -1
  127. package/dist/utility/epsilon.js.map +1 -1
  128. package/package.json +9 -9
  129. package/src/algorithms/approx.ts +12 -0
  130. package/src/algorithms/approxRelative.ts +12 -0
  131. package/src/algorithms/combinations.ts +2 -1
  132. package/src/algorithms/degreesToRadians.ts +6 -3
  133. package/src/algorithms/factorial.ts +3 -1
  134. package/src/algorithms/fibonacci.ts +7 -2
  135. package/src/algorithms/greatestCommonDivisor.ts +9 -4
  136. package/src/algorithms/hypergeometricPmf.ts +3 -2
  137. package/src/algorithms/isPrime.ts +2 -1
  138. package/src/algorithms/permutations.ts +2 -1
  139. package/src/algorithms/primeFactorization.ts +2 -1
  140. package/src/algorithms/radiansToDegrees.ts +6 -3
  141. package/src/algorithms/summation.ts +2 -1
  142. package/src/linalg/DualQuaternion.ts +424 -289
  143. package/src/linalg/Matrix.ts +14 -10
  144. package/src/linalg/Matrix2.ts +141 -188
  145. package/src/linalg/Matrix3.ts +400 -375
  146. package/src/linalg/Matrix4.ts +1083 -905
  147. package/src/linalg/Quaternion.ts +706 -188
  148. package/src/linalg/SlowMatrix.ts +10 -9
  149. package/src/linalg/SlowSquareMatrix.ts +10 -9
  150. package/src/linalg/SquareMatrix.ts +6 -5
  151. package/src/linalg/Vector.ts +8 -4
  152. package/src/linalg/Vector2.ts +146 -173
  153. package/src/linalg/Vector3.ts +293 -326
  154. package/src/linalg/Vector4.ts +227 -215
  155. package/src/types/AxisAngle.ts +4 -1
  156. package/src/types/FieldOfView.ts +4 -1
  157. package/src/utility/BigNumber.ts +6 -3
  158. package/src/utility/MagnitudeError.ts +4 -1
  159. package/src/utility/MatrixSizeError.ts +4 -1
  160. package/src/utility/PartialMatrixError.ts +4 -1
  161. package/src/utility/SingularMatrixError.ts +2 -1
  162. package/src/utility/epsilon.ts +4 -1
@@ -1,7 +1,8 @@
1
- import epsilon from "../utility/epsilon.js";
1
+ import approxRelative from "../algorithms/approxRelative.js";
2
2
  /**
3
3
  * Creates a 4x1 vector-like object.
4
4
  * @returns A 4x1 vector-like object.
5
+ * @public
5
6
  */
6
7
  export const createVector4Like = () => {
7
8
  return new Float32Array(4);
@@ -14,6 +15,7 @@ export const createVector4Like = () => {
14
15
  * @param w - The fourth component.
15
16
  * @param out - The vector to store the result in.
16
17
  * @returns A new vector.
18
+ * @public
17
19
  */
18
20
  export const fromValues = (x, y, z, w, out) => {
19
21
  out[0] = x;
@@ -27,180 +29,123 @@ export const fromValues = (x, y, z, w, out) => {
27
29
  * @param a - The first vector.
28
30
  * @param b - The second vector.
29
31
  * @returns Whether or not the vectors are equivalent.
32
+ * @public
30
33
  */
31
- export const equals = (a, b) => {
32
- const a0 = a[0];
33
- const a1 = a[1];
34
- const a2 = a[2];
35
- const a3 = a[3];
36
- const b0 = b[0];
37
- const b1 = b[1];
38
- const b2 = b[2];
39
- const b3 = b[3];
40
- return (Math.abs(a0 - b0) <= epsilon * Math.max(1, Math.abs(a0), Math.abs(b0)) &&
41
- Math.abs(a1 - b1) <= epsilon * Math.max(1, Math.abs(a1), Math.abs(b1)) &&
42
- Math.abs(a2 - b2) <= epsilon * Math.max(1, Math.abs(a2), Math.abs(b2)) &&
43
- Math.abs(a3 - b3) <= epsilon * Math.max(1, Math.abs(a3), Math.abs(b3)));
44
- };
34
+ export const equals = (a, b) => approxRelative(a[0], b[0]) &&
35
+ approxRelative(a[1], b[1]) &&
36
+ approxRelative(a[2], b[2]) &&
37
+ approxRelative(a[3], b[3]);
45
38
  /**
46
39
  * Determine whether or not two vectors are exactly equivalent.
47
40
  * @param a - The first vector.
48
41
  * @param b - The second vector.
49
42
  * @returns Whether or not the vectors are equivalent.
43
+ * @public
50
44
  */
51
- export const exactEquals = (a, b) => {
52
- return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3];
53
- };
45
+ export const exactEquals = (a, b) => a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3];
54
46
  /**
55
47
  * Add two vectors.
56
48
  * @param a - The augend.
57
49
  * @param b - The addend.
58
50
  * @param out - The vector to store the result in.
59
51
  * @returns The sum.
52
+ * @public
60
53
  */
61
- export const add = (a, b, out) => {
62
- out[0] = a[0] + b[0];
63
- out[1] = a[1] + b[1];
64
- out[2] = a[2] + b[2];
65
- out[3] = a[3] + b[3];
66
- return out;
67
- };
54
+ export const add = (a, b, out) => fromValues(a[0] + b[0], a[1] + b[1], a[2] + b[2], a[3] + b[3], out);
68
55
  /**
69
56
  * Copy the values from one vector to another.
70
57
  * @param vector - The vector to copy.
71
58
  * @param out - The vector to store the result in.
72
59
  * @returns The copy.
60
+ * @public
73
61
  */
74
- export const copy = (vector, out) => {
75
- out[0] = vector[0];
76
- out[1] = vector[1];
77
- out[2] = vector[2];
78
- out[3] = vector[3];
79
- return out;
80
- };
62
+ export const copy = (vector, out) => fromValues(vector[0], vector[1], vector[2], vector[3], out);
81
63
  /**
82
64
  * Multiply two vectors.
83
65
  * @param a - The multiplier.
84
66
  * @param b - The multiplicand.
85
67
  * @param out - The vector to store the result in.
86
68
  * @returns The product.
69
+ * @public
87
70
  */
88
- export const multiply = (a, b, out) => {
89
- out[0] = a[0] * b[0];
90
- out[1] = a[1] * b[1];
91
- out[2] = a[2] * b[2];
92
- out[3] = a[3] * b[3];
93
- return out;
94
- };
71
+ export const multiply = (a, b, out) => fromValues(a[0] * b[0], a[1] * b[1], a[2] * b[2], a[3] * b[3], out);
95
72
  /**
96
73
  * Divide two vectors.
97
74
  * @param a - The dividend.
98
75
  * @param b - The divisor.
99
76
  * @param out - The vector to store the result in.
100
77
  * @returns The quotient.
78
+ * @public
101
79
  */
102
- export const divide = (a, b, out) => {
103
- out[0] = a[0] / b[0];
104
- out[1] = a[1] / b[1];
105
- out[2] = a[2] / b[2];
106
- out[3] = a[3] / b[3];
107
- return out;
108
- };
80
+ export const divide = (a, b, out) => fromValues(a[0] / b[0], a[1] / b[1], a[2] / b[2], a[3] / b[3], out);
109
81
  /**
110
82
  * Subtract two vectors.
111
83
  * @param a - The minuend.
112
84
  * @param b - The subtrahend.
113
85
  * @param out - The vector to store the result in.
114
86
  * @returns The difference.
87
+ * @public
115
88
  */
116
- export const subtract = (a, b, out) => {
117
- out[0] = a[0] - b[0];
118
- out[1] = a[1] - b[1];
119
- out[2] = a[2] - b[2];
120
- out[3] = a[3] - b[3];
121
- return out;
122
- };
89
+ export const subtract = (a, b, out) => fromValues(a[0] - b[0], a[1] - b[1], a[2] - b[2], a[3] - b[3], out);
90
+ /**
91
+ * Absolutize the components of a vector.
92
+ * @param vector - The vector.
93
+ * @param out - The vector to store the result in.
94
+ * @returns The absolutized vector.
95
+ * @public
96
+ */
97
+ export const abs = (vector, out) => fromValues(Math.abs(vector[0]), Math.abs(vector[1]), Math.abs(vector[2]), Math.abs(vector[3]), out);
123
98
  /**
124
99
  * Round up the components of a vector.
125
100
  * @param vector - The vector.
126
101
  * @param out - The vector to store the result in.
127
102
  * @returns The rounded vector.
103
+ * @public
128
104
  */
129
- export const ceil = (vector, out) => {
130
- out[0] = Math.ceil(vector[0]);
131
- out[1] = Math.ceil(vector[1]);
132
- out[2] = Math.ceil(vector[2]);
133
- out[3] = Math.ceil(vector[3]);
134
- return out;
135
- };
105
+ export const ceil = (vector, out) => fromValues(Math.ceil(vector[0]), Math.ceil(vector[1]), Math.ceil(vector[2]), Math.ceil(vector[3]), out);
136
106
  /**
137
107
  * Round down the components of a vector.
138
108
  * @param vector - The vector.
139
109
  * @param out - The vector to store the result in.
140
110
  * @returns The rounded vector.
111
+ * @public
141
112
  */
142
- export const floor = (vector, out) => {
143
- out[0] = Math.floor(vector[0]);
144
- out[1] = Math.floor(vector[1]);
145
- out[2] = Math.floor(vector[2]);
146
- out[3] = Math.floor(vector[3]);
147
- return out;
148
- };
113
+ export const floor = (vector, out) => fromValues(Math.floor(vector[0]), Math.floor(vector[1]), Math.floor(vector[2]), Math.floor(vector[3]), out);
149
114
  /**
150
115
  * Round the components of a vector.
151
116
  * @param vector - The vector.
152
117
  * @param out - The vector to store the result in.
153
118
  * @returns The rounded vector.
119
+ * @public
154
120
  */
155
- export const round = (vector, out) => {
156
- out[0] = Math.round(vector[0]);
157
- out[1] = Math.round(vector[1]);
158
- out[2] = Math.round(vector[2]);
159
- out[3] = Math.round(vector[3]);
160
- return out;
161
- };
121
+ export const round = (vector, out) => fromValues(Math.round(vector[0]), Math.round(vector[1]), Math.round(vector[2]), Math.round(vector[3]), out);
162
122
  /**
163
123
  * Return the minimum of two vectors.
164
124
  * @param a - The first vector.
165
125
  * @param b - The second vector.
166
126
  * @param out - The vector to store the result in.
167
127
  * @returns The minimum.
128
+ * @public
168
129
  */
169
- export const min = (a, b, out) => {
170
- out[0] = Math.min(a[0], b[0]);
171
- out[1] = Math.min(a[1], b[1]);
172
- out[2] = Math.min(a[2], b[2]);
173
- out[3] = Math.min(a[3], b[3]);
174
- return out;
175
- };
130
+ export const min = (a, b, out) => fromValues(Math.min(a[0], b[0]), Math.min(a[1], b[1]), Math.min(a[2], b[2]), Math.min(a[3], b[3]), out);
176
131
  /**
177
132
  * Return the maximum of two vectors.
178
133
  * @param a - The first vector.
179
134
  * @param b - The second vector.
180
135
  * @param out - The vector to store the result in.
181
136
  * @returns The maximum.
137
+ * @public
182
138
  */
183
- export const max = (a, b, out) => {
184
- out[0] = Math.max(a[0], b[0]);
185
- out[1] = Math.max(a[1], b[1]);
186
- out[2] = Math.max(a[2], b[2]);
187
- out[3] = Math.max(a[3], b[3]);
188
- return out;
189
- };
139
+ export const max = (a, b, out) => fromValues(Math.max(a[0], b[0]), Math.max(a[1], b[1]), Math.max(a[2], b[2]), Math.max(a[3], b[3]), out);
190
140
  /**
191
141
  * Scale a vector by a scalar.
192
142
  * @param vector - The multiplier.
193
143
  * @param scalar - The multiplicand.
194
144
  * @param out - The vector to store the result in.
195
145
  * @returns The product.
146
+ * @public
196
147
  */
197
- export const scale = (vector, scalar, out) => {
198
- out[0] = vector[0] * scalar;
199
- out[1] = vector[1] * scalar;
200
- out[2] = vector[2] * scalar;
201
- out[3] = vector[3] * scalar;
202
- return out;
203
- };
148
+ export const scale = (vector, scalar, out) => fromValues(vector[0] * scalar, vector[1] * scalar, vector[2] * scalar, vector[3] * scalar, out);
204
149
  /**
205
150
  * Add two vectors after scaling the second by a scalar.
206
151
  * @param a - The augend.
@@ -208,34 +153,31 @@ export const scale = (vector, scalar, out) => {
208
153
  * @param scalar - The multiplicand.
209
154
  * @param out - The vector to store the result in.
210
155
  * @returns The sum.
156
+ * @public
211
157
  */
212
- export const scaleAndAdd = (a, b, scalar, out) => {
213
- out[0] = a[0] + b[0] * scalar;
214
- out[1] = a[1] + b[1] * scalar;
215
- out[2] = a[2] + b[2] * scalar;
216
- out[3] = a[3] + b[3] * scalar;
217
- return out;
218
- };
158
+ export const scaleAndAdd = (a, b, scalar, out) => fromValues(a[0] + b[0] * scalar, a[1] + b[1] * scalar, a[2] + b[2] * scalar, a[3] + b[3] * scalar, out);
219
159
  /**
220
160
  * Calculate the Euclidean distance between two vectors.
221
161
  * @param a - The first vector
222
162
  * @param b - The second vector.
223
163
  * @returns The distance.
224
- * @see [Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance)
164
+ * @see {@link https://en.wikipedia.org/wiki/Euclidean_distance | Euclidean distance}
165
+ * @public
225
166
  */
226
167
  export const distance = (a, b) => {
227
168
  const x = a[0] - b[0];
228
169
  const y = a[1] - b[1];
229
170
  const z = a[2] - b[2];
230
171
  const w = a[3] - b[3];
231
- return Math.sqrt(x * x + y * y + z * z + w * w);
172
+ return Math.sqrt(x * x + y * y + z * z + w * w); // `Math.hypot` is slower.
232
173
  };
233
174
  /**
234
175
  * Calculate the squared Euclidean distance between two vectors.
235
176
  * @param a - The first vector.
236
177
  * @param b - The second vector.
237
178
  * @returns The squared distance.
238
- * @see [Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance)
179
+ * @see {@link https://en.wikipedia.org/wiki/Euclidean_distance | Euclidean distance}
180
+ * @public
239
181
  */
240
182
  export const squaredDistance = (a, b) => {
241
183
  const x = a[0] - b[0];
@@ -248,18 +190,20 @@ export const squaredDistance = (a, b) => {
248
190
  * Calculate the magnitude (length) of a vector.
249
191
  * @param vector - The vector.
250
192
  * @returns The magnitude.
193
+ * @public
251
194
  */
252
195
  export const getMagnitude = (vector) => {
253
196
  const x = vector[0];
254
197
  const y = vector[1];
255
198
  const z = vector[2];
256
199
  const w = vector[3];
257
- return Math.sqrt(x * x + y * y + z * z + w * w);
200
+ return Math.sqrt(x * x + y * y + z * z + w * w); // `Math.hypot` is slower.
258
201
  };
259
202
  /**
260
203
  * Calculate the squared magnitude (length) of a vector.
261
204
  * @param vector - The vector.
262
205
  * @returns The magnitude.
206
+ * @public
263
207
  */
264
208
  export const getSquaredMagnitude = (vector) => {
265
209
  const x = vector[0];
@@ -273,33 +217,24 @@ export const getSquaredMagnitude = (vector) => {
273
217
  * @param vector - The vector.
274
218
  * @param out - The vector to store the result in.
275
219
  * @returns The negated vector.
220
+ * @public
276
221
  */
277
- export const negate = (vector, out) => {
278
- out[0] = -vector[0];
279
- out[1] = -vector[1];
280
- out[2] = -vector[2];
281
- out[3] = -vector[3];
282
- return out;
283
- };
222
+ export const negate = (vector, out) => fromValues(-vector[0], -vector[1], -vector[2], -vector[3], out);
284
223
  /**
285
224
  * Calculate the multiplicative inverse of the components of a vector.
286
225
  * @param vector - The vector.
287
226
  * @param out - The vector to store the result in.
288
227
  * @returns The inverted vector.
228
+ * @public
289
229
  */
290
- export const invert = (vector, out) => {
291
- out[0] = 1 / vector[0];
292
- out[1] = 1 / vector[1];
293
- out[2] = 1 / vector[2];
294
- out[3] = 1 / vector[3];
295
- return out;
296
- };
230
+ export const invert = (vector, out) => fromValues(1 / vector[0], 1 / vector[1], 1 / vector[2], 1 / vector[3], out);
297
231
  /**
298
232
  * Normalize a vector.
299
233
  * @param vector - The vector.
300
234
  * @param out - The vector to store the result in.
301
235
  * @returns The normalized vector.
302
- * @see [Unit vector](https://en.wikipedia.org/wiki/Unit_vector)
236
+ * @see {@link https://en.wikipedia.org/wiki/Unit_vector | Unit vector}
237
+ * @public
303
238
  */
304
239
  export const normalize = (vector, out) => {
305
240
  const x = vector[0];
@@ -310,22 +245,17 @@ export const normalize = (vector, out) => {
310
245
  if (len > 0) {
311
246
  len = 1 / Math.sqrt(len);
312
247
  }
313
- out[0] = x * len;
314
- out[1] = y * len;
315
- out[2] = z * len;
316
- out[3] = w * len;
317
- return out;
248
+ return fromValues(x * len, y * len, z * len, w * len, out);
318
249
  };
319
250
  /**
320
251
  * Calculate the dot product of two vectors.
321
252
  * @param a - The multiplier.
322
253
  * @param b - The multiplicand.
323
254
  * @returns The dot product.
324
- * @see [Dot product](https://en.wikipedia.org/wiki/Dot_product)
255
+ * @see {@link https://en.wikipedia.org/wiki/Dot_product | Dot product}
256
+ * @public
325
257
  */
326
- export const dot = (a, b) => {
327
- return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
328
- };
258
+ export const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
329
259
  /**
330
260
  * Calculate the cross product of three vectors in a four-dimensional space.
331
261
  * @param a - The first vector.
@@ -333,24 +263,29 @@ export const dot = (a, b) => {
333
263
  * @param c - The third vector.
334
264
  * @param out - The vector to store the result in.
335
265
  * @returns The cross product.
336
- * @see [Cross product](https://en.wikipedia.org/wiki/Cross_product)
266
+ * @see {@link https://en.wikipedia.org/wiki/Cross_product | Cross product}
267
+ * @public
337
268
  */
338
269
  export const cross = (a, b, c, out) => {
339
- const d = b[0] * c[1] - b[1] * c[0];
340
- const e = b[0] * c[2] - b[2] * c[0];
341
- const f = b[0] * c[3] - b[3] * c[0];
342
- const g = b[1] * c[2] - b[2] * c[1];
343
- const h = b[1] * c[3] - b[3] * c[1];
344
- const i = b[2] * c[3] - b[3] * c[2];
345
- const j = a[0];
346
- const k = a[1];
347
- const l = a[2];
348
- const m = a[3];
349
- out[0] = k * i - l * h + m * g;
350
- out[1] = -(j * i) + l * f - m * e;
351
- out[2] = j * h - k * f + m * d;
352
- out[3] = -(j * g) + k * e - l * d;
353
- return out;
270
+ const a0 = a[0];
271
+ const a1 = a[1];
272
+ const a2 = a[2];
273
+ const a3 = a[3];
274
+ const b0 = b[0];
275
+ const b1 = b[1];
276
+ const b2 = b[2];
277
+ const b3 = b[3];
278
+ const c0 = c[0];
279
+ const c1 = c[1];
280
+ const c2 = c[2];
281
+ const c3 = c[3];
282
+ const d = b0 * c1 - b1 * c0;
283
+ const e = b0 * c2 - b2 * c0;
284
+ const f = b0 * c3 - b3 * c0;
285
+ const g = b1 * c2 - b2 * c1;
286
+ const h = b1 * c3 - b3 * c1;
287
+ const i = b2 * c3 - b3 * c2;
288
+ return fromValues(a1 * i - a2 * h + a3 * g, -(a0 * i) + a2 * f - a3 * e, a0 * h - a1 * f + a3 * d, -(a0 * g) + a1 * e - a2 * d, out);
354
289
  };
355
290
  /**
356
291
  * Perform a linear interpolation between two vectors.
@@ -359,40 +294,33 @@ export const cross = (a, b, c, out) => {
359
294
  * @param t - The interpolation amount (in `[0,1]`).
360
295
  * @param out - The vector to store the result in.
361
296
  * @returns The interpolated vector.
362
- * @see [Linear interpolation](https://en.wikipedia.org/wiki/Linear_interpolation)
297
+ * @see {@link https://en.wikipedia.org/wiki/Linear_interpolation | Linear interpolation}
298
+ * @public
363
299
  */
364
300
  export const lerp = (a, b, t, out) => {
365
301
  const ax = a[0];
366
302
  const ay = a[1];
367
303
  const az = a[2];
368
304
  const aw = a[3];
369
- out[0] = ax + t * (b[0] - ax);
370
- out[1] = ay + t * (b[1] - ay);
371
- out[2] = az + t * (b[2] - az);
372
- out[3] = aw + t * (b[3] - aw);
373
- return out;
305
+ return fromValues(ax + t * (b[0] - ax), ay + t * (b[1] - ay), az + t * (b[2] - az), aw + t * (b[3] - aw), out);
374
306
  };
375
307
  /**
376
308
  * Set this vector to a random value with the given magnitude.
377
309
  * @param magnitude - The magnitude.
378
310
  * @param out - The vector to store the result in.
379
311
  * @returns This vector.
312
+ * @see {@link https://github.com/toji/gl-matrix/pull/458 | toji/gl-matrix#458}
313
+ * @public
380
314
  */
381
315
  export const random = (magnitude, out) => {
382
316
  const a = Math.random();
383
317
  const v1 = a * 2 - 1;
384
318
  const v2 = (4 * Math.random() - 2) * Math.sqrt(a * -a + a);
385
- const s1 = v1 * v1 + v2 * v2;
386
319
  const b = Math.random();
387
320
  const v3 = b * 2 - 1;
388
321
  const v4 = (4 * Math.random() - 2) * Math.sqrt(b * -b + b);
389
- const s2 = v3 * v3 + v4 * v4;
390
- const d = Math.sqrt((1 - s1) / s2);
391
- out[0] = magnitude * v1;
392
- out[1] = magnitude * v2;
393
- out[2] = magnitude * v3 * d;
394
- out[3] = magnitude * v4 * d;
395
- return out;
322
+ const d = Math.sqrt((1 - (v1 * v1 + v2 * v2)) / (v3 * v3 + v4 * v4));
323
+ return fromValues(magnitude * v1, magnitude * v2, magnitude * v3 * d, magnitude * v4 * d, out);
396
324
  };
397
325
  /**
398
326
  * Transform a vector by a four-by-four matrix.
@@ -400,38 +328,32 @@ export const random = (magnitude, out) => {
400
328
  * @param matrix - The matrix (multiplicand).
401
329
  * @param out - The vector to store the result in.
402
330
  * @returns The transformed vector.
403
- * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
331
+ * @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
332
+ * @public
404
333
  */
405
334
  export const transformMatrix4 = (vector, matrix, out) => {
406
335
  const x = vector[0];
407
336
  const y = vector[1];
408
337
  const z = vector[2];
409
338
  const w = vector[3];
410
- out[0] = matrix[0] * x + matrix[4] * y + matrix[8] * z + matrix[12] * w;
411
- out[1] = matrix[1] * x + matrix[5] * y + matrix[9] * z + matrix[13] * w;
412
- out[2] = matrix[2] * x + matrix[6] * y + matrix[10] * z + matrix[14] * w;
413
- out[3] = matrix[3] * x + matrix[7] * y + matrix[11] * z + matrix[15] * w;
414
- return out;
339
+ return fromValues(matrix[0] * x + matrix[4] * y + matrix[8] * z + matrix[12] * w, matrix[1] * x + matrix[5] * y + matrix[9] * z + matrix[13] * w, matrix[2] * x + matrix[6] * y + matrix[10] * z + matrix[14] * w, matrix[3] * x + matrix[7] * y + matrix[11] * z + matrix[15] * w, out);
415
340
  };
416
341
  /**
417
342
  * Set a vector to the zero vector.
418
343
  * @param out - The vector to store the result in.
419
344
  * @returns This vector.
345
+ * @public
420
346
  */
421
- export const zero = (out) => {
422
- out[0] = 0;
423
- out[1] = 0;
424
- out[2] = 0;
425
- out[3] = 0;
426
- return out;
427
- };
347
+ export const zero = (out) => fromValues(0, 0, 0, 0, out);
428
348
  /**
429
- * Transform a vector by a quaternion.
349
+ * Transform a vector by a unit quaternion.
430
350
  * @param vector - The vector.
431
- * @param quaternion - The quaternion.
351
+ * @param quaternion - The unit quaternion.
432
352
  * @param out - The vector to store the result in.
433
353
  * @returns The transformed vector.
434
- * @see [Quaternion](https://en.wikipedia.org/wiki/Quaternion)
354
+ * @see {@link https://en.wikipedia.org/wiki/Quaternion | Quaternion}
355
+ * @see {@link https://raw.org/proof/vector-rotation-using-quaternions/ | Fast Vector Rotation using Quaternions}
356
+ * @public
435
357
  */
436
358
  export const transformQuaternion = (vector, quaternion, out) => {
437
359
  const x = vector[0];
@@ -441,19 +363,15 @@ export const transformQuaternion = (vector, quaternion, out) => {
441
363
  const qy = quaternion[1];
442
364
  const qz = quaternion[2];
443
365
  const qw = quaternion[3];
444
- const ix = qw * x + qy * z - qz * y;
445
- const iy = qw * y + qz * x - qx * z;
446
- const iz = qw * z + qx * y - qy * x;
447
- const iw = -qx * x - qy * y - qz * z;
448
- out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
449
- out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
450
- out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
451
- out[3] = vector[3];
452
- return out;
366
+ const tx = (qy * z - qz * y) * 2;
367
+ const ty = (qz * x - qx * z) * 2;
368
+ const tz = (qx * y - qy * x) * 2;
369
+ return fromValues(x + qw * tx + qy * tz - qz * ty, y + qw * ty + qz * tx - qx * tz, z + qw * tz + qx * ty - qy * tx, vector[3], out);
453
370
  };
454
371
  /**
455
372
  * A quantity with magnitude and direction in four dimensions.
456
- * @see [Euclidean vector](https://en.wikipedia.org/wiki/Euclidean_vector)
373
+ * @see {@link https://en.wikipedia.org/wiki/Euclidean_vector | Euclidean vector}
374
+ * @public
457
375
  */
458
376
  export default class Vector4 extends Float32Array {
459
377
  /**
@@ -470,7 +388,7 @@ export default class Vector4 extends Float32Array {
470
388
  }
471
389
  /**
472
390
  * Create a four-dimensional zero vector.
473
- * @see [Euclidean vector](https://en.wikipedia.org/wiki/Euclidean_vector)
391
+ * @see {@link https://en.wikipedia.org/wiki/Euclidean_vector | Euclidean vector}
474
392
  */
475
393
  constructor() {
476
394
  super(4);
@@ -551,6 +469,14 @@ export default class Vector4 extends Float32Array {
551
469
  subtract(vector, out = new Vector4()) {
552
470
  return subtract(this, vector, out);
553
471
  }
472
+ /**
473
+ * Absolutize the components of this vector.
474
+ * @param out - The vector to store the result in.
475
+ * @returns The absolutized vector.
476
+ */
477
+ abs(out = new Vector4()) {
478
+ return abs(this, out);
479
+ }
554
480
  /**
555
481
  * Round up the components of this vector.
556
482
  * @param out - The vector to store the result in.
@@ -616,7 +542,7 @@ export default class Vector4 extends Float32Array {
616
542
  * Calculate the Euclidean distance between this vector and another.
617
543
  * @param vector - The other vector.
618
544
  * @returns The distance.
619
- * @see [Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance)
545
+ * @see {@link https://en.wikipedia.org/wiki/Euclidean_distance | Euclidean distance}
620
546
  */
621
547
  distance(vector) {
622
548
  return distance(this, vector);
@@ -625,7 +551,7 @@ export default class Vector4 extends Float32Array {
625
551
  * Calculate the squared Euclidean distance between this vector and another.
626
552
  * @param vector - The other vector.
627
553
  * @returns The squared distance.
628
- * @see [Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance)
554
+ * @see {@link https://en.wikipedia.org/wiki/Euclidean_distance | Euclidean distance}
629
555
  */
630
556
  squaredDistance(vector) {
631
557
  return squaredDistance(this, vector);
@@ -658,7 +584,7 @@ export default class Vector4 extends Float32Array {
658
584
  * Normalize this vector.
659
585
  * @param out - The vector to store the result in.
660
586
  * @returns The normalized vector.
661
- * @see [Unit vector](https://en.wikipedia.org/wiki/Unit_vector)
587
+ * @see {@link https://en.wikipedia.org/wiki/Unit_vector | Unit vector}
662
588
  */
663
589
  normalize(out = new Vector4()) {
664
590
  return normalize(this, out);
@@ -667,7 +593,7 @@ export default class Vector4 extends Float32Array {
667
593
  * Calculate the dot product of this and another vector.
668
594
  * @param vector - The other vector.
669
595
  * @returns The dot product.
670
- * @see [Dot product](https://en.wikipedia.org/wiki/Dot_product)
596
+ * @see {@link https://en.wikipedia.org/wiki/Dot_product | Dot product}
671
597
  */
672
598
  dot(vector) {
673
599
  return dot(this, vector);
@@ -678,7 +604,7 @@ export default class Vector4 extends Float32Array {
678
604
  * @param b - The other other vector.
679
605
  * @param out - The vector to store the result in.
680
606
  * @returns The cross product.
681
- * @see [Cross product](https://en.wikipedia.org/wiki/Cross_product)
607
+ * @see {@link https://en.wikipedia.org/wiki/Cross_product | Cross product}
682
608
  */
683
609
  cross(a, b, out = new Vector4()) {
684
610
  return cross(this, a, b, out);
@@ -689,7 +615,7 @@ export default class Vector4 extends Float32Array {
689
615
  * @param t - The interpolation amount (in `[0,1]`).
690
616
  * @param out - The vector to store the result in.
691
617
  * @returns The interpolated vector.
692
- * @see [Linear interpolation](https://en.wikipedia.org/wiki/Linear_interpolation)
618
+ * @see {@link https://en.wikipedia.org/wiki/Linear_interpolation | Linear interpolation}
693
619
  */
694
620
  lerp(vector, t, out = new Vector4()) {
695
621
  return lerp(this, vector, t, out);
@@ -707,7 +633,7 @@ export default class Vector4 extends Float32Array {
707
633
  * @param matrix - The matrix.
708
634
  * @param out - The vector to store the result in.
709
635
  * @returns The transformed vector.
710
- * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
636
+ * @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
711
637
  */
712
638
  transformMatrix4(matrix, out = new Vector4()) {
713
639
  return transformMatrix4(this, matrix, out);
@@ -724,7 +650,7 @@ export default class Vector4 extends Float32Array {
724
650
  * @param quaternion - The quaternion.
725
651
  * @param out - The vector to store the result in.
726
652
  * @returns The transformed vector.
727
- * @see [Quaternion](https://en.wikipedia.org/wiki/Quaternion)
653
+ * @see {@link https://en.wikipedia.org/wiki/Quaternion | Quaternion}
728
654
  */
729
655
  transformQuaternion(quaternion, out = new Vector4()) {
730
656
  return transformQuaternion(this, quaternion, out);