-
Notifications
You must be signed in to change notification settings - Fork 3
/
Vector.js
380 lines (337 loc) · 10.4 KB
/
Vector.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Owner: [email protected]
* @license MPL 2.0
* @copyright Famous Industries, Inc. 2014
*/
define(function(require, exports, module) {
/**
* Three-element floating point vector.
*
* @class Vector
* @constructor
*
* @param {number} x x element value
* @param {number} y y element value
* @param {number} z z element value
*/
function Vector(x,y,z) {
if (arguments.length === 1) this.set(x);
else {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
}
return this;
}
var _register = new Vector(0,0,0);
/**
* Add this element-wise to another Vector, element-wise.
* Note: This sets the internal result register, so other references to that vector will change.
*
* @method add
* @param {Vector} v addend
* @return {Vector} vector sum
*/
Vector.prototype.add = function add(v) {
return _setXYZ.call(_register,
this.x + v.x,
this.y + v.y,
this.z + v.z
);
};
/**
* Subtract another vector from this vector, element-wise.
* Note: This sets the internal result register, so other references to that vector will change.
*
* @method sub
* @param {Vector} v subtrahend
* @return {Vector} vector difference
*/
Vector.prototype.sub = function sub(v) {
return _setXYZ.call(_register,
this.x - v.x,
this.y - v.y,
this.z - v.z
);
};
/**
* Scale Vector by floating point r.
* Note: This sets the internal result register, so other references to that vector will change.
*
* @method mult
*
* @param {number} r scalar
* @return {Vector} vector result
*/
Vector.prototype.mult = function mult(r) {
return _setXYZ.call(_register,
r * this.x,
r * this.y,
r * this.z
);
};
/**
* Scale Vector by floating point 1/r.
* Note: This sets the internal result register, so other references to that vector will change.
*
* @method div
*
* @param {number} r scalar
* @return {Vector} vector result
*/
Vector.prototype.div = function div(r) {
return this.mult(1 / r);
};
/**
* Given another vector v, return cross product (v)x(this).
* Note: This sets the internal result register, so other references to that vector will change.
*
* @method cross
* @param {Vector} v Left Hand Vector
* @return {Vector} vector result
*/
Vector.prototype.cross = function cross(v) {
var x = this.x;
var y = this.y;
var z = this.z;
var vx = v.x;
var vy = v.y;
var vz = v.z;
return _setXYZ.call(_register,
z * vy - y * vz,
x * vz - z * vx,
y * vx - x * vy
);
};
/**
* Component-wise equality test between this and Vector v.
* @method equals
* @param {Vector} v vector to compare
* @return {boolean}
*/
Vector.prototype.equals = function equals(v) {
return (v.x === this.x && v.y === this.y && v.z === this.z);
};
/**
* Rotate clockwise around x-axis by theta radians.
* Note: This sets the internal result register, so other references to that vector will change.
* @method rotateX
* @param {number} theta radians
* @return {Vector} rotated vector
*/
Vector.prototype.rotateX = function rotateX(theta) {
var x = this.x;
var y = this.y;
var z = this.z;
var cosTheta = Math.cos(theta);
var sinTheta = Math.sin(theta);
return _setXYZ.call(_register,
x,
y * cosTheta - z * sinTheta,
y * sinTheta + z * cosTheta
);
};
/**
* Rotate clockwise around y-axis by theta radians.
* Note: This sets the internal result register, so other references to that vector will change.
* @method rotateY
* @param {number} theta radians
* @return {Vector} rotated vector
*/
Vector.prototype.rotateY = function rotateY(theta) {
var x = this.x;
var y = this.y;
var z = this.z;
var cosTheta = Math.cos(theta);
var sinTheta = Math.sin(theta);
return _setXYZ.call(_register,
z * sinTheta + x * cosTheta,
y,
z * cosTheta - x * sinTheta
);
};
/**
* Rotate clockwise around z-axis by theta radians.
* Note: This sets the internal result register, so other references to that vector will change.
* @method rotateZ
* @param {number} theta radians
* @return {Vector} rotated vector
*/
Vector.prototype.rotateZ = function rotateZ(theta) {
var x = this.x;
var y = this.y;
var z = this.z;
var cosTheta = Math.cos(theta);
var sinTheta = Math.sin(theta);
return _setXYZ.call(_register,
x * cosTheta - y * sinTheta,
x * sinTheta + y * cosTheta,
z
);
};
/**
* Return dot product of this with a second Vector
* @method dot
* @param {Vector} v second vector
* @return {number} dot product
*/
Vector.prototype.dot = function dot(v) {
return this.x * v.x + this.y * v.y + this.z * v.z;
};
/**
* Return squared length of this vector
* @method normSquared
* @return {number} squared length
*/
Vector.prototype.normSquared = function normSquared() {
return this.dot(this);
};
/**
* Return length of this vector
* @method norm
* @return {number} length
*/
Vector.prototype.norm = function norm() {
return Math.sqrt(this.normSquared());
};
/**
* Scale Vector to specified length.
* If length is less than internal tolerance, set vector to [length, 0, 0].
* Note: This sets the internal result register, so other references to that vector will change.
* @method normalize
*
* @param {number} length target length, default 1.0
* @return {Vector}
*/
Vector.prototype.normalize = function normalize(length) {
if (arguments.length === 0) length = 1;
var norm = this.norm();
if (norm > 1e-7) return _setFromVector.call(_register, this.mult(length / norm));
else return _setXYZ.call(_register, length, 0, 0);
};
/**
* Make a separate copy of the Vector.
*
* @method clone
*
* @return {Vector}
*/
Vector.prototype.clone = function clone() {
return new Vector(this);
};
/**
* True if and only if every value is 0 (or falsy)
*
* @method isZero
*
* @return {boolean}
*/
Vector.prototype.isZero = function isZero() {
return !(this.x || this.y || this.z);
};
function _setXYZ(x,y,z) {
this.x = x;
this.y = y;
this.z = z;
return this;
}
function _setFromArray(v) {
return _setXYZ.call(this,v[0],v[1],v[2] || 0);
}
function _setFromVector(v) {
return _setXYZ.call(this, v.x, v.y, v.z);
}
function _setFromNumber(x) {
return _setXYZ.call(this,x,0,0);
}
/**
* Set this Vector to the values in the provided Array or Vector.
*
* @method set
* @param {object} v array, Vector, or number
* @return {Vector} this
*/
Vector.prototype.set = function set(v) {
if (v instanceof Array) return _setFromArray.call(this, v);
if (v instanceof Vector) return _setFromVector.call(this, v);
if (typeof v === 'number') return _setFromNumber.call(this, v);
};
Vector.prototype.setXYZ = function(x,y,z) {
return _setXYZ.apply(this, arguments);
};
Vector.prototype.set1D = function(x) {
return _setFromNumber.call(this, x);
};
/**
* Put result of last internal register calculation in specified output vector.
*
* @method put
* @param {Vector} v destination vector
* @return {Vector} destination vector
*/
Vector.prototype.put = function put(v) {
if (this === _register) _setFromVector.call(v, _register);
else _setFromVector.call(v, this);
};
/**
* Set this vector to [0,0,0]
*
* @method clear
*/
Vector.prototype.clear = function clear() {
return _setXYZ.call(this,0,0,0);
};
/**
* Scale this Vector down to specified "cap" length.
* If Vector shorter than cap, or cap is Infinity, do nothing.
* Note: This sets the internal result register, so other references to that vector will change.
*
* @method cap
* @return {Vector} capped vector
*/
Vector.prototype.cap = function cap(cap) {
if (cap === Infinity) return _setFromVector.call(_register, this);
var norm = this.norm();
if (norm > cap) return _setFromVector.call(_register, this.mult(cap / norm));
else return _setFromVector.call(_register, this);
};
/**
* Return projection of this Vector onto another.
* Note: This sets the internal result register, so other references to that vector will change.
*
* @method project
* @param {Vector} n vector to project upon
* @return {Vector} projected vector
*/
Vector.prototype.project = function project(n) {
return n.mult(this.dot(n));
};
/**
* Reflect this Vector across provided vector.
* Note: This sets the internal result register, so other references to that vector will change.
*
* @method reflectAcross
* @param {Vector} n vector to reflect across
* @return {Vector} reflected vector
*/
Vector.prototype.reflectAcross = function reflectAcross(n) {
n.normalize().put(n);
return _setFromVector(_register, this.sub(this.project(n).mult(2)));
};
/**
* Convert Vector to three-element array.
*
* @method get
* @return {array<number>} three-element array
*/
Vector.prototype.get = function get() {
return [this.x, this.y, this.z];
};
Vector.prototype.get1D = function() {
return this.x;
};
module.exports = Vector;
});