使用Assimp导入STL模型时遇到的顶点数量问题及解决方案(开源代码)
最编程
2024-08-04 09:43:33
...
使用Assimp示例代码导入stl文件:
1 #include <assimp/Importer.hpp> // C++ importer interface 2 #include <assimp/scene.h> // Output data structure 3 #include <assimp/postprocess.h> // Post processing flags 4 bool DoTheImportThing(const std::string& pFile) 5 { 6 // Create an instance of the Importer class 7 Assimp::Importer importer; 8 // And have it read the given file with some example postprocessing 9 // Usually - if speed is not the most important aspect for you - you'll 10 // propably to request more postprocessing than we do in this example. 11 const aiScene* scene = importer.ReadFile(pFile, 12 aiProcess_CalcTangentSpace | 13 aiProcess_Triangulate | 14 aiProcess_JoinIdenticalVertices | 15 aiProcess_SortByPType); 16 17 // If the import failed, report it 18 if (!scene) 19 { 20 //DoTheErrorLogging(importer.GetErrorString()); 21 std::printf("failed to import %s!\n", pFile.c_str()); 22 return false; 23 } 24 // Now we can access the file's contents. 25 //DoTheSceneProcessing(scene); 26 if (scene->HasMeshes()) 27 { 28 std::printf("has %d meshes!\n", scene->mNumMeshes); 29 for (int i = 0; i < scene->mNumMeshes; ++i) 30 { 31 std::printf("%d: %u vertices!\n", i, scene->mMeshes[i]->mNumVertices); 32 if (scene->mMeshes[i]->HasFaces()) 33 std::printf("%d: %u faces!\n", i, scene->mMeshes[i]->mNumFaces); 34 } 35 } 36 // We're done. Everything will be cleaned up by the importer destructor 37 return true; 38 } 39 40 41 int main() 42 { 43 std::string File = ".\\bottle.stl"; 44 DoTheImportThing(File); 45 46 system("pause"); 47 return 0; 48 }
输出:
has 1 meshes! 0: 553770 vertices! 0: 190624 faces!
MeshLab导入同一stl模型的结果:
0: 95314 vertices! 0: 190624 faces!
问题在于顶点合并:
assimp-5.0.1\code\PostProcessing\JoinVerticesProcess.cpp
1 // A little helper to find locally close vertices faster. 2 // Try to reuse the lookup table from the last step. 3 const static float epsilon = 1e-5f; 4 // Squared because we check against squared length of the vector difference 5 static const float squareEpsilon = epsilon * epsilon; 6 7 // Square compare is useful for animeshes vertices compare 8 if ((lhs.position - rhs.position).SquareLength() > squareEpsilon) { 9 return false; 10 }