1313#include " ../Visualization/Visualization.h"
1414#include " Simulation/DistanceFieldCollisionDetection.h"
1515#include " Demos/Common/TweakBarParameters.h"
16+ #include " Utils/OBJLoader.h"
17+ #include " Utils/PLYLoader.h"
1618
1719INIT_LOGGING
1820INIT_TIMING
@@ -898,4 +900,91 @@ void DemoBase::selection(const Vector2i &start, const Vector2i &end, void *clien
898900
899901void DemoBase::reset ()
900902{
901- }
903+ }
904+
905+ void DemoBase::loadMesh (const std::string& filename, VertexData& vd, Utilities::IndexedFaceMesh& mesh, const Vector3r& translation,
906+ const Matrix3r& rotation, const Vector3r& scale)
907+ {
908+ string ext = FileSystem::getFileExt (filename);
909+ transform (ext.begin (), ext.end (), ext.begin (), ::toupper);
910+
911+ std::vector<std::array<float , 3 >> x;
912+ if (ext == " OBJ" )
913+ {
914+ std::vector<OBJLoader::Vec3f> normals;
915+ std::vector<OBJLoader::Vec2f> texCoords;
916+ std::vector<MeshFaceIndices> faces;
917+ OBJLoader::Vec3f s = { (float )scale[0 ], (float )scale[1 ], (float )scale[2 ] };
918+ OBJLoader::loadObj (filename, &x, &faces, &normals, &texCoords, s);
919+
920+ mesh.release ();
921+ const unsigned int nPoints = (unsigned int )x.size ();
922+ const unsigned int nFaces = (unsigned int )faces.size ();
923+ const unsigned int nTexCoords = (unsigned int )texCoords.size ();
924+ mesh.initMesh (nPoints, nFaces * 2 , nFaces);
925+ vd.reserve (nPoints);
926+ for (unsigned int i = 0 ; i < nPoints; i++)
927+ {
928+ vd.addVertex (Vector3r (x[i][0 ], x[i][1 ], x[i][2 ]));
929+ }
930+ for (unsigned int i = 0 ; i < nTexCoords; i++)
931+ {
932+ mesh.addUV (texCoords[i][0 ], texCoords[i][1 ]);
933+ }
934+ for (unsigned int i = 0 ; i < nFaces; i++)
935+ {
936+ int posIndices[3 ];
937+ int texIndices[3 ];
938+ for (int j = 0 ; j < 3 ; j++)
939+ {
940+ posIndices[j] = faces[i].posIndices [j];
941+ if (nTexCoords > 0 )
942+ {
943+ texIndices[j] = faces[i].texIndices [j];
944+ mesh.addUVIndex (texIndices[j]);
945+ }
946+ }
947+
948+ mesh.addFace (&posIndices[0 ]);
949+ }
950+ mesh.buildNeighbors ();
951+
952+ mesh.updateNormals (vd, 0 );
953+ mesh.updateVertexNormals (vd);
954+
955+ LOG_INFO << " Number of triangles: " << nFaces;
956+ LOG_INFO << " Number of vertices: " << nPoints;
957+ }
958+ else if (ext == " PLY" )
959+ {
960+ std::vector<std::array<int , 3 >> faces;
961+ OBJLoader::Vec3f s = { (float )scale[0 ], (float )scale[1 ], (float )scale[2 ] };
962+ PLYLoader::loadPly (filename, x, faces, s);
963+
964+ mesh.release ();
965+ const unsigned int nPoints = (unsigned int )x.size ();
966+ const unsigned int nFaces = (unsigned int )faces.size ();
967+ mesh.initMesh (nPoints, nFaces * 2 , nFaces);
968+ vd.reserve (nPoints);
969+ for (unsigned int i = 0 ; i < nPoints; i++)
970+ {
971+ vd.addVertex (Vector3r (x[i][0 ], x[i][1 ], x[i][2 ]));
972+ }
973+ for (unsigned int i = 0 ; i < nFaces; i++)
974+ {
975+ int posIndices[3 ];
976+ for (int j = 0 ; j < 3 ; j++)
977+ posIndices[j] = faces[i][j];
978+
979+ mesh.addFace (&posIndices[0 ]);
980+ }
981+
982+ LOG_INFO << " Number of triangles: " << nFaces;
983+ LOG_INFO << " Number of vertices: " << nPoints;
984+
985+ }
986+ else
987+ LOG_ERR << " File " << filename << " has a unknown file type." ;
988+ }
989+
990+
0 commit comments