File: vec3f.js

package info (click to toggle)
aseba-plugin-blockly 20180211%2Bgit-2
  • links: PTS
  • area: non-free
  • in suites: buster
  • size: 64,472 kB
  • sloc: xml: 7,976; python: 2,314; sh: 261; lisp: 24; makefile: 10
file content (548 lines) | stat: -rw-r--r-- 17,109 bytes parent folder | download | duplicates (2)
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
// Copyright 2013 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


////////////////////////// NOTE ABOUT EDITING THIS FILE ///////////////////////
//                                                                           //
// Any edits to this file must be applied to vec3d.js by running:            //
//   swap_type.sh vec3f.js > vec3d.js                                        //
//                                                                           //
////////////////////////// NOTE ABOUT EDITING THIS FILE ///////////////////////


/**
 * @fileoverview Provides functions for operating on 3 element float (32bit)
 * vectors.
 *
 * The last parameter will typically be the output object and an object
 * can be both an input and output parameter to all methods except where
 * noted.
 *
 * See the README for notes about the design and structure of the API
 * (especially related to performance).
 *
 */
goog.provide('goog.vec.vec3f');
goog.provide('goog.vec.vec3f.Type');

/** @suppress {extraRequire} */
goog.require('goog.vec');

/** @typedef {goog.vec.Float32} */ goog.vec.vec3f.Type;


/**
 * Creates a vec3f with all elements initialized to zero.
 *
 * @return {!goog.vec.vec3f.Type} The new vec3f.
 */
goog.vec.vec3f.create = function() {
  return new Float32Array(3);
};


/**
 * Creates a new vec3f initialized with the value from the given array.
 *
 * @param {!Array<number>} vec The source 3 element array.
 * @return {!goog.vec.vec3f.Type} The new vec3f.
 */
goog.vec.vec3f.createFromArray = function(vec) {
  var newVec = goog.vec.vec3f.create();
  goog.vec.vec3f.setFromArray(newVec, vec);
  return newVec;
};


/**
 * Creates a new vec3f initialized with the supplied values.
 *
 * @param {number} v0 The value for element at index 0.
 * @param {number} v1 The value for element at index 1.
 * @param {number} v2 The value for element at index 2.
 * @return {!goog.vec.vec3f.Type} The new vector.
 */
goog.vec.vec3f.createFromValues = function(v0, v1, v2) {
  var vec = goog.vec.vec3f.create();
  goog.vec.vec3f.setFromValues(vec, v0, v1, v2);
  return vec;
};


/**
 * Creates a clone of the given vec3f.
 *
 * @param {!goog.vec.vec3f.Type} vec The source vec3f.
 * @return {!goog.vec.vec3f.Type} The new cloned vec3f.
 */
goog.vec.vec3f.clone = function(vec) {
  var newVec = goog.vec.vec3f.create();
  goog.vec.vec3f.setFromVec3f(newVec, vec);
  return newVec;
};


/**
 * Initializes the vector with the given values.
 *
 * @param {!goog.vec.vec3f.Type} vec The vector to receive the values.
 * @param {number} v0 The value for element at index 0.
 * @param {number} v1 The value for element at index 1.
 * @param {number} v2 The value for element at index 2.
 * @return {!goog.vec.vec3f.Type} Return vec so that operations can be
 *     chained together.
 */
goog.vec.vec3f.setFromValues = function(vec, v0, v1, v2) {
  vec[0] = v0;
  vec[1] = v1;
  vec[2] = v2;
  return vec;
};


/**
 * Initializes vec3f vec from vec3f src.
 *
 * @param {!goog.vec.vec3f.Type} vec The destination vector.
 * @param {!goog.vec.vec3f.Type} src The source vector.
 * @return {!goog.vec.vec3f.Type} Return vec so that operations can be
 *     chained together.
 */
goog.vec.vec3f.setFromVec3f = function(vec, src) {
  vec[0] = src[0];
  vec[1] = src[1];
  vec[2] = src[2];
  return vec;
};


/**
 * Initializes vec3f vec from vec3d src (typed as a Float64Array to
 * avoid circular goog.requires).
 *
 * @param {!goog.vec.vec3f.Type} vec The destination vector.
 * @param {Float64Array} src The source vector.
 * @return {!goog.vec.vec3f.Type} Return vec so that operations can be
 *     chained together.
 */
goog.vec.vec3f.setFromVec3d = function(vec, src) {
  vec[0] = src[0];
  vec[1] = src[1];
  vec[2] = src[2];
  return vec;
};


/**
 * Initializes vec3f vec from Array src.
 *
 * @param {!goog.vec.vec3f.Type} vec The destination vector.
 * @param {Array<number>} src The source vector.
 * @return {!goog.vec.vec3f.Type} Return vec so that operations can be
 *     chained together.
 */
goog.vec.vec3f.setFromArray = function(vec, src) {
  vec[0] = src[0];
  vec[1] = src[1];
  vec[2] = src[2];
  return vec;
};


/**
 * Performs a component-wise addition of vec0 and vec1 together storing the
 * result into resultVec.
 *
 * @param {!goog.vec.vec3f.Type} vec0 The first addend.
 * @param {!goog.vec.vec3f.Type} vec1 The second addend.
 * @param {!goog.vec.vec3f.Type} resultVec The vector to
 *     receive the result. May be vec0 or vec1.
 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
 *     chained together.
 */
goog.vec.vec3f.add = function(vec0, vec1, resultVec) {
  resultVec[0] = vec0[0] + vec1[0];
  resultVec[1] = vec0[1] + vec1[1];
  resultVec[2] = vec0[2] + vec1[2];
  return resultVec;
};


/**
 * Performs a component-wise subtraction of vec1 from vec0 storing the
 * result into resultVec.
 *
 * @param {!goog.vec.vec3f.Type} vec0 The minuend.
 * @param {!goog.vec.vec3f.Type} vec1 The subtrahend.
 * @param {!goog.vec.vec3f.Type} resultVec The vector to
 *     receive the result. May be vec0 or vec1.
 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
 *     chained together.
 */
goog.vec.vec3f.subtract = function(vec0, vec1, resultVec) {
  resultVec[0] = vec0[0] - vec1[0];
  resultVec[1] = vec0[1] - vec1[1];
  resultVec[2] = vec0[2] - vec1[2];
  return resultVec;
};


/**
 * Negates vec0, storing the result into resultVec.
 *
 * @param {!goog.vec.vec3f.Type} vec0 The vector to negate.
 * @param {!goog.vec.vec3f.Type} resultVec The vector to
 *     receive the result. May be vec0.
 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
 *     chained together.
 */
goog.vec.vec3f.negate = function(vec0, resultVec) {
  resultVec[0] = -vec0[0];
  resultVec[1] = -vec0[1];
  resultVec[2] = -vec0[2];
  return resultVec;
};


/**
 * Takes the absolute value of each component of vec0 storing the result in
 * resultVec.
 *
 * @param {!goog.vec.vec3f.Type} vec0 The source vector.
 * @param {!goog.vec.vec3f.Type} resultVec The vector to receive the result.
 *     May be vec0.
 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
 *     chained together.
 */
goog.vec.vec3f.abs = function(vec0, resultVec) {
  resultVec[0] = Math.abs(vec0[0]);
  resultVec[1] = Math.abs(vec0[1]);
  resultVec[2] = Math.abs(vec0[2]);
  return resultVec;
};


/**
 * Multiplies each component of vec0 with scalar storing the product into
 * resultVec.
 *
 * @param {!goog.vec.vec3f.Type} vec0 The source vector.
 * @param {number} scalar The value to multiply with each component of vec0.
 * @param {!goog.vec.vec3f.Type} resultVec The vector to
 *     receive the result. May be vec0.
 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
 *     chained together.
 */
goog.vec.vec3f.scale = function(vec0, scalar, resultVec) {
  resultVec[0] = vec0[0] * scalar;
  resultVec[1] = vec0[1] * scalar;
  resultVec[2] = vec0[2] * scalar;
  return resultVec;
};


/**
 * Returns the magnitudeSquared of the given vector.
 *
 * @param {!goog.vec.vec3f.Type} vec0 The vector.
 * @return {number} The magnitude of the vector.
 */
goog.vec.vec3f.magnitudeSquared = function(vec0) {
  var x = vec0[0], y = vec0[1], z = vec0[2];
  return x * x + y * y + z * z;
};


/**
 * Returns the magnitude of the given vector.
 *
 * @param {!goog.vec.vec3f.Type} vec0 The vector.
 * @return {number} The magnitude of the vector.
 */
goog.vec.vec3f.magnitude = function(vec0) {
  var x = vec0[0], y = vec0[1], z = vec0[2];
  return Math.sqrt(x * x + y * y + z * z);
};


/**
 * Normalizes the given vector storing the result into resultVec.
 *
 * @param {!goog.vec.vec3f.Type} vec0 The vector to normalize.
 * @param {!goog.vec.vec3f.Type} resultVec The vector to
 *     receive the result. May be vec0.
 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
 *     chained together.
 */
goog.vec.vec3f.normalize = function(vec0, resultVec) {
  var x = vec0[0], y = vec0[1], z = vec0[2];
  var ilen = 1 / Math.sqrt(x * x + y * y + z * z);
  resultVec[0] = x * ilen;
  resultVec[1] = y * ilen;
  resultVec[2] = z * ilen;
  return resultVec;
};


/**
 * Returns the scalar product of vectors v0 and v1.
 *
 * @param {!goog.vec.vec3f.Type} v0 The first vector.
 * @param {!goog.vec.vec3f.Type} v1 The second vector.
 * @return {number} The scalar product.
 */
goog.vec.vec3f.dot = function(v0, v1) {
  return v0[0] * v1[0] + v0[1] * v1[1] + v0[2] * v1[2];
};


/**
 * Computes the vector (cross) product of v0 and v1 storing the result into
 * resultVec.
 *
 * @param {!goog.vec.vec3f.Type} v0 The first vector.
 * @param {!goog.vec.vec3f.Type} v1 The second vector.
 * @param {!goog.vec.vec3f.Type} resultVec The vector to receive the
 *     results. May be either v0 or v1.
 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
 *     chained together.
 */
goog.vec.vec3f.cross = function(v0, v1, resultVec) {
  var x0 = v0[0], y0 = v0[1], z0 = v0[2];
  var x1 = v1[0], y1 = v1[1], z1 = v1[2];
  resultVec[0] = y0 * z1 - z0 * y1;
  resultVec[1] = z0 * x1 - x0 * z1;
  resultVec[2] = x0 * y1 - y0 * x1;
  return resultVec;
};


/**
 * Returns the squared distance between two points.
 *
 * @param {!goog.vec.vec3f.Type} vec0 First point.
 * @param {!goog.vec.vec3f.Type} vec1 Second point.
 * @return {number} The squared distance between the points.
 */
goog.vec.vec3f.distanceSquared = function(vec0, vec1) {
  var x = vec0[0] - vec1[0];
  var y = vec0[1] - vec1[1];
  var z = vec0[2] - vec1[2];
  return x * x + y * y + z * z;
};


/**
 * Returns the distance between two points.
 *
 * @param {!goog.vec.vec3f.Type} vec0 First point.
 * @param {!goog.vec.vec3f.Type} vec1 Second point.
 * @return {number} The distance between the points.
 */
goog.vec.vec3f.distance = function(vec0, vec1) {
  return Math.sqrt(goog.vec.vec3f.distanceSquared(vec0, vec1));
};


/**
 * Returns a unit vector pointing from one point to another.
 * If the input points are equal then the result will be all zeros.
 *
 * @param {!goog.vec.vec3f.Type} vec0 Origin point.
 * @param {!goog.vec.vec3f.Type} vec1 Target point.
 * @param {!goog.vec.vec3f.Type} resultVec The vector to receive the
 *     results (may be vec0 or vec1).
 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
 *     chained together.
 */
goog.vec.vec3f.direction = function(vec0, vec1, resultVec) {
  var x = vec1[0] - vec0[0];
  var y = vec1[1] - vec0[1];
  var z = vec1[2] - vec0[2];
  var d = Math.sqrt(x * x + y * y + z * z);
  if (d) {
    d = 1 / d;
    resultVec[0] = x * d;
    resultVec[1] = y * d;
    resultVec[2] = z * d;
  } else {
    resultVec[0] = resultVec[1] = resultVec[2] = 0;
  }
  return resultVec;
};


/**
 * Linearly interpolate from vec0 to v1 according to f. The value of f should be
 * in the range [0..1] otherwise the results are undefined.
 *
 * @param {!goog.vec.vec3f.Type} v0 The first vector.
 * @param {!goog.vec.vec3f.Type} v1 The second vector.
 * @param {number} f The interpolation factor.
 * @param {!goog.vec.vec3f.Type} resultVec The vector to receive the
 *     results (may be v0 or v1).
 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
 *     chained together.
 */
goog.vec.vec3f.lerp = function(v0, v1, f, resultVec) {
  var x = v0[0], y = v0[1], z = v0[2];
  resultVec[0] = (v1[0] - x) * f + x;
  resultVec[1] = (v1[1] - y) * f + y;
  resultVec[2] = (v1[2] - z) * f + z;
  return resultVec;
};


/**
 * Perform a spherical linear interpolation from v0 to v1 according to f. The
 * value of f should be in the range [0..1] otherwise the results are undefined.
 *
 * Slerp is normally used to interpolate quaternions, but there is a geometric
 * formula for interpolating vectors directly, see "Geometric Slerp" in:
 * https://en.wikipedia.org/wiki/Slerp.
 *
 * This interpolates the vectors' directions via slerp, but linearly
 * interpolates the vectors' magnitudes.
 *
 * Results are undefined if v0 or v1 are of zero magnitude.
 *
 * @param {!goog.vec.vec3f.Type} v0 The first vector.
 * @param {!goog.vec.vec3f.Type} v1 The second vector.
 * @param {number} f The interpolation factor.
 * @param {!goog.vec.vec3f.Type} resultVec The vector to receive the
 *     results (may be v0 or v1).
 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
 *     chained together.
 */
goog.vec.vec3f.slerp = function(v0, v1, f, resultVec) {
  var v0Magnitude = goog.vec.vec3f.magnitude(v0);
  var v1Magnitude = goog.vec.vec3f.magnitude(v1);

  var cosAngle = goog.vec.vec3f.dot(v0, v1) / (v0Magnitude * v1Magnitude);

  // If v0 and v1 are almost the same direction, fall back on a straight lerp.
  if (cosAngle > 1 - goog.vec.EPSILON) {
    return goog.vec.vec3f.lerp(v0, v1, f, resultVec);
  }

  var angle = 0;
  var sinAngle = 0;

  // If v0 and v1 are opposite directions, pick an arbitrary 'mid' vector that
  // is perpendicular to both, and slerp from v0 -> mid -> v1.
  if (cosAngle < -1 + goog.vec.EPSILON) {
    var mid = goog.vec.vec3f.create();
    var magnitudeFactor = (v0Magnitude + v1Magnitude) / 2;
    if (v0[0]) {  // v0 not parallel to [0,0,1].
      magnitudeFactor /= Math.sqrt(v0[0] * v0[0] + v0[1] + v0[1]);
      mid[0] = -v0[1] * magnitudeFactor;
      mid[1] = v0[0] * magnitudeFactor;
      mid[2] = 0;
    } else {  // v0 not parallel to [1,0,0].
      magnitudeFactor /= Math.sqrt(v0[2] * v0[2] + v0[1] + v0[1]);
      mid[0] = 0;
      mid[1] = -v0[2] * magnitudeFactor;
      mid[2] = v0[1] * magnitudeFactor;
    }

    // Depending on f, slerp between either v0 and mid, or mid and v1.
    if (f <= 0.5) {
      v1Magnitude = v0Magnitude;
      v1 = mid;
      f *= 2;
    } else {
      v0 = mid;
      f = 2 * f - 1;
    }

    angle = Math.PI / 2;
    cosAngle = 0;
    sinAngle = 1;
  } else {
    angle = Math.acos(cosAngle);
    sinAngle = Math.sqrt(1 - cosAngle * cosAngle);
  }

  var coeff0 = (Math.sin((1 - f) * angle) / sinAngle) / v0Magnitude;
  var coeff1 = (Math.sin(f * angle) / sinAngle) / v1Magnitude;
  var magnitude = (1 - f) * v0Magnitude + f * v1Magnitude;

  resultVec[0] = (v0[0] * coeff0 + v1[0] * coeff1) * magnitude;
  resultVec[1] = (v0[1] * coeff0 + v1[1] * coeff1) * magnitude;
  resultVec[2] = (v0[2] * coeff0 + v1[2] * coeff1) * magnitude;
  return resultVec;
};


/**
 * Compares the components of vec0 with the components of another vector or
 * scalar, storing the larger values in resultVec.
 *
 * @param {!goog.vec.vec3f.Type} vec0 The source vector.
 * @param {!goog.vec.vec3f.Type|number} limit The limit vector or scalar.
 * @param {!goog.vec.vec3f.Type} resultVec The vector to receive the
 *     results (may be vec0 or limit).
 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
 *     chained together.
 */
goog.vec.vec3f.max = function(vec0, limit, resultVec) {
  if (goog.isNumber(limit)) {
    resultVec[0] = Math.max(vec0[0], limit);
    resultVec[1] = Math.max(vec0[1], limit);
    resultVec[2] = Math.max(vec0[2], limit);
  } else {
    resultVec[0] = Math.max(vec0[0], limit[0]);
    resultVec[1] = Math.max(vec0[1], limit[1]);
    resultVec[2] = Math.max(vec0[2], limit[2]);
  }
  return resultVec;
};


/**
 * Compares the components of vec0 with the components of another vector or
 * scalar, storing the smaller values in resultVec.
 *
 * @param {!goog.vec.vec3f.Type} vec0 The source vector.
 * @param {!goog.vec.vec3f.Type|number} limit The limit vector or scalar.
 * @param {!goog.vec.vec3f.Type} resultVec The vector to receive the
 *     results (may be vec0 or limit).
 * @return {!goog.vec.vec3f.Type} Return resultVec so that operations can be
 *     chained together.
 */
goog.vec.vec3f.min = function(vec0, limit, resultVec) {
  if (goog.isNumber(limit)) {
    resultVec[0] = Math.min(vec0[0], limit);
    resultVec[1] = Math.min(vec0[1], limit);
    resultVec[2] = Math.min(vec0[2], limit);
  } else {
    resultVec[0] = Math.min(vec0[0], limit[0]);
    resultVec[1] = Math.min(vec0[1], limit[1]);
    resultVec[2] = Math.min(vec0[2], limit[2]);
  }
  return resultVec;
};


/**
 * Returns true if the components of v0 are equal to the components of v1.
 *
 * @param {!goog.vec.vec3f.Type} v0 The first vector.
 * @param {!goog.vec.vec3f.Type} v1 The second vector.
 * @return {boolean} True if the vectors are equal, false otherwise.
 */
goog.vec.vec3f.equals = function(v0, v1) {
  return v0.length == v1.length && v0[0] == v1[0] && v0[1] == v1[1] &&
      v0[2] == v1[2];
};