123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656 |
- 'use strict';
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.forEach = exports.sqrLen = exports.len = exports.sqrDist = exports.dist = exports.div = exports.mul = exports.sub = undefined;
- exports.create = create;
- exports.clone = clone;
- exports.fromValues = fromValues;
- exports.copy = copy;
- exports.set = set;
- exports.add = add;
- exports.subtract = subtract;
- exports.multiply = multiply;
- exports.divide = divide;
- exports.ceil = ceil;
- exports.floor = floor;
- exports.min = min;
- exports.max = max;
- exports.round = round;
- exports.scale = scale;
- exports.scaleAndAdd = scaleAndAdd;
- exports.distance = distance;
- exports.squaredDistance = squaredDistance;
- exports.length = length;
- exports.squaredLength = squaredLength;
- exports.negate = negate;
- exports.inverse = inverse;
- exports.normalize = normalize;
- exports.dot = dot;
- exports.lerp = lerp;
- exports.random = random;
- exports.transformMat4 = transformMat4;
- exports.transformQuat = transformQuat;
- exports.str = str;
- exports.exactEquals = exactEquals;
- exports.equals = equals;
- var _common = require('./common.js');
- var glMatrix = _interopRequireWildcard(_common);
- function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
- /**
- * 4 Dimensional Vector
- * @module vec4
- */
- /**
- * Creates a new, empty vec4
- *
- * @returns {vec4} a new 4D vector
- */
- function create() {
- var out = new glMatrix.ARRAY_TYPE(4);
- if (glMatrix.ARRAY_TYPE != Float32Array) {
- out[0] = 0;
- out[1] = 0;
- out[2] = 0;
- out[3] = 0;
- }
- return out;
- }
- /**
- * Creates a new vec4 initialized with values from an existing vector
- *
- * @param {vec4} a vector to clone
- * @returns {vec4} a new 4D vector
- */
- function clone(a) {
- var out = new glMatrix.ARRAY_TYPE(4);
- out[0] = a[0];
- out[1] = a[1];
- out[2] = a[2];
- out[3] = a[3];
- return out;
- }
- /**
- * Creates a new vec4 initialized with the given values
- *
- * @param {Number} x X component
- * @param {Number} y Y component
- * @param {Number} z Z component
- * @param {Number} w W component
- * @returns {vec4} a new 4D vector
- */
- function fromValues(x, y, z, w) {
- var out = new glMatrix.ARRAY_TYPE(4);
- out[0] = x;
- out[1] = y;
- out[2] = z;
- out[3] = w;
- return out;
- }
- /**
- * Copy the values from one vec4 to another
- *
- * @param {vec4} out the receiving vector
- * @param {vec4} a the source vector
- * @returns {vec4} out
- */
- function copy(out, a) {
- out[0] = a[0];
- out[1] = a[1];
- out[2] = a[2];
- out[3] = a[3];
- return out;
- }
- /**
- * Set the components of a vec4 to the given values
- *
- * @param {vec4} out the receiving vector
- * @param {Number} x X component
- * @param {Number} y Y component
- * @param {Number} z Z component
- * @param {Number} w W component
- * @returns {vec4} out
- */
- function set(out, x, y, z, w) {
- out[0] = x;
- out[1] = y;
- out[2] = z;
- out[3] = w;
- return out;
- }
- /**
- * Adds two vec4's
- *
- * @param {vec4} out the receiving vector
- * @param {vec4} a the first operand
- * @param {vec4} b the second operand
- * @returns {vec4} out
- */
- function add(out, a, b) {
- out[0] = a[0] + b[0];
- out[1] = a[1] + b[1];
- out[2] = a[2] + b[2];
- out[3] = a[3] + b[3];
- return out;
- }
- /**
- * Subtracts vector b from vector a
- *
- * @param {vec4} out the receiving vector
- * @param {vec4} a the first operand
- * @param {vec4} b the second operand
- * @returns {vec4} out
- */
- function subtract(out, a, b) {
- out[0] = a[0] - b[0];
- out[1] = a[1] - b[1];
- out[2] = a[2] - b[2];
- out[3] = a[3] - b[3];
- return out;
- }
- /**
- * Multiplies two vec4's
- *
- * @param {vec4} out the receiving vector
- * @param {vec4} a the first operand
- * @param {vec4} b the second operand
- * @returns {vec4} out
- */
- function multiply(out, a, b) {
- out[0] = a[0] * b[0];
- out[1] = a[1] * b[1];
- out[2] = a[2] * b[2];
- out[3] = a[3] * b[3];
- return out;
- }
- /**
- * Divides two vec4's
- *
- * @param {vec4} out the receiving vector
- * @param {vec4} a the first operand
- * @param {vec4} b the second operand
- * @returns {vec4} out
- */
- function divide(out, a, b) {
- out[0] = a[0] / b[0];
- out[1] = a[1] / b[1];
- out[2] = a[2] / b[2];
- out[3] = a[3] / b[3];
- return out;
- }
- /**
- * Math.ceil the components of a vec4
- *
- * @param {vec4} out the receiving vector
- * @param {vec4} a vector to ceil
- * @returns {vec4} out
- */
- function ceil(out, a) {
- out[0] = Math.ceil(a[0]);
- out[1] = Math.ceil(a[1]);
- out[2] = Math.ceil(a[2]);
- out[3] = Math.ceil(a[3]);
- return out;
- }
- /**
- * Math.floor the components of a vec4
- *
- * @param {vec4} out the receiving vector
- * @param {vec4} a vector to floor
- * @returns {vec4} out
- */
- function floor(out, a) {
- out[0] = Math.floor(a[0]);
- out[1] = Math.floor(a[1]);
- out[2] = Math.floor(a[2]);
- out[3] = Math.floor(a[3]);
- return out;
- }
- /**
- * Returns the minimum of two vec4's
- *
- * @param {vec4} out the receiving vector
- * @param {vec4} a the first operand
- * @param {vec4} b the second operand
- * @returns {vec4} out
- */
- function min(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]);
- out[3] = Math.min(a[3], b[3]);
- return out;
- }
- /**
- * Returns the maximum of two vec4's
- *
- * @param {vec4} out the receiving vector
- * @param {vec4} a the first operand
- * @param {vec4} b the second operand
- * @returns {vec4} out
- */
- function max(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]);
- out[3] = Math.max(a[3], b[3]);
- return out;
- }
- /**
- * Math.round the components of a vec4
- *
- * @param {vec4} out the receiving vector
- * @param {vec4} a vector to round
- * @returns {vec4} out
- */
- function round(out, a) {
- out[0] = Math.round(a[0]);
- out[1] = Math.round(a[1]);
- out[2] = Math.round(a[2]);
- out[3] = Math.round(a[3]);
- return out;
- }
- /**
- * Scales a vec4 by a scalar number
- *
- * @param {vec4} out the receiving vector
- * @param {vec4} a the vector to scale
- * @param {Number} b amount to scale the vector by
- * @returns {vec4} out
- */
- function scale(out, a, b) {
- out[0] = a[0] * b;
- out[1] = a[1] * b;
- out[2] = a[2] * b;
- out[3] = a[3] * b;
- return out;
- }
- /**
- * Adds two vec4's after scaling the second operand by a scalar value
- *
- * @param {vec4} out the receiving vector
- * @param {vec4} a the first operand
- * @param {vec4} b the second operand
- * @param {Number} scale the amount to scale b by before adding
- * @returns {vec4} out
- */
- function scaleAndAdd(out, a, b, scale) {
- out[0] = a[0] + b[0] * scale;
- out[1] = a[1] + b[1] * scale;
- out[2] = a[2] + b[2] * scale;
- out[3] = a[3] + b[3] * scale;
- return out;
- }
- /**
- * Calculates the euclidian distance between two vec4's
- *
- * @param {vec4} a the first operand
- * @param {vec4} b the second operand
- * @returns {Number} distance between a and b
- */
- function distance(a, b) {
- var x = b[0] - a[0];
- var y = b[1] - a[1];
- var z = b[2] - a[2];
- var w = b[3] - a[3];
- return Math.sqrt(x * x + y * y + z * z + w * w);
- }
- /**
- * Calculates the squared euclidian distance between two vec4's
- *
- * @param {vec4} a the first operand
- * @param {vec4} b the second operand
- * @returns {Number} squared distance between a and b
- */
- function squaredDistance(a, b) {
- var x = b[0] - a[0];
- var y = b[1] - a[1];
- var z = b[2] - a[2];
- var w = b[3] - a[3];
- return x * x + y * y + z * z + w * w;
- }
- /**
- * Calculates the length of a vec4
- *
- * @param {vec4} a vector to calculate length of
- * @returns {Number} length of a
- */
- function length(a) {
- var x = a[0];
- var y = a[1];
- var z = a[2];
- var w = a[3];
- return Math.sqrt(x * x + y * y + z * z + w * w);
- }
- /**
- * Calculates the squared length of a vec4
- *
- * @param {vec4} a vector to calculate squared length of
- * @returns {Number} squared length of a
- */
- function squaredLength(a) {
- var x = a[0];
- var y = a[1];
- var z = a[2];
- var w = a[3];
- return x * x + y * y + z * z + w * w;
- }
- /**
- * Negates the components of a vec4
- *
- * @param {vec4} out the receiving vector
- * @param {vec4} a vector to negate
- * @returns {vec4} out
- */
- function negate(out, a) {
- out[0] = -a[0];
- out[1] = -a[1];
- out[2] = -a[2];
- out[3] = -a[3];
- return out;
- }
- /**
- * Returns the inverse of the components of a vec4
- *
- * @param {vec4} out the receiving vector
- * @param {vec4} a vector to invert
- * @returns {vec4} out
- */
- function inverse(out, a) {
- out[0] = 1.0 / a[0];
- out[1] = 1.0 / a[1];
- out[2] = 1.0 / a[2];
- out[3] = 1.0 / a[3];
- return out;
- }
- /**
- * Normalize a vec4
- *
- * @param {vec4} out the receiving vector
- * @param {vec4} a vector to normalize
- * @returns {vec4} out
- */
- function normalize(out, a) {
- var x = a[0];
- var y = a[1];
- var z = a[2];
- var w = a[3];
- var len = x * x + y * y + z * z + w * w;
- if (len > 0) {
- len = 1 / Math.sqrt(len);
- out[0] = x * len;
- out[1] = y * len;
- out[2] = z * len;
- out[3] = w * len;
- }
- return out;
- }
- /**
- * Calculates the dot product of two vec4's
- *
- * @param {vec4} a the first operand
- * @param {vec4} b the second operand
- * @returns {Number} dot product of a and b
- */
- function dot(a, b) {
- return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
- }
- /**
- * Performs a linear interpolation between two vec4's
- *
- * @param {vec4} out the receiving vector
- * @param {vec4} a the first operand
- * @param {vec4} b the second operand
- * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
- * @returns {vec4} out
- */
- function lerp(out, a, b, t) {
- var ax = a[0];
- var ay = a[1];
- var az = a[2];
- var aw = a[3];
- out[0] = ax + t * (b[0] - ax);
- out[1] = ay + t * (b[1] - ay);
- out[2] = az + t * (b[2] - az);
- out[3] = aw + t * (b[3] - aw);
- return out;
- }
- /**
- * Generates a random vector with the given scale
- *
- * @param {vec4} out the receiving vector
- * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned
- * @returns {vec4} out
- */
- function random(out, scale) {
- scale = scale || 1.0;
- // Marsaglia, George. Choosing a Point from the Surface of a
- // Sphere. Ann. Math. Statist. 43 (1972), no. 2, 645--646.
- // http://projecteuclid.org/euclid.aoms/1177692644;
- var v1, v2, v3, v4;
- var s1, s2;
- do {
- v1 = glMatrix.RANDOM() * 2 - 1;
- v2 = glMatrix.RANDOM() * 2 - 1;
- s1 = v1 * v1 + v2 * v2;
- } while (s1 >= 1);
- do {
- v3 = glMatrix.RANDOM() * 2 - 1;
- v4 = glMatrix.RANDOM() * 2 - 1;
- s2 = v3 * v3 + v4 * v4;
- } while (s2 >= 1);
- var d = Math.sqrt((1 - s1) / s2);
- out[0] = scale * v1;
- out[1] = scale * v2;
- out[2] = scale * v3 * d;
- out[3] = scale * v4 * d;
- return out;
- }
- /**
- * Transforms the vec4 with a mat4.
- *
- * @param {vec4} out the receiving vector
- * @param {vec4} a the vector to transform
- * @param {mat4} m matrix to transform with
- * @returns {vec4} out
- */
- function transformMat4(out, a, m) {
- var x = a[0],
- y = a[1],
- z = a[2],
- w = a[3];
- out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w;
- out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w;
- out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w;
- out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w;
- return out;
- }
- /**
- * Transforms the vec4 with a quat
- *
- * @param {vec4} out the receiving vector
- * @param {vec4} a the vector to transform
- * @param {quat} q quaternion to transform with
- * @returns {vec4} out
- */
- function transformQuat(out, a, q) {
- var x = a[0],
- y = a[1],
- z = a[2];
- var qx = q[0],
- qy = q[1],
- qz = q[2],
- qw = q[3];
- // calculate quat * vec
- var ix = qw * x + qy * z - qz * y;
- var iy = qw * y + qz * x - qx * z;
- var iz = qw * z + qx * y - qy * x;
- var 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;
- out[3] = a[3];
- return out;
- }
- /**
- * Returns a string representation of a vector
- *
- * @param {vec4} a vector to represent as a string
- * @returns {String} string representation of the vector
- */
- function str(a) {
- return 'vec4(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')';
- }
- /**
- * Returns whether or not the vectors have exactly the same elements in the same position (when compared with ===)
- *
- * @param {vec4} a The first vector.
- * @param {vec4} b The second vector.
- * @returns {Boolean} True if the vectors are equal, false otherwise.
- */
- function exactEquals(a, b) {
- return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3];
- }
- /**
- * Returns whether or not the vectors have approximately the same elements in the same position.
- *
- * @param {vec4} a The first vector.
- * @param {vec4} b The second vector.
- * @returns {Boolean} True if the vectors are equal, false otherwise.
- */
- function equals(a, b) {
- var a0 = a[0],
- a1 = a[1],
- a2 = a[2],
- a3 = a[3];
- var b0 = b[0],
- b1 = b[1],
- b2 = b[2],
- b3 = b[3];
- return Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3));
- }
- /**
- * Alias for {@link vec4.subtract}
- * @function
- */
- var sub = exports.sub = subtract;
- /**
- * Alias for {@link vec4.multiply}
- * @function
- */
- var mul = exports.mul = multiply;
- /**
- * Alias for {@link vec4.divide}
- * @function
- */
- var div = exports.div = divide;
- /**
- * Alias for {@link vec4.distance}
- * @function
- */
- var dist = exports.dist = distance;
- /**
- * Alias for {@link vec4.squaredDistance}
- * @function
- */
- var sqrDist = exports.sqrDist = squaredDistance;
- /**
- * Alias for {@link vec4.length}
- * @function
- */
- var len = exports.len = length;
- /**
- * Alias for {@link vec4.squaredLength}
- * @function
- */
- var sqrLen = exports.sqrLen = squaredLength;
- /**
- * Perform some operation over an array of vec4s.
- *
- * @param {Array} a the array of vectors to iterate over
- * @param {Number} stride Number of elements between the start of each vec4. If 0 assumes tightly packed
- * @param {Number} offset Number of elements to skip at the beginning of the array
- * @param {Number} count Number of vec4s 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
- */
- var forEach = exports.forEach = function () {
- var vec = create();
- return function (a, stride, offset, count, fn, arg) {
- var i = void 0,
- l = void 0;
- if (!stride) {
- stride = 4;
- }
- 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];vec[3] = a[i + 3];
- fn(vec, vec, arg);
- a[i] = vec[0];a[i + 1] = vec[1];a[i + 2] = vec[2];a[i + 3] = vec[3];
- }
- return a;
- };
- }();
|