Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Demos / NativeClient / bin_html / vector3.js
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 /**
6  * @fileoverview  A 3D vector class.  Proviudes some utility functions on
7  * 3-dimentional vectors.
8  */
9
10 // Requires tumbler
11
12 /**
13  * Constructor for the Vector3 object.  This class contains a 3-tuple that
14  * represents a vector in 3D space.
15  * @param {?number} opt_x The x-coordinate for this vector.  If null or
16  *     undefined, the x-coordinate value is set to 0.
17  * @param {?number} opt_y The y-coordinate for this vector.  If null or
18  *     undefined, the y-coordinate value is set to 0.
19  * @param {?number} opt_z The z-coordinate for this vector.  If null or
20  *     undefined, the z-coordinate value is set to 0.
21  * @constructor
22  */
23 tumbler.Vector3 = function(opt_x, opt_y, opt_z) {
24   /**
25    * The vector's 3-tuple.
26    * @type {number}
27    */
28   this.x = opt_x || 0;
29   this.y = opt_y || 0;
30   this.z = opt_z || 0;
31 }
32
33 /**
34  * Method to return the magnitude of a Vector3.
35  * @return {number} the magnitude of the vector.
36  */
37 tumbler.Vector3.prototype.magnitude = function() {
38   return Math.sqrt(this.dot(this));
39 }
40
41 /**
42  * Normalize the vector in-place.
43  * @return {number} the magnitude of the vector.
44  */
45 tumbler.Vector3.prototype.normalize = function() {
46   var mag = this.magnitude();
47   if (mag < tumbler.Vector3.DOUBLE_EPSILON)
48     return 0.0;  // |this| is equivalent to the 0-vector, don't normalize.
49   this.scale(1.0 / mag);
50   return mag;
51 }
52
53 /**
54  * Scale the vector in-place by |s|.
55  * @param {!number} s The scale factor.
56  */
57 tumbler.Vector3.prototype.scale = function(s) {
58   this.x *= s;
59   this.y *= s;
60   this.z *= s;
61 }
62
63 /**
64  * Compute the dot product: |this| . v.
65  * @param {!tumbler.Vector3} v The vector to dot.
66  * @return {number} the result of |this| . v.
67  */
68 tumbler.Vector3.prototype.dot = function(v) {
69   return this.x * v.x + this.y * v.y + this.z * v.z;
70 }
71
72 /**
73  * Compute the cross product: |this| X v.
74  * @param {!tumbler.Vector3} v The vector to cross with.
75  * @return {tumbler.Vector3} the result of |this| X v.
76  */
77 tumbler.Vector3.prototype.cross = function(v) {
78   var vCross = new tumbler.Vector3(this.y * v.z - this.z * v.y,
79                                    this.z * v.x - this.x * v.z,
80                                    this.x * v.y - this.y * v.x);
81   return vCross;
82 }
83
84 /**
85  * Real numbers that are less than this distance apart are considered
86  * equivalent.
87  * TODO(dspringer): It seems as though there should be a const like this
88  * in generally available somewhere.
89  * @type {number}
90  */
91 tumbler.Vector3.DOUBLE_EPSILON = 1.0e-16;