Beispiel #1
0
    private void FillCutEdges(ConvexHull a, ConvexHull b, IList <Edge> edgesA, IList <Edge> edgesB, Vector3 normalPlane)
    {
        //Create outline data
        int outlineEdgeCount = edgesA.Count;

        Vector3[] outlinePoints = new Vector3[outlineEdgeCount];
        int[]     outlineEdges  = new int[outlineEdgeCount * 2];

        int startIndex = 0;

        for (int i = 0; i < outlineEdgeCount; i++)
        {
            int currentIndex = i;
            int nextIndex    = (i + 1) % outlineEdgeCount;

            Edge current = edgesA[currentIndex];
            Edge next    = edgesA[nextIndex];

            //Set point
            outlinePoints[i] = current.point0.position;

            //Set edge
            outlineEdges[i * 2 + 0] = currentIndex;

            if (current.point1 == next.point0)
            {
                outlineEdges[i * 2 + 1] = nextIndex;
            }
            else
            {
                outlineEdges[i * 2 + 1] = startIndex;
                startIndex = nextIndex;
            }
        }

        //Triangulate
        Triangulation triangulator = new Triangulation(outlinePoints, outlineEdges, normalPlane);

        List <int[]> newData = triangulator.Fill();

        int[] newEdges         = newData[0];
        int[] newTriangles     = newData[1];
        int[] newTriangleEdges = newData[2];

        //Calculate vertex properties
        Vector3 normalA = -normalPlane;
        Vector3 normalB = normalPlane;

        Vector4[] tangentsA, tangentsB;

        //Create new vertices
        int[] verticesA = new int[outlineEdgeCount];
        int[] verticesB = new int[outlineEdgeCount];

        ////////////////////////
        List <Vector4[]> newTans = assignTangents(normalPlane);

        tangentsA = newTans[0];
        tangentsB = newTans[1];
        ////////////////////////

        for (int i = 0; i < outlineEdgeCount; i++)
        {
            if (i >= tangentsA.Length || i >= tangentsB.Length)
            {
                continue;
            }
            verticesA[i] = a.AddVertex(outlinePoints[i], normalA, tangentsA[i], edgesA[i].point0);
            verticesB[i] = b.AddVertex(outlinePoints[i], normalB, tangentsB[i], edgesB[i].point0);
        }

        //Create new edges
        for (int i = 0; i < newEdges.Length / 2; i++)
        {
            int point0 = newEdges[i * 2 + 0];
            int point1 = newEdges[i * 2 + 1];

            Edge edgeA = new Edge(edgesA[point0].point0, edgesA[point1].point0);
            Edge edgeB = new Edge(edgesB[point0].point0, edgesB[point1].point0);

            edgesA.Add(edgeA);
            edgesB.Add(edgeB);

            a.edges.Add(edgeA);
            b.edges.Add(edgeB);
        }

        //Create new triangles
        for (int i = 0; i < newTriangles.Length / 3; i++)
        {
            int point0 = newTriangles[i * 3 + 0];
            int point1 = newTriangles[i * 3 + 1];
            int point2 = newTriangles[i * 3 + 2];

            int edge0 = newTriangleEdges[i * 3 + 0];
            int edge1 = newTriangleEdges[i * 3 + 1];
            int edge2 = newTriangleEdges[i * 3 + 2];

            Triangle triangleA = new Triangle(verticesA[point0], verticesA[point2], verticesA[point1], edgesA[point0].point0, edgesA[point2].point0, edgesA[point1].point0, edgesA[edge2], edgesA[edge1], edgesA[edge0]);
            Triangle triangleB = new Triangle(verticesB[point0], verticesB[point1], verticesB[point2], edgesB[point0].point0, edgesB[point1].point0, edgesB[point2].point0, edgesB[edge0], edgesB[edge1], edgesB[edge2]);

            a.triangles.Add(triangleA);
            b.triangles.Add(triangleB);
        }
    }