@ac-kit/math-linear 0.2.0 → 0.3.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 (178) hide show
  1. package/CHANGELOG.md +268 -0
  2. package/README.md +229 -16
  3. package/dist/_brand.d.ts +14 -0
  4. package/dist/_brand.d.ts.map +1 -0
  5. package/dist/_brand.js +2 -0
  6. package/dist/_brand.js.map +1 -0
  7. package/dist/_invert.d.ts +19 -0
  8. package/dist/_invert.d.ts.map +1 -0
  9. package/dist/_invert.js +48 -0
  10. package/dist/_invert.js.map +1 -0
  11. package/dist/_slerp.d.ts +15 -0
  12. package/dist/_slerp.d.ts.map +1 -1
  13. package/dist/_slerp.js +47 -0
  14. package/dist/_slerp.js.map +1 -1
  15. package/dist/_strided-range.d.ts +23 -0
  16. package/dist/_strided-range.d.ts.map +1 -0
  17. package/dist/_strided-range.js +75 -0
  18. package/dist/_strided-range.js.map +1 -0
  19. package/dist/_trs.d.ts +38 -0
  20. package/dist/_trs.d.ts.map +1 -0
  21. package/dist/_trs.js +132 -0
  22. package/dist/_trs.js.map +1 -0
  23. package/dist/affine2-array.d.ts +53 -0
  24. package/dist/affine2-array.d.ts.map +1 -0
  25. package/dist/affine2-array.js +107 -0
  26. package/dist/affine2-array.js.map +1 -0
  27. package/dist/affine2-componentwise.d.ts +28 -0
  28. package/dist/affine2-componentwise.d.ts.map +1 -0
  29. package/dist/affine2-componentwise.js +66 -0
  30. package/dist/affine2-componentwise.js.map +1 -0
  31. package/dist/affine2.d.ts +144 -0
  32. package/dist/affine2.d.ts.map +1 -0
  33. package/dist/affine2.js +375 -0
  34. package/dist/affine2.js.map +1 -0
  35. package/dist/affine3-array.d.ts +53 -0
  36. package/dist/affine3-array.d.ts.map +1 -0
  37. package/dist/affine3-array.js +137 -0
  38. package/dist/affine3-array.js.map +1 -0
  39. package/dist/affine3-componentwise.d.ts +32 -0
  40. package/dist/affine3-componentwise.d.ts.map +1 -0
  41. package/dist/affine3-componentwise.js +100 -0
  42. package/dist/affine3-componentwise.js.map +1 -0
  43. package/dist/affine3.d.ts +192 -0
  44. package/dist/affine3.d.ts.map +1 -0
  45. package/dist/affine3.js +718 -0
  46. package/dist/affine3.js.map +1 -0
  47. package/dist/checked/index.d.ts +35 -0
  48. package/dist/checked/index.d.ts.map +1 -0
  49. package/dist/checked/index.js +58 -0
  50. package/dist/checked/index.js.map +1 -0
  51. package/dist/euler.d.ts +65 -0
  52. package/dist/euler.d.ts.map +1 -0
  53. package/dist/euler.js +217 -0
  54. package/dist/euler.js.map +1 -0
  55. package/dist/index.d.ts +29 -0
  56. package/dist/index.d.ts.map +1 -1
  57. package/dist/index.js +29 -0
  58. package/dist/index.js.map +1 -1
  59. package/dist/kl-activations.d.ts.map +1 -1
  60. package/dist/kl-activations.js +4 -3
  61. package/dist/kl-activations.js.map +1 -1
  62. package/dist/mat2x2-array.d.ts +36 -0
  63. package/dist/mat2x2-array.d.ts.map +1 -0
  64. package/dist/mat2x2-array.js +71 -0
  65. package/dist/mat2x2-array.js.map +1 -0
  66. package/dist/mat2x2-componentwise.d.ts +86 -0
  67. package/dist/mat2x2-componentwise.d.ts.map +1 -0
  68. package/dist/mat2x2-componentwise.js +199 -0
  69. package/dist/mat2x2-componentwise.js.map +1 -0
  70. package/dist/mat2x2.d.ts +67 -25
  71. package/dist/mat2x2.d.ts.map +1 -1
  72. package/dist/mat2x2.js +150 -94
  73. package/dist/mat2x2.js.map +1 -1
  74. package/dist/mat3x3-array.d.ts +36 -0
  75. package/dist/mat3x3-array.d.ts.map +1 -0
  76. package/dist/mat3x3-array.js +91 -0
  77. package/dist/mat3x3-array.js.map +1 -0
  78. package/dist/mat3x3-componentwise.d.ts +86 -0
  79. package/dist/mat3x3-componentwise.d.ts.map +1 -0
  80. package/dist/mat3x3-componentwise.js +299 -0
  81. package/dist/mat3x3-componentwise.js.map +1 -0
  82. package/dist/mat3x3.d.ts +131 -48
  83. package/dist/mat3x3.d.ts.map +1 -1
  84. package/dist/mat3x3.js +434 -193
  85. package/dist/mat3x3.js.map +1 -1
  86. package/dist/mat4x4-array.d.ts +46 -0
  87. package/dist/mat4x4-array.d.ts.map +1 -0
  88. package/dist/mat4x4-array.js +133 -0
  89. package/dist/mat4x4-array.js.map +1 -0
  90. package/dist/mat4x4-componentwise.d.ts +87 -0
  91. package/dist/mat4x4-componentwise.d.ts.map +1 -0
  92. package/dist/mat4x4-componentwise.js +434 -0
  93. package/dist/mat4x4-componentwise.js.map +1 -0
  94. package/dist/mat4x4.d.ts +195 -67
  95. package/dist/mat4x4.d.ts.map +1 -1
  96. package/dist/mat4x4.js +849 -335
  97. package/dist/mat4x4.js.map +1 -1
  98. package/dist/projection.d.ts +98 -0
  99. package/dist/projection.d.ts.map +1 -0
  100. package/dist/projection.js +369 -0
  101. package/dist/projection.js.map +1 -0
  102. package/dist/quaternion-array.d.ts +47 -0
  103. package/dist/quaternion-array.d.ts.map +1 -0
  104. package/dist/quaternion-array.js +86 -0
  105. package/dist/quaternion-array.js.map +1 -0
  106. package/dist/quaternion.d.ts +104 -22
  107. package/dist/quaternion.d.ts.map +1 -1
  108. package/dist/quaternion.js +294 -181
  109. package/dist/quaternion.js.map +1 -1
  110. package/dist/transform.d.ts +220 -0
  111. package/dist/transform.d.ts.map +1 -0
  112. package/dist/transform.js +412 -0
  113. package/dist/transform.js.map +1 -0
  114. package/dist/tridiagonal.d.ts +52 -0
  115. package/dist/tridiagonal.d.ts.map +1 -0
  116. package/dist/tridiagonal.js +90 -0
  117. package/dist/tridiagonal.js.map +1 -0
  118. package/dist/upload.d.ts +55 -0
  119. package/dist/upload.d.ts.map +1 -0
  120. package/dist/upload.js +129 -0
  121. package/dist/upload.js.map +1 -0
  122. package/dist/vec2-array-componentwise.d.ts +189 -0
  123. package/dist/vec2-array-componentwise.d.ts.map +1 -0
  124. package/dist/vec2-array-componentwise.js +424 -0
  125. package/dist/vec2-array-componentwise.js.map +1 -0
  126. package/dist/vec2-array.d.ts +53 -0
  127. package/dist/vec2-array.d.ts.map +1 -0
  128. package/dist/vec2-array.js +123 -0
  129. package/dist/vec2-array.js.map +1 -0
  130. package/dist/vec2-componentwise.d.ts +311 -0
  131. package/dist/vec2-componentwise.d.ts.map +1 -0
  132. package/dist/vec2-componentwise.js +542 -0
  133. package/dist/vec2-componentwise.js.map +1 -0
  134. package/dist/vec2.d.ts +97 -82
  135. package/dist/vec2.d.ts.map +1 -1
  136. package/dist/vec2.js +121 -216
  137. package/dist/vec2.js.map +1 -1
  138. package/dist/vec3-array-componentwise.d.ts +189 -0
  139. package/dist/vec3-array-componentwise.d.ts.map +1 -0
  140. package/dist/vec3-array-componentwise.js +458 -0
  141. package/dist/vec3-array-componentwise.js.map +1 -0
  142. package/dist/vec3-array.d.ts +102 -0
  143. package/dist/vec3-array.d.ts.map +1 -0
  144. package/dist/vec3-array.js +299 -0
  145. package/dist/vec3-array.js.map +1 -0
  146. package/dist/vec3-componentwise.d.ts +310 -0
  147. package/dist/vec3-componentwise.d.ts.map +1 -0
  148. package/dist/vec3-componentwise.js +594 -0
  149. package/dist/vec3-componentwise.js.map +1 -0
  150. package/dist/vec3.d.ts +177 -87
  151. package/dist/vec3.d.ts.map +1 -1
  152. package/dist/vec3.js +366 -273
  153. package/dist/vec3.js.map +1 -1
  154. package/dist/vec4-array-componentwise.d.ts +155 -0
  155. package/dist/vec4-array-componentwise.d.ts.map +1 -0
  156. package/dist/vec4-array-componentwise.js +401 -0
  157. package/dist/vec4-array-componentwise.js.map +1 -0
  158. package/dist/vec4-array.d.ts +12 -0
  159. package/dist/vec4-array.d.ts.map +1 -0
  160. package/dist/vec4-array.js +43 -0
  161. package/dist/vec4-array.js.map +1 -0
  162. package/dist/vec4-componentwise.d.ts +299 -0
  163. package/dist/vec4-componentwise.d.ts.map +1 -0
  164. package/dist/vec4-componentwise.js +637 -0
  165. package/dist/vec4-componentwise.js.map +1 -0
  166. package/dist/vec4.d.ts +57 -53
  167. package/dist/vec4.d.ts.map +1 -1
  168. package/dist/vec4.js +94 -246
  169. package/dist/vec4.js.map +1 -1
  170. package/dist/view-basis.d.ts +15 -0
  171. package/dist/view-basis.d.ts.map +1 -0
  172. package/dist/view-basis.js +29 -0
  173. package/dist/view-basis.js.map +1 -0
  174. package/dist/viewport.d.ts +114 -0
  175. package/dist/viewport.d.ts.map +1 -0
  176. package/dist/viewport.js +250 -0
  177. package/dist/viewport.js.map +1 -0
  178. package/package.json +18 -9
package/dist/mat3x3.js CHANGED
@@ -1,213 +1,454 @@
1
- import { isCloseTo } from "@ac-kit/core";
2
- import { diffOfProducts, dotPrecise } from "@ac-kit/math-scalar";
3
- export const MAT3X3_ZERO = [
4
- [0, 0, 0],
5
- [0, 0, 0],
6
- [0, 0, 0],
7
- ];
8
- export const MAT3X3_IDENTITY = [
9
- [1, 0, 0],
10
- [0, 1, 0],
11
- [0, 0, 1],
12
- ];
13
- export function mat3x3Add(a, b) {
14
- return [
15
- [a[0][0] + b[0][0], a[0][1] + b[0][1], a[0][2] + b[0][2]],
16
- [a[1][0] + b[1][0], a[1][1] + b[1][1], a[1][2] + b[1][2]],
17
- [a[2][0] + b[2][0], a[2][1] + b[2][1], a[2][2] + b[2][2]],
18
- ];
19
- }
20
- export function mat3x3Determinant(matrix) {
21
- const [[a, b, c], [d, e, f], [g, h, i]] = matrix;
22
- return dotPrecise([a, -b, c], [
23
- diffOfProducts(e, i, f, h),
24
- diffOfProducts(d, i, f, g),
25
- diffOfProducts(d, h, e, g),
26
- ]);
27
- }
28
- /** Exact element-wise equality. See {@link mat3x3IsClose} for a tolerant test. */
29
- export function mat3x3Equals(a, b) {
30
- return (a[0][0] === b[0][0] &&
31
- a[0][1] === b[0][1] &&
32
- a[0][2] === b[0][2] &&
33
- a[1][0] === b[1][0] &&
34
- a[1][1] === b[1][1] &&
35
- a[1][2] === b[1][2] &&
36
- a[2][0] === b[2][0] &&
37
- a[2][1] === b[2][1] &&
38
- a[2][2] === b[2][2]);
39
- }
40
- /**
41
- * Element-wise approximate equality.
42
- *
43
- * Not transitive, so it is not an equality in the sense a `Set` or `Map` needs,
44
- * and never a basis for ordering — use {@link mat3x3Equals} there.
45
- */
46
- export function mat3x3IsClose(a, b, tolerance) {
47
- for (let row = 0; row < 3; row++) {
48
- for (let column = 0; column < 3; column++) {
49
- if (!isCloseTo(a[row][column], b[row][column], tolerance)) {
50
- return false;
51
- }
52
- }
1
+ import { dotPrecise3 } from "@ac-kit/math-scalar";
2
+ import { scaleForInversion_, unscaleInverse_ } from "./_invert.js";
3
+ /**
4
+ * Writes the product `a · b` into `out`: `b` is applied first. Not commutative.
5
+ * Plain arithmetic: each entry is within three roundings of the exact value
6
+ * relative to the sum of the absolute values of its terms, so it can lose every
7
+ * digit where the terms cancel (see {@link mat3x3MultiplyPrecise}). `out` may be
8
+ * `a` or `b`.
9
+ */
10
+ export function mat3x3Multiply(out, a, b) {
11
+ const a0 = a[0];
12
+ const a1 = a[1];
13
+ const a2 = a[2];
14
+ const a3 = a[3];
15
+ const a4 = a[4];
16
+ const a5 = a[5];
17
+ const a6 = a[6];
18
+ const a7 = a[7];
19
+ const a8 = a[8];
20
+ const b0 = b[0];
21
+ const b1 = b[1];
22
+ const b2 = b[2];
23
+ const b3 = b[3];
24
+ const b4 = b[4];
25
+ const b5 = b[5];
26
+ const b6 = b[6];
27
+ const b7 = b[7];
28
+ const b8 = b[8];
29
+ out[0] = a0 * b0 + a3 * b1 + a6 * b2;
30
+ out[1] = a1 * b0 + a4 * b1 + a7 * b2;
31
+ out[2] = a2 * b0 + a5 * b1 + a8 * b2;
32
+ out[3] = a0 * b3 + a3 * b4 + a6 * b5;
33
+ out[4] = a1 * b3 + a4 * b4 + a7 * b5;
34
+ out[5] = a2 * b3 + a5 * b4 + a8 * b5;
35
+ out[6] = a0 * b6 + a3 * b7 + a6 * b8;
36
+ out[7] = a1 * b6 + a4 * b7 + a7 * b8;
37
+ out[8] = a2 * b6 + a5 * b7 + a8 * b8;
38
+ return out;
39
+ }
40
+ /**
41
+ * {@link mat3x3Multiply} with each entry a compensated dot product, as accurate
42
+ * as one in doubled precision rounded once. `out` may be `a` or `b`.
43
+ */
44
+ export function mat3x3MultiplyPrecise(out, a, b) {
45
+ const a0 = a[0];
46
+ const a1 = a[1];
47
+ const a2 = a[2];
48
+ const a3 = a[3];
49
+ const a4 = a[4];
50
+ const a5 = a[5];
51
+ const a6 = a[6];
52
+ const a7 = a[7];
53
+ const a8 = a[8];
54
+ const b0 = b[0];
55
+ const b1 = b[1];
56
+ const b2 = b[2];
57
+ const b3 = b[3];
58
+ const b4 = b[4];
59
+ const b5 = b[5];
60
+ const b6 = b[6];
61
+ const b7 = b[7];
62
+ const b8 = b[8];
63
+ out[0] = dotPrecise3(a0, b0, a3, b1, a6, b2);
64
+ out[1] = dotPrecise3(a1, b0, a4, b1, a7, b2);
65
+ out[2] = dotPrecise3(a2, b0, a5, b1, a8, b2);
66
+ out[3] = dotPrecise3(a0, b3, a3, b4, a6, b5);
67
+ out[4] = dotPrecise3(a1, b3, a4, b4, a7, b5);
68
+ out[5] = dotPrecise3(a2, b3, a5, b4, a8, b5);
69
+ out[6] = dotPrecise3(a0, b6, a3, b7, a6, b8);
70
+ out[7] = dotPrecise3(a1, b6, a4, b7, a7, b8);
71
+ out[8] = dotPrecise3(a2, b6, a5, b7, a8, b8);
72
+ return out;
73
+ }
74
+ /**
75
+ * Writes `m · diag(v)` into `out`: column `c` of `m` multiplied by `v[c]`.
76
+ * `out` may be `m`.
77
+ */
78
+ export function mat3x3ScaleColumns(out, m, v) {
79
+ const v0 = v[0];
80
+ const v1 = v[1];
81
+ const v2 = v[2];
82
+ out[0] = m[0] * v0;
83
+ out[1] = m[1] * v0;
84
+ out[2] = m[2] * v0;
85
+ out[3] = m[3] * v1;
86
+ out[4] = m[4] * v1;
87
+ out[5] = m[5] * v1;
88
+ out[6] = m[6] * v2;
89
+ out[7] = m[7] * v2;
90
+ out[8] = m[8] * v2;
91
+ return out;
92
+ }
93
+ /**
94
+ * Writes `diag(v) · m` into `out`: row `r` of `m` multiplied by `v[r]`. `out`
95
+ * may be `m`.
96
+ */
97
+ export function mat3x3ScaleRows(out, m, v) {
98
+ const v0 = v[0];
99
+ const v1 = v[1];
100
+ const v2 = v[2];
101
+ out[0] = m[0] * v0;
102
+ out[1] = m[1] * v1;
103
+ out[2] = m[2] * v2;
104
+ out[3] = m[3] * v0;
105
+ out[4] = m[4] * v1;
106
+ out[5] = m[5] * v2;
107
+ out[6] = m[6] * v0;
108
+ out[7] = m[7] * v1;
109
+ out[8] = m[8] * v2;
110
+ return out;
111
+ }
112
+ /** Writes the transpose of `m` into `out`. `out` may be `m`. */
113
+ export function mat3x3Transpose(out, m) {
114
+ const m1 = m[1];
115
+ const m2 = m[2];
116
+ const m3 = m[3];
117
+ const m5 = m[5];
118
+ const m6 = m[6];
119
+ const m7 = m[7];
120
+ out[0] = m[0];
121
+ out[1] = m3;
122
+ out[2] = m6;
123
+ out[3] = m1;
124
+ out[4] = m[4];
125
+ out[5] = m7;
126
+ out[6] = m2;
127
+ out[7] = m5;
128
+ out[8] = m[8];
129
+ return out;
130
+ }
131
+ /**
132
+ * The determinant, by the rule of Sarrus-style cofactor expansion along the
133
+ * first column, in plain arithmetic: the signed volume scale of the
134
+ * transformation.
135
+ */
136
+ export function mat3x3Determinant(m) {
137
+ const m0 = m[0];
138
+ const m1 = m[1];
139
+ const m2 = m[2];
140
+ const m3 = m[3];
141
+ const m4 = m[4];
142
+ const m5 = m[5];
143
+ const m6 = m[6];
144
+ const m7 = m[7];
145
+ const m8 = m[8];
146
+ return (m0 * (m4 * m8 - m7 * m5) -
147
+ m3 * (m1 * m8 - m7 * m2) +
148
+ m6 * (m1 * m5 - m4 * m2));
149
+ }
150
+ /**
151
+ * Writes the inverse of `m` into `out` and returns `true`. When `m` has no
152
+ * inverse a binary64 number can hold (the determinant is zero, an entry is not
153
+ * finite, or an entry of the inverse overflows) `out` is filled with `NaN` and
154
+ * the result is `false`: `true` always comes with a finite `out`, never a stale
155
+ * or partial matrix, and nothing throws. A matrix of one very large or very
156
+ * small magnitude, whose determinant overflows or underflows, is inverted all
157
+ * the same (`diag(1e-100)` gives `diag(1e100)`), by scaling it by a power of
158
+ * two first. Plain arithmetic (the adjugate over the determinant). `out` may be
159
+ * `m`.
160
+ */
161
+ export function mat3x3Invert(out, m) {
162
+ if (invert_(out, m, false)) {
163
+ return true;
164
+ }
165
+ const exponent = scaleForInversion_(out, m);
166
+ if (!Number.isNaN(exponent) &&
167
+ invert_(out, out, true) &&
168
+ unscaleInverse_(out, exponent)) {
169
+ return true;
170
+ }
171
+ out[0] = Number.NaN;
172
+ out[1] = Number.NaN;
173
+ out[2] = Number.NaN;
174
+ out[3] = Number.NaN;
175
+ out[4] = Number.NaN;
176
+ out[5] = Number.NaN;
177
+ out[6] = Number.NaN;
178
+ out[7] = Number.NaN;
179
+ out[8] = Number.NaN;
180
+ return false;
181
+ }
182
+ /**
183
+ * The inverse by the adjugate over the determinant, written to `out` only when
184
+ * every entry is finite. The reciprocal of the determinant multiplies the
185
+ * adjugate; when it overflows (a subnormal determinant) the adjugate is divided
186
+ * by the determinant instead, if `divide`, and not otherwise.
187
+ */
188
+ function invert_(out, m, divide) {
189
+ const m0 = m[0];
190
+ const m1 = m[1];
191
+ const m2 = m[2];
192
+ const m3 = m[3];
193
+ const m4 = m[4];
194
+ const m5 = m[5];
195
+ const m6 = m[6];
196
+ const m7 = m[7];
197
+ const m8 = m[8];
198
+ const c0 = m4 * m8 - m7 * m5;
199
+ const c1 = m7 * m2 - m1 * m8;
200
+ const c2 = m1 * m5 - m4 * m2;
201
+ const c3 = m6 * m5 - m3 * m8;
202
+ const c4 = m0 * m8 - m6 * m2;
203
+ const c5 = m3 * m2 - m0 * m5;
204
+ const c6 = m3 * m7 - m6 * m4;
205
+ const c7 = m6 * m1 - m0 * m7;
206
+ const c8 = m0 * m4 - m3 * m1;
207
+ const determinant = m0 * c0 + m3 * c1 + m6 * c2;
208
+ if (determinant === 0 || !Number.isFinite(determinant)) {
209
+ return false;
210
+ }
211
+ const scale = 1 / determinant;
212
+ const reciprocal = Number.isFinite(scale);
213
+ if (!reciprocal && !divide) {
214
+ return false;
53
215
  }
216
+ const o0 = reciprocal ? c0 * scale : c0 / determinant;
217
+ const o1 = reciprocal ? c1 * scale : c1 / determinant;
218
+ const o2 = reciprocal ? c2 * scale : c2 / determinant;
219
+ const o3 = reciprocal ? c3 * scale : c3 / determinant;
220
+ const o4 = reciprocal ? c4 * scale : c4 / determinant;
221
+ const o5 = reciprocal ? c5 * scale : c5 / determinant;
222
+ const o6 = reciprocal ? c6 * scale : c6 / determinant;
223
+ const o7 = reciprocal ? c7 * scale : c7 / determinant;
224
+ const o8 = reciprocal ? c8 * scale : c8 / determinant;
225
+ if (!(Number.isFinite(o0) &&
226
+ Number.isFinite(o1) &&
227
+ Number.isFinite(o2) &&
228
+ Number.isFinite(o3) &&
229
+ Number.isFinite(o4) &&
230
+ Number.isFinite(o5) &&
231
+ Number.isFinite(o6) &&
232
+ Number.isFinite(o7) &&
233
+ Number.isFinite(o8))) {
234
+ return false;
235
+ }
236
+ out[0] = o0;
237
+ out[1] = o1;
238
+ out[2] = o2;
239
+ out[3] = o3;
240
+ out[4] = o4;
241
+ out[5] = o5;
242
+ out[6] = o6;
243
+ out[7] = o7;
244
+ out[8] = o8;
54
245
  return true;
55
246
  }
56
247
  /**
57
- * Creates a 3×3 rotation matrix that rotates around the X axis by the given
58
- * angle in radians (right-hand rule).
248
+ * Writes the rotation by `angle` radians about the x axis (right-hand rule)
249
+ * into `out`.
59
250
  */
60
- export function mat3x3FromRotationX(angle) {
251
+ export function mat3x3FromRotationX(out, angle) {
61
252
  const cos = Math.cos(angle);
62
253
  const sin = Math.sin(angle);
63
- return [
64
- [1, 0, 0],
65
- [0, cos, -sin],
66
- [0, sin, cos],
67
- ];
254
+ out[0] = 1;
255
+ out[1] = 0;
256
+ out[2] = 0;
257
+ out[3] = 0;
258
+ out[4] = cos;
259
+ out[5] = sin;
260
+ out[6] = 0;
261
+ out[7] = -sin;
262
+ out[8] = cos;
263
+ return out;
68
264
  }
69
265
  /**
70
- * Creates a 3×3 rotation matrix that rotates around the Y axis by the given
71
- * angle in radians (right-hand rule).
266
+ * Writes the rotation by `angle` radians about the y axis (right-hand rule)
267
+ * into `out`.
72
268
  */
73
- export function mat3x3FromRotationY(angle) {
269
+ export function mat3x3FromRotationY(out, angle) {
74
270
  const cos = Math.cos(angle);
75
271
  const sin = Math.sin(angle);
76
- return [
77
- [cos, 0, sin],
78
- [0, 1, 0],
79
- [-sin, 0, cos],
80
- ];
272
+ out[0] = cos;
273
+ out[1] = 0;
274
+ out[2] = -sin;
275
+ out[3] = 0;
276
+ out[4] = 1;
277
+ out[5] = 0;
278
+ out[6] = sin;
279
+ out[7] = 0;
280
+ out[8] = cos;
281
+ return out;
81
282
  }
82
283
  /**
83
- * Creates a 3×3 rotation matrix that rotates around the Z axis by the given
84
- * angle in radians (right-hand rule).
284
+ * Writes the rotation by `angle` radians about the z axis (right-hand rule)
285
+ * into `out`.
85
286
  */
86
- export function mat3x3FromRotationZ(angle) {
287
+ export function mat3x3FromRotationZ(out, angle) {
87
288
  const cos = Math.cos(angle);
88
289
  const sin = Math.sin(angle);
89
- return [
90
- [cos, -sin, 0],
91
- [sin, cos, 0],
92
- [0, 0, 1],
93
- ];
94
- }
95
- export function mat3x3FromScale(sx, sy, sz) {
96
- return [
97
- [sx, 0, 0],
98
- [0, sy, 0],
99
- [0, 0, sz],
100
- ];
101
- }
102
- export function mat3x3Invert(matrix) {
103
- const det = mat3x3Determinant(matrix);
104
- if (det === 0) {
105
- throw new Error("Matrix is singular and cannot be inverted.");
290
+ out[0] = cos;
291
+ out[1] = sin;
292
+ out[2] = 0;
293
+ out[3] = -sin;
294
+ out[4] = cos;
295
+ out[5] = 0;
296
+ out[6] = 0;
297
+ out[7] = 0;
298
+ out[8] = 1;
299
+ return out;
300
+ }
301
+ /** Writes the scale `diag(scale)` into `out`. */
302
+ export function mat3x3FromScale(out, scale) {
303
+ out[0] = scale[0];
304
+ out[1] = 0;
305
+ out[2] = 0;
306
+ out[3] = 0;
307
+ out[4] = scale[1];
308
+ out[5] = 0;
309
+ out[6] = 0;
310
+ out[7] = 0;
311
+ out[8] = scale[2];
312
+ return out;
313
+ }
314
+ /**
315
+ * Writes the rotation by `angle` radians about the unit vector `axis`
316
+ * (right-hand rule, Rodrigues' formula) into `out`. `axis` is not normalised
317
+ * here: for any other length the matrix is not a rotation.
318
+ */
319
+ export function mat3x3FromAxisAngle(out, axis, angle) {
320
+ const x = axis[0];
321
+ const y = axis[1];
322
+ const z = axis[2];
323
+ const sin = Math.sin(angle);
324
+ // 1 − cos as twice the squared half-angle sine, and the diagonal as one
325
+ // minus its share: exact in the first digits for the small angles where
326
+ // cos − 1 would lose them, and exactly 1 on the axis itself.
327
+ const half = Math.sin(angle / 2);
328
+ const rest = 2 * half * half;
329
+ out[0] = 1 - rest * (y * y + z * z);
330
+ out[1] = rest * x * y + sin * z;
331
+ out[2] = rest * x * z - sin * y;
332
+ out[3] = rest * x * y - sin * z;
333
+ out[4] = 1 - rest * (x * x + z * z);
334
+ out[5] = rest * y * z + sin * x;
335
+ out[6] = rest * x * z + sin * y;
336
+ out[7] = rest * y * z - sin * x;
337
+ out[8] = 1 - rest * (x * x + y * y);
338
+ return out;
339
+ }
340
+ /**
341
+ * Writes the rotation of the unit quaternion `q` into `out`. `q` must be unit
342
+ * length; it is not normalised here.
343
+ */
344
+ export function mat3x3FromQuaternion(out, q) {
345
+ const x = q[0];
346
+ const y = q[1];
347
+ const z = q[2];
348
+ const w = q[3];
349
+ const xx = x * x;
350
+ const yy = y * y;
351
+ const zz = z * z;
352
+ const xy = x * y;
353
+ const xz = x * z;
354
+ const yz = y * z;
355
+ const wx = w * x;
356
+ const wy = w * y;
357
+ const wz = w * z;
358
+ out[0] = 1 - 2 * (yy + zz);
359
+ out[1] = 2 * (xy + wz);
360
+ out[2] = 2 * (xz - wy);
361
+ out[3] = 2 * (xy - wz);
362
+ out[4] = 1 - 2 * (xx + zz);
363
+ out[5] = 2 * (yz + wx);
364
+ out[6] = 2 * (xz + wy);
365
+ out[7] = 2 * (yz - wx);
366
+ out[8] = 1 - 2 * (xx + yy);
367
+ return out;
368
+ }
369
+ /** Writes the upper-left 3 × 3 block of `m`: its linear part, into `out`. */
370
+ export function mat3x3FromMat4x4(out, m) {
371
+ out[0] = m[0];
372
+ out[1] = m[1];
373
+ out[2] = m[2];
374
+ out[3] = m[4];
375
+ out[4] = m[5];
376
+ out[5] = m[6];
377
+ out[6] = m[8];
378
+ out[7] = m[9];
379
+ out[8] = m[10];
380
+ return out;
381
+ }
382
+ /** Writes the linear part of `transform`, without its translation, into `out`. */
383
+ export function mat3x3FromAffine3(out, transform) {
384
+ out[0] = transform[0];
385
+ out[1] = transform[1];
386
+ out[2] = transform[2];
387
+ out[3] = transform[3];
388
+ out[4] = transform[4];
389
+ out[5] = transform[5];
390
+ out[6] = transform[6];
391
+ out[7] = transform[7];
392
+ out[8] = transform[8];
393
+ return out;
394
+ }
395
+ /**
396
+ * Writes the 2D affine `transform` as the homogeneous 3 × 3 matrix `[a, c, e;
397
+ * b, d, f; 0, 0, 1]` into `out`: the bottom row, which an {@link Affine2} does
398
+ * not store, is `0, 0, 1`.
399
+ */
400
+ export function mat3x3FromAffine2(out, transform) {
401
+ out[0] = transform[0];
402
+ out[1] = transform[1];
403
+ out[2] = 0;
404
+ out[3] = transform[2];
405
+ out[4] = transform[3];
406
+ out[5] = 0;
407
+ out[6] = transform[4];
408
+ out[7] = transform[5];
409
+ out[8] = 1;
410
+ return out;
411
+ }
412
+ /**
413
+ * Turns the matrix in `out` into its inverse transpose `(A⁻¹)ᵀ`, the normal
414
+ * matrix, through {@link mat3x3Invert}: false with `NaN` throughout when there
415
+ * is no finite inverse.
416
+ */
417
+ function normalInPlace_(out) {
418
+ if (!mat3x3Invert(out, out)) {
419
+ return false;
106
420
  }
107
- const [[a, b, c], [d, e, f], [g, h, i]] = matrix;
108
- const adjugate = [
109
- [
110
- diffOfProducts(e, i, f, h),
111
- diffOfProducts(c, h, b, i),
112
- diffOfProducts(b, f, c, e),
113
- ],
114
- [
115
- diffOfProducts(f, g, d, i),
116
- diffOfProducts(a, i, c, g),
117
- diffOfProducts(c, d, a, f),
118
- ],
119
- [
120
- diffOfProducts(d, h, e, g),
121
- diffOfProducts(b, g, a, h),
122
- diffOfProducts(a, e, b, d),
123
- ],
124
- ];
125
- return mat3x3MultiplyScalar(adjugate, 1 / det);
126
- }
127
- export function mat3x3Multiply(a, b) {
128
- const columns = mat3x3Transpose(b);
129
- return [
130
- [
131
- dotPrecise(a[0], columns[0]),
132
- dotPrecise(a[0], columns[1]),
133
- dotPrecise(a[0], columns[2]),
134
- ],
135
- [
136
- dotPrecise(a[1], columns[0]),
137
- dotPrecise(a[1], columns[1]),
138
- dotPrecise(a[1], columns[2]),
139
- ],
140
- [
141
- dotPrecise(a[2], columns[0]),
142
- dotPrecise(a[2], columns[1]),
143
- dotPrecise(a[2], columns[2]),
144
- ],
145
- ];
146
- }
147
- export function mat3x3MultiplyCols(matrix, vector) {
148
- return [
149
- [
150
- matrix[0][0] * vector[0],
151
- matrix[0][1] * vector[1],
152
- matrix[0][2] * vector[2],
153
- ],
154
- [
155
- matrix[1][0] * vector[0],
156
- matrix[1][1] * vector[1],
157
- matrix[1][2] * vector[2],
158
- ],
159
- [
160
- matrix[2][0] * vector[0],
161
- matrix[2][1] * vector[1],
162
- matrix[2][2] * vector[2],
163
- ],
164
- ];
165
- }
166
- export function mat3x3MultiplyRows(matrix, vector) {
167
- return [
168
- [
169
- matrix[0][0] * vector[0],
170
- matrix[0][1] * vector[0],
171
- matrix[0][2] * vector[0],
172
- ],
173
- [
174
- matrix[1][0] * vector[1],
175
- matrix[1][1] * vector[1],
176
- matrix[1][2] * vector[1],
177
- ],
178
- [
179
- matrix[2][0] * vector[2],
180
- matrix[2][1] * vector[2],
181
- matrix[2][2] * vector[2],
182
- ],
183
- ];
184
- }
185
- export function mat3x3MultiplyScalar(matrix, scalar) {
186
- return [
187
- [matrix[0][0] * scalar, matrix[0][1] * scalar, matrix[0][2] * scalar],
188
- [matrix[1][0] * scalar, matrix[1][1] * scalar, matrix[1][2] * scalar],
189
- [matrix[2][0] * scalar, matrix[2][1] * scalar, matrix[2][2] * scalar],
190
- ];
191
- }
192
- export function mat3x3Negate(matrix) {
193
- return [
194
- [-matrix[0][0], -matrix[0][1], -matrix[0][2]],
195
- [-matrix[1][0], -matrix[1][1], -matrix[1][2]],
196
- [-matrix[2][0], -matrix[2][1], -matrix[2][2]],
197
- ];
198
- }
199
- export function mat3x3Sub(a, b) {
200
- return [
201
- [a[0][0] - b[0][0], a[0][1] - b[0][1], a[0][2] - b[0][2]],
202
- [a[1][0] - b[1][0], a[1][1] - b[1][1], a[1][2] - b[1][2]],
203
- [a[2][0] - b[2][0], a[2][1] - b[2][1], a[2][2] - b[2][2]],
204
- ];
205
- }
206
- export function mat3x3Transpose(matrix) {
207
- return [
208
- [matrix[0][0], matrix[1][0], matrix[2][0]],
209
- [matrix[0][1], matrix[1][1], matrix[2][1]],
210
- [matrix[0][2], matrix[1][2], matrix[2][2]],
211
- ];
421
+ const m1 = out[1];
422
+ const m2 = out[2];
423
+ const m5 = out[5];
424
+ out[1] = out[3];
425
+ out[2] = out[6];
426
+ out[5] = out[7];
427
+ out[3] = m1;
428
+ out[6] = m2;
429
+ out[7] = m5;
430
+ return true;
431
+ }
432
+ /**
433
+ * Writes the normal matrix of `m` into `out`: the inverse transpose of its
434
+ * upper-left 3 × 3 block, the matrix that carries surface normals through `m`
435
+ * (they stay perpendicular to the surface even under a non-uniform scale; scale
436
+ * the result to unit length afterwards). Returns `false` when that block has no
437
+ * finite inverse (it is singular, not finite, or the inverse overflows): `out`
438
+ * then holds `NaN` throughout, never a stale or partial matrix, and nothing
439
+ * throws. Plain arithmetic, as {@link mat3x3Invert}.
440
+ */
441
+ export function mat3x3NormalFromMat4x4(out, m) {
442
+ mat3x3FromMat4x4(out, m);
443
+ return normalInPlace_(out);
444
+ }
445
+ /**
446
+ * Writes the normal matrix of `transform` into `out`: the inverse transpose of
447
+ * its linear part, as {@link mat3x3NormalFromMat4x4}. Returns `false`, with
448
+ * `NaN` throughout, when the linear part has no finite inverse.
449
+ */
450
+ export function mat3x3NormalFromAffine3(out, transform) {
451
+ mat3x3FromAffine3(out, transform);
452
+ return normalInPlace_(out);
212
453
  }
213
454
  //# sourceMappingURL=mat3x3.js.map