Esempio n. 1
0
    public static List <Triangle3D> SplitPlaneTriangleIntersection(Plane3D plane, Triangle3D inTriangle, double epsilon = 1e-7)
    {
        Polygon3D poly = new Polygon3D();

        poly.vertices.Add(inTriangle[0]);
        poly.vertices.Add(inTriangle[1]);
        poly.vertices.Add(inTriangle[2]);
        Polygon3D front;
        Polygon3D back;
        var       res = poly.SplitWithPlane(plane, out front, out back, true);

        var triangleList = new List <Triangle3D>();

        if (res != Polygon3D.SplitType.SP_Split)
        {
            triangleList.AddRange(new[] { inTriangle });
            return(triangleList);
        }


        var polygons = new[] { front, back };

        foreach (var p in polygons)
        {
            int offset = 0;
            if (p.vertices.Count > 3)
            {
                double o0a0 = new Triangle3D(p.vertices[0], p.vertices[1], p.vertices[2]).ComputeArea();
                double o0a1 = new Triangle3D(p.vertices[0], p.vertices[2], p.vertices[3]).ComputeArea();
                double o1a0 = new Triangle3D(p.vertices[1], p.vertices[2], p.vertices[3]).ComputeArea();
                double o1a1 = new Triangle3D(p.vertices[1], p.vertices[3], p.vertices[0]).ComputeArea();
                // find optimat split by taking the least difference between triangle areas
                if (Math.Abs(o0a0 - o0a1) > Math.Abs(o1a0 - o1a1))
                {
                    offset = 1;
                }
            }
            for (int i = 2; i < p.vertices.Count; i++)
            {
                triangleList.Add(new Triangle3D(p.vertices[offset],
                                                p.vertices[(i - 1 + offset) % p.vertices.Count],
                                                p.vertices[(i + offset) % p.vertices.Count]
                                                ));
            }
        }

        return(triangleList);
    }