UNPKG

@turbox3d/math

Version:

Large-scale graphics application math library

232 lines 6.82 kB
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import { Quaternion } from './Quaternion'; import { Vector3 } from './Vector3'; import { Matrix4 } from './Matrix4'; import { MathUtils } from '../MathUtils'; var _matrix = new Matrix4(); var _quaternion = new Quaternion(); var Euler = /*#__PURE__*/function () { function Euler() { 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 order = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : Euler.DefaultOrder; _classCallCheck(this, Euler); this._onChangeCallback = function () { // }; this.isEuler = true; this._x = x; this._y = y; this._z = z; this._order = order; } return _createClass(Euler, [{ key: "x", get: function get() { return this._x; }, set: function set(value) { this._x = value; this._onChangeCallback(); } }, { key: "y", get: function get() { return this._y; }, set: function set(value) { this._y = value; this._onChangeCallback(); } }, { key: "z", get: function get() { return this._z; }, set: function set(value) { this._z = value; this._onChangeCallback(); } }, { key: "order", get: function get() { return this._order; }, set: function set(value) { this._order = value; this._onChangeCallback(); } }, { key: "set", value: function set(x, y, z, order) { this._x = x; this._y = y; this._z = z; this._order = order || this._order; this._onChangeCallback(); return this; } }, { key: "clone", value: function clone() { return new Euler(this._x, this._y, this._z, this._order); } }, { key: "copy", value: function copy(euler) { this._x = euler._x; this._y = euler._y; this._z = euler._z; this._order = euler._order; this._onChangeCallback(); return this; } }, { key: "setFromRotationMatrix", value: function setFromRotationMatrix(m, order) { var clamp = MathUtils.clamp; // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled) 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]; order = order || this._order; switch (order) { case 'XYZ': this._y = Math.asin(clamp(m13, -1, 1)); if (Math.abs(m13) < 0.9999999) { this._x = Math.atan2(-m23, m33); this._z = Math.atan2(-m12, m11); } else { this._x = Math.atan2(m32, m22); this._z = 0; } break; case 'YXZ': this._x = Math.asin(-clamp(m23, -1, 1)); if (Math.abs(m23) < 0.9999999) { this._y = Math.atan2(m13, m33); this._z = Math.atan2(m21, m22); } else { this._y = Math.atan2(-m31, m11); this._z = 0; } break; case 'ZXY': this._x = Math.asin(clamp(m32, -1, 1)); if (Math.abs(m32) < 0.9999999) { this._y = Math.atan2(-m31, m33); this._z = Math.atan2(-m12, m22); } else { this._y = 0; this._z = Math.atan2(m21, m11); } break; case 'ZYX': this._y = Math.asin(-clamp(m31, -1, 1)); if (Math.abs(m31) < 0.9999999) { this._x = Math.atan2(m32, m33); this._z = Math.atan2(m21, m11); } else { this._x = 0; this._z = Math.atan2(-m12, m22); } break; case 'YZX': this._z = Math.asin(clamp(m21, -1, 1)); if (Math.abs(m21) < 0.9999999) { this._x = Math.atan2(-m23, m22); this._y = Math.atan2(-m31, m11); } else { this._x = 0; this._y = Math.atan2(m13, m33); } break; case 'XZY': this._z = Math.asin(-clamp(m12, -1, 1)); if (Math.abs(m12) < 0.9999999) { this._x = Math.atan2(m32, m22); this._y = Math.atan2(m13, m11); } else { this._x = Math.atan2(-m23, m33); this._y = 0; } break; default: console.warn("THREE.Euler: .setFromRotationMatrix() encountered an unknown order: ".concat(order)); } this._order = order; this._onChangeCallback(); return this; } }, { key: "setFromQuaternion", value: function setFromQuaternion(q, order) { _matrix.makeRotationFromQuaternion(q); return this.setFromRotationMatrix(_matrix, order); } }, { key: "setFromVector3", value: function setFromVector3(v, order) { return this.set(v.x, v.y, v.z, order || this._order); } }, { key: "reorder", value: function reorder(newOrder) { // WARNING: this discards revolution information -bhouston _quaternion.setFromEuler(this); return this.setFromQuaternion(_quaternion, newOrder); } }, { key: "equals", value: function equals(euler) { return euler._x === this._x && euler._y === this._y && euler._z === this._z && euler._order === this._order; } }, { key: "fromArray", value: function fromArray(array) { this._x = array[0]; this._y = array[1]; this._z = array[2]; if (array[3] !== undefined) this._order = array[3]; this._onChangeCallback(); 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._order; return array; } }, { key: "toVector3", value: function toVector3(optionalResult) { if (optionalResult) { return optionalResult.set(this._x, this._y, this._z); } return new Vector3(this._x, this._y, this._z); } }, { key: "_onChange", value: function _onChange(callback) { this._onChangeCallback = callback; return this; } }]); }(); Euler.DefaultOrder = 'XYZ'; Euler.RotationOrders = ['XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX']; export { Euler };