Debian Codegraph @

closure-library/closure/goog/vec/vec3f.js

raw
1 // Copyright 2013 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15
16 ////////////////////////// NOTE ABOUT EDITING THIS FILE ///////////////////////
17 // //
18 // Any edits to this file must be applied to vec3d.js by running: //
19 // swap_type.sh vec3f.js > vec3d.js //
20 // //
21 ////////////////////////// NOTE ABOUT EDITING THIS FILE ///////////////////////
22
23
24 /**
25 * @fileoverview Provides functions for operating on 3 element float (32bit)
26 * vectors.
27 *
28 * The last parameter will typically be the output object and an object
29 * can be both an input and output parameter to all methods except where
30 * noted.
31 *
32 * See the README for notes about the design and structure of the API
33 * (especially related to performance).
34 *
35 */
36 goog.provide('goog.vec.vec3f');
37 goog.provide('goog.vec.vec3f.Type');
38
39 /** @suppress {extraRequire} */
40 goog.require('goog.vec');
41
42 /** @typedef {goog.vec.Float32} */ goog.vec.vec3f.Type;
43
44
45 /**
46 * Creates a vec3f with all elements initialized to zero.
47 *
48 * @return {!goog.vec.vec3f.Type} The new vec3f.
49 */
50 goog.vec.vec3f.create = function() {
51 return new Float32Array(3);
52 };
53
54
55 /**
56 * Creates a new vec3f initialized with the value from the given array.
57 *
58 * @param {!Array<number>} vec The source 3 element array.
59 * @return {!goog.vec.vec3f.Type} The new vec3f.
60 */
61 goog.vec.vec3f.createFromArray = function(vec) {
62 var newVec = goog.vec.vec3f.create();
63 goog.vec.vec3f.setFromArray(newVec, vec);
64 return newVec;
65 };
66
67
68 /**
69 * Creates a new vec3f initialized with the supplied values.
70 *
71 * @param {number} v0 The value for element at index 0.
72 * @param {number} v1 The value for element at index 1.
73 * @param {number} v2 The value for element at index 2.
74 * @return {!goog.vec.vec3f.Type} The new vector.
75 */
76 goog.vec.vec3f.createFromValues = function(v0, v1, v2) {
77 var vec = goog.vec.vec3f.create();
78 goog.vec.vec3f.setFromValues(vec, v0, v1, v2);
79 return vec;
80 };
81
82
83 /**
84 * Creates a clone of the given vec3f.
85 *
86 * @param {!goog.vec.vec3f.Type} vec The source vec3f.
87 * @return {!goog.vec.vec3f.Type} The new cloned vec3f.
88 */
89 goog.vec.vec3f.clone = function(vec) {
90 var newVec = goog.vec.vec3f.create();
91 goog.vec.vec3f.setFromVec3f(newVec, vec);
92 return newVec;
93 };
94
95
96 /**
97 * Initializes the vector with the given values.
98 *
99 * @param {!goog.vec.vec3f.Type} vec The vector to receive the values.
100 * @param {number} v0 The value for element at index 0.
101 * @param {number} v1 The value for element at index 1.
102 * @param {number} v2 The value for element at index 2.
103 * @return {!goog.vec.vec3f.Type} Return vec so that operations can be
104 * chained together.
105 */
106 goog.vec.vec3f.setFromValues = function(vec, v0, v1, v2) {
107 vec[0] = v0;
108 vec[1] = v1;
109 vec[2] = v2;
110 return vec;
111 };
112
113
114 /**
115 * Initializes vec3f vec from vec3f src.
116 *
117 * @param {!goog.vec.vec3f.Type} vec The destination vector.
118 * @param {!goog.vec.vec3f.Type} src The source vector.
119 * @return {!goog.vec.vec3f.Type} Return vec so that operations can be
120 * chained together.
121 */
122 goog.vec.vec3f.setFromVec3f = function(vec, src) {
123 vec[0] = src[0];
124 vec[1] = src[1];
125 vec[2] = src[2];
126 return vec;
127 };
128
129
130 /**
131 * Initializes vec3f vec from vec3d src (typed as a Float64Array to
132 * avoid circular goog.requires).
133 *
134 * @param {!goog.vec.vec3f.Type} vec The destination vector.
135 * @param {Float64Array} src The source vector.
136 * @return {!goog.vec.vec3f.Type} Return vec so that operations can be
137 * chained together.
138 */
139 goog.vec.vec3f.setFromVec3d = function(vec, src) {
140 vec[0] = src[0];
141 vec[1] = src[1];
142 vec[2] = src[2];
143 return vec;
144 };
145
146
147 /**
148 * Initializes vec3f vec from Array src.
149 *
150 * @param {!goog.vec.vec3f.Type} vec The destination vector.
151 * @param {Array<number>} src The source vector.
152 * @return {!goog.vec.vec3f.Type} Return vec so that operations can be
153 * chained together.
154 */
155 goog.vec.vec3f.setFromArray = function(vec, src) {
156 vec[0] = src[0];
157 vec[1] = src[1];
158 vec[2] = src[2];
159 return vec;
160 };
161
162
163 /**
164 * Performs a component-wise addition of vec0 and vec1 together storing the
165 * result into resultVec.
166 *
167 * @param {!goog.vec.vec3f.Type} vec0 The first addend.
168 * @param {!goog.vec.vec3f.Type} vec1 The second addend.
169 * @param {!goog.vec.vec3f.Type} resultVec The vector to
170 * receive the result. May be vec0 or vec1.
171 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
172 * chained together.
173 */
174 goog.vec.vec3f.add = function(vec0, vec1, resultVec) {
175 resultVec[0] = vec0[0] + vec1[0];
176 resultVec[1] = vec0[1] + vec1[1];
177 resultVec[2] = vec0[2] + vec1[2];
178 return resultVec;
179 };
180
181
182 /**
183 * Performs a component-wise subtraction of vec1 from vec0 storing the
184 * result into resultVec.
185 *
186 * @param {!goog.vec.vec3f.Type} vec0 The minuend.
187 * @param {!goog.vec.vec3f.Type} vec1 The subtrahend.
188 * @param {!goog.vec.vec3f.Type} resultVec The vector to
189 * receive the result. May be vec0 or vec1.
190 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
191 * chained together.
192 */
193 goog.vec.vec3f.subtract = function(vec0, vec1, resultVec) {
194 resultVec[0] = vec0[0] - vec1[0];
195 resultVec[1] = vec0[1] - vec1[1];
196 resultVec[2] = vec0[2] - vec1[2];
197 return resultVec;
198 };
199
200
201 /**
202 * Negates vec0, storing the result into resultVec.
203 *
204 * @param {!goog.vec.vec3f.Type} vec0 The vector to negate.
205 * @param {!goog.vec.vec3f.Type} resultVec The vector to
206 * receive the result. May be vec0.
207 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
208 * chained together.
209 */
210 goog.vec.vec3f.negate = function(vec0, resultVec) {
211 resultVec[0] = -vec0[0];
212 resultVec[1] = -vec0[1];
213 resultVec[2] = -vec0[2];
214 return resultVec;
215 };
216
217
218 /**
219 * Takes the absolute value of each component of vec0 storing the result in
220 * resultVec.
221 *
222 * @param {!goog.vec.vec3f.Type} vec0 The source vector.
223 * @param {!goog.vec.vec3f.Type} resultVec The vector to receive the result.
224 * May be vec0.
225 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
226 * chained together.
227 */
228 goog.vec.vec3f.abs = function(vec0, resultVec) {
229 resultVec[0] = Math.abs(vec0[0]);
230 resultVec[1] = Math.abs(vec0[1]);
231 resultVec[2] = Math.abs(vec0[2]);
232 return resultVec;
233 };
234
235
236 /**
237 * Multiplies each component of vec0 with scalar storing the product into
238 * resultVec.
239 *
240 * @param {!goog.vec.vec3f.Type} vec0 The source vector.
241 * @param {number} scalar The value to multiply with each component of vec0.
242 * @param {!goog.vec.vec3f.Type} resultVec The vector to
243 * receive the result. May be vec0.
244 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
245 * chained together.
246 */
247 goog.vec.vec3f.scale = function(vec0, scalar, resultVec) {
248 resultVec[0] = vec0[0] * scalar;
249 resultVec[1] = vec0[1] * scalar;
250 resultVec[2] = vec0[2] * scalar;
251 return resultVec;
252 };
253
254
255 /**
256 * Returns the magnitudeSquared of the given vector.
257 *
258 * @param {!goog.vec.vec3f.Type} vec0 The vector.
259 * @return {number} The magnitude of the vector.
260 */
261 goog.vec.vec3f.magnitudeSquared = function(vec0) {
262 var x = vec0[0], y = vec0[1], z = vec0[2];
263 return x * x + y * y + z * z;
264 };
265
266
267 /**
268 * Returns the magnitude of the given vector.
269 *
270 * @param {!goog.vec.vec3f.Type} vec0 The vector.
271 * @return {number} The magnitude of the vector.
272 */
273 goog.vec.vec3f.magnitude = function(vec0) {
274 var x = vec0[0], y = vec0[1], z = vec0[2];
275 return Math.sqrt(x * x + y * y + z * z);
276 };
277
278
279 /**
280 * Normalizes the given vector storing the result into resultVec.
281 *
282 * @param {!goog.vec.vec3f.Type} vec0 The vector to normalize.
283 * @param {!goog.vec.vec3f.Type} resultVec The vector to
284 * receive the result. May be vec0.
285 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
286 * chained together.
287 */
288 goog.vec.vec3f.normalize = function(vec0, resultVec) {
289 var x = vec0[0], y = vec0[1], z = vec0[2];
290 var ilen = 1 / Math.sqrt(x * x + y * y + z * z);
291 resultVec[0] = x * ilen;
292 resultVec[1] = y * ilen;
293 resultVec[2] = z * ilen;
294 return resultVec;
295 };
296
297
298 /**
299 * Returns the scalar product of vectors v0 and v1.
300 *
301 * @param {!goog.vec.vec3f.Type} v0 The first vector.
302 * @param {!goog.vec.vec3f.Type} v1 The second vector.
303 * @return {number} The scalar product.
304 */
305 goog.vec.vec3f.dot = function(v0, v1) {
306 return v0[0] * v1[0] + v0[1] * v1[1] + v0[2] * v1[2];
307 };
308
309
310 /**
311 * Computes the vector (cross) product of v0 and v1 storing the result into
312 * resultVec.
313 *
314 * @param {!goog.vec.vec3f.Type} v0 The first vector.
315 * @param {!goog.vec.vec3f.Type} v1 The second vector.
316 * @param {!goog.vec.vec3f.Type} resultVec The vector to receive the
317 * results. May be either v0 or v1.
318 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
319 * chained together.
320 */
321 goog.vec.vec3f.cross = function(v0, v1, resultVec) {
322 var x0 = v0[0], y0 = v0[1], z0 = v0[2];
323 var x1 = v1[0], y1 = v1[1], z1 = v1[2];
324 resultVec[0] = y0 * z1 - z0 * y1;
325 resultVec[1] = z0 * x1 - x0 * z1;
326 resultVec[2] = x0 * y1 - y0 * x1;
327 return resultVec;
328 };
329
330
331 /**
332 * Returns the squared distance between two points.
333 *
334 * @param {!goog.vec.vec3f.Type} vec0 First point.
335 * @param {!goog.vec.vec3f.Type} vec1 Second point.
336 * @return {number} The squared distance between the points.
337 */
338 goog.vec.vec3f.distanceSquared = function(vec0, vec1) {
339 var x = vec0[0] - vec1[0];
340 var y = vec0[1] - vec1[1];
341 var z = vec0[2] - vec1[2];
342 return x * x + y * y + z * z;
343 };
344
345
346 /**
347 * Returns the distance between two points.
348 *
349 * @param {!goog.vec.vec3f.Type} vec0 First point.
350 * @param {!goog.vec.vec3f.Type} vec1 Second point.
351 * @return {number} The distance between the points.
352 */
353 goog.vec.vec3f.distance = function(vec0, vec1) {
354 return Math.sqrt(goog.vec.vec3f.distanceSquared(vec0, vec1));
355 };
356
357
358 /**
359 * Returns a unit vector pointing from one point to another.
360 * If the input points are equal then the result will be all zeros.
361 *
362 * @param {!goog.vec.vec3f.Type} vec0 Origin point.
363 * @param {!goog.vec.vec3f.Type} vec1 Target point.
364 * @param {!goog.vec.vec3f.Type} resultVec The vector to receive the
365 * results (may be vec0 or vec1).
366 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
367 * chained together.
368 */
369 goog.vec.vec3f.direction = function(vec0, vec1, resultVec) {
370 var x = vec1[0] - vec0[0];
371 var y = vec1[1] - vec0[1];
372 var z = vec1[2] - vec0[2];
373 var d = Math.sqrt(x * x + y * y + z * z);
374 if (d) {
375 d = 1 / d;
376 resultVec[0] = x * d;
377 resultVec[1] = y * d;
378 resultVec[2] = z * d;
379 } else {
380 resultVec[0] = resultVec[1] = resultVec[2] = 0;
381 }
382 return resultVec;
383 };
384
385
386 /**
387 * Linearly interpolate from vec0 to v1 according to f. The value of f should be
388 * in the range [0..1] otherwise the results are undefined.
389 *
390 * @param {!goog.vec.vec3f.Type} v0 The first vector.
391 * @param {!goog.vec.vec3f.Type} v1 The second vector.
392 * @param {number} f The interpolation factor.
393 * @param {!goog.vec.vec3f.Type} resultVec The vector to receive the
394 * results (may be v0 or v1).
395 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
396 * chained together.
397 */
398 goog.vec.vec3f.lerp = function(v0, v1, f, resultVec) {
399 var x = v0[0], y = v0[1], z = v0[2];
400 resultVec[0] = (v1[0] - x) * f + x;
401 resultVec[1] = (v1[1] - y) * f + y;
402 resultVec[2] = (v1[2] - z) * f + z;
403 return resultVec;
404 };
405
406
407 /**
408 * Perform a spherical linear interpolation from v0 to v1 according to f. The
409 * value of f should be in the range [0..1] otherwise the results are undefined.
410 *
411 * Slerp is normally used to interpolate quaternions, but there is a geometric
412 * formula for interpolating vectors directly, see "Geometric Slerp" in:
413 * https://en.wikipedia.org/wiki/Slerp.
414 *
415 * This interpolates the vectors' directions via slerp, but linearly
416 * interpolates the vectors' magnitudes.
417 *
418 * Results are undefined if v0 or v1 are of zero magnitude.
419 *
420 * @param {!goog.vec.vec3f.Type} v0 The first vector.
421 * @param {!goog.vec.vec3f.Type} v1 The second vector.
422 * @param {number} f The interpolation factor.
423 * @param {!goog.vec.vec3f.Type} resultVec The vector to receive the
424 * results (may be v0 or v1).
425 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
426 * chained together.
427 */
428 goog.vec.vec3f.slerp = function(v0, v1, f, resultVec) {
429 var v0Magnitude = goog.vec.vec3f.magnitude(v0);
430 var v1Magnitude = goog.vec.vec3f.magnitude(v1);
431
432 var cosAngle = goog.vec.vec3f.dot(v0, v1) / (v0Magnitude * v1Magnitude);
433
434 // If v0 and v1 are almost the same direction, fall back on a straight lerp.
435 if (cosAngle > 1 - goog.vec.EPSILON) {
436 return goog.vec.vec3f.lerp(v0, v1, f, resultVec);
437 }
438
439 var angle = 0;
440 var sinAngle = 0;
441
442 // If v0 and v1 are opposite directions, pick an arbitrary 'mid' vector that
443 // is perpendicular to both, and slerp from v0 -> mid -> v1.
444 if (cosAngle < -1 + goog.vec.EPSILON) {
445 var mid = goog.vec.vec3f.create();
446 var magnitudeFactor = (v0Magnitude + v1Magnitude) / 2;
447 if (v0[0]) { // v0 not parallel to [0,0,1].
448 magnitudeFactor /= Math.sqrt(v0[0] * v0[0] + v0[1] + v0[1]);
449 mid[0] = -v0[1] * magnitudeFactor;
450 mid[1] = v0[0] * magnitudeFactor;
451 mid[2] = 0;
452 } else { // v0 not parallel to [1,0,0].
453 magnitudeFactor /= Math.sqrt(v0[2] * v0[2] + v0[1] + v0[1]);
454 mid[0] = 0;
455 mid[1] = -v0[2] * magnitudeFactor;
456 mid[2] = v0[1] * magnitudeFactor;
457 }
458
459 // Depending on f, slerp between either v0 and mid, or mid and v1.
460 if (f <= 0.5) {
461 v1Magnitude = v0Magnitude;
462 v1 = mid;
463 f *= 2;
464 } else {
465 v0 = mid;
466 f = 2 * f - 1;
467 }
468
469 angle = Math.PI / 2;
470 cosAngle = 0;
471 sinAngle = 1;
472 } else {
473 angle = Math.acos(cosAngle);
474 sinAngle = Math.sqrt(1 - cosAngle * cosAngle);
475 }
476
477 var coeff0 = (Math.sin((1 - f) * angle) / sinAngle) / v0Magnitude;
478 var coeff1 = (Math.sin(f * angle) / sinAngle) / v1Magnitude;
479 var magnitude = (1 - f) * v0Magnitude + f * v1Magnitude;
480
481 resultVec[0] = (v0[0] * coeff0 + v1[0] * coeff1) * magnitude;
482 resultVec[1] = (v0[1] * coeff0 + v1[1] * coeff1) * magnitude;
483 resultVec[2] = (v0[2] * coeff0 + v1[2] * coeff1) * magnitude;
484 return resultVec;
485 };
486
487
488 /**
489 * Compares the components of vec0 with the components of another vector or
490 * scalar, storing the larger values in resultVec.
491 *
492 * @param {!goog.vec.vec3f.Type} vec0 The source vector.
493 * @param {!goog.vec.vec3f.Type|number} limit The limit vector or scalar.
494 * @param {!goog.vec.vec3f.Type} resultVec The vector to receive the
495 * results (may be vec0 or limit).
496 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
497 * chained together.
498 */
499 goog.vec.vec3f.max = function(vec0, limit, resultVec) {
500 if (goog.isNumber(limit)) {
501 resultVec[0] = Math.max(vec0[0], limit);
502 resultVec[1] = Math.max(vec0[1], limit);
503 resultVec[2] = Math.max(vec0[2], limit);
504 } else {
505 resultVec[0] = Math.max(vec0[0], limit[0]);
506 resultVec[1] = Math.max(vec0[1], limit[1]);
507 resultVec[2] = Math.max(vec0[2], limit[2]);
508 }
509 return resultVec;
510 };
511
512
513 /**
514 * Compares the components of vec0 with the components of another vector or
515 * scalar, storing the smaller values in resultVec.
516 *
517 * @param {!goog.vec.vec3f.Type} vec0 The source vector.
518 * @param {!goog.vec.vec3f.Type|number} limit The limit vector or scalar.
519 * @param {!goog.vec.vec3f.Type} resultVec The vector to receive the
520 * results (may be vec0 or limit).
521 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
522 * chained together.
523 */
524 goog.vec.vec3f.min = function(vec0, limit, resultVec) {
525 if (goog.isNumber(limit)) {
526 resultVec[0] = Math.min(vec0[0], limit);
527 resultVec[1] = Math.min(vec0[1], limit);
528 resultVec[2] = Math.min(vec0[2], limit);
529 } else {
530 resultVec[0] = Math.min(vec0[0], limit[0]);
531 resultVec[1] = Math.min(vec0[1], limit[1]);
532 resultVec[2] = Math.min(vec0[2], limit[2]);
533 }
534 return resultVec;
535 };
536
537
538 /**
539 * Returns true if the components of v0 are equal to the components of v1.
540 *
541 * @param {!goog.vec.vec3f.Type} v0 The first vector.
542 * @param {!goog.vec.vec3f.Type} v1 The second vector.
543 * @return {boolean} True if the vectors are equal, false otherwise.
544 */
545 goog.vec.vec3f.equals = function(v0, v1) {
546 return v0.length == v1.length && v0[0] == v1[0] && v0[1] == v1[1] &&
547 v0[2] == v1[2];
548 };