Exemplo n.º 1
0
        public virtual bool Extrude(int group_id = -1)
        {
            // duplicate loop vertices
            int NV = Loop.Vertices.Length;

            NewLoop          = new EdgeLoop(Mesh);
            NewLoop.Vertices = new int[NV];

            for (int i = 0; i < NV; ++i)
            {
                int vid = Loop.Vertices[i];
                NewLoop.Vertices[i] = Mesh.AppendVertex(Mesh, vid);
            }

            // move to offset positions
            for (int i = 0; i < NV; ++i)
            {
                Vector3d v     = Mesh.GetVertex(Loop.Vertices[i]);
                Vector3f n     = Mesh.GetVertexNormal(Loop.Vertices[i]);
                Vector3d new_v = PositionF(v, n, i);
                Mesh.SetVertex(NewLoop.Vertices[i], new_v);
            }

            // stitch interior
            MeshEditor edit = new MeshEditor(Mesh);

            NewTriangles = edit.StitchLoop(Loop.Vertices, NewLoop.Vertices, group_id);

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reverse face orientation on a subset of triangles
        /// </summary>
        public void ReverseTriangles(IEnumerable <int> triangles, bool bFlipVtxNormals = true)
        {
            if (bFlipVtxNormals == false)
            {
                foreach (int tid in triangles)
                {
                    Mesh.ReverseTriOrientation(tid);
                }
            }
            else
            {
                BitArray donev = new BitArray(Mesh.MaxVertexID);

                foreach (int tid in triangles)
                {
                    Mesh.ReverseTriOrientation(tid);

                    Index3i tri = Mesh.GetTriangle(tid);
                    for (int j = 0; j < 3; ++j)
                    {
                        int vid = tri[j];
                        if (donev[vid] == false)
                        {
                            Mesh.SetVertexNormal(vid, -Mesh.GetVertexNormal(vid));
                            donev[vid] = true;
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        bool save_vertex(DMesh3 mesh, int vid, bool force = false)
        {
            if (force || mesh.VerticesRefCounts.refCount(vid) == 2)
            {
                RemovedV.Add(vid);
                Positions.Add(mesh.GetVertex(vid));
                if (Normals != null)
                {
                    Normals.Add(mesh.GetVertexNormal(vid));
                }

                if (Colors != null)
                {
                    Colors.Add(mesh.GetVertexColor(vid));
                }

                if (UVs != null)
                {
                    UVs.Add(mesh.GetVertexUV(vid));
                }

                return(false);
            }
            return(true);
        }
Exemplo n.º 4
0
 void append_vertex(DMesh3 mesh, int vid)
 {
     AddedV.Add(vid);
     Positions.Add(mesh.GetVertex(vid));
     if (Normals != null)
     {
         Normals.Add(mesh.GetVertexNormal(vid));
     }
     if (Colors != null)
     {
         Colors.Add(mesh.GetVertexColor(vid));
     }
     if (UVs != null)
     {
         UVs.Add(mesh.GetVertexUV(vid));
     }
 }
Exemplo n.º 5
0
        public static Rhino.Geometry.Mesh ConvertToRhinoMesh(g3.DMesh3 largeMesh)
        {
            var mesh = new g3.DMesh3();

            mesh.CompactCopy(largeMesh);

            Rhino.Geometry.Mesh ret = new Rhino.Geometry.Mesh();

            foreach (var p in mesh.Vertices())
            {
                ret.Vertices.Add(new Rhino.Geometry.Point3d(p.x, p.y, p.z));
            }

            ret.Normals.Count = ret.Vertices.Count;

            for (int i = 0; i < ret.Vertices.Count; i++)
            {
                var n = mesh.GetVertexNormal(i);
                ret.Normals[i] = new Rhino.Geometry.Vector3f(n.x, n.y, n.z);
            }


            foreach (var f in mesh.Triangles())
            {
                if (f.a >= 0 && f.a < ret.Vertices.Count &&
                    f.b >= 0 && f.b < ret.Vertices.Count &&
                    f.c >= 0 && f.c < ret.Vertices.Count)
                {
                    ret.Faces.AddFace(new Rhino.Geometry.MeshFace(f.a, f.b, f.c));
                }
                else
                {
                    Rhino.RhinoApp.WriteLine("Error Triangle:" + f.a + "," + f.b + ",", +f.c);
                }
            }

            ret.Normals.ComputeNormals();

            return(ret);
        }
Exemplo n.º 6
0
        public virtual bool Extrude()
        {
            MeshEditor editor = new MeshEditor(Mesh);


            editor.SeparateTriangles(Triangles, true, out EdgePairs);

            MeshNormals normals      = null;
            bool        bHaveNormals = Mesh.HasVertexNormals;

            if (!bHaveNormals)
            {
                normals = new MeshNormals(Mesh);
                normals.Compute();
            }

            ExtrudeVertices = new MeshVertexSelection(Mesh);
            ExtrudeVertices.SelectTriangleVertices(Triangles);

            Vector3d[] NewVertices = new Vector3d[ExtrudeVertices.Count];
            int        k           = 0;

            foreach (int vid in ExtrudeVertices)
            {
                Vector3d v = Mesh.GetVertex(vid);
                Vector3f n = (bHaveNormals) ? Mesh.GetVertexNormal(vid) : (Vector3f)normals.Normals[vid];
                NewVertices[k++] = ExtrudedPositionF(v, n, vid);
            }
            k = 0;
            foreach (int vid in ExtrudeVertices)
            {
                Mesh.SetVertex(vid, NewVertices[k++]);
            }

            SetGroupID    = Group.GetGroupID(Mesh);
            JoinTriangles = editor.StitchUnorderedEdges(EdgePairs, SetGroupID);

            return(true);
        }
Exemplo n.º 7
0
        public int AppendNewVertex(DMesh3 mesh, int vid)
        {
            int idx = ModifiedV.Length;

            ModifiedV.Add(vid);
            OldPositions.Add(mesh.GetVertex(vid));
            NewPositions.Add(OldPositions[idx]);
            if (NewNormals != null)
            {
                OldNormals.Add(mesh.GetVertexNormal(vid));
                NewNormals.Add(OldNormals[idx]);
            }
            if (NewColors != null)
            {
                OldColors.Add(mesh.GetVertexColor(vid));
                NewColors.Add(OldColors[idx]);
            }
            if (NewUVs != null)
            {
                OldUVs.Add(mesh.GetVertexUV(vid));
                NewUVs.Add(OldUVs[idx]);
            }
            return(idx);
        }
Exemplo n.º 8
0
 /// <summary>
 /// Check if this m2 is the same as this mesh. By default only checks
 /// vertices and triangles, turn on other parameters w/ flags
 /// </summary>
 public bool IsSameMesh(DMesh3 m2, bool bCheckEdges = false,
                        bool bCheckNormals          = false, bool bCheckColors = false, bool bCheckUVs = false,
                        bool bCheckGroups           = false,
                        float Epsilon = MathUtil.Epsilonf)
 {
     if (VertexCount != m2.VertexCount)
     {
         return(false);
     }
     if (TriangleCount != m2.TriangleCount)
     {
         return(false);
     }
     foreach (int vid in VertexIndices())
     {
         if (m2.IsVertex(vid) == false || GetVertex(vid).EpsilonEqual(m2.GetVertex(vid), Epsilon) == false)
         {
             return(false);
         }
     }
     foreach (int tid in TriangleIndices())
     {
         if (m2.IsTriangle(tid) == false || GetTriangle(tid).Equals(m2.GetTriangle(tid)) == false)
         {
             return(false);
         }
     }
     if (bCheckEdges)
     {
         if (EdgeCount != m2.EdgeCount)
         {
             return(false);
         }
         foreach (int eid in EdgeIndices())
         {
             if (m2.IsEdge(eid) == false || GetEdge(eid).Equals(m2.GetEdge(eid)) == false)
             {
                 return(false);
             }
         }
     }
     if (bCheckNormals)
     {
         if (HasVertexNormals != m2.HasVertexNormals)
         {
             return(false);
         }
         if (HasVertexNormals)
         {
             foreach (int vid in VertexIndices())
             {
                 if (GetVertexNormal(vid).EpsilonEqual(m2.GetVertexNormal(vid), Epsilon) == false)
                 {
                     return(false);
                 }
             }
         }
     }
     if (bCheckColors)
     {
         if (HasVertexColors != m2.HasVertexColors)
         {
             return(false);
         }
         if (HasVertexColors)
         {
             foreach (int vid in VertexIndices())
             {
                 if (GetVertexColor(vid).EpsilonEqual(m2.GetVertexColor(vid), Epsilon) == false)
                 {
                     return(false);
                 }
             }
         }
     }
     if (bCheckUVs)
     {
         if (HasVertexUVs != m2.HasVertexUVs)
         {
             return(false);
         }
         if (HasVertexUVs)
         {
             foreach (int vid in VertexIndices())
             {
                 if (GetVertexUV(vid).EpsilonEqual(m2.GetVertexUV(vid), Epsilon) == false)
                 {
                     return(false);
                 }
             }
         }
     }
     if (bCheckGroups)
     {
         if (HasTriangleGroups != m2.HasTriangleGroups)
         {
             return(false);
         }
         if (HasTriangleGroups)
         {
             foreach (int tid in TriangleIndices())
             {
                 if (GetTriangleGroup(tid) != m2.GetTriangleGroup(tid))
                 {
                     return(false);
                 }
             }
         }
     }
     return(true);
 }
Exemplo n.º 9
0
        /// <summary>
        /// Check if this m2 is the same as this mesh. By default only checks
        /// vertices and triangles, turn on other parameters w/ flags
        /// </summary>
        public bool IsSameMesh(DMesh3 m2, bool bCheckConnectivity, bool bCheckEdgeIDs = false,
                               bool bCheckNormals = false, bool bCheckColors = false, bool bCheckUVs = false,
                               bool bCheckGroups  = false,
                               float Epsilon      = MathUtil.Epsilonf)
        {
            if (VertexCount != m2.VertexCount)
            {
                return(false);
            }

            if (TriangleCount != m2.TriangleCount)
            {
                return(false);
            }

            foreach (int vid in VertexIndices())
            {
                if (m2.IsVertex(vid) == false || GetVertex(vid).EpsilonEqual(m2.GetVertex(vid), Epsilon) == false)
                {
                    return(false);
                }
            }
            foreach (int tid in TriangleIndices())
            {
                if (m2.IsTriangle(tid) == false || GetTriangle(tid).Equals(m2.GetTriangle(tid)) == false)
                {
                    return(false);
                }
            }
            if (bCheckConnectivity)
            {
                foreach (int eid in EdgeIndices())
                {
                    Index4i e         = GetEdge(eid);
                    int     other_eid = m2.FindEdge(e.a, e.b);
                    if (other_eid == InvalidID)
                    {
                        return(false);
                    }

                    Index4i oe = m2.GetEdge(other_eid);
                    if (Math.Min(e.c, e.d) != Math.Min(oe.c, oe.d) || Math.Max(e.c, e.d) != Math.Max(oe.c, oe.d))
                    {
                        return(false);
                    }
                }
            }
            if (bCheckEdgeIDs)
            {
                if (EdgeCount != m2.EdgeCount)
                {
                    return(false);
                }

                foreach (int eid in EdgeIndices())
                {
                    if (m2.IsEdge(eid) == false || GetEdge(eid).Equals(m2.GetEdge(eid)) == false)
                    {
                        return(false);
                    }
                }
            }
            if (bCheckNormals)
            {
                if (HasVertexNormals != m2.HasVertexNormals)
                {
                    return(false);
                }

                if (HasVertexNormals)
                {
                    foreach (int vid in VertexIndices())
                    {
                        if (GetVertexNormal(vid).EpsilonEqual(m2.GetVertexNormal(vid), Epsilon) == false)
                        {
                            return(false);
                        }
                    }
                }
            }
            if (bCheckColors)
            {
                if (HasVertexColors != m2.HasVertexColors)
                {
                    return(false);
                }

                if (HasVertexColors)
                {
                    foreach (int vid in VertexIndices())
                    {
                        if (GetVertexColor(vid).EpsilonEqual(m2.GetVertexColor(vid), Epsilon) == false)
                        {
                            return(false);
                        }
                    }
                }
            }
            if (bCheckUVs)
            {
                if (HasVertexUVs != m2.HasVertexUVs)
                {
                    return(false);
                }

                if (HasVertexUVs)
                {
                    foreach (int vid in VertexIndices())
                    {
                        if (GetVertexUV(vid).EpsilonEqual(m2.GetVertexUV(vid), Epsilon) == false)
                        {
                            return(false);
                        }
                    }
                }
            }
            if (bCheckGroups)
            {
                if (HasTriangleGroups != m2.HasTriangleGroups)
                {
                    return(false);
                }

                if (HasTriangleGroups)
                {
                    foreach (int tid in TriangleIndices())
                    {
                        if (GetTriangleGroup(tid) != m2.GetTriangleGroup(tid))
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }
Exemplo n.º 10
0
        public virtual bool Extrude()
        {
            MeshNormals normals      = null;
            bool        bHaveNormals = Mesh.HasVertexNormals;

            if (!bHaveNormals)
            {
                normals = new MeshNormals(Mesh);
                normals.Compute();
            }

            InitialLoops     = new MeshBoundaryLoops(Mesh);
            InitialTriangles = Mesh.TriangleIndices().ToArray();
            InitialVertices  = Mesh.VertexIndices().ToArray();

            // duplicate triangles of mesh
            InitialToOffsetMapV = new IndexMap(Mesh.MaxVertexID, Mesh.MaxVertexID);
            OffsetGroupID       = OffsetGroup.GetGroupID(Mesh);
            var editor = new MeshEditor(Mesh);

            OffsetTriangles = editor.DuplicateTriangles(InitialTriangles, ref InitialToOffsetMapV, OffsetGroupID);

            // set vertices to new positions
            foreach (int vid in InitialVertices)
            {
                int newvid = InitialToOffsetMapV[vid];
                if (!Mesh.IsVertex(newvid))
                {
                    continue;
                }

                Vector3d v    = Mesh.GetVertex(vid);
                Vector3f n    = (bHaveNormals) ? Mesh.GetVertexNormal(vid) : (Vector3f)normals.Normals[vid];
                Vector3d newv = ExtrudedPositionF(v, n, vid);

                Mesh.SetVertex(newvid, newv);
            }

            // we need to reverse one side
            if (IsPositiveOffset)
            {
                editor.ReverseTriangles(InitialTriangles);
            }
            else
            {
                editor.ReverseTriangles(OffsetTriangles);
            }

            // stitch each loop
            NewLoops        = new EdgeLoop[InitialLoops.Count];
            StitchTriangles = new int[InitialLoops.Count][];
            StitchGroupIDs  = new int[InitialLoops.Count];
            int li = 0;

            foreach (var loop in InitialLoops)
            {
                int[] loop2 = new int[loop.VertexCount];
                for (int k = 0; k < loop2.Length; ++k)
                {
                    loop2[k] = InitialToOffsetMapV[loop.Vertices[k]];
                }

                StitchGroupIDs[li] = StitchGroups.GetGroupID(Mesh);
                if (IsPositiveOffset)
                {
                    StitchTriangles[li] = editor.StitchLoop(loop2, loop.Vertices, StitchGroupIDs[li]);
                }
                else
                {
                    StitchTriangles[li] = editor.StitchLoop(loop.Vertices, loop2, StitchGroupIDs[li]);
                }
                NewLoops[li] = EdgeLoop.FromVertices(Mesh, loop2);
                li++;
            }

            return(true);
        }