UNPKG

@turbox3d/math

Version:

Large-scale graphics application math library

509 lines (508 loc) 14.1 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.Vector4 = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var Vector4 = exports.Vector4 = /*#__PURE__*/function () { function Vector4() { var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; var y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; var z = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0; var w = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1; (0, _classCallCheck2["default"])(this, Vector4); this.isVector4 = true; this.x = x; this.y = y; this.z = z; this.w = w; } return (0, _createClass2["default"])(Vector4, [{ key: "width", get: function get() { return this.z; }, set: function set(value) { this.z = value; } }, { key: "height", get: function get() { return this.w; }, set: function set(value) { this.w = value; } }, { key: "set", value: function set(x, y, z, w) { this.x = x; this.y = y; this.z = z; this.w = w; return this; } }, { key: "setScalar", value: function setScalar(scalar) { this.x = scalar; this.y = scalar; this.z = scalar; this.w = scalar; return this; } }, { key: "setX", value: function setX(x) { this.x = x; return this; } }, { key: "setY", value: function setY(y) { this.y = y; return this; } }, { key: "setZ", value: function setZ(z) { this.z = z; return this; } }, { key: "setW", value: function setW(w) { this.w = w; return this; } }, { key: "setComponent", value: function setComponent(index, value) { switch (index) { case 0: this.x = value; break; case 1: this.y = value; break; case 2: this.z = value; break; case 3: this.w = value; break; default: throw new Error("index is out of range: ".concat(index)); } return this; } }, { key: "getComponent", value: function getComponent(index) { switch (index) { case 0: return this.x; case 1: return this.y; case 2: return this.z; case 3: return this.w; default: throw new Error("index is out of range: ".concat(index)); } } }, { key: "clone", value: function clone() { return new Vector4(this.x, this.y, this.z, this.w); } }, { key: "copy", value: function copy(v) { this.x = v.x; this.y = v.y; this.z = v.z; this.w = v.w !== undefined ? v.w : 1; return this; } }, { key: "add", value: function add(v) { this.x += v.x; this.y += v.y; this.z += v.z; this.w += v.w; return this; } }, { key: "addScalar", value: function addScalar(s) { this.x += s; this.y += s; this.z += s; this.w += s; return this; } }, { key: "addVectors", value: function addVectors(a, b) { this.x = a.x + b.x; this.y = a.y + b.y; this.z = a.z + b.z; this.w = a.w + b.w; return this; } }, { key: "addScaledVector", value: function addScaledVector(v, s) { this.x += v.x * s; this.y += v.y * s; this.z += v.z * s; this.w += v.w * s; return this; } }, { key: "sub", value: function sub(v) { this.x -= v.x; this.y -= v.y; this.z -= v.z; this.w -= v.w; return this; } }, { key: "subScalar", value: function subScalar(s) { this.x -= s; this.y -= s; this.z -= s; this.w -= s; return this; } }, { key: "subVectors", value: function subVectors(a, b) { this.x = a.x - b.x; this.y = a.y - b.y; this.z = a.z - b.z; this.w = a.w - b.w; return this; } }, { key: "multiply", value: function multiply(v) { this.x *= v.x; this.y *= v.y; this.z *= v.z; this.w *= v.w; return this; } }, { key: "multiplyScalar", value: function multiplyScalar(scalar) { this.x *= scalar; this.y *= scalar; this.z *= scalar; this.w *= scalar; return this; } }, { key: "applyMatrix4", value: function applyMatrix4(m) { var x = this.x; var y = this.y; var z = this.z; var w = this.w; var e = m.elements; this.x = e[0] * x + e[4] * y + e[8] * z + e[12] * w; this.y = e[1] * x + e[5] * y + e[9] * z + e[13] * w; this.z = e[2] * x + e[6] * y + e[10] * z + e[14] * w; this.w = e[3] * x + e[7] * y + e[11] * z + e[15] * w; return this; } }, { key: "divideScalar", value: function divideScalar(scalar) { return this.multiplyScalar(1 / scalar); } }, { key: "setAxisAngleFromQuaternion", value: function setAxisAngleFromQuaternion(q) { // http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm // q is assumed to be normalized this.w = 2 * Math.acos(q.w); var s = Math.sqrt(1 - q.w * q.w); if (s < 0.0001) { this.x = 1; this.y = 0; this.z = 0; } else { this.x = q.x / s; this.y = q.y / s; this.z = q.z / s; } return this; } }, { key: "setAxisAngleFromRotationMatrix", value: function setAxisAngleFromRotationMatrix(m) { // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled) var angle; var x; var y; var z; // variables for result var epsilon = 0.01; // margin to allow for rounding errors var epsilon2 = 0.1; // margin to distinguish between 0 and 180 degrees var te = m.elements; var m11 = te[0]; var m12 = te[4]; var m13 = te[8]; var m21 = te[1]; var m22 = te[5]; var m23 = te[9]; var m31 = te[2]; var m32 = te[6]; var m33 = te[10]; if (Math.abs(m12 - m21) < epsilon && Math.abs(m13 - m31) < epsilon && Math.abs(m23 - m32) < epsilon) { // singularity found // first check for identity matrix which must have +1 for all terms // in leading diagonal and zero in other terms if (Math.abs(m12 + m21) < epsilon2 && Math.abs(m13 + m31) < epsilon2 && Math.abs(m23 + m32) < epsilon2 && Math.abs(m11 + m22 + m33 - 3) < epsilon2) { // this singularity is identity matrix so angle = 0 this.set(1, 0, 0, 0); return this; // zero angle, arbitrary axis } // otherwise this singularity is angle = 180 angle = Math.PI; var xx = (m11 + 1) / 2; var yy = (m22 + 1) / 2; var zz = (m33 + 1) / 2; var xy = (m12 + m21) / 4; var xz = (m13 + m31) / 4; var yz = (m23 + m32) / 4; if (xx > yy && xx > zz) { // m11 is the largest diagonal term if (xx < epsilon) { x = 0; y = 0.707106781; z = 0.707106781; } else { x = Math.sqrt(xx); y = xy / x; z = xz / x; } } else if (yy > zz) { // m22 is the largest diagonal term if (yy < epsilon) { x = 0.707106781; y = 0; z = 0.707106781; } else { y = Math.sqrt(yy); x = xy / y; z = yz / y; } } else { // m33 is the largest diagonal term so base result on this // eslint-disable-next-line no-lonely-if if (zz < epsilon) { x = 0.707106781; y = 0.707106781; z = 0; } else { z = Math.sqrt(zz); x = xz / z; y = yz / z; } } this.set(x, y, z, angle); return this; // return 180 deg rotation } // as we have reached here there are no singularities so we can handle normally var s = Math.sqrt((m32 - m23) * (m32 - m23) + (m13 - m31) * (m13 - m31) + (m21 - m12) * (m21 - m12)); // used to normalize if (Math.abs(s) < 0.001) s = 1; // prevent divide by zero, should not happen if matrix is orthogonal and should be // caught by singularity test above, but I've left it in just in case this.x = (m32 - m23) / s; this.y = (m13 - m31) / s; this.z = (m21 - m12) / s; this.w = Math.acos((m11 + m22 + m33 - 1) / 2); return this; } }, { key: "min", value: function min(v) { this.x = Math.min(this.x, v.x); this.y = Math.min(this.y, v.y); this.z = Math.min(this.z, v.z); this.w = Math.min(this.w, v.w); return this; } }, { key: "max", value: function max(v) { this.x = Math.max(this.x, v.x); this.y = Math.max(this.y, v.y); this.z = Math.max(this.z, v.z); this.w = Math.max(this.w, v.w); return this; } }, { key: "clamp", value: function clamp(min, max) { // assumes min < max, componentwise this.x = Math.max(min.x, Math.min(max.x, this.x)); this.y = Math.max(min.y, Math.min(max.y, this.y)); this.z = Math.max(min.z, Math.min(max.z, this.z)); this.w = Math.max(min.w, Math.min(max.w, this.w)); return this; } }, { key: "clampScalar", value: function clampScalar(minVal, maxVal) { this.x = Math.max(minVal, Math.min(maxVal, this.x)); this.y = Math.max(minVal, Math.min(maxVal, this.y)); this.z = Math.max(minVal, Math.min(maxVal, this.z)); this.w = Math.max(minVal, Math.min(maxVal, this.w)); return this; } }, { key: "clampLength", value: function clampLength(min, max) { var length = this.length; return this.divideScalar(length || 1).multiplyScalar(Math.max(min, Math.min(max, length))); } }, { key: "floor", value: function floor() { this.x = Math.floor(this.x); this.y = Math.floor(this.y); this.z = Math.floor(this.z); this.w = Math.floor(this.w); return this; } }, { key: "ceil", value: function ceil() { this.x = Math.ceil(this.x); this.y = Math.ceil(this.y); this.z = Math.ceil(this.z); this.w = Math.ceil(this.w); return this; } }, { key: "round", value: function round() { this.x = Math.round(this.x); this.y = Math.round(this.y); this.z = Math.round(this.z); this.w = Math.round(this.w); return this; } }, { key: "roundToZero", value: function roundToZero() { this.x = this.x < 0 ? Math.ceil(this.x) : Math.floor(this.x); this.y = this.y < 0 ? Math.ceil(this.y) : Math.floor(this.y); this.z = this.z < 0 ? Math.ceil(this.z) : Math.floor(this.z); this.w = this.w < 0 ? Math.ceil(this.w) : Math.floor(this.w); return this; } }, { key: "negate", value: function negate() { this.x = -this.x; this.y = -this.y; this.z = -this.z; this.w = -this.w; return this; } }, { key: "dot", value: function dot(v) { return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w; } }, { key: "lengthSq", get: function get() { return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w; } }, { key: "length", get: function get() { return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w); } }, { key: "manhattanLength", get: function get() { return Math.abs(this.x) + Math.abs(this.y) + Math.abs(this.z) + Math.abs(this.w); } }, { key: "normalize", value: function normalize() { return this.divideScalar(this.length || 1); } }, { key: "setLength", value: function setLength(length) { return this.normalize().multiplyScalar(length); } }, { key: "lerp", value: function lerp(v, alpha) { this.x += (v.x - this.x) * alpha; this.y += (v.y - this.y) * alpha; this.z += (v.z - this.z) * alpha; this.w += (v.w - this.w) * alpha; return this; } }, { key: "lerpVectors", value: function lerpVectors(v1, v2, alpha) { this.x = v1.x + (v2.x - v1.x) * alpha; this.y = v1.y + (v2.y - v1.y) * alpha; this.z = v1.z + (v2.z - v1.z) * alpha; this.w = v1.w + (v2.w - v1.w) * alpha; return this; } }, { key: "equals", value: function equals(v) { return v.x === this.x && v.y === this.y && v.z === this.z && v.w === this.w; } }, { key: "fromArray", value: function fromArray(array) { var offset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; this.x = array[offset]; this.y = array[offset + 1]; this.z = array[offset + 2]; this.w = array[offset + 3]; return this; } }, { key: "toArray", value: function toArray() { var array = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var offset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; array[offset] = this.x; array[offset + 1] = this.y; array[offset + 2] = this.z; array[offset + 3] = this.w; return array; } }, { key: "random", value: function random() { this.x = Math.random(); this.y = Math.random(); this.z = Math.random(); this.w = Math.random(); return this; } }]); }();