glmaths 0.0.1
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/README.md +952 -0
- package/benchmark.js +747 -0
- package/dist/cjs/glmaths.d.ts +12053 -0
- package/dist/cjs/glmaths.js +6496 -0
- package/dist/cjs/glmaths.js.map +1 -0
- package/dist/cjs/glmaths.min.js +2 -0
- package/dist/cjs/glmaths.min.js.map +1 -0
- package/dist/esm/glmaths.d.ts +12053 -0
- package/dist/esm/glmaths.js +6453 -0
- package/dist/esm/glmaths.js.map +1 -0
- package/dist/esm/glmaths.min.js +2 -0
- package/dist/esm/glmaths.min.js.map +1 -0
- package/dist/glmaths.d.ts +12053 -0
- package/dist/glmaths.js +6501 -0
- package/dist/glmaths.js.map +1 -0
- package/dist/glmaths.min.js +2 -0
- package/dist/glmaths.min.js.map +1 -0
- package/docs.js +64 -0
- package/package.json +37 -0
- package/rollup.config.js +70 -0
- package/src/index.ts +19 -0
- package/src/internalUtils.ts +78 -0
- package/src/mat2.ts +324 -0
- package/src/mat2x3.ts +328 -0
- package/src/mat3.ts +629 -0
- package/src/mat4.ts +1319 -0
- package/src/quat.ts +819 -0
- package/src/quat2.ts +412 -0
- package/src/utils.ts +248 -0
- package/src/vec2.ts +798 -0
- package/src/vec3.ts +1069 -0
- package/src/vec4.ts +810 -0
- package/tests/mat2.test.ts +277 -0
- package/tests/mat2x3.test.ts +217 -0
- package/tests/mat3.test.ts +306 -0
- package/tests/mat4.test.ts +586 -0
- package/tests/quat.test.ts +418 -0
- package/tests/quat2.test.ts +222 -0
- package/tests/utils.test.ts +115 -0
- package/tests/vec2.test.ts +617 -0
- package/tests/vec3.test.ts +649 -0
- package/tests/vec4.test.ts +390 -0
- package/tsconfig.json +17 -0
- package/tsconfig.test.json +8 -0
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "glmaths",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"scripts": {
|
|
5
|
+
"build": "rollup -c",
|
|
6
|
+
"test": "TS_NODE_PROJECT=tsconfig.test.json node --require ts-node/register --test tests/*.test.ts",
|
|
7
|
+
"bench": "node benchmark",
|
|
8
|
+
"benchmark": "node benchmark",
|
|
9
|
+
"docs": "node docs"
|
|
10
|
+
},
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"main": "dist/glmat.cjs.js",
|
|
13
|
+
"module": "dist/glmat.esm.js",
|
|
14
|
+
"browser": "dist/glmat.min.js",
|
|
15
|
+
"types": "dist/glmat.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"import": "./dist/glmat.esm.js",
|
|
19
|
+
"require": "./dist/glmat.cjs.js",
|
|
20
|
+
"types": "./dist/glmat.d.ts"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@rollup/plugin-terser": "^1.0.0",
|
|
25
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
26
|
+
"@types/node": "^25.5.2",
|
|
27
|
+
"benchmark": "^2.1.4",
|
|
28
|
+
"gl-matrix": "^3.4.4",
|
|
29
|
+
"rollup": "^4.60.1",
|
|
30
|
+
"rollup-plugin-dts": "^6.4.1",
|
|
31
|
+
"ts-morph": "^27.0.2",
|
|
32
|
+
"ts-node": "^10.9.2",
|
|
33
|
+
"tslib": "^2.8.1",
|
|
34
|
+
"tsx": "^4.21.0",
|
|
35
|
+
"typescript": "file:../TypeScript2"
|
|
36
|
+
}
|
|
37
|
+
}
|
package/rollup.config.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import typescript from "@rollup/plugin-typescript";
|
|
2
|
+
import dts from "rollup-plugin-dts";
|
|
3
|
+
import terser from "@rollup/plugin-terser";
|
|
4
|
+
import { rmSync } from "fs";
|
|
5
|
+
|
|
6
|
+
const cleanupTypes = {
|
|
7
|
+
name: "cleanup-types",
|
|
8
|
+
closeBundle() {
|
|
9
|
+
rmSync("dist/types", { recursive: true, force: true });
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const TERSER_OPTIONS = {
|
|
14
|
+
compress: {
|
|
15
|
+
passes: 3,
|
|
16
|
+
unsafe: true,
|
|
17
|
+
unsafe_math: true,
|
|
18
|
+
pure_getters: true
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default [
|
|
23
|
+
{
|
|
24
|
+
input: "src/index.ts",
|
|
25
|
+
output: [
|
|
26
|
+
{ file: "dist/cjs/glmaths.js", format: "cjs", sourcemap: true },
|
|
27
|
+
{ file: "dist/cjs/glmaths.min.js", format: "cjs", sourcemap: true, plugins: [terser(TERSER_OPTIONS)] },
|
|
28
|
+
{ file: "dist/esm/glmaths.js", format: "esm", sourcemap: true },
|
|
29
|
+
{ file: "dist/esm/glmaths.min.js", format: "esm", sourcemap: true, plugins: [terser(TERSER_OPTIONS)] },
|
|
30
|
+
],
|
|
31
|
+
plugins: [typescript({
|
|
32
|
+
tsconfig: "./tsconfig.json",
|
|
33
|
+
declaration: false,
|
|
34
|
+
declarationMap: false
|
|
35
|
+
})],
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
input: "src/index.ts",
|
|
39
|
+
output: [
|
|
40
|
+
{
|
|
41
|
+
file: "dist/glmaths.js",
|
|
42
|
+
format: "iife",
|
|
43
|
+
name: "glmaths",
|
|
44
|
+
sourcemap: true,
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
file: "dist/glmaths.min.js",
|
|
48
|
+
format: "iife",
|
|
49
|
+
name: "glmaths",
|
|
50
|
+
plugins: [terser(TERSER_OPTIONS)],
|
|
51
|
+
sourcemap: true,
|
|
52
|
+
}
|
|
53
|
+
],
|
|
54
|
+
plugins: [typescript({
|
|
55
|
+
tsconfig: "./tsconfig.json",
|
|
56
|
+
declarationDir: "./dist/types",
|
|
57
|
+
declaration: true,
|
|
58
|
+
outDir: undefined
|
|
59
|
+
})],
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
input: "dist/types/index.d.ts",
|
|
63
|
+
output: [
|
|
64
|
+
{ file: "dist/glmaths.d.ts", format: "es" },
|
|
65
|
+
{ file: "dist/esm/glmaths.d.ts", format: "esm" },
|
|
66
|
+
{ file: "dist/cjs/glmaths.d.ts", format: "cjs" }
|
|
67
|
+
],
|
|
68
|
+
plugins: [dts(), cleanupTypes],
|
|
69
|
+
}
|
|
70
|
+
];
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
EPSILON: 0.000001,
|
|
3
|
+
|
|
4
|
+
RANDOM: Math.random,
|
|
5
|
+
ANGLE_ORDER: 'zyx',
|
|
6
|
+
ALWAYS_COPY: true,
|
|
7
|
+
|
|
8
|
+
LEFT_HANDED: false
|
|
9
|
+
}
|
|
10
|
+
export * from './vec2'
|
|
11
|
+
export * from './vec3'
|
|
12
|
+
export * from './vec4'
|
|
13
|
+
export * from './quat'
|
|
14
|
+
export * from './quat2'
|
|
15
|
+
export * from './mat2'
|
|
16
|
+
export * from './mat2x3'
|
|
17
|
+
export * from './mat3'
|
|
18
|
+
export * from './mat4'
|
|
19
|
+
export * from './utils'
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import glm from '.'
|
|
2
|
+
import { Vec2 } from './vec2'
|
|
3
|
+
import { Vec3 } from './vec3'
|
|
4
|
+
import { Vec4 } from './vec4'
|
|
5
|
+
|
|
6
|
+
export function equals(a: number, b: number) {
|
|
7
|
+
return Math.abs(a - b) <= glm.EPSILON * Math.max(1.0, Math.abs(a), Math.abs(b))
|
|
8
|
+
}
|
|
9
|
+
export function defineSwizzles(prototype: any, N = 4, S = ['xyzw', 'rgba', 'stpq', 'uv']) {
|
|
10
|
+
// let code = `interface Vec${N}Class {`
|
|
11
|
+
for (let s of S) {
|
|
12
|
+
// code += '\n '
|
|
13
|
+
for (let n = 2; n <= N; ++n) {
|
|
14
|
+
const sn = `01${s.substring(0, n)}`
|
|
15
|
+
switch (n) {
|
|
16
|
+
case 2:
|
|
17
|
+
for (let a = 0; a < sn.length; ++a)
|
|
18
|
+
for (let b = 0; b < sn.length; ++b) {
|
|
19
|
+
const key = `${sn[a]}${sn[b]}`
|
|
20
|
+
if (/^[01]/.test(key) || Object.prototype.hasOwnProperty.call(prototype, key))
|
|
21
|
+
continue
|
|
22
|
+
// code += ` ${key}: Vec2;`
|
|
23
|
+
Object.defineProperty(prototype, key, {
|
|
24
|
+
get() {
|
|
25
|
+
return new Vec2(
|
|
26
|
+
a < 2 ? a : this[a - 2],
|
|
27
|
+
b < 2 ? b : this[b - 2]
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
break
|
|
33
|
+
case 3:
|
|
34
|
+
for (let a = 0; a < sn.length; ++a)
|
|
35
|
+
for (let b = 0; b < sn.length; ++b)
|
|
36
|
+
for (let c = 0; c < sn.length; ++c) {
|
|
37
|
+
const key = `${sn[a]}${sn[b]}${sn[c]}`
|
|
38
|
+
if (/^[01]/.test(key) || Object.prototype.hasOwnProperty.call(prototype, key))
|
|
39
|
+
continue
|
|
40
|
+
// code += ` ${key}: Vec3;`
|
|
41
|
+
Object.defineProperty(prototype, key, {
|
|
42
|
+
get() {
|
|
43
|
+
return new Vec3(
|
|
44
|
+
a < 2 ? a : this[a - 2],
|
|
45
|
+
b < 2 ? b : this[b - 2],
|
|
46
|
+
c < 2 ? c : this[c - 2]
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
break
|
|
52
|
+
case 4:
|
|
53
|
+
for (let a = 0; a < sn.length; ++a)
|
|
54
|
+
for (let b = 0; b < sn.length; ++b)
|
|
55
|
+
for (let c = 0; c < sn.length; ++c)
|
|
56
|
+
for (let d = 0; d < sn.length; ++d) {
|
|
57
|
+
const key = `${sn[a]}${sn[b]}${sn[c]}${sn[d]}`
|
|
58
|
+
if (/^[01]/.test(key) || Object.prototype.hasOwnProperty.call(prototype, key))
|
|
59
|
+
continue
|
|
60
|
+
// code += ` ${key}: Vec4;`
|
|
61
|
+
Object.defineProperty(prototype, key, {
|
|
62
|
+
get() {
|
|
63
|
+
return new Vec4(
|
|
64
|
+
a < 2 ? a : this[a - 2],
|
|
65
|
+
b < 2 ? b : this[b - 2],
|
|
66
|
+
c < 2 ? c : this[c - 2],
|
|
67
|
+
d < 2 ? d : this[d - 2]
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
break
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// code += '\n}'
|
|
77
|
+
// console.log(code)
|
|
78
|
+
}
|
package/src/mat2.ts
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
import glm from '.'
|
|
2
|
+
import { equals } from './internalUtils'
|
|
3
|
+
import { Vec2 } from './vec2'
|
|
4
|
+
|
|
5
|
+
/** 2x2 Matrix in column-major order
|
|
6
|
+
* @extends Float32Array
|
|
7
|
+
*/
|
|
8
|
+
export class Mat2 extends Float32Array {
|
|
9
|
+
|
|
10
|
+
static get identity() { return new Mat2(1, 0, 0, 1) }
|
|
11
|
+
static get Identity() { return new Mat2(1, 0, 0, 1) }
|
|
12
|
+
static get IDENTITY() { return new Mat2(1, 0, 0, 1) }
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new Mat2
|
|
16
|
+
*
|
|
17
|
+
* @param {Number} m00 component in column 0, row 0
|
|
18
|
+
* @param {Number} m01 component in column 0, row 1
|
|
19
|
+
* @param {Number} m10 component in column 1, row 0
|
|
20
|
+
* @param {Number} m11 component in column 1, row 1
|
|
21
|
+
*/
|
|
22
|
+
constructor(m00 = 0, m01 = 0, m10 = 0, m11 = 0) {
|
|
23
|
+
super(4)
|
|
24
|
+
this[0] = m00
|
|
25
|
+
this[1] = m01
|
|
26
|
+
this[2] = m10
|
|
27
|
+
this[3] = m11
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Creates a new Mat2 initialized with values from a matrix
|
|
32
|
+
*
|
|
33
|
+
* @returns {Mat2} a new Mat2
|
|
34
|
+
*/
|
|
35
|
+
clone() {
|
|
36
|
+
return new Mat2(this[0], this[1], this[2], this[3])
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Transposes a mat2
|
|
41
|
+
*
|
|
42
|
+
* @param {Mat2} out the receiving matrix, defaults to this
|
|
43
|
+
* @returns {Mat2} out
|
|
44
|
+
*/
|
|
45
|
+
transpose(out = glm.ALWAYS_COPY ? new Mat2() : this) {
|
|
46
|
+
if (out === this) {
|
|
47
|
+
const tmp = out[1]
|
|
48
|
+
out[1] = out[2]
|
|
49
|
+
out[2] = tmp
|
|
50
|
+
} else {
|
|
51
|
+
out[0] = this[0]
|
|
52
|
+
out[1] = this[2]
|
|
53
|
+
out[2] = this[1]
|
|
54
|
+
out[3] = this[3]
|
|
55
|
+
}
|
|
56
|
+
return out
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Inverts a mat2
|
|
61
|
+
*
|
|
62
|
+
* @param {Mat2} out the receiving matrix, defaults to this
|
|
63
|
+
* @returns {Mat2} out or null if the matrix is not invertible
|
|
64
|
+
*/
|
|
65
|
+
invert(out = glm.ALWAYS_COPY ? new Mat2() : this) {
|
|
66
|
+
const a0 = this[0], a1 = this[1], a2 = this[2], a3 = this[3]
|
|
67
|
+
|
|
68
|
+
// Calculate the determinant
|
|
69
|
+
let det = a0 * a3 - a2 * a1
|
|
70
|
+
if (!det) return null;
|
|
71
|
+
det = 1.0 / det
|
|
72
|
+
|
|
73
|
+
out[0] = a3 * det
|
|
74
|
+
out[1] = -a1 * det
|
|
75
|
+
out[2] = -a2 * det
|
|
76
|
+
out[3] = a0 * det
|
|
77
|
+
return out
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Calculates the adjugate of a mat2
|
|
82
|
+
*
|
|
83
|
+
* @param {Mat2} out the receiving matrix, defaults to this
|
|
84
|
+
* @returns {Mat2} out
|
|
85
|
+
*/
|
|
86
|
+
adjoint(out = glm.ALWAYS_COPY ? new Mat2() : this) {
|
|
87
|
+
let a0 = this[0]
|
|
88
|
+
out[0] = this[3]
|
|
89
|
+
out[1] = -this[1]
|
|
90
|
+
out[2] = -this[2]
|
|
91
|
+
out[3] = a0
|
|
92
|
+
return out
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Calculates the determinant of a mat2
|
|
97
|
+
*
|
|
98
|
+
* @returns {Number} determinant of a mat2
|
|
99
|
+
*/
|
|
100
|
+
determinant() {
|
|
101
|
+
return this[0] * this[3] - this[2] * this[1]
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Rotates a mat2 by the given angle
|
|
106
|
+
*
|
|
107
|
+
* @param {Number} rad the angle to rotate the matrix by
|
|
108
|
+
* @param {Mat2} out the receiving matrix, defaults to this
|
|
109
|
+
* @returns {Mat2} out
|
|
110
|
+
*/
|
|
111
|
+
rotate(rad: number, out = glm.ALWAYS_COPY ? new Mat2() : this) {
|
|
112
|
+
const a0 = this[0], a1 = this[1], a2 = this[2], a3 = this[3]
|
|
113
|
+
const s = Math.sin(rad), c = Math.cos(rad)
|
|
114
|
+
out[0] = a0 * c + a2 * s
|
|
115
|
+
out[1] = a1 * c + a3 * s
|
|
116
|
+
out[2] = a0 * -s + a2 * c
|
|
117
|
+
out[3] = a1 * -s + a3 * c
|
|
118
|
+
return out
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Scales a mat2 by the dimensions in the given Vec2
|
|
123
|
+
*
|
|
124
|
+
* @param {Vec2} v the Vec2 to scale the matrix by
|
|
125
|
+
* @param {Mat2} out the receiving matrix, defaults to this
|
|
126
|
+
* @returns {Mat2} out
|
|
127
|
+
*/
|
|
128
|
+
scale(v: Vec2, out = glm.ALWAYS_COPY ? new Mat2() : this) {
|
|
129
|
+
const v0 = v[0], v1 = v[1]
|
|
130
|
+
out[0] = this[0] * v0
|
|
131
|
+
out[1] = this[1] * v0
|
|
132
|
+
out[2] = this[2] * v1
|
|
133
|
+
out[3] = this[3] * v1
|
|
134
|
+
return out
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Creates a Mat2 from a given angle
|
|
139
|
+
*
|
|
140
|
+
* @param {Number} rad the angle to rotate the matrix by
|
|
141
|
+
* @param {Mat2} out the receiving matrix, defaults to new Mat2()
|
|
142
|
+
* @returns {Mat2} out
|
|
143
|
+
*/
|
|
144
|
+
static fromRotation(rad: number, out = new Mat2()) {
|
|
145
|
+
const s = Math.sin(rad), c = Math.cos(rad)
|
|
146
|
+
out[0] = c
|
|
147
|
+
out[1] = s
|
|
148
|
+
out[2] = -s
|
|
149
|
+
out[3] = c
|
|
150
|
+
return out
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Creates a Mat2 from a scaling vector
|
|
155
|
+
*
|
|
156
|
+
* @param {Vec2} v scaling vector
|
|
157
|
+
* @param {Mat2} out the receiving matrix, defaults to new Mat2()
|
|
158
|
+
* @returns {Mat2} out
|
|
159
|
+
*/
|
|
160
|
+
static fromScaling(v: Vec2, out = new Mat2()) {
|
|
161
|
+
out[0] = v[0]
|
|
162
|
+
out[1] = out[2] = 0
|
|
163
|
+
out[3] = v[1]
|
|
164
|
+
return out
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Returns a string representation of a mat2
|
|
169
|
+
*
|
|
170
|
+
* @returns {String} string representation of the matrix
|
|
171
|
+
*/
|
|
172
|
+
toString() {
|
|
173
|
+
return `mat2x2(${this[0]}, ${this[1]}, ${this[2]}, ${this[3]})`
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Returns Frobenius norm of a mat2
|
|
178
|
+
* @returns {Number} Frobenius norm
|
|
179
|
+
*/
|
|
180
|
+
frob() {
|
|
181
|
+
const a0 = this[0], a1 = this[1], a2 = this[2], a3 = this[3]
|
|
182
|
+
return Math.sqrt(a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Returns L, D and U matrices (Lower triangular, Diagonal and Upper triangular) by factorizing a matrix
|
|
187
|
+
* @param {Mat2} L the lower triangular matrix
|
|
188
|
+
* @param {Mat2} D the diagonal matrix
|
|
189
|
+
* @param {Mat2} U the upper triangular matrix
|
|
190
|
+
*/
|
|
191
|
+
LDU(L = new Mat2(), D = new Mat2(), U = new Mat2()) {
|
|
192
|
+
L[2] = this[2] / this[0]
|
|
193
|
+
U[0] = this[0]
|
|
194
|
+
U[1] = this[1]
|
|
195
|
+
U[3] = this[3] - L[2] * U[1]
|
|
196
|
+
return [L, D, U]
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Adds two Mat2's
|
|
201
|
+
*
|
|
202
|
+
* @param {Mat2} b the second operand
|
|
203
|
+
* @param {Mat2} out the receiving matrix, defaults to this
|
|
204
|
+
* @returns {Mat2} out
|
|
205
|
+
*/
|
|
206
|
+
plus(b: Mat2, out = glm.ALWAYS_COPY ? new Mat2() : this) {
|
|
207
|
+
out[0] = this[0] + b[0]
|
|
208
|
+
out[1] = this[1] + b[1]
|
|
209
|
+
out[2] = this[2] + b[2]
|
|
210
|
+
out[3] = this[3] + b[3]
|
|
211
|
+
return out
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Subtracts matrix b from a mat2
|
|
216
|
+
*
|
|
217
|
+
* @param {Mat2} b the second operand
|
|
218
|
+
* @param {Mat2} out the receiving matrix, defaults to this
|
|
219
|
+
* @returns {Mat2} out
|
|
220
|
+
*/
|
|
221
|
+
minus(b: Mat2, out = glm.ALWAYS_COPY ? new Mat2() : this) {
|
|
222
|
+
out[0] = this[0] - b[0]
|
|
223
|
+
out[1] = this[1] - b[1]
|
|
224
|
+
out[2] = this[2] - b[2]
|
|
225
|
+
out[3] = this[3] - b[3]
|
|
226
|
+
return out
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Multiplies a mat2 by another Mat2
|
|
231
|
+
*
|
|
232
|
+
* @param {Mat2} b the second operand
|
|
233
|
+
* @param {Mat2} out the receiving matrix, defaults to this
|
|
234
|
+
* @returns {Mat2} out
|
|
235
|
+
*/
|
|
236
|
+
multiply(b: Vec2): Vec2
|
|
237
|
+
multiply(b: Mat2, out?: Mat2): Mat2
|
|
238
|
+
multiply(b: Mat2 | Vec2, out = glm.ALWAYS_COPY ? new Mat2() : this) {
|
|
239
|
+
if (b instanceof Vec2)
|
|
240
|
+
return b.transformMat2(this)
|
|
241
|
+
|
|
242
|
+
const a0 = this[0], a1 = this[1], a2 = this[2], a3 = this[3]
|
|
243
|
+
const b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3]
|
|
244
|
+
out[0] = a0 * b0 + a2 * b1
|
|
245
|
+
out[1] = a1 * b0 + a3 * b1
|
|
246
|
+
out[2] = a0 * b2 + a2 * b3
|
|
247
|
+
out[3] = a1 * b2 + a3 * b3
|
|
248
|
+
return out
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Returns whether a mat2 and another Mat2 have exactly equal components
|
|
253
|
+
*
|
|
254
|
+
* @param {Mat2} b the matrix to compare against
|
|
255
|
+
* @returns {Boolean} true if the matrices are exactly equal
|
|
256
|
+
*/
|
|
257
|
+
exactEquals(b: Mat2) {
|
|
258
|
+
return this[0] === b[0] && this[1] === b[1] && this[2] === b[2] && this[3] === b[3]
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Returns whether a mat2 and another Mat2 have approximately equal components
|
|
262
|
+
*
|
|
263
|
+
* @param {Mat2} b the matrix to compare against
|
|
264
|
+
* @returns {Boolean} true if the matrices are approximately equal
|
|
265
|
+
*/
|
|
266
|
+
equals(b: Mat2) {
|
|
267
|
+
return (
|
|
268
|
+
equals(this[0], b[0]) && equals(this[1], b[1]) &&
|
|
269
|
+
equals(this[2], b[2]) && equals(this[3], b[3])
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Multiplies each element of a mat2 by a scalar value
|
|
275
|
+
*
|
|
276
|
+
* @param {Number} b amount to scale the matrix's elements by
|
|
277
|
+
* @param {Mat2} out the receiving matrix, defaults to this
|
|
278
|
+
* @returns {Mat2} out
|
|
279
|
+
*/
|
|
280
|
+
scaleScalar(b: number, out = glm.ALWAYS_COPY ? new Mat2() : this) {
|
|
281
|
+
out[0] = this[0] * b
|
|
282
|
+
out[1] = this[1] * b
|
|
283
|
+
out[2] = this[2] * b
|
|
284
|
+
out[3] = this[3] * b
|
|
285
|
+
return out;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
}
|
|
289
|
+
export interface Mat2 {
|
|
290
|
+
add: (b: Mat2, out?: Mat2) => Mat2
|
|
291
|
+
sub: (b: Mat2, out?: Mat2) => Mat2
|
|
292
|
+
subtract: (b: Mat2, out?: Mat2) => Mat2
|
|
293
|
+
mul(b: Vec2): Vec2
|
|
294
|
+
mul(b: Mat2, out?: Mat2): Mat2
|
|
295
|
+
mult(b: Vec2): Vec2
|
|
296
|
+
mult(b: Mat2, out?: Mat2): Mat2
|
|
297
|
+
times(b: Vec2): Vec2
|
|
298
|
+
times(b: Mat2, out?: Mat2): Mat2
|
|
299
|
+
multiplyScalar: (b: number, out?: Mat2) => Mat2
|
|
300
|
+
str: () => string
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// @aliases
|
|
304
|
+
Mat2.prototype.add = Mat2.prototype.plus
|
|
305
|
+
Mat2.prototype.sub = Mat2.prototype.minus
|
|
306
|
+
Mat2.prototype.subtract = Mat2.prototype.minus
|
|
307
|
+
Mat2.prototype.mul = Mat2.prototype.multiply
|
|
308
|
+
Mat2.prototype.mult = Mat2.prototype.multiply
|
|
309
|
+
Mat2.prototype.times = Mat2.prototype.multiply
|
|
310
|
+
Mat2.prototype.multiplyScalar = Mat2.prototype.scaleScalar
|
|
311
|
+
Mat2.prototype.str = Mat2.prototype.toString
|
|
312
|
+
|
|
313
|
+
const createMat2 = (...args: (number | Float32Array)[]): Mat2 => {
|
|
314
|
+
const out = new Mat2()
|
|
315
|
+
let i = 0
|
|
316
|
+
for (const a of args) {
|
|
317
|
+
if (typeof a === 'number') out[i++] = a
|
|
318
|
+
else for (const v of a) out[i++] = v
|
|
319
|
+
}
|
|
320
|
+
return out
|
|
321
|
+
}
|
|
322
|
+
Object.setPrototypeOf(createMat2, Mat2)
|
|
323
|
+
export const mat2 = createMat2 as typeof createMat2 & typeof Mat2
|
|
324
|
+
export const mat2x2 = mat2
|