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.
6 * @fileoverview A 3D vector class. Proviudes some utility functions on
7 * 3-dimentional vectors.
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.
23 tumbler.Vector3 = function(opt_x, opt_y, opt_z) {
25 * The vector's 3-tuple.
34 * Method to return the magnitude of a Vector3.
35 * @return {number} the magnitude of the vector.
37 tumbler.Vector3.prototype.magnitude = function() {
38 return Math.sqrt(this.dot(this));
42 * Normalize the vector in-place.
43 * @return {number} the magnitude of the vector.
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);
54 * Scale the vector in-place by |s|.
55 * @param {!number} s The scale factor.
57 tumbler.Vector3.prototype.scale = function(s) {
64 * Compute the dot product: |this| . v.
65 * @param {!tumbler.Vector3} v The vector to dot.
66 * @return {number} the result of |this| . v.
68 tumbler.Vector3.prototype.dot = function(v) {
69 return this.x * v.x + this.y * v.y + this.z * v.z;
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.
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);
85 * Real numbers that are less than this distance apart are considered
87 * TODO(dspringer): It seems as though there should be a const like this
88 * in generally available somewhere.
91 tumbler.Vector3.DOUBLE_EPSILON = 1.0e-16;