main.cpp 14.9 KB
Newer Older
1
2
3
4
#include <cstring>
#include <iostream>
#include <memory>
#include <thread>
5
#include <iomanip>
6

Alexis Girault's avatar
Alexis Girault committed
7
8
#include "imstkMath.h"
#include "imstkSimulationManager.h"
Alexis Girault's avatar
Alexis Girault committed
9
#include "imstkSceneObject.h"
Alexis Girault's avatar
Alexis Girault committed
10
#include "imstkPlane.h"
11
12
#include "imstkSphere.h"
#include "imstkCube.h"
Alexis Girault's avatar
Alexis Girault committed
13
#include "imstkLight.h"
Alexis Girault's avatar
Alexis Girault committed
14
#include "imstkCamera.h"
Sreekanth Arikatla's avatar
Sreekanth Arikatla committed
15
16
17
18

// Geometry
#include "imstkTetrahedralMesh.h"
#include "imstkSurfaceMesh.h"
19
#include "imstkMeshReader.h"
Sreekanth Arikatla's avatar
Sreekanth Arikatla committed
20

21
// Maps
Sreekanth Arikatla's avatar
Sreekanth Arikatla committed
22
#include "imstkTetraTriangleMap.h"
23
#include "imstkIsometricMap.h"
24
#include "imstkOneToOneMap.h"
25

26
27
#include "g3log/g3log.hpp"

28
void testViewer();
29
void testReadMesh();
30
31
void testAnalyticalGeometry();
void testScenesManagement();
32
void testIsometricMap();
Sreekanth Arikatla's avatar
Sreekanth Arikatla committed
33
void testTetraTriangleMap();
34
35
void testOneToOneNodalMap();
void testExtractSurfaceMesh();
36
void testSurfaceMeshOptimizer();
37

38
39
int main()
{
40
41
42
    std::cout << "****************\n"
              << "Starting Sandbox\n"
              << "****************\n";
43

Sreekanth Arikatla's avatar
Sreekanth Arikatla committed
44
    //testViewer();
Alexis Girault's avatar
Alexis Girault committed
45
    testReadMesh();
46
47
    //testAnalyticalGeometry();
    //testScenesManagement();
48
    //testIsometricMap();
49
    //testTetraTriangleMap();
Alexis Girault's avatar
Alexis Girault committed
50
    //testExtractSurfaceMesh();
51
52
    //testOneToOneNodalMap();
    //testSurfaceMeshOptimizer();
53

54
55
56
    return 0;
}

57
58
59
60
61
62
63
void testReadMesh()
{
    // SDK and Scene
    auto sdk = std::make_shared<imstk::SimulationManager>();
    auto scene = sdk->createNewScene("SceneTestMesh");
    scene->setLoopDelay(1000);

64
65
66
    // Read surface mesh
    /*
    auto objMesh = imstk::MeshReader::read("/home/virtualfls/Projects/IMSTK/resources/asianDragon/asianDragon.obj");
Alexis Girault's avatar
Alexis Girault committed
67
68
69
70
    auto plyMesh = imstk::MeshReader::read("/home/virtualfls/Projects/IMSTK/resources/Cube/models/cube.ply");
    auto stlMesh = imstk::MeshReader::read("/home/virtualfls/Projects/IMSTK/resources/Cube/models/cube.stl");
    auto vtkMesh = imstk::MeshReader::read("/home/virtualfls/Projects/IMSTK/resources/Cube/models/cube.vtk");
    auto vtpMesh = imstk::MeshReader::read("/home/virtualfls/Projects/IMSTK/resources/Cube/models/cube.vtp");
71
    */
72

73
74
75
    // Read volumetricMesh
    auto mesh = imstk::MeshReader::read("/home/virtualfls/Projects/IMSTK/resources/AVM/nidus-model/nidus10KTet.vtk");
    auto volumeMesh = std::dynamic_pointer_cast<imstk::VolumetricMesh>(mesh);
76

77
    volumeMesh->computeAttachedSurfaceMesh();
78
79
80
    auto surfaceMesh = volumeMesh->getAttachedSurfaceMesh();

    // Create object and add to scene
Alexis Girault's avatar
Alexis Girault committed
81
    auto object = std::make_shared<imstk::VisualObject>("meshObject");
82
    object->setVisualGeometry(surfaceMesh);
Alexis Girault's avatar
Alexis Girault committed
83
    scene->addSceneObject(object);
84
85
86
87
88
89

    // Run
    sdk->setCurrentScene("SceneTestMesh");
    sdk->startSimulation(true);
}

90
91
92
void testViewer()
{
    // SDK and Scene
93
    auto sdk = std::make_shared<imstk::SimulationManager>();
94
95
    auto sceneTest = sdk->createNewScene("SceneTest");
    sceneTest->setLoopDelay(1000);
96

97
    // Plane
98
    auto planeGeom = std::make_shared<imstk::Plane>();
Alexis Girault's avatar
Alexis Girault committed
99
    planeGeom->scale(10);
100
101
102
    auto planeObj = std::make_shared<imstk::VisualObject>("VisualPlane");
    planeObj->setVisualGeometry(planeGeom);

103
    // Cube
104
105
    auto cubeGeom = std::make_shared<imstk::Cube>();
    cubeGeom->scale(0.5);
106
107
    cubeGeom->rotate(imstk::UP_VECTOR, imstk::PI_4);
    cubeGeom->rotate(imstk::RIGHT_VECTOR, imstk::PI_4);
Alexis Girault's avatar
Alexis Girault committed
108
    cubeGeom->translate(1.0, - 1.0, 0.5);
109
110
111
    auto cubeObj = std::make_shared<imstk::VisualObject>("VisualCube");
    cubeObj->setVisualGeometry(cubeGeom);

112
    // Sphere
113
114
    auto sphereGeom = std::make_shared<imstk::Sphere>();
    sphereGeom->scale(0.3);
Alexis Girault's avatar
Alexis Girault committed
115
    sphereGeom->translate(0, 2, 0);
116
117
    auto sphereObj = std::make_shared<imstk::VisualObject>("VisualSphere");
    sphereObj->setVisualGeometry(sphereGeom);
118

119
    // Light (white)
Alexis Girault's avatar
Alexis Girault committed
120
    auto whiteLight = std::make_shared<imstk::Light>("whiteLight");
Alexis Girault's avatar
Alexis Girault committed
121
    whiteLight->setPosition(imstk::Vec3d(5, 8, 5));
Alexis Girault's avatar
Alexis Girault committed
122
123
    whiteLight->setPositional();

124
    // Light (red)
Alexis Girault's avatar
Alexis Girault committed
125
    auto colorLight = std::make_shared<imstk::Light>("colorLight");
Alexis Girault's avatar
Alexis Girault committed
126
127
128
    colorLight->setPosition(imstk::Vec3d(4, -3, 1));
    colorLight->setFocalPoint(imstk::Vec3d(0, 0, 0));
    colorLight->setColor(imstk::Color::Red);
Alexis Girault's avatar
Alexis Girault committed
129
130
    colorLight->setPositional();
    colorLight->setSpotAngle(15);
Alexis Girault's avatar
Alexis Girault committed
131

132
    // Add in scene
133
134
135
    sceneTest->addSceneObject(planeObj);
    sceneTest->addSceneObject(cubeObj);
    sceneTest->addSceneObject(sphereObj);
Alexis Girault's avatar
Alexis Girault committed
136
137
    sceneTest->addLight(whiteLight);
    sceneTest->addLight(colorLight);
138

139
    // Update Camera
Alexis Girault's avatar
Alexis Girault committed
140
141
142
143
    auto cam1 = sceneTest->getCamera();
    cam1->setPosition(imstk::Vec3d(-5.5, 2.5, 32));
    cam1->setFocalPoint(imstk::Vec3d(1, 1, 0));

144
145
146
147
    // Run
    sdk->setCurrentScene("SceneTest");
    sdk->startSimulation(true);
}
Alexis Girault's avatar
Alexis Girault committed
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
void testAnalyticalGeometry()
{
    auto sdk = std::make_shared<imstk::SimulationManager>();

    // Plane
    LOG(INFO) << "-- Plane : Init";
    auto pos   = imstk::Vec3d(5, 2, 5);
    auto norm  = imstk::Vec3d(0, 1, 1);
    auto width = 10;
    LOG(INFO) << "p = " << pos;
    LOG(INFO) << "n = " << norm;
    LOG(INFO) << "w = " << width;

    LOG(INFO) << "-- Plane : Create";
    auto plane = std::make_shared<imstk::Plane>(pos, norm, width);
    LOG(INFO) << "p = " << plane->getPosition();
    LOG(INFO) << "n = " << plane->getNormal();
    LOG(INFO) << "w = " << plane->getWidth();

    LOG(INFO) << "-- Plane : Set Position";
    plane->setPosition(imstk::Vec3d(1, 1, 1));
    LOG(INFO) << "p = " << plane->getPosition();

    LOG(INFO) << "-- Plane : Translate";
    plane->translate(imstk::Vec3d(2, 1, -3));
    LOG(INFO) << "p = " << plane->getPosition();

    LOG(INFO) << "-- Plane : Set Normal";
    plane->setNormal(imstk::FORWARD_VECTOR);
    LOG(INFO) << "n = " << plane->getNormal();

    LOG(INFO) << "-- Plane : Rotate";
    plane->rotate(imstk::UP_VECTOR, imstk::PI_2);
    LOG(INFO) << "n = " << plane->getNormal();
183
}
184

185
void testScenesManagement()
186
{
187
    // THIS TESTS NEEDS TO DISABLE STANDALONE VIEWER RENDERING
188
189
190

    auto sdk = std::make_shared<imstk::SimulationManager>();

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
    // Scenes
    LOG(INFO) << "-- Test add scenes";
    auto scene1 = std::make_shared<imstk::Scene>("scene1");
    scene1->setLoopDelay(500);
    sdk->addScene(scene1);

    sdk->createNewScene("scene2");
    auto scene2 = sdk->getScene("scene2");
    scene2->setLoopDelay(500);

    auto scene3 = sdk->createNewScene();
    sdk->removeScene("Scene_3");

    // switch
    LOG(INFO) << "-- Test scene switch";
    int delay = 5;
    sdk->setCurrentScene("scene1");
    sdk->startSimulation();
    std::this_thread::sleep_for(std::chrono::seconds(delay));
    sdk->setCurrentScene("scene2", false);
    std::this_thread::sleep_for(std::chrono::seconds(delay));
    sdk->setCurrentScene("scene1", true);
    std::this_thread::sleep_for(std::chrono::seconds(delay));
    sdk->endSimulation();

    // pause/run
    LOG(INFO) << "-- Test simulation pause/run";
    sdk->setCurrentScene("scene2");
    sdk->startSimulation();
    std::this_thread::sleep_for(std::chrono::seconds(delay));
    sdk->pauseSimulation();
    std::this_thread::sleep_for(std::chrono::seconds(delay));
    sdk->runSimulation();
    std::this_thread::sleep_for(std::chrono::seconds(delay));
    sdk->pauseSimulation();
    std::this_thread::sleep_for(std::chrono::seconds(delay));
    sdk->endSimulation();

    // Quit
    while (sdk->getStatus() != imstk::SimulationStatus::INACTIVE) {}
}

233
void testIsometricMap()
234
235
236
237
238
239
240
{
    // SDK and Scene
    auto sdk = std::make_shared<imstk::SimulationManager>();
    auto geometryMapTest = sdk->createNewScene("geometryMapTest");
    geometryMapTest->setLoopDelay(1000);

    // Cube
241
242
243
244
245
    auto cubeGeom = std::make_shared<imstk::Cube>();
    cubeGeom->scale(0.5);
    auto cubeObj = std::make_shared<imstk::VisualObject>("VisualCube");
    cubeObj->setVisualGeometry(cubeGeom);

246
    // Sphere
247
248
249
250
251
    auto sphereGeom = std::make_shared<imstk::Sphere>();
    sphereGeom->scale(0.3);
    auto sphereObj = std::make_shared<imstk::VisualObject>("VisualSphere");
    sphereObj->setVisualGeometry(sphereGeom);

252
253
254
255
    // Add objects in Scene
    geometryMapTest->addSceneObject(cubeObj);
    geometryMapTest->addSceneObject(sphereObj);

256
    // Isometric Map
257
258
259
    auto transform = imstk::RigidTransform3d::Identity();
    transform.translate(imstk::Vec3d(0.0, 1.0, 0.0));
    transform.rotate(Eigen::AngleAxisd(imstk::PI_4, imstk::Vec3d(0, 1.0, 0)));
Sreekanth Arikatla's avatar
Sreekanth Arikatla committed
260

261
262
263
    auto rigidMap = std::make_shared<imstk::IsometricMap>();
    rigidMap->setMaster(sphereObj->getVisualGeometry());
    rigidMap->setSlave(cubeObj->getVisualGeometry());
264
    rigidMap->setTransform(transform);
265

266
267
    // Test map
    LOG(INFO) << cubeGeom->getPosition();
268

269
    rigidMap->apply();
270
    LOG(INFO) << cubeGeom->getPosition();
271

272
    sphereGeom->setPosition(1.0, 0.0, 1.0);
273
    rigidMap->apply();
274
    LOG(INFO) << cubeGeom->getPosition();
275

276
277
278
    // Start simulation
    sdk->setCurrentScene("geometryMapTest");
    sdk->startSimulation(imstk::Renderer::Mode::DEBUG);
Alexis Girault's avatar
Alexis Girault committed
279
}
Sreekanth Arikatla's avatar
Sreekanth Arikatla committed
280
281
282

void testTetraTriangleMap()
{
283
284
    auto sdk = std::make_shared<imstk::SimulationManager>();

Sreekanth Arikatla's avatar
Sreekanth Arikatla committed
285
286
287
288
289
290
291
    // Tetrahedral mesh
    auto tetMesh = std::make_shared<imstk::TetrahedralMesh>();
    std::vector<imstk::Vec3d> vertList;
    vertList.push_back(imstk::Vec3d(0, 0, 0));
    vertList.push_back(imstk::Vec3d(1.0, 0, 0));
    vertList.push_back(imstk::Vec3d(0, 1.0, 0));
    vertList.push_back(imstk::Vec3d(0, 0, 1.0));
292
293
    tetMesh->setInitialVerticesPositions(vertList);
    tetMesh->setVerticesPositions(vertList);
Sreekanth Arikatla's avatar
Sreekanth Arikatla committed
294

295
296
    std::vector<imstk::TetrahedralMesh::TetraArray> tetConnectivity;
    imstk::TetrahedralMesh::TetraArray tet1 = { 0, 1, 2, 3 };
Sreekanth Arikatla's avatar
Sreekanth Arikatla committed
297
    tetConnectivity.push_back(tet1);
298
    tetMesh->setTetrahedraVertices(tetConnectivity);
Sreekanth Arikatla's avatar
Sreekanth Arikatla committed
299
300
301
302
303
304
305

    // Triangular mesh
    auto triMesh = std::make_shared<imstk::SurfaceMesh>();
    std::vector<imstk::Vec3d> SurfVertList;
    SurfVertList.push_back(imstk::Vec3d(0, 0, 1));// coincides with one vertex
    SurfVertList.push_back(imstk::Vec3d(0.25, 0.25, 0.25));// centroid
    SurfVertList.push_back(imstk::Vec3d(1.05, 0, 0));
306
307
    triMesh->setInitialVerticesPositions(SurfVertList);
    triMesh->setVerticesPositions(SurfVertList);
Sreekanth Arikatla's avatar
Sreekanth Arikatla committed
308
309
310
311
312

    // Construct a map
    auto tetTriMap = std::make_shared<imstk::TetraTriangleMap>();
    tetTriMap->setMaster(tetMesh);
    tetTriMap->setSlave(triMesh);
313
    tetTriMap->compute();
Sreekanth Arikatla's avatar
Sreekanth Arikatla committed
314

315
    tetTriMap->print();
Sreekanth Arikatla's avatar
Sreekanth Arikatla committed
316
317

    getchar();
318
}
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344

void testExtractSurfaceMesh()
{
    auto sdk = std::make_shared<imstk::SimulationManager>();

    // a. Construct a sample tetrahedral mesh

    // a.1 add vertex positions
    auto tetMesh = std::make_shared<imstk::TetrahedralMesh>();
    std::vector<imstk::Vec3d> vertList;
    vertList.push_back(imstk::Vec3d(0, 0, 0));
    vertList.push_back(imstk::Vec3d(1.0, 0, 0));
    vertList.push_back(imstk::Vec3d(0, 1.0, 0));
    vertList.push_back(imstk::Vec3d(0, 0, 1.0));
    vertList.push_back(imstk::Vec3d(1.0, 1.0, 1.0));
    tetMesh->setInitialVerticesPositions(vertList);
    tetMesh->setVerticesPositions(vertList);

    // a.2 add connectivity
    std::vector<imstk::TetrahedralMesh::TetraArray> tetConnectivity;
    imstk::TetrahedralMesh::TetraArray tet1 = { 0, 1, 2, 3 };
    imstk::TetrahedralMesh::TetraArray tet2 = { 1, 2, 3, 4 };
    tetConnectivity.push_back(tet1);
    tetConnectivity.push_back(tet2);
    tetMesh->setTetrahedraVertices(tetConnectivity);

345
346
347
348
349
    // b. Print tetrahedral mesh
    tetMesh->print();

    // c. Extract the surface mesh
    tetMesh->computeAttachedSurfaceMesh();
350

351
352
    // d. Print the resulting mesh
    tetMesh->getAttachedSurfaceMesh()->print();
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373

    getchar();
}

void testOneToOneNodalMap()
{
    auto sdk = std::make_shared<imstk::SimulationManager>();

    // a. Construct a sample tetrahedral mesh

    // a.1 add vertex positions
    auto tetMesh = std::make_shared<imstk::TetrahedralMesh>();
    std::vector<imstk::Vec3d> vertList;
    vertList.push_back(imstk::Vec3d(0, 0, 0));
    vertList.push_back(imstk::Vec3d(1.0, 0, 0));
    vertList.push_back(imstk::Vec3d(0, 1.0, 0));
    vertList.push_back(imstk::Vec3d(0, 0, 1.0));
    vertList.push_back(imstk::Vec3d(1.0, 1.0, 1.0));
    tetMesh->setInitialVerticesPositions(vertList);
    tetMesh->setVerticesPositions(vertList);

374
375
    tetMesh->print();

376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
    // b. Construct a surface mesh
    auto triMesh = std::make_shared<imstk::SurfaceMesh>();

    // b.1 Add vertex positions
    std::vector<imstk::Vec3d> SurfVertList;
    SurfVertList.push_back(imstk::Vec3d(0, 0, 0));
    SurfVertList.push_back(imstk::Vec3d(1.0, 0, 0));
    SurfVertList.push_back(imstk::Vec3d(0, 1.0, 0));
    SurfVertList.push_back(imstk::Vec3d(0, 0, 1.0));
    SurfVertList.push_back(imstk::Vec3d(1.0, 1.0, 1.0));
    triMesh->setInitialVerticesPositions(SurfVertList);
    triMesh->setVerticesPositions(SurfVertList);

    // b.2 Add vertex connectivity
    std::vector<imstk::SurfaceMesh::TriangleArray> triConnectivity;
    triConnectivity.push_back({ { 0, 1, 2 } });
    triConnectivity.push_back({ { 0, 1, 3 } });
    triConnectivity.push_back({ { 0, 2, 3 } });
    triConnectivity.push_back({ { 1, 2, 4 } });
    triConnectivity.push_back({ { 1, 3, 4 } });
    triConnectivity.push_back({ { 2, 3, 4 } });
    triMesh->setTrianglesVertices(triConnectivity);

399
400
    triMesh->print();

401
402
403
404
405
406
407
408
409
410
411
412
413
414
    // c. Construct the one to one nodal map based on the above meshes
    auto oneToOneNodalMap = std::make_shared<imstk::OneToOneMap>();
    oneToOneNodalMap->setMaster(tetMesh);
    oneToOneNodalMap->setSlave(triMesh);

    // d. Compute the map
    oneToOneNodalMap->compute();

    // e. Print the computed nodal map if valid
    if (oneToOneNodalMap->isValid())
    {
        oneToOneNodalMap->print();
    }

415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
    getchar();
}

void testSurfaceMeshOptimizer()
{
    auto sdk = std::make_shared<imstk::SimulationManager>();

    // a. Construct a sample triangular mesh

    // b. Add nodal data
    auto surfMesh = std::make_shared<imstk::SurfaceMesh>();
    std::vector<imstk::Vec3d> vertList;
    vertList.push_back(imstk::Vec3d(0, 0, 0));
    vertList.push_back(imstk::Vec3d(0.5, 0.5, 0));
    vertList.push_back(imstk::Vec3d(1, 1, 0));
    vertList.push_back(imstk::Vec3d(1, 0, 0));
    vertList.push_back(imstk::Vec3d(0, 1, 0));
    vertList.push_back(imstk::Vec3d(0.5, 1, 0));
    vertList.push_back(imstk::Vec3d(0, 0.5, 0));
    vertList.push_back(imstk::Vec3d(1, 0.5, 0));
    vertList.push_back(imstk::Vec3d(0.5, 0, 0));
    surfMesh->setInitialVerticesPositions(vertList);
    surfMesh->setVerticesPositions(vertList);

    // c. Add connectivity data
    std::vector<imstk::SurfaceMesh::TriangleArray> triangles;
    imstk::SurfaceMesh::TriangleArray tri[8];
    tri[0] = { { 0, 8, 6 } };
    tri[1] = { { 7, 2, 5 } };
    tri[2] = { { 1, 5, 4 } };
    tri[3] = { { 3, 7, 1 } };
    tri[4] = { { 8, 1, 6 } };
    tri[5] = { { 1, 4, 6 } };
    tri[6] = { { 1, 7, 5 } };
    tri[7] = { { 3, 1, 8 } };

    for (int i = 0; i < 8; i++)
    {
        triangles.push_back(tri[i]);
    }

    surfMesh->setTrianglesVertices(triangles);

    // d. Print the mesh
    surfMesh->print();

    // e. Rewire the mesh position and connectivity
    surfMesh->optimizeForDataLocality();

    // f. Print the resulting mesh
    surfMesh->print();

    // Cross-check
    // Connectivity: 0:(0, 1, 2), 1:(1, 3, 2), 2:(3, 4, 2), 3:(5, 3, 1), 4:(3, 6, 4), 5:(5, 7, 3), 6:(3, 7, 6), 7:(7, 8, 6)
    // Nodal data: 0:(0, 0, 0), 1:(0.5, 0, 0), 2:(0, 0.5, 0), 3:(0.5, 0.5, 0), 4:(0, 1, 0), 5:(1, 0, 0), 6:(0.5, 1, 0), 7:(1, 0.5, 0), 8:(1, 1, 0)

471
    getchar();
Alexis Girault's avatar
Alexis Girault committed
472
}