Skip to content

Commit a14b24d

Browse files
committed
- added missing files
1 parent 66afb4a commit a14b24d

File tree

4 files changed

+909
-0
lines changed

4 files changed

+909
-0
lines changed

SPlisHSPlasH/Utilities/MeshImport.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
}

SPlisHSPlasH/Utilities/MeshImport.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef __MeshImport_h__
2+
#define __MeshImport_h__
3+
4+
#include "SPlisHSPlasH/Common.h"
5+
#include "SPlisHSPlasH/TriangleMesh.h"
6+
7+
namespace SPH
8+
{
9+
class MeshImport
10+
{
11+
protected:
12+
/** Load a mesh from an OBJ file in the TriangleMesh. */
13+
static bool importMesh_OBJ(const std::string& filename, TriangleMesh& mesh,
14+
const Vector3r& translation, const Matrix3r& rotation, const Vector3r& scale);
15+
16+
/** Load a mesh from an PLY file in the TriangleMesh. */
17+
static bool importMesh_PLY(const std::string& filename, TriangleMesh& mesh,
18+
const Vector3r& translation, const Matrix3r& rotation, const Vector3r& scale);
19+
20+
public:
21+
/** Load a mesh from a file in the TriangleMesh. */
22+
static bool importMesh(const std::string& filename, TriangleMesh& mesh,
23+
const Vector3r& translation, const Matrix3r& rotation, const Vector3r& scale);
24+
};
25+
}
26+
27+
#endif
28+

0 commit comments

Comments
 (0)