collada_urdf: first try assimp, then fallback to STLLoader

This commit is contained in:
tfield 2010-03-05 03:05:35 +00:00
parent 6fd7f8a489
commit 1a48be8bb4
2 changed files with 36 additions and 26 deletions

View File

@ -101,8 +101,6 @@ void STLLoader::readBinary(FILE* filein, Mesh* mesh) {
int face_num = readLongInt(filein); int face_num = readLongInt(filein);
std::cout << "Reading " << face_num << " faces " << std::endl;
for (int iface = 0; iface < face_num; iface++) { for (int iface = 0; iface < face_num; iface++) {
Vector3 normal(readFloat(filein), readFloat(filein), readFloat(filein)); Vector3 normal(readFloat(filein), readFloat(filein), readFloat(filein));
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {

View File

@ -279,31 +279,43 @@ public:
return; return;
} }
if (true) { // Try assimp first, then STLLoader
// Write the resource to a temporary file if (!loadMeshWithAssimp(resource, mesh))
char tmp_filename[] = "/tmp/collada_urdf_XXXXXX"; if (!loadMeshWithSTLLoader(resource, mesh))
int fd = mkstemp(tmp_filename); cerr << "*** Can't load " << filename << endl;
write(fd, resource.data.get(), resource.size); }
close(fd);
// Import the mesh using STLLoader bool loadMeshWithAssimp(const resource_retriever::MemoryResource& resource, domGeometryRef mesh) {
STLLoader loader; // Import the mesh using assimp
Mesh* mesh = loader.load(string(tmp_filename)); const aiScene* scene = importer_.ReadFileFromMemory(resource.data.get(), resource.size, aiProcess_SortByPType /* aiProcess_CalcTangentSpace | aiProcess_Triangulate | aiProcess_JoinIdenticalVertices */);
delete mesh; if (!scene)
return false;
// Delete the temporary file
unlink(tmp_filename); buildMeshFromAssimp(scene->mRootNode, mesh);
} return true;
else { }
// Import the mesh using assimp
const aiScene* scene = importer_.ReadFileFromMemory(resource.data.get(), resource.size, aiProcess_SortByPType /* aiProcess_CalcTangentSpace | aiProcess_Triangulate | aiProcess_JoinIdenticalVertices */); bool loadMeshWithSTLLoader(const resource_retriever::MemoryResource& resource, domGeometryRef mesh) {
if (!scene) // Write the resource to a temporary file
cerr << "Unable to import mesh " << filename << ": " << importer_.GetErrorString() << endl; char tmp_filename[] = "/tmp/collada_urdf_XXXXXX";
else { int fd = mkstemp(tmp_filename);
cout << "Successfully loaded mesh " << filename << endl; write(fd, resource.data.get(), resource.size);
buildMeshFromAssimp(scene->mRootNode, mesh); 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) { void buildMeshFromAssimp(aiNode* node, daeElementRef parent) {