-
Notifications
You must be signed in to change notification settings - Fork 2
/
avec3.js
349 lines (317 loc) · 8.23 KB
/
avec3.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/** @module avec3 */
import * as vec3 from "./vec3.js";
const TEMP_VEC3 = vec3.create();
/**
* Sets a vector components.
* @param {import("./types.js").avec3} a
* @param {number} i
* @param {number} x
* @param {number} y
* @param {number} z
*/
export function set3(a, i, x, y, z) {
a[i * 3] = x;
a[i * 3 + 1] = y;
a[i * 3 + 2] = z;
}
/**
* Sets a vector to another vector.
* @param {import("./types.js").avec3} a
* @param {number} i
* @param {import("./types.js").avec3} b
* @param {number} j
*/
export function set(a, i, b, j) {
a[i * 3] = b[j * 3];
a[i * 3 + 1] = b[j * 3 + 1];
a[i * 3 + 2] = b[j * 3 + 2];
}
/**
* Compares two vectors.
* @param {import("./types.js").avec3} a
* @param {number} i
* @param {import("./types.js").avec3} b
* @param {number} j
* @returns {boolean}
*/
export function equals(a, i, b, j) {
return (
a[i * 3] === b[j * 3] &&
a[i * 3 + 1] === b[j * 3 + 1] &&
a[i * 3 + 2] === b[j * 3 + 2]
);
}
/**
* Adds a vector to another.
* @param {import("./types.js").avec3} a
* @param {number} i
* @param {import("./types.js").avec3} b
* @param {number} j
*/
export function add(a, i, b, j) {
a[i * 3] += b[j * 3];
a[i * 3 + 1] += b[j * 3 + 1];
a[i * 3 + 2] += b[j * 3 + 2];
}
/**
* Subtracts a vector from another.
* @param {import("./types.js").avec3} a
* @param {number} i
* @param {import("./types.js").avec3} b
* @param {number} j
*/
export function sub(a, i, b, j) {
a[i * 3] -= b[j * 3];
a[i * 3 + 1] -= b[j * 3 + 1];
a[i * 3 + 2] -= b[j * 3 + 2];
}
/**
* Scales a vector by a number.
* @param {import("./types.js").avec3} a
* @param {number} i
* @param {number} s
*/
export function scale(a, i, s) {
a[i * 3] *= s;
a[i * 3 + 1] *= s;
a[i * 3 + 2] *= s;
}
/**
* Adds two vectors after scaling the second one.
* @param {import("./types.js").avec3} a
* @param {number} i
* @param {import("./types.js").avec3} b
* @param {number} j
* @param {number} s
*/
export function addScaled(a, i, b, j, s) {
a[i * 3] += b[j * 3] * s;
a[i * 3 + 1] += b[j * 3 + 1] * s;
a[i * 3 + 2] += b[j * 3 + 2] * s;
}
/**
* Multiplies a vector by a matrix.
* @param {import("./types.js").avec3} a
* @param {number} i
* @param {import("./types.js").amat4} m
* @param {number} j
*/
export function multMat4(a, i, m, j) {
const x = a[i * 3];
const y = a[i * 3 + 1];
const z = a[i * 3 + 2];
a[i * 3] =
m[j * 16] * x + m[j * 16 + 4] * y + m[j * 16 + 8] * z + m[j * 16 + 12];
a[i * 3 + 1] =
m[j * 16 + 1] * x + m[j * 16 + 5] * y + m[j * 16 + 9] * z + m[j * 16 + 13];
a[i * 3 + 2] =
m[j * 16 + 2] * x + m[j * 16 + 6] * y + m[j * 16 + 10] * z + m[j * 16 + 14];
}
/**
* Multiplies a vector by a quaternion.
* @param {import("./types.js").avec3} a
* @param {number} i
* @param {import("./types.js").aquat} q
* @param {number} j
*/
export function multQuat(a, i, q, j) {
const x = a[i * 3];
const y = a[i * 3 + 1];
const z = a[i * 3 + 2];
const qx = q[j * 4];
const qy = q[j * 4 + 1];
const qz = q[j * 4 + 2];
const qw = q[j * 4 + 3];
const ix = qw * x + qy * z - qz * y;
const iy = qw * y + qz * x - qx * z;
const iz = qw * z + qx * y - qy * x;
const iw = -qx * x - qy * y - qz * z;
a[i * 3] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
a[i * 3 + 1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
a[i * 3 + 2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
}
/**
* Calculates the dot product of two vectors.
* @param {import("./types.js").avec3} a
* @param {number} i
* @param {import("./types.js").avec3} b
* @param {number} j
* @returns {number}
*/
export function dot(a, i, b, j) {
return (
a[i * 3] * b[j * 3] +
a[i * 3 + 1] * b[j * 3 + 1] +
a[i * 3 + 2] * b[j * 3 + 2]
);
}
/**
* Calculates the cross product of two vectors.
* @param {import("./types.js").avec3} a
* @param {number} i
* @param {import("./types.js").avec3} b
* @param {number} j
*/
export function cross(a, i, b, j) {
const x = a[i * 3];
const y = a[i * 3 + 1];
const z = a[i * 3 + 2];
const vx = b[j * 3];
const vy = b[j * 3 + 1];
const vz = b[j * 3 + 2];
a[i * 3] = y * vz - vy * z;
a[i * 3 + 1] = z * vx - vz * x;
a[i * 3 + 2] = x * vy - vx * y;
}
/**
* Calculates the length of a vector.
* @param {import("./types.js").avec3} a
* @param {number} i
* @returns {number}
*/
export function length(a, i) {
const x = a[i * 3];
const y = a[i * 3 + 1];
const z = a[i * 3 + 2];
return Math.sqrt(x * x + y * y + z * z);
}
/**
* Calculates the squared length of a vector.
* @param {import("./types.js").avec3} a
* @param {number} i
* @returns {number}
*/
export function lengthSq(a, i) {
const x = a[i * 3];
const y = a[i * 3 + 1];
const z = a[i * 3 + 2];
return x * x + y * y + z * z;
}
/**
* Normalises a vector.
* @param {import("./types.js").avec3} a
* @param {number} i
*/
export function normalize(a, i) {
const lenSq =
a[i * 3] * a[i * 3] +
a[i * 3 + 1] * a[i * 3 + 1] +
a[i * 3 + 2] * a[i * 3 + 2];
if (lenSq > 0) {
const len = Math.sqrt(lenSq);
a[i * 3] /= len;
a[i * 3 + 1] /= len;
a[i * 3 + 2] /= len;
}
}
/**
* Calculates the distance between two vectors.
* @param {import("./types.js").avec3} a
* @param {number} i
* @param {import("./types.js").avec3} b
* @param {number} j
* @returns {number}
*/
export function distance(a, i, b, j) {
const dx = b[j * 3] - a[i * 3];
const dy = b[j * 3 + 1] - a[i * 3 + 1];
const dz = b[j * 3 + 2] - a[i * 3 + 2];
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
/**
* Calculates the squared distance between two vectors.
* @param {import("./types.js").avec3} a
* @param {number} i
* @param {import("./types.js").avec3} b
* @param {number} j
* @returns {number}
*/
export function distanceSq(a, i, b, j) {
const dx = b[j * 3] - a[i * 3];
const dy = b[j * 3 + 1] - a[i * 3 + 1];
const dz = b[j * 3 + 2] - a[i * 3 + 2];
return dx * dx + dy * dy + dz * dz;
}
/**
* Limits a vector to a length.
* @param {import("./types.js").avec3} a
* @param {number} i
* @param {number} len
*/
export function limit(a, i, len) {
const x = a[i * 3];
const y = a[i * 3 + 1];
const z = a[i * 3 + 2];
const dsq = x * x + y * y + z * z;
const lsq = len * len;
if (lsq > 0 && dsq > lsq) {
const nd = len / Math.sqrt(dsq);
a[i * 3] *= nd;
a[i * 3 + 1] *= nd;
a[i * 3 + 2] *= nd;
}
}
/**
* Linearly interpolates between two vectors.
* @param {import("./types.js").avec3} a
* @param {number} i
* @param {import("./types.js").avec3} b
* @param {number} j
* @param {number} t
*/
export function lerp(a, i, b, j, t) {
const x = a[i * 3];
const y = a[i * 3 + 1];
const z = a[i * 3 + 2];
a[i * 3] = x + (b[j * 3] - x) * t;
a[i * 3 + 1] = y + (b[j * 3 + 1] - y) * t;
a[i * 3 + 2] = z + (b[j * 3 + 2] - z) * t;
}
/**
* Executes a function once for each array element.
* @param {import("./types.js").avec3} a
* @param {import("./types.js").iterativeCallback} callbackFn
*/
export function forEach(a, callbackFn) {
for (let i = 0; i < a.length / 3; i++) {
TEMP_VEC3[0] = a[i * 3];
TEMP_VEC3[1] = a[i * 3 + 1];
TEMP_VEC3[2] = a[i * 3 + 2];
callbackFn(TEMP_VEC3, i, a);
a[i * 3] = TEMP_VEC3[0];
a[i * 3 + 1] = TEMP_VEC3[1];
a[i * 3 + 2] = TEMP_VEC3[2];
}
}
/**
* Creates a new array populated with the results of calling a provided function on every element in the calling array.
* @param {import("./types.js").avec3} a
* @param {import("./types.js").iterativeCallback} callbackFn
* @returns {import("./types.js").avec3}
*/
export function map(a, callbackFn) {
const b = new a.constructor(a.length);
const element = new a.constructor(3);
for (let i = 0; i < a.length / 3; i++) {
element[0] = a[i * 3];
element[1] = a[i * 3 + 1];
element[2] = a[i * 3 + 2];
const returnValue = callbackFn(element, i, a);
b[i * 3] = returnValue[0];
b[i * 3 + 1] = returnValue[1];
b[i * 3 + 2] = returnValue[2];
}
return b;
}
/**
* Prints a vector to a string.
* @param {import("./types.js").avec3} a
* @param {number} i
* @param {number} [precision=4]
* @returns {string}
*/
export function toString(a, i, precision = 4) {
const scale = 10 ** precision;
// prettier-ignore
return `[${Math.floor(a[i * 3] * scale) / scale}, ${Math.floor(a[i * 3 + 1] * scale) / scale}, ${Math.floor(a[i * 3 + 2] * scale) / scale}]`;
}