var LineIntersectionHelper = {
    //Get line and local plane's intersection points.
    getLineIntersectionPoints: function (lines, localPlane) {
        var points = [];
        lines.forEach(line => {
            let pointOfIntersection = localPlane.intersectLine(line, new THREE.Vector3());
            if (pointOfIntersection) {
                tryAddDistinctVector3(points, pointOfIntersection.clone());
            }
        });

        return points;
    },

    //Get raycaster and local plane's intersection points.
    getRaycasterIntersectionPoints: function (raycaster, localPlane) {
        let intersectPlanePoint = new THREE.Vector3();
        if (raycaster.ray.intersectsPlane(localPlane)) {
            raycaster.ray.intersectPlane(localPlane, intersectPlanePoint);
        }
        return intersectPlanePoint;
    },

    //获取切割后的顶点
    getActiveMeshPoints: function (clippingPlanes, verticePoints, normal) {
        if (clippingPlanes == undefined)
            return verticePoints;

        let intersectPoint = new THREE.Vector3();
        clippingPlanes.forEach(plane => {
            let lines = [];
            for (let cursor = 0; cursor < verticePoints.length - 1; cursor++) {
                lines.push(new THREE.Line3(verticePoints[cursor], verticePoints[cursor + 1]));
            }
            lines.push(new THREE.Line3(verticePoints[verticePoints.length - 1], verticePoints[0]));
            //计算切割后的新顶点列表
            let pointsNeedToAdd = [];

            lines.forEach(line => {
                if (plane.intersectsLine(line)) {
                    //计算切面与边框线的交点
                    plane.intersectLine(line, intersectPoint);
                    if (intersectPoint != undefined) {
                        //判断交点是否需要添加到切割后的新顶点列表中
                        let needAdd = verticePoints.every(verticePoint => {
                            //边框线与切面交点等于其中一个顶点，则不需添加
                            if (verticePoint.xyzFixedEq(intersectPoint, 4)) {
                                return false;
                            }
                            return true;
                        });

                        if (needAdd) {
                            pointsNeedToAdd.push(intersectPoint.clone());
                        }
                    }
                }
            });
            //查找未被切割的顶点列表
            let pointsNeedToKeep = [];
            if (pointsNeedToAdd.length > 0) {
                for (let cursor = 0; cursor < verticePoints.length; cursor++) {
                    let direction = pointsNeedToAdd[0].clone().sub(verticePoints[cursor]);
                    //顶点到交点向量与平面法向量的夹角为钝角或直角，则保留该顶点
                    if (direction.dot(plane.normal).toFixed(4) <= 0) {
                        pointsNeedToKeep.push(verticePoints[cursor]);
                    }
                }
                //切割后的新顶点与未被切割的顶点构成新的顶点列表
                let tempPoints = pointsNeedToKeep.concat(pointsNeedToAdd);
                verticePoints = PolyPointsTool.clockwiseSortPoints(tempPoints, normal);
            }
        });
        return verticePoints;
    },
};






