@turbox3d/math
Version:
Large-scale graphics application math library
717 lines (716 loc) • 21.2 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Vector3 = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _MathUtils = require("../MathUtils");
var _Quaternion = require("./Quaternion");
var _Tolerance = require("./Tolerance");
var _Vector = require("./Vector2");
var _quaternion = new _Quaternion.Quaternion();
var Vector3 = exports.Vector3 = /*#__PURE__*/function () {
function Vector3() {
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;
(0, _classCallCheck2["default"])(this, Vector3);
this.isVector3 = true;
this.x = x;
this.y = y;
this.z = z;
}
return (0, _createClass2["default"])(Vector3, [{
key: "set",
value: function set(x, y, z) {
if (z === undefined) z = this.z; // sprite.scale.set(x,y)
this.x = x;
this.y = y;
this.z = z;
return this;
}
}, {
key: "setScalar",
value: function setScalar(scalar) {
this.x = scalar;
this.y = scalar;
this.z = 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: "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;
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;
default:
throw new Error("index is out of range: ".concat(index));
}
}
}, {
key: "clone",
value: function clone() {
return new Vector3(this.x, this.y, this.z);
}
}, {
key: "copy",
value: function copy(v) {
this.x = v.x;
this.y = v.y;
this.z = v.z;
return this;
}
}, {
key: "add",
value: function add(v) {
this.x += v.x;
this.y += v.y;
this.z += v.z;
return this;
}
}, {
key: "added",
value: function added(v) {
return new Vector3(this.x + v.x, this.y + v.y, this.z + v.z);
}
}, {
key: "addScalar",
value: function addScalar(s) {
this.x += s;
this.y += s;
this.z += 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;
return this;
}
}, {
key: "addScaledVector",
value: function addScaledVector(v, s) {
this.x += v.x * s;
this.y += v.y * s;
this.z += v.z * s;
return this;
}
}, {
key: "sub",
value: function sub(v) {
this.x -= v.x;
this.y -= v.y;
this.z -= v.z;
return this;
}
}, {
key: "subtracted",
value: function subtracted(v) {
return new Vector3(this.x - v.x, this.y - v.y, this.z - v.z);
}
}, {
key: "subScalar",
value: function subScalar(s) {
this.x -= s;
this.y -= s;
this.z -= 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;
return this;
}
}, {
key: "multiply",
value: function multiply(v) {
this.x *= v.x;
this.y *= v.y;
this.z *= v.z;
return this;
}
}, {
key: "multiplied",
value: function multiplied(v) {
return new Vector3(this.x * v.x, this.y * v.y, this.z * v.z);
}
}, {
key: "multiplyScalar",
value: function multiplyScalar(scalar) {
this.x *= scalar;
this.y *= scalar;
this.z *= scalar;
return this;
}
}, {
key: "multiplyVectors",
value: function multiplyVectors(a, b) {
this.x = a.x * b.x;
this.y = a.y * b.y;
this.z = a.z * b.z;
return this;
}
}, {
key: "applyEuler",
value: function applyEuler(euler) {
return this.applyQuaternion(_quaternion.setFromEuler(euler));
}
}, {
key: "appliedEuler",
value: function appliedEuler(euler) {
return this.appliedQuaternion(_quaternion.setFromEuler(euler));
}
}, {
key: "applyAxisAngle",
value: function applyAxisAngle(axis, angle) {
return this.applyQuaternion(_quaternion.setFromAxisAngle(axis, angle));
}
}, {
key: "applyMatrix3",
value: function applyMatrix3(m) {
var x = this.x;
var y = this.y;
var z = this.z;
var e = m.elements;
this.x = e[0] * x + e[3] * y + e[6] * z;
this.y = e[1] * x + e[4] * y + e[7] * z;
this.z = e[2] * x + e[5] * y + e[8] * z;
return this;
}
}, {
key: "applyNormalMatrix",
value: function applyNormalMatrix(m) {
return this.applyMatrix3(m).normalize();
}
}, {
key: "applyMatrix4",
value: function applyMatrix4(m) {
var x = this.x;
var y = this.y;
var z = this.z;
var e = m.elements;
var w = 1 / (e[3] * x + e[7] * y + e[11] * z + e[15]);
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;
return this;
}
}, {
key: "appliedMatrix4",
value: function appliedMatrix4(m) {
var x = this.x;
var y = this.y;
var z = this.z;
var e = m.elements;
var w = 1 / (e[3] * x + e[7] * y + e[11] * z + e[15]);
var newX = (e[0] * x + e[4] * y + e[8] * z + e[12]) * w;
var newY = (e[1] * x + e[5] * y + e[9] * z + e[13]) * w;
var newZ = (e[2] * x + e[6] * y + e[10] * z + e[14]) * w;
// NOTE: translation has no effect on vector.
return new Vector3(newX - e[12] * w, newY - e[13] * w, newZ - e[14] * w);
}
}, {
key: "applyQuaternion",
value: function applyQuaternion(q) {
var x = this.x;
var y = this.y;
var z = this.z;
var qx = q.x;
var qy = q.y;
var qz = q.z;
var qw = q.w;
// calculate quat * vector
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
this.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
this.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
this.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
return this;
}
}, {
key: "appliedQuaternion",
value: function appliedQuaternion(q) {
var x = this.x;
var y = this.y;
var z = this.z;
var qx = q.x;
var qy = q.y;
var qz = q.z;
var qw = q.w;
// calculate quat * vector
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
var newX = ix * qw + iw * -qx + iy * -qz - iz * -qy;
var newY = iy * qw + iw * -qy + iz * -qx - ix * -qz;
var newZ = iz * qw + iw * -qz + ix * -qy - iy * -qx;
return new Vector3(newX, newY, newZ);
}
}, {
key: "transformDirection",
value: function transformDirection(m) {
// input: THREE.Matrix4 affine matrix
// vector interpreted as a direction
var x = this.x;
var y = this.y;
var z = this.z;
var e = m.elements;
this.x = e[0] * x + e[4] * y + e[8] * z;
this.y = e[1] * x + e[5] * y + e[9] * z;
this.z = e[2] * x + e[6] * y + e[10] * z;
return this.normalize();
}
}, {
key: "divide",
value: function divide(v) {
this.x /= v.x;
this.y /= v.y;
this.z /= v.z;
return this;
}
}, {
key: "divideScalar",
value: function divideScalar(scalar) {
return this.multiplyScalar(1 / scalar);
}
}, {
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);
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);
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));
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));
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);
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);
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);
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);
return this;
}
}, {
key: "negate",
value: function negate() {
this.x = -this.x;
this.y = -this.y;
this.z = -this.z;
return this;
}
}, {
key: "dot",
value: function dot(v) {
return this.x * v.x + this.y * v.y + this.z * v.z;
}
// TODO lengthSquared?
}, {
key: "lengthSq",
get: function get() {
return this.x * this.x + this.y * this.y + this.z * this.z;
}
}, {
key: "length",
get: function get() {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
}
}, {
key: "manhattanLength",
get: function get() {
return Math.abs(this.x) + Math.abs(this.y) + Math.abs(this.z);
}
}, {
key: "normalize",
value: function normalize() {
return this.divideScalar(this.length || 1);
}
}, {
key: "normalized",
value: function normalized() {
var tol = 1e-16;
var length = this.lengthSq;
if (length === 0 || _MathUtils.MathUtils.isEqual(length, 1, tol)) {
return new Vector3(this.x, this.y, this.z);
}
length = Math.sqrt(length);
var newX = this.x / length;
var newY = this.y / length;
var newZ = this.z / length;
if (!Number.isFinite(newX) || !Number.isFinite(newY) || !Number.isFinite(newZ)) {
return new Vector3(this.x, this.y, this.z);
}
return new Vector3(newX, newY, newZ);
}
}, {
key: "reverse",
value: function reverse() {
this.x = -this.x;
this.y = -this.y;
this.z = -this.z;
return this;
}
}, {
key: "reversed",
value: function reversed() {
return new Vector3(-this.x, -this.y, -this.z);
}
}, {
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;
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;
return this;
}
}, {
key: "toVector2",
value: function toVector2() {
return new _Vector.Vector2(this.x, this.y);
}
}, {
key: "cross",
value: function cross(v) {
return this.crossVectors(this, v);
}
}, {
key: "crossed",
value: function crossed(vec) {
return new Vector3(this.y * vec.z - this.z * vec.y, this.z * vec.x - this.x * vec.z, this.x * vec.y - this.y * vec.x);
}
}, {
key: "crossVectors",
value: function crossVectors(a, b) {
var ax = a.x;
var ay = a.y;
var az = a.z;
var bx = b.x;
var by = b.y;
var bz = b.z;
this.x = ay * bz - az * by;
this.y = az * bx - ax * bz;
this.z = ax * by - ay * bx;
return this;
}
}, {
key: "projectOnVector",
value: function projectOnVector(v) {
var denominator = v.lengthSq;
if (denominator === 0) return this.set(0, 0, 0);
var scalar = v.dot(this) / denominator;
return this.copy(v).multiplyScalar(scalar);
}
}, {
key: "projectOnPlane",
value: function projectOnPlane(planeNormal) {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
_vector.copy(this).projectOnVector(planeNormal);
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return this.sub(_vector);
}
}, {
key: "reflect",
value: function reflect(normal) {
// reflect incident vector off plane orthogonal to normal
// normal is assumed to have unit length
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return this.sub(_vector.copy(normal).multiplyScalar(2 * this.dot(normal)));
}
/**
* The angle in [0, PI]
*/
}, {
key: "angle",
value: function angle(v) {
return Math.atan2(this.crossed(v).length, this.dot(v));
}
/**
* The angle in [0, 2PI)
*/
}, {
key: "angleTo",
value: function angleTo(v, vecRef) {
var crossed = this.crossed(v);
var angle = this.angle(v);
var pi2 = Math.PI * 2;
if (crossed.dot(vecRef) < 0.0 && angle < Math.PI && angle > 0) {
return pi2 - angle;
}
return angle;
}
}, {
key: "distanceTo",
value: function distanceTo(v) {
return Math.sqrt(this.distanceToSquared(v));
}
}, {
key: "distanceToSquared",
value: function distanceToSquared(v) {
var dx = this.x - v.x;
var dy = this.y - v.y;
var dz = this.z - v.z;
return dx * dx + dy * dy + dz * dz;
}
}, {
key: "manhattanDistanceTo",
value: function manhattanDistanceTo(v) {
return Math.abs(this.x - v.x) + Math.abs(this.y - v.y) + Math.abs(this.z - v.z);
}
}, {
key: "setFromMatrixPosition",
value: function setFromMatrixPosition(m) {
var e = m.elements;
this.x = e[12];
this.y = e[13];
this.z = e[14];
return this;
}
}, {
key: "setFromMatrixScale",
value: function setFromMatrixScale(m) {
var sx = this.setFromMatrixColumn(m, 0).length;
var sy = this.setFromMatrixColumn(m, 1).length;
var sz = this.setFromMatrixColumn(m, 2).length;
this.x = sx;
this.y = sy;
this.z = sz;
return this;
}
}, {
key: "setFromMatrixColumn",
value: function setFromMatrixColumn(m, index) {
return this.fromArray(m.elements, index * 4);
}
}, {
key: "setFromMatrix3Column",
value: function setFromMatrix3Column(m, index) {
return this.fromArray(m.elements, index * 3);
}
}, {
key: "equals",
value: function equals(v) {
var distTol = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _Tolerance.Tolerance.global.distTol;
var cosTol = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _Tolerance.Tolerance.global.cosTol;
var sLen0 = this.lengthSq;
var sLen1 = v.lengthSq;
if (_MathUtils.MathUtils.isZero(sLen0, distTol * distTol) && _MathUtils.MathUtils.isZero(sLen1, distTol * distTol)) {
return true;
}
// 向量距离相等并且方向相同
return _MathUtils.MathUtils.isEqual(Math.sqrt(sLen0), Math.sqrt(sLen1), distTol) && this.isSameDirection(v, new _Tolerance.Tolerance(cosTol, distTol, _Tolerance.Tolerance.global.numTol), false);
}
}, {
key: "isPerpendicular",
value: function isPerpendicular(vec, tol) {
var checkZeroVec = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
var cosTol = tol ? tol.cosTol : _Tolerance.Tolerance.global.cosTol;
var distTol = tol ? tol.distTol : _Tolerance.Tolerance.global.distTol;
var len1 = this.length;
var len2 = vec.length;
if (len1 === 0 || len2 === 0 || checkZeroVec && (_MathUtils.MathUtils.isZero(len1, distTol) || _MathUtils.MathUtils.isZero(len2, distTol))) {
return false;
}
var multiLen = len1 * len2;
return this.cross(vec).lengthSq >= Math.pow(multiLen - cosTol * multiLen, 2);
}
}, {
key: "isParallel",
value: function isParallel(vec, tol) {
var checkZeroVec = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
var cosTol = tol ? tol.cosTol : _Tolerance.Tolerance.global.cosTol;
var distTol = tol ? tol.distTol : _Tolerance.Tolerance.global.distTol;
var len1 = this.length;
var len2 = vec.length;
if (len1 === 0 || len2 === 0 || checkZeroVec && (_MathUtils.MathUtils.isZero(len1, distTol) || _MathUtils.MathUtils.isZero(len2, distTol))) {
return false;
}
var multiLen = len1 * len2;
return Math.abs(multiLen - Math.abs(this.dot(vec))) <= cosTol * multiLen;
}
}, {
key: "isSameDirection",
value: function isSameDirection(vec, tol) {
var checkZeroVec = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
var cosTol = tol ? tol.cosTol : _Tolerance.Tolerance.global.cosTol;
var distTol = tol ? tol.distTol : _Tolerance.Tolerance.global.distTol;
var len1 = this.length;
var len2 = vec.length;
if (len1 === 0 || len2 === 0 || checkZeroVec && (_MathUtils.MathUtils.isZero(len1, distTol) || _MathUtils.MathUtils.isZero(len2, distTol))) {
return false;
}
var multiLen = len1 * len2;
return Math.abs(multiLen - this.dot(vec)) <= cosTol * multiLen;
}
}, {
key: "isOpposite",
value: function isOpposite(vec, tol) {
var checkZeroVec = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
var cosTol = tol ? tol.cosTol : _Tolerance.Tolerance.global.cosTol;
var distTol = tol ? tol.distTol : _Tolerance.Tolerance.global.distTol;
var len1 = this.length;
var len2 = vec.length;
if (len1 === 0 || len2 === 0 || checkZeroVec && (_MathUtils.MathUtils.isZero(len1, distTol) || _MathUtils.MathUtils.isZero(len2, distTol))) {
return false;
}
var multiLen = len1 * len2;
return Math.abs(multiLen + this.dot(vec)) <= cosTol * multiLen;
}
}, {
key: "isZero",
value: function isZero() {
var distTol = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _Tolerance.Tolerance.global.distTol;
return this.lengthSq < distTol * distTol;
}
}, {
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];
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;
return array;
}
}, {
key: "random",
value: function random() {
this.x = Math.random();
this.y = Math.random();
this.z = Math.random();
return this;
}
}], [{
key: "fromObject",
value: function fromObject(obj) {
return new Vector3(obj.x, obj.y, obj.z);
}
}]);
}();
var _vector = new Vector3();