-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathClothCollisionDemo.cpp
More file actions
217 lines (173 loc) · 6.54 KB
/
ClothCollisionDemo.cpp
File metadata and controls
217 lines (173 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "Common/Common.h"
#include "Demos/Visualization/MiniGL.h"
#include "Demos/Visualization/Selection.h"
#include "Simulation/TimeManager.h"
#include <Eigen/Dense>
#include "Simulation/SimulationModel.h"
#include "Simulation/TimeStepController.h"
#include <iostream>
#include "Demos/Visualization/Visualization.h"
#include "Simulation/DistanceFieldCollisionDetection.h"
#include "Utils/Logger.h"
#include "Utils/Timing.h"
#include "Utils/FileSystem.h"
#include "Demos/Common/DemoBase.h"
#include "Simulation/Simulation.h"
// Enable memory leak detection
#if defined(_DEBUG) && !defined(EIGEN_ALIGN)
#define new DEBUG_NEW
#endif
using namespace PBD;
using namespace Eigen;
using namespace std;
using namespace Utilities;
void timeStep ();
void buildModel ();
void createMesh();
void render ();
void reset();
const int nRows = 50;
const int nCols = 50;
const Real width = 10.0;
const Real height = 10.0;
bool doPause = true;
DemoBase *base;
DistanceFieldCollisionDetection cd;
// main
int main( int argc, char **argv )
{
REPORT_MEMORY_LEAKS
base = new DemoBase();
base->init(argc, argv, "Cloth collision demo");
SimulationModel *model = new SimulationModel();
model->init();
Simulation::getCurrent()->setModel(model);
buildModel();
base->createParameterGUI();
// reset simulation when cloth simulation/bending method has changed
model->setClothSimulationMethodChangedCallback([&]() { reset(); });
model->setClothBendingMethodChangedCallback([&]() { reset(); });
// OpenGL
MiniGL::setClientIdleFunc (timeStep);
MiniGL::addKeyFunc('r', reset);
MiniGL::setClientSceneFunc(render);
MiniGL::setViewport (40.0f, 0.1f, 500.0f, Vector3r (0.0, 10.0, 25.0), Vector3r (0.0, 0.0, 0.0));
MiniGL::mainLoop();
base->cleanup();
Utilities::Timing::printAverageTimes();
Utilities::Timing::printTimeSums();
delete Simulation::getCurrent();
delete base;
delete model;
return 0;
}
void reset()
{
Utilities::Timing::printAverageTimes();
Utilities::Timing::reset();
Simulation::getCurrent()->reset();
base->getSelectedParticles().clear();
Simulation::getCurrent()->getModel()->cleanup();
cd.cleanup();
buildModel();
}
void timeStep ()
{
const Real pauseAt = base->getValue<Real>(DemoBase::PAUSE_AT);
if ((pauseAt > 0.0) && (pauseAt < TimeManager::getCurrent()->getTime()))
base->setValue(DemoBase::PAUSE, true);
if (base->getValue<bool>(DemoBase::PAUSE))
return;
// Simulation code
SimulationModel *model = Simulation::getCurrent()->getModel();
const unsigned int numSteps = base->getValue<unsigned int>(DemoBase::NUM_STEPS_PER_RENDER);
for (unsigned int i = 0; i < numSteps; i++)
{
START_TIMING("SimStep");
Simulation::getCurrent()->getTimeStep()->step(*model);
STOP_TIMING_AVG;
base->step();
}
for (unsigned int i = 0; i < model->getTriangleModels().size(); i++)
model->getTriangleModels()[i]->updateMeshNormals(model->getParticles());
}
void buildModel ()
{
TimeManager::getCurrent ()->setTimeStepSize (static_cast<Real>(0.005));
createMesh();
// create static rigid body
string fileName = FileSystem::normalizePath(base->getExePath() + "/resources/models/cube.obj");
IndexedFaceMesh mesh;
VertexData vd;
DemoBase::loadMesh(fileName, vd, mesh, Vector3r::Zero(), Matrix3r::Identity(), Vector3r::Ones());
mesh.setFlatShading(true);
string fileNameTorus = FileSystem::normalizePath(base->getExePath() + "/resources/models/torus.obj");
IndexedFaceMesh meshTorus;
VertexData vdTorus;
DemoBase::loadMesh(fileNameTorus, vdTorus, meshTorus, Vector3r::Zero(), Matrix3r::Identity(), Vector3r::Ones());
SimulationModel *model = Simulation::getCurrent()->getModel();
SimulationModel::RigidBodyVector &rb = model->getRigidBodies();
rb.resize(2);
// floor
rb[0] = new RigidBody();
rb[0]->initBody(1.0,
Vector3r(0.0, -2.5, 0.0),
Quaternionr(1.0, 0.0, 0.0, 0.0),
vd, mesh,
Vector3r(100.0, 1.0, 100.0));
rb[0]->setMass(0.0);
// torus
rb[1] = new RigidBody();
rb[1]->initBody(1.0,
Vector3r(0.0, 1.5, 0.0),
Quaternionr(1.0, 0.0, 0.0, 0.0),
vdTorus, meshTorus,
Vector3r(2.0, 2.0, 2.0));
rb[1]->setMass(0.0);
rb[1]->setFrictionCoeff(static_cast<Real>(0.1));
Simulation::getCurrent()->getTimeStep()->setCollisionDetection(*model, &cd);
cd.setTolerance(static_cast<Real>(0.05));
const std::vector<Vector3r> &vertices1 = rb[0]->getGeometry().getVertexDataLocal().getVertices();
const unsigned int nVert1 = static_cast<unsigned int>(vertices1.size());
cd.addCollisionBox(0, CollisionDetection::CollisionObject::RigidBodyCollisionObjectType, vertices1.data(), nVert1, Vector3r(100.0, 1.0, 100.0));
const std::vector<Vector3r> &vertices2 = rb[1]->getGeometry().getVertexDataLocal().getVertices();
const unsigned int nVert2 = static_cast<unsigned int>(vertices2.size());
cd.addCollisionTorus(1, CollisionDetection::CollisionObject::RigidBodyCollisionObjectType, vertices2.data(), nVert2, Vector2r(2.0, 1.0));
SimulationModel::TriangleModelVector &tm = model->getTriangleModels();
ParticleData &pd = model->getParticles();
for (unsigned int i = 0; i < tm.size(); i++)
{
const unsigned int nVert = tm[i]->getParticleMesh().numVertices();
unsigned int offset = tm[i]->getIndexOffset();
tm[i]->setFrictionCoeff(static_cast<Real>(0.1));
cd.addCollisionObjectWithoutGeometry(i, CollisionDetection::CollisionObject::TriangleModelCollisionObjectType, &pd.getPosition(offset), nVert, true);
}
}
void render ()
{
base->render();
}
/** Create a particle model mesh
*/
void createMesh()
{
SimulationModel* model = Simulation::getCurrent()->getModel();
model->addRegularTriangleModel(nCols, nRows,
Vector3r(-5, 4, -5), AngleAxisr(M_PI * 0.5, Vector3r(1, 0, 0)).matrix(), Vector2r(width, height));
// init constraints
for (unsigned int cm = 0; cm < model->getTriangleModels().size(); cm++)
{
model->setClothStiffness(1.0);
if (model->getClothSimulationMethod() == 4)
model->setClothStiffness(100000);
model->addClothConstraints(model->getTriangleModels()[cm], model->getClothSimulationMethod(), model->getClothStiffness(), model->getClothStiffnessXX(),
model->getClothStiffnessYY(), model->getClothStiffnessXY(), model->getClothPoissonRatioXY(), model->getClothPoissonRatioYX(),
model->getClothNormalizeStretch(), model->getClothNormalizeShear());
model->setClothBendingStiffness(0.01);
if (model->getClothBendingMethod() == 3)
model->setClothBendingStiffness(100.0);
model->addBendingConstraints(model->getTriangleModels()[cm], model->getClothBendingMethod(), model->getClothBendingStiffness());
}
LOG_INFO << "Number of triangles: " << model->getTriangleModels()[0]->getParticleMesh().numFaces();
LOG_INFO << "Number of vertices: " << nRows*nCols;
}