This repository has been archived by the owner on Jan 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.js
299 lines (296 loc) · 6.54 KB
/
math.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
function deg2rad(deg) {
return Math.PI * deg / 180
}
class Vector {
constructor(raw) {
if (raw === undefined) {
this.v = [0, 0, 0, 0]
} else if (raw.length < 4) {
throw new Error("Vector(): the length of `raw` parameter must be no fewer than 4: ", raw)
} else {
this.v = [raw[0], raw[1], raw[2], raw[3]]
}
}
clone() {
return new Vector(this.v)
}
get() {
return new Float32Array(this.v)
}
at(i) {
return this.v[i]
}
x() {
return this.v[0]
}
y() {
return this.v[1]
}
z() {
return this.v[2]
}
w() {
return this.v[3]
}
setAt(i, val) {
this.v[i] = val
}
setX(x) {
this.v[0] = x
}
setY(y) {
this.v[1] = y
}
setZ(z) {
this.v[2] = z
}
setW(w) {
this.v[3] = w
}
normalize() {
const mag = Math.sqrt(this.x() * this.x() + this.y() * this.y() + this.z() * this.z() + this.w() * this.w())
return new Vector([
this.x() / mag,
this.y() / mag,
this.z() / mag,
this.w() / mag,
])
}
add(u) {
return new Vector([
this.x() + u.x(),
this.y() + u.y(),
this.z() + u.z(),
this.w() + u.w(),
])
}
muls(s) {
return new Vector([
this.x() * s,
this.y() * s,
this.z() * s,
this.w() * s,
])
}
mul(u) {
return new Vector([
this.x() * u.x(),
this.y() * u.y(),
this.z() * u.z(),
this.w() * u.w(),
])
}
dot(u) {
return this.x() * u.x() + this.y() * u.y() + this.z() * u.z() + this.w() * u.w()
}
}
function cross(v, u) {
return new Vector([
v.y() * u.z() - v.z() * u.y(),
v.z() * u.x() - v.x() * u.z(),
v.x() * u.y() - v.y() * u.x(),
0,
])
}
class Matrix {
constructor(raw) {
if (raw === undefined) {
this.m = [
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
]
} else if (raw.length < 16) {
throw new Error("Matrix(): the length of `raw` parameter must be no fewer than 16: ", raw)
} else {
this.m = [
raw[0], raw[1], raw[2], raw[3],
raw[4], raw[5], raw[6], raw[7],
raw[8], raw[9], raw[10], raw[11],
raw[12], raw[13], raw[14], raw[15],
]
}
}
clone() {
return new Matrix(this.m)
}
get() {
return new Float32Array(this.m)
}
at(i, j) {
return this.m[4 * j + i]
}
setAt(i, j, val) {
this.m[4 * j + i] = val
}
swapRow(oldi, newi) {
for (let j = 0; j < 4; ++j) {
const tmp = this.at(oldi, j)
this.setAt(oldi, j, this.at(newi, j))
this.setAt(newi, j, tmp)
}
}
swapCol(oldj, newj) {
for (let i = 0; i < 4; ++i) {
const tmp = this.at(i, oldj)
this.setAt(i, oldj, this.at(i, newj))
this.setAt(i, newj, tmp)
}
}
muls(s) {
const result = new Matrix()
for (let i = 0; i < 4; ++i) {
for (let j = 0; j < 4; ++j) {
result.setAt(i, j, this.at(i, j) * s)
}
}
return result
}
mulv(v) {
const result = new Vector()
for (let i = 0; i < 4; ++i) {
let val = 0
for (let j = 0; j < 4; ++j) {
val += this.at(i, j) * v.at(j)
}
result.setAt(i, val)
}
return result
}
mul(m2) {
const result = new Matrix()
for (let i = 0; i < 4; ++i) {
for (let j = 0; j < 4; ++j) {
let val = 0
for (let k = 0; k < 4; ++k) {
val += this.at(i, k) * m2.at(k, j)
}
result.setAt(i, j, val)
}
}
return result
}
transpose() {
return new Matrix([
this.at(0, 0), this.at(0, 1), this.at(0, 2), this.at(0, 3),
this.at(1, 0), this.at(1, 1), this.at(1, 2), this.at(1, 3),
this.at(2, 0), this.at(2, 1), this.at(2, 2), this.at(2, 3),
this.at(3, 0), this.at(3, 1), this.at(3, 2), this.at(3, 3),
])
}
inverse() {
const cloned = this.clone()
const result = new Matrix([
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
])
for (let i = 0; i < 4; ++i) {
// partial pivoting
let max = Math.abs(cloned.at(i, i))
let newi = i
for (let k = i + 1; k < 4; ++k) {
const mki = Math.abs(cloned.at(k, i))
if (mki > max) {
max = mki
newi = k
}
}
if (newi !== i) {
cloned.swapRow(i, newi)
result.swapRow(i, newi)
}
// divide row by pivot
const pivot = cloned.at(i, i)
for (let j = 0; j < 4; ++j) {
cloned.setAt(i, j, cloned.at(i, j) / pivot)
result.setAt(i, j, result.at(i, j) / pivot)
}
// remove
for (let j = 0; j < 4; ++j) {
if (i === j) {
continue
}
const mji = cloned.at(j, i)
for (let k = 0; k < 4; ++k) {
cloned.setAt(j, k, cloned.at(j, k) - cloned.at(i, k) * mji)
result.setAt(j, k, result.at(j, k) - result.at(i, k) * mji)
}
}
}
return result
}
static scaler(x, y, z) {
return new Matrix([
x, 0, 0, 0,
0, y, 0, 0,
0, 0, z, 0,
0, 0, 0, 1,
])
}
static rotatorX(r) {
return new Matrix([
1, 0, 0, 0,
0, Math.cos(r), -Math.sin(r), 0,
0, Math.sin(r), Math.cos(r), 0,
0, 0, 0, 1,
])
}
static rotatorY(r) {
return new Matrix([
Math.cos(r), 0, Math.sin(r), 0,
0, 1, 0, 0,
-Math.sin(r), 0, Math.cos(r), 0,
0, 0, 0, 1,
])
}
static rotatorZ(r) {
return new Matrix([
Math.cos(r), -Math.sin(r), 0, 0,
Math.sin(r), Math.cos(r), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
])
}
static translator(x, y, z) {
return new Matrix([
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
x, y, z, 1,
])
}
static view(pos, at, up) {
// vectorize
const vpos = new Vector([pos[0], pos[1], pos[2], 0])
const vat = new Vector([at[0], at[1], at[2], 0])
const vup = new Vector([up[0], up[1], up[2], 0])
// axis vectors in the view coordinate system
const z = vat.add(vpos.muls(-1)).normalize()
const x = cross(z, vup).normalize()
const y = cross(x, z).normalize()
// object translation
const dx = vpos.dot(x)
const dy = vpos.dot(y)
const dz = vpos.dot(z)
// finish
return new Matrix([
x.x(), y.x(), z.x(), 0,
x.y(), y.y(), z.y(), 0,
x.z(), y.z(), z.z(), 0,
-dx, -dy, -dz, 1,
])
}
static perse(pov, aspect, near, far) {
const divTanpov = 1.0 / Math.tan(pov)
const divDepth = 1.0 / (far - near)
return new Matrix([
divTanpov, 0.0, 0.0, 0.0,
0.0, divTanpov * aspect, 0.0, 0.0,
0.0, 0.0, far * divDepth, 1.0,
0.0, 0.0, -far * near * divDepth, 0.0,
])
}
}