| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653 | <!DOCTYPE html><html lang="en"><head>    <meta charset="utf-8">    <title>JSDoc: Source: vec4.js</title>    <script src="scripts/prettify/prettify.js"> </script>    <script src="scripts/prettify/lang-css.js"> </script>    <!--[if lt IE 9]>      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>    <![endif]-->    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"></head><body><div id="main">    <h1 class="page-title">Source: vec4.js</h1>            <section>        <article>            <pre class="prettyprint source linenums"><code>import * as glMatrix from "./common.js";/** * 4 Dimensional Vector * @module vec4 *//** * Creates a new, empty vec4 * * @returns {vec4} a new 4D vector */export function create() {  let 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 */export function clone(a) {  let 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 */export function fromValues(x, y, z, w) {  let 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 */export 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 */export 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 */export 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 */export 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 */export 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 */export 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 */export 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 */export 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 */export 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 */export 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 */export 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 */export 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 */export 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 */export function distance(a, b) {  let x = b[0] - a[0];  let y = b[1] - a[1];  let z = b[2] - a[2];  let 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 */export function squaredDistance(a, b) {  let x = b[0] - a[0];  let y = b[1] - a[1];  let z = b[2] - a[2];  let 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 */export function length(a) {  let x = a[0];  let y = a[1];  let z = a[2];  let 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 */export function squaredLength(a) {  let x = a[0];  let y = a[1];  let z = a[2];  let 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 */export 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 */export 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 */export function normalize(out, a) {  let x = a[0];  let y = a[1];  let z = a[2];  let w = a[3];  let 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 */export 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 */export function lerp(out, a, b, t) {  let ax = a[0];  let ay = a[1];  let az = a[2];  let 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 */export 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 */export function transformMat4(out, a, m) {  let 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 */export function transformQuat(out, a, q) {  let x = a[0], y = a[1], z = a[2];  let qx = q[0], qy = q[1], qz = q[2], qw = q[3];  // calculate quat * vec  let ix = qw * x + qy * z - qz * y;  let iy = qw * y + qz * x - qx * z;  let iz = qw * z + qx * y - qy * x;  let 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 */export 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. */export 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. */export function equals(a, b) {  let a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3];  let 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 */export const sub = subtract;/** * Alias for {@link vec4.multiply} * @function */export const mul = multiply;/** * Alias for {@link vec4.divide} * @function */export const div = divide;/** * Alias for {@link vec4.distance} * @function */export const dist = distance;/** * Alias for {@link vec4.squaredDistance} * @function */export const sqrDist = squaredDistance;/** * Alias for {@link vec4.length} * @function */export const len = length;/** * Alias for {@link vec4.squaredLength} * @function */export const 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 */export const forEach = (function() {  let vec = create();  return function(a, stride, offset, count, fn, arg) {    let i, l;    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;  };})();</code></pre>        </article>    </section></div><nav>    <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-glMatrix.html">glMatrix</a></li><li><a href="module-mat2.html">mat2</a></li><li><a href="module-mat2d.html">mat2d</a></li><li><a href="module-mat3.html">mat3</a></li><li><a href="module-mat4.html">mat4</a></li><li><a href="module-quat.html">quat</a></li><li><a href="module-quat2.html">quat2</a></li><li><a href="module-vec2.html">vec2</a></li><li><a href="module-vec3.html">vec3</a></li><li><a href="module-vec4.html">vec4</a></li></ul></nav><br class="clear"><footer>    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Fri Jul 13 2018 11:51:33 GMT+0200 (W. Europe Daylight Time)</footer><script> prettyPrint(); </script><script src="scripts/linenumber.js"> </script></body></html>
 |