"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.v4 = exports.Vec4 = void 0;
var eventhandler_1 = require("../render/eventhandler");
var Vec4 = /** @class */ (function (_super) {
    __extends(Vec4, _super);
    function Vec4(_x, _y, _z, _w) {
        if (_x === void 0) { _x = 0; }
        if (_y === void 0) { _y = 0; }
        if (_z === void 0) { _z = 0; }
        if (_w === void 0) { _w = 1; }
        var _this = _super.call(this) || this;
        _this._x = _x;
        _this._y = _y;
        _this._z = _z;
        _this._w = _w;
        _this.isVec4 = true;
        return _this;
    }
    Object.defineProperty(Vec4.prototype, "x", {
        get: function () {
            return this._x;
        },
        set: function (value) {
            if (this._x !== value) {
                this._x = value;
                this.fire('change', 'x', this._x, value);
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Vec4.prototype, "y", {
        get: function () {
            return this._y;
        },
        set: function (value) {
            if (this._y !== value) {
                this._y = value;
                this.fire('change', 'y', this._y, value);
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Vec4.prototype, "z", {
        get: function () {
            return this._z;
        },
        set: function (value) {
            if (this._z !== value) {
                this._z = value;
                this.fire('change', 'z', this._z, value);
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Vec4.prototype, "w", {
        get: function () {
            return this._w;
        },
        set: function (value) {
            if (this._w !== value) {
                this._w = value;
                this.fire('change', 'w', this._w, value);
            }
        },
        enumerable: false,
        configurable: true
    });
    Vec4.isVec4 = function (v) {
        return !isNaN(v.x) && !isNaN(v.y) && !isNaN(v.z) && !isNaN(v.w);
    };
    Object.defineProperty(Vec4.prototype, "width", {
        get: function () {
            return this._z;
        },
        set: function (value) {
            this._z = value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Vec4.prototype, "height", {
        get: function () {
            return this._w;
        },
        set: function (value) {
            this._w = value;
        },
        enumerable: false,
        configurable: true
    });
    Vec4.prototype.set = function (x, y, z, w) {
        this._x = x;
        this._y = y;
        this._z = z;
        this._w = w;
        return this;
    };
    Vec4.prototype.setScalar = function (scalar) {
        this._x = scalar;
        this._y = scalar;
        this._z = scalar;
        this._w = scalar;
        return this;
    };
    Vec4.prototype.setX = function (x) {
        this._x = x;
        return this;
    };
    Vec4.prototype.setY = function (y) {
        this._y = y;
        return this;
    };
    Vec4.prototype.setZ = function (z) {
        this._z = z;
        return this;
    };
    Vec4.prototype.setW = function (w) {
        this._w = w;
        return this;
    };
    Vec4.prototype.setComponent = function (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: " + index);
        }
        return this;
    };
    Vec4.prototype.getComponent = function (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: " + index);
        }
    };
    Vec4.prototype.clone = function () {
        return new Vec4(this._x, this._y, this._z, this._w);
    };
    Vec4.prototype.copy = function (v) {
        this._x = v.x;
        this._y = v.y;
        this._z = v.z;
        this._w = v.w !== undefined ? v.w : 1;
        return this;
    };
    Vec4.prototype.add = function (v, w) {
        if (w !== undefined) {
            console.warn("Vec4: .add() now only accepts one argument. Use .addVecs( a, b ) instead.");
            return this.addVecs(v, w);
        }
        this._x += v.x;
        this._y += v.y;
        this._z += v.z;
        this._w += v.w;
        return this;
    };
    Vec4.prototype.addScalar = function (s) {
        this._x += s;
        this._y += s;
        this._z += s;
        this._w += s;
        return this;
    };
    Vec4.prototype.addVecs = function (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;
    };
    Vec4.prototype.addScaledVec = function (v, s) {
        this._x += v.x * s;
        this._y += v.y * s;
        this._z += v.z * s;
        this._w += v.w * s;
        return this;
    };
    Vec4.prototype.sub = function (v, w) {
        if (w !== undefined) {
            return this.subVecs(v, w);
        }
        this._x -= v.x;
        this._y -= v.y;
        this._z -= v.z;
        this._w -= v.w;
        return this;
    };
    Vec4.prototype.subScalar = function (s) {
        this._x -= s;
        this._y -= s;
        this._z -= s;
        this._w -= s;
        return this;
    };
    Vec4.prototype.subVecs = function (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;
    };
    Vec4.prototype.multiplyScalar = function (scalar) {
        this._x *= scalar;
        this._y *= scalar;
        this._z *= scalar;
        this._w *= scalar;
        return this;
    };
    Vec4.prototype.applyMat4 = function (m) {
        var x = this._x, y = this._y, z = this._z, 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;
    };
    Vec4.prototype.divideScalar = function (scalar) {
        return this.multiplyScalar(1 / scalar);
    };
    Vec4.prototype.setAxisAngleFromQuat = function (q) {
        // http://www.euclideanspace.com/maths/geometry/rotations/conversions/QuatToAngle/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;
    };
    Vec4.prototype.setAxisAngleFromRotationMat = function (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, x, y, z, // variables for result
        epsilon = 0.01, // margin to allow for rounding errors
        epsilon2 = 0.1, // margin to distinguish between 0 and 180 degrees
        te = m.elements, m11 = te[0], m12 = te[4], m13 = te[8], m21 = te[1], m22 = te[5], m23 = te[9], m31 = te[2], m32 = te[6], 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
                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;
    };
    Vec4.prototype.min = function (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;
    };
    Vec4.prototype.max = function (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;
    };
    Vec4.prototype.clamp = function (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;
    };
    Vec4.prototype.clampScalar = function (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;
    };
    Vec4.prototype.clampLength = function (min, max) {
        var length = this.length();
        return this.divideScalar(length || 1).multiplyScalar(Math.max(min, Math.min(max, length)));
    };
    Vec4.prototype.floor = function () {
        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;
    };
    Vec4.prototype.ceil = function () {
        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;
    };
    Vec4.prototype.round = function () {
        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;
    };
    Vec4.prototype.roundToZero = function () {
        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;
    };
    Vec4.prototype.negate = function () {
        this._x = -this._x;
        this._y = -this._y;
        this._z = -this._z;
        this._w = -this._w;
        return this;
    };
    Vec4.prototype.dot = function (v) {
        return this._x * v.x + this._y * v.y + this._z * v.z + this._w * v.w;
    };
    Vec4.prototype.lengthSq = function () {
        return (this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w);
    };
    Vec4.prototype.length = function () {
        return Math.sqrt(this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w);
    };
    Vec4.prototype.manhattanLength = function () {
        return (Math.abs(this._x) + Math.abs(this._y) + Math.abs(this._z) + Math.abs(this._w));
    };
    Vec4.prototype.normalize = function () {
        return this.divideScalar(this.length() || 1);
    };
    Vec4.prototype.setLength = function (length) {
        return this.normalize().multiplyScalar(length);
    };
    Vec4.prototype.lerp = function (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;
    };
    Vec4.prototype.lerpVecs = function (v1, v2, alpha) {
        return this.subVecs(v2, v1)
            .multiplyScalar(alpha)
            .add(v1);
    };
    Vec4.prototype.equals = function (v) {
        return v.x === this._x && v.y === this._y && v.z === this._z && v.w === this._w;
    };
    Vec4.prototype.fromArray = function (array, offset) {
        if (offset === void 0) { offset = 0; }
        this._x = array[offset];
        this._y = array[offset + 1];
        this._z = array[offset + 2];
        this._w = array[offset + 3];
        return this;
    };
    Vec4.prototype.toArray = function (array, offset) {
        if (array === void 0) { array = []; }
        if (offset === void 0) { offset = 0; }
        array[offset] = this._x;
        array[offset + 1] = this._y;
        array[offset + 2] = this._z;
        array[offset + 3] = this._w;
        return array;
    };
    Vec4.prototype.fromBufferAttribute = function (attribute, index, offset) {
        if (offset !== undefined) {
            console.warn("Vec4: offset has been removed from .fromBufferAttribute().");
        }
        this._x = attribute.getX(index);
        this._y = attribute.getY(index);
        this._z = attribute.getZ(index);
        this._w = attribute.getW(index);
        return this;
    };
    return Vec4;
}(eventhandler_1.EventHandler));
exports.Vec4 = Vec4;
function v4(x, y, z, w) {
    return new Vec4(x, y, z, w);
}
exports.v4 = v4;
