1+ #include " MeshImport.h"
2+
3+ #include < Utilities/Logger.h>
4+ #include < Utilities/FileSystem.h>
5+ #include < Utilities/PLYLoader.h>
6+ #include < Utilities/OBJLoader.h>
7+
8+ using namespace SPH ;
9+ using namespace Utilities ;
10+ using namespace std ;
11+
12+
13+ bool MeshImport::importMesh (const std::string& filename, TriangleMesh& mesh,
14+ const Vector3r& translation, const Matrix3r& rotation, const Vector3r& scale)
15+ {
16+ if (!FileSystem::fileExists (filename))
17+ {
18+ LOG_ERR << " File not found: " << filename;
19+ return false ;
20+ }
21+ string ext = FileSystem::getFileExt (filename);
22+ transform (ext.begin (), ext.end (), ext.begin (), ::toupper);
23+
24+ if (ext == " PLY" )
25+ return importMesh_PLY (filename, mesh, translation, rotation, scale);
26+ else if (ext == " OBJ" )
27+ return importMesh_OBJ (filename, mesh, translation, rotation, scale);
28+ else
29+ {
30+ LOG_ERR << " File " << filename << " has an unknown file type." ;
31+ return false ;
32+ }
33+ }
34+
35+ bool MeshImport::importMesh_PLY (const std::string& filename, TriangleMesh& mesh,
36+ const Vector3r& translation, const Matrix3r& rotation, const Vector3r& scale)
37+ {
38+ std::vector<std::array<float , 3 >> x;
39+ std::vector<std::array<int , 3 >> faces;
40+ std::array<float , 3 > s = { (float )scale[0 ], (float )scale[1 ], (float )scale[2 ] };
41+ PLYLoader::loadPly (filename, x, faces, s);
42+
43+ mesh.release ();
44+ const unsigned int nPoints = (unsigned int )x.size ();
45+ const unsigned int nFaces = (unsigned int )faces.size ();
46+ mesh.initMesh (nPoints, nFaces);
47+ for (unsigned int i = 0 ; i < nPoints; i++)
48+ {
49+ mesh.addVertex (Vector3r (x[i][0 ], x[i][1 ], x[i][2 ]));
50+ }
51+ for (unsigned int i = 0 ; i < nFaces; i++)
52+ {
53+ int posIndices[3 ];
54+ for (int j = 0 ; j < 3 ; j++)
55+ posIndices[j] = faces[i][j];
56+
57+ mesh.addFace (&posIndices[0 ]);
58+ }
59+
60+ LOG_INFO << " Number of triangles: " << nFaces;
61+ LOG_INFO << " Number of vertices: " << nPoints;
62+ return true ;
63+ }
64+
65+
66+ bool MeshImport::importMesh_OBJ (const std::string& filename, TriangleMesh& mesh,
67+ const Vector3r& translation, const Matrix3r& rotation, const Vector3r& scale)
68+ {
69+ std::vector<std::array<float , 3 >> x;
70+ std::vector<OBJLoader::Vec3f> normals;
71+ std::vector<MeshFaceIndices> faces;
72+ OBJLoader::Vec3f s = { (float )scale[0 ], (float )scale[1 ], (float )scale[2 ] };
73+ OBJLoader::loadObj (filename, &x, &faces, &normals, nullptr , s);
74+
75+ mesh.release ();
76+ const unsigned int nPoints = (unsigned int )x.size ();
77+ const unsigned int nFaces = (unsigned int )faces.size ();
78+ mesh.initMesh (nPoints, nFaces);
79+ for (unsigned int i = 0 ; i < nPoints; i++)
80+ {
81+ mesh.addVertex (rotation * Vector3r (x[i][0 ], x[i][1 ], x[i][2 ]) + translation);
82+ }
83+ for (unsigned int i = 0 ; i < nFaces; i++)
84+ {
85+ int posIndices[3 ];
86+ for (int j = 0 ; j < 3 ; j++)
87+ {
88+ posIndices[j] = faces[i].posIndices [j];
89+ }
90+
91+ mesh.addFace (&posIndices[0 ]);
92+ }
93+
94+ LOG_INFO << " Number of triangles: " << nFaces;
95+ LOG_INFO << " Number of vertices: " << nPoints;
96+
97+ return true ;
98+ }
0 commit comments