예제 #1
0
        public void Subdivision()
        {
            /*        /\                  /\
             *       /  \                /__\
             *      /    \              /\  /\
             *     /      \            /__\/__\
             *    /        \          /\  /\  /\
             *   /__________\        /__\/__\/__\
             *
             *   为了让每个顶点周围都有它专属的一圈价为6的点,把每个三角形拆成9个
             */
            TriMesh copy = TriMeshIO.Clone(this.mesh);

            this.mesh.Clear();

            TriMesh.Vertex[] faceMap = new HalfEdgeMesh.Vertex[copy.Faces.Count];
            TriMesh.Vertex[] hfMap   = new HalfEdgeMesh.Vertex[copy.HalfEdges.Count];

            foreach (var v in copy.Vertices)
            {
                this.mesh.Vertices.Add(new VertexTraits(v.Traits.Position));
            }

            foreach (var face in copy.Faces)
            {
                faceMap[face.Index] = this.mesh.Vertices.Add(new VertexTraits(TriMeshUtil.GetMidPoint(face)));
            }

            foreach (var hf in copy.HalfEdges)
            {
                Vector3D pos = hf.FromVertex.Traits.Position * 2 / 3 + hf.ToVertex.Traits.Position * 1 / 3;
                hfMap[hf.Index] = this.mesh.Vertices.Add(new VertexTraits(pos));
            }

            foreach (var face in copy.Faces)
            {
                foreach (var hf in face.Halfedges)
                {
                    this.mesh.Faces.AddTriangles(faceMap[face.Index], hfMap[hf.Index], hfMap[hf.Opposite.Index]);
                    this.mesh.Faces.AddTriangles(faceMap[face.Index], hfMap[hf.Opposite.Index], hfMap[hf.Next.Index]);
                }
            }

            foreach (var v in copy.Vertices)
            {
                foreach (var hf in v.HalfEdges)
                {
                    if (hf.Face != null)
                    {
                        this.mesh.Faces.AddTriangles(this.mesh.Vertices[v.Index], hfMap[hf.Index], hfMap[hf.Previous.Opposite.Index]);
                    }
                }
            }
        }