示例#1
0
        /// <summary>
        /// Unites two TriangleMeshes making sure the Vertices remain unique.
        /// </summary>
        /// <param name="one">The first TriangleMesh</param>
        /// <param name="two">The second TriangleMesh</param>
        /// <returns>The united TriangleMesh</returns>
        public static TriangleMesh Union(TriangleMesh one, TriangleMesh two)
        {
            TriangleMesh union = new TriangleMesh();
            TriangleMesh big;
            TriangleMesh small;

            if (one.Vertices.Count >= two.Vertices.Count)
            {
                big   = one;
                small = two;
            }
            else
            {
                big   = two;
                small = one;
            }

            // The bigger TriangleMesh is part of the union.
            union.Vertices.AddRange(big.Vertices.ToArray());
            union.AddRange(big.ToArray());
            List <Triangle> temp = new List <Triangle>(small.ToArray());

            // The Vertices of the smaller TriangleMesh are included in the union.
            for (int i = 0; i < small.Vertices.Count; i++)
            {
                Vertex vertex = small.Vertices[i];

                // The indices of the Vertices in the Triangles of the smaller TriangleMesh
                // are set to the corresponding indices in the union.
                int index = big.Vertices.IndexOf(vertex);
                if (index == -1)
                {
                    union.Vertices.Add(vertex.Copy().ToVertex());
                    for (int j = 0; j < vertex.Triangles.Count; j++)
                    {
                        temp[vertex.Triangles[j]].Replace(i, union.Vertices.Count - 1);
                    }
                }
                else
                {
                    for (int j = 0; j < vertex.Triangles.Count; j++)
                    {
                        temp[vertex.Triangles[j]].Replace(i, index);
                    }
                }
            }

            // The Triangles of the smaller TriangleMesh are included in the union.
            for (int k = 0; k < temp.Count; k++)
            {
                union.Add(new Triangle(temp[k][0], temp[k][1], temp[k][2]));
            }

            union.Finish(true);
            return(union);
        }
示例#2
0
        /// <summary>
        /// Connects the Vertices to the correct triangles for the chosen mesh type.
        /// </summary>
        /// <param name="mesh">The new TriangleMesh containing only the Vertices.</param>
        /// <param name="steps">The number of steps to be taken.</param>
        /// <returns>The completed TriangleMesh.</returns>
        private TriangleMesh CreateTriangles(TriangleMesh mesh, int steps)
        {
            int point = 0;

            if (meshType == 0)
            {
                steps *= 2;
                do
                {
                    // Creates eight triangles per square.
                    mesh.Add(new Triangle(point, point + 1, point + steps + 2));
                    mesh.Add(new Triangle(point + 1, point + 2, point + steps + 2));
                    mesh.Add(new Triangle(point + 2, point + steps + 3, point + steps + 2));
                    mesh.Add(new Triangle(point + steps + 3, point + 2 * steps + 4, point + steps + 2));
                    mesh.Add(new Triangle(point + 2 * steps + 4, point + 2 * steps + 3, point + steps + 2));
                    mesh.Add(new Triangle(point + 2 * steps + 3, point + 2 * steps + 2, point + steps + 2));
                    mesh.Add(new Triangle(point + 2 * steps + 2, point + steps + 1, point + steps + 2));
                    mesh.Add(new Triangle(point + steps + 1, point, point + steps + 2));

                    if (point % (2 * steps + 2) < steps - 2)
                    {
                        point += 2;
                    }
                    else
                    {
                        point += steps + 4;
                    }
                } while (point <= mesh.Vertices.Count - (5 + 2 * steps));
            }
            else
            {
                do
                {
                    // Creates two triangles per square.
                    mesh.Add(new Triangle(point, point + 1, point + steps + 2));
                    mesh.Add(new Triangle(point, point + steps + 2, point + steps + 1));

                    if (point % (steps + 1) < steps - 1)
                    {
                        point++;
                    }
                    else
                    {
                        point += 2;
                    }
                } while (point <= mesh.Vertices.Count - (steps + 3));
            }

            mesh.Finish(true);
            return(mesh);
        }