glmaths 0.0.3 → 0.0.4
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 +561 -543
- package/dist/cjs/glmaths.d.ts +240 -198
- package/dist/cjs/glmaths.js +544 -462
- package/dist/cjs/glmaths.js.map +1 -1
- package/dist/cjs/glmaths.min.js +1 -1
- package/dist/cjs/glmaths.min.js.map +1 -1
- package/dist/esm/glmaths.d.ts +240 -198
- package/dist/esm/glmaths.js +544 -462
- package/dist/esm/glmaths.js.map +1 -1
- package/dist/esm/glmaths.min.js +1 -1
- package/dist/esm/glmaths.min.js.map +1 -1
- package/dist/glmaths.d.ts +240 -198
- package/dist/glmaths.js +544 -462
- package/dist/glmaths.js.map +1 -1
- package/dist/glmaths.min.js +1 -1
- package/dist/glmaths.min.js.map +1 -1
- package/examples/index.html +23 -0
- package/examples/main.js +134 -0
- package/examples/main.ts +159 -0
- package/package.json +1 -1
- package/src/mat2.ts +27 -27
- package/src/mat2x3.ts +37 -31
- package/src/mat3.ts +53 -54
- package/src/mat4.ts +81 -82
- package/src/quat.ts +36 -23
- package/src/quat2.ts +45 -31
- package/src/vec2.ts +90 -70
- package/src/vec3.ts +143 -121
- package/src/vec4.ts +79 -57
- package/tests/mat2.test.ts +53 -53
- package/tests/mat2x3.test.ts +46 -46
- package/tests/mat3.test.ts +59 -59
package/tests/mat3.test.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, it } from 'node:test'
|
|
2
2
|
import assert from 'node:assert/strict'
|
|
3
|
-
import glmaths, { Mat3, mat3, Mat2x3, Quat, Vec2, Vec3 } from '../dist/esm/glmaths'
|
|
3
|
+
import glmaths, { Mat3, mat3, Mat2x3, quat, Quat, vec2, Vec2, vec3, Vec3 } from '../dist/esm/glmaths'
|
|
4
4
|
|
|
5
5
|
function closeTo(actual: number, expected: number, numDigits = 5) {
|
|
6
6
|
const pass = Math.abs(actual - expected) < Math.pow(10, -numDigits) / 2
|
|
@@ -10,22 +10,22 @@ function closeTo(actual: number, expected: number, numDigits = 5) {
|
|
|
10
10
|
describe('Mat3', () => {
|
|
11
11
|
describe('constructor', () => {
|
|
12
12
|
it('creates zero matrix by default', () => {
|
|
13
|
-
const m =
|
|
13
|
+
const m = mat3()
|
|
14
14
|
for (let i = 0; i < 9; i++) assert.strictEqual(m[i], 0)
|
|
15
15
|
})
|
|
16
16
|
it('creates with given values', () => {
|
|
17
|
-
const m =
|
|
17
|
+
const m = mat3(1, 2, 3, 4, 5, 6, 7, 8, 9)
|
|
18
18
|
assert.strictEqual(m[0], 1)
|
|
19
19
|
assert.strictEqual(m[8], 9)
|
|
20
20
|
})
|
|
21
21
|
it('extends Float32Array with length 9', () => {
|
|
22
|
-
assert.strictEqual(
|
|
22
|
+
assert.strictEqual(mat3().length, 9)
|
|
23
23
|
})
|
|
24
24
|
})
|
|
25
25
|
|
|
26
26
|
describe('identity', () => {
|
|
27
27
|
it('creates identity', () => {
|
|
28
|
-
const m =
|
|
28
|
+
const m = mat3.identity
|
|
29
29
|
assert.strictEqual(m[0], 1)
|
|
30
30
|
assert.strictEqual(m[4], 1)
|
|
31
31
|
assert.strictEqual(m[8], 1)
|
|
@@ -36,7 +36,7 @@ describe('Mat3', () => {
|
|
|
36
36
|
|
|
37
37
|
describe('clone', () => {
|
|
38
38
|
it('clones independently', () => {
|
|
39
|
-
const a =
|
|
39
|
+
const a = mat3(1, 2, 3, 4, 5, 6, 7, 8, 9)
|
|
40
40
|
const b = a.clone()
|
|
41
41
|
b[0] = 99
|
|
42
42
|
assert.strictEqual(a[0], 1)
|
|
@@ -45,7 +45,7 @@ describe('Mat3', () => {
|
|
|
45
45
|
|
|
46
46
|
describe('transpose', () => {
|
|
47
47
|
it('transposes in-place', () => {
|
|
48
|
-
const m =
|
|
48
|
+
const m = mat3(1, 2, 3, 4, 5, 6, 7, 8, 9)
|
|
49
49
|
const r = m.transpose()
|
|
50
50
|
assert.strictEqual(r[1], 4)
|
|
51
51
|
assert.strictEqual(r[3], 2)
|
|
@@ -53,8 +53,8 @@ describe('Mat3', () => {
|
|
|
53
53
|
assert.strictEqual(r[6], 3)
|
|
54
54
|
})
|
|
55
55
|
it('transposes to out', () => {
|
|
56
|
-
const m =
|
|
57
|
-
const out =
|
|
56
|
+
const m = mat3(1, 2, 3, 4, 5, 6, 7, 8, 9)
|
|
57
|
+
const out = mat3()
|
|
58
58
|
m.transpose(out)
|
|
59
59
|
assert.strictEqual(out[1], 4)
|
|
60
60
|
assert.strictEqual(out[3], 2)
|
|
@@ -63,18 +63,18 @@ describe('Mat3', () => {
|
|
|
63
63
|
|
|
64
64
|
describe('invert', () => {
|
|
65
65
|
it('inverts identity to identity', () => {
|
|
66
|
-
const m =
|
|
67
|
-
const out =
|
|
66
|
+
const m = mat3.identity
|
|
67
|
+
const out = mat3()
|
|
68
68
|
m.invert(out)
|
|
69
69
|
closeTo(out[0], 1)
|
|
70
70
|
closeTo(out[4], 1)
|
|
71
71
|
closeTo(out[8], 1)
|
|
72
72
|
})
|
|
73
73
|
it('m * m^-1 = identity', () => {
|
|
74
|
-
const m =
|
|
75
|
-
const inv =
|
|
74
|
+
const m = mat3(1, 0, 0, 0, 2, 0, 0, 0, 3)
|
|
75
|
+
const inv = mat3()
|
|
76
76
|
m.invert(inv)
|
|
77
|
-
const result =
|
|
77
|
+
const result = mat3()
|
|
78
78
|
m.multiply(inv!, result)
|
|
79
79
|
closeTo(result[0], 1)
|
|
80
80
|
closeTo(result[4], 1)
|
|
@@ -82,15 +82,15 @@ describe('Mat3', () => {
|
|
|
82
82
|
closeTo(result[1], 0)
|
|
83
83
|
})
|
|
84
84
|
it('returns null for singular matrix', () => {
|
|
85
|
-
const m =
|
|
86
|
-
assert.strictEqual(m.invert(
|
|
85
|
+
const m = mat3(1, 2, 3, 4, 5, 6, 7, 8, 9)
|
|
86
|
+
assert.strictEqual(m.invert(mat3()), null)
|
|
87
87
|
})
|
|
88
88
|
})
|
|
89
89
|
|
|
90
90
|
describe('adjoint', () => {
|
|
91
91
|
it('adjoint of identity is identity', () => {
|
|
92
|
-
const out =
|
|
93
|
-
|
|
92
|
+
const out = mat3()
|
|
93
|
+
mat3.identity.adjoint(out)
|
|
94
94
|
assert.strictEqual(out[0], 1)
|
|
95
95
|
assert.strictEqual(out[4], 1)
|
|
96
96
|
assert.strictEqual(out[8], 1)
|
|
@@ -99,31 +99,31 @@ describe('Mat3', () => {
|
|
|
99
99
|
|
|
100
100
|
describe('determinant', () => {
|
|
101
101
|
it('identity determinant is 1', () => {
|
|
102
|
-
assert.strictEqual(
|
|
102
|
+
assert.strictEqual(mat3.identity.determinant(), 1)
|
|
103
103
|
})
|
|
104
104
|
it('diagonal matrix determinant is product of diagonal', () => {
|
|
105
|
-
const m =
|
|
105
|
+
const m = mat3(2, 0, 0, 0, 3, 0, 0, 0, 4)
|
|
106
106
|
assert.strictEqual(m.determinant(), 24)
|
|
107
107
|
})
|
|
108
108
|
it('singular matrix determinant is 0', () => {
|
|
109
|
-
const m =
|
|
109
|
+
const m = mat3(1, 2, 3, 4, 5, 6, 7, 8, 9)
|
|
110
110
|
closeTo(m.determinant(), 0)
|
|
111
111
|
})
|
|
112
112
|
})
|
|
113
113
|
|
|
114
114
|
describe('multiply', () => {
|
|
115
115
|
it('identity * A = A', () => {
|
|
116
|
-
const a =
|
|
117
|
-
const out =
|
|
118
|
-
|
|
116
|
+
const a = mat3(1, 2, 3, 4, 5, 6, 7, 8, 9)
|
|
117
|
+
const out = mat3()
|
|
118
|
+
mat3.identity.multiply(a, out)
|
|
119
119
|
for (let i = 0; i < 9; i++) closeTo(out[i], a[i])
|
|
120
120
|
})
|
|
121
121
|
})
|
|
122
122
|
|
|
123
123
|
describe('translate', () => {
|
|
124
124
|
it('translates identity', () => {
|
|
125
|
-
const m =
|
|
126
|
-
const r = m.translate(
|
|
125
|
+
const m = mat3.identity
|
|
126
|
+
const r = m.translate(vec2(5, 10))
|
|
127
127
|
assert.strictEqual(r[6], 5)
|
|
128
128
|
assert.strictEqual(r[7], 10)
|
|
129
129
|
assert.strictEqual(r[8], 1)
|
|
@@ -132,7 +132,7 @@ describe('Mat3', () => {
|
|
|
132
132
|
|
|
133
133
|
describe('rotate', () => {
|
|
134
134
|
it('rotates identity by PI/2', () => {
|
|
135
|
-
const m =
|
|
135
|
+
const m = mat3.identity
|
|
136
136
|
const r = m.rotate(Math.PI / 2)
|
|
137
137
|
closeTo(r[0], 0)
|
|
138
138
|
closeTo(r[1], 1)
|
|
@@ -143,8 +143,8 @@ describe('Mat3', () => {
|
|
|
143
143
|
|
|
144
144
|
describe('scale', () => {
|
|
145
145
|
it('scales identity', () => {
|
|
146
|
-
const m =
|
|
147
|
-
const r = m.scale(
|
|
146
|
+
const m = mat3.identity
|
|
147
|
+
const r = m.scale(vec2(2, 3))
|
|
148
148
|
assert.strictEqual(r[0], 2)
|
|
149
149
|
assert.strictEqual(r[4], 3)
|
|
150
150
|
assert.strictEqual(r[8], 1)
|
|
@@ -153,7 +153,7 @@ describe('Mat3', () => {
|
|
|
153
153
|
|
|
154
154
|
describe('static fromTranslation', () => {
|
|
155
155
|
it('creates translation matrix', () => {
|
|
156
|
-
const m =
|
|
156
|
+
const m = mat3.fromTranslation(vec2(5, 10))
|
|
157
157
|
assert.strictEqual(m[0], 1)
|
|
158
158
|
assert.strictEqual(m[4], 1)
|
|
159
159
|
assert.strictEqual(m[6], 5)
|
|
@@ -164,7 +164,7 @@ describe('Mat3', () => {
|
|
|
164
164
|
|
|
165
165
|
describe('static fromRotation', () => {
|
|
166
166
|
it('creates rotation matrix', () => {
|
|
167
|
-
const m =
|
|
167
|
+
const m = mat3.fromRotation(Math.PI / 2)
|
|
168
168
|
closeTo(m[0], 0)
|
|
169
169
|
closeTo(m[1], 1)
|
|
170
170
|
closeTo(m[3], -1)
|
|
@@ -174,7 +174,7 @@ describe('Mat3', () => {
|
|
|
174
174
|
|
|
175
175
|
describe('static fromScaling', () => {
|
|
176
176
|
it('creates scaling matrix', () => {
|
|
177
|
-
const m =
|
|
177
|
+
const m = mat3.fromScaling(vec2(2, 3))
|
|
178
178
|
assert.strictEqual(m[0], 2)
|
|
179
179
|
assert.strictEqual(m[4], 3)
|
|
180
180
|
assert.strictEqual(m[8], 1)
|
|
@@ -184,7 +184,7 @@ describe('Mat3', () => {
|
|
|
184
184
|
describe('static fromMat2x3', () => {
|
|
185
185
|
it('converts from Mat2x3', () => {
|
|
186
186
|
const a = new Mat2x3(1, 0, 0, 1, 5, 10)
|
|
187
|
-
const m =
|
|
187
|
+
const m = mat3.fromMat2x3(a)
|
|
188
188
|
assert.strictEqual(m[0], 1)
|
|
189
189
|
assert.strictEqual(m[4], 1)
|
|
190
190
|
assert.strictEqual(m[6], 5)
|
|
@@ -195,8 +195,8 @@ describe('Mat3', () => {
|
|
|
195
195
|
|
|
196
196
|
describe('static fromQuat', () => {
|
|
197
197
|
it('identity quaternion gives identity matrix', () => {
|
|
198
|
-
const q =
|
|
199
|
-
const m =
|
|
198
|
+
const q = quat.identity
|
|
199
|
+
const m = mat3.fromQuat(q)
|
|
200
200
|
closeTo(m[0], 1)
|
|
201
201
|
closeTo(m[4], 1)
|
|
202
202
|
closeTo(m[8], 1)
|
|
@@ -206,7 +206,7 @@ describe('Mat3', () => {
|
|
|
206
206
|
|
|
207
207
|
describe('static projection', () => {
|
|
208
208
|
it('creates 2D projection', () => {
|
|
209
|
-
const m =
|
|
209
|
+
const m = mat3.projection(100, 200)
|
|
210
210
|
closeTo(m[0], 2 / 100)
|
|
211
211
|
closeTo(m[4], -2 / 200)
|
|
212
212
|
})
|
|
@@ -214,17 +214,17 @@ describe('Mat3', () => {
|
|
|
214
214
|
|
|
215
215
|
describe('plus / minus', () => {
|
|
216
216
|
it('adds', () => {
|
|
217
|
-
const a =
|
|
218
|
-
const b =
|
|
219
|
-
const out =
|
|
217
|
+
const a = mat3.identity
|
|
218
|
+
const b = mat3.identity
|
|
219
|
+
const out = mat3()
|
|
220
220
|
a.plus(b, out)
|
|
221
221
|
assert.strictEqual(out[0], 2)
|
|
222
222
|
assert.strictEqual(out[4], 2)
|
|
223
223
|
})
|
|
224
224
|
it('subtracts', () => {
|
|
225
|
-
const a =
|
|
226
|
-
const b =
|
|
227
|
-
const out =
|
|
225
|
+
const a = mat3.identity
|
|
226
|
+
const b = mat3.identity
|
|
227
|
+
const out = mat3()
|
|
228
228
|
a.minus(b, out)
|
|
229
229
|
assert.strictEqual(out[0], 0)
|
|
230
230
|
})
|
|
@@ -232,7 +232,7 @@ describe('Mat3', () => {
|
|
|
232
232
|
|
|
233
233
|
describe('scaleScalar', () => {
|
|
234
234
|
it('scales all elements', () => {
|
|
235
|
-
const m =
|
|
235
|
+
const m = mat3.identity
|
|
236
236
|
const r = m.scaleScalar(3)
|
|
237
237
|
assert.strictEqual(r[0], 3)
|
|
238
238
|
assert.strictEqual(r[4], 3)
|
|
@@ -242,17 +242,17 @@ describe('Mat3', () => {
|
|
|
242
242
|
|
|
243
243
|
describe('frob', () => {
|
|
244
244
|
it('frobenius norm of identity', () => {
|
|
245
|
-
closeTo(
|
|
245
|
+
closeTo(mat3.identity.frob(), Math.sqrt(3))
|
|
246
246
|
})
|
|
247
247
|
})
|
|
248
248
|
|
|
249
249
|
describe('equals / exactEquals', () => {
|
|
250
250
|
it('exactEquals', () => {
|
|
251
|
-
assert.strictEqual(
|
|
251
|
+
assert.strictEqual(mat3.identity.exactEquals(mat3.identity), true)
|
|
252
252
|
})
|
|
253
253
|
it('equals with epsilon', () => {
|
|
254
|
-
const a =
|
|
255
|
-
const b =
|
|
254
|
+
const a = mat3.identity
|
|
255
|
+
const b = mat3.identity
|
|
256
256
|
b[0] = 1 + glmaths.EPSILON * 0.1
|
|
257
257
|
assert.strictEqual(a.equals(b), true)
|
|
258
258
|
})
|
|
@@ -260,7 +260,7 @@ describe('Mat3', () => {
|
|
|
260
260
|
|
|
261
261
|
describe('toString', () => {
|
|
262
262
|
it('returns string', () => {
|
|
263
|
-
assert.ok(
|
|
263
|
+
assert.ok(mat3.identity.toString().includes('mat3'))
|
|
264
264
|
})
|
|
265
265
|
})
|
|
266
266
|
|
|
@@ -273,10 +273,10 @@ describe('Mat3', () => {
|
|
|
273
273
|
|
|
274
274
|
describe('mat * vec operators', () => {
|
|
275
275
|
it('Mat3 * Vec3 from quat rotation', () => {
|
|
276
|
-
const q =
|
|
277
|
-
const m =
|
|
278
|
-
const v =
|
|
279
|
-
const expected = v.transformMat3(m,
|
|
276
|
+
const q = quat.fromAxisAngle(vec3(1, 1, 0).normalize(), Math.PI / 3)
|
|
277
|
+
const m = mat3.fromQuat(q)
|
|
278
|
+
const v = vec3(2, 3, 4)
|
|
279
|
+
const expected = v.transformMat3(m, vec3())
|
|
280
280
|
const r = m * v
|
|
281
281
|
assert.ok(r instanceof Vec3)
|
|
282
282
|
closeTo(r.x, expected.x)
|
|
@@ -284,19 +284,19 @@ describe('Mat3', () => {
|
|
|
284
284
|
closeTo(r.z, expected.z)
|
|
285
285
|
})
|
|
286
286
|
it('Mat3 * Vec3 scale + rotation', () => {
|
|
287
|
-
const m =
|
|
288
|
-
m.scale(
|
|
289
|
-
const v =
|
|
290
|
-
const expected = v.transformMat3(m,
|
|
287
|
+
const m = mat3.fromRotation(Math.PI / 3)
|
|
288
|
+
m.scale(vec2(2, 3))
|
|
289
|
+
const v = vec3(1, 1, 1)
|
|
290
|
+
const expected = v.transformMat3(m, vec3())
|
|
291
291
|
const r = m * v
|
|
292
292
|
closeTo(r.x, expected.x)
|
|
293
293
|
closeTo(r.y, expected.y)
|
|
294
294
|
closeTo(r.z, expected.z)
|
|
295
295
|
})
|
|
296
296
|
it('Mat3 * Vec2 rotation via mat3', () => {
|
|
297
|
-
const m =
|
|
298
|
-
const v =
|
|
299
|
-
const expected = v.transformMat3(m,
|
|
297
|
+
const m = mat3.fromRotation(Math.PI / 4)
|
|
298
|
+
const v = vec2(5, 7)
|
|
299
|
+
const expected = v.transformMat3(m, vec2())
|
|
300
300
|
const r = m * v
|
|
301
301
|
assert.ok(r instanceof Vec2)
|
|
302
302
|
closeTo(r.x, expected.x)
|