@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/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,342 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.4.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 0b4fe43: `mat4x4ConvertDepthRange(out, matrix, from, to)` re-expresses a projection
|
|
8
|
+
built for one `DepthRange` in another: the `x`, `y` and `w` rows are copied and
|
|
9
|
+
the depth row maps the near and far planes of `from` to those of `to`, with
|
|
10
|
+
exact coefficients (one rounding per depth-row element). A GL-style
|
|
11
|
+
`"negative-one-to-one"` projection then draws under WebGPU's `"zero-to-one"`
|
|
12
|
+
clip depth, and a forward projection turns reversed. `out` may be `matrix`; an
|
|
13
|
+
unknown range throws a `RangeError` and leaves `out` untouched.
|
|
14
|
+
- 0b4fe43: Add `vec2ArraySplitStrided`, `vec3ArraySplitStrided` and `vec4ArraySplitStrided`: one pass that writes each binary64 number as two binary32 parts into an interleaved buffer, `high = fround(x)` and `low = fround(x − high)` at their own offsets, with no allocation; the parts of one element must not overlap each other or another element's. `vec4ArrayNarrowRelative` and `vec4ArrayNarrowRelativeStrided` now exist too (an origin of four components, for boxes stored as two corners).
|
|
15
|
+
|
|
16
|
+
### Patch Changes
|
|
17
|
+
|
|
18
|
+
- 0b4fe43: Wide-range inputs no longer fail or lose their result.
|
|
19
|
+
|
|
20
|
+
- `quaternionSlerp` and `vec4Slerp` take their angle from the tangent part, so
|
|
21
|
+
a tiny rotation extrapolated by a huge `t` stays on the unit sphere (`1e-16`
|
|
22
|
+
rad at `t = 1e16` gives the quaternion half a radian on) and a rounding off
|
|
23
|
+
the unit sphere does not grow with `t`. `vec3Slerp` between exact antipodes of
|
|
24
|
+
different lengths interpolates the lengths and turns on a perpendicular arc,
|
|
25
|
+
keeps a pair a rounding away from opposite in its own plane, and slerps
|
|
26
|
+
vectors of magnitude `1e±200`.
|
|
27
|
+
- `mat2x2Invert`, `mat3x3Invert`, `mat4x4Invert`, `affine2Invert` and
|
|
28
|
+
`affine3Invert` invert every matrix whose inverse is representable, however
|
|
29
|
+
widely its entries span (`diag(1e160, 1e160, 1e-160)`, scales of `2^600` with
|
|
30
|
+
a translation of `Number.MAX_VALUE`) and however its cofactors cancel: `false`
|
|
31
|
+
means singular, or an inverse entry beyond `Number.MAX_VALUE` once rounded,
|
|
32
|
+
never a loss to rounding, and `true` is never a rounding artefact
|
|
33
|
+
(`diag(2^-1024, 4237)` is refused). A nearly singular matrix is no longer
|
|
34
|
+
inverted from a rounding residue: its determinant is kept within `2^-41` of
|
|
35
|
+
itself, or its inverse is computed by a backward-stable elimination. A
|
|
36
|
+
well-conditioned matrix gives the same result as before, at about 1.4× its
|
|
37
|
+
former cost for a 4 × 4 view matrix. Inversion allocates nothing, except for
|
|
38
|
+
a matrix of condition number beyond about `2^52`, an inverse that overflows
|
|
39
|
+
or comes within its error bound of overflowing, or a matrix whose scaling
|
|
40
|
+
rounds an entry.
|
|
41
|
+
- Each translation component of an affine inverse is within `2^-41` of `−B · t`
|
|
42
|
+
for the inverse linear part `B`, and correctly rounded whenever plain
|
|
43
|
+
arithmetic cannot guarantee that (a linear part whose inverse starts with
|
|
44
|
+
`(1, 1, 1)` and a translation of `Number.MAX_VALUE`, `1` and
|
|
45
|
+
`-Number.MAX_VALUE` give `-1`, not `0`). Whether an affine inverse overflows
|
|
46
|
+
follows the exact translation `−A⁻¹ · t`, as for the equivalent homogeneous
|
|
47
|
+
matrix: an x scale of `161/256` with an x translation of `161 · 2^1016` is
|
|
48
|
+
refused, and a translation whose `−B · t` overflows while the exact one fits
|
|
49
|
+
is inverted.
|
|
50
|
+
- Updated dependencies [0b4fe43]
|
|
51
|
+
- Updated dependencies [0b4fe43]
|
|
52
|
+
- Updated dependencies [0b4fe43]
|
|
53
|
+
- Updated dependencies [0b4fe43]
|
|
54
|
+
- Updated dependencies [0b4fe43]
|
|
55
|
+
- Updated dependencies [0b4fe43]
|
|
56
|
+
- Updated dependencies [0b4fe43]
|
|
57
|
+
- Updated dependencies [0b4fe43]
|
|
58
|
+
- Updated dependencies [0b4fe43]
|
|
59
|
+
- Updated dependencies [0b4fe43]
|
|
60
|
+
- Updated dependencies [0b4fe43]
|
|
61
|
+
- Updated dependencies [0b4fe43]
|
|
62
|
+
- Updated dependencies [0b4fe43]
|
|
63
|
+
- Updated dependencies [0b4fe43]
|
|
64
|
+
- Updated dependencies [0b4fe43]
|
|
65
|
+
- Updated dependencies [0b4fe43]
|
|
66
|
+
- Updated dependencies [0b4fe43]
|
|
67
|
+
- Updated dependencies [0b4fe43]
|
|
68
|
+
- Updated dependencies [0b4fe43]
|
|
69
|
+
- Updated dependencies [0b4fe43]
|
|
70
|
+
- Updated dependencies [0b4fe43]
|
|
71
|
+
- Updated dependencies [0b4fe43]
|
|
72
|
+
- Updated dependencies [0b4fe43]
|
|
73
|
+
- Updated dependencies [0b4fe43]
|
|
74
|
+
- Updated dependencies [0b4fe43]
|
|
75
|
+
- Updated dependencies [0b4fe43]
|
|
76
|
+
- Updated dependencies [0b4fe43]
|
|
77
|
+
- Updated dependencies [0b4fe43]
|
|
78
|
+
- Updated dependencies [0b4fe43]
|
|
79
|
+
- @ac-kit/core@0.13.0
|
|
80
|
+
- @ac-kit/math-scalar@0.4.0
|
|
81
|
+
- @ac-kit/math-complex@0.2.1
|
|
82
|
+
|
|
83
|
+
## 0.3.0
|
|
84
|
+
|
|
85
|
+
### Minor Changes
|
|
86
|
+
|
|
87
|
+
- 34e8cfa: Buffers of vectors, matrices and transforms: a `Float64Array` of `count × N`
|
|
88
|
+
numbers, element `i` at `[i·N, i·N + N)`, handled by dedicated kernels instead
|
|
89
|
+
of a load, compute and store loop per element. The value types and their
|
|
90
|
+
functions are unchanged; a buffer is never an argument of a value function and
|
|
91
|
+
a value is never one of a kernel.
|
|
92
|
+
|
|
93
|
+
- **Boundary, for every value type with a buffer** (`vec2/3/4`, `mat2x2`,
|
|
94
|
+
`mat3x3`, `mat4x4`, `affine2`, `affine3`, `quaternion`): `…Load(out, array,
|
|
95
|
+
index)` and `…Store(array, index, value)` copy element `index`;
|
|
96
|
+
`…LoadStrided` and `…StoreStrided` take `(offset, stride)` in numbers for
|
|
97
|
+
interleaved data; `…ArrayIsFinite(array)`; `…ArrayCreate(count)` (vectors,
|
|
98
|
+
`mat4x4`, `affine2`, `affine3`, `quaternion`). `…Load` past the end of the
|
|
99
|
+
array writes `NaN` and never `undefined`; `…Store` past the end writes
|
|
100
|
+
nothing. `affine2StoreFloat32` and `affine3StoreFloat32` keep their
|
|
101
|
+
signatures.
|
|
102
|
+
- **Vector kernels** (`vec2/3/4`): `…ArrayAdd`, `…ArraySubtract`,
|
|
103
|
+
`…ArrayMultiplyScalar`, `…ArrayAddScaled`, `…ArrayTranslate`, `…ArrayLerp`,
|
|
104
|
+
`…ArrayNormalize`, `…ArrayMean`, `…ArrayWeightedMean` and
|
|
105
|
+
`…ArrayWeightedMeanPrecise` (exact sums through `PreciseSum`, for products
|
|
106
|
+
between about `1e−290` and `1e300`: the replacement of 0.2.1's
|
|
107
|
+
`vecNWeightedMean(items, get)`), `…ArrayFromValues` and `…ArrayToValues`.
|
|
108
|
+
`vec2`/`vec3` add `…ArrayMeanDistance` and `…ArrayNarrowRelative`; `vec2` adds
|
|
109
|
+
`vec2ArrayFindNearestXy`, `vec3` `vec3ArrayFindNearest`. Transforms:
|
|
110
|
+
`vec2ArrayTransformAffine2`, `vec2ArrayTransformMat3x3`,
|
|
111
|
+
`vec3ArrayTransformMat3x3`, `…TransformNormal` (transform and normalise in
|
|
112
|
+
one pass), `…TransformAffine3`, `…TransformDirectionAffine3`,
|
|
113
|
+
`…TransformMat4x4` (divides by `w`) and `vec4ArrayTransformMat4x4`.
|
|
114
|
+
- **Interleaved and f32.** `…Strided` kernels take `(out, outOffset,
|
|
115
|
+
outStride, input, inputOffset, inputStride, count, …)` in numbers:
|
|
116
|
+
`vec2ArrayTransformAffine2Strided`, `vec3ArrayTransformAffine3Strided`,
|
|
117
|
+
`vec3ArrayTransformMat4x4Strided`. `…ArrayNarrowStrided` writes a tight
|
|
118
|
+
binary64 buffer into an interleaved `Float32Array`, `…ArrayWidenStrided` reads
|
|
119
|
+
one back, and `…ArrayNarrowRelative(Strided)` subtracts an origin before
|
|
120
|
+
narrowing, so large coordinates keep their precision. No kernel computes on a
|
|
121
|
+
`Float32Array`; whole-array narrowing is `Float32Array.prototype.set`.
|
|
122
|
+
- **Contract.** The element count is `input.length / N`. A kernel checks once,
|
|
123
|
+
in O(1), that its lengths agree and are a multiple of `N` (strided ones that
|
|
124
|
+
`count`, the offsets and the strides are non-negative integers, the strides at
|
|
125
|
+
least `N` from two elements on, and the elements fit; `out` and `input` may
|
|
126
|
+
overlap only as the same elements at the same offset and stride; narrowing and
|
|
127
|
+
widening need views that share no memory) and throws a `RangeError` before
|
|
128
|
+
writing anything; there is no per-element check. `out` may be the input array
|
|
129
|
+
itself, not a partially overlapping view of it. `…ArrayCreate(count)` takes a
|
|
130
|
+
non-negative integer. Empty buffers are fine; means of nothing are `NaN`.
|
|
131
|
+
- 34e8cfa: `Quaternion` is now the branded `[x, y, z, w]` tuple of `@ac-kit/math-complex`
|
|
132
|
+
and `@ac-kit/math-linear` re-exports its quaternion functions
|
|
133
|
+
(`quaternionOf`, `quaternionMultiply`, `quaternionInvert`, …), all out first. An
|
|
134
|
+
array literal no longer type-checks as a `Quaternion`: build one with
|
|
135
|
+
`quaternionOf(x, y, z, w)`. The rotation functions write such values. `quaternionNlerp` and `quaternionSlerp` take the
|
|
136
|
+
shorter side by the plain dot product's sign, and by the exact sign where the
|
|
137
|
+
plain one is within its rounding error, so two rotations whose dot product
|
|
138
|
+
cancels to a few ulp, even to `−2⁻¹²⁰`, no longer interpolate the long way
|
|
139
|
+
round.
|
|
140
|
+
- 34e8cfa: Vectors, matrices and the other value types are flat, branded arrays made by
|
|
141
|
+
creators, and every function takes `out` first. This part covers the vectors,
|
|
142
|
+
the value types, the matrices, the affine and TRS forms, the rotations, the
|
|
143
|
+
projections and views, the viewport and the uploads; the buffer kernels follow.
|
|
144
|
+
|
|
145
|
+
- **Values.** `Vec2`/`Vec3`/`Vec4`, `Mat2x2`/`Mat3x3`/`Mat4x4` (flat,
|
|
146
|
+
**column-major**: the entry of row `r`, column `c` of an `N×N` matrix is at
|
|
147
|
+
`N·c + r`), `Affine2`, `Affine3`, `Transform2`, `Transform3` and `Viewport`
|
|
148
|
+
are arrays of numbers with a type-level brand. An array literal, a
|
|
149
|
+
`number[]` or a `Float64Array` no longer type-checks: build one with
|
|
150
|
+
`vec3Create()`, `vec3Of(x, y, z)`, `vec3Clone(v)` or `vec3FromArray(out,
|
|
151
|
+
source, offset?)` (the matrices add `mat4x4OfRows(…)`, rows as written on
|
|
152
|
+
paper). Every creator yields a V8 `PACKED_DOUBLE_ELEMENTS` array, integers
|
|
153
|
+
included. `…FromArray` is the checked way in from JSON or messages: an
|
|
154
|
+
element that is not a number, or lies past `source.length`, is stored as
|
|
155
|
+
`NaN`. `…IsFinite` checks a value after parsing.
|
|
156
|
+
- **Operations.** Each takes the destination first, writes it, returns it,
|
|
157
|
+
allocates nothing and accepts any of its inputs as `out`
|
|
158
|
+
(`vec3Add(a, a, b)`). They never throw: normalising the zero vector gives
|
|
159
|
+
zero, `…Invert` of a matrix with no finite inverse (singular, a non-finite
|
|
160
|
+
entry, or an inverse beyond the range of binary64) returns `false` and fills
|
|
161
|
+
`out` with `NaN`, never `true` with a non-finite result (a matrix of one huge
|
|
162
|
+
or tiny magnitude, `diag(1e-150)` or `diag(1e150)`, is scaled by a power of
|
|
163
|
+
two first and inverted), a perspective divide by zero gives non-finite
|
|
164
|
+
components.
|
|
165
|
+
- **Plain arithmetic.** Sums, dots, lengths and products are rounded as written,
|
|
166
|
+
so results can differ from 0.2.1 in the last bits. The compensated forms are
|
|
167
|
+
separate: `…DotPrecise`, `vec2CrossPrecise`, `vec3CrossPrecise`,
|
|
168
|
+
`…TransformMat…Precise`, `mat2x2/3x3/4x4MultiplyPrecise`. The compensated
|
|
169
|
+
forms compute what math-scalar's `sumOfProducts`, `dotPrecise3` and
|
|
170
|
+
`dotPrecise4` do, so a sum that overflows on the way to a finite result is
|
|
171
|
+
still found. The lengths and normalisations are right between about `1e-150`
|
|
172
|
+
and `1e150`; beyond that, `…LengthScaled` and `…NormalizeScaled`. Each
|
|
173
|
+
function that squares components (`…Length`, `…Distance`, `…ClampLength`,
|
|
174
|
+
`…Project`, `…Reject`, `vec3AngleTo`, `…Slerp`) states its range and the
|
|
175
|
+
scaled form to use in its documentation.
|
|
176
|
+
- **Boundary.** `…Load`, `…Store`, `…LoadStrided`, `…StoreStrided`,
|
|
177
|
+
`…StoreFloat32` and `…ArrayIsFinite` move values between a `Float64Array` and
|
|
178
|
+
a vector or matrix. A `Float64Array` is never an argument of a value function.
|
|
179
|
+
- **Vectors in full**: component-wise arithmetic (`Add`, `Subtract`,
|
|
180
|
+
`Multiply`, `Divide`, scalar forms, `AddScaled`, `Negate`, `Abs`, `Round`,
|
|
181
|
+
`Min`, `Max`, `Clamp`, `Lerp`), `Equals`/`IsClose`, `CompareLexicographic`,
|
|
182
|
+
dot, cross, lengths and distances (Euclidean, squared, Manhattan, Chebyshev),
|
|
183
|
+
`Normalize`, `ClampLength`, `Project`, `Reject`, `Reflect`, angles
|
|
184
|
+
(`vec2Angle`, `…AngleTo`, `…AngleBetween`, in `(−π, π]`), `…Slerp` (constant
|
|
185
|
+
angular speed, also when `t` leaves `[0, 1]`, and in the plane of the two
|
|
186
|
+
vectors however close to opposite they are),
|
|
187
|
+
`vec3RotateAxisAngle`, `vec3Perpendicular`, `vec3OrthonormalBasis`,
|
|
188
|
+
`…Random`, and the transforms by matrices, affine forms and quaternions
|
|
189
|
+
(`vec3TransformMat4x4`, `vec3TransformDirectionMat4x4`,
|
|
190
|
+
`vec3TransformAffine3`, `vec3TransformQuaternion`, …).
|
|
191
|
+
- **Matrices.** The 0.2.1 operations on the flat layout: `Multiply`,
|
|
192
|
+
`ScaleColumns`, `ScaleRows`, `Transpose`, `Determinant`, `Invert`, and the
|
|
193
|
+
builders `mat2x2FromRotation`, `mat3x3FromRotationX/Y/Z`,
|
|
194
|
+
`mat4x4FromTranslation`, `FromScale`, `FromRotationX/Y/Z`,
|
|
195
|
+
`FromQuaternion`, `Compose`. Their element-wise families (`Add`, `Subtract`,
|
|
196
|
+
`MultiplyScalar`, `Negate`, `Equals`, `IsClose`, creators and setters) are
|
|
197
|
+
new. New builders: `mat3x3FromQuaternion`, `mat3x3FromAxisAngle`,
|
|
198
|
+
`mat4x4FromAxisAngle`, `…FromMat3x3`, `…FromMat4x4`, `…FromAffine2`,
|
|
199
|
+
`…FromAffine3`; the normal matrices `mat3x3NormalFromMat4x4` and
|
|
200
|
+
`mat3x3NormalFromAffine3`; `mat4x4Decompose` (the inverse of `Compose`),
|
|
201
|
+
`mat4x4MultiplyAffine3`, `mat4x4Translate`, `mat4x4GetTranslation`,
|
|
202
|
+
`mat4x4GetScale`, `mat4x4GetColumn`.
|
|
203
|
+
- **Affine and TRS.** `Affine2` (Canvas's `a, b, c, d, e, f`) and `Affine3` (four
|
|
204
|
+
columns of three, a WGSL `mat4x3f`) with `Multiply`, `Invert` (`false`, with
|
|
205
|
+
`NaN`, for a singular transform or an inverse that does not fit in binary64),
|
|
206
|
+
`Determinant`, the builders (`FromTranslation`, `FromScale`, `FromRotation…`,
|
|
207
|
+
`FromScaleAbout`, `FromRotationAbout`, `FromQuaternion`, `Compose`,
|
|
208
|
+
`affine2Orthographic`), `Translate`/`Rotate`/`Scale` in the local space (as
|
|
209
|
+
Canvas's), `affine3Decompose` and `Equals`/`IsClose`. `Transform2` and
|
|
210
|
+
`Transform3` are translate-rotate-scale records (glTF nodes, animation keys)
|
|
211
|
+
with `Lerp` (exact at both ends; the angle by the shorter arc, the rotation
|
|
212
|
+
by slerp) and conversions to and from the affine forms. Every decomposition
|
|
213
|
+
returns `false`, with `NaN` in its outputs, for a zero scale, a shear (a
|
|
214
|
+
cosine of more than about `1e-6` between two columns) or non-finite input; a
|
|
215
|
+
reflection puts its sign in a scale.
|
|
216
|
+
- **Rotations and Euler angles.** `quaternionFromAxisAngle`, `quaternionAxis`,
|
|
217
|
+
`quaternionAngle`, `quaternionFromVec3ToVec3`, `quaternionFromMat3x3`,
|
|
218
|
+
`quaternionFromMat4x4`, `quaternionRotateX/Y/Z`, `quaternionSlerp`,
|
|
219
|
+
`quaternionNlerp`, `quaternionAngleBetween`. Euler angles in all twelve orders
|
|
220
|
+
(`EulerOrder`), intrinsic, the angles a `Vec3` in the order's sequence:
|
|
221
|
+
`quaternionFromEuler`, `eulerFromQuaternion`, `mat3x3FromEuler`,
|
|
222
|
+
`eulerFromMat3x3`. The first and third angles are in `(−π, π]`; at gimbal
|
|
223
|
+
lock (within `1e-10` rad) the third is `0`.
|
|
224
|
+
- **Projections and views.** `mat4x4Perspective`, `mat4x4Frustum` and
|
|
225
|
+
`mat4x4Orthographic` take the depth range as a required last argument,
|
|
226
|
+
`"zero-to-one"` (WebGPU), `"negative-one-to-one"` (OpenGL) or `"one-to-zero"`
|
|
227
|
+
(reversed depth); the perspective and frustum builders accept `far =
|
|
228
|
+
Infinity` (an orthographic `far` must be finite); `mat4x4LookAt` and
|
|
229
|
+
`affine3TargetTo` build the view and the camera's world transform (right-handed,
|
|
230
|
+
forward along −Z, axes normalised at any magnitude, an orthonormal basis to the
|
|
231
|
+
last bit). They are the builders called once per frame, so they throw a
|
|
232
|
+
`RangeError` for an invalid frustum or view (an `up` vector parallel to the
|
|
233
|
+
view direction to within the rounding of a cross product, a projection scale
|
|
234
|
+
that underflows to zero) and leave `out` untouched. `DepthRange` is the type
|
|
235
|
+
of the argument.
|
|
236
|
+
- **Viewport.** `vec2NdcToViewport`, `vec2ViewportToNdc` (pixels top-left, `y`
|
|
237
|
+
down; NDC `y` up), `vec3ProjectToViewport` (`NaN` on or behind the eye),
|
|
238
|
+
`vec3UnprojectFromViewport` and the bulk kernel `vec3ArrayProjectToViewport`.
|
|
239
|
+
- **Uploads and the checked layer.** `mat2x2/3x3/4x4`, `affine2/3` and
|
|
240
|
+
`quaternion` `…StoreFloat32` (unrolled); the uniform-block forms
|
|
241
|
+
`mat3x3StoreStd140Float32`, `affine3StoreStd140Float32` and
|
|
242
|
+
`affine2StoreStd140Float32` write every float of the field, padding as `0`.
|
|
243
|
+
The new `@ac-kit/math-linear/checked` subpath exports `assertFiniteValue` and
|
|
244
|
+
`assertFiniteBuffer`, which throw a `RangeError` naming the argument and the
|
|
245
|
+
first bad component.
|
|
246
|
+
|
|
247
|
+
Migration. A literal `[1, 2, 3]` becomes `vec3Of(1, 2, 3)`, and a nested matrix
|
|
248
|
+
`[[a, b], [c, d]]` becomes `mat2x2OfRows(a, b, c, d)`. Results are written into
|
|
249
|
+
the first argument: `vec3Add(a, b)` becomes `vec3Add(vec3Create(), a, b)`, or
|
|
250
|
+
reuse an `out`. Renamed: `…Sub` → `…Subtract`; `…SubScalar` →
|
|
251
|
+
`…AddScalar` with a negated scalar; `…SubScaled` → `…AddScaled` with a negated
|
|
252
|
+
scalar; `…LengthSq` → `…LengthSquared`; `…DistanceTo` → `…Distance`;
|
|
253
|
+
`…DistanceToSq` → `…DistanceSquared`; `vec2ApplyMat2` → `vec2TransformMat2x2`;
|
|
254
|
+
`vec2ApplyMat3` → `vec2TransformMat3x3`; `vec3ApplyMat3` →
|
|
255
|
+
`vec3TransformMat3x3`; `vec3ApplyMat4` → `vec3TransformMat4x4`;
|
|
256
|
+
`vec4ApplyMat4` → `vec4TransformMat4x4`; `vec3ApplyQuaternion` →
|
|
257
|
+
`vec3TransformQuaternion`; `vec3RotateAroundAxis` → `vec3RotateAxisAngle`;
|
|
258
|
+
`vec4PerspectiveDivide` → `vec3FromHomogeneous`; `matNxNMultiplyRows` and
|
|
259
|
+
`…MultiplyCols` → `…ScaleRows` and `…ScaleColumns`; `matNxNSub` →
|
|
260
|
+
`matNxNSubtract`. `vecNMinmax` and `vecNWeightedMean` are removed (the bounds
|
|
261
|
+
come with the box functions, the mean with the buffer functions). `DistanceType`
|
|
262
|
+
is removed: the dedicated `…Distance`, `…DistanceManhattan` and
|
|
263
|
+
`…DistanceChebyshev` replace it. `mat2x2Invert`, `mat3x3Invert` and
|
|
264
|
+
`mat4x4Invert` return `boolean` and write the inverse into `out`, in place of a
|
|
265
|
+
nested result or an exception. Code that stored a matrix in a nested tuple
|
|
266
|
+
indexes `m[column * N + row]` now. Rotations: `quaternionToRotationMatrix(q)`
|
|
267
|
+
and `mat4x4FromQuaternion(q)` → `mat4x4FromQuaternion(out, q)` (or
|
|
268
|
+
`mat3x3FromQuaternion`); `quaternionFromRotationMatrix(m)` →
|
|
269
|
+
`quaternionFromMat4x4(out, m)` or `quaternionFromMat3x3`; `quaternionLerp` is
|
|
270
|
+
`quaternionNlerp` and now **normalises** its result; `quaternionAxis(q)` →
|
|
271
|
+
`quaternionAxis(out, q)`, which no longer returns `[1, 0, 0]` for every rotation
|
|
272
|
+
under about `0.1°` and flips with the sign of the scalar part;
|
|
273
|
+
`quaternionAngle` is in `[0, π]` (it was `2·acos(w)`, up to `2π`);
|
|
274
|
+
`quaternionFromAxisAngle`, `quaternionFromVec3ToVec3` and `quaternionSlerp` take
|
|
275
|
+
`out` first, and `quaternionFromVec3ToVec3` no longer rounds an arc under about
|
|
276
|
+
`1.4e-5` rad to the identity. The `~acKitMathType` property of the brand
|
|
277
|
+
is reserved.
|
|
278
|
+
- 34e8cfa: Build a view matrix straight from a camera's axes with
|
|
279
|
+
`mat4x4ViewFromBasis(out, right, up, back, eye)`: the rows are the axes and the
|
|
280
|
+
translation is `−axis · eye`. It takes the axes as given and never throws, so
|
|
281
|
+
it is exact for a camera looking straight up or down, where `mat4x4LookAt` has
|
|
282
|
+
no roll to take from `up`.
|
|
283
|
+
- 34e8cfa: New: `tridiagonalSolve(out, lower, diagonal, upper, right, scratch)` solves a
|
|
284
|
+
tridiagonal system in O(n) without allocating (the Thomas algorithm). `lower`
|
|
285
|
+
and `upper` hold the `n − 1` off-diagonal entries, `scratch` is `n − 1` numbers
|
|
286
|
+
of working space (it may be `upper` itself), and `out` may be `right`. It
|
|
287
|
+
returns `false`, with `out` filled with `NaN`, when the elimination meets a zero
|
|
288
|
+
pivot (the only test on the values, so it wins over a `NaN` elsewhere);
|
|
289
|
+
otherwise non-finite values propagate and it returns `true`. It is stable
|
|
290
|
+
without pivoting for diagonally dominant or symmetric positive definite
|
|
291
|
+
matrices, the systems of splines and finite differences, while the entries lie
|
|
292
|
+
within about [1e−150, 1e150]. A `RangeError` reports lengths that disagree.
|
|
293
|
+
|
|
294
|
+
### Patch Changes
|
|
295
|
+
|
|
296
|
+
- 34e8cfa: An error message that quotes a numeric argument describes it with
|
|
297
|
+
`@ac-kit/core`'s `describeValue`, so building the message can no longer throw
|
|
298
|
+
on a symbol. The text changes: `-0` keeps its sign (`got -0`), a bigint shows
|
|
299
|
+
its digits (a digit-count bound when it is too large to show) and any other
|
|
300
|
+
kind of value is named by its kind. Affected:
|
|
301
|
+
the argument checks of `factorial`, the biquad, window, DCT, FFT size,
|
|
302
|
+
log-frequency map, spectral flux, level meter and novelty peak picker
|
|
303
|
+
functions, every probability distribution's parameters and `quantile`,
|
|
304
|
+
`normalQuantile`, `chiSquaredTest`, `confidenceInterval`, `histogram`,
|
|
305
|
+
`SlidingMedian`, the incomplete beta and gamma functions (and their inverses),
|
|
306
|
+
`trapezoidalRule`, the decimal and rational formatting digit counts and radix,
|
|
307
|
+
the fixed-width integer bit width and shift count, `linspace`, `quantize`,
|
|
308
|
+
`bigIntDigitAt`, `bigIntDigitCount`, `sampleRamp` and `KlActivations`.
|
|
309
|
+
- Updated dependencies [34e8cfa]
|
|
310
|
+
- Updated dependencies [34e8cfa]
|
|
311
|
+
- Updated dependencies [34e8cfa]
|
|
312
|
+
- Updated dependencies [34e8cfa]
|
|
313
|
+
- Updated dependencies [34e8cfa]
|
|
314
|
+
- Updated dependencies [34e8cfa]
|
|
315
|
+
- Updated dependencies [34e8cfa]
|
|
316
|
+
- Updated dependencies [34e8cfa]
|
|
317
|
+
- Updated dependencies [34e8cfa]
|
|
318
|
+
- Updated dependencies [34e8cfa]
|
|
319
|
+
- Updated dependencies [34e8cfa]
|
|
320
|
+
- Updated dependencies [34e8cfa]
|
|
321
|
+
- Updated dependencies [34e8cfa]
|
|
322
|
+
- Updated dependencies [34e8cfa]
|
|
323
|
+
- Updated dependencies [34e8cfa]
|
|
324
|
+
- Updated dependencies [34e8cfa]
|
|
325
|
+
- Updated dependencies [34e8cfa]
|
|
326
|
+
- Updated dependencies [34e8cfa]
|
|
327
|
+
- Updated dependencies [34e8cfa]
|
|
328
|
+
- Updated dependencies [34e8cfa]
|
|
329
|
+
- Updated dependencies [34e8cfa]
|
|
330
|
+
- Updated dependencies [34e8cfa]
|
|
331
|
+
- Updated dependencies [34e8cfa]
|
|
332
|
+
- Updated dependencies [34e8cfa]
|
|
333
|
+
- Updated dependencies [34e8cfa]
|
|
334
|
+
- Updated dependencies [34e8cfa]
|
|
335
|
+
- Updated dependencies [34e8cfa]
|
|
336
|
+
- @ac-kit/core@0.12.0
|
|
337
|
+
- @ac-kit/math-scalar@0.3.0
|
|
338
|
+
- @ac-kit/math-complex@0.2.0
|
|
339
|
+
|
|
3
340
|
## 0.2.1
|
|
4
341
|
|
|
5
342
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -6,8 +6,9 @@ fixed, small dimension.
|
|
|
6
6
|
## Scope
|
|
7
7
|
|
|
8
8
|
The fixed-dimension types that 2D and 3D work needs — `Vec2`/`Vec3`/`Vec4`,
|
|
9
|
-
`Mat2x2`/`Mat3x3`/`Mat4x4`, `
|
|
10
|
-
|
|
9
|
+
`Mat2x2`/`Mat3x3`/`Mat4x4`, `Affine2`/`Affine3`, `Transform2`/`Transform3`,
|
|
10
|
+
`Viewport`, `Quaternion` — represented as flat arrays of numbers carrying a
|
|
11
|
+
type-level brand, and operated on by free functions.
|
|
11
12
|
|
|
12
13
|
Dimension is part of the type, not a runtime property. That is what makes the
|
|
13
14
|
operations inlineable and allocation-predictable, and it is why an arbitrary
|
|
@@ -15,24 +16,144 @@ operations inlineable and allocation-predictable, and it is why an arbitrary
|
|
|
15
16
|
|
|
16
17
|
## Contents
|
|
17
18
|
|
|
18
|
-
| Module
|
|
19
|
-
|
|
|
20
|
-
| `vec2`, `vec3`, `vec4`
|
|
21
|
-
| `mat2x2`, `mat3x3`, `mat4x4`
|
|
22
|
-
| `
|
|
23
|
-
| `
|
|
19
|
+
| Module | Contents |
|
|
20
|
+
| --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
21
|
+
| `vec2`, `vec3`, `vec4` | Creators, component-wise arithmetic, dot, cross, length, distance, normalisation, interpolation, angles, projection, matrix and quaternion transforms, boundary I/O |
|
|
22
|
+
| `mat2x2`, `mat3x3`, `mat4x4` | Creators, multiplication, determinant, inverse, transpose, the builders from rotations, scales, translations, quaternions, axis and angle, affine transforms, compose and decompose, normal matrices |
|
|
23
|
+
| `affine2`, `affine3` | 2D (`a…f` of Canvas and SVG) and 3D (a WGSL `mat4x3f`) affine transforms: multiply, invert, builders, translate/rotate/scale in the local space, compose and decompose |
|
|
24
|
+
| `transform` | `Transform2`, `Transform3`: translate-rotate-scale records (glTF nodes, animation keys), their interpolation and their conversion to and from the affine forms |
|
|
25
|
+
| `quaternion` | Rotations: axis and angle, shortest arc between two directions, matrices, `slerp` and `nlerp`, rotation about an axis, angle between rotations |
|
|
26
|
+
| `euler` | Euler angles in the twelve intrinsic orders, to and from quaternions and matrices |
|
|
27
|
+
| `projection` | `mat4x4Perspective`, `Frustum`, `Orthographic` (three depth ranges, an infinite far plane), `mat4x4ConvertDepthRange`, `mat4x4LookAt`, `affine3TargetTo` |
|
|
28
|
+
| `view-basis` | `mat4x4ViewFromBasis`: a view matrix from a camera's right, up and back axes and its eye (exact looking straight up or down) |
|
|
29
|
+
| `viewport` | Pixels (top left, y down) and normalised device coordinates (y up): `vec3ProjectToViewport`, `vec3UnprojectFromViewport`, a bulk kernel |
|
|
30
|
+
| `vec2-array`, `vec3-array`, `vec4-array` | Buffers of vectors (a `Float64Array` of `count × N`): fused kernels (arithmetic, lerp, normalise, means, transforms by matrices and affine forms), strided kernels, narrowing to and widening from interleaved `Float32Array`, nearest point |
|
|
31
|
+
| `mat4x4-array`, `affine2-array`, `affine3-array`, `quaternion-array` | Instance buffers: `…ArrayCreate`, `…Load`, `…Store`, `…LoadStrided`, `…StoreStrided`, `…ArrayIsFinite` (also for `mat2x2` and `mat3x3`) |
|
|
32
|
+
| `tridiagonal` | `tridiagonalSolve(out, lower, diagonal, upper, right, scratch)`: the Thomas algorithm in O(n), `false` on a zero pivot; stable for diagonally dominant or symmetric positive definite systems |
|
|
33
|
+
| `upload` | Unrolled `…StoreFloat32` of matrices and quaternions, and the padded `…StoreStd140Float32` of uniform blocks |
|
|
34
|
+
| `checked` | The `@ac-kit/math-linear/checked` subpath: `assertFiniteValue`, `assertFiniteBuffer`, throwing assertions for the boundaries of a program |
|
|
35
|
+
| `kl-activations` | `KlActivations`: KL-divergence NMF activations of a sparse fixed dictionary, with per-column L1 penalties, iterated across calls |
|
|
36
|
+
|
|
37
|
+
## Values and functions
|
|
38
|
+
|
|
39
|
+
A `Vec3` is an array `[x, y, z]`; a `Mat4x4` is sixteen numbers in
|
|
40
|
+
**column-major** order (entry of row `r`, column `c` at `4c + r`), the layout of
|
|
41
|
+
WebGL and WebGPU uniforms. The functions follow one standard:
|
|
42
|
+
|
|
43
|
+
- **Creators only.** `vec3Create()` (zero), `vec3Of(x, y, z)`, `vec3Clone(v)` and
|
|
44
|
+
`vec3FromArray(out, source, offset?)` make a vector; `mat4x4Create()` (the
|
|
45
|
+
identity), `mat4x4Of(…)`, `mat4x4OfRows(…)` (rows as you write them on paper),
|
|
46
|
+
`mat4x4Clone` and `mat4x4FromArray` make a matrix. An array literal, a
|
|
47
|
+
`number[]`, a `Float64Array` or any other array of the right length is rejected
|
|
48
|
+
by the compiler, so `vec3Of(1, 0, 0)` replaces `[1, 0, 0]`.
|
|
49
|
+
- **`out` first, alias-safe, no allocation.** Every operation writes into its
|
|
50
|
+
first argument and returns it, and that argument may be any of the inputs:
|
|
51
|
+
`vec3Add(a, a, b)` is `a ← a + b`. Only the creators allocate; keep them out of
|
|
52
|
+
hot loops and reuse an `out`. Functions that return a number or a boolean take
|
|
53
|
+
no `out`.
|
|
54
|
+
- **No throw.** Normalising the zero vector gives zero; inverting a singular
|
|
55
|
+
matrix returns `false` and fills `out` with `NaN`; `NaN` propagates.
|
|
56
|
+
Operations never check their arguments: `vec3IsFinite` is the check to run
|
|
57
|
+
after parsing untrusted data, and `vec3FromArray` is the way in from JSON,
|
|
58
|
+
messages or any array of unknown origin — it stores `NaN` for an element that
|
|
59
|
+
is not a number, never anything else.
|
|
60
|
+
- **Plain arithmetic.** A sum, a dot or a product is rounded as written. The
|
|
61
|
+
`…Precise` variants (`vec3DotPrecise`, `vec3CrossPrecise`,
|
|
62
|
+
`mat4x4MultiplyPrecise`, `vec3TransformMat3x3Precise`) are compensated, and
|
|
63
|
+
cost several times as much. The lengths and the normalisation are right
|
|
64
|
+
between about `1e-150` and `1e150`; beyond that use `vec3LengthScaled` and
|
|
65
|
+
`vec3NormalizeScaled`.
|
|
66
|
+
- **Buffers at the boundary.** `vec3Load(out, array, index)` and
|
|
67
|
+
`vec3Store(array, index, v)` move a vector between a `Float64Array` and a
|
|
68
|
+
value; `…LoadStrided`, `…StoreStrided` and `…StoreFloat32` cover interleaved
|
|
69
|
+
and single-precision buffers; `vec3ArrayIsFinite` scans a buffer. A
|
|
70
|
+
`Float64Array` is never passed to a function that takes a value. Many values
|
|
71
|
+
at once are a buffer, see [Buffers](#buffers).
|
|
72
|
+
|
|
73
|
+
Every creator copies a private array of non-integer doubles, so a value is a V8
|
|
74
|
+
`PACKED_DOUBLE_ELEMENTS` array even when its components are whole numbers. That
|
|
75
|
+
is what keeps the code that reads it fast, and it holds only while nothing else
|
|
76
|
+
is stored in one. For callers who bypass the types (JavaScript, a cast, `any`):
|
|
77
|
+
|
|
78
|
+
- never store anything but a number in a value, not even briefly;
|
|
79
|
+
- no holes, no `push`, no change of `length`;
|
|
80
|
+
- never pass a `Float64Array`, a hand-made array or any other array where a
|
|
81
|
+
value is expected, even once, even in a test: the engine keeps type feedback
|
|
82
|
+
per function, so one such call slows every later call of that function in the
|
|
83
|
+
process (`…FromArray`'s `source` is the one parameter that takes any array);
|
|
84
|
+
- do not make one with `structuredClone` or `[...float64Array]` — use
|
|
85
|
+
`…Clone` or `…FromArray`.
|
|
86
|
+
|
|
87
|
+
`JSON.parse` (typed as `JsonValue` in this repository), `Response.json()` and
|
|
88
|
+
`MessageEvent.data` are not checked values: pass them through `…FromArray`. The
|
|
89
|
+
`~acKitMathType` property of the brand is reserved.
|
|
90
|
+
|
|
91
|
+
## Buffers
|
|
92
|
+
|
|
93
|
+
Thousands of points, normals or instance matrices are a `Float64Array` of
|
|
94
|
+
`count × N` numbers (3 per `Vec3`, 16 per `Mat4x4`, …), element `i` at
|
|
95
|
+
`[i·N, i·N + N)`, made by `vec3ArrayCreate(count)` (or `vec3ArrayFromValues`).
|
|
96
|
+
Each bulk operation is one dedicated kernel, so a pass over a million points is
|
|
97
|
+
a single loop with no call per element:
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
import {
|
|
101
|
+
affine3Of,
|
|
102
|
+
vec3ArrayCreate,
|
|
103
|
+
vec3ArrayNarrowStrided,
|
|
104
|
+
vec3ArrayTransformAffine3,
|
|
105
|
+
vec3ArrayWidenStrided,
|
|
106
|
+
} from "@ac-kit/math-linear";
|
|
107
|
+
|
|
108
|
+
const positions = vec3ArrayCreate(1000);
|
|
109
|
+
const moved = vec3ArrayCreate(1000);
|
|
110
|
+
vec3ArrayTransformAffine3(
|
|
111
|
+
moved,
|
|
112
|
+
positions,
|
|
113
|
+
affine3Of(1, 0, 0, 0, 1, 0, 0, 0, 1, 10, 0, 0),
|
|
114
|
+
); // translate by 10 along x; `moved` may be `positions` itself
|
|
115
|
+
|
|
116
|
+
// Into a vertex buffer of position (3), normal (3): positions at offset 0.
|
|
117
|
+
const vertices = new Float32Array(1000 * 6);
|
|
118
|
+
vec3ArrayNarrowStrided(vertices, 0, 6, moved);
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
- **Kernels.** `vecNArrayAdd`, `…Subtract`, `…MultiplyScalar`, `…AddScaled`,
|
|
122
|
+
`…Translate`, `…Lerp`, `…Normalize`, `…Mean`, `…WeightedMean` (and
|
|
123
|
+
`…WeightedMeanPrecise`), and the transforms by matrices and affine forms
|
|
124
|
+
(`vec3ArrayTransformMat4x4` divides by `w`, `vec3ArrayTransformNormal`
|
|
125
|
+
normalises in the same pass). Out first, in place allowed (`out` may be the
|
|
126
|
+
input, not a partially overlapping view of it).
|
|
127
|
+
- **Checks.** The element count is `input.length / N`. One O(1) check per call
|
|
128
|
+
that the lengths agree and are a multiple of `N` throws a `RangeError`
|
|
129
|
+
before anything is written; there is no per-element check, so a `NaN` goes
|
|
130
|
+
through (`vec3ArrayIsFinite` and `assertFiniteBuffer` are the checks).
|
|
131
|
+
- **Interleaved data.** The `…Strided` kernels take `(out, outOffset,
|
|
132
|
+
outStride, input, inputOffset, inputStride, count, …)` in numbers, and tight
|
|
133
|
+
kernels take no stride.
|
|
134
|
+
- **Single precision.** Nothing computes on a `Float32Array`: `…ArrayNarrowStrided`
|
|
135
|
+
writes a tight binary64 buffer into an interleaved binary32 one,
|
|
136
|
+
`…ArrayWidenStrided` reads one back (a glTF accessor, a readback), and
|
|
137
|
+
`…ArrayNarrowRelative` subtracts an origin first so that large coordinates
|
|
138
|
+
keep their precision; `…ArraySplitStrided` writes each number as two
|
|
139
|
+
binary32 parts, high and low, for two-float positions on the GPU. A whole
|
|
140
|
+
array without stride is `Float32Array.prototype.set`.
|
|
141
|
+
- **Instances.** `mat4x4ArrayCreate`, `affine2ArrayCreate`, `affine3ArrayCreate`
|
|
142
|
+
and `quaternionArrayCreate` make instance buffers, written one element at a
|
|
143
|
+
time with `…Store` and read with `…Load`.
|
|
24
144
|
|
|
25
145
|
## Equality and tolerance
|
|
26
146
|
|
|
27
|
-
`*Equals` is **exact**, component by component
|
|
28
|
-
|
|
147
|
+
`*Equals` is **exact**, component by component (`−0` equals `+0`, `NaN` equals
|
|
148
|
+
nothing). It is safe as a basis for deduplication. `*IsClose` is the tolerant
|
|
29
149
|
counterpart and is deliberately named differently, because it is _not_ an
|
|
30
150
|
equality: `a ≈ b` and `b ≈ c` does not give `a ≈ c`, so it must never back an
|
|
31
151
|
ordering or a container.
|
|
32
152
|
|
|
33
|
-
`*IsClose`
|
|
34
|
-
unlike `@ac-kit/math-geometry`. A vector or matrix here is
|
|
35
|
-
with no units of its own, so only the caller knows what
|
|
153
|
+
`*IsClose` takes its tolerance as an option — none means exact, and there is no
|
|
154
|
+
global config, unlike `@ac-kit/math-geometry`. A vector or matrix here is
|
|
155
|
+
dimensionless algebra with no units of its own, so only the caller knows what
|
|
156
|
+
counts as close:
|
|
36
157
|
|
|
37
158
|
```ts
|
|
38
159
|
import { vec3IsClose } from "@ac-kit/math-linear";
|
|
@@ -50,18 +171,113 @@ instead.
|
|
|
50
171
|
## Quaternion here versus quaternion in math-numbers
|
|
51
172
|
|
|
52
173
|
The `Quaternion` in this package is a rotation operator: four floats, always
|
|
53
|
-
used unit-length, optimised for composing and interpolating orientations.
|
|
174
|
+
used unit-length, optimised for composing and interpolating orientations. It is
|
|
175
|
+
the branded `[x, y, z, w]` tuple of `@ac-kit/math-complex`, re-exported here with
|
|
176
|
+
its out-first functions: build one with `quaternionOf(x, y, z, w)` or
|
|
177
|
+
`quaternionCreate()`, since an array literal does not type-check.
|
|
54
178
|
|
|
55
179
|
`@ac-kit/math-complex`'s `QuaternionNum` is ℍ as an algebraic object — a
|
|
56
180
|
division ring implementing the `@ac-kit/math-algebra` hierarchy. Same
|
|
57
181
|
mathematics, different purpose; neither is a substitute for the other.
|
|
58
182
|
|
|
183
|
+
## Rotations, transforms and cameras
|
|
184
|
+
|
|
185
|
+
- **Conventions.** Right-handed; a camera looks down **−Z**; column vectors, so
|
|
186
|
+
`mat4x4Multiply(out, a, b)` is `a · b` and applies `b` first. Every signed
|
|
187
|
+
angle a function returns is in `(−π, π]`.
|
|
188
|
+
- **Euler angles** are a `Vec3` of radians **in the order's sequence**, always
|
|
189
|
+
intrinsic: `quaternionFromEuler(out, angles, "ZXY")` is `Rz(a₀) · Rx(a₁) ·
|
|
190
|
+
Ry(a₂)`, which is also the extrinsic Y–X–Z sequence. All twelve orders are
|
|
191
|
+
there. A `DeviceOrientationEvent` is `"ZXY"` with `alpha`, `beta`, `gamma`
|
|
192
|
+
converted from degrees. `eulerFromQuaternion` gives the first and third angles
|
|
193
|
+
in `(−π, π]`, the middle one in its order's range, and at gimbal lock (within
|
|
194
|
+
`1e-10` rad) puts the whole of the combined rotation into the first angle, the
|
|
195
|
+
third being `0`.
|
|
196
|
+
- **Depth range** is a required argument of every projection:
|
|
197
|
+
`"zero-to-one"` (WebGPU and both backends of `@ac-kit/graphics-gpu`),
|
|
198
|
+
`"negative-one-to-one"` (OpenGL) or `"one-to-zero"` (reversed depth, near at
|
|
199
|
+
1; its extra precision needs a floating-point depth buffer and depth produced
|
|
200
|
+
in `[0, 1]`). `far` may be `Infinity` in all three.
|
|
201
|
+
`mat4x4ConvertDepthRange` re-expresses a projection built for one range in
|
|
202
|
+
another (a GL-style projection drawn under WebGPU's clip depth).
|
|
203
|
+
`mat4x4Perspective`, `mat4x4Frustum`, `mat4x4Orthographic`, `mat4x4LookAt` and
|
|
204
|
+
`affine3TargetTo` are built once per frame or resize, so they validate and
|
|
205
|
+
throw a `RangeError` (leaving `out` untouched) where the operations return
|
|
206
|
+
`false` or `NaN`.
|
|
207
|
+
- **Viewports.** Pixels have a top-left origin and `y` down, NDC has `y` up;
|
|
208
|
+
pixel `i` covers `[i, i + 1)` and NDC `−1` is the left edge, not a pixel
|
|
209
|
+
centre. `vec3ProjectToViewport` gives `[x px, y px, depth]` (`NaN` behind the
|
|
210
|
+
eye), `vec3UnprojectFromViewport` inverts it, and `vec3ArrayProjectToViewport`
|
|
211
|
+
does a whole buffer in one pass.
|
|
212
|
+
- **Affine forms.** `Affine2` is Canvas's `a, b, c, d, e, f`; `Affine3` is four
|
|
213
|
+
columns of three (the translation at 9, 10, 11). `affine2Translate`,
|
|
214
|
+
`…Rotate` and `…Scale` apply in the local space like Canvas's. `Transform2`
|
|
215
|
+
and `Transform3` are translate-rotate-scale records, composed through the
|
|
216
|
+
affine forms; `…FromAffine…` and `mat4x4Decompose` return `false`, with `NaN`
|
|
217
|
+
in the outputs, for a shear (a cosine between two columns above about `1e-6`),
|
|
218
|
+
a zero scale or non-finite input.
|
|
219
|
+
|
|
220
|
+
## Uploads and the checked layer
|
|
221
|
+
|
|
222
|
+
`mat4x4StoreFloat32(array, offset, m)` writes a matrix into a `Float32Array` by
|
|
223
|
+
sixteen unrolled stores, a few nanoseconds where `Float32Array.prototype.set`
|
|
224
|
+
on an array costs fifteen. A uniform block pads matrix columns to four floats,
|
|
225
|
+
so `mat3x3StoreStd140Float32`, `affine3StoreStd140Float32` (a `mat4x3f`) and
|
|
226
|
+
`affine2StoreStd140Float32` (a `mat3x3f` holding the homogeneous matrix) write
|
|
227
|
+
every float of the field, padding included.
|
|
228
|
+
|
|
229
|
+
Operations never check, so the boundaries of a program do: the
|
|
230
|
+
`@ac-kit/math-linear/checked` subpath exports `assertFiniteValue(name, value)`
|
|
231
|
+
and `assertFiniteBuffer(name, array, elementSize, count?)`, which throw a
|
|
232
|
+
`RangeError` naming the argument and the first bad component. Call them after
|
|
233
|
+
parsing untrusted data, before an upload in a development build, and in tests.
|
|
234
|
+
|
|
59
235
|
## Usage
|
|
60
236
|
|
|
61
237
|
```ts
|
|
62
|
-
import {
|
|
238
|
+
import {
|
|
239
|
+
mat4x4Create,
|
|
240
|
+
mat4x4LookAt,
|
|
241
|
+
mat4x4Multiply,
|
|
242
|
+
mat4x4Perspective,
|
|
243
|
+
mat4x4StoreFloat32,
|
|
244
|
+
vec3Create,
|
|
245
|
+
vec3Cross,
|
|
246
|
+
vec3Normalize,
|
|
247
|
+
vec3Of,
|
|
248
|
+
vec3ProjectToViewport,
|
|
249
|
+
viewportOf,
|
|
250
|
+
} from "@ac-kit/math-linear";
|
|
251
|
+
|
|
252
|
+
const out = vec3Create();
|
|
253
|
+
vec3Normalize(out, vec3Cross(out, vec3Of(1, 0, 0), vec3Of(0, 1, 0))); // [0, 0, 1]
|
|
254
|
+
|
|
255
|
+
// A camera 10 units back, a WebGPU projection, and where the origin lands on
|
|
256
|
+
// an 800 × 600 canvas (the middle).
|
|
257
|
+
const view = mat4x4LookAt(
|
|
258
|
+
mat4x4Create(),
|
|
259
|
+
vec3Of(0, 0, 10),
|
|
260
|
+
vec3Of(0, 0, 0),
|
|
261
|
+
vec3Of(0, 1, 0),
|
|
262
|
+
);
|
|
263
|
+
const projection = mat4x4Perspective(
|
|
264
|
+
mat4x4Create(),
|
|
265
|
+
Math.PI / 3,
|
|
266
|
+
800 / 600,
|
|
267
|
+
0.1,
|
|
268
|
+
100,
|
|
269
|
+
"zero-to-one",
|
|
270
|
+
);
|
|
271
|
+
const viewProjection = mat4x4Multiply(mat4x4Create(), projection, view);
|
|
272
|
+
vec3ProjectToViewport(
|
|
273
|
+
vec3Create(),
|
|
274
|
+
vec3Of(0, 0, 0),
|
|
275
|
+
viewProjection,
|
|
276
|
+
viewportOf(0, 0, 800, 600),
|
|
277
|
+
); // [400, 300, depth in 0…1]
|
|
63
278
|
|
|
64
|
-
|
|
279
|
+
const uniforms = new Float32Array(16);
|
|
280
|
+
mat4x4StoreFloat32(uniforms, 0, viewProjection);
|
|
65
281
|
```
|
|
66
282
|
|
|
67
283
|
See the
|
package/dist/_brand.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The brand of every value type of the math packages: a property with a string
|
|
3
|
+
* key that is stable across versions, so two installed copies of a package (two
|
|
4
|
+
* dependents pinning different minors) stay assignable to each other, which a
|
|
5
|
+
* per-package `unique symbol` would not allow.
|
|
6
|
+
*
|
|
7
|
+
* Declared identically and internally in each package, never imported across
|
|
8
|
+
* packages: the structure is the identity. The key is reserved, and the brand
|
|
9
|
+
* costs nothing at run time.
|
|
10
|
+
*/
|
|
11
|
+
export type Brand_<Name extends string> = {
|
|
12
|
+
readonly "~acKitMathType": Name;
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=_brand.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_brand.d.ts","sourceRoot":"","sources":["../src/_brand.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,MAAM,MAAM,CAAC,IAAI,SAAS,MAAM,IAAI;IACzC,QAAQ,CAAC,gBAAgB,EAAE,IAAI,CAAC;CAChC,CAAC"}
|
package/dist/_brand.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_brand.js","sourceRoot":"","sources":["../src/_brand.ts"],"names":[],"mappings":""}
|