@ac-kit/math-linear 0.2.1 → 0.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.
- package/CHANGELOG.md +337 -0
- package/README.md +232 -16
- package/dist/_brand.d.ts +14 -0
- package/dist/_brand.d.ts.map +1 -0
- package/dist/_brand.js +2 -0
- package/dist/_brand.js.map +1 -0
- package/dist/_invert.d.ts +214 -0
- package/dist/_invert.d.ts.map +1 -0
- package/dist/_invert.js +884 -0
- package/dist/_invert.js.map +1 -0
- package/dist/_slerp.d.ts +29 -0
- package/dist/_slerp.d.ts.map +1 -1
- package/dist/_slerp.js +103 -0
- package/dist/_slerp.js.map +1 -1
- package/dist/_strided-range.d.ts +23 -0
- package/dist/_strided-range.d.ts.map +1 -0
- package/dist/_strided-range.js +75 -0
- package/dist/_strided-range.js.map +1 -0
- package/dist/_trs.d.ts +38 -0
- package/dist/_trs.d.ts.map +1 -0
- package/dist/_trs.js +132 -0
- package/dist/_trs.js.map +1 -0
- package/dist/affine2-array.d.ts +53 -0
- package/dist/affine2-array.d.ts.map +1 -0
- package/dist/affine2-array.js +107 -0
- package/dist/affine2-array.js.map +1 -0
- package/dist/affine2-componentwise.d.ts +28 -0
- package/dist/affine2-componentwise.d.ts.map +1 -0
- package/dist/affine2-componentwise.js +66 -0
- package/dist/affine2-componentwise.js.map +1 -0
- package/dist/affine2.d.ts +156 -0
- package/dist/affine2.d.ts.map +1 -0
- package/dist/affine2.js +437 -0
- package/dist/affine2.js.map +1 -0
- package/dist/affine3-array.d.ts +53 -0
- package/dist/affine3-array.d.ts.map +1 -0
- package/dist/affine3-array.js +137 -0
- package/dist/affine3-array.js.map +1 -0
- package/dist/affine3-componentwise.d.ts +32 -0
- package/dist/affine3-componentwise.d.ts.map +1 -0
- package/dist/affine3-componentwise.js +100 -0
- package/dist/affine3-componentwise.js.map +1 -0
- package/dist/affine3.d.ts +205 -0
- package/dist/affine3.d.ts.map +1 -0
- package/dist/affine3.js +792 -0
- package/dist/affine3.js.map +1 -0
- package/dist/checked/index.d.ts +35 -0
- package/dist/checked/index.d.ts.map +1 -0
- package/dist/checked/index.js +58 -0
- package/dist/checked/index.js.map +1 -0
- package/dist/euler.d.ts +65 -0
- package/dist/euler.d.ts.map +1 -0
- package/dist/euler.js +217 -0
- package/dist/euler.js.map +1 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -1
- package/dist/kl-activations.d.ts.map +1 -1
- package/dist/kl-activations.js +4 -3
- package/dist/kl-activations.js.map +1 -1
- package/dist/mat2x2-array.d.ts +36 -0
- package/dist/mat2x2-array.d.ts.map +1 -0
- package/dist/mat2x2-array.js +71 -0
- package/dist/mat2x2-array.js.map +1 -0
- package/dist/mat2x2-componentwise.d.ts +86 -0
- package/dist/mat2x2-componentwise.d.ts.map +1 -0
- package/dist/mat2x2-componentwise.js +199 -0
- package/dist/mat2x2-componentwise.js.map +1 -0
- package/dist/mat2x2.d.ts +79 -25
- package/dist/mat2x2.d.ts.map +1 -1
- package/dist/mat2x2.js +192 -93
- package/dist/mat2x2.js.map +1 -1
- package/dist/mat3x3-array.d.ts +36 -0
- package/dist/mat3x3-array.d.ts.map +1 -0
- package/dist/mat3x3-array.js +91 -0
- package/dist/mat3x3-array.js.map +1 -0
- package/dist/mat3x3-componentwise.d.ts +86 -0
- package/dist/mat3x3-componentwise.d.ts.map +1 -0
- package/dist/mat3x3-componentwise.js +299 -0
- package/dist/mat3x3-componentwise.js.map +1 -0
- package/dist/mat3x3.d.ts +153 -48
- package/dist/mat3x3.d.ts.map +1 -1
- package/dist/mat3x3.js +470 -192
- package/dist/mat3x3.js.map +1 -1
- package/dist/mat4x4-array.d.ts +46 -0
- package/dist/mat4x4-array.d.ts.map +1 -0
- package/dist/mat4x4-array.js +133 -0
- package/dist/mat4x4-array.js.map +1 -0
- package/dist/mat4x4-componentwise.d.ts +87 -0
- package/dist/mat4x4-componentwise.d.ts.map +1 -0
- package/dist/mat4x4-componentwise.js +434 -0
- package/dist/mat4x4-componentwise.js.map +1 -0
- package/dist/mat4x4.d.ts +221 -67
- package/dist/mat4x4.d.ts.map +1 -1
- package/dist/mat4x4.js +972 -334
- package/dist/mat4x4.js.map +1 -1
- package/dist/projection.d.ts +113 -0
- package/dist/projection.d.ts.map +1 -0
- package/dist/projection.js +418 -0
- package/dist/projection.js.map +1 -0
- package/dist/quaternion-array.d.ts +47 -0
- package/dist/quaternion-array.d.ts.map +1 -0
- package/dist/quaternion-array.js +86 -0
- package/dist/quaternion-array.js.map +1 -0
- package/dist/quaternion.d.ts +106 -22
- package/dist/quaternion.d.ts.map +1 -1
- package/dist/quaternion.js +253 -182
- package/dist/quaternion.js.map +1 -1
- package/dist/transform.d.ts +220 -0
- package/dist/transform.d.ts.map +1 -0
- package/dist/transform.js +412 -0
- package/dist/transform.js.map +1 -0
- package/dist/tridiagonal.d.ts +52 -0
- package/dist/tridiagonal.d.ts.map +1 -0
- package/dist/tridiagonal.js +90 -0
- package/dist/tridiagonal.js.map +1 -0
- package/dist/upload.d.ts +55 -0
- package/dist/upload.d.ts.map +1 -0
- package/dist/upload.js +129 -0
- package/dist/upload.js.map +1 -0
- package/dist/vec2-array-componentwise.d.ts +207 -0
- package/dist/vec2-array-componentwise.d.ts.map +1 -0
- package/dist/vec2-array-componentwise.js +472 -0
- package/dist/vec2-array-componentwise.js.map +1 -0
- package/dist/vec2-array.d.ts +53 -0
- package/dist/vec2-array.d.ts.map +1 -0
- package/dist/vec2-array.js +123 -0
- package/dist/vec2-array.js.map +1 -0
- package/dist/vec2-componentwise.d.ts +311 -0
- package/dist/vec2-componentwise.d.ts.map +1 -0
- package/dist/vec2-componentwise.js +542 -0
- package/dist/vec2-componentwise.js.map +1 -0
- package/dist/vec2.d.ts +97 -82
- package/dist/vec2.d.ts.map +1 -1
- package/dist/vec2.js +121 -216
- package/dist/vec2.js.map +1 -1
- package/dist/vec3-array-componentwise.d.ts +207 -0
- package/dist/vec3-array-componentwise.d.ts.map +1 -0
- package/dist/vec3-array-componentwise.js +510 -0
- package/dist/vec3-array-componentwise.js.map +1 -0
- package/dist/vec3-array.d.ts +102 -0
- package/dist/vec3-array.d.ts.map +1 -0
- package/dist/vec3-array.js +299 -0
- package/dist/vec3-array.js.map +1 -0
- package/dist/vec3-componentwise.d.ts +310 -0
- package/dist/vec3-componentwise.d.ts.map +1 -0
- package/dist/vec3-componentwise.js +594 -0
- package/dist/vec3-componentwise.js.map +1 -0
- package/dist/vec3.d.ts +177 -87
- package/dist/vec3.d.ts.map +1 -1
- package/dist/vec3.js +391 -272
- package/dist/vec3.js.map +1 -1
- package/dist/vec4-array-componentwise.d.ts +198 -0
- package/dist/vec4-array-componentwise.d.ts.map +1 -0
- package/dist/vec4-array-componentwise.js +521 -0
- package/dist/vec4-array-componentwise.js.map +1 -0
- package/dist/vec4-array.d.ts +12 -0
- package/dist/vec4-array.d.ts.map +1 -0
- package/dist/vec4-array.js +43 -0
- package/dist/vec4-array.js.map +1 -0
- package/dist/vec4-componentwise.d.ts +299 -0
- package/dist/vec4-componentwise.d.ts.map +1 -0
- package/dist/vec4-componentwise.js +637 -0
- package/dist/vec4-componentwise.js.map +1 -0
- package/dist/vec4.d.ts +59 -53
- package/dist/vec4.d.ts.map +1 -1
- package/dist/vec4.js +74 -249
- package/dist/vec4.js.map +1 -1
- package/dist/view-basis.d.ts +15 -0
- package/dist/view-basis.d.ts.map +1 -0
- package/dist/view-basis.js +29 -0
- package/dist/view-basis.js.map +1 -0
- package/dist/viewport.d.ts +114 -0
- package/dist/viewport.d.ts.map +1 -0
- package/dist/viewport.js +250 -0
- package/dist/viewport.js.map +1 -0
- package/package.json +19 -9
package/dist/affine3.js
ADDED
|
@@ -0,0 +1,792 @@
|
|
|
1
|
+
import { numberAtOrNaN } from "@ac-kit/core";
|
|
2
|
+
import { BOUND_INPUT_, INVERSE_LIMIT_, isWithinTolerance3_, ROUNDING_TOLERANCE_, UNDERFLOW_TOLERANCE_, UNDERFLOW_UNIT_, UNIT_ROUNDOFF_, writeInverseTranslation_, } from "./_invert.js";
|
|
3
|
+
import { decomposeLinear_ } from "./_trs.js";
|
|
4
|
+
import { mat3x3Create } from "./mat3x3-componentwise.js";
|
|
5
|
+
import { mat3x3Invert } from "./mat3x3.js";
|
|
6
|
+
import { mat4x4Create } from "./mat4x4-componentwise.js";
|
|
7
|
+
import { mat4x4Invert } from "./mat4x4.js";
|
|
8
|
+
/**
|
|
9
|
+
* Every creator copies this array, which holds non-integer doubles and is never
|
|
10
|
+
* exposed: nothing a caller does can change the element kind it copies.
|
|
11
|
+
*/
|
|
12
|
+
const TEMPLATE_ = [
|
|
13
|
+
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
|
|
14
|
+
];
|
|
15
|
+
/**
|
|
16
|
+
* Creates the identity transform.
|
|
17
|
+
*
|
|
18
|
+
* Allocates; keep it out of hot loops.
|
|
19
|
+
*/
|
|
20
|
+
export function affine3Create() {
|
|
21
|
+
const out = TEMPLATE_.slice();
|
|
22
|
+
out[0] = 1;
|
|
23
|
+
out[1] = 0;
|
|
24
|
+
out[2] = 0;
|
|
25
|
+
out[3] = 0;
|
|
26
|
+
out[4] = 1;
|
|
27
|
+
out[5] = 0;
|
|
28
|
+
out[6] = 0;
|
|
29
|
+
out[7] = 0;
|
|
30
|
+
out[8] = 1;
|
|
31
|
+
out[9] = 0;
|
|
32
|
+
out[10] = 0;
|
|
33
|
+
out[11] = 0;
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Creates the transform with the entries in storage order, the columns one
|
|
38
|
+
* after the other: `m00, m10, m20` is the x axis, `m03, m13, m23` the
|
|
39
|
+
* translation.
|
|
40
|
+
*
|
|
41
|
+
* Allocates; keep it out of hot loops.
|
|
42
|
+
*/
|
|
43
|
+
export function affine3Of(m00, m10, m20, m01, m11, m21, m02, m12, m22, m03, m13, m23) {
|
|
44
|
+
const out = TEMPLATE_.slice();
|
|
45
|
+
out[0] = m00;
|
|
46
|
+
out[1] = m10;
|
|
47
|
+
out[2] = m20;
|
|
48
|
+
out[3] = m01;
|
|
49
|
+
out[4] = m11;
|
|
50
|
+
out[5] = m21;
|
|
51
|
+
out[6] = m02;
|
|
52
|
+
out[7] = m12;
|
|
53
|
+
out[8] = m22;
|
|
54
|
+
out[9] = m03;
|
|
55
|
+
out[10] = m13;
|
|
56
|
+
out[11] = m23;
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Creates the transform from the three rows of its 3 × 4 matrix, written as in
|
|
61
|
+
* a textbook: `m00, m01, m02, m03` is the first row, whose last entry is the x
|
|
62
|
+
* translation.
|
|
63
|
+
*
|
|
64
|
+
* Allocates; keep it out of hot loops.
|
|
65
|
+
*/
|
|
66
|
+
export function affine3OfRows(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23) {
|
|
67
|
+
return affine3Of(m00, m10, m20, m01, m11, m21, m02, m12, m22, m03, m13, m23);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Creates a copy of `a`. Like every creator it starts from a clean array.
|
|
71
|
+
*
|
|
72
|
+
* Allocates; keep it out of hot loops.
|
|
73
|
+
*/
|
|
74
|
+
export function affine3Clone(a) {
|
|
75
|
+
const out = TEMPLATE_.slice();
|
|
76
|
+
out[0] = a[0];
|
|
77
|
+
out[1] = a[1];
|
|
78
|
+
out[2] = a[2];
|
|
79
|
+
out[3] = a[3];
|
|
80
|
+
out[4] = a[4];
|
|
81
|
+
out[5] = a[5];
|
|
82
|
+
out[6] = a[6];
|
|
83
|
+
out[7] = a[7];
|
|
84
|
+
out[8] = a[8];
|
|
85
|
+
out[9] = a[9];
|
|
86
|
+
out[10] = a[10];
|
|
87
|
+
out[11] = a[11];
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The checked way in from untyped data (JSON, messages, arrays of unknown
|
|
92
|
+
* origin): `source[offset … offset + 12)` copied into `out`, each element that
|
|
93
|
+
* is not a number written as `NaN`, an index past the end of `source` too, so
|
|
94
|
+
* `out` never holds a non-number. A cold path. O(1).
|
|
95
|
+
*
|
|
96
|
+
* `source` may be `out` itself: the twelve elements are read before the first
|
|
97
|
+
* write.
|
|
98
|
+
*
|
|
99
|
+
* @param out The transform to fill.
|
|
100
|
+
* @param source The array to read, of any length and any content.
|
|
101
|
+
* @param offset The index of the first entry in `source`.
|
|
102
|
+
* @returns `out`.
|
|
103
|
+
*/
|
|
104
|
+
export function affine3FromArray(out, source, offset = 0) {
|
|
105
|
+
const m00 = numberAtOrNaN(source, offset);
|
|
106
|
+
const m10 = numberAtOrNaN(source, offset + 1);
|
|
107
|
+
const m20 = numberAtOrNaN(source, offset + 2);
|
|
108
|
+
const m01 = numberAtOrNaN(source, offset + 3);
|
|
109
|
+
const m11 = numberAtOrNaN(source, offset + 4);
|
|
110
|
+
const m21 = numberAtOrNaN(source, offset + 5);
|
|
111
|
+
const m02 = numberAtOrNaN(source, offset + 6);
|
|
112
|
+
const m12 = numberAtOrNaN(source, offset + 7);
|
|
113
|
+
const m22 = numberAtOrNaN(source, offset + 8);
|
|
114
|
+
const m03 = numberAtOrNaN(source, offset + 9);
|
|
115
|
+
const m13 = numberAtOrNaN(source, offset + 10);
|
|
116
|
+
const m23 = numberAtOrNaN(source, offset + 11);
|
|
117
|
+
out[0] = m00;
|
|
118
|
+
out[1] = m10;
|
|
119
|
+
out[2] = m20;
|
|
120
|
+
out[3] = m01;
|
|
121
|
+
out[4] = m11;
|
|
122
|
+
out[5] = m21;
|
|
123
|
+
out[6] = m02;
|
|
124
|
+
out[7] = m12;
|
|
125
|
+
out[8] = m22;
|
|
126
|
+
out[9] = m03;
|
|
127
|
+
out[10] = m13;
|
|
128
|
+
out[11] = m23;
|
|
129
|
+
return out;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Whether every entry is a finite number: the check to run after parsing
|
|
133
|
+
* untrusted data or before an upload, as operations never check. An entry that
|
|
134
|
+
* is not a number at all counts as not finite.
|
|
135
|
+
*/
|
|
136
|
+
export function affine3IsFinite(a) {
|
|
137
|
+
return (Number.isFinite(a[0]) &&
|
|
138
|
+
Number.isFinite(a[1]) &&
|
|
139
|
+
Number.isFinite(a[2]) &&
|
|
140
|
+
Number.isFinite(a[3]) &&
|
|
141
|
+
Number.isFinite(a[4]) &&
|
|
142
|
+
Number.isFinite(a[5]) &&
|
|
143
|
+
Number.isFinite(a[6]) &&
|
|
144
|
+
Number.isFinite(a[7]) &&
|
|
145
|
+
Number.isFinite(a[8]) &&
|
|
146
|
+
Number.isFinite(a[9]) &&
|
|
147
|
+
Number.isFinite(a[10]) &&
|
|
148
|
+
Number.isFinite(a[11]));
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Writes the composition `a · b` into `out`: `b` is applied first, then `a`.
|
|
152
|
+
* Not commutative. Plain arithmetic, straight-line: the entries of the product
|
|
153
|
+
* of the two 4 × 4 matrices that these stand for, without the bottom row's
|
|
154
|
+
* zeros. `out` may be `a` or `b`.
|
|
155
|
+
*/
|
|
156
|
+
export function affine3Multiply(out, a, b) {
|
|
157
|
+
const a0 = a[0];
|
|
158
|
+
const a1 = a[1];
|
|
159
|
+
const a2 = a[2];
|
|
160
|
+
const a3 = a[3];
|
|
161
|
+
const a4 = a[4];
|
|
162
|
+
const a5 = a[5];
|
|
163
|
+
const a6 = a[6];
|
|
164
|
+
const a7 = a[7];
|
|
165
|
+
const a8 = a[8];
|
|
166
|
+
const a9 = a[9];
|
|
167
|
+
const a10 = a[10];
|
|
168
|
+
const a11 = a[11];
|
|
169
|
+
const b0 = b[0];
|
|
170
|
+
const b1 = b[1];
|
|
171
|
+
const b2 = b[2];
|
|
172
|
+
const b3 = b[3];
|
|
173
|
+
const b4 = b[4];
|
|
174
|
+
const b5 = b[5];
|
|
175
|
+
const b6 = b[6];
|
|
176
|
+
const b7 = b[7];
|
|
177
|
+
const b8 = b[8];
|
|
178
|
+
const b9 = b[9];
|
|
179
|
+
const b10 = b[10];
|
|
180
|
+
const b11 = b[11];
|
|
181
|
+
out[0] = a0 * b0 + a3 * b1 + a6 * b2;
|
|
182
|
+
out[1] = a1 * b0 + a4 * b1 + a7 * b2;
|
|
183
|
+
out[2] = a2 * b0 + a5 * b1 + a8 * b2;
|
|
184
|
+
out[3] = a0 * b3 + a3 * b4 + a6 * b5;
|
|
185
|
+
out[4] = a1 * b3 + a4 * b4 + a7 * b5;
|
|
186
|
+
out[5] = a2 * b3 + a5 * b4 + a8 * b5;
|
|
187
|
+
out[6] = a0 * b6 + a3 * b7 + a6 * b8;
|
|
188
|
+
out[7] = a1 * b6 + a4 * b7 + a7 * b8;
|
|
189
|
+
out[8] = a2 * b6 + a5 * b7 + a8 * b8;
|
|
190
|
+
out[9] = a0 * b9 + a3 * b10 + a6 * b11 + a9;
|
|
191
|
+
out[10] = a1 * b9 + a4 * b10 + a7 * b11 + a10;
|
|
192
|
+
out[11] = a2 * b9 + a5 * b10 + a8 * b11 + a11;
|
|
193
|
+
return out;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* The determinant of the linear part: the signed volume scale of the transform,
|
|
197
|
+
* negative for a reflection. Plain arithmetic.
|
|
198
|
+
*/
|
|
199
|
+
export function affine3Determinant(a) {
|
|
200
|
+
const a0 = a[0];
|
|
201
|
+
const a1 = a[1];
|
|
202
|
+
const a2 = a[2];
|
|
203
|
+
const a3 = a[3];
|
|
204
|
+
const a4 = a[4];
|
|
205
|
+
const a5 = a[5];
|
|
206
|
+
const a6 = a[6];
|
|
207
|
+
const a7 = a[7];
|
|
208
|
+
const a8 = a[8];
|
|
209
|
+
return (a0 * (a4 * a8 - a5 * a7) +
|
|
210
|
+
a1 * (a5 * a6 - a3 * a8) +
|
|
211
|
+
a2 * (a3 * a7 - a4 * a6));
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Scratch for {@link affine3Invert}'s second attempt: the linear part, and its
|
|
215
|
+
* inverse. One call at a time, no callback inside it.
|
|
216
|
+
*/
|
|
217
|
+
const LINEAR_PART_ = mat3x3Create();
|
|
218
|
+
const LINEAR_INVERSE_ = mat3x3Create();
|
|
219
|
+
const HOMOGENEOUS_ = mat4x4Create();
|
|
220
|
+
const HOMOGENEOUS_INVERSE_ = mat4x4Create();
|
|
221
|
+
/**
|
|
222
|
+
* Whether the inverse of the homogeneous matrix of `a` rounds to finite
|
|
223
|
+
* numbers, by {@link mat4x4Invert}: the verdict on the translation that
|
|
224
|
+
* `writeInverseTranslation_` takes when its own bound cannot give one.
|
|
225
|
+
*/
|
|
226
|
+
function isHomogeneousInvertible_(a) {
|
|
227
|
+
for (let column = 0; column < 4; column++) {
|
|
228
|
+
for (let row = 0; row < 3; row++) {
|
|
229
|
+
HOMOGENEOUS_[column * 4 + row] = a[column * 3 + row];
|
|
230
|
+
}
|
|
231
|
+
HOMOGENEOUS_[column * 4 + 3] = column === 3 ? 1 : 0;
|
|
232
|
+
}
|
|
233
|
+
return mat4x4Invert(HOMOGENEOUS_INVERSE_, HOMOGENEOUS_);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Writes the inverse transform of `a` into `out` and returns `true`. When the
|
|
237
|
+
* determinant of the linear part is zero or not finite, or the inverse does not
|
|
238
|
+
* fit in binary64, the transform has no usable inverse: `out` is filled with
|
|
239
|
+
* `NaN` and the result is `false`: `true` always comes with a finite `out`,
|
|
240
|
+
* never a stale or partial transform, and nothing throws. A transform whose
|
|
241
|
+
* linear part has very large or very small entries, whose determinant or
|
|
242
|
+
* cofactors overflow or underflow, is inverted all the same (a uniform scale of
|
|
243
|
+
* `1e-100` gives `1e100`; scales of `2^600`, `2^599` and `2^600` with a
|
|
244
|
+
* translation of `Number.MAX_VALUE` invert too), by inverting the linear part
|
|
245
|
+
* as {@link mat3x3Invert} does and taking the translation from that unscaled
|
|
246
|
+
* inverse. Plain arithmetic (the adjugate over the determinant). Each
|
|
247
|
+
* translation component is `−B · t` for the returned linear part `B` and the
|
|
248
|
+
* translation `t`: in plain arithmetic when a forward error bound keeps it
|
|
249
|
+
* within `2^-41` of itself, and otherwise correctly rounded (`dotExactInto`),
|
|
250
|
+
* however its terms cancel (a linear part whose inverse starts with the row
|
|
251
|
+
* `(1, 1, 1)` and a translation of `(Number.MAX_VALUE, 1, −Number.MAX_VALUE)`
|
|
252
|
+
* give `−1`, not `0`). Whether it overflows is decided on the exact `−A⁻¹ · t`,
|
|
253
|
+
* never on the rounded `B`: a component that a rigorous bound cannot keep from
|
|
254
|
+
* the overflow threshold is settled in exact arithmetic, and is then `−A⁻¹ · t`
|
|
255
|
+
* rounded once (an x scale of `161/256` with an x translation of `161 · 2^1016`
|
|
256
|
+
* is refused, its exact `−2^1024` overflowing where `−B · t` rounds to
|
|
257
|
+
* `−Number.MAX_VALUE`). `out` may be `a`.
|
|
258
|
+
*/
|
|
259
|
+
export function affine3Invert(out, a) {
|
|
260
|
+
if (invert_(out, a, false)) {
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
for (let index = 0; index < 9; index++) {
|
|
264
|
+
LINEAR_PART_[index] = a[index];
|
|
265
|
+
}
|
|
266
|
+
if (mat3x3Invert(LINEAR_INVERSE_, LINEAR_PART_) &&
|
|
267
|
+
writeInverseTranslation_(out, 9, LINEAR_INVERSE_, 3, a, isHomogeneousInvertible_)) {
|
|
268
|
+
for (let index = 0; index < 9; index++) {
|
|
269
|
+
out[index] = LINEAR_INVERSE_[index];
|
|
270
|
+
}
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
out[0] = Number.NaN;
|
|
274
|
+
out[1] = Number.NaN;
|
|
275
|
+
out[2] = Number.NaN;
|
|
276
|
+
out[3] = Number.NaN;
|
|
277
|
+
out[4] = Number.NaN;
|
|
278
|
+
out[5] = Number.NaN;
|
|
279
|
+
out[6] = Number.NaN;
|
|
280
|
+
out[7] = Number.NaN;
|
|
281
|
+
out[8] = Number.NaN;
|
|
282
|
+
out[9] = Number.NaN;
|
|
283
|
+
out[10] = Number.NaN;
|
|
284
|
+
out[11] = Number.NaN;
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* The inverse by the adjugate over the determinant, written to `out` only when
|
|
289
|
+
* every entry is finite. The reciprocal of the determinant multiplies the
|
|
290
|
+
* adjugate; when it overflows (a subnormal determinant) the adjugate is divided
|
|
291
|
+
* by the determinant instead, if `divide`, and not otherwise.
|
|
292
|
+
*/
|
|
293
|
+
function invert_(out, a, divide) {
|
|
294
|
+
const a0 = a[0];
|
|
295
|
+
const a1 = a[1];
|
|
296
|
+
const a2 = a[2];
|
|
297
|
+
const a3 = a[3];
|
|
298
|
+
const a4 = a[4];
|
|
299
|
+
const a5 = a[5];
|
|
300
|
+
const a6 = a[6];
|
|
301
|
+
const a7 = a[7];
|
|
302
|
+
const a8 = a[8];
|
|
303
|
+
const a9 = a[9];
|
|
304
|
+
const a10 = a[10];
|
|
305
|
+
const a11 = a[11];
|
|
306
|
+
const r0x = a4 * a8 - a5 * a7;
|
|
307
|
+
const r0y = a5 * a6 - a3 * a8;
|
|
308
|
+
const r0z = a3 * a7 - a4 * a6;
|
|
309
|
+
const determinant = a0 * r0x + a1 * r0y + a2 * r0z;
|
|
310
|
+
if (determinant === 0 || !Number.isFinite(determinant)) {
|
|
311
|
+
return false;
|
|
312
|
+
}
|
|
313
|
+
// The forward error bound of `mat3x3Invert`'s expansion, first bounded
|
|
314
|
+
// cheaply as there: a linear part it refuses is left to `mat3x3Invert`,
|
|
315
|
+
// which takes it further.
|
|
316
|
+
const s1 = Math.abs(a0) + Math.abs(a1) + Math.abs(a2);
|
|
317
|
+
const s2 = Math.abs(a3) + Math.abs(a4) + Math.abs(a5);
|
|
318
|
+
const s3 = Math.abs(a6) + Math.abs(a7) + Math.abs(a8);
|
|
319
|
+
const s12 = s1 * s2;
|
|
320
|
+
const magnitude = Math.abs(determinant);
|
|
321
|
+
if (!(7 * UNIT_ROUNDOFF_ * (s12 * s3) <= ROUNDING_TOLERANCE_ * magnitude &&
|
|
322
|
+
9 * UNIT_ROUNDOFF_ * (s12 + (s1 + s2) * s3) * s1 <=
|
|
323
|
+
ROUNDING_TOLERANCE_ * magnitude &&
|
|
324
|
+
s1 + s2 + s3 <= 2 ** 200 &&
|
|
325
|
+
magnitude >= 2 ** -400)) {
|
|
326
|
+
BOUND_INPUT_[0] = magnitude;
|
|
327
|
+
if (!isWithinTolerance3_(a)) {
|
|
328
|
+
return false;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
const c1 = a7 * a2 - a8 * a1;
|
|
332
|
+
const c2 = a1 * a5 - a2 * a4;
|
|
333
|
+
const c4 = a8 * a0 - a6 * a2;
|
|
334
|
+
const c5 = a2 * a3 - a0 * a5;
|
|
335
|
+
const c7 = a6 * a1 - a7 * a0;
|
|
336
|
+
const c8 = a0 * a4 - a1 * a3;
|
|
337
|
+
const scale = 1 / determinant;
|
|
338
|
+
const reciprocal = Number.isFinite(scale);
|
|
339
|
+
if (!reciprocal && !divide) {
|
|
340
|
+
return false;
|
|
341
|
+
}
|
|
342
|
+
const b0 = reciprocal ? r0x * scale : r0x / determinant;
|
|
343
|
+
const b1 = reciprocal ? c1 * scale : c1 / determinant;
|
|
344
|
+
const b2 = reciprocal ? c2 * scale : c2 / determinant;
|
|
345
|
+
const b3 = reciprocal ? r0y * scale : r0y / determinant;
|
|
346
|
+
const b4 = reciprocal ? c4 * scale : c4 / determinant;
|
|
347
|
+
const b5 = reciprocal ? c5 * scale : c5 / determinant;
|
|
348
|
+
const b6 = reciprocal ? r0z * scale : r0z / determinant;
|
|
349
|
+
const b7 = reciprocal ? c7 * scale : c7 / determinant;
|
|
350
|
+
const b8 = reciprocal ? c8 * scale : c8 / determinant;
|
|
351
|
+
const p0 = b0 * a9;
|
|
352
|
+
const p1 = b3 * a10;
|
|
353
|
+
const p2 = b6 * a11;
|
|
354
|
+
const p3 = b1 * a9;
|
|
355
|
+
const p4 = b4 * a10;
|
|
356
|
+
const p5 = b7 * a11;
|
|
357
|
+
const p6 = b2 * a9;
|
|
358
|
+
const p7 = b5 * a10;
|
|
359
|
+
const p8 = b8 * a11;
|
|
360
|
+
const t0 = -(p0 + p1 + p2);
|
|
361
|
+
const t1 = -(p3 + p4 + p5);
|
|
362
|
+
const t2 = -(p6 + p7 + p8);
|
|
363
|
+
// Each translation component, a sum of three products, is within
|
|
364
|
+
// γ₃ · Σ|products| (γ₃ below 4u) plus an underflow per product of the
|
|
365
|
+
// exact −B · t for the B returned (Higham, §3.1), and accepted within 2^-41
|
|
366
|
+
// of itself, as the determinant is; a component the bound cannot keep is
|
|
367
|
+
// left to the correctly rounded kernel. A component whose every product
|
|
368
|
+
// has a zero factor is exactly zero.
|
|
369
|
+
const m0 = Math.abs(t0);
|
|
370
|
+
const m1 = Math.abs(t1);
|
|
371
|
+
const m2 = Math.abs(t2);
|
|
372
|
+
if (!((4 * UNIT_ROUNDOFF_ * (Math.abs(p0) + Math.abs(p1) + Math.abs(p2)) <=
|
|
373
|
+
ROUNDING_TOLERANCE_ * m0 &&
|
|
374
|
+
3 * UNDERFLOW_UNIT_ <= UNDERFLOW_TOLERANCE_ * m0) ||
|
|
375
|
+
((b0 === 0 || a9 === 0) &&
|
|
376
|
+
(b3 === 0 || a10 === 0) &&
|
|
377
|
+
(b6 === 0 || a11 === 0))) ||
|
|
378
|
+
!((4 * UNIT_ROUNDOFF_ * (Math.abs(p3) + Math.abs(p4) + Math.abs(p5)) <=
|
|
379
|
+
ROUNDING_TOLERANCE_ * m1 &&
|
|
380
|
+
3 * UNDERFLOW_UNIT_ <= UNDERFLOW_TOLERANCE_ * m1) ||
|
|
381
|
+
((b1 === 0 || a9 === 0) &&
|
|
382
|
+
(b4 === 0 || a10 === 0) &&
|
|
383
|
+
(b7 === 0 || a11 === 0))) ||
|
|
384
|
+
!((4 * UNIT_ROUNDOFF_ * (Math.abs(p6) + Math.abs(p7) + Math.abs(p8)) <=
|
|
385
|
+
ROUNDING_TOLERANCE_ * m2 &&
|
|
386
|
+
3 * UNDERFLOW_UNIT_ <= UNDERFLOW_TOLERANCE_ * m2) ||
|
|
387
|
+
((b2 === 0 || a9 === 0) &&
|
|
388
|
+
(b5 === 0 || a10 === 0) &&
|
|
389
|
+
(b8 === 0 || a11 === 0)))) {
|
|
390
|
+
return false;
|
|
391
|
+
}
|
|
392
|
+
// Within INVERSE_LIMIT_, the exact entries, within about 2^-40 of the
|
|
393
|
+
// largest of these, round to finite numbers. The exact −A⁻¹ · t differs
|
|
394
|
+
// from the −B · t computed by its rounding, within 2^-41 of each
|
|
395
|
+
// component, and by (B − A⁻¹) · t: an entry of B is within 2^-36 of the
|
|
396
|
+
// largest (ERROR_SHARES_ in `_invert.ts`), however small itself, as a
|
|
397
|
+
// cofactor's error follows its products and not its value, so that term
|
|
398
|
+
// stays within 2^-36 · max|B| · Σ|t|; at most 32 · Number.MAX_VALUE, it
|
|
399
|
+
// is within 2^-31 · Number.MAX_VALUE, which the 2^-30 margin of the limit
|
|
400
|
+
// takes. Past either, or not finite, the second attempt decides.
|
|
401
|
+
const largest = Math.max(Math.abs(b0), Math.abs(b1), Math.abs(b2), Math.abs(b3), Math.abs(b4), Math.abs(b5), Math.abs(b6), Math.abs(b7), Math.abs(b8));
|
|
402
|
+
if (!(Math.max(largest, m0, m1, m2) <= INVERSE_LIMIT_ &&
|
|
403
|
+
largest * (Math.abs(a9) + Math.abs(a10) + Math.abs(a11)) * 2 ** -5 <=
|
|
404
|
+
Number.MAX_VALUE)) {
|
|
405
|
+
return false;
|
|
406
|
+
}
|
|
407
|
+
out[0] = b0;
|
|
408
|
+
out[1] = b1;
|
|
409
|
+
out[2] = b2;
|
|
410
|
+
out[3] = b3;
|
|
411
|
+
out[4] = b4;
|
|
412
|
+
out[5] = b5;
|
|
413
|
+
out[6] = b6;
|
|
414
|
+
out[7] = b7;
|
|
415
|
+
out[8] = b8;
|
|
416
|
+
out[9] = t0;
|
|
417
|
+
out[10] = t1;
|
|
418
|
+
out[11] = t2;
|
|
419
|
+
return true;
|
|
420
|
+
}
|
|
421
|
+
/** Writes the translation by `translation` into `out`. */
|
|
422
|
+
export function affine3FromTranslation(out, translation) {
|
|
423
|
+
out[0] = 1;
|
|
424
|
+
out[1] = 0;
|
|
425
|
+
out[2] = 0;
|
|
426
|
+
out[3] = 0;
|
|
427
|
+
out[4] = 1;
|
|
428
|
+
out[5] = 0;
|
|
429
|
+
out[6] = 0;
|
|
430
|
+
out[7] = 0;
|
|
431
|
+
out[8] = 1;
|
|
432
|
+
out[9] = translation[0];
|
|
433
|
+
out[10] = translation[1];
|
|
434
|
+
out[11] = translation[2];
|
|
435
|
+
return out;
|
|
436
|
+
}
|
|
437
|
+
/** Writes the scale `diag(scale)` into `out`. */
|
|
438
|
+
export function affine3FromScale(out, scale) {
|
|
439
|
+
out[0] = scale[0];
|
|
440
|
+
out[1] = 0;
|
|
441
|
+
out[2] = 0;
|
|
442
|
+
out[3] = 0;
|
|
443
|
+
out[4] = scale[1];
|
|
444
|
+
out[5] = 0;
|
|
445
|
+
out[6] = 0;
|
|
446
|
+
out[7] = 0;
|
|
447
|
+
out[8] = scale[2];
|
|
448
|
+
out[9] = 0;
|
|
449
|
+
out[10] = 0;
|
|
450
|
+
out[11] = 0;
|
|
451
|
+
return out;
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Writes the rotation by `angle` radians about the x axis (right-hand rule)
|
|
455
|
+
* into `out`.
|
|
456
|
+
*/
|
|
457
|
+
export function affine3FromRotationX(out, angle) {
|
|
458
|
+
const cos = Math.cos(angle);
|
|
459
|
+
const sin = Math.sin(angle);
|
|
460
|
+
out[0] = 1;
|
|
461
|
+
out[1] = 0;
|
|
462
|
+
out[2] = 0;
|
|
463
|
+
out[3] = 0;
|
|
464
|
+
out[4] = cos;
|
|
465
|
+
out[5] = sin;
|
|
466
|
+
out[6] = 0;
|
|
467
|
+
out[7] = -sin;
|
|
468
|
+
out[8] = cos;
|
|
469
|
+
out[9] = 0;
|
|
470
|
+
out[10] = 0;
|
|
471
|
+
out[11] = 0;
|
|
472
|
+
return out;
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Writes the rotation by `angle` radians about the y axis (right-hand rule)
|
|
476
|
+
* into `out`.
|
|
477
|
+
*/
|
|
478
|
+
export function affine3FromRotationY(out, angle) {
|
|
479
|
+
const cos = Math.cos(angle);
|
|
480
|
+
const sin = Math.sin(angle);
|
|
481
|
+
out[0] = cos;
|
|
482
|
+
out[1] = 0;
|
|
483
|
+
out[2] = -sin;
|
|
484
|
+
out[3] = 0;
|
|
485
|
+
out[4] = 1;
|
|
486
|
+
out[5] = 0;
|
|
487
|
+
out[6] = sin;
|
|
488
|
+
out[7] = 0;
|
|
489
|
+
out[8] = cos;
|
|
490
|
+
out[9] = 0;
|
|
491
|
+
out[10] = 0;
|
|
492
|
+
out[11] = 0;
|
|
493
|
+
return out;
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Writes the rotation by `angle` radians about the z axis (right-hand rule)
|
|
497
|
+
* into `out`.
|
|
498
|
+
*/
|
|
499
|
+
export function affine3FromRotationZ(out, angle) {
|
|
500
|
+
const cos = Math.cos(angle);
|
|
501
|
+
const sin = Math.sin(angle);
|
|
502
|
+
out[0] = cos;
|
|
503
|
+
out[1] = sin;
|
|
504
|
+
out[2] = 0;
|
|
505
|
+
out[3] = -sin;
|
|
506
|
+
out[4] = cos;
|
|
507
|
+
out[5] = 0;
|
|
508
|
+
out[6] = 0;
|
|
509
|
+
out[7] = 0;
|
|
510
|
+
out[8] = 1;
|
|
511
|
+
out[9] = 0;
|
|
512
|
+
out[10] = 0;
|
|
513
|
+
out[11] = 0;
|
|
514
|
+
return out;
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* Writes the rotation by `angle` radians about the unit vector `axis`
|
|
518
|
+
* (right-hand rule, Rodrigues' formula) into `out`. `axis` is not normalised
|
|
519
|
+
* here: for any other length the matrix is not a rotation.
|
|
520
|
+
*/
|
|
521
|
+
export function affine3FromAxisAngle(out, axis, angle) {
|
|
522
|
+
const x = axis[0];
|
|
523
|
+
const y = axis[1];
|
|
524
|
+
const z = axis[2];
|
|
525
|
+
const sin = Math.sin(angle);
|
|
526
|
+
// 1 − cos as twice the squared half-angle sine, and the diagonal as one
|
|
527
|
+
// minus its share: exact in the first digits for the small angles where
|
|
528
|
+
// cos − 1 would lose them, and exactly 1 on the axis itself.
|
|
529
|
+
const half = Math.sin(angle / 2);
|
|
530
|
+
const rest = 2 * half * half;
|
|
531
|
+
out[0] = 1 - rest * (y * y + z * z);
|
|
532
|
+
out[1] = rest * x * y + sin * z;
|
|
533
|
+
out[2] = rest * x * z - sin * y;
|
|
534
|
+
out[3] = rest * x * y - sin * z;
|
|
535
|
+
out[4] = 1 - rest * (x * x + z * z);
|
|
536
|
+
out[5] = rest * y * z + sin * x;
|
|
537
|
+
out[6] = rest * x * z + sin * y;
|
|
538
|
+
out[7] = rest * y * z - sin * x;
|
|
539
|
+
out[8] = 1 - rest * (x * x + y * y);
|
|
540
|
+
out[9] = 0;
|
|
541
|
+
out[10] = 0;
|
|
542
|
+
out[11] = 0;
|
|
543
|
+
return out;
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* Writes the rotation of the unit quaternion `q`, with no translation, into
|
|
547
|
+
* `out`. `q` must be unit length; it is not normalised here.
|
|
548
|
+
*/
|
|
549
|
+
export function affine3FromQuaternion(out, q) {
|
|
550
|
+
const x = q[0];
|
|
551
|
+
const y = q[1];
|
|
552
|
+
const z = q[2];
|
|
553
|
+
const w = q[3];
|
|
554
|
+
const xx = x * x;
|
|
555
|
+
const yy = y * y;
|
|
556
|
+
const zz = z * z;
|
|
557
|
+
const xy = x * y;
|
|
558
|
+
const xz = x * z;
|
|
559
|
+
const yz = y * z;
|
|
560
|
+
const wx = w * x;
|
|
561
|
+
const wy = w * y;
|
|
562
|
+
const wz = w * z;
|
|
563
|
+
out[0] = 1 - 2 * (yy + zz);
|
|
564
|
+
out[1] = 2 * (xy + wz);
|
|
565
|
+
out[2] = 2 * (xz - wy);
|
|
566
|
+
out[3] = 2 * (xy - wz);
|
|
567
|
+
out[4] = 1 - 2 * (xx + zz);
|
|
568
|
+
out[5] = 2 * (yz + wx);
|
|
569
|
+
out[6] = 2 * (xz + wy);
|
|
570
|
+
out[7] = 2 * (yz - wx);
|
|
571
|
+
out[8] = 1 - 2 * (xx + yy);
|
|
572
|
+
out[9] = 0;
|
|
573
|
+
out[10] = 0;
|
|
574
|
+
out[11] = 0;
|
|
575
|
+
return out;
|
|
576
|
+
}
|
|
577
|
+
/** Writes the 3 × 3 matrix `m` as a transform with no translation into `out`. */
|
|
578
|
+
export function affine3FromMat3x3(out, m) {
|
|
579
|
+
out[0] = m[0];
|
|
580
|
+
out[1] = m[1];
|
|
581
|
+
out[2] = m[2];
|
|
582
|
+
out[3] = m[3];
|
|
583
|
+
out[4] = m[4];
|
|
584
|
+
out[5] = m[5];
|
|
585
|
+
out[6] = m[6];
|
|
586
|
+
out[7] = m[7];
|
|
587
|
+
out[8] = m[8];
|
|
588
|
+
out[9] = 0;
|
|
589
|
+
out[10] = 0;
|
|
590
|
+
out[11] = 0;
|
|
591
|
+
return out;
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Writes the upper three rows of `m` into `out`, dropping its bottom row, which
|
|
595
|
+
* an {@link Affine3} does not store (`0, 0, 0, 1` for an affine matrix). A
|
|
596
|
+
* perspective row is lost.
|
|
597
|
+
*/
|
|
598
|
+
export function affine3FromMat4x4(out, m) {
|
|
599
|
+
out[0] = m[0];
|
|
600
|
+
out[1] = m[1];
|
|
601
|
+
out[2] = m[2];
|
|
602
|
+
out[3] = m[4];
|
|
603
|
+
out[4] = m[5];
|
|
604
|
+
out[5] = m[6];
|
|
605
|
+
out[6] = m[8];
|
|
606
|
+
out[7] = m[9];
|
|
607
|
+
out[8] = m[10];
|
|
608
|
+
out[9] = m[12];
|
|
609
|
+
out[10] = m[13];
|
|
610
|
+
out[11] = m[14];
|
|
611
|
+
return out;
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Writes the transform that scales, then rotates, then translates into `out`:
|
|
615
|
+
* `T · R · S`, with `rotation` a unit quaternion (not normalised here).
|
|
616
|
+
*/
|
|
617
|
+
export function affine3Compose(out, translation, rotation, scale) {
|
|
618
|
+
const x = rotation[0];
|
|
619
|
+
const y = rotation[1];
|
|
620
|
+
const z = rotation[2];
|
|
621
|
+
const w = rotation[3];
|
|
622
|
+
const sx = scale[0];
|
|
623
|
+
const sy = scale[1];
|
|
624
|
+
const sz = scale[2];
|
|
625
|
+
const xx = x * x;
|
|
626
|
+
const yy = y * y;
|
|
627
|
+
const zz = z * z;
|
|
628
|
+
const xy = x * y;
|
|
629
|
+
const xz = x * z;
|
|
630
|
+
const yz = y * z;
|
|
631
|
+
const wx = w * x;
|
|
632
|
+
const wy = w * y;
|
|
633
|
+
const wz = w * z;
|
|
634
|
+
out[0] = (1 - 2 * (yy + zz)) * sx;
|
|
635
|
+
out[1] = 2 * (xy + wz) * sx;
|
|
636
|
+
out[2] = 2 * (xz - wy) * sx;
|
|
637
|
+
out[3] = 2 * (xy - wz) * sy;
|
|
638
|
+
out[4] = (1 - 2 * (xx + zz)) * sy;
|
|
639
|
+
out[5] = 2 * (yz + wx) * sy;
|
|
640
|
+
out[6] = 2 * (xz + wy) * sz;
|
|
641
|
+
out[7] = 2 * (yz - wx) * sz;
|
|
642
|
+
out[8] = (1 - 2 * (xx + yy)) * sz;
|
|
643
|
+
out[9] = translation[0];
|
|
644
|
+
out[10] = translation[1];
|
|
645
|
+
out[11] = translation[2];
|
|
646
|
+
return out;
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* Splits `a` into the translation, rotation and scale that
|
|
650
|
+
* {@link affine3Compose} builds it from: the translation is the last column, the
|
|
651
|
+
* scale is the length of each of the other three, and the rotation is the unit
|
|
652
|
+
* quaternion of those columns divided by it. A reflection (negative
|
|
653
|
+
* determinant) puts its sign in the x scale, so the rotation is proper.
|
|
654
|
+
*
|
|
655
|
+
* Returns `false` when there is no such split: a column of zero length or not
|
|
656
|
+
* finite, two columns further from perpendicular than about `1e-6` in cosine (a
|
|
657
|
+
* shear), or a translation that is not finite. The three outputs then hold
|
|
658
|
+
* `NaN`, never a stale or partial result, and nothing throws.
|
|
659
|
+
*/
|
|
660
|
+
export function affine3Decompose(outTranslation, outRotation, outScale, a) {
|
|
661
|
+
const x = a[9];
|
|
662
|
+
const y = a[10];
|
|
663
|
+
const z = a[11];
|
|
664
|
+
if (Number.isFinite(x) &&
|
|
665
|
+
Number.isFinite(y) &&
|
|
666
|
+
Number.isFinite(z) &&
|
|
667
|
+
decomposeLinear_(outRotation, outScale, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8])) {
|
|
668
|
+
outTranslation[0] = x;
|
|
669
|
+
outTranslation[1] = y;
|
|
670
|
+
outTranslation[2] = z;
|
|
671
|
+
return true;
|
|
672
|
+
}
|
|
673
|
+
outTranslation[0] = Number.NaN;
|
|
674
|
+
outTranslation[1] = Number.NaN;
|
|
675
|
+
outTranslation[2] = Number.NaN;
|
|
676
|
+
outRotation[0] = Number.NaN;
|
|
677
|
+
outRotation[1] = Number.NaN;
|
|
678
|
+
outRotation[2] = Number.NaN;
|
|
679
|
+
outRotation[3] = Number.NaN;
|
|
680
|
+
outScale[0] = Number.NaN;
|
|
681
|
+
outScale[1] = Number.NaN;
|
|
682
|
+
outScale[2] = Number.NaN;
|
|
683
|
+
return false;
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* Writes `a · T(translation)` into `out`: the translation applied first, in the
|
|
687
|
+
* local space of `a`. Only the translation column changes. `out` may be `a`.
|
|
688
|
+
*/
|
|
689
|
+
export function affine3Translate(out, a, translation) {
|
|
690
|
+
const a0 = a[0];
|
|
691
|
+
const a1 = a[1];
|
|
692
|
+
const a2 = a[2];
|
|
693
|
+
const a3 = a[3];
|
|
694
|
+
const a4 = a[4];
|
|
695
|
+
const a5 = a[5];
|
|
696
|
+
const a6 = a[6];
|
|
697
|
+
const a7 = a[7];
|
|
698
|
+
const a8 = a[8];
|
|
699
|
+
const x = translation[0];
|
|
700
|
+
const y = translation[1];
|
|
701
|
+
const z = translation[2];
|
|
702
|
+
const e = a0 * x + a3 * y + a6 * z + a[9];
|
|
703
|
+
const f = a1 * x + a4 * y + a7 * z + a[10];
|
|
704
|
+
const g = a2 * x + a5 * y + a8 * z + a[11];
|
|
705
|
+
out[0] = a0;
|
|
706
|
+
out[1] = a1;
|
|
707
|
+
out[2] = a2;
|
|
708
|
+
out[3] = a3;
|
|
709
|
+
out[4] = a4;
|
|
710
|
+
out[5] = a5;
|
|
711
|
+
out[6] = a6;
|
|
712
|
+
out[7] = a7;
|
|
713
|
+
out[8] = a8;
|
|
714
|
+
out[9] = e;
|
|
715
|
+
out[10] = f;
|
|
716
|
+
out[11] = g;
|
|
717
|
+
return out;
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* Writes `a · R(rotation)` into `out`: the rotation of the unit quaternion
|
|
721
|
+
* `rotation` applied first, in the local space of `a` (`rotation` is not
|
|
722
|
+
* normalised here). The translation does not change. `out` may be `a`.
|
|
723
|
+
*/
|
|
724
|
+
export function affine3Rotate(out, a, rotation) {
|
|
725
|
+
const a0 = a[0];
|
|
726
|
+
const a1 = a[1];
|
|
727
|
+
const a2 = a[2];
|
|
728
|
+
const a3 = a[3];
|
|
729
|
+
const a4 = a[4];
|
|
730
|
+
const a5 = a[5];
|
|
731
|
+
const a6 = a[6];
|
|
732
|
+
const a7 = a[7];
|
|
733
|
+
const a8 = a[8];
|
|
734
|
+
const x = rotation[0];
|
|
735
|
+
const y = rotation[1];
|
|
736
|
+
const z = rotation[2];
|
|
737
|
+
const w = rotation[3];
|
|
738
|
+
const xx = x * x;
|
|
739
|
+
const yy = y * y;
|
|
740
|
+
const zz = z * z;
|
|
741
|
+
const xy = x * y;
|
|
742
|
+
const xz = x * z;
|
|
743
|
+
const yz = y * z;
|
|
744
|
+
const wx = w * x;
|
|
745
|
+
const wy = w * y;
|
|
746
|
+
const wz = w * z;
|
|
747
|
+
const r0 = 1 - 2 * (yy + zz);
|
|
748
|
+
const r1 = 2 * (xy + wz);
|
|
749
|
+
const r2 = 2 * (xz - wy);
|
|
750
|
+
const r3 = 2 * (xy - wz);
|
|
751
|
+
const r4 = 1 - 2 * (xx + zz);
|
|
752
|
+
const r5 = 2 * (yz + wx);
|
|
753
|
+
const r6 = 2 * (xz + wy);
|
|
754
|
+
const r7 = 2 * (yz - wx);
|
|
755
|
+
const r8 = 1 - 2 * (xx + yy);
|
|
756
|
+
out[0] = a0 * r0 + a3 * r1 + a6 * r2;
|
|
757
|
+
out[1] = a1 * r0 + a4 * r1 + a7 * r2;
|
|
758
|
+
out[2] = a2 * r0 + a5 * r1 + a8 * r2;
|
|
759
|
+
out[3] = a0 * r3 + a3 * r4 + a6 * r5;
|
|
760
|
+
out[4] = a1 * r3 + a4 * r4 + a7 * r5;
|
|
761
|
+
out[5] = a2 * r3 + a5 * r4 + a8 * r5;
|
|
762
|
+
out[6] = a0 * r6 + a3 * r7 + a6 * r8;
|
|
763
|
+
out[7] = a1 * r6 + a4 * r7 + a7 * r8;
|
|
764
|
+
out[8] = a2 * r6 + a5 * r7 + a8 * r8;
|
|
765
|
+
out[9] = a[9];
|
|
766
|
+
out[10] = a[10];
|
|
767
|
+
out[11] = a[11];
|
|
768
|
+
return out;
|
|
769
|
+
}
|
|
770
|
+
/**
|
|
771
|
+
* Writes `a · S(scale)` into `out`: the scale applied first, in the local space
|
|
772
|
+
* of `a`. The translation does not change. `out` may be `a`.
|
|
773
|
+
*/
|
|
774
|
+
export function affine3Scale(out, a, scale) {
|
|
775
|
+
const sx = scale[0];
|
|
776
|
+
const sy = scale[1];
|
|
777
|
+
const sz = scale[2];
|
|
778
|
+
out[0] = a[0] * sx;
|
|
779
|
+
out[1] = a[1] * sx;
|
|
780
|
+
out[2] = a[2] * sx;
|
|
781
|
+
out[3] = a[3] * sy;
|
|
782
|
+
out[4] = a[4] * sy;
|
|
783
|
+
out[5] = a[5] * sy;
|
|
784
|
+
out[6] = a[6] * sz;
|
|
785
|
+
out[7] = a[7] * sz;
|
|
786
|
+
out[8] = a[8] * sz;
|
|
787
|
+
out[9] = a[9];
|
|
788
|
+
out[10] = a[10];
|
|
789
|
+
out[11] = a[11];
|
|
790
|
+
return out;
|
|
791
|
+
}
|
|
792
|
+
//# sourceMappingURL=affine3.js.map
|