示例#1
0
        public MeshGeometry GetPartialGeometry(int startIndex, int indexCount, int startVertex, int vertexCount)
        {
            var builder = new GeometryBuilder();

            for (int i = 0; i < vertexCount; i++)
            {
                var v = Vertices[i + startVertex];
                builder.AddVertex(v.Clone(), false);
            }

            var triangleIndices = GetTriangleIndices();

            for (int i = 0; i < indexCount; i += 3)
            {
                int idx1 = triangleIndices[i + startIndex];
                int idx2 = triangleIndices[i + 1 + startIndex];
                int idx3 = triangleIndices[i + 2 + startIndex];
                builder.AddTriangle(idx1 - startVertex, idx2 - startVertex, idx3 - startVertex);
            }

            var geom = builder.GetGeometry();

            for (int i = 0; i < indexCount; i++)
            {
                var originalIndex = Indices[i + startIndex];
                geom.Indices[i].RoundEdgeData = originalIndex.RoundEdgeData.Clone();
                geom.Indices[i].AverageNormal = originalIndex.AverageNormal;
            }

            return(geom);
        }
示例#2
0
        public static List <MeshGeometry> SplitSurfaces(MeshGeometry mesh)
        {
            var currentMesh        = new GeometryBuilder();
            var remainingTriangles = mesh.Triangles.ToList();
            var resultingMeshes    = new List <MeshGeometry>();

            var edgeTriangleDic = new Dictionary <Edge, List <Triangle> >();

            //var allEdges = remainingTriangles.SelectMany(x => x.Edges).Distinct();

            foreach (var tri in remainingTriangles)
            {
                foreach (var edge in tri.Edges)
                {
                    if (!edgeTriangleDic.ContainsKey(edge))
                    {
                        edgeTriangleDic.Add(edge, new List <Triangle>());
                    }
                    edgeTriangleDic[edge].Add(tri);
                }
            }

            Triangle TakeTriangle(int index)
            {
                var result = remainingTriangles[index];

                remainingTriangles.RemoveAt(index);
                return(result);
            }

            IEnumerable <Triangle> GetNeighboringTriangles(Triangle triangle)
            {
                var neighbors = triangle.Edges.SelectMany(e =>
                                                          edgeTriangleDic[e]).Intersect(remainingTriangles).ToList();


                foreach (var tri in neighbors)
                {
                    remainingTriangles.Remove(tri);
                }

                foreach (var tri in neighbors.ToArray())
                {
                    neighbors.AddRange(GetNeighboringTriangles(tri));
                }

                return(neighbors);
                //for (int i = 0; i < remainingTriangles.Count; i++)
                //{
                //    if (triangle.ShareEdge(remainingTriangles[i], false))
                //    {
                //        yield return TakeTriangle(i);
                //        i--;
                //    }
                //}
            }

            while (remainingTriangles.Count > 0)
            {
                var curTri = TakeTriangle(0);
                currentMesh.AddTriangle(curTri);
                var neighbors = GetNeighboringTriangles(curTri);
                foreach (var tri in neighbors)
                {
                    currentMesh.AddTriangle(tri);
                }

                resultingMeshes.Add(currentMesh.GetGeometry());

                currentMesh.Reset();
            }

            return(resultingMeshes);
        }