@ac-kit/math-linear 0.1.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 +13 -0
- package/LICENSE +28 -0
- package/README.md +68 -0
- package/dist/_slerp.d.ts +40 -0
- package/dist/_slerp.d.ts.map +1 -0
- package/dist/_slerp.js +40 -0
- package/dist/_slerp.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/mat2x2.d.ts +30 -0
- package/dist/mat2x2.d.ts.map +1 -0
- package/dist/mat2x2.js +111 -0
- package/dist/mat2x2.js.map +1 -0
- package/dist/mat3x3.d.ts +57 -0
- package/dist/mat3x3.d.ts.map +1 -0
- package/dist/mat3x3.js +213 -0
- package/dist/mat3x3.js.map +1 -0
- package/dist/mat4x4.d.ts +70 -0
- package/dist/mat4x4.d.ts.map +1 -0
- package/dist/mat4x4.js +371 -0
- package/dist/mat4x4.js.map +1 -0
- package/dist/quaternion.d.ts +23 -0
- package/dist/quaternion.d.ts.map +1 -0
- package/dist/quaternion.js +208 -0
- package/dist/quaternion.js.map +1 -0
- package/dist/vec2.d.ts +103 -0
- package/dist/vec2.d.ts.map +1 -0
- package/dist/vec2.js +253 -0
- package/dist/vec2.js.map +1 -0
- package/dist/vec3.d.ts +91 -0
- package/dist/vec3.d.ts.map +1 -0
- package/dist/vec3.js +314 -0
- package/dist/vec3.js.map +1 -0
- package/dist/vec4.d.ts +59 -0
- package/dist/vec4.d.ts.map +1 -0
- package/dist/vec4.js +260 -0
- package/dist/vec4.js.map +1 -0
- package/package.json +66 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to
|
|
7
|
+
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
8
|
+
|
|
9
|
+
## [0.1.0] - 2026-08-26
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- Initial release.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-2026 Anthony Champagne and contributors
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# @ac-kit/math-linear
|
|
2
|
+
|
|
3
|
+
Linear algebra over IEEE 754 binary64: vectors, matrices and quaternions of
|
|
4
|
+
fixed, small dimension.
|
|
5
|
+
|
|
6
|
+
## Scope
|
|
7
|
+
|
|
8
|
+
The fixed-dimension types that 2D and 3D work needs — `Vec2`/`Vec3`/`Vec4`,
|
|
9
|
+
`Mat2x2`/`Mat3x3`/`Mat4x4`, `Quaternion` — represented as plain tuples and
|
|
10
|
+
operated on by free functions.
|
|
11
|
+
|
|
12
|
+
Dimension is part of the type, not a runtime property. That is what makes the
|
|
13
|
+
operations inlineable and allocation-predictable, and it is why an arbitrary
|
|
14
|
+
`Matrix<n, m>` is a different design rather than a generalisation of this one.
|
|
15
|
+
|
|
16
|
+
## Contents
|
|
17
|
+
|
|
18
|
+
| Module | Contents |
|
|
19
|
+
| ---------------------------- | -------------------------------------------------------------------------------------------------- |
|
|
20
|
+
| `vec2`, `vec3`, `vec4` | Component-wise arithmetic, dot, cross, length, normalisation, interpolation, matrix application |
|
|
21
|
+
| `mat2x2`, `mat3x3`, `mat4x4` | Multiplication, determinant, inverse, transpose, transform construction |
|
|
22
|
+
| `quaternion` | Rotation composition, slerp, conversion to and from axis-angle, Euler angles and rotation matrices |
|
|
23
|
+
|
|
24
|
+
## Equality and tolerance
|
|
25
|
+
|
|
26
|
+
`*Equals` is **exact**, component by component. It is transitive, so it is safe
|
|
27
|
+
as a `Set` or `Map` key and as a dedupe basis. `*IsClose` is the tolerant
|
|
28
|
+
counterpart and is deliberately named differently, because it is _not_ an
|
|
29
|
+
equality: `a ≈ b` and `b ≈ c` does not give `a ≈ c`, so it must never back an
|
|
30
|
+
ordering or a container.
|
|
31
|
+
|
|
32
|
+
`*IsClose` requires its tolerance — there is no default and no global config,
|
|
33
|
+
unlike `@ac-kit/math-geometry`. A vector or matrix here is dimensionless algebra
|
|
34
|
+
with no units of its own, so only the caller knows what counts as close:
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { vec3IsClose } from "@ac-kit/math-linear";
|
|
38
|
+
|
|
39
|
+
vec3IsClose(a, b, { absTol: 1e-9 }); // values near the origin
|
|
40
|
+
vec3IsClose(a, b, { relTol: 1e-9 }); // values of unknown magnitude
|
|
41
|
+
vec3IsClose(a, b, { relTol: 1e-9, absTol: 1e-12 }); // both, whichever is looser
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
`quaternionEquals` and `quaternionIsClose` compare the four components, so `q`
|
|
45
|
+
and `-q` are neither equal nor close even though they denote the same rotation.
|
|
46
|
+
Compare `Math.abs(quaternionDot(q1, q2))` against `1` to test the rotations
|
|
47
|
+
instead.
|
|
48
|
+
|
|
49
|
+
## Quaternion here versus quaternion in math-numbers
|
|
50
|
+
|
|
51
|
+
The `Quaternion` in this package is a rotation operator: four floats, always
|
|
52
|
+
used unit-length, optimised for composing and interpolating orientations.
|
|
53
|
+
|
|
54
|
+
`@ac-kit/math-complex`'s `QuaternionNum` is ℍ as an algebraic object — a
|
|
55
|
+
division ring implementing the `@ac-kit/math-algebra` hierarchy. Same
|
|
56
|
+
mathematics, different purpose; neither is a substitute for the other.
|
|
57
|
+
|
|
58
|
+
## Usage
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { vec3Cross, vec3Normalize } from "@ac-kit/math-linear";
|
|
62
|
+
|
|
63
|
+
vec3Normalize(vec3Cross([1, 0, 0], [0, 1, 0])); // [0, 0, 1]
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
See the
|
|
67
|
+
[generated API docs](https://anthochamp.github.io/node-essentials/api/math-linear/)
|
|
68
|
+
for the full reference.
|
package/dist/_slerp.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Largest `dot` at which spherical linear interpolation still divides by `sin
|
|
3
|
+
* θ₀` safely; above it, callers fall back to normalised linear interpolation.
|
|
4
|
+
*
|
|
5
|
+
* Every slerp here evaluates the same expression, for unit-length operands
|
|
6
|
+
* whose dot product is `dot = cos θ₀`:
|
|
7
|
+
*
|
|
8
|
+
* ```text
|
|
9
|
+
* slerp(a, b, t) = a·(cos θ − dot·sin θ / sin θ₀) + b·(sin θ / sin θ₀)
|
|
10
|
+
* θ₀ = acos(dot)
|
|
11
|
+
* θ = θ₀·t
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* Both weights carry `sin θ₀` in the denominator, and `θ₀ → 0` as the operands
|
|
15
|
+
* align, so the quotient amplifies whatever error `acos` returned. `acos` is
|
|
16
|
+
* itself ill-conditioned there:
|
|
17
|
+
*
|
|
18
|
+
* ```text
|
|
19
|
+
* d(acos dot)/d(dot) = −1 / √(1 − dot²)
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* Which diverges at `dot = ±1`. At this threshold that derivative is `≈ −31.6`,
|
|
23
|
+
* so one ULP of error in `dot` (`≈ 1.1e-16` near 1) costs `≈ 3.5e-15 rad` in
|
|
24
|
+
* `θ₀` — negligible against `θ₀ = acos(0.9995) ≈ 0.0316 rad ≈ 1.81°`, and `sin
|
|
25
|
+
* θ₀ ≈ 0.0316` stays three orders of magnitude above `√ε ≈ 1.5e-8`. Closer to
|
|
26
|
+
* `1` both bounds collapse together.
|
|
27
|
+
*
|
|
28
|
+
* Below that angle the fallback costs nothing observable: normalised linear
|
|
29
|
+
* interpolation traces the same great-circle arc and differs only in
|
|
30
|
+
* parameterisation, agreeing exactly at `t = 0`, `0.5` and `1` and deviating by
|
|
31
|
+
* `O(θ₀²)` — under `1e-3 rad` here — in between.
|
|
32
|
+
*
|
|
33
|
+
* `0.9995` is the conventional value: glMatrix, three.js and Shoemake's
|
|
34
|
+
* original formulation all switch here.
|
|
35
|
+
*
|
|
36
|
+
* Not a tolerance, and deliberately not configurable — it selects which formula
|
|
37
|
+
* computes the answer, never what the answer means.
|
|
38
|
+
*/
|
|
39
|
+
export declare const SLERP_LINEAR_FALLBACK_DOT_ = 0.9995;
|
|
40
|
+
//# sourceMappingURL=_slerp.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_slerp.d.ts","sourceRoot":"","sources":["../src/_slerp.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,eAAO,MAAM,0BAA0B,SAAS,CAAC"}
|
package/dist/_slerp.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Largest `dot` at which spherical linear interpolation still divides by `sin
|
|
3
|
+
* θ₀` safely; above it, callers fall back to normalised linear interpolation.
|
|
4
|
+
*
|
|
5
|
+
* Every slerp here evaluates the same expression, for unit-length operands
|
|
6
|
+
* whose dot product is `dot = cos θ₀`:
|
|
7
|
+
*
|
|
8
|
+
* ```text
|
|
9
|
+
* slerp(a, b, t) = a·(cos θ − dot·sin θ / sin θ₀) + b·(sin θ / sin θ₀)
|
|
10
|
+
* θ₀ = acos(dot)
|
|
11
|
+
* θ = θ₀·t
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* Both weights carry `sin θ₀` in the denominator, and `θ₀ → 0` as the operands
|
|
15
|
+
* align, so the quotient amplifies whatever error `acos` returned. `acos` is
|
|
16
|
+
* itself ill-conditioned there:
|
|
17
|
+
*
|
|
18
|
+
* ```text
|
|
19
|
+
* d(acos dot)/d(dot) = −1 / √(1 − dot²)
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* Which diverges at `dot = ±1`. At this threshold that derivative is `≈ −31.6`,
|
|
23
|
+
* so one ULP of error in `dot` (`≈ 1.1e-16` near 1) costs `≈ 3.5e-15 rad` in
|
|
24
|
+
* `θ₀` — negligible against `θ₀ = acos(0.9995) ≈ 0.0316 rad ≈ 1.81°`, and `sin
|
|
25
|
+
* θ₀ ≈ 0.0316` stays three orders of magnitude above `√ε ≈ 1.5e-8`. Closer to
|
|
26
|
+
* `1` both bounds collapse together.
|
|
27
|
+
*
|
|
28
|
+
* Below that angle the fallback costs nothing observable: normalised linear
|
|
29
|
+
* interpolation traces the same great-circle arc and differs only in
|
|
30
|
+
* parameterisation, agreeing exactly at `t = 0`, `0.5` and `1` and deviating by
|
|
31
|
+
* `O(θ₀²)` — under `1e-3 rad` here — in between.
|
|
32
|
+
*
|
|
33
|
+
* `0.9995` is the conventional value: glMatrix, three.js and Shoemake's
|
|
34
|
+
* original formulation all switch here.
|
|
35
|
+
*
|
|
36
|
+
* Not a tolerance, and deliberately not configurable — it selects which formula
|
|
37
|
+
* computes the answer, never what the answer means.
|
|
38
|
+
*/
|
|
39
|
+
export const SLERP_LINEAR_FALLBACK_DOT_ = 0.9995;
|
|
40
|
+
//# sourceMappingURL=_slerp.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_slerp.js","sourceRoot":"","sources":["../src/_slerp.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,MAAM,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC"}
|
package/dist/mat2x2.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type IsCloseToOptions } from "@ac-kit/core";
|
|
2
|
+
import { Vec2 } from "./vec2.js";
|
|
3
|
+
export type Mat2x2 = [[number, number], [number, number]];
|
|
4
|
+
export declare const MAT2X2_ZERO: [[0, 0], [0, 0]];
|
|
5
|
+
export declare const MAT2X2_IDENTITY: [[1, 0], [0, 1]];
|
|
6
|
+
export declare function mat2x2Add(a: Mat2x2, b: Mat2x2): Mat2x2;
|
|
7
|
+
export declare function mat2x2MultiplyScalar(matrix: Mat2x2, scalar: number): Mat2x2;
|
|
8
|
+
export declare function mat2x2MultiplyCols(matrix: Mat2x2, vector: Vec2): Mat2x2;
|
|
9
|
+
export declare function mat2x2MultiplyRows(matrix: Mat2x2, vector: Vec2): Mat2x2;
|
|
10
|
+
export declare function mat2x2Multiply(a: Mat2x2, b: Mat2x2): Mat2x2;
|
|
11
|
+
export declare function mat2x2Transpose(matrix: Mat2x2): Mat2x2;
|
|
12
|
+
export declare function mat2x2Determinant(matrix: Mat2x2): number;
|
|
13
|
+
/** Exact element-wise equality. See {@link mat2x2IsClose} for a tolerant test. */
|
|
14
|
+
export declare function mat2x2Equals(a: Mat2x2, b: Mat2x2): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Element-wise approximate equality.
|
|
17
|
+
*
|
|
18
|
+
* Not transitive, so it is not an equality in the sense a `Set` or `Map` needs,
|
|
19
|
+
* and never a basis for ordering — use {@link mat2x2Equals} there.
|
|
20
|
+
*/
|
|
21
|
+
export declare function mat2x2IsClose(a: Mat2x2, b: Mat2x2, tolerance: IsCloseToOptions): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a 2×2 rotation matrix for the given angle in radians
|
|
24
|
+
* (counter-clockwise).
|
|
25
|
+
*/
|
|
26
|
+
export declare function mat2x2FromRotation(angle: number): Mat2x2;
|
|
27
|
+
export declare function mat2x2Invert(matrix: Mat2x2): Mat2x2;
|
|
28
|
+
export declare function mat2x2Negate(matrix: Mat2x2): Mat2x2;
|
|
29
|
+
export declare function mat2x2Sub(a: Mat2x2, b: Mat2x2): Mat2x2;
|
|
30
|
+
//# sourceMappingURL=mat2x2.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mat2x2.d.ts","sourceRoot":"","sources":["../src/mat2x2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,KAAK,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAGhE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,MAAM,MAAM,MAAM,GAAG,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AAE1D,eAAO,MAAM,WAAW,kBAGG,CAAC;AAC5B,eAAO,MAAM,eAAe,kBAGD,CAAC;AAE5B,wBAAgB,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAKtD;AAED,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAK3E;AAED,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,GAAG,MAAM,CAKvE;AAED,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,GAAG,MAAM,CAKvE;AAED,wBAAgB,cAAc,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAW3D;AAED,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAKtD;AAED,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAExD;AAED,kFAAkF;AAClF,wBAAgB,YAAY,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAO1D;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAC5B,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,SAAS,EAAE,gBAAgB,GACzB,OAAO,CAOT;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAOxD;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAcnD;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAKnD;AAED,wBAAgB,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAKtD"}
|
package/dist/mat2x2.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { isCloseTo } from "@ac-kit/core";
|
|
2
|
+
import { diffOfProducts } from "@ac-kit/math-scalar";
|
|
3
|
+
export const MAT2X2_ZERO = [
|
|
4
|
+
[0, 0],
|
|
5
|
+
[0, 0],
|
|
6
|
+
];
|
|
7
|
+
export const MAT2X2_IDENTITY = [
|
|
8
|
+
[1, 0],
|
|
9
|
+
[0, 1],
|
|
10
|
+
];
|
|
11
|
+
export function mat2x2Add(a, b) {
|
|
12
|
+
return [
|
|
13
|
+
[a[0][0] + b[0][0], a[0][1] + b[0][1]],
|
|
14
|
+
[a[1][0] + b[1][0], a[1][1] + b[1][1]],
|
|
15
|
+
];
|
|
16
|
+
}
|
|
17
|
+
export function mat2x2MultiplyScalar(matrix, scalar) {
|
|
18
|
+
return [
|
|
19
|
+
[matrix[0][0] * scalar, matrix[0][1] * scalar],
|
|
20
|
+
[matrix[1][0] * scalar, matrix[1][1] * scalar],
|
|
21
|
+
];
|
|
22
|
+
}
|
|
23
|
+
export function mat2x2MultiplyCols(matrix, vector) {
|
|
24
|
+
return [
|
|
25
|
+
[matrix[0][0] * vector[0], matrix[0][1] * vector[1]],
|
|
26
|
+
[matrix[1][0] * vector[0], matrix[1][1] * vector[1]],
|
|
27
|
+
];
|
|
28
|
+
}
|
|
29
|
+
export function mat2x2MultiplyRows(matrix, vector) {
|
|
30
|
+
return [
|
|
31
|
+
[matrix[0][0] * vector[0], matrix[0][1] * vector[0]],
|
|
32
|
+
[matrix[1][0] * vector[1], matrix[1][1] * vector[1]],
|
|
33
|
+
];
|
|
34
|
+
}
|
|
35
|
+
export function mat2x2Multiply(a, b) {
|
|
36
|
+
return [
|
|
37
|
+
[
|
|
38
|
+
a[0][0] * b[0][0] + a[0][1] * b[1][0],
|
|
39
|
+
a[0][0] * b[0][1] + a[0][1] * b[1][1],
|
|
40
|
+
],
|
|
41
|
+
[
|
|
42
|
+
a[1][0] * b[0][0] + a[1][1] * b[1][0],
|
|
43
|
+
a[1][0] * b[0][1] + a[1][1] * b[1][1],
|
|
44
|
+
],
|
|
45
|
+
];
|
|
46
|
+
}
|
|
47
|
+
export function mat2x2Transpose(matrix) {
|
|
48
|
+
return [
|
|
49
|
+
[matrix[0][0], matrix[1][0]],
|
|
50
|
+
[matrix[0][1], matrix[1][1]],
|
|
51
|
+
];
|
|
52
|
+
}
|
|
53
|
+
export function mat2x2Determinant(matrix) {
|
|
54
|
+
return diffOfProducts(matrix[0][0], matrix[1][1], matrix[0][1], matrix[1][0]);
|
|
55
|
+
}
|
|
56
|
+
/** Exact element-wise equality. See {@link mat2x2IsClose} for a tolerant test. */
|
|
57
|
+
export function mat2x2Equals(a, b) {
|
|
58
|
+
return (a[0][0] === b[0][0] &&
|
|
59
|
+
a[0][1] === b[0][1] &&
|
|
60
|
+
a[1][0] === b[1][0] &&
|
|
61
|
+
a[1][1] === b[1][1]);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Element-wise approximate equality.
|
|
65
|
+
*
|
|
66
|
+
* Not transitive, so it is not an equality in the sense a `Set` or `Map` needs,
|
|
67
|
+
* and never a basis for ordering — use {@link mat2x2Equals} there.
|
|
68
|
+
*/
|
|
69
|
+
export function mat2x2IsClose(a, b, tolerance) {
|
|
70
|
+
return (isCloseTo(a[0][0], b[0][0], tolerance) &&
|
|
71
|
+
isCloseTo(a[0][1], b[0][1], tolerance) &&
|
|
72
|
+
isCloseTo(a[1][0], b[1][0], tolerance) &&
|
|
73
|
+
isCloseTo(a[1][1], b[1][1], tolerance));
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Creates a 2×2 rotation matrix for the given angle in radians
|
|
77
|
+
* (counter-clockwise).
|
|
78
|
+
*/
|
|
79
|
+
export function mat2x2FromRotation(angle) {
|
|
80
|
+
const cos = Math.cos(angle);
|
|
81
|
+
const sin = Math.sin(angle);
|
|
82
|
+
return [
|
|
83
|
+
[cos, -sin],
|
|
84
|
+
[sin, cos],
|
|
85
|
+
];
|
|
86
|
+
}
|
|
87
|
+
export function mat2x2Invert(matrix) {
|
|
88
|
+
const det = mat2x2Determinant(matrix);
|
|
89
|
+
if (det === 0) {
|
|
90
|
+
throw new Error("Matrix is not invertible");
|
|
91
|
+
}
|
|
92
|
+
const [[a, b], [c, d]] = matrix;
|
|
93
|
+
const adjugate = [
|
|
94
|
+
[d, -b],
|
|
95
|
+
[-c, a],
|
|
96
|
+
];
|
|
97
|
+
return mat2x2MultiplyScalar(adjugate, 1 / det);
|
|
98
|
+
}
|
|
99
|
+
export function mat2x2Negate(matrix) {
|
|
100
|
+
return [
|
|
101
|
+
[-matrix[0][0], -matrix[0][1]],
|
|
102
|
+
[-matrix[1][0], -matrix[1][1]],
|
|
103
|
+
];
|
|
104
|
+
}
|
|
105
|
+
export function mat2x2Sub(a, b) {
|
|
106
|
+
return [
|
|
107
|
+
[a[0][0] - b[0][0], a[0][1] - b[0][1]],
|
|
108
|
+
[a[1][0] - b[1][0], a[1][1] - b[1][1]],
|
|
109
|
+
];
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=mat2x2.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mat2x2.js","sourceRoot":"","sources":["../src/mat2x2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAyB,MAAM,cAAc,CAAC;AAChE,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAMrD,MAAM,CAAC,MAAM,WAAW,GAAG;IAC1B,CAAC,CAAC,EAAE,CAAC,CAAC;IACN,CAAC,CAAC,EAAE,CAAC,CAAC;CACoB,CAAC;AAC5B,MAAM,CAAC,MAAM,eAAe,GAAG;IAC9B,CAAC,CAAC,EAAE,CAAC,CAAC;IACN,CAAC,CAAC,EAAE,CAAC,CAAC;CACoB,CAAC;AAE5B,MAAM,UAAU,SAAS,CAAC,CAAS,EAAE,CAAS;IAC7C,OAAO;QACN,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACtC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;KACtC,CAAC;AACH,CAAC;AAED,MAAM,UAAU,oBAAoB,CAAC,MAAc,EAAE,MAAc;IAClE,OAAO;QACN,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,EAAE,MAAM,CAAC,C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,CAAC,CAAC,CAAC,CAAC;QACtC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;KACtC,CAAC;AACH,CAAC"}
|
package/dist/mat3x3.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { type IsCloseToOptions } from "@ac-kit/core";
|
|
2
|
+
import { Vec3 } from "./vec3.js";
|
|
3
|
+
export type Mat3x3 = [
|
|
4
|
+
[
|
|
5
|
+
number,
|
|
6
|
+
number,
|
|
7
|
+
number
|
|
8
|
+
],
|
|
9
|
+
[
|
|
10
|
+
number,
|
|
11
|
+
number,
|
|
12
|
+
number
|
|
13
|
+
],
|
|
14
|
+
[
|
|
15
|
+
number,
|
|
16
|
+
number,
|
|
17
|
+
number
|
|
18
|
+
]
|
|
19
|
+
];
|
|
20
|
+
export declare const MAT3X3_ZERO: [[0, 0, 0], [0, 0, 0], [0, 0, 0]];
|
|
21
|
+
export declare const MAT3X3_IDENTITY: [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
|
|
22
|
+
export declare function mat3x3Add(a: Mat3x3, b: Mat3x3): Mat3x3;
|
|
23
|
+
export declare function mat3x3Determinant(matrix: Mat3x3): number;
|
|
24
|
+
/** Exact element-wise equality. See {@link mat3x3IsClose} for a tolerant test. */
|
|
25
|
+
export declare function mat3x3Equals(a: Mat3x3, b: Mat3x3): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Element-wise approximate equality.
|
|
28
|
+
*
|
|
29
|
+
* Not transitive, so it is not an equality in the sense a `Set` or `Map` needs,
|
|
30
|
+
* and never a basis for ordering — use {@link mat3x3Equals} there.
|
|
31
|
+
*/
|
|
32
|
+
export declare function mat3x3IsClose(a: Mat3x3, b: Mat3x3, tolerance: IsCloseToOptions): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Creates a 3×3 rotation matrix that rotates around the X axis by the given
|
|
35
|
+
* angle in radians (right-hand rule).
|
|
36
|
+
*/
|
|
37
|
+
export declare function mat3x3FromRotationX(angle: number): Mat3x3;
|
|
38
|
+
/**
|
|
39
|
+
* Creates a 3×3 rotation matrix that rotates around the Y axis by the given
|
|
40
|
+
* angle in radians (right-hand rule).
|
|
41
|
+
*/
|
|
42
|
+
export declare function mat3x3FromRotationY(angle: number): Mat3x3;
|
|
43
|
+
/**
|
|
44
|
+
* Creates a 3×3 rotation matrix that rotates around the Z axis by the given
|
|
45
|
+
* angle in radians (right-hand rule).
|
|
46
|
+
*/
|
|
47
|
+
export declare function mat3x3FromRotationZ(angle: number): Mat3x3;
|
|
48
|
+
export declare function mat3x3FromScale(sx: number, sy: number, sz: number): Mat3x3;
|
|
49
|
+
export declare function mat3x3Invert(matrix: Mat3x3): Mat3x3;
|
|
50
|
+
export declare function mat3x3Multiply(a: Mat3x3, b: Mat3x3): Mat3x3;
|
|
51
|
+
export declare function mat3x3MultiplyCols(matrix: Mat3x3, vector: Vec3): Mat3x3;
|
|
52
|
+
export declare function mat3x3MultiplyRows(matrix: Mat3x3, vector: Vec3): Mat3x3;
|
|
53
|
+
export declare function mat3x3MultiplyScalar(matrix: Mat3x3, scalar: number): Mat3x3;
|
|
54
|
+
export declare function mat3x3Negate(matrix: Mat3x3): Mat3x3;
|
|
55
|
+
export declare function mat3x3Sub(a: Mat3x3, b: Mat3x3): Mat3x3;
|
|
56
|
+
export declare function mat3x3Transpose(matrix: Mat3x3): Mat3x3;
|
|
57
|
+
//# sourceMappingURL=mat3x3.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mat3x3.d.ts","sourceRoot":"","sources":["../src/mat3x3.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,KAAK,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAGhE,OAAO,EAAE,IAAI,EA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AAG,MAAM,CAkBvE;AAED,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,GAAG,MAAM,CAkBvE;AAED,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAM3E;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAMnD;AAED,wBAAgB,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAMtD;AAED,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAMtD"}
|
package/dist/mat3x3.js
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { isCloseTo } from "@ac-kit/core";
|
|
2
|
+
import { diffOfProducts, dotPrecise } from "@ac-kit/math-scalar";
|
|
3
|
+
export const MAT3X3_ZERO = [
|
|
4
|
+
[0, 0, 0],
|
|
5
|
+
[0, 0, 0],
|
|
6
|
+
[0, 0, 0],
|
|
7
|
+
];
|
|
8
|
+
export const MAT3X3_IDENTITY = [
|
|
9
|
+
[1, 0, 0],
|
|
10
|
+
[0, 1, 0],
|
|
11
|
+
[0, 0, 1],
|
|
12
|
+
];
|
|
13
|
+
export function mat3x3Add(a, b) {
|
|
14
|
+
return [
|
|
15
|
+
[a[0][0] + b[0][0], a[0][1] + b[0][1], a[0][2] + b[0][2]],
|
|
16
|
+
[a[1][0] + b[1][0], a[1][1] + b[1][1], a[1][2] + b[1][2]],
|
|
17
|
+
[a[2][0] + b[2][0], a[2][1] + b[2][1], a[2][2] + b[2][2]],
|
|
18
|
+
];
|
|
19
|
+
}
|
|
20
|
+
export function mat3x3Determinant(matrix) {
|
|
21
|
+
const [[a, b, c], [d, e, f], [g, h, i]] = matrix;
|
|
22
|
+
return dotPrecise([a, -b, c], [
|
|
23
|
+
diffOfProducts(e, i, f, h),
|
|
24
|
+
diffOfProducts(d, i, f, g),
|
|
25
|
+
diffOfProducts(d, h, e, g),
|
|
26
|
+
]);
|
|
27
|
+
}
|
|
28
|
+
/** Exact element-wise equality. See {@link mat3x3IsClose} for a tolerant test. */
|
|
29
|
+
export function mat3x3Equals(a, b) {
|
|
30
|
+
return (a[0][0] === b[0][0] &&
|
|
31
|
+
a[0][1] === b[0][1] &&
|
|
32
|
+
a[0][2] === b[0][2] &&
|
|
33
|
+
a[1][0] === b[1][0] &&
|
|
34
|
+
a[1][1] === b[1][1] &&
|
|
35
|
+
a[1][2] === b[1][2] &&
|
|
36
|
+
a[2][0] === b[2][0] &&
|
|
37
|
+
a[2][1] === b[2][1] &&
|
|
38
|
+
a[2][2] === b[2][2]);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Element-wise approximate equality.
|
|
42
|
+
*
|
|
43
|
+
* Not transitive, so it is not an equality in the sense a `Set` or `Map` needs,
|
|
44
|
+
* and never a basis for ordering — use {@link mat3x3Equals} there.
|
|
45
|
+
*/
|
|
46
|
+
export function mat3x3IsClose(a, b, tolerance) {
|
|
47
|
+
for (let row = 0; row < 3; row++) {
|
|
48
|
+
for (let column = 0; column < 3; column++) {
|
|
49
|
+
if (!isCloseTo(a[row][column], b[row][column], tolerance)) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Creates a 3×3 rotation matrix that rotates around the X axis by the given
|
|
58
|
+
* angle in radians (right-hand rule).
|
|
59
|
+
*/
|
|
60
|
+
export function mat3x3FromRotationX(angle) {
|
|
61
|
+
const cos = Math.cos(angle);
|
|
62
|
+
const sin = Math.sin(angle);
|
|
63
|
+
return [
|
|
64
|
+
[1, 0, 0],
|
|
65
|
+
[0, cos, -sin],
|
|
66
|
+
[0, sin, cos],
|
|
67
|
+
];
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Creates a 3×3 rotation matrix that rotates around the Y axis by the given
|
|
71
|
+
* angle in radians (right-hand rule).
|
|
72
|
+
*/
|
|
73
|
+
export function mat3x3FromRotationY(angle) {
|
|
74
|
+
const cos = Math.cos(angle);
|
|
75
|
+
const sin = Math.sin(angle);
|
|
76
|
+
return [
|
|
77
|
+
[cos, 0, sin],
|
|
78
|
+
[0, 1, 0],
|
|
79
|
+
[-sin, 0, cos],
|
|
80
|
+
];
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Creates a 3×3 rotation matrix that rotates around the Z axis by the given
|
|
84
|
+
* angle in radians (right-hand rule).
|
|
85
|
+
*/
|
|
86
|
+
export function mat3x3FromRotationZ(angle) {
|
|
87
|
+
const cos = Math.cos(angle);
|
|
88
|
+
const sin = Math.sin(angle);
|
|
89
|
+
return [
|
|
90
|
+
[cos, -sin, 0],
|
|
91
|
+
[sin, cos, 0],
|
|
92
|
+
[0, 0, 1],
|
|
93
|
+
];
|
|
94
|
+
}
|
|
95
|
+
export function mat3x3FromScale(sx, sy, sz) {
|
|
96
|
+
return [
|
|
97
|
+
[sx, 0, 0],
|
|
98
|
+
[0, sy, 0],
|
|
99
|
+
[0, 0, sz],
|
|
100
|
+
];
|
|
101
|
+
}
|
|
102
|
+
export function mat3x3Invert(matrix) {
|
|
103
|
+
const det = mat3x3Determinant(matrix);
|
|
104
|
+
if (det === 0) {
|
|
105
|
+
throw new Error("Matrix is singular and cannot be inverted.");
|
|
106
|
+
}
|
|
107
|
+
const [[a, b, c], [d, e, f], [g, h, i]] = matrix;
|
|
108
|
+
const adjugate = [
|
|
109
|
+
[
|
|
110
|
+
diffOfProducts(e, i, f, h),
|
|
111
|
+
diffOfProducts(c, h, b, i),
|
|
112
|
+
diffOfProducts(b, f, c, e),
|
|
113
|
+
],
|
|
114
|
+
[
|
|
115
|
+
diffOfProducts(f, g, d, i),
|
|
116
|
+
diffOfProducts(a, i, c, g),
|
|
117
|
+
diffOfProducts(c, d, a, f),
|
|
118
|
+
],
|
|
119
|
+
[
|
|
120
|
+
diffOfProducts(d, h, e, g),
|
|
121
|
+
diffOfProducts(b, g, a, h),
|
|
122
|
+
diffOfProducts(a, e, b, d),
|
|
123
|
+
],
|
|
124
|
+
];
|
|
125
|
+
return mat3x3MultiplyScalar(adjugate, 1 / det);
|
|
126
|
+
}
|
|
127
|
+
export function mat3x3Multiply(a, b) {
|
|
128
|
+
const columns = mat3x3Transpose(b);
|
|
129
|
+
return [
|
|
130
|
+
[
|
|
131
|
+
dotPrecise(a[0], columns[0]),
|
|
132
|
+
dotPrecise(a[0], columns[1]),
|
|
133
|
+
dotPrecise(a[0], columns[2]),
|
|
134
|
+
],
|
|
135
|
+
[
|
|
136
|
+
dotPrecise(a[1], columns[0]),
|
|
137
|
+
dotPrecise(a[1], columns[1]),
|
|
138
|
+
dotPrecise(a[1], columns[2]),
|
|
139
|
+
],
|
|
140
|
+
[
|
|
141
|
+
dotPrecise(a[2], columns[0]),
|
|
142
|
+
dotPrecise(a[2], columns[1]),
|
|
143
|
+
dotPrecise(a[2], columns[2]),
|
|
144
|
+
],
|
|
145
|
+
];
|
|
146
|
+
}
|
|
147
|
+
export function mat3x3MultiplyCols(matrix, vector) {
|
|
148
|
+
return [
|
|
149
|
+
[
|
|
150
|
+
matrix[0][0] * vector[0],
|
|
151
|
+
matrix[0][1] * vector[1],
|
|
152
|
+
matrix[0][2] * vector[2],
|
|
153
|
+
],
|
|
154
|
+
[
|
|
155
|
+
matrix[1][0] * vector[0],
|
|
156
|
+
matrix[1][1] * vector[1],
|
|
157
|
+
matrix[1][2] * vector[2],
|
|
158
|
+
],
|
|
159
|
+
[
|
|
160
|
+
matrix[2][0] * vector[0],
|
|
161
|
+
matrix[2][1] * vector[1],
|
|
162
|
+
matrix[2][2] * vector[2],
|
|
163
|
+
],
|
|
164
|
+
];
|
|
165
|
+
}
|
|
166
|
+
export function mat3x3MultiplyRows(matrix, vector) {
|
|
167
|
+
return [
|
|
168
|
+
[
|
|
169
|
+
matrix[0][0] * vector[0],
|
|
170
|
+
matrix[0][1] * vector[0],
|
|
171
|
+
matrix[0][2] * vector[0],
|
|
172
|
+
],
|
|
173
|
+
[
|
|
174
|
+
matrix[1][0] * vector[1],
|
|
175
|
+
matrix[1][1] * vector[1],
|
|
176
|
+
matrix[1][2] * vector[1],
|
|
177
|
+
],
|
|
178
|
+
[
|
|
179
|
+
matrix[2][0] * vector[2],
|
|
180
|
+
matrix[2][1] * vector[2],
|
|
181
|
+
matrix[2][2] * vector[2],
|
|
182
|
+
],
|
|
183
|
+
];
|
|
184
|
+
}
|
|
185
|
+
export function mat3x3MultiplyScalar(matrix, scalar) {
|
|
186
|
+
return [
|
|
187
|
+
[matrix[0][0] * scalar, matrix[0][1] * scalar, matrix[0][2] * scalar],
|
|
188
|
+
[matrix[1][0] * scalar, matrix[1][1] * scalar, matrix[1][2] * scalar],
|
|
189
|
+
[matrix[2][0] * scalar, matrix[2][1] * scalar, matrix[2][2] * scalar],
|
|
190
|
+
];
|
|
191
|
+
}
|
|
192
|
+
export function mat3x3Negate(matrix) {
|
|
193
|
+
return [
|
|
194
|
+
[-matrix[0][0], -matrix[0][1], -matrix[0][2]],
|
|
195
|
+
[-matrix[1][0], -matrix[1][1], -matrix[1][2]],
|
|
196
|
+
[-matrix[2][0], -matrix[2][1], -matrix[2][2]],
|
|
197
|
+
];
|
|
198
|
+
}
|
|
199
|
+
export function mat3x3Sub(a, b) {
|
|
200
|
+
return [
|
|
201
|
+
[a[0][0] - b[0][0], a[0][1] - b[0][1], a[0][2] - b[0][2]],
|
|
202
|
+
[a[1][0] - b[1][0], a[1][1] - b[1][1], a[1][2] - b[1][2]],
|
|
203
|
+
[a[2][0] - b[2][0], a[2][1] - b[2][1], a[2][2] - b[2][2]],
|
|
204
|
+
];
|
|
205
|
+
}
|
|
206
|
+
export function mat3x3Transpose(matrix) {
|
|
207
|
+
return [
|
|
208
|
+
[matrix[0][0], matrix[1][0], matrix[2][0]],
|
|
209
|
+
[matrix[0][1], matrix[1][1], matrix[2][1]],
|
|
210
|
+
[matrix[0][2], matrix[1][2], matrix[2][2]],
|
|
211
|
+
];
|
|
212
|
+
}
|
|
213
|
+
//# sourceMappingURL=mat3x3.js.map
|