Exemple #1
0
        public static TriMesh Clone(TriMesh mesh)
        {
            TriMesh newMesh = new TriMesh();
            for (int i = 0; i < mesh.Vertices.Count; i++)
            {
                VertexTraits traits = new VertexTraits(mesh.Vertices[i].Traits.Position.x,
                                                       mesh.Vertices[i].Traits.Position.y,
                                                       mesh.Vertices[i].Traits.Position.z);
                newMesh.Vertices.Add(traits);
            }

            TriMesh.Vertex[] faceVetices = new TriMesh.Vertex[3];
            for (int i = 0; i < mesh.Faces.Count; i++)
            {
                int faceVertexIndex1 = mesh.Faces[i].GetVertex(0).Index;
                int faceVertexIndex2 = mesh.Faces[i].GetVertex(1).Index;
                int faceVertexIndex3 = mesh.Faces[i].GetVertex(2).Index;

                faceVetices[0] = newMesh.Vertices[faceVertexIndex1];
                faceVetices[1] = newMesh.Vertices[faceVertexIndex2];
                faceVetices[2] = newMesh.Vertices[faceVertexIndex3];
                newMesh.Faces.AddTriangles(faceVetices);
            }
            newMesh.TrimExcess();
            return newMesh;
        }
Exemple #2
0
        public static TriMesh CreateCylinderPlane(int m, int n, double r,
                                                  double l, double maxU, double maxV, int diff)
        {
            TriMesh mesh = new TriMesh();

            TriMesh.Vertex[,] arr = new HalfEdgeMesh.Vertex[m, n];
            for (int i = 0; i < m; i++)
            {
                double x = l / m * i - l / 2;

                for (int j = 0; j < n; j++)
                {
                    double       y     = l / n * j - l / 2;
                    VertexTraits trait = new VertexTraits(x, y, 0);

                    trait.UV  = new Vector2D(x, y);
                    arr[i, j] = mesh.Vertices.Add(trait);
                }
            }
            for (int i = 0; i < m - 1; i++)
            {
                for (int j = 0; j < n - diff; j++)
                {
                    int ni = i + 1;
                    int nj = (j + 1) % n;
                    mesh.Faces.AddTriangles(arr[i, nj], arr[ni, nj],
                                            arr[ni, j], arr[i, j]);
                }
            }
            return(mesh);
        }
Exemple #3
0
        public static TriMesh CreateCylinder(int m, int n, double r,
                                             double l, double maxU, double maxV, int diff)
        {
            TriMesh mesh = new TriMesh();

            TriMesh.Vertex[,] arr = new HalfEdgeMesh.Vertex[m, n];
            for (int i = 0; i < m; i++)
            {
                double z = l / m * i - l / 2;
                double v = maxV / m * i;
                for (int j = 0; j < n; j++)
                {
                    double       x     = r * Math.Cos(Math.PI * 2 / n * j);
                    double       y     = r * Math.Sin(Math.PI * 2 / n * j);
                    double       u     = maxU / n * j;
                    VertexTraits trait = new VertexTraits(x, y, z);
                    trait.UV  = new Vector2D(u, v);
                    arr[i, j] = mesh.Vertices.Add(trait);
                }
            }
            for (int i = 0; i < m - 1; i++)
            {
                for (int j = 0; j < n - diff; j++)
                {
                    int ni = i + 1;
                    int nj = (j + 1) % n;
                    mesh.Faces.AddTriangles(arr[i, nj], arr[ni, nj],
                                            arr[ni, j], arr[i, j]);
                }
            }
            return(mesh);
        }
Exemple #4
0
        public static TriMesh CreatePlaneTest(int m, int n, double l)
        {
            TriMesh mesh = new TriMesh();

            TriMesh.Vertex[,] arr = new HalfEdgeMesh.Vertex[m + 1, n + 1];
            for (int i = 0; i <= m; i++)
            {
                double x = l / m * i;

                for (int j = 0; j <= n; j++)
                {
                    double       y     = l / n * j;
                    VertexTraits trait = new VertexTraits(x, y, 0);

                    trait.UV  = new Vector2D(x, y);
                    arr[i, j] = mesh.Vertices.Add(trait);
                }
            }
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    int ni = i + 1;
                    int nj = j + 1;
                    mesh.Faces.AddTriangles(arr[i, nj], arr[ni, nj],
                                            arr[ni, j], arr[i, j]);
                }
            }
            return(mesh);
        }
Exemple #5
0
        public static TriMesh Clone(TriMesh mesh)
        {
            TriMesh newMesh = new TriMesh();

            for (int i = 0; i < mesh.Vertices.Count; i++)
            {
                VertexTraits traits = new VertexTraits(mesh.Vertices[i].Traits.Position.x,
                                                       mesh.Vertices[i].Traits.Position.y,
                                                       mesh.Vertices[i].Traits.Position.z);
                newMesh.Vertices.Add(traits);
            }

            TriMesh.Vertex[] faceVetices = new TriMesh.Vertex[3];
            for (int i = 0; i < mesh.Faces.Count; i++)
            {
                int faceVertexIndex1 = mesh.Faces[i].GetVertex(0).Index;
                int faceVertexIndex2 = mesh.Faces[i].GetVertex(1).Index;
                int faceVertexIndex3 = mesh.Faces[i].GetVertex(2).Index;

                faceVetices[0] = newMesh.Vertices[faceVertexIndex1];
                faceVetices[1] = newMesh.Vertices[faceVertexIndex2];
                faceVetices[2] = newMesh.Vertices[faceVertexIndex3];
                newMesh.Faces.AddTriangles(faceVetices);
            }
            newMesh.TrimExcess();
            return(newMesh);
        }
Exemple #6
0
            /// <summary>
            /// Adds a vertex to the mesh with the specified vertex traits.
            /// </summary>
            /// <param name="vertexTraits">The custom traits for the vertex to add to the mesh.</param>
            /// <returns>The vertex created by this method.</returns>
            public Vertex Add(VertexTraits vertexTraits)
            {
                Vertex v = new Vertex(vertexTraits);

                mesh.AppendToVertexList(v);
                return(v);
            }
Exemple #7
0
        public static PolygonMesh BuildDual(TriMesh mesh, EnumDual type)
        {
            TriMeshModify.RepaireAllHole(mesh);

            PolygonMesh.Vertex[] faceMap = new HalfEdgeMesh.Vertex[mesh.Faces.Count];
            PolygonMesh.Vertex[] edgeMap = new HalfEdgeMesh.Vertex[mesh.Edges.Count];
            PolygonMesh          pm      = new PolygonMesh();

            foreach (var face in mesh.Faces)
            {
                Vector3D center = Vector3D.Zero;
                switch (type)
                {
                case EnumDual.DualA:
                    center = TriMeshUtil.GetMidPoint(face);
                    break;

                case EnumDual.DualB:
                    TriMesh.Vertex vertex0  = face.GetVertex(0);
                    TriMesh.Vertex vertex1  = face.GetVertex(1);
                    TriMesh.Vertex vertex2  = face.GetVertex(2);
                    Triangle       triangle = new Triangle(vertex0.Traits.Position,
                                                           vertex1.Traits.Position,
                                                           vertex2.Traits.Position);
                    center = triangle.ComputeCircumCenter();
                    break;

                default:
                    break;
                }
                PolygonMesh.Vertex v = new HalfEdgeMesh.Vertex(new VertexTraits(center));
                faceMap[face.Index] = v;
                pm.AppendToVertexList(v);
            }

            foreach (var edge in mesh.Edges)
            {
                VertexTraits       trait = new VertexTraits(TriMeshUtil.GetMidPoint(edge));
                PolygonMesh.Vertex v     = new HalfEdgeMesh.Vertex(trait);
                edgeMap[edge.Index] = v;
                pm.AppendToVertexList(v);
            }

            foreach (var v in mesh.Vertices)
            {
                List <PolygonMesh.Vertex> list = new List <HalfEdgeMesh.Vertex>();
                foreach (var hf in v.HalfEdges)
                {
                    list.Add(faceMap[hf.Face.Index]);
                    list.Add(edgeMap[hf.Edge.Index]);
                }
                list.Reverse();
                pm.Faces.Add(list.ToArray());
            }
            return(pm);
        }
Exemple #8
0
        public static PolygonMesh BuildDual(TriMesh mesh, EnumDual type)
        {
            TriMeshModify.RepaireAllHole(mesh);

            PolygonMesh.Vertex[] faceMap = new HalfEdgeMesh.Vertex[mesh.Faces.Count];
            PolygonMesh.Vertex[] edgeMap = new HalfEdgeMesh.Vertex[mesh.Edges.Count];
            PolygonMesh pm = new PolygonMesh();

            foreach (var face in mesh.Faces)
            {
                Vector3D center = Vector3D.Zero;
                switch (type)
                {
                    case EnumDual.DualA:
                        center = TriMeshUtil.GetMidPoint(face);
                        break;
                    case EnumDual.DualB:
                        TriMesh.Vertex vertex0 = face.GetVertex(0);
                        TriMesh.Vertex vertex1 = face.GetVertex(1);
                        TriMesh.Vertex vertex2 = face.GetVertex(2);
                        Triangle triangle = new Triangle(vertex0.Traits.Position, 
                                                         vertex1.Traits.Position, 
                                                         vertex2.Traits.Position);
                        center = triangle.ComputeCircumCenter();
                        break;
                    default:
                        break;
                }
                PolygonMesh.Vertex v = new HalfEdgeMesh.Vertex(new VertexTraits(center));
                faceMap[face.Index] = v;
                pm.AppendToVertexList(v);
            }

            foreach (var edge in mesh.Edges)
            {
                VertexTraits trait = new VertexTraits(TriMeshUtil.GetMidPoint(edge));
                PolygonMesh.Vertex v = new HalfEdgeMesh.Vertex(trait);
                edgeMap[edge.Index] = v;
                pm.AppendToVertexList(v);
            }

            foreach (var v in mesh.Vertices)
            {
                List<PolygonMesh.Vertex> list = new List<HalfEdgeMesh.Vertex>();
                foreach (var hf in v.HalfEdges)
                {
                    list.Add(faceMap[hf.Face.Index]);
                    list.Add(edgeMap[hf.Edge.Index]);
                }
                list.Reverse();
                pm.Faces.Add(list.ToArray());
            }
            return pm;
        }
Exemple #9
0
        public TriMesh ConvertTriMesh(MFnMesh mesh)
        {
            if (mesh == null)
            {
                MGlobal.displayInfo("Mesh is null \n");
            }

            TriMesh trimesh = new TriMesh();

            MIntArray   indices        = new MIntArray();
            MIntArray   triangleCounts = new MIntArray();
            MPointArray points         = new MPointArray();

            mesh.getTriangles(triangleCounts, indices);
            mesh.getPoints(points);

            // Get the triangle indices

            for (int i = 0; i < (int)points.length; ++i)
            {
                MPoint       pt    = points[i];
                VertexTraits trait = new VertexTraits(pt.x, pt.y, pt.z);
                trimesh.Vertices.Add(trait);
            }


            MGlobal.displayInfo(indices.Count.ToString() + "\n");
            int j = 0;

            while (j < indices.Count)
            {
                int a = indices[j];
                j++;
                int b = indices[j];
                j++;
                int c = indices[j];
                j++;
                trimesh.Faces.AddTriangles(trimesh.Vertices[a], trimesh.Vertices[b], trimesh.Vertices[c]);
            }
            return(trimesh);
        }
Exemple #10
0
 /// <summary>
 /// Creates a vertex with the given traits.
 /// </summary>
 /// <param name="vertexTraits">Traits for this vertex.</param>
 public Vertex(VertexTraits vertexTraits)
 {
     Traits = vertexTraits;
 }
Exemple #11
0
 /// <summary>
 /// Creates a vertex with the given traits.
 /// </summary>
 /// <param name="vertexTraits">Traits for this vertex.</param>
 public Vertex(VertexTraits vertexTraits)
 {
     Traits = vertexTraits;
 }
Exemple #12
0
        public static TriMesh CreateCylinderPlane(int m, int n, double r,
                                double l, double maxU, double maxV, int diff)
        {
            TriMesh mesh = new TriMesh();
            TriMesh.Vertex[,] arr = new HalfEdgeMesh.Vertex[m, n];
            for (int i = 0; i < m; i++)
            {
                double x = l / m * i - l / 2;
              
                for (int j = 0; j < n; j++)
                { 
                    double y = l / n * j - l / 2;
                    VertexTraits trait = new VertexTraits(x,y, 0);

                    trait.UV = new Vector2D(x, y);
                    arr[i, j] = mesh.Vertices.Add(trait);
                }
            }
            for (int i = 0; i < m - 1; i++)
            {
                for (int j = 0; j < n - diff; j++)
                {
                    int ni = i + 1;
                    int nj = (j + 1) % n;
                    mesh.Faces.AddTriangles(arr[i, nj], arr[ni, nj],
                                            arr[ni, j], arr[i, j]);
                }
            }
            return mesh;
        }
Exemple #13
0
 public static TriMesh CreateCylinder(int m, int n, double r, 
                          double l, double maxU, double maxV,int diff)
 {
     TriMesh mesh = new TriMesh();
     TriMesh.Vertex[,] arr = new HalfEdgeMesh.Vertex[m, n];
     for (int i = 0; i < m; i++)
     {
         double z = l / m * i - l / 2;
         double v = maxV / m * i;
         for (int j = 0; j < n; j++)
         {
             double x = r * Math.Cos(Math.PI * 2 / n * j);
             double y = r * Math.Sin(Math.PI * 2 / n * j);
             double u = maxU / n * j;
             VertexTraits trait = new VertexTraits(x, y, z);
             trait.UV = new Vector2D(u, v);
             arr[i, j] = mesh.Vertices.Add(trait);
         }
     }
     for (int i = 0; i < m - 1; i++)
     {
         for (int j = 0; j < n-diff; j++)
         {
             int ni = i + 1;
             int nj = (j + 1) % n;
             mesh.Faces.AddTriangles(arr[i, nj], arr[ni, nj], 
                                     arr[ni, j], arr[i, j]);
         }
     }
     return mesh;
 }
Exemple #14
0
        public static TriMesh CreatePlaneFolded(int m, int n, double l)
        {
            TriMesh mesh = new TriMesh();
            TriMesh.Vertex[,] arr = new HalfEdgeMesh.Vertex[m + 1, n + 1];
            for (int i = 0; i <= m; i++)
            {
                double x = l / m * i  ;

                double temp = 0;
                for (int j = 0; j <= n/2; j++)
                {
                    double y = l / n * j  ;
                    VertexTraits trait = new VertexTraits(x, y, 0);

                    trait.UV = new Vector2D(x, y);
                    arr[i, j] = mesh.Vertices.Add(trait);


                    temp = y;
                }


                for (int j = 1; j <= n / 2; j++)
                {
                    double y = l / n * j;
                    VertexTraits trait = new VertexTraits(x, temp, y);

                    trait.UV = new Vector2D(x, y);
                    arr[i,  n / 2+j] = mesh.Vertices.Add(trait);
                }

            }
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    int ni = i + 1;
                    int nj = j + 1;
                    mesh.Faces.AddTriangles(arr[i, nj], arr[ni, nj],
                                            arr[ni, j], arr[i, j]);
                }
            }
            return mesh;
        }
Exemple #15
0
       public TriMesh ConvertTriMesh(MFnMesh mesh)
       {
            if(mesh==null)
            {
                MGlobal.displayInfo("Mesh is null \n");
            }

           TriMesh trimesh = new TriMesh();

           MIntArray indices = new MIntArray();
           MIntArray triangleCounts = new MIntArray();
           MPointArray points = new MPointArray();

           mesh.getTriangles(triangleCounts, indices);
           mesh.getPoints(points);

           // Get the triangle indices 
        
           for (int i = 0; i < (int)points.length; ++i)
           {
               MPoint pt = points[i];
               VertexTraits trait = new VertexTraits(pt.x, pt.y, pt.z);
               trimesh.Vertices.Add(trait);
           }
           
           
           MGlobal.displayInfo( indices.Count.ToString() +"\n");
           int j=0;
           while(j<indices.Count)
           {
               int a = indices[j];
               j++;
               int b = indices[j];
               j++;
               int c = indices[j];
               j++;
               trimesh.Faces.AddTriangles(trimesh.Vertices[a], trimesh.Vertices[b], trimesh.Vertices[c]);
                
           } 
           return trimesh;
       }
Exemple #16
0
        public static TriMesh ConvertToTriMesh(NonManifoldMesh mesh)
        {
            TriMesh triMesh = new GraphicResearchHuiZhao.TriMesh();

            triMesh.Traits.HasFaceVertexNormals  = true;
            triMesh.Traits.HasTextureCoordinates = true;

            if (mesh.VertexNormal == null)
            {
                triMesh.Traits.HasFaceVertexNormals = false;
            }
            else
            {
                triMesh.Traits.HasFaceVertexNormals = true;
            }

            if (mesh.TextextCoordinate == null)
            {
                triMesh.Traits.HasTextureCoordinates = false;
            }
            else
            {
                triMesh.Traits.HasTextureCoordinates = true;
            }

            int vertexCount = mesh.VertexCount;
            int faceCount   = mesh.FaceCount;

            for (int i = 0; i < vertexCount; i++)
            {
                VertexTraits traits = new VertexTraits((float)mesh.VertexPos[i * 3], (float)mesh.VertexPos[i * 3 + 1], (float)mesh.VertexPos[i * 3 + 2]);
                triMesh.Vertices.Add(traits);
            }

            TriMesh.Vertex[] vertices = new TriMesh.Vertex[3];

            for (int i = 0; i < faceCount; i++)
            {
                vertices[0] = triMesh.Vertices[mesh.FaceIndex[i * 3]];
                vertices[1] = triMesh.Vertices[mesh.FaceIndex[i * 3 + 1]];
                vertices[2] = triMesh.Vertices[mesh.FaceIndex[i * 3 + 2]];


                triMesh.Faces.AddTriangles(vertices);
            }

            if (triMesh.Traits.HasTextureCoordinates)
            {
                TriMesh.HalfEdge faceVertex;
                foreach (TriMesh.Face face in triMesh.Faces)
                {
                    foreach (TriMesh.Vertex vertex in face.Vertices)
                    {
                        faceVertex = face.FindHalfedgeTo(vertex);
                        if (faceVertex != null) // Make sure vertex belongs to face if triangularization is on
                        {
                            faceVertex.Traits.TextureCoordinate = new Vector2D(mesh.TextextCoordinate[vertex.Index * 2], mesh.TextextCoordinate[vertex.Index * 2 + 1]);
                        }
                    }
                }
            }

            if (triMesh.Traits.HasFaceVertexNormals)
            {
                TriMesh.HalfEdge faceVertex;
                foreach (TriMesh.Face face in triMesh.Faces)
                {
                    foreach (TriMesh.Vertex vertex in face.Vertices)
                    {
                        faceVertex = face.FindHalfedgeTo(vertex);
                        if (faceVertex != null) // Make sure vertex belongs to face if triangularization is on
                        {
                            faceVertex.Traits.Normal = new Vector3D(mesh.FaceNormal, vertex.Index);
                        }
                    }
                }
            }



            //if (triMesh.Traits.HasTextureCoordinates)
            //{

            //    foreach (HalfEdgeMesh.TriMesh.Vertex vertex in triMesh.Vertices )
            //    {

            //       vertex.Traits.TextureCoordinate = new Vector2d(mesh.TextextCoordinate[vertex.Index * 2], mesh.TextextCoordinate[vertex.Index * 2 + 1]);


            //    }
            //}

            //if (triMesh.Traits.HasFaceVertexNormals)
            //{
            //   foreach (HalfEdgeMesh.TriMesh.Vertex vertex in triMesh.Vertices)
            //   {
            //       vertex.Traits.Normal = new Vector3d(mesh.FaceNormal, vertex.Index);

            //    }
            //}


            return(triMesh);
        }
Exemple #17
0
 TriMesh.Vertex AddMid(TriMesh.Vertex v1, TriMesh.Vertex v2, double rate)
 {
     VertexTraits trait = new VertexTraits(v1.Traits.Position * (1 - rate) + v2.Traits.Position * rate);
     return this.mesh.Vertices.Add(trait);
 }
Exemple #18
0
        TriMesh.Vertex AddMid(TriMesh.Vertex v1, TriMesh.Vertex v2, double rate)
        {
            VertexTraits trait = new VertexTraits(v1.Traits.Position * (1 - rate) + v2.Traits.Position * rate);

            return(this.mesh.Vertices.Add(trait));
        }
Exemple #19
0
        public static TriMesh ConvertToTriMesh(NonManifoldMesh mesh)
        {
            TriMesh triMesh = new GraphicResearchHuiZhao.TriMesh();
            triMesh.Traits.HasFaceVertexNormals = true;
            triMesh.Traits.HasTextureCoordinates = true;

            if (mesh.VertexNormal == null)
            {
                triMesh.Traits.HasFaceVertexNormals = false;

            }
            else
            {
                triMesh.Traits.HasFaceVertexNormals = true;
            }

            if (mesh.TextextCoordinate == null)
            {
                triMesh.Traits.HasTextureCoordinates = false;
            }
            else
            {
                triMesh.Traits.HasTextureCoordinates = true;
            }

            int vertexCount = mesh.VertexCount;
            int faceCount = mesh.FaceCount;
            for (int i = 0; i < vertexCount; i++)
            {
                VertexTraits traits = new VertexTraits((float)mesh.VertexPos[i * 3], (float)mesh.VertexPos[i * 3 + 1], (float)mesh.VertexPos[i * 3 + 2]);
                triMesh.Vertices.Add(traits);

            }

            TriMesh.Vertex[] vertices = new TriMesh.Vertex[3];

            for (int i = 0; i < faceCount; i++)
            {
                vertices[0] = triMesh.Vertices[mesh.FaceIndex[i * 3]];
                vertices[1] = triMesh.Vertices[mesh.FaceIndex[i * 3 + 1]];
                vertices[2] = triMesh.Vertices[mesh.FaceIndex[i * 3 + 2]];


                triMesh.Faces.AddTriangles(vertices);
            }

            if (triMesh.Traits.HasTextureCoordinates)
            {
                TriMesh.HalfEdge faceVertex;
                foreach (TriMesh.Face face in triMesh.Faces)
                {
                    foreach (TriMesh.Vertex vertex in face.Vertices)
                    {
                        faceVertex = face.FindHalfedgeTo(vertex);
                        if (faceVertex != null) // Make sure vertex belongs to face if triangularization is on
                        {

                            faceVertex.Traits.TextureCoordinate = new Vector2D(mesh.TextextCoordinate[vertex.Index * 2], mesh.TextextCoordinate[vertex.Index * 2 + 1]);

                        }
                    }
                }
            }

            if (triMesh.Traits.HasFaceVertexNormals)
            {
                TriMesh.HalfEdge faceVertex;
                foreach (TriMesh.Face face in triMesh.Faces)
                {
                    foreach (TriMesh.Vertex vertex in face.Vertices)
                    {
                        faceVertex = face.FindHalfedgeTo(vertex);
                        if (faceVertex != null) // Make sure vertex belongs to face if triangularization is on
                        {

                            faceVertex.Traits.Normal = new Vector3D(mesh.FaceNormal, vertex.Index);

                        }
                    }
                }
            }



            //if (triMesh.Traits.HasTextureCoordinates)
            //{

            //    foreach (HalfEdgeMesh.TriMesh.Vertex vertex in triMesh.Vertices )
            //    {

            //       vertex.Traits.TextureCoordinate = new Vector2d(mesh.TextextCoordinate[vertex.Index * 2], mesh.TextextCoordinate[vertex.Index * 2 + 1]);


            //    }
            //}

            //if (triMesh.Traits.HasFaceVertexNormals)
            //{
            //   foreach (HalfEdgeMesh.TriMesh.Vertex vertex in triMesh.Vertices)
            //   { 
            //       vertex.Traits.Normal = new Vector3d(mesh.FaceNormal, vertex.Index);

            //    }
            //}


            return triMesh;
        }