+ Reply to Thread
Results 1 to 4 of 4

Thread: MeshVertexToplogy

  1. #1

    MeshVertexToplogy

    Hi,

    I am trying to get my head around what looks to be the incredibly powerful MeshTopologyVertex and Edge methods in rhinocommon. Essentially I want to export a mesh with CCW radially ordered vertices to text file. However I am getting some scrambled results - it appears (on checking on some simple mesh configs) that I am not implementing the ConnectedTopologyVertices() correctly as some vertices have obviously strange connection. Code snippet is as follows:

    Python Code:
      def exportMeshTopology():
       
          """GET MESH OBJECT IDENTIFIER"""
          rc, objref = Rhino.Input.RhinoGet.GetOneObject("Select MESH", False, Rhino.DocObjects.ObjectType.Mesh)
          if rc!=Rhino.Commands.Result.Success: return
       
          meshID  = scriptcontext.doc.Objects.Find(objref.ObjectId)
          meshObj = meshID.Geometry
       
          """SETUP EXPORT TEXT FILE"""
          filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*||"
          filename = rs.SaveFileName("Gimme a destination FILENAME", filter)
          if( filename==None ): return
          output = open( filename, "w" )
         
          """PROCESS MESH"""
          meshObj.Compact()
          meshObj.Normals.ComputeNormals()
         
          mVs  = meshObj.Vertices
          mNs  = meshObj.Normals
          mFs  = meshObj.Faces
          mEs  = meshObj.TopologyEdges
          mVTs = meshObj.TopologyVertices
          mVTs.SortEdges()
       
          """EXPORT VERTICES & VERTEX DATA"""
          for i in range( mVTs.Count ):
              # vertex id
              vIndex       = mVTs.TopologyVertexIndex(i)
              vConnections = mVTs.ConnectedTopologyVertices(i)
              # text file header
              strVtxHeader = "#VERTEX "   + str( vIndex )
              # vtx coordinates
              strVtxCoord  = "vCoord "    + str(mVs[vIndex].X) +","+ str(mVs[vIndex].Y) +","+ str(mVs[vIndex].Z)
              # vtx normal
              strVtxNormal = "vNormal "   + str(mNs[vIndex].X) +","+ str(mNs[vIndex].Y) +","+ str(mNs[vIndex].Z)
              # solve connected faces
              strVtxFaces  = "vFaces "
              conFaces     = mVTs.ConnectedFaces(i)
              for j in range(conFaces.Count):
                  if(j==0):
                      strVtxFaces = strVtxFaces + str(conFaces[j])
                  else:
                      strVtxFaces = strVtxFaces +"//"+ str(conFaces[j])
              # solve vertex topology
              strVtxTopo   = "vTopo "
              for k in range(vConnections.Count):
                  if(k==0):
                      strVtxTopo = strVtxTopo + str(vConnections[k])
                  else:
                      strVtxTopo = strVtxTopo +"//"+ str(vConnections[k])
                     
              # export data
              output.write( strVtxHeader +" "+ strVtxCoord +" "+ strVtxNormal +" "+ strVtxFaces +" "+ strVtxTopo + "\n"   )
              rs.AddTextDot(strVtxHeader +"__"+ strVtxFaces +"__"+ strVtxTopo, mVs[vIndex] )
       
          """EXPORT FACES & FACEDATA"""
          for i in range(mFs.Count):
              strFaceHeader = "#FACE " + str(i)
              strFaceVtcs   = "Vtcs " + str(mFs[i].A) +"//"+ str(mFs[i].B) +"//"+ str(mFs[i].C) +"//"+ str(mFs[i].D)
              eSet          = mEs.GetEdgesForFace(i)
              strEdgeSet    = "Edges "
              arrPtCentroid = averagePoints( [ mVs[mFs[i].A], mVs[mFs[i].B], mVs[mFs[i].C], mVs[mFs[i].D] ] )
              for m in range( eSet.Count ):
                  if(m==0): strEdgeSet = strEdgeSet + str(eSet[m])
                  else:     strEdgeSet = strEdgeSet +"//"+ str(eSet[m])
              # notate
              rs.AddTextDot( str(i) +"__"+ strFaceVtcs +"__"+ strEdgeSet, arrPtCentroid )
              # export data
              output.write(strFaceHeader +" "+ strFaceVtcs +" "+ strEdgeSet + "\n")
       
          output.flush()
          output.close()
       
      def averagePoints(PTS):
          count = 0
          sum   = Rhino.Geometry.Point3d(0,0,0)
          for i in range(PTS.Count):
              sum = Rhino.Geometry.Point3d.Add(sum, PTS[i])
              count+=1
          avg   = Rhino.Geometry.Point3d.Divide(sum, count)
          return avg
       
      if( __name__ == "__main__" ):
          exportMeshTopology()
    Last edited by Steve Baer; 03-12-2012 at 11:18 PM. Reason: fixed syntax highlighting

  2. #2
    Senior Member Steve Baer's Avatar
    Join Date
    Apr 2010
    Location
    Seattle
    Posts
    1,464
    Blog Entries
    19
    Topology vertices represent a single location in space. There may be multiple regular mesh vertices for any given topology vertex. If you are trying to just export a list of faces and vertex locations for each face then you don't need the topology stuff at all.

  3. #3
    Hi Steve,

    Assuming the mesh is valid and has no duplicate vertices and that exporting vertices and faces (using rhinoscriptsyntax or rhinocommon) is no problem. What I want to achieve is an understanding of all the topological connections (edges and the vertices at the other end of these edges), ordered counter-clockwise, for each vertex of an arbitrary mesh. The vertex/edge topology is critical for latter fluid vector fields I am building across an arbitrary (unordered) mesh.

  4. #4
    Senior Member Steve Baer's Avatar
    Join Date
    Apr 2010
    Location
    Seattle
    Posts
    1,464
    Blog Entries
    19
    One piece of code that jumps out at me is the line
    Python Code:
      for i in range( mVTs.Count ):
          # vertex id
          vIndex       = mVTs.TopologyVertexIndex(i)

    TopologyVertexIndex finds the corresponding index in the TopologyVertices list for a given index of a vertex in the Vertices list. If you want the location of the TopologyVertex, then you can just rewrite your script as
    Python Code:
      for i in range( mVTs.Count ):
          coordinate = mVTs[i]
          ...

    I'm not sure that is what you're after though since you are then trying to export the normal. A TopologyVertex may have multiple vertices associated with it and therefore multiple normals. There is a one-to-one association of vertices and normals, but there can be a many-to-one association of vertices to topology vertices.

    I hope I'm being somewhat clear; it is a hard topic to describe without making some pictures.
    -Steve

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts