-
Notifications
You must be signed in to change notification settings - Fork 2
/
avec4.js
215 lines (199 loc) · 5.24 KB
/
avec4.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
/** @module avec4 */
import * as vec4 from "./vec4.js";
const TEMP_VEC4 = vec4.create();
/**
* Sets a vector components.
* @param {import("./types.js").avec4} a
* @param {number} i
* @param {number} x
* @param {number} y
* @param {number} z
* @param {number} w
*/
export function set4(a, i, x, y, z, w) {
a[i * 4] = x;
a[i * 4 + 1] = y;
a[i * 4 + 2] = z;
a[i * 4 + 3] = w;
}
/**
* Sets a vector to another vector.
* @param {import("./types.js").avec4} a
* @param {number} i
* @param {import("./types.js").avec4} b
* @param {number} j
*/
export function set(a, i, b, j) {
a[i * 4] = b[j * 4];
a[i * 4 + 1] = b[j * 4 + 1];
a[i * 4 + 2] = b[j * 4 + 2];
a[i * 4 + 3] = b[j * 4 + 3];
}
/**
* Compares two vectors.
* @param {import("./types.js").avec4} a
* @param {number} i
* @param {import("./types.js").avec4} b
* @param {number} j
* @returns {boolean}
*/
export function equals(a, i, b, j) {
return (
a[i * 4] === b[j * 4] &&
a[i * 4 + 1] === b[j * 4 + 1] &&
a[i * 4 + 2] === b[j * 4 + 2] &&
a[i * 4 + 3] === b[j * 4 + 3]
);
}
/**
* Adds a vector to another.
* @param {import("./types.js").avec4} a
* @param {number} i
* @param {import("./types.js").avec4} b
* @param {number} j
*/
export function add(a, i, b, j) {
a[i * 4] += b[j * 4];
a[i * 4 + 1] += b[j * 4 + 1];
a[i * 4 + 2] += b[j * 4 + 2];
a[i * 4 + 3] += b[j * 4 + 3];
}
/**
* Subtracts a vector from another.
* @param {import("./types.js").avec4} a
* @param {number} i
* @param {import("./types.js").avec4} b
* @param {number} j
*/
export function sub(a, i, b, j) {
a[i * 4] -= b[j * 4];
a[i * 4 + 1] -= b[j * 4 + 1];
a[i * 4 + 2] -= b[j * 4 + 2];
a[i * 4 + 3] -= b[j * 4 + 3];
}
/**
* Scales a vector by a number.
* @param {import("./types.js").avec4} a
* @param {number} i
* @param {number} s
*/
export function scale(a, i, s) {
a[i * 4] *= s;
a[i * 4 + 1] *= s;
a[i * 4 + 2] *= s;
a[i * 4 + 3] *= s;
}
/**
* Adds two vectors after scaling the second one.
* @param {import("./types.js").avec4} a
* @param {number} i
* @param {import("./types.js").avec4} b
* @param {number} j
* @param {number} s
*/
export function addScaled(a, i, b, j, s) {
a[i * 4] += b[j * 4] * s;
a[i * 4 + 1] += b[j * 4 + 1] * s;
a[i * 4 + 2] += b[j * 4 + 2] * s;
a[i * 4 + 3] += b[j * 4 + 3] * s;
}
/**
* Multiplies a vector with a matrix.
* @param {import("./types.js").avec4} a
* @param {number} i
* @param {import("./types.js").amat4} m
* @param {number} j
*/
export function multMat4(a, i, m, j) {
const x = a[i * 4];
const y = a[i * 4 + 1];
const z = a[i * 4 + 2];
const w = a[i * 4 + 3];
a[i * 4] =
m[j * 16] * x + m[j * 16 + 4] * y + m[j * 16 + 8] * z + m[j * 16 + 12] * w;
a[i * 4 + 1] =
m[j * 16 + 1] * x +
m[j * 16 + 5] * y +
m[j * 16 + 9] * z +
m[j * 16 + 13] * w;
a[i * 4 + 2] =
m[j * 16 + 2] * x +
m[j * 16 + 6] * y +
m[j * 16 + 10] * z +
m[j * 16 + 14] * w;
a[i * 4 + 3] =
m[j * 16 + 3] * x +
m[j * 16 + 7] * y +
m[j * 16 + 11] * z +
m[j * 16 + 15] * w;
}
/**
* Linearly interpolates between two vectors.
* @param {import("./types.js").avec4} a
* @param {number} i
* @param {import("./types.js").avec4} b
* @param {number} j
* @param {number} t
*/
export function lerp(a, i, b, j, t) {
const x = a[i * 4];
const y = a[i * 4 + 1];
const z = a[i * 4 + 2];
const w = a[i * 4 + 3];
a[i * 4] = x + (b[j * 4] - x) * t;
a[i * 4 + 1] = y + (b[j * 4 + 1] - y) * t;
a[i * 4 + 2] = z + (b[j * 4 + 2] - z) * t;
a[i * 4 + 3] = w + (b[j * 4 + 3] - w) * t;
}
/**
* Executes a function once for each array element.
* @param {import("./types.js").avec4} a
* @param {import("./types.js").iterativeCallback} callbackFn
*/
export function forEach(a, callbackFn) {
for (let i = 0; i < a.length / 4; i++) {
TEMP_VEC4[0] = a[i * 4];
TEMP_VEC4[1] = a[i * 4 + 1];
TEMP_VEC4[2] = a[i * 4 + 2];
TEMP_VEC4[3] = a[i * 4 + 3];
callbackFn(TEMP_VEC4, i, a);
a[i * 4] = TEMP_VEC4[0];
a[i * 4 + 1] = TEMP_VEC4[1];
a[i * 4 + 2] = TEMP_VEC4[2];
a[i * 4 + 3] = TEMP_VEC4[3];
}
}
/**
* Creates a new array populated with the results of calling a provided function on every element in the calling array.
* @param {import("./types.js").avec4} a
* @param {import("./types.js").iterativeCallback} callbackFn
* @returns {import("./types.js").avec4}
*/
export function map(a, callbackFn) {
const b = new a.constructor(a.length);
const element = new a.constructor(4);
for (let i = 0; i < a.length / 4; i++) {
element[0] = a[i * 4];
element[1] = a[i * 4 + 1];
element[2] = a[i * 4 + 2];
element[3] = a[i * 4 + 3];
const returnValue = callbackFn(element, i, a);
b[i * 4] = returnValue[0];
b[i * 4 + 1] = returnValue[1];
b[i * 4 + 2] = returnValue[2];
b[i * 4 + 3] = returnValue[3];
}
return b;
}
/**
* Prints a vector to a string.
* @param {import("./types.js").avec4} 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 * 4] * scale) / scale}, ${Math.floor(a[i * 4 + 1] * scale) / scale}, ${Math.floor(a[i * 4 + 2] * scale) / scale}, ${Math.floor(a[i * 4 + 3] * scale) / scale}]`;
}