Skip to main content
Solved

Count number of vertex and triangle of a 3D geometry that will be export to OBJ

  • June 30, 2017
  • 3 replies
  • 87 views

lavairye
Contributor
Forum|alt.badge.img

Hello,

I have CityGML files for buildings and I want to know and expose the number of vertex and triangles that will be exported by the OBJ writer.

I use the aggregator transformer to merge all the geometries and count the number of vertex, then remove all duplicated vertex (not immediate, but feasible), but I don't know how to find the number of triangles.

Is somebody having an idea ?

Maybe using the pythocaller, but I'm not a specialist !

Best answer by daveatsafe

Hi @lavairye

The CityGML surfaces will be converted to triangular meshes by the OBJ writer, but in order to count the resulting vertices, we can do the mesh conversion before writing, with the Triangulator transformer.

After the Triangulator, add a PythonCaller with the following code:

import fme
import fmeobjects
def meshStats(feature):
    # extract geometry from feature
    geom = feature.getGeometry()
    # get number of vertices in Mesh
    numVerts = geom.numVertices()
    # set number of vertices on feature as attribute
    feature.setAttribute("_numVertices", numVerts)
    # convert Mesh to MultiSurface
    msurf = geom.getAsMultiSurface()
    # get number of parts in MultiSurface
    numTriangles = msurf.numParts()
    # set number of parts on feature as attribute
    feature.setAttribute("_numTriangles", numTriangles)

This will create two new attributes on the feature:

  • _numVertices will contain the number of vertices in the Mesh
  • _numTriangles will contain the number of triangles in the Mesh

Although the geometry in the Python is converted from a Mesh to a MultiSurface to get the number of triangles, that geometry is not written back to the feature, so the feature geometry will be unchanged.

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

3 replies

daveatsafe
Safer
Forum|alt.badge.img+20
  • Safer
  • Best Answer
  • June 30, 2017

Hi @lavairye

The CityGML surfaces will be converted to triangular meshes by the OBJ writer, but in order to count the resulting vertices, we can do the mesh conversion before writing, with the Triangulator transformer.

After the Triangulator, add a PythonCaller with the following code:

import fme
import fmeobjects
def meshStats(feature):
    # extract geometry from feature
    geom = feature.getGeometry()
    # get number of vertices in Mesh
    numVerts = geom.numVertices()
    # set number of vertices on feature as attribute
    feature.setAttribute("_numVertices", numVerts)
    # convert Mesh to MultiSurface
    msurf = geom.getAsMultiSurface()
    # get number of parts in MultiSurface
    numTriangles = msurf.numParts()
    # set number of parts on feature as attribute
    feature.setAttribute("_numTriangles", numTriangles)

This will create two new attributes on the feature:

  • _numVertices will contain the number of vertices in the Mesh
  • _numTriangles will contain the number of triangles in the Mesh

Although the geometry in the Python is converted from a Mesh to a MultiSurface to get the number of triangles, that geometry is not written back to the feature, so the feature geometry will be unchanged.


takashi
Celebrity
  • July 1, 2017

Hi @lavairye

The CityGML surfaces will be converted to triangular meshes by the OBJ writer, but in order to count the resulting vertices, we can do the mesh conversion before writing, with the Triangulator transformer.

After the Triangulator, add a PythonCaller with the following code:

import fme
import fmeobjects
def meshStats(feature):
    # extract geometry from feature
    geom = feature.getGeometry()
    # get number of vertices in Mesh
    numVerts = geom.numVertices()
    # set number of vertices on feature as attribute
    feature.setAttribute("_numVertices", numVerts)
    # convert Mesh to MultiSurface
    msurf = geom.getAsMultiSurface()
    # get number of parts in MultiSurface
    numTriangles = msurf.numParts()
    # set number of parts on feature as attribute
    feature.setAttribute("_numTriangles", numTriangles)

This will create two new attributes on the feature:

  • _numVertices will contain the number of vertices in the Mesh
  • _numTriangles will contain the number of triangles in the Mesh

Although the geometry in the Python is converted from a Mesh to a MultiSurface to get the number of triangles, that geometry is not written back to the feature, so the feature geometry will be unchanged.

It seems that the FMEMesh.numParts method (inherited from FMESimpleSurface class) can also be used here.

 

def meshStats(feature):
    # extract geometry from feature
    geom = feature.getGeometry()
    
    # get number of vertices and parts in Mesh
    numVerts = geom.numVertices()
    numTriangles = geom.numParts()
    
    # set number of vertices and parts on feature as attributes
    feature.setAttribute("_numVertices", numVerts)
    feature.setAttribute("_numTriangles", numTriangles) 

lavairye
Contributor
Forum|alt.badge.img
  • Author
  • Contributor
  • July 2, 2017

Thanks a lot. All my workflow works fine now. I shall use more python in the future!