var Euler = /** @class */ (function () {
    function Euler(x, y, z, order) {
        this.x = x;
        this.y = y;
        this.z = z;
        this.order = order;
    }
    Euler.create = function (x, y, z, order) {
        if (x === void 0) { x = 0; }
        if (y === void 0) { y = 0; }
        if (z === void 0) { z = 0; }
        if (order === void 0) { order = "xyz"; }
        return { x: x, y: y, z: z, order: order };
    };
    Euler.threeaxisrot = function (r11, r12, r21, r31, r32, order) {
        if (order === void 0) { order = "xyz"; }
        var result = Euler.create(undefined, undefined, undefined, order);
        result.y = Math.asin(r21);
        result.x = Math.atan2(r11, r12);
        result.z = Math.atan2(r31, r32);
        return result;
    };
    /**
     * Creates Euler angles from Quaternion
     * @param quaternion Quaternion
     * @param order EulerOrder
     */
    Euler.createFromQuaternion = function (quaternion, order) {
        if (order === void 0) { order = "xyz"; }
        // from: http://bediyap.com/programming/convert-quaternion-to-euler-rotations/
        var q = quaternion;
        if (order === "xyz") {
            return Euler.threeaxisrot(-2 * (q.y * q.z - q.w * q.x), q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z, 2 * (q.x * q.z + q.w * q.y), -2 * (q.x * q.y - q.w * q.z), q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z, order);
        }
        else {
            throw new Error("Unknown order");
        }
    };
    /**
     * Creates Euler angles from Rotation Matrix
     * @param matrix Matrix4
     * @param order EulerOrder
     */
    Euler.createFromRotationMatrix = function (matrix, order) {
        if (order === void 0) { order = "xyz"; }
        // from: https://www.geometrictools.com/Documentation/EulerAngles.pdf
        var r00 = matrix.elements[0][0];
        var r01 = matrix.elements[0][1];
        var r02 = matrix.elements[0][2];
        var r10 = matrix.elements[1][0];
        var r11 = matrix.elements[1][1];
        var r12 = matrix.elements[1][2];
        var r20 = matrix.elements[2][0];
        var r21 = matrix.elements[2][1];
        var r22 = matrix.elements[2][2];
        var result = Euler.create(undefined, undefined, undefined, order);
        if (order === "xyz") {
            if (r02 < 0.99999) {
                if (r02 > -0.99999) {
                    result.y = Math.asin(r02);
                    result.x = Math.atan2(-r12, r22);
                    result.z = Math.atan2(-r01, r00);
                }
                else { // r02 = -1
                    // Not a unique solution: thetaZ − thetaX = atan2( r10 , r11 )
                    result.y = -Math.PI / 2;
                    result.x = -Math.atan2(r10, r11);
                    result.z = 0;
                }
            }
            else { // r02 = +1
                // Not a unique solution: thetaZ + thetaX = atan2( r10 , r11 )
                result.y = Math.PI / 2;
                result.x = Math.atan2(r10, r11);
                result.z = 0;
            }
        }
        else {
            throw new Error("Unknown order");
        }
        return result;
    };
    return Euler;
}());
export { Euler };
