This commit is contained in:
Éric Lemoine
2013-03-11 13:35:17 +01:00
parent 849774dceb
commit f150259eee
1189 changed files with 341774 additions and 2001 deletions
@@ -0,0 +1,110 @@
// Copyright 2011 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 Supplies a Float32Array implementation that implements
* most of the Float32Array spec and that can be used when a built-in
* implementation is not available.
*
* Note that if no existing Float32Array implementation is found then
* this class and all its public properties are exported as Float32Array.
*
* Adding support for the other TypedArray classes here does not make sense
* since this vector math library only needs Float32Array.
*
*/
goog.provide('goog.vec.Float32Array');
/**
* Constructs a new Float32Array. The new array is initialized to all zeros.
*
* @param {goog.vec.Float32Array|Array|ArrayBuffer|number} p0
* The length of the array, or an array to initialize the contents of the
* new Float32Array.
* @constructor
*/
goog.vec.Float32Array = function(p0) {
this.length = p0.length || p0;
for (var i = 0; i < this.length; i++) {
this[i] = p0[i] || 0;
}
};
/**
* The number of bytes in an element (as defined by the Typed Array
* specification).
*
* @type {number}
*/
goog.vec.Float32Array.BYTES_PER_ELEMENT = 4;
/**
* The number of bytes in an element (as defined by the Typed Array
* specification).
*
* @type {number}
*/
goog.vec.Float32Array.prototype.BYTES_PER_ELEMENT = 4;
/**
* Sets elements of the array.
* @param {Array.<number>|Float32Array} values The array of values.
* @param {number=} opt_offset The offset in this array to start.
*/
goog.vec.Float32Array.prototype.set = function(values, opt_offset) {
opt_offset = opt_offset || 0;
for (var i = 0; i < values.length && opt_offset + i < this.length; i++) {
this[opt_offset + i] = values[i];
}
};
/**
* Creates a string representation of this array.
* @return {string} The string version of this array.
* @override
*/
goog.vec.Float32Array.prototype.toString = Array.prototype.join;
/**
* Note that we cannot implement the subarray() or (deprecated) slice()
* methods properly since doing so would require being able to overload
* the [] operator which is not possible in javascript. So we leave
* them unimplemented. Any attempt to call these methods will just result
* in a javascript error since we leave them undefined.
*/
/**
* If no existing Float32Array implementation is found then we export
* goog.vec.Float32Array as Float32Array.
*/
if (typeof Float32Array == 'undefined') {
goog.exportProperty(goog.vec.Float32Array, 'BYTES_PER_ELEMENT',
goog.vec.Float32Array.BYTES_PER_ELEMENT);
goog.exportProperty(goog.vec.Float32Array.prototype, 'BYTES_PER_ELEMENT',
goog.vec.Float32Array.prototype.BYTES_PER_ELEMENT);
goog.exportProperty(goog.vec.Float32Array.prototype, 'set',
goog.vec.Float32Array.prototype.set);
goog.exportProperty(goog.vec.Float32Array.prototype, 'toString',
goog.vec.Float32Array.prototype.toString);
goog.exportSymbol('Float32Array', goog.vec.Float32Array);
}
@@ -0,0 +1,110 @@
// Copyright 2011 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 Supplies a Float64Array implementation that implements
* most of the Float64Array spec and that can be used when a built-in
* implementation is not available.
*
* Note that if no existing Float64Array implementation is found then this
* class and all its public properties are exported as Float64Array.
*
* Adding support for the other TypedArray classes here does not make sense
* since this vector math library only needs Float32Array and Float64Array.
*
*/
goog.provide('goog.vec.Float64Array');
/**
* Constructs a new Float64Array. The new array is initialized to all zeros.
*
* @param {goog.vec.Float64Array|Array|ArrayBuffer|number} p0
* The length of the array, or an array to initialize the contents of the
* new Float64Array.
* @constructor
*/
goog.vec.Float64Array = function(p0) {
this.length = p0.length || p0;
for (var i = 0; i < this.length; i++) {
this[i] = p0[i] || 0;
}
};
/**
* The number of bytes in an element (as defined by the Typed Array
* specification).
*
* @type {number}
*/
goog.vec.Float64Array.BYTES_PER_ELEMENT = 8;
/**
* The number of bytes in an element (as defined by the Typed Array
* specification).
*
* @type {number}
*/
goog.vec.Float64Array.prototype.BYTES_PER_ELEMENT = 8;
/**
* Sets elements of the array.
* @param {Array.<number>|Float64Array} values The array of values.
* @param {number=} opt_offset The offset in this array to start.
*/
goog.vec.Float64Array.prototype.set = function(values, opt_offset) {
opt_offset = opt_offset || 0;
for (var i = 0; i < values.length && opt_offset + i < this.length; i++) {
this[opt_offset + i] = values[i];
}
};
/**
* Creates a string representation of this array.
* @return {string} The string version of this array.
* @override
*/
goog.vec.Float64Array.prototype.toString = Array.prototype.join;
/**
* Note that we cannot implement the subarray() or (deprecated) slice()
* methods properly since doing so would require being able to overload
* the [] operator which is not possible in javascript. So we leave
* them unimplemented. Any attempt to call these methods will just result
* in a javascript error since we leave them undefined.
*/
/**
* If no existing Float64Array implementation is found then we export
* goog.vec.Float64Array as Float64Array.
*/
if (typeof Float64Array == 'undefined') {
goog.exportProperty(goog.vec.Float64Array, 'BYTES_PER_ELEMENT',
goog.vec.Float64Array.BYTES_PER_ELEMENT);
goog.exportProperty(goog.vec.Float64Array.prototype, 'BYTES_PER_ELEMENT',
goog.vec.Float64Array.prototype.BYTES_PER_ELEMENT);
goog.exportProperty(goog.vec.Float64Array.prototype, 'set',
goog.vec.Float64Array.prototype.set);
goog.exportProperty(goog.vec.Float64Array.prototype, 'toString',
goog.vec.Float64Array.prototype.toString);
goog.exportSymbol('Float64Array', goog.vec.Float64Array);
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,722 @@
// Copyright 2011 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 WARNING: DEPRECATED. Use Mat3 instead.
* Implements 3x3 matrices and their related functions which are
* compatible with WebGL. The API is structured to avoid unnecessary memory
* allocations. The last parameter will typically be the output vector and
* an object can be both an input and output parameter to all methods except
* where noted. Matrix operations follow the mathematical form when multiplying
* vectors as follows: resultVec = matrix * vec.
*
*/
goog.provide('goog.vec.Matrix3');
goog.require('goog.vec');
/**
* @typedef {goog.vec.ArrayType}
*/
goog.vec.Matrix3.Type;
/**
* Creates the array representation of a 3x3 matrix. The use of the array
* directly eliminates any overhead associated with the class representation
* defined above. The returned matrix is cleared to all zeros.
*
* @return {goog.vec.Matrix3.Type} The new, nine element array.
*/
goog.vec.Matrix3.create = function() {
return new Float32Array(9);
};
/**
* Creates the array representation of a 3x3 matrix. The use of the array
* directly eliminates any overhead associated with the class representation
* defined above. The returned matrix is initialized with the identity.
*
* @return {goog.vec.Matrix3.Type} The new, nine element array.
*/
goog.vec.Matrix3.createIdentity = function() {
var mat = goog.vec.Matrix3.create();
mat[0] = mat[4] = mat[8] = 1;
return mat;
};
/**
* Creates a 3x3 matrix initialized from the given array.
*
* @param {goog.vec.ArrayType} matrix The array containing the
* matrix values in column major order.
* @return {goog.vec.Matrix3.Type} The new, nine element array.
*/
goog.vec.Matrix3.createFromArray = function(matrix) {
var newMatrix = goog.vec.Matrix3.create();
goog.vec.Matrix3.setFromArray(newMatrix, matrix);
return newMatrix;
};
/**
* Creates a 3x3 matrix initialized from the given values.
*
* @param {number} v00 The values at (0, 0).
* @param {number} v10 The values at (1, 0).
* @param {number} v20 The values at (2, 0).
* @param {number} v01 The values at (0, 1).
* @param {number} v11 The values at (1, 1).
* @param {number} v21 The values at (2, 1).
* @param {number} v02 The values at (0, 2).
* @param {number} v12 The values at (1, 2).
* @param {number} v22 The values at (2, 2).
* @return {goog.vec.Matrix3.Type} The new, nine element array.
*/
goog.vec.Matrix3.createFromValues = function(
v00, v10, v20, v01, v11, v21, v02, v12, v22) {
var newMatrix = goog.vec.Matrix3.create();
goog.vec.Matrix3.setFromValues(
newMatrix, v00, v10, v20, v01, v11, v21, v02, v12, v22);
return newMatrix;
};
/**
* Creates a clone of a 3x3 matrix.
*
* @param {goog.vec.Matrix3.Type} matrix The source 3x3 matrix.
* @return {goog.vec.Matrix3.Type} The new 3x3 element matrix.
*/
goog.vec.Matrix3.clone =
goog.vec.Matrix3.createFromArray;
/**
* Retrieves the element at the requested row and column.
*
* @param {goog.vec.ArrayType} mat The matrix containing the
* value to retrieve.
* @param {number} row The row index.
* @param {number} column The column index.
* @return {number} The element value at the requested row, column indices.
*/
goog.vec.Matrix3.getElement = function(mat, row, column) {
return mat[row + column * 3];
};
/**
* Sets the element at the requested row and column.
*
* @param {goog.vec.ArrayType} mat The matrix containing the
* value to retrieve.
* @param {number} row The row index.
* @param {number} column The column index.
* @param {number} value The value to set at the requested row, column.
*/
goog.vec.Matrix3.setElement = function(mat, row, column, value) {
mat[row + column * 3] = value;
};
/**
* Initializes the matrix from the set of values. Note the values supplied are
* in column major order.
*
* @param {goog.vec.ArrayType} mat The matrix to receive the
* values.
* @param {number} v00 The values at (0, 0).
* @param {number} v10 The values at (1, 0).
* @param {number} v20 The values at (2, 0).
* @param {number} v01 The values at (0, 1).
* @param {number} v11 The values at (1, 1).
* @param {number} v21 The values at (2, 1).
* @param {number} v02 The values at (0, 2).
* @param {number} v12 The values at (1, 2).
* @param {number} v22 The values at (2, 2).
*/
goog.vec.Matrix3.setFromValues = function(
mat, v00, v10, v20, v01, v11, v21, v02, v12, v22) {
mat[0] = v00;
mat[1] = v10;
mat[2] = v20;
mat[3] = v01;
mat[4] = v11;
mat[5] = v21;
mat[6] = v02;
mat[7] = v12;
mat[8] = v22;
};
/**
* Sets the matrix from the array of values stored in column major order.
*
* @param {goog.vec.ArrayType} mat The matrix to receive the
* values.
* @param {goog.vec.ArrayType} values The column major ordered
* array of values to store in the matrix.
*/
goog.vec.Matrix3.setFromArray = function(mat, values) {
mat[0] = values[0];
mat[1] = values[1];
mat[2] = values[2];
mat[3] = values[3];
mat[4] = values[4];
mat[5] = values[5];
mat[6] = values[6];
mat[7] = values[7];
mat[8] = values[8];
};
/**
* Sets the matrix from the array of values stored in row major order.
*
* @param {goog.vec.ArrayType} mat The matrix to receive the
* values.
* @param {goog.vec.ArrayType} values The row major ordered array
* of values to store in the matrix.
*/
goog.vec.Matrix3.setFromRowMajorArray = function(mat, values) {
mat[0] = values[0];
mat[1] = values[3];
mat[2] = values[6];
mat[3] = values[1];
mat[4] = values[4];
mat[5] = values[7];
mat[6] = values[2];
mat[7] = values[5];
mat[8] = values[8];
};
/**
* Sets the diagonal values of the matrix from the given values.
*
* @param {goog.vec.ArrayType} mat The matrix to receive the
* values.
* @param {number} v00 The values for (0, 0).
* @param {number} v11 The values for (1, 1).
* @param {number} v22 The values for (2, 2).
*/
goog.vec.Matrix3.setDiagonalValues = function(mat, v00, v11, v22) {
mat[0] = v00;
mat[4] = v11;
mat[8] = v22;
};
/**
* Sets the diagonal values of the matrix from the given vector.
*
* @param {goog.vec.ArrayType} mat The matrix to receive the
* values.
* @param {goog.vec.ArrayType} vec The vector containing the
* values.
*/
goog.vec.Matrix3.setDiagonal = function(mat, vec) {
mat[0] = vec[0];
mat[4] = vec[1];
mat[8] = vec[2];
};
/**
* Sets the specified column with the supplied values.
*
* @param {goog.vec.ArrayType} mat The matrix to recieve the
* values.
* @param {number} column The column index to set the values on.
* @param {number} v0 The value for row 0.
* @param {number} v1 The value for row 1.
* @param {number} v2 The value for row 2.
*/
goog.vec.Matrix3.setColumnValues = function(
mat, column, v0, v1, v2) {
var i = column * 3;
mat[i] = v0;
mat[i + 1] = v1;
mat[i + 2] = v2;
};
/**
* Sets the specified column with the value from the supplied array.
*
* @param {goog.vec.ArrayType} mat The matrix to receive the
* values.
* @param {number} column The column index to set the values on.
* @param {goog.vec.ArrayType} vec The vector elements for the
* column.
*/
goog.vec.Matrix3.setColumn = function(mat, column, vec) {
var i = column * 3;
mat[i] = vec[0];
mat[i + 1] = vec[1];
mat[i + 2] = vec[2];
};
/**
* Retrieves the specified column from the matrix into the given vector
* array.
*
* @param {goog.vec.ArrayType} mat The matrix supplying the
* values.
* @param {number} column The column to get the values from.
* @param {goog.vec.ArrayType} vec The vector elements to receive
* the column.
*/
goog.vec.Matrix3.getColumn = function(mat, column, vec) {
var i = column * 3;
vec[0] = mat[i];
vec[1] = mat[i + 1];
vec[2] = mat[i + 2];
};
/**
* Sets the columns of the matrix from the set of vector elements.
*
* @param {goog.vec.ArrayType} mat The matrix to receive the
* values.
* @param {goog.vec.ArrayType} vec0 The values for column 0.
* @param {goog.vec.ArrayType} vec1 The values for column 1.
* @param {goog.vec.ArrayType} vec2 The values for column 2.
*/
goog.vec.Matrix3.setColumns = function(
mat, vec0, vec1, vec2) {
goog.vec.Matrix3.setColumn(mat, 0, vec0);
goog.vec.Matrix3.setColumn(mat, 1, vec1);
goog.vec.Matrix3.setColumn(mat, 2, vec2);
};
/**
* Retrieves the column values from the given matrix into the given vector
* elements.
*
* @param {goog.vec.ArrayType} mat The matrix containing the
* columns to retrieve.
* @param {goog.vec.ArrayType} vec0 The vector elements to receive
* column 0.
* @param {goog.vec.ArrayType} vec1 The vector elements to receive
* column 1.
* @param {goog.vec.ArrayType} vec2 The vector elements to receive
* column 2.
*/
goog.vec.Matrix3.getColumns = function(
mat, vec0, vec1, vec2) {
goog.vec.Matrix3.getColumn(mat, 0, vec0);
goog.vec.Matrix3.getColumn(mat, 1, vec1);
goog.vec.Matrix3.getColumn(mat, 2, vec2);
};
/**
* Sets the row values from the supplied values.
*
* @param {goog.vec.ArrayType} mat The matrix to receive the
* values.
* @param {number} row The index of the row to receive the values.
* @param {number} v0 The value for column 0.
* @param {number} v1 The value for column 1.
* @param {number} v2 The value for column 2.
*/
goog.vec.Matrix3.setRowValues = function(mat, row, v0, v1, v2) {
mat[row] = v0;
mat[row + 3] = v1;
mat[row + 6] = v2;
};
/**
* Sets the row values from the supplied vector.
*
* @param {goog.vec.ArrayType} mat The matrix to receive the
* row values.
* @param {number} row The index of the row.
* @param {goog.vec.ArrayType} vec The vector containing the values.
*/
goog.vec.Matrix3.setRow = function(mat, row, vec) {
mat[row] = vec[0];
mat[row + 3] = vec[1];
mat[row + 6] = vec[2];
};
/**
* Retrieves the row values into the given vector.
*
* @param {goog.vec.ArrayType} mat The matrix supplying the
* values.
* @param {number} row The index of the row supplying the values.
* @param {goog.vec.ArrayType} vec The vector to receive the row.
*/
goog.vec.Matrix3.getRow = function(mat, row, vec) {
vec[0] = mat[row];
vec[1] = mat[row + 3];
vec[2] = mat[row + 6];
};
/**
* Sets the rows of the matrix from the supplied vectors.
*
* @param {goog.vec.ArrayType} mat The matrix to receive the
* values.
* @param {goog.vec.ArrayType} vec0 The values for row 0.
* @param {goog.vec.ArrayType} vec1 The values for row 1.
* @param {goog.vec.ArrayType} vec2 The values for row 2.
*/
goog.vec.Matrix3.setRows = function(
mat, vec0, vec1, vec2) {
goog.vec.Matrix3.setRow(mat, 0, vec0);
goog.vec.Matrix3.setRow(mat, 1, vec1);
goog.vec.Matrix3.setRow(mat, 2, vec2);
};
/**
* Retrieves the rows of the matrix into the supplied vectors.
*
* @param {goog.vec.ArrayType} mat The matrix to supplying
* the values.
* @param {goog.vec.ArrayType} vec0 The vector to receive row 0.
* @param {goog.vec.ArrayType} vec1 The vector to receive row 1.
* @param {goog.vec.ArrayType} vec2 The vector to receive row 2.
*/
goog.vec.Matrix3.getRows = function(
mat, vec0, vec1, vec2) {
goog.vec.Matrix3.getRow(mat, 0, vec0);
goog.vec.Matrix3.getRow(mat, 1, vec1);
goog.vec.Matrix3.getRow(mat, 2, vec2);
};
/**
* Clears the given matrix to zero.
*
* @param {goog.vec.ArrayType} mat The matrix to clear.
*/
goog.vec.Matrix3.setZero = function(mat) {
mat[0] = 0;
mat[1] = 0;
mat[2] = 0;
mat[3] = 0;
mat[4] = 0;
mat[5] = 0;
mat[6] = 0;
mat[7] = 0;
mat[8] = 0;
};
/**
* Sets the given matrix to the identity matrix.
*
* @param {goog.vec.ArrayType} mat The matrix to set.
*/
goog.vec.Matrix3.setIdentity = function(mat) {
mat[0] = 1;
mat[1] = 0;
mat[2] = 0;
mat[3] = 0;
mat[4] = 1;
mat[5] = 0;
mat[6] = 0;
mat[7] = 0;
mat[8] = 1;
};
/**
* Performs a per-component addition of the matrices mat0 and mat1, storing
* the result into resultMat.
*
* @param {goog.vec.ArrayType} mat0 The first addend.
* @param {goog.vec.ArrayType} mat1 The second addend.
* @param {goog.vec.ArrayType} resultMat The matrix to
* receive the results (may be either mat0 or mat1).
* @return {goog.vec.ArrayType} return resultMat so that operations can be
* chained together.
*/
goog.vec.Matrix3.add = function(mat0, mat1, resultMat) {
resultMat[0] = mat0[0] + mat1[0];
resultMat[1] = mat0[1] + mat1[1];
resultMat[2] = mat0[2] + mat1[2];
resultMat[3] = mat0[3] + mat1[3];
resultMat[4] = mat0[4] + mat1[4];
resultMat[5] = mat0[5] + mat1[5];
resultMat[6] = mat0[6] + mat1[6];
resultMat[7] = mat0[7] + mat1[7];
resultMat[8] = mat0[8] + mat1[8];
return resultMat;
};
/**
* Performs a per-component subtraction of the matrices mat0 and mat1,
* storing the result into resultMat.
*
* @param {goog.vec.ArrayType} mat0 The minuend.
* @param {goog.vec.ArrayType} mat1 The subtrahend.
* @param {goog.vec.ArrayType} resultMat The matrix to receive
* the results (may be either mat0 or mat1).
* @return {goog.vec.ArrayType} return resultMat so that operations can be
* chained together.
*/
goog.vec.Matrix3.subtract = function(mat0, mat1, resultMat) {
resultMat[0] = mat0[0] - mat1[0];
resultMat[1] = mat0[1] - mat1[1];
resultMat[2] = mat0[2] - mat1[2];
resultMat[3] = mat0[3] - mat1[3];
resultMat[4] = mat0[4] - mat1[4];
resultMat[5] = mat0[5] - mat1[5];
resultMat[6] = mat0[6] - mat1[6];
resultMat[7] = mat0[7] - mat1[7];
resultMat[8] = mat0[8] - mat1[8];
return resultMat;
};
/**
* Performs a component-wise multiplication of mat0 with the given scalar
* storing the result into resultMat.
*
* @param {goog.vec.ArrayType} mat0 The matrix to scale.
* @param {number} scalar The scalar value to multiple to each element of mat0.
* @param {goog.vec.ArrayType} resultMat The matrix to receive
* the results (may be mat0).
* @return {goog.vec.ArrayType} return resultMat so that operations can be
* chained together.
*/
goog.vec.Matrix3.scale = function(mat0, scalar, resultMat) {
resultMat[0] = mat0[0] * scalar;
resultMat[1] = mat0[1] * scalar;
resultMat[2] = mat0[2] * scalar;
resultMat[3] = mat0[3] * scalar;
resultMat[4] = mat0[4] * scalar;
resultMat[5] = mat0[5] * scalar;
resultMat[6] = mat0[6] * scalar;
resultMat[7] = mat0[7] * scalar;
resultMat[8] = mat0[8] * scalar;
return resultMat;
};
/**
* Multiplies the two matrices mat0 and mat1 using matrix multiplication,
* storing the result into resultMat.
*
* @param {goog.vec.ArrayType} mat0 The first (left hand) matrix.
* @param {goog.vec.ArrayType} mat1 The second (right hand)
* matrix.
* @param {goog.vec.ArrayType} resultMat The matrix to receive
* the results (may be either mat0 or mat1).
* @return {goog.vec.ArrayType} return resultMat so that operations can be
* chained together.
*/
goog.vec.Matrix3.multMat = function(mat0, mat1, resultMat) {
var a00 = mat0[0], a10 = mat0[1], a20 = mat0[2];
var a01 = mat0[3], a11 = mat0[4], a21 = mat0[5];
var a02 = mat0[6], a12 = mat0[7], a22 = mat0[8];
var b00 = mat1[0], b10 = mat1[1], b20 = mat1[2];
var b01 = mat1[3], b11 = mat1[4], b21 = mat1[5];
var b02 = mat1[6], b12 = mat1[7], b22 = mat1[8];
resultMat[0] = a00 * b00 + a01 * b10 + a02 * b20;
resultMat[1] = a10 * b00 + a11 * b10 + a12 * b20;
resultMat[2] = a20 * b00 + a21 * b10 + a22 * b20;
resultMat[3] = a00 * b01 + a01 * b11 + a02 * b21;
resultMat[4] = a10 * b01 + a11 * b11 + a12 * b21;
resultMat[5] = a20 * b01 + a21 * b11 + a22 * b21;
resultMat[6] = a00 * b02 + a01 * b12 + a02 * b22;
resultMat[7] = a10 * b02 + a11 * b12 + a12 * b22;
resultMat[8] = a20 * b02 + a21 * b12 + a22 * b22;
return resultMat;
};
/**
* Transposes the given matrix mat storing the result into resultMat.
* @param {goog.vec.ArrayType} mat The matrix to transpose.
* @param {goog.vec.ArrayType} resultMat The matrix to receive
* the results (may be mat).
* @return {goog.vec.ArrayType} return resultMat so that operations can be
* chained together.
*/
goog.vec.Matrix3.transpose = function(mat, resultMat) {
if (resultMat == mat) {
var a10 = mat[1], a20 = mat[2], a21 = mat[5];
resultMat[1] = mat[3];
resultMat[2] = mat[6];
resultMat[3] = a10;
resultMat[5] = mat[7];
resultMat[6] = a20;
resultMat[7] = a21;
} else {
resultMat[0] = mat[0];
resultMat[1] = mat[3];
resultMat[2] = mat[6];
resultMat[3] = mat[1];
resultMat[4] = mat[4];
resultMat[5] = mat[7];
resultMat[6] = mat[2];
resultMat[7] = mat[5];
resultMat[8] = mat[8];
}
return resultMat;
};
/**
* Computes the inverse of mat0 storing the result into resultMat. If the
* inverse is defined, this function returns true, false otherwise.
* @param {goog.vec.ArrayType} mat0 The matrix to invert.
* @param {goog.vec.ArrayType} resultMat The matrix to receive
* the result (may be mat0).
* @return {boolean} True if the inverse is defined. If false is returned,
* resultMat is not modified.
*/
goog.vec.Matrix3.invert = function(mat0, resultMat) {
var a00 = mat0[0], a10 = mat0[1], a20 = mat0[2];
var a01 = mat0[3], a11 = mat0[4], a21 = mat0[5];
var a02 = mat0[6], a12 = mat0[7], a22 = mat0[8];
var t00 = a11 * a22 - a12 * a21;
var t10 = a12 * a20 - a10 * a22;
var t20 = a10 * a21 - a11 * a20;
var det = a00 * t00 + a01 * t10 + a02 * t20;
if (det == 0) {
return false;
}
var idet = 1 / det;
resultMat[0] = t00 * idet;
resultMat[3] = (a02 * a21 - a01 * a22) * idet;
resultMat[6] = (a01 * a12 - a02 * a11) * idet;
resultMat[1] = t10 * idet;
resultMat[4] = (a00 * a22 - a02 * a20) * idet;
resultMat[7] = (a02 * a10 - a00 * a12) * idet;
resultMat[2] = t20 * idet;
resultMat[5] = (a01 * a20 - a00 * a21) * idet;
resultMat[8] = (a00 * a11 - a01 * a10) * idet;
return true;
};
/**
* Returns true if the components of mat0 are equal to the components of mat1.
*
* @param {goog.vec.ArrayType} mat0 The first matrix.
* @param {goog.vec.ArrayType} mat1 The second matrix.
* @return {boolean} True if the the two matrices are equivalent.
*/
goog.vec.Matrix3.equals = function(mat0, mat1) {
return mat0.length == mat1.length &&
mat0[0] == mat1[0] && mat0[1] == mat1[1] && mat0[2] == mat1[2] &&
mat0[3] == mat1[3] && mat0[4] == mat1[4] && mat0[5] == mat1[5] &&
mat0[6] == mat1[6] && mat0[7] == mat1[7] && mat0[8] == mat1[8];
};
/**
* Transforms the given vector with the given matrix storing the resulting,
* transformed matrix into resultVec.
*
* @param {goog.vec.ArrayType} mat The matrix supplying the
* transformation.
* @param {goog.vec.ArrayType} vec The vector to transform.
* @param {goog.vec.ArrayType} resultVec The vector to
* receive the results (may be vec).
* @return {goog.vec.ArrayType} return resultVec so that operations can be
* chained together.
*/
goog.vec.Matrix3.multVec3 = function(mat, vec, resultVec) {
var x = vec[0], y = vec[1], z = vec[2];
resultVec[0] = x * mat[0] + y * mat[3] + z * mat[6];
resultVec[1] = x * mat[1] + y * mat[4] + z * mat[7];
resultVec[2] = x * mat[2] + y * mat[5] + z * mat[8];
return resultVec;
};
/**
* Initializes the given 3x3 matrix as a translation matrix with x and y
* translation values.
*
* @param {goog.vec.ArrayType} mat The 3x3 (9-element) matrix
* array to receive the new translation matrix.
* @param {number} x The translation along the x axis.
* @param {number} y The translation along the y axis.
*/
goog.vec.Matrix3.makeTranslate = function(mat, x, y) {
goog.vec.Matrix3.setIdentity(mat);
goog.vec.Matrix3.setColumnValues(mat, 2, x, y, 1);
};
/**
* Initializes the given 3x3 matrix as a scale matrix with x, y and z scale
* factors.
* @param {goog.vec.ArrayType} mat The 3x3 (9-element) matrix
* array to receive the new scale matrix.
* @param {number} x The scale along the x axis.
* @param {number} y The scale along the y axis.
* @param {number} z The scale along the z axis.
*/
goog.vec.Matrix3.makeScale = function(mat, x, y, z) {
goog.vec.Matrix3.setIdentity(mat);
goog.vec.Matrix3.setDiagonalValues(mat, x, y, z);
};
/**
* Initializes the given 3x3 matrix as a rotation matrix with the given rotation
* angle about the axis defined by the vector (ax, ay, az).
* @param {goog.vec.ArrayType} mat The 3x3 (9-element) matrix
* array to receive the new scale matrix.
* @param {number} angle The rotation angle in radians.
* @param {number} ax The x component of the rotation axis.
* @param {number} ay The y component of the rotation axis.
* @param {number} az The z component of the rotation axis.
*/
goog.vec.Matrix3.makeAxisAngleRotate = function(
mat, angle, ax, ay, az) {
var c = Math.cos(angle);
var d = 1 - c;
var s = Math.sin(angle);
goog.vec.Matrix3.setFromValues(mat,
ax * ax * d + c,
ax * ay * d + az * s,
ax * az * d - ay * s,
ax * ay * d - az * s,
ay * ay * d + c,
ay * az * d + ax * s,
ax * az * d + ay * s,
ay * az * d - ax * s,
az * az * d + c);
};
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,458 @@
// Copyright 2011 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 Implements quaternions and their conversion functions. In this
* implementation, quaternions are represented as 4 element vectors with the
* first 3 elements holding the imaginary components and the 4th element holding
* the real component.
*
*/
goog.provide('goog.vec.Quaternion');
goog.require('goog.vec');
goog.require('goog.vec.Vec3');
goog.require('goog.vec.Vec4');
/** @typedef {goog.vec.Float32} */ goog.vec.Quaternion.Float32;
/** @typedef {goog.vec.Float64} */ goog.vec.Quaternion.Float64;
/** @typedef {goog.vec.Number} */ goog.vec.Quaternion.Number;
/** @typedef {goog.vec.AnyType} */ goog.vec.Quaternion.AnyType;
/**
* Creates a Float32 quaternion, initialized to zero.
*
* @return {!goog.vec.Quaternion.Float32} The new quaternion.
*/
goog.vec.Quaternion.createFloat32 = goog.vec.Vec4.createFloat32;
/**
* Creates a Float64 quaternion, initialized to zero.
*
* @return {goog.vec.Quaternion.Float64} The new quaternion.
*/
goog.vec.Quaternion.createFloat64 = goog.vec.Vec4.createFloat64;
/**
* Creates a Number quaternion, initialized to zero.
*
* @return {goog.vec.Quaternion.Number} The new quaternion.
*/
goog.vec.Quaternion.createNumber = goog.vec.Vec4.createNumber;
/**
* Creates a new Float32 quaternion initialized with the values from the
* supplied array.
*
* @param {goog.vec.AnyType} vec The source 4 element array.
* @return {!goog.vec.Quaternion.Float32} The new quaternion.
*/
goog.vec.Quaternion.createFloat32FromArray =
goog.vec.Vec4.createFloat32FromArray;
/**
* Creates a new Float64 quaternion initialized with the values from the
* supplied array.
*
* @param {goog.vec.AnyType} vec The source 4 element array.
* @return {!goog.vec.Quaternion.Float64} The new quaternion.
*/
goog.vec.Quaternion.createFloat64FromArray =
goog.vec.Vec4.createFloat64FromArray;
/**
* Creates a new Float32 quaternion 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.
* @param {number} v3 The value for element at index 3.
* @return {!goog.vec.Quaternion.Float32} The new quaternion.
*/
goog.vec.Quaternion.createFloat32FromValues =
goog.vec.Vec4.createFloat32FromValues;
/**
* Creates a new Float64 quaternion 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.
* @param {number} v3 The value for element at index 3.
* @return {!goog.vec.Quaternion.Float64} The new quaternion.
*/
goog.vec.Quaternion.createFloat64FromValues =
goog.vec.Vec4.createFloat64FromValues;
/**
* Creates a clone of the given Float32 quaternion.
*
* @param {goog.vec.Quaternion.Float32} q The source quaternion.
* @return {goog.vec.Quaternion.Float32} The new quaternion.
*/
goog.vec.Quaternion.cloneFloat32 = goog.vec.Vec4.cloneFloat32;
/**
* Creates a clone of the given Float64 quaternion.
*
* @param {goog.vec.Quaternion.Float64} q The source quaternion.
* @return {goog.vec.Quaternion.Float64} The new quaternion.
*/
goog.vec.Quaternion.cloneFloat64 = goog.vec.Vec4.cloneFloat64;
/**
* Initializes the quaternion with the given values.
*
* @param {goog.vec.Quaternion.AnyType} q The quaternion 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.
* @param {number} v3 The value for element at index 3.
* @return {!goog.vec.Vec4.AnyType} return q so that operations can be
* chained together.
*/
goog.vec.Quaternion.setFromValues = goog.vec.Vec4.setFromValues;
/**
* Initializes the quaternion with the given array of values.
*
* @param {goog.vec.Quaternion.AnyType} q The quaternion to receive
* the values.
* @param {goog.vec.AnyType} values The array of values.
* @return {!goog.vec.Quaternion.AnyType} return q so that operations can be
* chained together.
*/
goog.vec.Quaternion.setFromArray = goog.vec.Vec4.setFromArray;
/**
* Adds the two quaternions.
*
* @param {goog.vec.Quaternion.AnyType} quat0 The first addend.
* @param {goog.vec.Quaternion.AnyType} quat1 The second addend.
* @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
* receive the result. May be quat0 or quat1.
*/
goog.vec.Quaternion.add = goog.vec.Vec4.add;
/**
* Negates a quaternion, storing the result into resultQuat.
*
* @param {goog.vec.Quaternion.AnyType} quat0 The quaternion to negate.
* @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
* receive the result. May be quat0.
*/
goog.vec.Quaternion.negate = goog.vec.Vec4.negate;
/**
* Multiplies each component of quat0 with scalar storing the product into
* resultVec.
*
* @param {goog.vec.Quaternion.AnyType} quat0 The source quaternion.
* @param {number} scalar The value to multiply with each component of quat0.
* @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
* receive the result. May be quat0.
*/
goog.vec.Quaternion.scale = goog.vec.Vec4.scale;
/**
* Returns the square magnitude of the given quaternion.
*
* @param {goog.vec.Quaternion.AnyType} quat0 The quaternion.
* @return {number} The magnitude of the quaternion.
*/
goog.vec.Quaternion.magnitudeSquared =
goog.vec.Vec4.magnitudeSquared;
/**
* Returns the magnitude of the given quaternion.
*
* @param {goog.vec.Quaternion.AnyType} quat0 The quaternion.
* @return {number} The magnitude of the quaternion.
*/
goog.vec.Quaternion.magnitude =
goog.vec.Vec4.magnitude;
/**
* Normalizes the given quaternion storing the result into resultVec.
*
* @param {goog.vec.Quaternion.AnyType} quat0 The quaternion to
* normalize.
* @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
* receive the result. May be quat0.
*/
goog.vec.Quaternion.normalize = goog.vec.Vec4.normalize;
/**
* Computes the dot (scalar) product of two quaternions.
*
* @param {goog.vec.Quaternion.AnyType} q0 The first quaternion.
* @param {goog.vec.Quaternion.AnyType} q1 The second quaternion.
* @return {number} The scalar product.
*/
goog.vec.Quaternion.dot = goog.vec.Vec4.dot;
/**
* Computes the conjugate of the quaternion in quat storing the result into
* resultQuat.
*
* @param {goog.vec.Quaternion.AnyType} quat The source quaternion.
* @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
* receive the result.
* @return {!goog.vec.Quaternion.AnyType} Return q so that
* operations can be chained together.
*/
goog.vec.Quaternion.conjugate = function(quat, resultQuat) {
resultQuat[0] = -quat[0];
resultQuat[1] = -quat[1];
resultQuat[2] = -quat[2];
resultQuat[3] = quat[3];
return resultQuat;
};
/**
* Concatenates the two quaternions storing the result into resultQuat.
*
* @param {goog.vec.Quaternion.AnyType} quat0 The first quaternion.
* @param {goog.vec.Quaternion.AnyType} quat1 The second quaternion.
* @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
* receive the result.
* @return {!goog.vec.Quaternion.AnyType} Return q so that
* operations can be chained together.
*/
goog.vec.Quaternion.concat = function(quat0, quat1, resultQuat) {
var x0 = quat0[0], y0 = quat0[1], z0 = quat0[2], w0 = quat0[3];
var x1 = quat1[0], y1 = quat1[1], z1 = quat1[2], w1 = quat1[3];
resultQuat[0] = w0 * x1 + x0 * w1 + y0 * z1 - z0 * y1;
resultQuat[1] = w0 * y1 - x0 * z1 + y0 * w1 + z0 * x1;
resultQuat[2] = w0 * z1 + x0 * y1 - y0 * x1 + z0 * w1;
resultQuat[3] = w0 * w1 - x0 * x1 - y0 * y1 - z0 * z1;
return resultQuat;
};
/**
* Generates a unit quaternion from the given angle-axis rotation pair.
* The rotation axis is not required to be a unit vector, but should
* have non-zero length. The angle should be specified in radians.
*
* @param {number} angle The angle (in radians) to rotate about the axis.
* @param {goog.vec.Quaternion.AnyType} axis Unit vector specifying the
* axis of rotation.
* @param {goog.vec.Quaternion.AnyType} quat Unit quaternion to store the
* result.
* @return {goog.vec.Quaternion.AnyType} Return q so that
* operations can be chained together.
*/
goog.vec.Quaternion.fromAngleAxis = function(angle, axis, quat) {
// Normalize the axis of rotation.
goog.vec.Vec3.normalize(axis, axis);
var halfAngle = 0.5 * angle;
var sin = Math.sin(halfAngle);
goog.vec.Quaternion.setFromValues(
quat, sin * axis[0], sin * axis[1], sin * axis[2], Math.cos(halfAngle));
// Normalize the resulting quaternion.
goog.vec.Quaternion.normalize(quat, quat);
return quat;
};
/**
* Generates an angle-axis rotation pair from a unit quaternion.
* The quaternion is assumed to be of unit length. The calculated
* values are returned via the passed 'axis' object and the 'angle'
* number returned by the function itself. The returned rotation axis
* is a non-zero length unit vector, and the returned angle is in
* radians in the range of [-PI, +PI].
*
* @param {goog.vec.Quaternion.AnyType} quat Unit quaternion to convert.
* @param {goog.vec.Quaternion.AnyType} axis Vector to store the returned
* rotation axis.
* @return {number} angle Angle (in radians) to rotate about 'axis'.
* The range of the returned angle is [-PI, +PI].
*/
goog.vec.Quaternion.toAngleAxis = function(quat, axis) {
var angle = 2 * Math.acos(quat[3]);
var magnitude = Math.min(Math.max(1 - quat[3] * quat[3], 0), 1);
if (magnitude < goog.vec.EPSILON) {
// This is nearly an identity rotation, so just use a fixed +X axis.
goog.vec.Vec3.setFromValues(axis, 1, 0, 0);
} else {
// Compute the proper rotation axis.
goog.vec.Vec3.setFromValues(axis, quat[0], quat[1], quat[2]);
// Make sure the rotation axis is of unit length.
goog.vec.Vec3.normalize(axis, axis);
}
// Adjust the range of the returned angle to [-PI, +PI].
if (angle > Math.PI) {
angle -= 2 * Math.PI;
}
return angle;
};
/**
* Generates the quaternion from the given rotation matrix.
*
* @param {goog.vec.Quaternion.AnyType} matrix The source matrix.
* @param {goog.vec.Quaternion.AnyType} quat The resulting quaternion.
* @return {!goog.vec.Quaternion.AnyType} Return q so that
* operations can be chained together.
*/
goog.vec.Quaternion.fromRotationMatrix4 = function(matrix, quat) {
var sx = matrix[0], sy = matrix[5], sz = matrix[10];
quat[3] = Math.sqrt(Math.max(0, 1 + sx + sy + sz)) / 2;
quat[0] = Math.sqrt(Math.max(0, 1 + sx - sy - sz)) / 2;
quat[1] = Math.sqrt(Math.max(0, 1 - sx + sy - sz)) / 2;
quat[2] = Math.sqrt(Math.max(0, 1 - sx - sy + sz)) / 2;
quat[0] = (matrix[6] - matrix[9] < 0) != (quat[0] < 0) ? -quat[0] : quat[0];
quat[1] = (matrix[8] - matrix[2] < 0) != (quat[1] < 0) ? -quat[1] : quat[1];
quat[2] = (matrix[1] - matrix[4] < 0) != (quat[2] < 0) ? -quat[2] : quat[2];
return quat;
};
/**
* Generates the rotation matrix from the given quaternion.
*
* @param {goog.vec.Quaternion.AnyType} quat The source quaternion.
* @param {goog.vec.AnyType} matrix The resulting matrix.
* @return {!goog.vec.AnyType} Return resulting matrix so that
* operations can be chained together.
*/
goog.vec.Quaternion.toRotationMatrix4 = function(quat, matrix) {
var x = quat[0], y = quat[1], z = quat[2], w = quat[3];
var x2 = 2 * x, y2 = 2 * y, z2 = 2 * z;
var wx = x2 * w;
var wy = y2 * w;
var wz = z2 * w;
var xx = x2 * x;
var xy = y2 * x;
var xz = z2 * x;
var yy = y2 * y;
var yz = z2 * y;
var zz = z2 * z;
matrix[0] = 1 - (yy + zz);
matrix[1] = xy + wz;
matrix[2] = xz - wy;
matrix[3] = 0;
matrix[4] = xy - wz;
matrix[5] = 1 - (xx + zz);
matrix[6] = yz + wx;
matrix[7] = 0;
matrix[8] = xz + wy;
matrix[9] = yz - wx;
matrix[10] = 1 - (xx + yy);
matrix[11] = 0;
matrix[12] = 0;
matrix[13] = 0;
matrix[14] = 0;
matrix[15] = 1;
return matrix;
};
/**
* Computes the spherical linear interpolated value from the given quaternions
* q0 and q1 according to the coefficient t. The resulting quaternion is stored
* in resultQuat.
*
* @param {goog.vec.Quaternion.AnyType} q0 The first quaternion.
* @param {goog.vec.Quaternion.AnyType} q1 The second quaternion.
* @param {number} t The interpolating coefficient.
* @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
* receive the result.
* @return {goog.vec.Quaternion.AnyType} Return q so that
* operations can be chained together.
*/
goog.vec.Quaternion.slerp = function(q0, q1, t, resultQuat) {
// Compute the dot product between q0 and q1 (cos of the angle between q0 and
// q1). If it's outside the interval [-1,1], then the arccos is not defined.
// The usual reason for this is that q0 and q1 are colinear. In this case
// the angle between the two is zero, so just return q1.
var cosVal = goog.vec.Quaternion.dot(q0, q1);
if (cosVal > 1 || cosVal < -1) {
goog.vec.Vec4.setFromArray(resultQuat, q1);
return resultQuat;
}
// Quaternions are a double cover on the space of rotations. That is, q and -q
// represent the same rotation. Thus we have two possibilities when
// interpolating between q0 and q1: going the short way or the long way. We
// prefer the short way since that is the likely expectation from users.
var factor = 1;
if (cosVal < 0) {
factor = -1;
cosVal = -cosVal;
}
// Compute the angle between q0 and q1. If it's very small, then just return
// q1 to avoid a very large denominator below.
var angle = Math.acos(cosVal);
if (angle <= goog.vec.EPSILON) {
goog.vec.Vec4.setFromArray(resultQuat, q1);
return resultQuat;
}
// Compute the coefficients and interpolate.
var invSinVal = 1 / Math.sin(angle);
var c0 = Math.sin((1 - t) * angle) * invSinVal;
var c1 = factor * Math.sin(t * angle) * invSinVal;
resultQuat[0] = q0[0] * c0 + q1[0] * c1;
resultQuat[1] = q0[1] * c0 + q1[1] * c1;
resultQuat[2] = q0[2] * c0 + q1[2] * c1;
resultQuat[3] = q0[3] * c0 + q1[3] * c1;
return resultQuat;
};
/**
* Compute the simple linear interpolation of the two quaternions q0 and q1
* according to the coefficient t. The resulting quaternion is stored in
* resultVec.
*
* @param {goog.vec.Quaternion.AnyType} q0 The first quaternion.
* @param {goog.vec.Quaternion.AnyType} q1 The second quaternion.
* @param {number} t The interpolation factor.
* @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
* receive the results (may be q0 or q1).
*/
goog.vec.Quaternion.nlerp = goog.vec.Vec4.lerp;
@@ -0,0 +1,94 @@
// Copyright 2011 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 Implements a 3D ray that are compatible with WebGL.
* Each element is a float64 in case high precision is required.
* The API is structured to avoid unnecessary memory allocations.
* The last parameter will typically be the output vector and an
* object can be both an input and output parameter to all methods
* except where noted.
*
*/
goog.provide('goog.vec.Ray');
goog.require('goog.vec.Vec3');
/**
* Constructs a new ray with an optional origin and direction. If not specified,
* the default is [0, 0, 0].
* @param {goog.vec.Vec3.AnyType=} opt_origin The optional origin.
* @param {goog.vec.Vec3.AnyType=} opt_dir The optional direction.
* @constructor
*/
goog.vec.Ray = function(opt_origin, opt_dir) {
/**
* @type {goog.vec.Vec3.Number}
*/
this.origin = goog.vec.Vec3.createNumber();
if (opt_origin) {
goog.vec.Vec3.setFromArray(this.origin, opt_origin);
}
/**
* @type {goog.vec.Vec3.Number}
*/
this.dir = goog.vec.Vec3.createNumber();
if (opt_dir) {
goog.vec.Vec3.setFromArray(this.dir, opt_dir);
}
};
/**
* Sets the origin and direction of the ray.
* @param {goog.vec.AnyType} origin The new origin.
* @param {goog.vec.AnyType} dir The new direction.
*/
goog.vec.Ray.prototype.set = function(origin, dir) {
goog.vec.Vec3.setFromArray(this.origin, origin);
goog.vec.Vec3.setFromArray(this.dir, dir);
};
/**
* Sets the origin of the ray.
* @param {goog.vec.AnyType} origin the new origin.
*/
goog.vec.Ray.prototype.setOrigin = function(origin) {
goog.vec.Vec3.setFromArray(this.origin, origin);
};
/**
* Sets the direction of the ray.
* @param {goog.vec.AnyType} dir The new direction.
*/
goog.vec.Ray.prototype.setDir = function(dir) {
goog.vec.Vec3.setFromArray(this.dir, dir);
};
/**
* Returns true if this ray is equal to the other ray.
* @param {goog.vec.Ray} other The other ray.
* @return {boolean} True if this ray is equal to the other ray.
*/
goog.vec.Ray.prototype.equals = function(other) {
return other != null &&
goog.vec.Vec3.equals(this.origin, other.origin) &&
goog.vec.Vec3.equals(this.dir, other.dir);
};
@@ -0,0 +1,65 @@
// Copyright 2011 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 Supplies global data types and constants for the vector math
* library.
*/
goog.provide('goog.vec');
/**
* On platforms that don't have native Float32Array or Float64Array support we
* use a javascript implementation so that this math library can be used on all
* platforms.
*/
goog.require('goog.vec.Float32Array');
goog.require('goog.vec.Float64Array');
// All vector and matrix operations are based upon arrays of numbers using
// either Float32Array, Float64Array, or a standard Javascript Array of
// Numbers.
/** @typedef {Float32Array} */
goog.vec.Float32;
/** @typedef {Float64Array} */
goog.vec.Float64;
/** @typedef {Array.<number>} */
goog.vec.Number;
/** @typedef {goog.vec.Float32|goog.vec.Float64|goog.vec.Number} */
goog.vec.AnyType;
/**
* @deprecated Use AnyType.
* @typedef {Float32Array|Array.<number>}
*/
goog.vec.ArrayType;
/**
* For graphics work, 6 decimal places of accuracy are typically all that is
* required.
*
* @type {number}
* @const
*/
goog.vec.EPSILON = 1e-6;
@@ -0,0 +1,375 @@
// Copyright 2012 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 Definition of 2 element vectors. This follows the same design
* patterns as Vec3 and Vec4.
*
*/
goog.provide('goog.vec.Vec2');
goog.require('goog.vec');
/** @typedef {goog.vec.Float32} */ goog.vec.Vec2.Float32;
/** @typedef {goog.vec.Float64} */ goog.vec.Vec2.Float64;
/** @typedef {goog.vec.Number} */ goog.vec.Vec2.Number;
/** @typedef {goog.vec.AnyType} */ goog.vec.Vec2.AnyType;
/**
* Creates a 2 element vector of Float32. The array is initialized to zero.
*
* @return {!goog.vec.Vec2.Float32} The new 2 element array.
*/
goog.vec.Vec2.createFloat32 = function() {
return new Float32Array(2);
};
/**
* Creates a 2 element vector of Float64. The array is initialized to zero.
*
* @return {!goog.vec.Vec2.Float64} The new 2 element array.
*/
goog.vec.Vec2.createFloat64 = function() {
return new Float64Array(2);
};
/**
* Creates a 2 element vector of Number. The array is initialized to zero.
*
* @return {!goog.vec.Vec2.Number} The new 2 element array.
*/
goog.vec.Vec2.createNumber = function() {
var a = new Array(2);
goog.vec.Vec2.setFromValues(a, 0, 0);
return a;
};
/**
* Creates a new 2 element FLoat32 vector initialized with the value from the
* given array.
*
* @param {goog.vec.Vec2.AnyType} vec The source 2 element array.
* @return {!goog.vec.Vec2.Float32} The new 2 element array.
*/
goog.vec.Vec2.createFloat32FromArray = function(vec) {
var newVec = goog.vec.Vec2.createFloat32();
goog.vec.Vec2.setFromArray(newVec, vec);
return newVec;
};
/**
* Creates a new 2 element Float32 vector initialized with the supplied values.
*
* @param {number} vec0 The value for element at index 0.
* @param {number} vec1 The value for element at index 1.
* @return {!goog.vec.Vec2.Float32} The new vector.
*/
goog.vec.Vec2.createFloat32FromValues = function(vec0, vec1) {
var a = goog.vec.Vec2.createFloat32();
goog.vec.Vec2.setFromValues(a, vec0, vec1);
return a;
};
/**
* Creates a clone of the given 2 element Float32 vector.
*
* @param {goog.vec.Vec2.Float32} vec The source 2 element vector.
* @return {!goog.vec.Vec2.Float32} The new cloned vector.
*/
goog.vec.Vec2.cloneFloat32 = goog.vec.Vec2.createFloat32FromArray;
/**
* Creates a new 2 element Float64 vector initialized with the value from the
* given array.
*
* @param {goog.vec.Vec2.AnyType} vec The source 2 element array.
* @return {!goog.vec.Vec2.Float64} The new 2 element array.
*/
goog.vec.Vec2.createFloat64FromArray = function(vec) {
var newVec = goog.vec.Vec2.createFloat64();
goog.vec.Vec2.setFromArray(newVec, vec);
return newVec;
};
/**
* Creates a new 2 element Float64 vector initialized with the supplied values.
*
* @param {number} vec0 The value for element at index 0.
* @param {number} vec1 The value for element at index 1.
* @return {!goog.vec.Vec2.Float64} The new vector.
*/
goog.vec.Vec2.createFloat64FromValues = function(vec0, vec1) {
var vec = goog.vec.Vec2.createFloat64();
goog.vec.Vec2.setFromValues(vec, vec0, vec1);
return vec;
};
/**
* Creates a clone of the given 2 element vector.
*
* @param {goog.vec.Vec2.Float64} vec The source 2 element vector.
* @return {!goog.vec.Vec2.Float64} The new cloned vector.
*/
goog.vec.Vec2.cloneFloat64 = goog.vec.Vec2.createFloat64FromArray;
/**
* Initializes the vector with the given values.
*
* @param {goog.vec.Vec2.AnyType} vec The vector to receive the values.
* @param {number} vec0 The value for element at index 0.
* @param {number} vec1 The value for element at index 1.
* @return {!goog.vec.Vec2.AnyType} Return vec so that operations can be
* chained together.
*/
goog.vec.Vec2.setFromValues = function(vec, vec0, vec1) {
vec[0] = vec0;
vec[1] = vec1;
return vec;
};
/**
* Initializes the vector with the given array of values.
*
* @param {goog.vec.Vec2.AnyType} vec The vector to receive the
* values.
* @param {goog.vec.Vec2.AnyType} values The array of values.
* @return {!goog.vec.Vec2.AnyType} Return vec so that operations can be
* chained together.
*/
goog.vec.Vec2.setFromArray = function(vec, values) {
vec[0] = values[0];
vec[1] = values[1];
return vec;
};
/**
* Performs a component-wise addition of vec0 and vec1 together storing the
* result into resultVec.
*
* @param {goog.vec.Vec2.AnyType} vec0 The first addend.
* @param {goog.vec.Vec2.AnyType} vec1 The second addend.
* @param {goog.vec.Vec2.AnyType} resultVec The vector to
* receive the result. May be vec0 or vec1.
* @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
* chained together.
*/
goog.vec.Vec2.add = function(vec0, vec1, resultVec) {
resultVec[0] = vec0[0] + vec1[0];
resultVec[1] = vec0[1] + vec1[1];
return resultVec;
};
/**
* Performs a component-wise subtraction of vec1 from vec0 storing the
* result into resultVec.
*
* @param {goog.vec.Vec2.AnyType} vec0 The minuend.
* @param {goog.vec.Vec2.AnyType} vec1 The subtrahend.
* @param {goog.vec.Vec2.AnyType} resultVec The vector to
* receive the result. May be vec0 or vec1.
* @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
* chained together.
*/
goog.vec.Vec2.subtract = function(vec0, vec1, resultVec) {
resultVec[0] = vec0[0] - vec1[0];
resultVec[1] = vec0[1] - vec1[1];
return resultVec;
};
/**
* Negates vec0, storing the result into resultVec.
*
* @param {goog.vec.Vec2.AnyType} vec0 The vector to negate.
* @param {goog.vec.Vec2.AnyType} resultVec The vector to
* receive the result. May be vec0.
* @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
* chained together.
*/
goog.vec.Vec2.negate = function(vec0, resultVec) {
resultVec[0] = -vec0[0];
resultVec[1] = -vec0[1];
return resultVec;
};
/**
* Multiplies each component of vec0 with scalar storing the product into
* resultVec.
*
* @param {goog.vec.Vec2.AnyType} vec0 The source vector.
* @param {number} scalar The value to multiply with each component of vec0.
* @param {goog.vec.Vec2.AnyType} resultVec The vector to
* receive the result. May be vec0.
* @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
* chained together.
*/
goog.vec.Vec2.scale = function(vec0, scalar, resultVec) {
resultVec[0] = vec0[0] * scalar;
resultVec[1] = vec0[1] * scalar;
return resultVec;
};
/**
* Returns the magnitudeSquared of the given vector.
*
* @param {goog.vec.Vec2.AnyType} vec0 The vector.
* @return {number} The magnitude of the vector.
*/
goog.vec.Vec2.magnitudeSquared = function(vec0) {
var x = vec0[0], y = vec0[1];
return x * x + y * y;
};
/**
* Returns the magnitude of the given vector.
*
* @param {goog.vec.Vec2.AnyType} vec0 The vector.
* @return {number} The magnitude of the vector.
*/
goog.vec.Vec2.magnitude = function(vec0) {
var x = vec0[0], y = vec0[1];
return Math.sqrt(x * x + y * y);
};
/**
* Normalizes the given vector storing the result into resultVec.
*
* @param {goog.vec.Vec2.AnyType} vec0 The vector to normalize.
* @param {goog.vec.Vec2.AnyType} resultVec The vector to
* receive the result. May be vec0.
* @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
* chained together.
*/
goog.vec.Vec2.normalize = function(vec0, resultVec) {
var ilen = 1 / goog.vec.Vec2.magnitude(vec0);
resultVec[0] = vec0[0] * ilen;
resultVec[1] = vec0[1] * ilen;
return resultVec;
};
/**
* Returns the scalar product of vectors vec0 and vec1.
*
* @param {goog.vec.Vec2.AnyType} vec0 The first vector.
* @param {goog.vec.Vec2.AnyType} vec1 The second vector.
* @return {number} The scalar product.
*/
goog.vec.Vec2.dot = function(vec0, vec1) {
return vec0[0] * vec1[0] + vec0[1] * vec1[1];
};
/**
* Returns the squared distance between two points.
*
* @param {goog.vec.Vec2.AnyType} vec0 First point.
* @param {goog.vec.Vec2.AnyType} vec1 Second point.
* @return {number} The squared distance between the points.
*/
goog.vec.Vec2.distanceSquared = function(vec0, vec1) {
var x = vec0[0] - vec1[0];
var y = vec0[1] - vec1[1];
return x * x + y * y;
};
/**
* Returns the distance between two points.
*
* @param {goog.vec.Vec2.AnyType} vec0 First point.
* @param {goog.vec.Vec2.AnyType} vec1 Second point.
* @return {number} The distance between the points.
*/
goog.vec.Vec2.distance = function(vec0, vec1) {
return Math.sqrt(goog.vec.Vec2.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.Vec2.AnyType} vec0 Origin point.
* @param {goog.vec.Vec2.AnyType} vec1 Target point.
* @param {goog.vec.Vec2.AnyType} resultVec The vector to receive the
* results (may be vec0 or vec1).
* @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
* chained together.
*/
goog.vec.Vec2.direction = function(vec0, vec1, resultVec) {
var x = vec1[0] - vec0[0];
var y = vec1[1] - vec0[1];
var d = Math.sqrt(x * x + y * y);
if (d) {
d = 1 / d;
resultVec[0] = x * d;
resultVec[1] = y * d;
} else {
resultVec[0] = resultVec[1] = 0;
}
return resultVec;
};
/**
* Linearly interpolate from vec0 to vec1 according to f. The value of f should
* be in the range [0..1] otherwise the results are undefined.
*
* @param {goog.vec.Vec2.AnyType} vec0 The first vector.
* @param {goog.vec.Vec2.AnyType} vec1 The second vector.
* @param {number} f The interpolation factor.
* @param {goog.vec.Vec2.AnyType} resultVec The vector to receive the
* results (may be vec0 or vec1).
* @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
* chained together.
*/
goog.vec.Vec2.lerp = function(vec0, vec1, f, resultVec) {
var x = vec0[0], y = vec0[1];
resultVec[0] = (vec1[0] - x) * f + x;
resultVec[1] = (vec1[1] - y) * f + y;
return resultVec;
};
/**
* Returns true if the components of vec0 are equal to the components of vec1.
*
* @param {goog.vec.Vec2.AnyType} vec0 The first vector.
* @param {goog.vec.Vec2.AnyType} vec1 The second vector.
* @return {boolean} True if the vectors are equal, false otherwise.
*/
goog.vec.Vec2.equals = function(vec0, vec1) {
return vec0.length == vec1.length &&
vec0[0] == vec1[0] && vec0[1] == vec1[1];
};
@@ -0,0 +1,473 @@
// Copyright 2011 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 Supplies 3 element vectors that are compatible with WebGL.
* Each element is a float32 since that is typically the desired size of a
* 3-vector in the GPU. The API is structured to avoid unnecessary memory
* allocations. The last parameter will typically be the output vector and
* an object can be both an input and output parameter to all methods except
* where noted.
*
*/
goog.provide('goog.vec.Vec3');
goog.require('goog.vec');
/** @typedef {goog.vec.Float32} */ goog.vec.Vec3.Float32;
/** @typedef {goog.vec.Float64} */ goog.vec.Vec3.Float64;
/** @typedef {goog.vec.Number} */ goog.vec.Vec3.Number;
/** @typedef {goog.vec.AnyType} */ goog.vec.Vec3.AnyType;
// The following two types are deprecated - use the above types instead.
/** @typedef {Float32Array} */ goog.vec.Vec3.Type;
/** @typedef {goog.vec.ArrayType} */ goog.vec.Vec3.Vec3Like;
/**
* Creates a 3 element vector of Float32. The array is initialized to zero.
*
* @return {!goog.vec.Vec3.Float32} The new 3 element array.
*/
goog.vec.Vec3.createFloat32 = function() {
return new Float32Array(3);
};
/**
* Creates a 3 element vector of Float64. The array is initialized to zero.
*
* @return {!goog.vec.Vec3.Float64} The new 3 element array.
*/
goog.vec.Vec3.createFloat64 = function() {
return new Float64Array(3);
};
/**
* Creates a 3 element vector of Number. The array is initialized to zero.
*
* @return {!goog.vec.Vec3.Number} The new 3 element array.
*/
goog.vec.Vec3.createNumber = function() {
var a = new Array(3);
goog.vec.Vec3.setFromValues(a, 0, 0, 0);
return a;
};
/**
* Creates a 3 element vector of Float32Array. The array is initialized to zero.
*
* @deprecated Use createFloat32.
* @return {!goog.vec.Vec3.Type} The new 3 element array.
*/
goog.vec.Vec3.create = function() {
return new Float32Array(3);
};
/**
* Creates a new 3 element FLoat32 vector initialized with the value from the
* given array.
*
* @param {goog.vec.Vec3.AnyType} vec The source 3 element array.
* @return {!goog.vec.Vec3.Float32} The new 3 element array.
*/
goog.vec.Vec3.createFloat32FromArray = function(vec) {
var newVec = goog.vec.Vec3.createFloat32();
goog.vec.Vec3.setFromArray(newVec, vec);
return newVec;
};
/**
* Creates a new 3 element Float32 vector 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.Vec3.Float32} The new vector.
*/
goog.vec.Vec3.createFloat32FromValues = function(v0, v1, v2) {
var a = goog.vec.Vec3.createFloat32();
goog.vec.Vec3.setFromValues(a, v0, v1, v2);
return a;
};
/**
* Creates a clone of the given 3 element Float32 vector.
*
* @param {goog.vec.Vec3.Float32} vec The source 3 element vector.
* @return {!goog.vec.Vec3.Float32} The new cloned vector.
*/
goog.vec.Vec3.cloneFloat32 = goog.vec.Vec3.createFloat32FromArray;
/**
* Creates a new 3 element Float64 vector initialized with the value from the
* given array.
*
* @param {goog.vec.Vec3.AnyType} vec The source 3 element array.
* @return {!goog.vec.Vec3.Float64} The new 3 element array.
*/
goog.vec.Vec3.createFloat64FromArray = function(vec) {
var newVec = goog.vec.Vec3.createFloat64();
goog.vec.Vec3.setFromArray(newVec, vec);
return newVec;
};
/**
* Creates a new 3 element Float64 vector 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.Vec3.Float64} The new vector.
*/
goog.vec.Vec3.createFloat64FromValues = function(v0, v1, v2) {
var vec = goog.vec.Vec3.createFloat64();
goog.vec.Vec3.setFromValues(vec, v0, v1, v2);
return vec;
};
/**
* Creates a clone of the given 3 element vector.
*
* @param {goog.vec.Vec3.Float64} vec The source 3 element vector.
* @return {!goog.vec.Vec3.Float64} The new cloned vector.
*/
goog.vec.Vec3.cloneFloat64 = goog.vec.Vec3.createFloat64FromArray;
/**
* Creates a new 3 element vector initialized with the value from the given
* array.
*
* @deprecated Use createFloat32FromArray.
* @param {goog.vec.Vec3.Vec3Like} vec The source 3 element array.
* @return {!goog.vec.Vec3.Type} The new 3 element array.
*/
goog.vec.Vec3.createFromArray = function(vec) {
var newVec = goog.vec.Vec3.create();
goog.vec.Vec3.setFromArray(newVec, vec);
return newVec;
};
/**
* Creates a new 3 element vector initialized with the supplied values.
*
* @deprecated Use createFloat32FromValues.
* @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.Vec3.Type} The new vector.
*/
goog.vec.Vec3.createFromValues = function(v0, v1, v2) {
var vec = goog.vec.Vec3.create();
goog.vec.Vec3.setFromValues(vec, v0, v1, v2);
return vec;
};
/**
* Creates a clone of the given 3 element vector.
*
* @deprecated Use cloneFloat32.
* @param {goog.vec.Vec3.Vec3Like} vec The source 3 element vector.
* @return {!goog.vec.Vec3.Type} The new cloned vector.
*/
goog.vec.Vec3.clone = function(vec) {
var newVec = goog.vec.Vec3.create();
goog.vec.Vec3.setFromArray(newVec, vec);
return newVec;
};
/**
* Initializes the vector with the given values.
*
* @param {goog.vec.Vec3.AnyType} 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.Vec3.AnyType} return vec so that operations can be
* chained together.
*/
goog.vec.Vec3.setFromValues = function(vec, v0, v1, v2) {
vec[0] = v0;
vec[1] = v1;
vec[2] = v2;
return vec;
};
/**
* Initializes the vector with the given array of values.
*
* @param {goog.vec.Vec3.AnyType} vec The vector to receive the
* values.
* @param {goog.vec.Vec3.AnyType} values The array of values.
* @return {!goog.vec.Vec3.AnyType} return vec so that operations can be
* chained together.
*/
goog.vec.Vec3.setFromArray = function(vec, values) {
vec[0] = values[0];
vec[1] = values[1];
vec[2] = values[2];
return vec;
};
/**
* Performs a component-wise addition of vec0 and vec1 together storing the
* result into resultVec.
*
* @param {goog.vec.Vec3.AnyType} vec0 The first addend.
* @param {goog.vec.Vec3.AnyType} vec1 The second addend.
* @param {goog.vec.Vec3.AnyType} resultVec The vector to
* receive the result. May be vec0 or vec1.
* @return {!goog.vec.Vec3.AnyType} return resultVec so that operations can be
* chained together.
*/
goog.vec.Vec3.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.Vec3.AnyType} vec0 The minuend.
* @param {goog.vec.Vec3.AnyType} vec1 The subtrahend.
* @param {goog.vec.Vec3.AnyType} resultVec The vector to
* receive the result. May be vec0 or vec1.
* @return {!goog.vec.Vec3.AnyType} return resultVec so that operations can be
* chained together.
*/
goog.vec.Vec3.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.Vec3.AnyType} vec0 The vector to negate.
* @param {goog.vec.Vec3.AnyType} resultVec The vector to
* receive the result. May be vec0.
* @return {!goog.vec.Vec3.AnyType} return resultVec so that operations can be
* chained together.
*/
goog.vec.Vec3.negate = function(vec0, resultVec) {
resultVec[0] = -vec0[0];
resultVec[1] = -vec0[1];
resultVec[2] = -vec0[2];
return resultVec;
};
/**
* Multiplies each component of vec0 with scalar storing the product into
* resultVec.
*
* @param {goog.vec.Vec3.AnyType} vec0 The source vector.
* @param {number} scalar The value to multiply with each component of vec0.
* @param {goog.vec.Vec3.AnyType} resultVec The vector to
* receive the result. May be vec0.
* @return {!goog.vec.Vec3.AnyType} return resultVec so that operations can be
* chained together.
*/
goog.vec.Vec3.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.Vec3.AnyType} vec0 The vector.
* @return {number} The magnitude of the vector.
*/
goog.vec.Vec3.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.Vec3.AnyType} vec0 The vector.
* @return {number} The magnitude of the vector.
*/
goog.vec.Vec3.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.Vec3.AnyType} vec0 The vector to normalize.
* @param {goog.vec.Vec3.AnyType} resultVec The vector to
* receive the result. May be vec0.
* @return {!goog.vec.Vec3.AnyType} return resultVec so that operations can be
* chained together.
*/
goog.vec.Vec3.normalize = function(vec0, resultVec) {
var ilen = 1 / goog.vec.Vec3.magnitude(vec0);
resultVec[0] = vec0[0] * ilen;
resultVec[1] = vec0[1] * ilen;
resultVec[2] = vec0[2] * ilen;
return resultVec;
};
/**
* Returns the scalar product of vectors v0 and v1.
*
* @param {goog.vec.Vec3.AnyType} v0 The first vector.
* @param {goog.vec.Vec3.AnyType} v1 The second vector.
* @return {number} The scalar product.
*/
goog.vec.Vec3.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.Vec3.AnyType} v0 The first vector.
* @param {goog.vec.Vec3.AnyType} v1 The second vector.
* @param {goog.vec.Vec3.AnyType} resultVec The vector to receive the
* results. May be either v0 or v1.
* @return {!goog.vec.Vec3.AnyType} return resultVec so that operations can be
* chained together.
*/
goog.vec.Vec3.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.Vec3.AnyType} vec0 First point.
* @param {goog.vec.Vec3.AnyType} vec1 Second point.
* @return {number} The squared distance between the points.
*/
goog.vec.Vec3.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.Vec3.AnyType} vec0 First point.
* @param {goog.vec.Vec3.AnyType} vec1 Second point.
* @return {number} The distance between the points.
*/
goog.vec.Vec3.distance = function(vec0, vec1) {
return Math.sqrt(goog.vec.Vec3.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.Vec3.AnyType} vec0 Origin point.
* @param {goog.vec.Vec3.AnyType} vec1 Target point.
* @param {goog.vec.Vec3.AnyType} resultVec The vector to receive the
* results (may be vec0 or vec1).
* @return {!goog.vec.Vec3.AnyType} return resultVec so that operations can be
* chained together.
*/
goog.vec.Vec3.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.Vec3.AnyType} v0 The first vector.
* @param {goog.vec.Vec3.AnyType} v1 The second vector.
* @param {number} f The interpolation factor.
* @param {goog.vec.Vec3.AnyType} resultVec The vector to receive the
* results (may be v0 or v1).
* @return {!goog.vec.Vec3.AnyType} return resultVec so that operations can be
* chained together.
*/
goog.vec.Vec3.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;
};
/**
* Returns true if the components of v0 are equal to the components of v1.
*
* @param {goog.vec.Vec3.AnyType} v0 The first vector.
* @param {goog.vec.Vec3.AnyType} v1 The second vector.
* @return {boolean} True if the vectors are equal, false otherwise.
*/
goog.vec.Vec3.equals = function(v0, v1) {
return v0.length == v1.length &&
v0[0] == v1[0] && v0[1] == v1[1] && v0[2] == v1[2];
};
@@ -0,0 +1,405 @@
// Copyright 2011 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 Supplies 4 element vectors that are compatible with WebGL.
* Each element is a float32 since that is typically the desired size of a
* 4-vector in the GPU. The API is structured to avoid unnecessary memory
* allocations. The last parameter will typically be the output vector and
* an object can be both an input and output parameter to all methods except
* where noted.
*
*/
goog.provide('goog.vec.Vec4');
goog.require('goog.vec');
/** @typedef {goog.vec.Float32} */ goog.vec.Vec4.Float32;
/** @typedef {goog.vec.Float64} */ goog.vec.Vec4.Float64;
/** @typedef {goog.vec.Number} */ goog.vec.Vec4.Number;
/** @typedef {goog.vec.AnyType} */ goog.vec.Vec4.AnyType;
// The following two types are deprecated - use the above types instead.
/** @typedef {Float32Array} */ goog.vec.Vec4.Type;
/** @typedef {goog.vec.ArrayType} */ goog.vec.Vec4.Vec4Like;
/**
* Creates a 4 element vector of Float32. The array is initialized to zero.
*
* @return {!goog.vec.Vec4.Float32} The new 3 element array.
*/
goog.vec.Vec4.createFloat32 = function() {
return new Float32Array(4);
};
/**
* Creates a 4 element vector of Float64. The array is initialized to zero.
*
* @return {!goog.vec.Vec4.Float64} The new 4 element array.
*/
goog.vec.Vec4.createFloat64 = function() {
return new Float64Array(4);
};
/**
* Creates a 4 element vector of Number. The array is initialized to zero.
*
* @return {!goog.vec.Vec4.Number} The new 4 element array.
*/
goog.vec.Vec4.createNumber = function() {
var v = new Array(4);
goog.vec.Vec4.setFromValues(v, 0, 0, 0, 0);
return v;
};
/**
* Creates a 4 element vector of Float32Array. The array is initialized to zero.
*
* @deprecated Use createFloat32.
* @return {!goog.vec.Vec4.Type} The new 4 element array.
*/
goog.vec.Vec4.create = function() {
return new Float32Array(4);
};
/**
* Creates a new 4 element vector initialized with the value from the given
* array.
*
* @deprecated Use createFloat32FromArray.
* @param {goog.vec.Vec4.Vec4Like} vec The source 4 element array.
* @return {!goog.vec.Vec4.Type} The new 4 element array.
*/
goog.vec.Vec4.createFromArray = function(vec) {
var newVec = goog.vec.Vec4.create();
goog.vec.Vec4.setFromArray(newVec, vec);
return newVec;
};
/**
* Creates a new 4 element FLoat32 vector initialized with the value from the
* given array.
*
* @param {goog.vec.Vec4.AnyType} vec The source 3 element array.
* @return {!goog.vec.Vec4.Float32} The new 3 element array.
*/
goog.vec.Vec4.createFloat32FromArray = function(vec) {
var newVec = goog.vec.Vec4.createFloat32();
goog.vec.Vec4.setFromArray(newVec, vec);
return newVec;
};
/**
* Creates a new 4 element Float32 vector 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.
* @param {number} v3 The value for element at index 3.
* @return {!goog.vec.Vec4.Float32} The new vector.
*/
goog.vec.Vec4.createFloat32FromValues = function(v0, v1, v2, v3) {
var vec = goog.vec.Vec4.createFloat32();
goog.vec.Vec4.setFromValues(vec, v0, v1, v2, v3);
return vec;
};
/**
* Creates a clone of the given 4 element Float32 vector.
*
* @param {goog.vec.Vec4.Float32} vec The source 3 element vector.
* @return {!goog.vec.Vec4.Float32} The new cloned vector.
*/
goog.vec.Vec4.cloneFloat32 = goog.vec.Vec4.createFloat32FromArray;
/**
* Creates a new 4 element Float64 vector initialized with the value from the
* given array.
*
* @param {goog.vec.Vec4.AnyType} vec The source 4 element array.
* @return {!goog.vec.Vec4.Float64} The new 4 element array.
*/
goog.vec.Vec4.createFloat64FromArray = function(vec) {
var newVec = goog.vec.Vec4.createFloat64();
goog.vec.Vec4.setFromArray(newVec, vec);
return newVec;
};
/**
* Creates a new 4 element Float64 vector 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.
* @param {number} v3 The value for element at index 3.
* @return {!goog.vec.Vec4.Float64} The new vector.
*/
goog.vec.Vec4.createFloat64FromValues = function(v0, v1, v2, v3) {
var vec = goog.vec.Vec4.createFloat64();
goog.vec.Vec4.setFromValues(vec, v0, v1, v2, v3);
return vec;
};
/**
* Creates a clone of the given 4 element vector.
*
* @param {goog.vec.Vec4.Float64} vec The source 4 element vector.
* @return {!goog.vec.Vec4.Float64} The new cloned vector.
*/
goog.vec.Vec4.cloneFloat64 = goog.vec.Vec4.createFloat64FromArray;
/**
* Creates a new 4 element vector initialized with the supplied values.
*
* @deprecated Use createFloat32FromValues.
* @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.
* @param {number} v3 The value for element at index 3.
* @return {!goog.vec.Vec4.Type} The new vector.
*/
goog.vec.Vec4.createFromValues = function(v0, v1, v2, v3) {
var vec = goog.vec.Vec4.create();
goog.vec.Vec4.setFromValues(vec, v0, v1, v2, v3);
return vec;
};
/**
* Creates a clone of the given 4 element vector.
*
* @deprecated Use cloneFloat32.
* @param {goog.vec.Vec4.Vec4Like} vec The source 4 element vector.
* @return {!goog.vec.Vec4.Type} The new cloned vector.
*/
goog.vec.Vec4.clone = goog.vec.Vec4.createFromArray;
/**
* Initializes the vector with the given values.
*
* @param {goog.vec.Vec4.AnyType} 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.
* @param {number} v3 The value for element at index 3.
* @return {!goog.vec.Vec4.AnyType} return vec so that operations can be
* chained together.
*/
goog.vec.Vec4.setFromValues = function(vec, v0, v1, v2, v3) {
vec[0] = v0;
vec[1] = v1;
vec[2] = v2;
vec[3] = v3;
return vec;
};
/**
* Initializes the vector with the given array of values.
*
* @param {goog.vec.Vec4.AnyType} vec The vector to receive the
* values.
* @param {goog.vec.Vec4.AnyType} values The array of values.
* @return {!goog.vec.Vec4.AnyType} return vec so that operations can be
* chained together.
*/
goog.vec.Vec4.setFromArray = function(vec, values) {
vec[0] = values[0];
vec[1] = values[1];
vec[2] = values[2];
vec[3] = values[3];
return vec;
};
/**
* Performs a component-wise addition of vec0 and vec1 together storing the
* result into resultVec.
*
* @param {goog.vec.Vec4.AnyType} vec0 The first addend.
* @param {goog.vec.Vec4.AnyType} vec1 The second addend.
* @param {goog.vec.Vec4.AnyType} resultVec The vector to
* receive the result. May be vec0 or vec1.
* @return {!goog.vec.Vec4.AnyType} return resultVec so that operations can be
* chained together.
*/
goog.vec.Vec4.add = function(vec0, vec1, resultVec) {
resultVec[0] = vec0[0] + vec1[0];
resultVec[1] = vec0[1] + vec1[1];
resultVec[2] = vec0[2] + vec1[2];
resultVec[3] = vec0[3] + vec1[3];
return resultVec;
};
/**
* Performs a component-wise subtraction of vec1 from vec0 storing the
* result into resultVec.
*
* @param {goog.vec.Vec4.AnyType} vec0 The minuend.
* @param {goog.vec.Vec4.AnyType} vec1 The subtrahend.
* @param {goog.vec.Vec4.AnyType} resultVec The vector to
* receive the result. May be vec0 or vec1.
* @return {!goog.vec.Vec4.AnyType} return resultVec so that operations can be
* chained together.
*/
goog.vec.Vec4.subtract = function(vec0, vec1, resultVec) {
resultVec[0] = vec0[0] - vec1[0];
resultVec[1] = vec0[1] - vec1[1];
resultVec[2] = vec0[2] - vec1[2];
resultVec[3] = vec0[3] - vec1[3];
return resultVec;
};
/**
* Negates vec0, storing the result into resultVec.
*
* @param {goog.vec.Vec4.AnyType} vec0 The vector to negate.
* @param {goog.vec.Vec4.AnyType} resultVec The vector to
* receive the result. May be vec0.
* @return {!goog.vec.Vec4.AnyType} return resultVec so that operations can be
* chained together.
*/
goog.vec.Vec4.negate = function(vec0, resultVec) {
resultVec[0] = -vec0[0];
resultVec[1] = -vec0[1];
resultVec[2] = -vec0[2];
resultVec[3] = -vec0[3];
return resultVec;
};
/**
* Multiplies each component of vec0 with scalar storing the product into
* resultVec.
*
* @param {goog.vec.Vec4.AnyType} vec0 The source vector.
* @param {number} scalar The value to multiply with each component of vec0.
* @param {goog.vec.Vec4.AnyType} resultVec The vector to
* receive the result. May be vec0.
* @return {!goog.vec.Vec4.AnyType} return resultVec so that operations can be
* chained together.
*/
goog.vec.Vec4.scale = function(vec0, scalar, resultVec) {
resultVec[0] = vec0[0] * scalar;
resultVec[1] = vec0[1] * scalar;
resultVec[2] = vec0[2] * scalar;
resultVec[3] = vec0[3] * scalar;
return resultVec;
};
/**
* Returns the magnitudeSquared of the given vector.
*
* @param {goog.vec.Vec4.AnyType} vec0 The vector.
* @return {number} The magnitude of the vector.
*/
goog.vec.Vec4.magnitudeSquared = function(vec0) {
var x = vec0[0], y = vec0[1], z = vec0[2], w = vec0[3];
return x * x + y * y + z * z + w * w;
};
/**
* Returns the magnitude of the given vector.
*
* @param {goog.vec.Vec4.AnyType} vec0 The vector.
* @return {number} The magnitude of the vector.
*/
goog.vec.Vec4.magnitude = function(vec0) {
var x = vec0[0], y = vec0[1], z = vec0[2], w = vec0[3];
return Math.sqrt(x * x + y * y + z * z + w * w);
};
/**
* Normalizes the given vector storing the result into resultVec.
*
* @param {goog.vec.Vec4.AnyType} vec0 The vector to normalize.
* @param {goog.vec.Vec4.AnyType} resultVec The vector to
* receive the result. May be vec0.
* @return {!goog.vec.Vec4.AnyType} return resultVec so that operations can be
* chained together.
*/
goog.vec.Vec4.normalize = function(vec0, resultVec) {
var ilen = 1 / goog.vec.Vec4.magnitude(vec0);
resultVec[0] = vec0[0] * ilen;
resultVec[1] = vec0[1] * ilen;
resultVec[2] = vec0[2] * ilen;
resultVec[3] = vec0[3] * ilen;
return resultVec;
};
/**
* Returns the scalar product of vectors v0 and v1.
*
* @param {goog.vec.Vec4.AnyType} v0 The first vector.
* @param {goog.vec.Vec4.AnyType} v1 The second vector.
* @return {number} The scalar product.
*/
goog.vec.Vec4.dot = function(v0, v1) {
return v0[0] * v1[0] + v0[1] * v1[1] + v0[2] * v1[2] + v0[3] * v1[3];
};
/**
* Linearly interpolate from v0 to v1 according to f. The value of f should be
* in the range [0..1] otherwise the results are undefined.
*
* @param {goog.vec.Vec4.AnyType} v0 The first vector.
* @param {goog.vec.Vec4.AnyType} v1 The second vector.
* @param {number} f The interpolation factor.
* @param {goog.vec.Vec4.AnyType} resultVec The vector to receive the
* results (may be v0 or v1).
* @return {!goog.vec.Vec4.AnyType} return resultVec so that operations can be
* chained together.
*/
goog.vec.Vec4.lerp = function(v0, v1, f, resultVec) {
var x = v0[0], y = v0[1], z = v0[2], w = v0[3];
resultVec[0] = (v1[0] - x) * f + x;
resultVec[1] = (v1[1] - y) * f + y;
resultVec[2] = (v1[2] - z) * f + z;
resultVec[3] = (v1[3] - w) * f + w;
return resultVec;
};
/**
* Returns true if the components of v0 are equal to the components of v1.
*
* @param {goog.vec.Vec4.AnyType} v0 The first vector.
* @param {goog.vec.Vec4.AnyType} v1 The second vector.
* @return {boolean} True if the vectors are equal, false otherwise.
*/
goog.vec.Vec4.equals = function(v0, v1) {
return v0.length == v1.length &&
v0[0] == v1[0] && v0[1] == v1[1] && v0[2] == v1[2] && v0[3] == v1[3];
};