2

I have created the following simple C++ script with OpenMesh:

#include <string>
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>

struct MyTraits : OpenMesh::DefaultTraits{
     typedef OpenMesh::Vec3d Point;
     typedef OpenMesh::Vec3d Normal;
};

typedef OpenMesh::TriMesh_ArrayKernelT<MyTraits>  MyMesh;

int main(int argc, char *argv[]){
    std::string filename = "filename.stl";    
    MyMesh OM_mesh;

    OM_mesh.request_face_normals();
    OM_mesh.request_halfedge_normals();
    OM_mesh.request_vertex_normals();
    OM_mesh.request_face_status();
    OM_mesh.request_edge_status();
    OM_mesh.request_halfedge_status();
    OM_mesh.request_vertex_status();

    OpenMesh::IO::Options ropt;
    ropt += OpenMesh::IO::Options::Binary;
    ropt += OpenMesh::IO::Options::FaceNormal;

    OpenMesh::IO::read_mesh(OM_mesh, filename);

    for(int k=0; k<1000; k++){
        OM_mesh.update_face_normals();
    }

    return 0;
}

Also, I have developed the following simple Python script using the OpenMesh bindings:

import openmesh as OM
filename = "filename.stl"
OM_mesh = OM.TriMesh()

OM_mesh.request_face_normals()
OM_mesh.request_halfedge_normals()
OM_mesh.request_vertex_normals()
OM_mesh.request_face_status()
OM_mesh.request_edge_status()
OM_mesh.request_halfedge_status()
OM_mesh.request_vertex_status()

options = OM.Options()
options += OM.Options.Binary
options += OM.Options.FaceNormal

OM.read_mesh(OM_mesh, filename, options)
for k in range(1000):
    OM_mesh.update_face_normals()

Both scripts update the face normals of the loaded mesh 1000 times. I expected that the C++ script would be considerably faster than the Python script, but in fact it is just the opposite. I found that the C++ script spends around 8 seconds, while the Python script only spends around 0.3 seconds.

How can this be possible? Are the Python bindings doing something different than just "wrap" the C++ update_face_normals method? Thanks.

GLR
  • 1,070
  • 1
  • 11
  • 29
  • Did you enable optimizations in C++? Also keep in mind that C++ is not a silver bullet. In fact, you can easily write crappy code on C++ that will be even slower than bash. – The Quantum Physicist Mar 03 '17 at 11:12
  • Yes, I did. Furthermore, the C++ code is more or less the same than the Python code, I am only using the Python bindings provided by OpenMesh. – GLR Mar 03 '17 at 11:28

1 Answers1

3

I've found that I should use the reading options when I read the file in C++, like this:

OpenMesh::IO::read_mesh(OM_mesh, filename, ropt);

By doing so, the speed in C++ is higher than in Python. However, in .off files, this update is not correct, but this is another issue.

GLR
  • 1,070
  • 1
  • 11
  • 29