Esempio n. 1
0
        public static List <int> CreateFaceIndices <VERTEX, EDGE, FACE>(HBMesh <VERTEX, EDGE, FACE> mesh, int faceVertices)
            where VERTEX : HBVertex, new()
            where EDGE : HBEdge, new()
            where FACE : HBFace, new()
        {
            int count = mesh.Faces.Count;
            int size  = mesh.Faces.Count * faceVertices;

            List <int> indices = new List <int>(size);

            for (int i = 0; i < count; i++)
            {
                int num = 0;
                foreach (var v in mesh.Faces[i].Edge.EnumerateVertices())
                {
                    int index = mesh.IndexOf(v);
                    if (index == -1)
                    {
                        continue;
                    }
                    indices.Add(index);
                    num++;
                }

                if (num != faceVertices)
                {
                    throw new InvalidOperationException("Face did not have the expected number of vertices.");
                }
            }

            return(indices);
        }
Esempio n. 2
0
        public override HBMesh <VERTEX, EDGE, FACE> PopMesh()
        {
            var tmp = Mesh;

            Mesh = null;
            return(tmp);
        }
Esempio n. 3
0
        public static Mesh2f ToIndexableMesh2f <VERTEX, EDGE, FACE>(HBMesh <VERTEX, EDGE, FACE> mesh, int faceVertices = 0)
            where VERTEX : HBVertex2f, new()
            where EDGE : HBEdge, new()
            where FACE : HBFace, new()
        {
            int    numPositions = mesh.Vertices.Count;
            Mesh2f indexed      = new Mesh2f(numPositions);

            for (int i = 0; i < numPositions; i++)
            {
                indexed.Positions[i] = mesh.Vertices[i].Position;
            }

            if (faceVertices == 0)
            {
                //no faces. Mesh represents edge lines.
                indexed.SetIndices(CreateEdgeIndices(mesh));
            }
            else
            {
                indexed.SetIndices(CreateFaceIndices(mesh, faceVertices));
            }

            return(indexed);
        }
Esempio n. 4
0
        public static List <int> CreateEdgeIndices <VERTEX, EDGE, FACE>(HBMesh <VERTEX, EDGE, FACE> mesh)
            where VERTEX : HBVertex, new()
            where EDGE : HBEdge, new()
            where FACE : HBFace, new()
        {
            int count = mesh.Edges.Count;

            List <int>       indices = new List <int>(count / 2);
            HashSet <HBEdge> set     = new HashSet <HBEdge>();

            for (int i = 0; i < count; i++)
            {
                var edge = mesh.Edges[i];
                if (edge.Opposite == null)
                {
                    continue;
                }
                if (set.Contains(edge))
                {
                    continue;
                }

                var v0 = edge.Vertex;
                var v1 = edge.Opposite.Vertex;

                int i0 = mesh.IndexOf(v0);
                int i1 = mesh.IndexOf(v1);
                if (i0 == -1 || i1 == -1)
                {
                    continue;
                }
                indices.Add(i0);
                indices.Add(i1);

                set.Add(edge.Opposite);
            }

            return(indices);
        }
Esempio n. 5
0
 public override void PushEdgeMesh(int numVertices, int numEdges)
 {
     Mesh = new HBMesh <VERTEX, EDGE, FACE>(numVertices, numEdges, 0);
 }
Esempio n. 6
0
 public override void PushTriangleMesh(int numVertices, int numFaces)
 {
     Mesh = new HBMesh <VERTEX, EDGE, FACE>(numVertices, numFaces * 3 * 2, numFaces);
 }
Esempio n. 7
0
        public static void WeldVertices <VERTEX, EDGE, FACE>(HBMesh <VERTEX, EDGE, FACE> mesh, float minDistance)
            where VERTEX : HBVertex2f, new()
            where EDGE : HBEdge, new()
            where FACE : HBFace, new()
        {
            int vertCount = mesh.Vertices.Count;

            if (vertCount < 2)
            {
                return;
            }

            List <VERTEX> vertices = new List <VERTEX>(vertCount);
            List <VERTEX> removed  = new List <VERTEX>();

            float d2 = minDistance * minDistance;

            for (int i = 0; i < vertCount; i++)
            {
                var  v         = mesh.Vertices[i];
                bool wasWelded = FindClosest(v, vertices, d2) != null;

                if (!wasWelded)
                {
                    vertices.Add(v);
                }
                else
                {
                    removed.Add(v);
                }
            }

            mesh.Vertices.Clear();
            mesh.Vertices.AddRange(vertices);

            int edgeCount = mesh.Edges.Count;

            if (edgeCount > 0)
            {
                List <EDGE> edges = new List <EDGE>(edgeCount);

                for (int i = 0; i < edgeCount; i++)
                {
                    var    edge = mesh.Edges[i];
                    VERTEX v0   = edge.GetVertex <VERTEX>();

                    if (!removed.Contains(v0))
                    {
                        edges.Add(edge);
                    }
                    else
                    {
                        VERTEX v1 = null;

                        if (edge.Opposite != null)
                        {
                            v1 = edge.Opposite.GetVertex <VERTEX>();
                        }
                        else if (edge.Previous != null)
                        {
                            v1 = edge.Previous.GetVertex <VERTEX>();
                        }

                        if (!removed.Contains(v1))
                        {
                            edge.Vertex = FindClosest(v0, vertices, d2, false);
                            edges.Add(edge);
                        }
                        else
                        {
                            if (edge.Previous != null)
                            {
                                edge.Previous.Next = edge.Next;
                            }

                            if (edge.Next != null)
                            {
                                edge.Next.Previous = edge.Previous;
                            }
                        }
                    }
                }

                mesh.Edges.Clear();
                mesh.Edges.AddRange(edges);
            }
        }