@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.
- package/CHANGELOG.md +268 -0
- package/README.md +229 -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 +19 -0
- package/dist/_invert.d.ts.map +1 -0
- package/dist/_invert.js +48 -0
- package/dist/_invert.js.map +1 -0
- package/dist/_slerp.d.ts +15 -0
- package/dist/_slerp.d.ts.map +1 -1
- package/dist/_slerp.js +47 -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 +144 -0
- package/dist/affine2.d.ts.map +1 -0
- package/dist/affine2.js +375 -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 +192 -0
- package/dist/affine3.d.ts.map +1 -0
- package/dist/affine3.js +718 -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 +67 -25
- package/dist/mat2x2.d.ts.map +1 -1
- package/dist/mat2x2.js +150 -94
- 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 +131 -48
- package/dist/mat3x3.d.ts.map +1 -1
- package/dist/mat3x3.js +434 -193
- 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 +195 -67
- package/dist/mat4x4.d.ts.map +1 -1
- package/dist/mat4x4.js +849 -335
- package/dist/mat4x4.js.map +1 -1
- package/dist/projection.d.ts +98 -0
- package/dist/projection.d.ts.map +1 -0
- package/dist/projection.js +369 -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 +104 -22
- package/dist/quaternion.d.ts.map +1 -1
- package/dist/quaternion.js +294 -181
- 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 +189 -0
- package/dist/vec2-array-componentwise.d.ts.map +1 -0
- package/dist/vec2-array-componentwise.js +424 -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 +189 -0
- package/dist/vec3-array-componentwise.d.ts.map +1 -0
- package/dist/vec3-array-componentwise.js +458 -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 +366 -273
- package/dist/vec3.js.map +1 -1
- package/dist/vec4-array-componentwise.d.ts +155 -0
- package/dist/vec4-array-componentwise.d.ts.map +1 -0
- package/dist/vec4-array-componentwise.js +401 -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 +57 -53
- package/dist/vec4.d.ts.map +1 -1
- package/dist/vec4.js +94 -246
- 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 +18 -9
package/dist/vec3.js
CHANGED
|
@@ -1,314 +1,407 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { diffOfProducts, dotPrecise, lerp, TWO_PI } from "@ac-kit/math-scalar";
|
|
1
|
+
import { angleNormalizeRad, diffOfProducts, dotPrecise3, lerp, TWO_PI, } from "@ac-kit/math-scalar";
|
|
3
2
|
import { SLERP_LINEAR_FALLBACK_DOT_ } from "./_slerp.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
import { vec3DotPrecise, vec3Length, vec3NormalizeScaled, } from "./vec3-componentwise.js";
|
|
4
|
+
/**
|
|
5
|
+
* Writes the cross product `a × b` into `out`, in plain arithmetic: a vector
|
|
6
|
+
* perpendicular to both, of length `|a||b| sin θ`, right-handed (`x × y = z`).
|
|
7
|
+
* Where a component's two terms cancel use {@link vec3CrossPrecise}. `out` may
|
|
8
|
+
* be `a` or `b`.
|
|
9
|
+
*/
|
|
10
|
+
export function vec3Cross(out, a, b) {
|
|
11
|
+
const ax = a[0];
|
|
12
|
+
const ay = a[1];
|
|
13
|
+
const az = a[2];
|
|
14
|
+
const bx = b[0];
|
|
15
|
+
const by = b[1];
|
|
16
|
+
const bz = b[2];
|
|
17
|
+
out[0] = ay * bz - az * by;
|
|
18
|
+
out[1] = az * bx - ax * bz;
|
|
19
|
+
out[2] = ax * by - ay * bx;
|
|
20
|
+
return out;
|
|
8
21
|
}
|
|
9
|
-
|
|
10
|
-
|
|
22
|
+
/**
|
|
23
|
+
* {@link vec3Cross} with the rounding of every product recovered
|
|
24
|
+
* (`@ac-kit/math-scalar`'s `diffOfProducts`): each component within 1.5 ULP of
|
|
25
|
+
* the exact value however badly its terms cancel. About five times the cost.
|
|
26
|
+
* `out` may be `a` or `b`.
|
|
27
|
+
*/
|
|
28
|
+
export function vec3CrossPrecise(out, a, b) {
|
|
29
|
+
const ax = a[0];
|
|
30
|
+
const ay = a[1];
|
|
31
|
+
const az = a[2];
|
|
32
|
+
const bx = b[0];
|
|
33
|
+
const by = b[1];
|
|
34
|
+
const bz = b[2];
|
|
35
|
+
out[0] = diffOfProducts(ay, bz, az, by);
|
|
36
|
+
out[1] = diffOfProducts(az, bx, ax, bz);
|
|
37
|
+
out[2] = diffOfProducts(ax, by, ay, bx);
|
|
38
|
+
return out;
|
|
11
39
|
}
|
|
12
|
-
|
|
13
|
-
|
|
40
|
+
/**
|
|
41
|
+
* The sine of the angle between two vectors times their lengths, `|a × b|`,
|
|
42
|
+
* from the compensated cross product.
|
|
43
|
+
*/
|
|
44
|
+
function crossLengthPrecise_(a, b) {
|
|
45
|
+
const x = diffOfProducts(a[1], b[2], a[2], b[1]);
|
|
46
|
+
const y = diffOfProducts(a[2], b[0], a[0], b[2]);
|
|
47
|
+
const z = diffOfProducts(a[0], b[1], a[1], b[0]);
|
|
48
|
+
return Math.sqrt(x * x + y * y + z * z);
|
|
14
49
|
}
|
|
15
|
-
|
|
16
|
-
|
|
50
|
+
/**
|
|
51
|
+
* The signed angle from `a` to `b`, positive counter-clockwise when seen from
|
|
52
|
+
* the tip of `axis` (the right-hand rule about `axis`), in `(−π, π]`: `π` for
|
|
53
|
+
* opposite vectors. `axis` need not be a unit vector, and only its side of the
|
|
54
|
+
* plane of `a` and `b` matters. Computed from the compensated cross and dot
|
|
55
|
+
* products, so the angle of nearly parallel vectors keeps its digits. A zero
|
|
56
|
+
* vector gives `0`. Right while the product of the two lengths lies between
|
|
57
|
+
* about `1e−150` and `1e150`, which keeps the squares of the cross product in
|
|
58
|
+
* range (vectors of `1e100` give `Infinity` there and a wrong angle); an angle
|
|
59
|
+
* does not depend on the lengths, so beyond, normalise both vectors with
|
|
60
|
+
* {@link vec3NormalizeScaled} first.
|
|
61
|
+
*/
|
|
62
|
+
export function vec3AngleTo(a, b, axis) {
|
|
63
|
+
const cx = diffOfProducts(a[1], b[2], a[2], b[1]);
|
|
64
|
+
const cy = diffOfProducts(a[2], b[0], a[0], b[2]);
|
|
65
|
+
const cz = diffOfProducts(a[0], b[1], a[1], b[0]);
|
|
66
|
+
const angle = Math.atan2(Math.sqrt(cx * cx + cy * cy + cz * cz), vec3DotPrecise(a, b) + 0);
|
|
67
|
+
return angleNormalizeRad(cx * axis[0] + cy * axis[1] + cz * axis[2] < 0 ? -angle : angle);
|
|
17
68
|
}
|
|
18
69
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
70
|
+
* The unsigned angle between two vectors, `atan2(|a × b|, a · b)`, in `[0, π]`,
|
|
71
|
+
* from the compensated cross and dot products. A zero vector gives `0`. Right
|
|
72
|
+
* while the product of the two lengths lies between about `1e−150` and `1e150`,
|
|
73
|
+
* which keeps the squares of the cross product in range (vectors of `1e100`
|
|
74
|
+
* give a wrong angle); an angle does not depend on the lengths, so beyond,
|
|
75
|
+
* normalise both vectors with {@link vec3NormalizeScaled} first.
|
|
21
76
|
*/
|
|
22
|
-
export function
|
|
23
|
-
|
|
24
|
-
throw new Error("Cannot compute angle to/from a zero-length vector.");
|
|
25
|
-
}
|
|
26
|
-
const cross = vec3Cross(v1, v2);
|
|
27
|
-
const angle = Math.atan2(vec3Length(cross), vec3Dot(v1, v2));
|
|
28
|
-
const sign = vec3Dot(cross, axis) < 0 ? -1 : 1;
|
|
29
|
-
return angle * sign;
|
|
77
|
+
export function vec3AngleBetween(a, b) {
|
|
78
|
+
return Math.atan2(crossLengthPrecise_(a, b), vec3DotPrecise(a, b) + 0);
|
|
30
79
|
}
|
|
31
80
|
/**
|
|
32
|
-
*
|
|
33
|
-
* range [0, π].
|
|
81
|
+
* Writes `[a.x, a.y, z]` into `out`: the plane vector lifted to space.
|
|
34
82
|
*
|
|
35
|
-
* @
|
|
83
|
+
* @param z The third component; defaults to `0`.
|
|
36
84
|
*/
|
|
37
|
-
export function
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
throw new Error("Cannot compute angle between zero-length vectors");
|
|
43
|
-
}
|
|
44
|
-
const cosTheta = clamp(dot / (len1 * len2), -1, 1);
|
|
45
|
-
return Math.acos(cosTheta);
|
|
46
|
-
}
|
|
47
|
-
export function vec3ApplyMat3(v, m) {
|
|
48
|
-
return [dotPrecise(v, m[0]), dotPrecise(v, m[1]), dotPrecise(v, m[2])];
|
|
85
|
+
export function vec3FromVec2(out, a, z = 0) {
|
|
86
|
+
out[0] = a[0];
|
|
87
|
+
out[1] = a[1];
|
|
88
|
+
out[2] = z;
|
|
89
|
+
return out;
|
|
49
90
|
}
|
|
50
91
|
/**
|
|
51
|
-
*
|
|
52
|
-
*
|
|
92
|
+
* Writes `[x, y, z] / w` into `out`: the Cartesian point of a homogeneous
|
|
93
|
+
* vector, the perspective divide. A `w` of zero (a point at infinity) gives
|
|
94
|
+
* non-finite components, `±Infinity` or `NaN`, not an exception.
|
|
53
95
|
*/
|
|
54
|
-
export function
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
dotPrecise(homogeneous, m[2]),
|
|
61
|
-
];
|
|
62
|
-
}
|
|
63
|
-
export function vec3ApplyQuaternion(v, q) {
|
|
64
|
-
const [qx, qy, qz, qw] = q;
|
|
65
|
-
// Calculate quaternion * vector
|
|
66
|
-
const ix = dotPrecise([qw, qy, -qz], [v[0], v[2], v[1]]);
|
|
67
|
-
const iy = dotPrecise([qw, qz, -qx], [v[1], v[0], v[2]]);
|
|
68
|
-
const iz = dotPrecise([qw, qx, -qy], [v[2], v[1], v[0]]);
|
|
69
|
-
const iw = dotPrecise([-qx, -qy, -qz], v);
|
|
70
|
-
// Calculate result * inverse quaternion
|
|
71
|
-
return [
|
|
72
|
-
dotPrecise([ix, -iw, -iy, iz], [qw, qx, qz, qy]),
|
|
73
|
-
dotPrecise([iy, -iw, -iz, ix], [qw, qy, qx, qz]),
|
|
74
|
-
dotPrecise([iz, -iw, -ix, iy], [qw, qz, qy, qx]),
|
|
75
|
-
];
|
|
76
|
-
}
|
|
77
|
-
export function vec3Clamp(v, min, max) {
|
|
78
|
-
return [
|
|
79
|
-
clamp(v[0], min[0], max[0]),
|
|
80
|
-
clamp(v[1], min[1], max[1]),
|
|
81
|
-
clamp(v[2], min[2], max[2]),
|
|
82
|
-
];
|
|
83
|
-
}
|
|
84
|
-
export function vec3ClampLength(v, minLength, maxLength) {
|
|
85
|
-
const length = vec3Length(v);
|
|
86
|
-
if (length === 0) {
|
|
87
|
-
return v; // Can't scale a zero-length vector
|
|
88
|
-
}
|
|
89
|
-
const clampedLength = clamp(length, minLength, maxLength);
|
|
90
|
-
return vec3MultiplyScalar(v, clampedLength / length);
|
|
91
|
-
}
|
|
92
|
-
export function vec3ClampScalar(v, min, max) {
|
|
93
|
-
return [clamp(v[0], min, max), clamp(v[1], min, max), clamp(v[2], min, max)];
|
|
94
|
-
}
|
|
95
|
-
export function vec3Cross(a, b) {
|
|
96
|
-
return [
|
|
97
|
-
diffOfProducts(a[1], b[2], a[2], b[1]),
|
|
98
|
-
diffOfProducts(a[2], b[0], a[0], b[2]),
|
|
99
|
-
diffOfProducts(a[0], b[1], a[1], b[0]),
|
|
100
|
-
];
|
|
101
|
-
}
|
|
102
|
-
export function vec3DistanceTo(a, b, type = "euclidean") {
|
|
103
|
-
const dx = Math.abs(a[0] - b[0]);
|
|
104
|
-
const dy = Math.abs(a[1] - b[1]);
|
|
105
|
-
const dz = Math.abs(a[2] - b[2]);
|
|
106
|
-
switch (type) {
|
|
107
|
-
case "euclidean":
|
|
108
|
-
return Math.hypot(dx, dy, dz);
|
|
109
|
-
case "manhattan":
|
|
110
|
-
return dx + dy + dz;
|
|
111
|
-
case "chebyshev":
|
|
112
|
-
return Math.max(dx, dy, dz);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
export function vec3DistanceToSq(a, b) {
|
|
116
|
-
const dx = a[0] - b[0];
|
|
117
|
-
const dy = a[1] - b[1];
|
|
118
|
-
const dz = a[2] - b[2];
|
|
119
|
-
return dx * dx + dy * dy + dz * dz;
|
|
120
|
-
}
|
|
121
|
-
export function vec3Divide(a, b) {
|
|
122
|
-
return [a[0] / b[0], a[1] / b[1], a[2] / b[2]];
|
|
123
|
-
}
|
|
124
|
-
export function vec3DivideScalar(v, s) {
|
|
125
|
-
return [v[0] / s, v[1] / s, v[2] / s];
|
|
126
|
-
}
|
|
127
|
-
export function vec3Dot(a, b) {
|
|
128
|
-
return dotPrecise(a, b);
|
|
129
|
-
}
|
|
130
|
-
/** Exact component-wise equality. See {@link vec3IsClose} for a tolerant test. */
|
|
131
|
-
export function vec3Equals(a, b) {
|
|
132
|
-
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2];
|
|
96
|
+
export function vec3FromHomogeneous(out, a) {
|
|
97
|
+
const w = a[3];
|
|
98
|
+
out[0] = a[0] / w;
|
|
99
|
+
out[1] = a[1] / w;
|
|
100
|
+
out[2] = a[2] / w;
|
|
101
|
+
return out;
|
|
133
102
|
}
|
|
134
103
|
/**
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
* required: a vector carries no units of its own, so only the caller knows what
|
|
140
|
-
* counts as close.
|
|
104
|
+
* Writes `a` rotated by `angle` radians about the unit vector `axis`
|
|
105
|
+
* (Rodrigues' formula), counter-clockwise seen from the tip of `axis`, into
|
|
106
|
+
* `out`. `axis` is not normalised here: a vector that is not unit scales the
|
|
107
|
+
* result. `out` may be `a` or `axis`.
|
|
141
108
|
*/
|
|
142
|
-
export function
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
109
|
+
export function vec3RotateAxisAngle(out, a, axis, angle) {
|
|
110
|
+
const x = a[0];
|
|
111
|
+
const y = a[1];
|
|
112
|
+
const z = a[2];
|
|
113
|
+
const kx = axis[0];
|
|
114
|
+
const ky = axis[1];
|
|
115
|
+
const kz = axis[2];
|
|
116
|
+
const cos = Math.cos(angle);
|
|
117
|
+
const sin = Math.sin(angle);
|
|
118
|
+
const along = (kx * x + ky * y + kz * z) * (1 - cos);
|
|
119
|
+
out[0] = x * cos + (ky * z - kz * y) * sin + kx * along;
|
|
120
|
+
out[1] = y * cos + (kz * x - kx * z) * sin + ky * along;
|
|
121
|
+
out[2] = z * cos + (kx * y - ky * x) * sin + kz * along;
|
|
122
|
+
return out;
|
|
146
123
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
124
|
+
/**
|
|
125
|
+
* Writes into `out` a unit vector perpendicular to the scalar components `[x,
|
|
126
|
+
* y, z]`: their cross product with the axis of the smallest component, which is
|
|
127
|
+
* as far from parallel as an axis can be, normalised at any magnitude.
|
|
128
|
+
*/
|
|
129
|
+
function perpendicular_(out, x, y, z) {
|
|
130
|
+
const absoluteX = Math.abs(x);
|
|
131
|
+
const absoluteY = Math.abs(y);
|
|
132
|
+
const absoluteZ = Math.abs(z);
|
|
133
|
+
if (absoluteX <= absoluteY && absoluteX <= absoluteZ) {
|
|
134
|
+
out[0] = 0;
|
|
135
|
+
out[1] = z;
|
|
136
|
+
out[2] = -y;
|
|
158
137
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
export function vec3Lerp(a, b, t) {
|
|
164
|
-
return [lerp(a[0], b[0], t), lerp(a[1], b[1], t), lerp(a[2], b[2], t)];
|
|
165
|
-
}
|
|
166
|
-
export function vec3Max(a, b) {
|
|
167
|
-
return [Math.max(a[0], b[0]), Math.max(a[1], b[1]), Math.max(a[2], b[2])];
|
|
168
|
-
}
|
|
169
|
-
export function vec3Min(a, b) {
|
|
170
|
-
return [Math.min(a[0], b[0]), Math.min(a[1], b[1]), Math.min(a[2], b[2])];
|
|
171
|
-
}
|
|
172
|
-
export function vec3Minmax(vs) {
|
|
173
|
-
let minX = Infinity, minY = Infinity, minZ = Infinity;
|
|
174
|
-
let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity;
|
|
175
|
-
for (const v of vs) {
|
|
176
|
-
if (v[0] < minX)
|
|
177
|
-
minX = v[0];
|
|
178
|
-
if (v[1] < minY)
|
|
179
|
-
minY = v[1];
|
|
180
|
-
if (v[2] < minZ)
|
|
181
|
-
minZ = v[2];
|
|
182
|
-
if (v[0] > maxX)
|
|
183
|
-
maxX = v[0];
|
|
184
|
-
if (v[1] > maxY)
|
|
185
|
-
maxY = v[1];
|
|
186
|
-
if (v[2] > maxZ)
|
|
187
|
-
maxZ = v[2];
|
|
138
|
+
else if (absoluteY <= absoluteZ) {
|
|
139
|
+
out[0] = -z;
|
|
140
|
+
out[1] = 0;
|
|
141
|
+
out[2] = x;
|
|
188
142
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
}
|
|
194
|
-
export function vec3MultiplyScalar(v, s) {
|
|
195
|
-
return [v[0] * s, v[1] * s, v[2] * s];
|
|
196
|
-
}
|
|
197
|
-
export function vec3Negate(v) {
|
|
198
|
-
return [-v[0], -v[1], -v[2]];
|
|
199
|
-
}
|
|
200
|
-
export function vec3Normalize(v) {
|
|
201
|
-
const length = vec3Length(v);
|
|
202
|
-
if (length === 0) {
|
|
203
|
-
throw new Error("Cannot normalize a zero-length vector");
|
|
143
|
+
else {
|
|
144
|
+
out[0] = y;
|
|
145
|
+
out[1] = -x;
|
|
146
|
+
out[2] = 0;
|
|
204
147
|
}
|
|
205
|
-
return
|
|
148
|
+
return vec3NormalizeScaled(out, out);
|
|
206
149
|
}
|
|
207
150
|
/**
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
* The
|
|
212
|
-
* but its direction is arbitrary. If the input vector is a zero vector, the
|
|
213
|
-
* function will throw an error.
|
|
151
|
+
* Writes a unit vector perpendicular to `a` into `out`: `a` crossed with the
|
|
152
|
+
* axis of its smallest component, normalised at any magnitude. The result is
|
|
153
|
+
* not unique; this one is stable, so a continuous input gives it a few jumps
|
|
154
|
+
* only. The zero vector stays zero. `out` may be `a`.
|
|
214
155
|
*/
|
|
215
|
-
export function vec3Perpendicular(
|
|
216
|
-
|
|
217
|
-
let other;
|
|
218
|
-
if (Math.abs(v[0]) < Math.abs(v[1])) {
|
|
219
|
-
other = [1, 0, 0];
|
|
220
|
-
}
|
|
221
|
-
else {
|
|
222
|
-
other = [0, 1, 0];
|
|
223
|
-
}
|
|
224
|
-
// Compute the cross product to get a perpendicular vector
|
|
225
|
-
const perp = vec3Cross(v, other);
|
|
226
|
-
// Normalize the perpendicular vector
|
|
227
|
-
return vec3Normalize(perp);
|
|
156
|
+
export function vec3Perpendicular(out, a) {
|
|
157
|
+
return perpendicular_(out, a[0], a[1], a[2]);
|
|
228
158
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
159
|
+
/**
|
|
160
|
+
* Writes the tangent and the bitangent that complete the unit vector `normal`
|
|
161
|
+
* to a right-handed orthonormal basis (`tangent × bitangent = normal`), by
|
|
162
|
+
* Duff, Burgess, Christensen, Hery, Kensler, Liani and Villemin's branch-light
|
|
163
|
+
* construction ("Building an Orthonormal Basis, Revisited", 2017): no
|
|
164
|
+
* normalisation and no singularity, whatever the direction of `normal`.
|
|
165
|
+
* `normal` is not normalised here. The outputs may be `normal`.
|
|
166
|
+
*/
|
|
167
|
+
export function vec3OrthonormalBasis(outTangent, outBitangent, normal) {
|
|
168
|
+
const x = normal[0];
|
|
169
|
+
const y = normal[1];
|
|
170
|
+
const z = normal[2];
|
|
171
|
+
const sign = z < 0 ? -1 : 1;
|
|
172
|
+
const a = -1 / (sign + z);
|
|
173
|
+
const b = x * y * a;
|
|
174
|
+
outTangent[0] = 1 + sign * x * x * a;
|
|
175
|
+
outTangent[1] = sign * b;
|
|
176
|
+
outTangent[2] = -sign * x;
|
|
177
|
+
outBitangent[0] = b;
|
|
178
|
+
outBitangent[1] = sign + y * y * a;
|
|
179
|
+
outBitangent[2] = -y;
|
|
236
180
|
}
|
|
237
|
-
|
|
238
|
-
|
|
181
|
+
/**
|
|
182
|
+
* Writes `a` minus its component along `normal` into `out`: `a` flattened onto
|
|
183
|
+
* the plane through the origin whose normal is `normal`. `normal` need not be a
|
|
184
|
+
* unit vector; a zero `normal` gives `NaN`. Right when the largest component of
|
|
185
|
+
* `normal` lies between about `1e−150` and `1e150`, where its squared length
|
|
186
|
+
* stays in range: above about `1.3e154` the result is `a` unflattened, and
|
|
187
|
+
* below about `1.5e−154` it is not finite. Beyond, normalise `normal` with
|
|
188
|
+
* {@link vec3NormalizeScaled} first. `out` may be `a` or `normal`.
|
|
189
|
+
*/
|
|
190
|
+
export function vec3ProjectOntoPlane(out, a, normal) {
|
|
191
|
+
const x = a[0];
|
|
192
|
+
const y = a[1];
|
|
193
|
+
const z = a[2];
|
|
194
|
+
const nx = normal[0];
|
|
195
|
+
const ny = normal[1];
|
|
196
|
+
const nz = normal[2];
|
|
197
|
+
const scale = (x * nx + y * ny + z * nz) / (nx * nx + ny * ny + nz * nz);
|
|
198
|
+
out[0] = x - nx * scale;
|
|
199
|
+
out[1] = y - ny * scale;
|
|
200
|
+
out[2] = z - nz * scale;
|
|
201
|
+
return out;
|
|
239
202
|
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
];
|
|
203
|
+
/**
|
|
204
|
+
* Writes `m · a` into `out`, the matrix applied to the column vector. `out` may
|
|
205
|
+
* be `a`.
|
|
206
|
+
*/
|
|
207
|
+
export function vec3TransformMat3x3(out, a, m) {
|
|
208
|
+
const x = a[0];
|
|
209
|
+
const y = a[1];
|
|
210
|
+
const z = a[2];
|
|
211
|
+
out[0] = m[0] * x + m[3] * y + m[6] * z;
|
|
212
|
+
out[1] = m[1] * x + m[4] * y + m[7] * z;
|
|
213
|
+
out[2] = m[2] * x + m[5] * y + m[8] * z;
|
|
214
|
+
return out;
|
|
250
215
|
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
216
|
+
/**
|
|
217
|
+
* {@link vec3TransformMat3x3} with each component a compensated dot product, as
|
|
218
|
+
* accurate as one in doubled precision rounded once. `out` may be `a`.
|
|
219
|
+
*/
|
|
220
|
+
export function vec3TransformMat3x3Precise(out, a, m) {
|
|
221
|
+
const x = a[0];
|
|
222
|
+
const y = a[1];
|
|
223
|
+
const z = a[2];
|
|
224
|
+
out[0] = dotPrecise3(m[0], x, m[3], y, m[6], z);
|
|
225
|
+
out[1] = dotPrecise3(m[1], x, m[4], y, m[7], z);
|
|
226
|
+
out[2] = dotPrecise3(m[2], x, m[5], y, m[8], z);
|
|
227
|
+
return out;
|
|
254
228
|
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
];
|
|
229
|
+
/**
|
|
230
|
+
* Writes the point `a` (as `[x, y, z, 1]`) transformed by `m` into `out`,
|
|
231
|
+
* divided by the resulting `w`, as gl-matrix and three.js do: for an affine
|
|
232
|
+
* matrix `w` is exactly `1` and nothing changes, for a projection this is the
|
|
233
|
+
* perspective divide. A `w` of zero gives non-finite components, not an
|
|
234
|
+
* exception. `out` may be `a`.
|
|
235
|
+
*/
|
|
236
|
+
export function vec3TransformMat4x4(out, a, m) {
|
|
237
|
+
const x = a[0];
|
|
238
|
+
const y = a[1];
|
|
239
|
+
const z = a[2];
|
|
240
|
+
const w = m[3] * x + m[7] * y + m[11] * z + m[15];
|
|
241
|
+
out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w;
|
|
242
|
+
out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w;
|
|
243
|
+
out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w;
|
|
244
|
+
return out;
|
|
266
245
|
}
|
|
267
|
-
|
|
268
|
-
|
|
246
|
+
/**
|
|
247
|
+
* Writes the direction `a` transformed by the upper-left 3×3 part of `m`, with
|
|
248
|
+
* no translation and no divide, into `out`. `out` may be `a`.
|
|
249
|
+
*/
|
|
250
|
+
export function vec3TransformDirectionMat4x4(out, a, m) {
|
|
251
|
+
const x = a[0];
|
|
252
|
+
const y = a[1];
|
|
253
|
+
const z = a[2];
|
|
254
|
+
out[0] = m[0] * x + m[4] * y + m[8] * z;
|
|
255
|
+
out[1] = m[1] * x + m[5] * y + m[9] * z;
|
|
256
|
+
out[2] = m[2] * x + m[6] * y + m[10] * z;
|
|
257
|
+
return out;
|
|
269
258
|
}
|
|
270
|
-
|
|
271
|
-
|
|
259
|
+
/**
|
|
260
|
+
* Writes the point `a` transformed by the affine transform (the linear part
|
|
261
|
+
* applied, then the translation added) into `out`. `out` may be `a`.
|
|
262
|
+
*/
|
|
263
|
+
export function vec3TransformAffine3(out, a, transform) {
|
|
264
|
+
const x = a[0];
|
|
265
|
+
const y = a[1];
|
|
266
|
+
const z = a[2];
|
|
267
|
+
out[0] =
|
|
268
|
+
transform[0] * x + transform[3] * y + transform[6] * z + transform[9];
|
|
269
|
+
out[1] =
|
|
270
|
+
transform[1] * x + transform[4] * y + transform[7] * z + transform[10];
|
|
271
|
+
out[2] =
|
|
272
|
+
transform[2] * x + transform[5] * y + transform[8] * z + transform[11];
|
|
273
|
+
return out;
|
|
272
274
|
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
+
/**
|
|
276
|
+
* Writes the direction `a` transformed by the linear part of the affine
|
|
277
|
+
* transform, with no translation, into `out`. `out` may be `a`.
|
|
278
|
+
*/
|
|
279
|
+
export function vec3TransformDirectionAffine3(out, a, transform) {
|
|
280
|
+
const x = a[0];
|
|
281
|
+
const y = a[1];
|
|
282
|
+
const z = a[2];
|
|
283
|
+
out[0] = transform[0] * x + transform[3] * y + transform[6] * z;
|
|
284
|
+
out[1] = transform[1] * x + transform[4] * y + transform[7] * z;
|
|
285
|
+
out[2] = transform[2] * x + transform[5] * y + transform[8] * z;
|
|
286
|
+
return out;
|
|
275
287
|
}
|
|
276
|
-
|
|
277
|
-
|
|
288
|
+
/**
|
|
289
|
+
* Writes `a` rotated by the unit quaternion `q` into `out`: `q · a · q⁻¹` in 18
|
|
290
|
+
* multiplications, no matrix. `q` must be unit length; it is not normalised
|
|
291
|
+
* here. `out` may be `a`.
|
|
292
|
+
*/
|
|
293
|
+
export function vec3TransformQuaternion(out, a, q) {
|
|
294
|
+
const x = a[0];
|
|
295
|
+
const y = a[1];
|
|
296
|
+
const z = a[2];
|
|
297
|
+
const qx = q[0];
|
|
298
|
+
const qy = q[1];
|
|
299
|
+
const qz = q[2];
|
|
300
|
+
const qw = q[3];
|
|
301
|
+
const tx = 2 * (qy * z - qz * y);
|
|
302
|
+
const ty = 2 * (qz * x - qx * z);
|
|
303
|
+
const tz = 2 * (qx * y - qy * x);
|
|
304
|
+
out[0] = x + qw * tx + (qy * tz - qz * ty);
|
|
305
|
+
out[1] = y + qw * ty + (qz * tx - qx * tz);
|
|
306
|
+
out[2] = z + qw * tz + (qx * ty - qy * tx);
|
|
307
|
+
return out;
|
|
278
308
|
}
|
|
279
309
|
/**
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
*
|
|
310
|
+
* Writes the interpolation of `a` and `b` along the great circle into `out`:
|
|
311
|
+
* the direction turns at constant angular speed along the shorter arc, and the
|
|
312
|
+
* length is interpolated linearly. The inputs need not be unit vectors. For `t`
|
|
313
|
+
* in `[0, 1]` and nearly parallel directions (about 1.8° apart or less) the
|
|
314
|
+
* directions are interpolated linearly and normalised, which traces the same
|
|
315
|
+
* arc. The arc lies in the plane of the two vectors, however close to opposite
|
|
316
|
+
* they are (`(1, 0, 0)` to `(−1, 0, 10⁻⁹)` passes through `+z`); for exactly
|
|
317
|
+
* opposite directions, where every great circle is as short, it turns about the
|
|
318
|
+
* axis of {@link vec3Perpendicular} of `a`. The length is interpolated all the
|
|
319
|
+
* same, so `t = 0.5` of opposite vectors has the mean length, never the origin
|
|
320
|
+
* that a straight line between them would give. `t` outside `[0, 1]`
|
|
321
|
+
* extrapolates along the same circle at the same angular speed. A zero vector
|
|
322
|
+
* has no direction: the result is `NaN`. The lengths are `Math.sqrt` of sums of
|
|
323
|
+
* squares, so the result is right for lengths between about `1e−150` and
|
|
324
|
+
* `1e150`; beyond, normalise with {@link vec3NormalizeScaled} first. `out` may
|
|
325
|
+
* be `a` or `b`.
|
|
283
326
|
*/
|
|
284
|
-
export function vec3Slerp(a, b, t) {
|
|
285
|
-
const
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
327
|
+
export function vec3Slerp(out, a, b, t) {
|
|
328
|
+
const lengthA = vec3Length(a);
|
|
329
|
+
const lengthB = vec3Length(b);
|
|
330
|
+
const ax = a[0] / lengthA;
|
|
331
|
+
const ay = a[1] / lengthA;
|
|
332
|
+
const az = a[2] / lengthA;
|
|
333
|
+
const bx = b[0] / lengthB;
|
|
334
|
+
const by = b[1] / lengthB;
|
|
335
|
+
const bz = b[2] / lengthB;
|
|
336
|
+
const length = lerp(lengthA, lengthB, t);
|
|
337
|
+
const dot = ax * bx + ay * by + az * bz;
|
|
338
|
+
if (dot > SLERP_LINEAR_FALLBACK_DOT_ && t >= 0 && t <= 1) {
|
|
339
|
+
const x = lerp(ax, bx, t);
|
|
340
|
+
const y = lerp(ay, by, t);
|
|
341
|
+
const z = lerp(az, bz, t);
|
|
342
|
+
const scale = length / Math.sqrt(x * x + y * y + z * z);
|
|
343
|
+
out[0] = x * scale;
|
|
344
|
+
out[1] = y * scale;
|
|
345
|
+
out[2] = z * scale;
|
|
346
|
+
return out;
|
|
289
347
|
}
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
348
|
+
// The axis of the turn is `a × b`, normalised after dividing by its largest
|
|
349
|
+
// component so that a tiny one (nearly opposite directions) keeps its digits.
|
|
350
|
+
const cx = ay * bz - az * by;
|
|
351
|
+
const cy = az * bx - ax * bz;
|
|
352
|
+
const cz = ax * by - ay * bx;
|
|
353
|
+
const peak = Math.max(Math.abs(cx), Math.abs(cy), Math.abs(cz));
|
|
354
|
+
// The unit vector, perpendicular to `a`, toward `b` in their plane.
|
|
355
|
+
let rx;
|
|
356
|
+
let ry;
|
|
357
|
+
let rz;
|
|
358
|
+
let sine = 0;
|
|
359
|
+
if (peak > 0) {
|
|
360
|
+
const ux = cx / peak;
|
|
361
|
+
const uy = cy / peak;
|
|
362
|
+
const uz = cz / peak;
|
|
363
|
+
const norm = Math.sqrt(ux * ux + uy * uy + uz * uz);
|
|
364
|
+
const nx = ux / norm;
|
|
365
|
+
const ny = uy / norm;
|
|
366
|
+
const nz = uz / norm;
|
|
367
|
+
rx = ny * az - nz * ay;
|
|
368
|
+
ry = nz * ax - nx * az;
|
|
369
|
+
rz = nx * ay - ny * ax;
|
|
370
|
+
sine = peak * norm;
|
|
294
371
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
const [vec, w] = getWeightedValue(item);
|
|
306
|
-
x.add(vec[0] * w);
|
|
307
|
-
y.add(vec[1] * w);
|
|
308
|
-
z.add(vec[2] * w);
|
|
309
|
-
totalWeight.add(w);
|
|
372
|
+
else {
|
|
373
|
+
// Parallel or opposite, exactly (or `NaN`, which `perpendicular_` carries
|
|
374
|
+
// through): any plane holds both.
|
|
375
|
+
perpendicular_(out, ax, ay, az);
|
|
376
|
+
const px = out[0];
|
|
377
|
+
const py = out[1];
|
|
378
|
+
const pz = out[2];
|
|
379
|
+
rx = py * az - pz * ay;
|
|
380
|
+
ry = pz * ax - px * az;
|
|
381
|
+
rz = px * ay - py * ax;
|
|
310
382
|
}
|
|
311
|
-
const
|
|
312
|
-
|
|
383
|
+
const theta = Math.atan2(sine, dot) * t;
|
|
384
|
+
const cos = Math.cos(theta) * length;
|
|
385
|
+
const sin = Math.sin(theta) * length;
|
|
386
|
+
out[0] = ax * cos + rx * sin;
|
|
387
|
+
out[1] = ay * cos + ry * sin;
|
|
388
|
+
out[2] = az * cos + rz * sin;
|
|
389
|
+
return out;
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Writes a unit vector in a uniformly random direction (uniform on the sphere:
|
|
393
|
+
* `z` uniform in `[−1, 1]`, the azimuth uniform in `[0, 2π)`) into `out`.
|
|
394
|
+
*
|
|
395
|
+
* @param out The vector to fill.
|
|
396
|
+
* @param random A source of numbers in `[0, 1)`; defaults to `Math.random`.
|
|
397
|
+
*/
|
|
398
|
+
export function vec3Random(out, random = Math.random) {
|
|
399
|
+
const azimuth = random() * TWO_PI;
|
|
400
|
+
const z = 2 * random() - 1;
|
|
401
|
+
const radius = Math.sqrt(1 - z * z);
|
|
402
|
+
out[0] = radius * Math.cos(azimuth);
|
|
403
|
+
out[1] = radius * Math.sin(azimuth);
|
|
404
|
+
out[2] = z;
|
|
405
|
+
return out;
|
|
313
406
|
}
|
|
314
407
|
//# sourceMappingURL=vec3.js.map
|