PBDCollisionExample.cpp 12.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*=========================================================================

   Library: iMSTK

   Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
   & Imaging in Medicine, Rensselaer Polytechnic Institute.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

12
	  http://www.apache.org/licenses/LICENSE-2.0.txt
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

=========================================================================*/

#include "imstkSimulationManager.h"
#include "imstkPbdObject.h"
#include "imstkPbdSolver.h"
#include "imstkTetrahedralMesh.h"
#include "imstkMeshIO.h"
#include "imstkOneToOneMap.h"
28
29
#include "imstkMeshToMeshBruteForceCD.h"
#include "imstkPBDCollisionHandling.h"
30
31
32

using namespace imstk;

33
34
35
36
///
/// \brief This example demonstrates the collision interaction
/// using Position based dynamics
///
37
int main()
38
{
39
40
41
42
43
44
	auto sdk = std::make_shared<SimulationManager>();
	auto scene = sdk->createNewScene("PbdCollision");

	scene->getCamera()->setPosition(0, 10.0, 10.0);

	// dragon
45
	auto tetMesh = MeshIO::read(iMSTK_DATA_ROOT "/asianDragon/asianDragon.veg");
46
47
48
49
50
	if (!tetMesh)
	{
		LOG(WARNING) << "Could not read mesh from file.";
		return 1;
	}
51
52
	//tetMesh->scale(5., Geometry::TransformType::ApplyToData);
	//tetMesh->translate(Vec3d(0., -5., -5.) , Geometry::TransformType::ApplyToData);
53
54
55
56
57
58
59
60
61
62
63
64
65
66


	auto surfMesh = std::make_shared<SurfaceMesh>();
	auto surfMeshVisual = std::make_shared<SurfaceMesh>();
	auto volTetMesh = std::dynamic_pointer_cast<TetrahedralMesh>(tetMesh);
	if (!volTetMesh)
	{
		LOG(WARNING) << "Dynamic pointer cast from PointSet to TetrahedralMesh failed!";
		return 1;
	}
	volTetMesh->extractSurfaceMesh(surfMesh, true);

	auto material = std::make_shared<RenderMaterial>();
	material->setDisplayMode(RenderMaterial::DisplayMode::WIREFRAME_SURFACE);
67
68
	auto surfMeshVisualModel = std::make_shared<VisualModel>(surfMesh);
	surfMeshVisualModel->setRenderMaterial(material);
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

	auto deformMapP2V = std::make_shared<OneToOneMap>();
	deformMapP2V->setMaster(tetMesh);
	deformMapP2V->setSlave(surfMesh);
	deformMapP2V->compute();

	auto deformMapC2V = std::make_shared<OneToOneMap>();
	deformMapC2V->setMaster(surfMesh);
	deformMapC2V->setSlave(surfMesh);
	deformMapC2V->compute();

	auto deformMapP2C = std::make_shared<OneToOneMap>();
	deformMapP2C->setMaster(tetMesh);
	deformMapP2C->setSlave(surfMesh);
	deformMapP2C->compute();

	auto deformableObj = std::make_shared<PbdObject>("Dragon");
86
87
	//deformableObj->setVisualGeometry(surfMesh);
	deformableObj->addVisualModel(surfMeshVisualModel);
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
	deformableObj->setCollidingGeometry(surfMesh);
	deformableObj->setPhysicsGeometry(volTetMesh);
	deformableObj->setPhysicsToCollidingMap(deformMapP2C);
	deformableObj->setPhysicsToVisualMap(deformMapP2V);
	deformableObj->setCollidingToVisualMap(deformMapC2V);

	auto pbdModel = std::make_shared<PbdModel>();
	pbdModel->setModelGeometry(volTetMesh);
	pbdModel->configure(/*Number of Constraints*/ 1,
		/*Constraint configuration*/ "FEM NeoHookean 1.0 0.3",
		/*Mass*/ 1.0,
		/*Gravity*/ "0 -9.8 0",
		/*TimeStep*/ 0.001,
		/*FixedPoint*/ "",
		/*NumberOfIterationInConstraintSolver*/ 2,
		/*Proximity*/ 0.1,
		/*Contact stiffness*/ 0.01);
	deformableObj->setDynamicalModel(pbdModel);

	auto pbdSolver = std::make_shared<PbdSolver>();
	pbdSolver->setPbdObject(deformableObj);
	scene->addNonlinearSolver(pbdSolver);

	scene->addSceneObject(deformableObj);

	bool clothTest = 0;
	bool volumetric = !clothTest;
	if (clothTest)
	{
		auto clothMesh = std::make_shared<SurfaceMesh>();
		StdVectorOfVec3d vertList;
		double width = 60.0;
		double height = 60.0;
		int nRows = 10;
		int nCols = 10;
		int corner[4] = { 1, nRows, nRows*nCols - nCols + 1, nRows*nCols };
		char intStr[33];
		std::string fixed_corner;
		for (unsigned int i = 0; i < 4; ++i)
		{
			std::sprintf(intStr, "%d", corner[i]);
			fixed_corner += std::string(intStr) + ' ';
		}
		vertList.resize(nRows*nCols);
		const double dy = width / (double)(nCols - 1);
		const double dx = height / (double)(nRows - 1);
		for (int i = 0; i < nRows; ++i)
		{
			for (int j = 0; j < nCols; j++)
			{
				const double y = (double)dy*j;
				const double x = (double)dx*i;
				vertList[i*nCols + j] = Vec3d(x - 30, -10, y - 30);
			}
		}
		clothMesh->setInitialVertexPositions(vertList);
		clothMesh->setVertexPositions(vertList);

		// c. Add connectivity data
		std::vector<SurfaceMesh::TriangleArray> triangles;
		for (std::size_t i = 0; i < nRows - 1; ++i)
		{
			for (std::size_t j = 0; j < nCols - 1; j++)
			{
				SurfaceMesh::TriangleArray tri[2];
				tri[0] = { { i*nCols + j, i*nCols + j + 1, (i + 1)*nCols + j } };
				tri[1] = { { (i + 1)*nCols + j + 1, (i + 1)*nCols + j, i*nCols + j + 1 } };
				triangles.push_back(tri[0]);
				triangles.push_back(tri[1]);
			}
		}
		clothMesh->setTrianglesVertices(triangles);

		auto oneToOneFloor = std::make_shared<OneToOneMap>();
		oneToOneFloor->setMaster(clothMesh);
		oneToOneFloor->setSlave(clothMesh);
		oneToOneFloor->compute();

		auto floor = std::make_shared<PbdObject>("Floor");
		floor->setCollidingGeometry(clothMesh);
		floor->setVisualGeometry(clothMesh);
		floor->setPhysicsGeometry(clothMesh);
		floor->setPhysicsToCollidingMap(oneToOneFloor);
		floor->setPhysicsToVisualMap(oneToOneFloor);
		//floor->setCollidingToVisualMap(oneToOneFloor);
		//floor->initialize(/*Number of constraints*/ 2,
		//                  /*Constraint configuration*/ "Distance 0.1",
		//                  /*Constraint configuration*/ "Dihedral 0.001",
		//                  /*Mass*/ 0.1,
		//                  /*Gravity*/ "0 9.8 0",
		//                  /*TimeStep*/ 0.002,
		//                  /*FixedPoint*/ fixed_corner.c_str(),
		//                  /*NumberOfIterationInConstraintSolver*/ 5,
		//                  /*Proximity*/ 0.1,
		//                  /*Contact stiffness*/ 0.95);
		scene->addSceneObject(floor);

		std::cout << "nbr of vertices in cloth mesh" << clothMesh->getNumVertices() << std::endl;

		// Collisions
		auto clothTestcolGraph = scene->getCollisionGraph();
		auto pair1 = std::make_shared<PbdInteractionPair>(PbdInteractionPair(deformableObj, floor));
		pair1->setNumberOfInterations(5);

		clothTestcolGraph->addInteractionPair(pair1);

		scene->getCamera()->setPosition(0, 0, 50);
	}
	else if (0)
	{
		auto tetMesh1 = MeshIO::read(iMSTK_DATA_ROOT "/asianDragon/asianDragon.veg");
		if (!tetMesh1)
		{
			LOG(WARNING) << "Could not read mesh from file.";
			return 1;
		}

		auto surfMesh1 = std::make_shared<SurfaceMesh>();
		auto surfMeshVisual1 = std::make_shared<SurfaceMesh>();
		auto volTetMesh1 = std::dynamic_pointer_cast<TetrahedralMesh>(tetMesh1);
		if (!volTetMesh1)
		{
			LOG(WARNING) << "Dynamic pointer cast from PointSet to TetrahedralMesh failed!";
			return 1;
		}

		auto vs = volTetMesh1->getInitialVertexPositions();
		Vec3d tmpPos;
		for (int i = 0; i < volTetMesh1->getNumVertices(); ++i)
		{
			tmpPos = volTetMesh1->getVertexPosition(i);
			tmpPos[1] -= 6;
			volTetMesh1->setVertexPosition(i, tmpPos);
		}
		volTetMesh1->setInitialVertexPositions(volTetMesh1->getVertexPositions());

		volTetMesh1->extractSurfaceMesh(surfMesh1);
		volTetMesh1->extractSurfaceMesh(surfMeshVisual1);


		auto deformMapP2V1 = std::make_shared<OneToOneMap>();
		deformMapP2V1->setMaster(volTetMesh1);
		deformMapP2V1->setSlave(surfMeshVisual1);
		deformMapP2V1->compute();

		auto deformMapC2V1 = std::make_shared<OneToOneMap>();
		deformMapC2V1->setMaster(surfMesh1);
		deformMapC2V1->setSlave(surfMeshVisual1);
		deformMapC2V1->compute();

		auto deformMapP2C1 = std::make_shared<OneToOneMap>();
		deformMapP2C1->setMaster(volTetMesh1);
		deformMapP2C1->setSlave(surfMesh1);
		deformMapP2C1->compute();

		auto deformableObj1 = std::make_shared<PbdObject>("Dragon2");
		deformableObj1->setVisualGeometry(surfMeshVisual1);
		deformableObj1->setCollidingGeometry(surfMesh1);
		deformableObj1->setPhysicsGeometry(volTetMesh1);
		deformableObj1->setPhysicsToCollidingMap(deformMapP2C1);
		deformableObj1->setPhysicsToVisualMap(deformMapP2V1);
		deformableObj1->setCollidingToVisualMap(deformMapC2V1);
		//deformableObj1->initialize(/*Number of Constraints*/ 1,
		//                           /*Constraint configuration*/ "FEM NeoHookean 10.0 0.5",
		//                           /*Mass*/ 0.0,
		//                           /*Gravity*/ "0 -9.8 0",
		//                           /*TimeStep*/ 0.002,
		//                           /*FixedPoint*/ "",
		//                           /*NumberOfIterationInConstraintSolver*/ 2,
		//                           /*Proximity*/ 0.1,
		//                           /*Contact stiffness*/ 0.01);

		scene->addSceneObject(deformableObj1);

		// Collisions
		auto colGraph = scene->getCollisionGraph();
		auto pair = std::make_shared<PbdInteractionPair>(PbdInteractionPair(deformableObj, deformableObj1));
		pair->setNumberOfInterations(2);

		colGraph->addInteractionPair(pair);
	}
	else
	{
		// Build floor geometry
		StdVectorOfVec3d vertList;
		const double width = 100.0;
		const double height = 100.0;
		const int nRows = 2;
		const int nCols = 2;
		vertList.resize(nRows*nCols);
		const double dy = width / (double)(nCols - 1);
		const double dx = height / (double)(nRows - 1);
		for (int i = 0; i < nRows; ++i)
		{
			for (int j = 0; j < nCols; j++)
			{
				const double y = (double)dy*j;
				const double x = (double)dx*i;
				vertList[i*nCols + j] = Vec3d(x - 50, -10.0, y - 50);
			}
		}

		// c. Add connectivity data
		std::vector<SurfaceMesh::TriangleArray> triangles;
		for (std::size_t i = 0; i < nRows - 1; ++i)
		{
			for (std::size_t j = 0; j < nCols - 1; j++)
			{
				SurfaceMesh::TriangleArray tri[2];
				tri[0] = { { i*nCols + j, i*nCols + j + 1, (i + 1)*nCols + j } };
				tri[1] = { { (i + 1)*nCols + j + 1, (i + 1)*nCols + j, i*nCols + j + 1 } };
				triangles.push_back(tri[0]);
				triangles.push_back(tri[1]);
			}
		}
		auto floorMesh = std::make_shared<SurfaceMesh>();
		floorMesh->initialize(vertList, triangles);

		auto materialFloor = std::make_shared<RenderMaterial>();
		materialFloor->setDisplayMode(RenderMaterial::DisplayMode::WIREFRAME_SURFACE);
308
309
		auto floorMeshVisual = std::make_shared<VisualModel>(floorMesh);
		floorMeshVisual->setRenderMaterial(materialFloor);
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328

		auto floorMapP2V = std::make_shared<OneToOneMap>();
		floorMapP2V->setMaster(floorMesh);
		floorMapP2V->setSlave(floorMesh);
		floorMapP2V->compute();

		auto floorMapP2C = std::make_shared<OneToOneMap>();
		floorMapP2C->setMaster(floorMesh);
		floorMapP2C->setSlave(floorMesh);
		floorMapP2C->compute();

		auto floorMapC2V = std::make_shared<OneToOneMap>();
		floorMapC2V->setMaster(floorMesh);
		floorMapC2V->setSlave(floorMesh);
		floorMapC2V->compute();

		auto floor = std::make_shared<PbdObject>("Floor");
		floor->setCollidingGeometry(floorMesh);
		floor->setVisualGeometry(floorMesh);
329
		floor->addVisualModel(floorMeshVisual);
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
		floor->setPhysicsGeometry(floorMesh);
		floor->setPhysicsToCollidingMap(floorMapP2C);
		floor->setPhysicsToVisualMap(floorMapP2V);
		floor->setCollidingToVisualMap(floorMapC2V);

		auto pbdModel2 = std::make_shared<PbdModel>();
		pbdModel2->setModelGeometry(floorMesh);
		pbdModel2->configure(/*Number of Constraints*/ 0,
			/*Mass*/ 0.0,
			/*Proximity*/ 0.1,
			/*Contact stiffness*/ 1.0);
		floor->setDynamicalModel(pbdModel2);

		auto pbdSolverfloor = std::make_shared<PbdSolver>();
		pbdSolverfloor->setPbdObject(floor);
		scene->addNonlinearSolver(pbdSolverfloor);

		scene->addSceneObject(floor);

		auto colData = std::make_shared<CollisionData>();
350
		auto CD = std::make_shared<MeshToMeshBruteForceCD>(surfMesh, floorMesh, colData);
351
352

		auto CH = std::make_shared<PBDCollisionHandling>(CollisionHandling::Side::A,
353
			CD->getCollisionData(), deformableObj, floor, pbdSolver);
354

355
		scene->getCollisionGraph()->addInteractionPair(deformableObj, floor, CD, CH, nullptr);
356
357
358
359
360
361
362
363
364
365
366
367
	}

	// Light
	auto light = std::make_shared<DirectionalLight>("light");
	light->setFocalPoint(Vec3d(5, -8, -5));
	light->setIntensity(1);
	scene->addLight(light);

	sdk->setActiveScene(scene);
	sdk->startSimulation(SimulationStatus::RUNNING);

	return 0;
368
}