| /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without modification, |
| are permitted provided that the following conditions are met: |
| |
| * Redistributions of source code must retain the above copyright notice, this |
| list of conditions and the following disclaimer. |
| * Redistributions in binary form must reproduce the above copyright notice, |
| this list of conditions and the following disclaimer in the documentation |
| and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ |
| |
| /** |
| * @class 3 Dimensional Vector |
| * @name vec3 |
| */ |
| var vec3 = {}; |
| |
| /** |
| * Creates a new, empty vec3 |
| * |
| * @returns {vec3} a new 3D vector |
| */ |
| vec3.create = function() { |
| var out = new GLMAT_ARRAY_TYPE(3); |
| out[0] = 0; |
| out[1] = 0; |
| out[2] = 0; |
| return out; |
| }; |
| |
| /** |
| * Creates a new vec3 initialized with values from an existing vector |
| * |
| * @param {vec3} a vector to clone |
| * @returns {vec3} a new 3D vector |
| */ |
| vec3.clone = function(a) { |
| var out = new GLMAT_ARRAY_TYPE(3); |
| out[0] = a[0]; |
| out[1] = a[1]; |
| out[2] = a[2]; |
| return out; |
| }; |
| |
| /** |
| * Creates a new vec3 initialized with the given values |
| * |
| * @param {Number} x X component |
| * @param {Number} y Y component |
| * @param {Number} z Z component |
| * @returns {vec3} a new 3D vector |
| */ |
| vec3.fromValues = function(x, y, z) { |
| var out = new GLMAT_ARRAY_TYPE(3); |
| out[0] = x; |
| out[1] = y; |
| out[2] = z; |
| return out; |
| }; |
| |
| /** |
| * Copy the values from one vec3 to another |
| * |
| * @param {vec3} out the receiving vector |
| * @param {vec3} a the source vector |
| * @returns {vec3} out |
| */ |
| vec3.copy = function(out, a) { |
| out[0] = a[0]; |
| out[1] = a[1]; |
| out[2] = a[2]; |
| return out; |
| }; |
| |
| /** |
| * Set the components of a vec3 to the given values |
| * |
| * @param {vec3} out the receiving vector |
| * @param {Number} x X component |
| * @param {Number} y Y component |
| * @param {Number} z Z component |
| * @returns {vec3} out |
| */ |
| vec3.set = function(out, x, y, z) { |
| out[0] = x; |
| out[1] = y; |
| out[2] = z; |
| return out; |
| }; |
| |
| /** |
| * Adds two vec3's |
| * |
| * @param {vec3} out the receiving vector |
| * @param {vec3} a the first operand |
| * @param {vec3} b the second operand |
| * @returns {vec3} out |
| */ |
| vec3.add = function(out, a, b) { |
| out[0] = a[0] + b[0]; |
| out[1] = a[1] + b[1]; |
| out[2] = a[2] + b[2]; |
| return out; |
| }; |
| |
| /** |
| * Subtracts two vec3's |
| * |
| * @param {vec3} out the receiving vector |
| * @param {vec3} a the first operand |
| * @param {vec3} b the second operand |
| * @returns {vec3} out |
| */ |
| vec3.subtract = function(out, a, b) { |
| out[0] = a[0] - b[0]; |
| out[1] = a[1] - b[1]; |
| out[2] = a[2] - b[2]; |
| return out; |
| }; |
| |
| /** |
| * Alias for {@link vec3.subtract} |
| * @function |
| */ |
| vec3.sub = vec3.subtract; |
| |
| /** |
| * Multiplies two vec3's |
| * |
| * @param {vec3} out the receiving vector |
| * @param {vec3} a the first operand |
| * @param {vec3} b the second operand |
| * @returns {vec3} out |
| */ |
| vec3.multiply = function(out, a, b) { |
| out[0] = a[0] * b[0]; |
| out[1] = a[1] * b[1]; |
| out[2] = a[2] * b[2]; |
| return out; |
| }; |
| |
| /** |
| * Alias for {@link vec3.multiply} |
| * @function |
| */ |
| vec3.mul = vec3.multiply; |
| |
| /** |
| * Divides two vec3's |
| * |
| * @param {vec3} out the receiving vector |
| * @param {vec3} a the first operand |
| * @param {vec3} b the second operand |
| * @returns {vec3} out |
| */ |
| vec3.divide = function(out, a, b) { |
| out[0] = a[0] / b[0]; |
| out[1] = a[1] / b[1]; |
| out[2] = a[2] / b[2]; |
| return out; |
| }; |
| |
| /** |
| * Alias for {@link vec3.divide} |
| * @function |
| */ |
| vec3.div = vec3.divide; |
| |
| /** |
| * Returns the minimum of two vec3's |
| * |
| * @param {vec3} out the receiving vector |
| * @param {vec3} a the first operand |
| * @param {vec3} b the second operand |
| * @returns {vec3} out |
| */ |
| vec3.min = function(out, a, b) { |
| out[0] = Math.min(a[0], b[0]); |
| out[1] = Math.min(a[1], b[1]); |
| out[2] = Math.min(a[2], b[2]); |
| return out; |
| }; |
| |
| /** |
| * Returns the maximum of two vec3's |
| * |
| * @param {vec3} out the receiving vector |
| * @param {vec3} a the first operand |
| * @param {vec3} b the second operand |
| * @returns {vec3} out |
| */ |
| vec3.max = function(out, a, b) { |
| out[0] = Math.max(a[0], b[0]); |
| out[1] = Math.max(a[1], b[1]); |
| out[2] = Math.max(a[2], b[2]); |
| return out; |
| }; |
| |
| /** |
| * Scales a vec3 by a scalar number |
| * |
| * @param {vec3} out the receiving vector |
| * @param {vec3} a the vector to scale |
| * @param {Number} b amount to scale the vector by |
| * @returns {vec3} out |
| */ |
| vec3.scale = function(out, a, b) { |
| out[0] = a[0] * b; |
| out[1] = a[1] * b; |
| out[2] = a[2] * b; |
| return out; |
| }; |
| |
| /** |
| * Calculates the euclidian distance between two vec3's |
| * |
| * @param {vec3} a the first operand |
| * @param {vec3} b the second operand |
| * @returns {Number} distance between a and b |
| */ |
| vec3.distance = function(a, b) { |
| var x = b[0] - a[0], |
| y = b[1] - a[1], |
| z = b[2] - a[2]; |
| return Math.sqrt(x*x + y*y + z*z); |
| }; |
| |
| /** |
| * Alias for {@link vec3.distance} |
| * @function |
| */ |
| vec3.dist = vec3.distance; |
| |
| /** |
| * Calculates the squared euclidian distance between two vec3's |
| * |
| * @param {vec3} a the first operand |
| * @param {vec3} b the second operand |
| * @returns {Number} squared distance between a and b |
| */ |
| vec3.squaredDistance = function(a, b) { |
| var x = b[0] - a[0], |
| y = b[1] - a[1], |
| z = b[2] - a[2]; |
| return x*x + y*y + z*z; |
| }; |
| |
| /** |
| * Alias for {@link vec3.squaredDistance} |
| * @function |
| */ |
| vec3.sqrDist = vec3.squaredDistance; |
| |
| /** |
| * Calculates the length of a vec3 |
| * |
| * @param {vec3} a vector to calculate length of |
| * @returns {Number} length of a |
| */ |
| vec3.length = function (a) { |
| var x = a[0], |
| y = a[1], |
| z = a[2]; |
| return Math.sqrt(x*x + y*y + z*z); |
| }; |
| |
| /** |
| * Alias for {@link vec3.length} |
| * @function |
| */ |
| vec3.len = vec3.length; |
| |
| /** |
| * Calculates the squared length of a vec3 |
| * |
| * @param {vec3} a vector to calculate squared length of |
| * @returns {Number} squared length of a |
| */ |
| vec3.squaredLength = function (a) { |
| var x = a[0], |
| y = a[1], |
| z = a[2]; |
| return x*x + y*y + z*z; |
| }; |
| |
| /** |
| * Alias for {@link vec3.squaredLength} |
| * @function |
| */ |
| vec3.sqrLen = vec3.squaredLength; |
| |
| /** |
| * Negates the components of a vec3 |
| * |
| * @param {vec3} out the receiving vector |
| * @param {vec3} a vector to negate |
| * @returns {vec3} out |
| */ |
| vec3.negate = function(out, a) { |
| out[0] = -a[0]; |
| out[1] = -a[1]; |
| out[2] = -a[2]; |
| return out; |
| }; |
| |
| /** |
| * Normalize a vec3 |
| * |
| * @param {vec3} out the receiving vector |
| * @param {vec3} a vector to normalize |
| * @returns {vec3} out |
| */ |
| vec3.normalize = function(out, a) { |
| var x = a[0], |
| y = a[1], |
| z = a[2]; |
| var len = x*x + y*y + z*z; |
| if (len > 0) { |
| //TODO: evaluate use of glm_invsqrt here? |
| len = 1 / Math.sqrt(len); |
| out[0] = a[0] * len; |
| out[1] = a[1] * len; |
| out[2] = a[2] * len; |
| } |
| return out; |
| }; |
| |
| /** |
| * Calculates the dot product of two vec3's |
| * |
| * @param {vec3} a the first operand |
| * @param {vec3} b the second operand |
| * @returns {Number} dot product of a and b |
| */ |
| vec3.dot = function (a, b) { |
| return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; |
| }; |
| |
| /** |
| * Computes the cross product of two vec3's |
| * |
| * @param {vec3} out the receiving vector |
| * @param {vec3} a the first operand |
| * @param {vec3} b the second operand |
| * @returns {vec3} out |
| */ |
| vec3.cross = function(out, a, b) { |
| var ax = a[0], ay = a[1], az = a[2], |
| bx = b[0], by = b[1], bz = b[2]; |
| |
| out[0] = ay * bz - az * by; |
| out[1] = az * bx - ax * bz; |
| out[2] = ax * by - ay * bx; |
| return out; |
| }; |
| |
| /** |
| * Performs a linear interpolation between two vec3's |
| * |
| * @param {vec3} out the receiving vector |
| * @param {vec3} a the first operand |
| * @param {vec3} b the second operand |
| * @param {Number} t interpolation amount between the two inputs |
| * @returns {vec3} out |
| */ |
| vec3.lerp = function (out, a, b, t) { |
| var ax = a[0], |
| ay = a[1], |
| az = a[2]; |
| out[0] = ax + t * (b[0] - ax); |
| out[1] = ay + t * (b[1] - ay); |
| out[2] = az + t * (b[2] - az); |
| return out; |
| }; |
| |
| /** |
| * Transforms the vec3 with a mat4. |
| * 4th vector component is implicitly '1' |
| * |
| * @param {vec3} out the receiving vector |
| * @param {vec3} a the vector to transform |
| * @param {mat4} m matrix to transform with |
| * @returns {vec3} out |
| */ |
| vec3.transformMat4 = function(out, a, m) { |
| var x = a[0], y = a[1], z = a[2]; |
| out[0] = m[0] * x + m[4] * y + m[8] * z + m[12]; |
| out[1] = m[1] * x + m[5] * y + m[9] * z + m[13]; |
| out[2] = m[2] * x + m[6] * y + m[10] * z + m[14]; |
| return out; |
| }; |
| |
| /** |
| * Transforms the vec3 with a quat |
| * |
| * @param {vec3} out the receiving vector |
| * @param {vec3} a the vector to transform |
| * @param {quat} q quaternion to transform with |
| * @returns {vec3} out |
| */ |
| vec3.transformQuat = function(out, a, q) { |
| var x = a[0], y = a[1], z = a[2], |
| qx = q[0], qy = q[1], qz = q[2], qw = q[3], |
| |
| // calculate quat * vec |
| ix = qw * x + qy * z - qz * y, |
| iy = qw * y + qz * x - qx * z, |
| iz = qw * z + qx * y - qy * x, |
| iw = -qx * x - qy * y - qz * z; |
| |
| // calculate result * inverse quat |
| out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy; |
| out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz; |
| out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx; |
| return out; |
| }; |
| |
| /** |
| * Perform some operation over an array of vec3s. |
| * |
| * @param {Array} a the array of vectors to iterate over |
| * @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed |
| * @param {Number} offset Number of elements to skip at the beginning of the array |
| * @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array |
| * @param {Function} fn Function to call for each vector in the array |
| * @param {Object} [arg] additional argument to pass to fn |
| * @returns {Array} a |
| * @function |
| */ |
| vec3.forEach = (function() { |
| var vec = vec3.create(); |
| |
| return function(a, stride, offset, count, fn, arg) { |
| var i, l; |
| if(!stride) { |
| stride = 3; |
| } |
| |
| if(!offset) { |
| offset = 0; |
| } |
| |
| if(count) { |
| l = Math.min((count * stride) + offset, a.length); |
| } else { |
| l = a.length; |
| } |
| |
| for(i = offset; i < l; i += stride) { |
| vec[0] = a[i]; vec[1] = a[i+1]; vec[2] = a[i+2]; |
| fn(vec, vec, arg); |
| a[i] = vec[0]; a[i+1] = vec[1]; a[i+2] = vec[2]; |
| } |
| |
| return a; |
| }; |
| })(); |
| |
| /** |
| * Returns a string representation of a vector |
| * |
| * @param {vec3} vec vector to represent as a string |
| * @returns {String} string representation of the vector |
| */ |
| vec3.str = function (a) { |
| return 'vec3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ')'; |
| }; |
| |
| if(typeof(exports) !== 'undefined') { |
| exports.vec3 = vec3; |
| } |