Skip to content

Commit 2e599ee

Browse files
committed
- updated pybind11
- cleanup mesh loading code - added support of PLY files
1 parent 2fed40c commit 2e599ee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+11425
-6716
lines changed

Changelog.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2.1.5
2+
- updated pybind11
3+
- cleanup mesh loading code
4+
- added support of PLY files
5+
16
2.1.4
27
- cleanup some code
38
- glfw fps limit

Demos/Common/DemoBase.cpp

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
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

1719
INIT_LOGGING
1820
INIT_TIMING
@@ -898,4 +900,91 @@ void DemoBase::selection(const Vector2i &start, const Vector2i &end, void *clien
898900

899901
void 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+

Demos/Common/DemoBase.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ namespace PBD
121121
std::string getOutputPath() const { return m_outputPath; }
122122

123123
Utilities::SceneLoader::SceneData& getSceneData() { return m_scene; }
124+
125+
static void loadMesh(const std::string& filename, VertexData& vd, Utilities::IndexedFaceMesh& mesh, const Vector3r& translation = Vector3r::Zero(),
126+
const Matrix3r& rotation = Matrix3r::Identity(), const Vector3r& scale = Vector3r::Ones());
124127
};
125128
}
126129

Demos/CouplingDemos/RigidBodyClothCouplingDemo.cpp

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <iostream>
99
#include "Simulation/Constraints.h"
1010
#include "Demos/Visualization/Visualization.h"
11-
#include "Utils/OBJLoader.h"
1211
#include "Utils/Logger.h"
1312
#include "Utils/Timing.h"
1413
#include "Utils/FileSystem.h"
@@ -190,53 +189,6 @@ Vector3r computeInertiaTensorBox(const Real mass, const Real width, const Real h
190189
return Vector3r(Ix, Iy, Iz);
191190
}
192191

193-
void loadObj(const std::string &filename, VertexData &vd, IndexedFaceMesh &mesh, const Vector3r &scale)
194-
{
195-
std::vector<OBJLoader::Vec3f> x;
196-
std::vector<OBJLoader::Vec3f> normals;
197-
std::vector<OBJLoader::Vec2f> texCoords;
198-
std::vector<MeshFaceIndices> faces;
199-
OBJLoader::Vec3f s = { (float)scale[0], (float)scale[1], (float)scale[2] };
200-
OBJLoader::loadObj(filename, &x, &faces, &normals, &texCoords, s);
201-
202-
mesh.release();
203-
const unsigned int nPoints = (unsigned int)x.size();
204-
const unsigned int nFaces = (unsigned int)faces.size();
205-
const unsigned int nTexCoords = (unsigned int)texCoords.size();
206-
mesh.initMesh(nPoints, nFaces * 2, nFaces);
207-
vd.reserve(nPoints);
208-
for (unsigned int i = 0; i < nPoints; i++)
209-
{
210-
vd.addVertex(Vector3r(x[i][0], x[i][1], x[i][2]));
211-
}
212-
for (unsigned int i = 0; i < nTexCoords; i++)
213-
{
214-
mesh.addUV(texCoords[i][0], texCoords[i][1]);
215-
}
216-
for (unsigned int i = 0; i < nFaces; i++)
217-
{
218-
int posIndices[3];
219-
int texIndices[3];
220-
for (int j = 0; j < 3; j++)
221-
{
222-
posIndices[j] = faces[i].posIndices[j];
223-
if (nTexCoords > 0)
224-
{
225-
texIndices[j] = faces[i].texIndices[j];
226-
mesh.addUVIndex(texIndices[j]);
227-
}
228-
}
229-
230-
mesh.addFace(&posIndices[0]);
231-
}
232-
mesh.buildNeighbors();
233-
234-
mesh.updateNormals(vd, 0);
235-
mesh.updateVertexNormals(vd);
236-
237-
LOG_INFO << "Number of triangles: " << nFaces;
238-
LOG_INFO << "Number of vertices: " << nPoints;
239-
}
240192

241193
/** Create the model
242194
*/
@@ -248,12 +200,12 @@ void createRigidBodyModel()
248200
string fileName = FileSystem::normalizePath(base->getExePath() + "/resources/models/cube.obj");
249201
IndexedFaceMesh mesh;
250202
VertexData vd;
251-
loadObj(fileName, vd, mesh, Vector3r(width, height, depth));
203+
DemoBase::loadMesh(fileName, vd, mesh, Vector3r::Zero(), Matrix3r::Identity(), Vector3r(width, height, depth));
252204
mesh.setFlatShading(true);
253205

254206
IndexedFaceMesh mesh_static;
255207
VertexData vd_static;
256-
loadObj(fileName, vd_static, mesh_static, Vector3r(0.5, 0.5, 0.5));
208+
DemoBase::loadMesh(fileName, vd_static, mesh_static, Vector3r::Zero(), Matrix3r::Identity(), Vector3r(0.5, 0.5, 0.5));
257209
mesh_static.setFlatShading(true);
258210

259211
rb.resize(12);

Demos/DistanceFieldDemos/ClothCollisionDemo.cpp

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <iostream>
99
#include "Demos/Visualization/Visualization.h"
1010
#include "Simulation/DistanceFieldCollisionDetection.h"
11-
#include "Utils/OBJLoader.h"
1211
#include "Utils/Logger.h"
1312
#include "Utils/Timing.h"
1413
#include "Utils/FileSystem.h"
@@ -164,54 +163,6 @@ void timeStep ()
164163
model->getTriangleModels()[i]->updateMeshNormals(model->getParticles());
165164
}
166165

167-
void loadObj(const std::string &filename, VertexData &vd, IndexedFaceMesh &mesh, const Vector3r &scale)
168-
{
169-
std::vector<OBJLoader::Vec3f> x;
170-
std::vector<OBJLoader::Vec3f> normals;
171-
std::vector<OBJLoader::Vec2f> texCoords;
172-
std::vector<MeshFaceIndices> faces;
173-
OBJLoader::Vec3f s = { (float)scale[0], (float)scale[1], (float)scale[2] };
174-
OBJLoader::loadObj(filename, &x, &faces, &normals, &texCoords, s);
175-
176-
mesh.release();
177-
const unsigned int nPoints = (unsigned int)x.size();
178-
const unsigned int nFaces = (unsigned int)faces.size();
179-
const unsigned int nTexCoords = (unsigned int)texCoords.size();
180-
mesh.initMesh(nPoints, nFaces * 2, nFaces);
181-
vd.reserve(nPoints);
182-
for (unsigned int i = 0; i < nPoints; i++)
183-
{
184-
vd.addVertex(Vector3r(x[i][0], x[i][1], x[i][2]));
185-
}
186-
for (unsigned int i = 0; i < nTexCoords; i++)
187-
{
188-
mesh.addUV(texCoords[i][0], texCoords[i][1]);
189-
}
190-
for (unsigned int i = 0; i < nFaces; i++)
191-
{
192-
int posIndices[3];
193-
int texIndices[3];
194-
for (int j = 0; j < 3; j++)
195-
{
196-
posIndices[j] = faces[i].posIndices[j];
197-
if (nTexCoords > 0)
198-
{
199-
texIndices[j] = faces[i].texIndices[j];
200-
mesh.addUVIndex(texIndices[j]);
201-
}
202-
}
203-
204-
mesh.addFace(&posIndices[0]);
205-
}
206-
mesh.buildNeighbors();
207-
208-
mesh.updateNormals(vd, 0);
209-
mesh.updateVertexNormals(vd);
210-
211-
LOG_INFO << "Number of triangles: " << nFaces;
212-
LOG_INFO << "Number of vertices: " << nPoints;
213-
}
214-
215166
void buildModel ()
216167
{
217168
TimeManager::getCurrent ()->setTimeStepSize (static_cast<Real>(0.005));
@@ -222,13 +173,13 @@ void buildModel ()
222173
string fileName = FileSystem::normalizePath(base->getExePath() + "/resources/models/cube.obj");
223174
IndexedFaceMesh mesh;
224175
VertexData vd;
225-
loadObj(fileName, vd, mesh, Vector3r::Ones());
176+
DemoBase::loadMesh(fileName, vd, mesh, Vector3r::Zero(), Matrix3r::Identity(), Vector3r::Ones());
226177
mesh.setFlatShading(true);
227178

228179
string fileNameTorus = FileSystem::normalizePath(base->getExePath() + "/resources/models/torus.obj");
229180
IndexedFaceMesh meshTorus;
230181
VertexData vdTorus;
231-
loadObj(fileNameTorus, vdTorus, meshTorus, Vector3r::Ones());
182+
DemoBase::loadMesh(fileNameTorus, vdTorus, meshTorus, Vector3r::Zero(), Matrix3r::Identity(), Vector3r::Ones());
232183

233184
SimulationModel *model = Simulation::getCurrent()->getModel();
234185
SimulationModel::RigidBodyVector &rb = model->getRigidBodies();

Demos/DistanceFieldDemos/DeformableCollisionDemo.cpp

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <iostream>
99
#include "Demos/Visualization/Visualization.h"
1010
#include "Simulation/DistanceFieldCollisionDetection.h"
11-
#include "Utils/OBJLoader.h"
1211
#include "Utils/Logger.h"
1312
#include "Utils/Timing.h"
1413
#include "Utils/FileSystem.h"
@@ -149,54 +148,6 @@ void timeStep ()
149148
model->getTetModels()[i]->updateMeshNormals(model->getParticles());
150149
}
151150

152-
void loadObj(const std::string &filename, VertexData &vd, IndexedFaceMesh &mesh, const Vector3r &scale)
153-
{
154-
std::vector<OBJLoader::Vec3f> x;
155-
std::vector<OBJLoader::Vec3f> normals;
156-
std::vector<OBJLoader::Vec2f> texCoords;
157-
std::vector<MeshFaceIndices> faces;
158-
OBJLoader::Vec3f s = { (float)scale[0], (float)scale[1], (float)scale[2] };
159-
OBJLoader::loadObj(filename, &x, &faces, &normals, &texCoords, s);
160-
161-
mesh.release();
162-
const unsigned int nPoints = (unsigned int)x.size();
163-
const unsigned int nFaces = (unsigned int)faces.size();
164-
const unsigned int nTexCoords = (unsigned int)texCoords.size();
165-
mesh.initMesh(nPoints, nFaces * 2, nFaces);
166-
vd.reserve(nPoints);
167-
for (unsigned int i = 0; i < nPoints; i++)
168-
{
169-
vd.addVertex(Vector3r(x[i][0], x[i][1], x[i][2]));
170-
}
171-
for (unsigned int i = 0; i < nTexCoords; i++)
172-
{
173-
mesh.addUV(texCoords[i][0], texCoords[i][1]);
174-
}
175-
for (unsigned int i = 0; i < nFaces; i++)
176-
{
177-
int posIndices[3];
178-
int texIndices[3];
179-
for (int j = 0; j < 3; j++)
180-
{
181-
posIndices[j] = faces[i].posIndices[j];
182-
if (nTexCoords > 0)
183-
{
184-
texIndices[j] = faces[i].texIndices[j];
185-
mesh.addUVIndex(texIndices[j]);
186-
}
187-
}
188-
189-
mesh.addFace(&posIndices[0]);
190-
}
191-
mesh.buildNeighbors();
192-
193-
mesh.updateNormals(vd, 0);
194-
mesh.updateVertexNormals(vd);
195-
196-
LOG_INFO << "Number of triangles: " << nFaces;
197-
LOG_INFO << "Number of vertices: " << nPoints;
198-
}
199-
200151
void buildModel ()
201152
{
202153
TimeManager::getCurrent ()->setTimeStepSize (static_cast<Real>(0.005));
@@ -208,13 +159,13 @@ void buildModel ()
208159
string fileName = FileSystem::normalizePath(base->getExePath() + "/resources/models/cube.obj");
209160
IndexedFaceMesh mesh;
210161
VertexData vd;
211-
loadObj(fileName, vd, mesh, Vector3r::Ones());
162+
DemoBase::loadMesh(fileName, vd, mesh, Vector3r::Zero(), Matrix3r::Identity(), Vector3r::Ones());
212163
mesh.setFlatShading(true);
213164

214165
string fileNameTorus = FileSystem::normalizePath(base->getExePath() + "/resources/models/torus.obj");
215166
IndexedFaceMesh meshTorus;
216167
VertexData vdTorus;
217-
loadObj(fileNameTorus, vdTorus, meshTorus, Vector3r::Ones());
168+
DemoBase::loadMesh(fileNameTorus, vdTorus, meshTorus, Vector3r::Zero(), Matrix3r::Identity(), Vector3r::Ones());
218169

219170
SimulationModel::RigidBodyVector &rb = model->getRigidBodies();
220171
rb.resize(2);

0 commit comments

Comments
 (0)