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
|
// Copyright 2008 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.
/**
* @fileoverview Defines a 3-element vector class that can be used for
* coordinate math, useful for animation systems and point manipulation.
*
* Based heavily on code originally by:
* @author brenneman@google.com (Shawn Brenneman)
*/
goog.provide('goog.math.Vec3');
goog.require('goog.math');
goog.require('goog.math.Coordinate3');
/**
* Class for a three-dimensional vector object and assorted functions useful for
* manipulation.
*
* Inherits from goog.math.Coordinate3 so that a Vec3 may be passed in to any
* function that requires a Coordinate.
*
* @param {number} x The x value for the vector.
* @param {number} y The y value for the vector.
* @param {number} z The z value for the vector.
* @struct
* @constructor
* @extends {goog.math.Coordinate3}
*/
goog.math.Vec3 = function(x, y, z) {
/**
* X-value
* @type {number}
*/
this.x = x;
/**
* Y-value
* @type {number}
*/
this.y = y;
/**
* Z-value
* @type {number}
*/
this.z = z;
};
goog.inherits(goog.math.Vec3, goog.math.Coordinate3);
/**
* Generates a random unit vector.
*
* http://mathworld.wolfram.com/SpherePointPicking.html
* Using (6), (7), and (8) to generate coordinates.
* @return {!goog.math.Vec3} A random unit-length vector.
*/
goog.math.Vec3.randomUnit = function() {
var theta = Math.random() * Math.PI * 2;
var phi = Math.random() * Math.PI * 2;
var z = Math.cos(phi);
var x = Math.sqrt(1 - z * z) * Math.cos(theta);
var y = Math.sqrt(1 - z * z) * Math.sin(theta);
return new goog.math.Vec3(x, y, z);
};
/**
* Generates a random vector inside the unit sphere.
*
* @return {!goog.math.Vec3} A random vector.
*/
goog.math.Vec3.random = function() {
return goog.math.Vec3.randomUnit().scale(Math.random());
};
/**
* Returns a new Vec3 object from a given coordinate.
*
* @param {goog.math.Coordinate3} a The coordinate.
* @return {!goog.math.Vec3} A new vector object.
*/
goog.math.Vec3.fromCoordinate3 = function(a) {
return new goog.math.Vec3(a.x, a.y, a.z);
};
/**
* Creates a new copy of this Vec3.
*
* @return {!goog.math.Vec3} A new vector with the same coordinates as this one.
* @override
*/
goog.math.Vec3.prototype.clone = function() {
return new goog.math.Vec3(this.x, this.y, this.z);
};
/**
* Returns the magnitude of the vector measured from the origin.
*
* @return {number} The length of the vector.
*/
goog.math.Vec3.prototype.magnitude = function() {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
};
/**
* Returns the squared magnitude of the vector measured from the origin.
* NOTE(brenneman): Leaving out the square root is not a significant
* optimization in JavaScript.
*
* @return {number} The length of the vector, squared.
*/
goog.math.Vec3.prototype.squaredMagnitude = function() {
return this.x * this.x + this.y * this.y + this.z * this.z;
};
/**
* Scales the current vector by a constant.
*
* @param {number} s The scale factor.
* @return {!goog.math.Vec3} This vector, scaled.
*/
goog.math.Vec3.prototype.scale = function(s) {
this.x *= s;
this.y *= s;
this.z *= s;
return this;
};
/**
* Reverses the sign of the vector. Equivalent to scaling the vector by -1.
*
* @return {!goog.math.Vec3} This vector, inverted.
*/
goog.math.Vec3.prototype.invert = function() {
this.x = -this.x;
this.y = -this.y;
this.z = -this.z;
return this;
};
/**
* Normalizes the current vector to have a magnitude of 1.
*
* @return {!goog.math.Vec3} This vector, normalized.
*/
goog.math.Vec3.prototype.normalize = function() {
return this.scale(1 / this.magnitude());
};
/**
* Adds another vector to this vector in-place.
*
* @param {goog.math.Vec3} b The vector to add.
* @return {!goog.math.Vec3} This vector with {@code b} added.
*/
goog.math.Vec3.prototype.add = function(b) {
this.x += b.x;
this.y += b.y;
this.z += b.z;
return this;
};
/**
* Subtracts another vector from this vector in-place.
*
* @param {goog.math.Vec3} b The vector to subtract.
* @return {!goog.math.Vec3} This vector with {@code b} subtracted.
*/
goog.math.Vec3.prototype.subtract = function(b) {
this.x -= b.x;
this.y -= b.y;
this.z -= b.z;
return this;
};
/**
* Compares this vector with another for equality.
*
* @param {goog.math.Vec3} b The other vector.
* @return {boolean} True if this vector's x, y and z equal the given vector's
* x, y, and z, respectively.
*/
goog.math.Vec3.prototype.equals = function(b) {
return this == b || !!b && this.x == b.x && this.y == b.y && this.z == b.z;
};
/**
* Returns the distance between two vectors.
*
* @param {goog.math.Vec3} a The first vector.
* @param {goog.math.Vec3} b The second vector.
* @return {number} The distance.
*/
goog.math.Vec3.distance = goog.math.Coordinate3.distance;
/**
* Returns the squared distance between two vectors.
*
* @param {goog.math.Vec3} a The first vector.
* @param {goog.math.Vec3} b The second vector.
* @return {number} The squared distance.
*/
goog.math.Vec3.squaredDistance = goog.math.Coordinate3.squaredDistance;
/**
* Compares vectors for equality.
*
* @param {goog.math.Vec3} a The first vector.
* @param {goog.math.Vec3} b The second vector.
* @return {boolean} True if the vectors have equal x, y, and z coordinates.
*/
goog.math.Vec3.equals = goog.math.Coordinate3.equals;
/**
* Returns the sum of two vectors as a new Vec3.
*
* @param {goog.math.Vec3} a The first vector.
* @param {goog.math.Vec3} b The second vector.
* @return {!goog.math.Vec3} The sum vector.
*/
goog.math.Vec3.sum = function(a, b) {
return new goog.math.Vec3(a.x + b.x, a.y + b.y, a.z + b.z);
};
/**
* Returns the difference of two vectors as a new Vec3.
*
* @param {goog.math.Vec3} a The first vector.
* @param {goog.math.Vec3} b The second vector.
* @return {!goog.math.Vec3} The difference vector.
*/
goog.math.Vec3.difference = function(a, b) {
return new goog.math.Vec3(a.x - b.x, a.y - b.y, a.z - b.z);
};
/**
* Returns the dot-product of two vectors.
*
* @param {goog.math.Vec3} a The first vector.
* @param {goog.math.Vec3} b The second vector.
* @return {number} The dot-product of the two vectors.
*/
goog.math.Vec3.dot = function(a, b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
};
/**
* Returns the cross-product of two vectors.
*
* @param {goog.math.Vec3} a The first vector.
* @param {goog.math.Vec3} b The second vector.
* @return {!goog.math.Vec3} The cross-product of the two vectors.
*/
goog.math.Vec3.cross = function(a, b) {
return new goog.math.Vec3(
a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
};
/**
* Returns a new Vec3 that is the linear interpolant between vectors a and b at
* scale-value x.
*
* @param {goog.math.Vec3} a Vector a.
* @param {goog.math.Vec3} b Vector b.
* @param {number} x The proportion between a and b.
* @return {!goog.math.Vec3} The interpolated vector.
*/
goog.math.Vec3.lerp = function(a, b, x) {
return new goog.math.Vec3(
goog.math.lerp(a.x, b.x, x), goog.math.lerp(a.y, b.y, x),
goog.math.lerp(a.z, b.z, x));
};
/**
* Returns a new Vec3 that is a copy of the vector a, but rescaled by a factor s
* in all dimensions.
* @param {!goog.math.Vec3} a Vector a.
* @param {number} s Scale factor.
* @return {!goog.math.Vec3} A new rescaled vector.
*/
goog.math.Vec3.rescaled = function(a, s) {
return new goog.math.Vec3(a.x * s, a.y * s, a.z * s);
};
|