"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Frustum = void 0;
var Plane_1 = require("./Plane");
var Sphere_1 = require("./Sphere");
var _sphere = new Sphere_1.Sphere();
/**
 * 视锥体
 */
var Frustum = /** @class */ (function () {
    function Frustum() {
        this.planes = [new Plane_1.Plane(), new Plane_1.Plane(), new Plane_1.Plane(), new Plane_1.Plane(), new Plane_1.Plane(), new Plane_1.Plane()];
    }
    Object.defineProperty(Frustum.prototype, "front", {
        get: function () {
            return this.planes[0];
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Frustum.prototype, "back", {
        get: function () {
            return this.planes[1];
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Frustum.prototype, "top", {
        get: function () {
            return this.planes[2];
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Frustum.prototype, "bottom", {
        get: function () {
            return this.planes[3];
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Frustum.prototype, "left", {
        get: function () {
            return this.planes[4];
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Frustum.prototype, "right", {
        get: function () {
            return this.planes[5];
        },
        enumerable: false,
        configurable: true
    });
    /**
     * 从投影矩阵计算视锥体
     * @param m
     * @returns
     */
    Frustum.prototype.setFromProjectionMatrix = function (m) {
        var planes = this.planes;
        var me = m.elements;
        var me0 = me[0], me1 = me[1], me2 = me[2], me3 = me[3];
        var me4 = me[4], me5 = me[5], me6 = me[6], me7 = me[7];
        var me8 = me[8], me9 = me[9], me10 = me[10], me11 = me[11];
        var me12 = me[12], me13 = me[13], me14 = me[14], me15 = me[15];
        planes[0].setComponents(me3 - me0, me7 - me4, me11 - me8, me12 - me15).normalize();
        planes[1].setComponents(me3 + me0, me7 + me4, me11 + me8, me12 + me15).normalize();
        planes[2].setComponents(me3 + me1, me7 + me5, me11 + me9, me13 + me15).normalize();
        planes[3].setComponents(me3 - me1, me7 - me5, me11 - me9, me13 - me15).normalize();
        planes[4].setComponents(me3 - me2, me7 - me6, me11 - me10, me14 - me15).normalize();
        planes[5].setComponents(me3 + me2, me7 + me6, me11 + me10, me14 + me15).normalize();
        return this;
    };
    Frustum.fromProjectionMatrix = function (m) {
        return new Frustum().setFromProjectionMatrix(m);
    };
    Frustum.prototype.setFromPerspective = function (position, target, up, fov, aspect, near, far) {
        var direction = target.clone().sub(position);
    };
    Frustum.prototype.intersectsObject = function (geometry, mat) {
        if (!geometry.boundingSphere)
            geometry.computeBoundingSphere();
        _sphere.copy(geometry.boundingSphere).applyMat4(mat);
        return this.intersectsSphere(_sphere);
    };
    // intersectsSprite(sprite) {
    //     _sphere.center.set(0, 0, 0);
    //     _sphere.radius = 0.7071067811865476;
    //     _sphere.applyMatrix4(sprite.matrixWorld);
    //     return this.intersectsSphere(_sphere);
    // }
    Frustum.prototype.intersectsSphere = function (sphere) {
        var planes = this.planes;
        var center = sphere.center;
        var negRadius = -sphere.radius;
        for (var i = 0; i < 6; i++) {
            var distance = planes[i].distancePoint(center);
            if (distance < negRadius) {
                return false;
            }
        }
        return true;
    };
    Frustum.prototype.intersectsSphereComponents = function (cx, cy, cz, radius) {
        _sphere.setComponents(cx, cy, cz, radius);
        return this.intersectsSphere(_sphere);
    };
    Frustum.prototype.containsPoint = function (point) {
        var planes = this.planes;
        for (var i = 0; i < 6; i++) {
            if (planes[i].distancePoint(point) < 0) {
                return false;
            }
        }
        return true;
    };
    Frustum.prototype.intersectSegment = function (segment) {
        var planes = this.planes;
        for (var i = 0; i < 6; i++) {
            var intersectPoint = planes[i].intersectSegmentLw(segment);
            if (intersectPoint !== null) {
                return intersectPoint;
            }
        }
        return null;
    };
    Frustum.prototype.simpleIntersectVS = function (vs) {
        var _this = this;
        var contains = vs.map(function (v) { return _this.containsPoint(v); });
        var res = [];
        var oneres = [];
        for (var i = 0; i < vs.length; i++) {
            var p = vs[i];
            var c0 = contains[i];
            p.index = i;
            if (c0) {
                res.push(p);
            }
        }
        if (res.length > 0) {
            var startI = res[0].index;
            var endI = res[res.length - 1].index;
            if (startI > 0)
                res.unshift(vs[startI - 1]);
            if (endI < vs.length - 2)
                res.push(vs[endI + 1]);
        }
        return res;
    };
    Frustum.prototype.copy = function (frustum) {
    };
    Frustum.prototype.clone = function () {
        return new this.constructor().copy(this);
    };
    return Frustum;
}());
exports.Frustum = Frustum;
