diff --git a/collada_urdf/src/STLLoader.cpp b/collada_urdf/src/STLLoader.cpp index 446310e..0443c68 100644 --- a/collada_urdf/src/STLLoader.cpp +++ b/collada_urdf/src/STLLoader.cpp @@ -101,8 +101,6 @@ void STLLoader::readBinary(FILE* filein, Mesh* mesh) { int face_num = readLongInt(filein); - std::cout << "Reading " << face_num << " faces " << std::endl; - for (int iface = 0; iface < face_num; iface++) { Vector3 normal(readFloat(filein), readFloat(filein), readFloat(filein)); for (int i = 0; i < 3; i++) { diff --git a/collada_urdf/src/urdf_to_collada.cpp b/collada_urdf/src/urdf_to_collada.cpp index 30efe46..2011d6d 100644 --- a/collada_urdf/src/urdf_to_collada.cpp +++ b/collada_urdf/src/urdf_to_collada.cpp @@ -279,31 +279,43 @@ public: return; } - if (true) { - // Write the resource to a temporary file - char tmp_filename[] = "/tmp/collada_urdf_XXXXXX"; - int fd = mkstemp(tmp_filename); - write(fd, resource.data.get(), resource.size); - close(fd); + // Try assimp first, then STLLoader + if (!loadMeshWithAssimp(resource, mesh)) + if (!loadMeshWithSTLLoader(resource, mesh)) + cerr << "*** Can't load " << filename << endl; + } - // Import the mesh using STLLoader - STLLoader loader; - Mesh* mesh = loader.load(string(tmp_filename)); - delete mesh; - - // Delete the temporary file - unlink(tmp_filename); - } - else { - // Import the mesh using assimp - const aiScene* scene = importer_.ReadFileFromMemory(resource.data.get(), resource.size, aiProcess_SortByPType /* aiProcess_CalcTangentSpace | aiProcess_Triangulate | aiProcess_JoinIdenticalVertices */); - if (!scene) - cerr << "Unable to import mesh " << filename << ": " << importer_.GetErrorString() << endl; - else { - cout << "Successfully loaded mesh " << filename << endl; - buildMeshFromAssimp(scene->mRootNode, mesh); - } - } + bool loadMeshWithAssimp(const resource_retriever::MemoryResource& resource, domGeometryRef mesh) { + // Import the mesh using assimp + const aiScene* scene = importer_.ReadFileFromMemory(resource.data.get(), resource.size, aiProcess_SortByPType /* aiProcess_CalcTangentSpace | aiProcess_Triangulate | aiProcess_JoinIdenticalVertices */); + if (!scene) + return false; + + buildMeshFromAssimp(scene->mRootNode, mesh); + return true; + } + + bool loadMeshWithSTLLoader(const resource_retriever::MemoryResource& resource, domGeometryRef mesh) { + // Write the resource to a temporary file + char tmp_filename[] = "/tmp/collada_urdf_XXXXXX"; + int fd = mkstemp(tmp_filename); + write(fd, resource.data.get(), resource.size); + close(fd); + + // Import the mesh using STLLoader + STLLoader loader; + Mesh* stl_mesh = loader.load(string(tmp_filename)); + buildMeshFromSTLLoader(stl_mesh, mesh); + delete stl_mesh; + + // Delete the temporary file + unlink(tmp_filename); + + return true; + } + + void buildMeshFromSTLLoader(Mesh* mesh, daeElementRef parent) { + // } void buildMeshFromAssimp(aiNode* node, daeElementRef parent) {