@turbox3d/math
Version:
Large-scale graphics application math library
238 lines (237 loc) • 7.13 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Euler = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _Quaternion = require("./Quaternion");
var _Vector = require("./Vector3");
var _Matrix = require("./Matrix4");
var _MathUtils = require("../MathUtils");
var _matrix = new _Matrix.Matrix4();
var _quaternion = new _Quaternion.Quaternion();
var Euler = exports.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;
(0, _classCallCheck2["default"])(this, Euler);
this._onChangeCallback = function () {
//
};
this.isEuler = true;
this._x = x;
this._y = y;
this._z = z;
this._order = order;
}
return (0, _createClass2["default"])(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.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 _Vector.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'];