-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvector.js
More file actions
368 lines (304 loc) · 6.84 KB
/
vector.js
File metadata and controls
368 lines (304 loc) · 6.84 KB
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import { acos, isArray, multQuatVec, normRad, multiplyVecMat3, isNumber } from './utils/math';
import {
cachedFunction,
cachedGetter,
cachedMethod,
cachedValueOf,
defineVectorLength,
operatorCalc
} from './operator';
import { IPoint } from './point';
import { convertToCSSVars } from './utils/css';
const X = 0;
const Y = 1;
const Z = 2;
const AXES = Symbol('axes');
function square(val) {
return val * val;
}
class AVector {
constructor(...args) {
if (typeof args[0] === 'function') {
operatorCalc(args[0], (nx, ny, nz) => {
this[AXES] = [nx, ny, nz];
});
} else if (isArray(args[0])) {
this[AXES] = [...args[0]];
} else if (args[0] && isNumber(args[0].x)) {
this[AXES] = [args[0].x || 0, args[0].y || 0, args[0].z || 0];
} else {
this[AXES] = [args[0] || 0, args[1] || 0, args[2] || 0];
}
}
valueOf() {
throw new Error('valueOf() not implemented, looks like you try to calculate outside of calc');
}
/**
* @returns {this}
*/
normalize() {
const { length } = this;
return new this.constructor(this.x / length, this.y / length, this.z / length);
}
/**
* @returns {this}
*/
norm() {
return this.normalize();
}
// methods ispired by
// https://evanw.github.io/lightgl.js/docs/vector.html
dot(v) {
return this.x * v.x + this.y * v.y + this.z * v.z;
}
/**
* @param {AVector} v
* @returns {this}
*/
cross(v) {
return new this.constructor(this.y * v.z - this.z * v.y, this.z * v.x - this.x * v.z, this.x * v.y - this.y * v.x);
}
/**
* @param {AVector} v
* @returns {this}
*/
crossNormalize(v) {
const vec = this.cross(v);
const { length } = vec;
vec[AXES][X] /= length;
vec[AXES][Y] /= length;
vec[AXES][Z] /= length;
return vec;
}
/**
* @param {AVector} v
* @returns {this}
*/
cn(v) {
return this.crossNormalize(v);
}
toAngles() {
return {
theta: Math.atan2(this.z, this.x),
phi: Math.asin(this.y / this.length)
};
}
/**
* @param {AVector} v
*/
angleTo(v) {
return normRad(acos(this.dot(v) / (this.length * v.length)));
}
multiply(quat) {
if (quat.x === undefined) {
return this.multiplyMat3(quat);
}
if (quat.w === undefined) {
return this.multiplyVec3(quat);
}
return multQuatVec(quat, this);
}
multiplyMat3(other) {
return multiplyVecMat3(this, other);
}
multiplyVec3({ x, y, z }) {
return new this.constructor(this.x * x, this.y * y, this.z * z);
}
/**
* @param {AVector} v
*/
distance(v) {
return Math.sqrt(square(this.x - v.x) + square(this.y - v.y) + square(this.z - v.z));
}
/**
* @param {AVector} v
*/
dist(v) {
return this.distance(v);
}
/**
* @returns {[number, number, number]}
*/
toArray() {
return [this.x, this.y, this.z];
}
swizzle(target) {
const data = target.split('').map(t => this[t]);
if (data.length === 2) {
return new IPoint(data[0], data[1]);
}
return new this.constructor(data[0], data[1], data[2]);
}
// eslint-disable-next-line no-unused-vars
calc(arg) {
throw new Error('calc() not implemented');
}
clone() {
throw new Error('clone() not implemented');
}
equals(v) {
return this.x === v.x && this.y === v.y && this.z === v.z;
}
toJSON() {
return { x: this.x, y: this.y, z: this.z };
}
toString() {
return JSON.stringify(this.toJSON());
}
toCSSVars(name, target = {}) {
return convertToCSSVars(name, this.toJSON(), target);
}
get lengthSq() {
return this.dot(this);
}
set lengthSq(_) {
throw new Error('set lengthSq() not implemented');
}
get length() {
return Math.sqrt(this.lengthSq);
}
set length(_) {
throw new Error('set length() not implemented');
}
get lensq() {
return this.lengthSq;
}
set lensq(_) {
throw new Error('set lensq() not implemented');
}
get len() {
return this.length;
}
set len(_) {
throw new Error('set len() not implemented');
}
get x() {
return this[AXES][X];
}
set x(_) {
throw new Error('set x() not implemented');
}
get y() {
return this[AXES][Y];
}
set y(_) {
throw new Error('set y() not implemented');
}
get z() {
return this[AXES][Z];
}
set z(_) {
throw new Error('set z() not implemented');
}
get xy() {
return new IPoint(this[AXES][X], this[AXES][Y]);
}
set xy(_) {
throw new Error('set xz() not implemented');
}
get xz() {
return new IPoint(this[AXES][X], this[AXES][Z]);
}
set xz(_) {
throw new Error('set xz() not implemented');
}
get yz() {
return new IPoint(this[AXES][Y], this[AXES][Z]);
}
set yz(_) {
throw new Error('set yz() not implemented');
}
[Symbol.iterator]() {
return this[AXES].values();
}
}
cachedValueOf(AVector);
defineVectorLength(AVector, 3);
cachedMethod(AVector, 'dot');
cachedMethod(AVector, 'cross');
cachedMethod(AVector, 'crossNormalize');
cachedMethod(AVector, 'toAngles');
cachedMethod(AVector, 'angleTo');
cachedMethod(AVector, 'rotate');
cachedMethod(AVector, 'distance');
cachedMethod(AVector, 'toArray');
cachedGetter(AVector, 'length');
cachedGetter(AVector, 'lengthSq');
export class Vector extends AVector {
set x(x) {
this[AXES][X] = x;
}
set y(y) {
this[AXES][Y] = y;
}
set z(z) {
this[AXES][Z] = z;
}
get x() {
return this[AXES][X];
}
get y() {
return this[AXES][Y];
}
get z() {
return this[AXES][Z];
}
/**
* @param {() => number} alg
* @returns {Vector & number}
*/
calc(alg) {
return operatorCalc(alg, this);
}
/**
* @returns {Vector & number}
*/
clone() {
return new Vector(this.x, this.y, this.z);
}
}
export class Victor extends AVector {
/**
* @returns {Vector & number}
*/
toVector() {
return new Vector(this.x, this.y, this.z);
}
/**
* @returns {Vector & number}
*/
clone() {
return this;
}
}
/**
* @param {() => number} alg
* @returns {Vector & number}
*/
export function calc(alg) {
return operatorCalc(alg);
}
const vectorFactory = cachedFunction((x, y, z) => new Vector(x, y, z));
/**
* @param {number | () => number} x
* @param {number} [y]
* @param {number} [z]
* @returns {Vector & number}
*/
export const vector = (x, y, z) => vectorFactory(x, y, z);
const victorFactory = cachedFunction((x, y, z) => new Victor(x, y, z));
/**
* @param {number | () => number} x
* @param {number} [y]
* @param {number} [z]
* @returns {Victor & number}
*/
export const victor = (x, y, z) => victorFactory(x, y, z);
export const ZERO = victor(0, 0, 0);
export const FORWARD = victor(0, 0, -1);
export const BACK = victor(0, 0, 1);
export const LEFT = victor(-1, 0, 0);
export const RIGHT = victor(1, 0, 0);
export const UP = victor(0, 1, 0);
export const BOTTOM = victor(0, -1, 0);
export const ONE = victor(1, 1, 1);