Path: blob/trunk/third_party/closure/goog/math/vec3.js
2868 views
// Copyright 2008 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Defines a 3-element vector class that can be used for16* coordinate math, useful for animation systems and point manipulation.17*18* Based heavily on code originally by:19* @author [email protected] (Shawn Brenneman)20*/212223goog.provide('goog.math.Vec3');2425goog.require('goog.math');26goog.require('goog.math.Coordinate3');27282930/**31* Class for a three-dimensional vector object and assorted functions useful for32* manipulation.33*34* Inherits from goog.math.Coordinate3 so that a Vec3 may be passed in to any35* function that requires a Coordinate.36*37* @param {number} x The x value for the vector.38* @param {number} y The y value for the vector.39* @param {number} z The z value for the vector.40* @struct41* @constructor42* @extends {goog.math.Coordinate3}43*/44goog.math.Vec3 = function(x, y, z) {45/**46* X-value47* @type {number}48*/49this.x = x;5051/**52* Y-value53* @type {number}54*/55this.y = y;5657/**58* Z-value59* @type {number}60*/61this.z = z;62};63goog.inherits(goog.math.Vec3, goog.math.Coordinate3);646566/**67* Generates a random unit vector.68*69* http://mathworld.wolfram.com/SpherePointPicking.html70* Using (6), (7), and (8) to generate coordinates.71* @return {!goog.math.Vec3} A random unit-length vector.72*/73goog.math.Vec3.randomUnit = function() {74var theta = Math.random() * Math.PI * 2;75var phi = Math.random() * Math.PI * 2;7677var z = Math.cos(phi);78var x = Math.sqrt(1 - z * z) * Math.cos(theta);79var y = Math.sqrt(1 - z * z) * Math.sin(theta);8081return new goog.math.Vec3(x, y, z);82};838485/**86* Generates a random vector inside the unit sphere.87*88* @return {!goog.math.Vec3} A random vector.89*/90goog.math.Vec3.random = function() {91return goog.math.Vec3.randomUnit().scale(Math.random());92};939495/**96* Returns a new Vec3 object from a given coordinate.97*98* @param {goog.math.Coordinate3} a The coordinate.99* @return {!goog.math.Vec3} A new vector object.100*/101goog.math.Vec3.fromCoordinate3 = function(a) {102return new goog.math.Vec3(a.x, a.y, a.z);103};104105106/**107* Creates a new copy of this Vec3.108*109* @return {!goog.math.Vec3} A new vector with the same coordinates as this one.110* @override111*/112goog.math.Vec3.prototype.clone = function() {113return new goog.math.Vec3(this.x, this.y, this.z);114};115116117/**118* Returns the magnitude of the vector measured from the origin.119*120* @return {number} The length of the vector.121*/122goog.math.Vec3.prototype.magnitude = function() {123return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);124};125126127/**128* Returns the squared magnitude of the vector measured from the origin.129* NOTE(brenneman): Leaving out the square root is not a significant130* optimization in JavaScript.131*132* @return {number} The length of the vector, squared.133*/134goog.math.Vec3.prototype.squaredMagnitude = function() {135return this.x * this.x + this.y * this.y + this.z * this.z;136};137138139/**140* Scales the current vector by a constant.141*142* @param {number} s The scale factor.143* @return {!goog.math.Vec3} This vector, scaled.144*/145goog.math.Vec3.prototype.scale = function(s) {146this.x *= s;147this.y *= s;148this.z *= s;149return this;150};151152153/**154* Reverses the sign of the vector. Equivalent to scaling the vector by -1.155*156* @return {!goog.math.Vec3} This vector, inverted.157*/158goog.math.Vec3.prototype.invert = function() {159this.x = -this.x;160this.y = -this.y;161this.z = -this.z;162return this;163};164165166/**167* Normalizes the current vector to have a magnitude of 1.168*169* @return {!goog.math.Vec3} This vector, normalized.170*/171goog.math.Vec3.prototype.normalize = function() {172return this.scale(1 / this.magnitude());173};174175176/**177* Adds another vector to this vector in-place.178*179* @param {goog.math.Vec3} b The vector to add.180* @return {!goog.math.Vec3} This vector with {@code b} added.181*/182goog.math.Vec3.prototype.add = function(b) {183this.x += b.x;184this.y += b.y;185this.z += b.z;186return this;187};188189190/**191* Subtracts another vector from this vector in-place.192*193* @param {goog.math.Vec3} b The vector to subtract.194* @return {!goog.math.Vec3} This vector with {@code b} subtracted.195*/196goog.math.Vec3.prototype.subtract = function(b) {197this.x -= b.x;198this.y -= b.y;199this.z -= b.z;200return this;201};202203204/**205* Compares this vector with another for equality.206*207* @param {goog.math.Vec3} b The other vector.208* @return {boolean} True if this vector's x, y and z equal the given vector's209* x, y, and z, respectively.210*/211goog.math.Vec3.prototype.equals = function(b) {212return this == b || !!b && this.x == b.x && this.y == b.y && this.z == b.z;213};214215216/**217* Returns the distance between two vectors.218*219* @param {goog.math.Vec3} a The first vector.220* @param {goog.math.Vec3} b The second vector.221* @return {number} The distance.222*/223goog.math.Vec3.distance = goog.math.Coordinate3.distance;224225226/**227* Returns the squared distance between two vectors.228*229* @param {goog.math.Vec3} a The first vector.230* @param {goog.math.Vec3} b The second vector.231* @return {number} The squared distance.232*/233goog.math.Vec3.squaredDistance = goog.math.Coordinate3.squaredDistance;234235236/**237* Compares vectors for equality.238*239* @param {goog.math.Vec3} a The first vector.240* @param {goog.math.Vec3} b The second vector.241* @return {boolean} True if the vectors have equal x, y, and z coordinates.242*/243goog.math.Vec3.equals = goog.math.Coordinate3.equals;244245246/**247* Returns the sum of two vectors as a new Vec3.248*249* @param {goog.math.Vec3} a The first vector.250* @param {goog.math.Vec3} b The second vector.251* @return {!goog.math.Vec3} The sum vector.252*/253goog.math.Vec3.sum = function(a, b) {254return new goog.math.Vec3(a.x + b.x, a.y + b.y, a.z + b.z);255};256257258/**259* Returns the difference of two vectors as a new Vec3.260*261* @param {goog.math.Vec3} a The first vector.262* @param {goog.math.Vec3} b The second vector.263* @return {!goog.math.Vec3} The difference vector.264*/265goog.math.Vec3.difference = function(a, b) {266return new goog.math.Vec3(a.x - b.x, a.y - b.y, a.z - b.z);267};268269270/**271* Returns the dot-product of two vectors.272*273* @param {goog.math.Vec3} a The first vector.274* @param {goog.math.Vec3} b The second vector.275* @return {number} The dot-product of the two vectors.276*/277goog.math.Vec3.dot = function(a, b) {278return a.x * b.x + a.y * b.y + a.z * b.z;279};280281282/**283* Returns the cross-product of two vectors.284*285* @param {goog.math.Vec3} a The first vector.286* @param {goog.math.Vec3} b The second vector.287* @return {!goog.math.Vec3} The cross-product of the two vectors.288*/289goog.math.Vec3.cross = function(a, b) {290return new goog.math.Vec3(291a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);292};293294295/**296* Returns a new Vec3 that is the linear interpolant between vectors a and b at297* scale-value x.298*299* @param {goog.math.Vec3} a Vector a.300* @param {goog.math.Vec3} b Vector b.301* @param {number} x The proportion between a and b.302* @return {!goog.math.Vec3} The interpolated vector.303*/304goog.math.Vec3.lerp = function(a, b, x) {305return new goog.math.Vec3(306goog.math.lerp(a.x, b.x, x), goog.math.lerp(a.y, b.y, x),307goog.math.lerp(a.z, b.z, x));308};309310311