iMSTK
Interactive Medical Simulation Toolkit
DeformableBodyExample.cpp
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #include "imstkBackwardEuler.h"
8 #include "imstkCamera.h"
9 #include "imstkDirectionalLight.h"
10 #include "imstkFeDeformableObject.h"
11 #include "imstkFemDeformableBodyModel.h"
12 #include "imstkKeyboardDeviceClient.h"
13 #include "imstkKeyboardSceneControl.h"
14 #include "imstkLogger.h"
15 #include "imstkMeshIO.h"
16 #include "imstkMouseDeviceClient.h"
17 #include "imstkMouseSceneControl.h"
18 #include "imstkNew.h"
19 #include "imstkPlane.h"
20 #include "imstkPointwiseMap.h"
21 #include "imstkRenderMaterial.h"
22 #include "imstkScene.h"
23 #include "imstkSceneManager.h"
24 #include "imstkSimulationManager.h"
25 #include "imstkSimulationUtils.h"
26 #include "imstkSurfaceMesh.h"
27 #include "imstkTetrahedralMesh.h"
28 #include "imstkVisualModel.h"
29 #include "imstkVTKViewer.h"
30 
31 using namespace imstk;
32 
33 std::shared_ptr<FeDeformableObject> makeFEDeformableObject(std::shared_ptr<TetrahedralMesh> tetMesh);
34 
35 enum Geom
36 {
37  Dragon = 0,
38  Heart
39 };
40 
41 struct Input
42 {
43  std::string meshFileName;
44  std::vector<std::size_t> fixedNodeIds;
45 };
46 
47 // Geom geom = Dragon;
48 const Geom geom = Heart;
49 Input input;
50 
55 int
56 main()
57 {
58  // Setup logger (write to file and stdout)
60 
61  if (geom == Dragon)
62  {
63  input.meshFileName = iMSTK_DATA_ROOT "asianDragon/asianDragon.veg";
64  input.fixedNodeIds = { 50, 126, 177 };
65  }
66  else if (geom == Heart)
67  {
68  input.meshFileName = iMSTK_DATA_ROOT "textured_organs/heart_volume.vtk";
69  input.fixedNodeIds = { 75, 82, 84, 94, 95, 105, 110, 124, 139, 150, 161, 171, 350 };
70  }
71 
72  // Construct the scene
73  imstkNew<Scene> scene("DeformableBodyFEM");
74  {
75  std::shared_ptr<Camera> cam = scene->getActiveCamera();
76  cam->setPosition(0.0, 2.0, -25.0);
77  cam->setFocalPoint(0.0, 0.0, 0.0);
78 
79  // Load a tetrahedral mesh
80  std::shared_ptr<TetrahedralMesh> tetMesh = MeshIO::read<TetrahedralMesh>(input.meshFileName);
81  CHECK(tetMesh != nullptr) << "Could not read mesh from file.";
82 
83  // Scene object 1: fe-FeDeformableObject
84  std::shared_ptr<FeDeformableObject> deformableObj = makeFEDeformableObject(tetMesh);
85  scene->addSceneObject(deformableObj);
86 
87  // Scene object 2: Plane
88  imstkNew<Plane> planeGeom;
89  planeGeom->setWidth(40.0);
90  planeGeom->setPosition(0.0, -8.0, 0.0);
91  imstkNew<CollidingObject> planeObj("Plane");
92  planeObj->setVisualGeometry(planeGeom);
93  planeObj->setCollidingGeometry(planeGeom);
94  scene->addSceneObject(planeObj);
95 
96  // Light
98  light->setFocalPoint(Vec3d(5.0, -8.0, -5.0));
99  light->setIntensity(1);
100  scene->addLight("light", light);
101  }
102 
103  // Run the simulation
104  {
105  // Setup a viewer to render in its own thread
106  imstkNew<VTKViewer> viewer;
107  viewer->setActiveScene(scene);
108 
109  // Setup a scene manager to advance the scene in its own thread
110  imstkNew<SceneManager> sceneManager;
111  sceneManager->setActiveScene(scene);
112  sceneManager->pause();
113 
115  driver->addModule(viewer);
116  driver->addModule(sceneManager);
117  driver->setDesiredDt(0.03);
118 
119  // Add default mouse and keyboard controls to the viewer
120  std::shared_ptr<Entity> mouseAndKeyControls =
121  SimulationUtils::createDefaultSceneControl(driver);
122  scene->addSceneObject(mouseAndKeyControls);
123 
124  driver->start();
125  }
126 
127  return 0;
128 }
129 
130 std::shared_ptr<FeDeformableObject>
131 makeFEDeformableObject(std::shared_ptr<TetrahedralMesh> tetMesh)
132 {
133  std::shared_ptr<SurfaceMesh> surfMesh = tetMesh->extractSurfaceMesh();
134 
135  // Configure dynamic model
138  config->m_fixedNodeIds = input.fixedNodeIds;
139  dynaModel->configure(config);
140  //dynaModel->configure(iMSTK_DATA_ROOT "/asianDragon/asianDragon.config");
141 
142  dynaModel->setTimeStepSizeType(TimeSteppingType::Fixed);
143  dynaModel->setModelGeometry(tetMesh);
144  imstkNew<BackwardEuler> timeIntegrator(0.01); // Create and add Backward Euler time integrator
145  dynaModel->setTimeIntegrator(timeIntegrator);
146 
148  mat->setDisplayMode(RenderMaterial::DisplayMode::WireframeSurface);
149  mat->setPointSize(10.0);
150  mat->setLineWidth(2.0);
151  mat->setEdgeColor(Color::Orange);
152 
153  // Scene object 1: Dragon
154  imstkNew<FeDeformableObject> deformableObj("Dragon");
155  deformableObj->setVisualGeometry(surfMesh);
156  deformableObj->getVisualModel(0)->setRenderMaterial(mat);
157  deformableObj->setPhysicsGeometry(tetMesh);
158  // Map simulated geometry to visual
159  deformableObj->setPhysicsToVisualMap(std::make_shared<PointwiseMap>(tetMesh, surfMesh));
160  deformableObj->setDynamicalModel(dynaModel);
161 
162  return deformableObj;
163 }
void setDesiredDt(const double dt)
Sets the target fixed timestep (may violate), seconds This ultimately effects the number of iteration...
void setActiveScene(std::shared_ptr< Scene > scene) override
Set scene to be rendered.
virtual void setTimeStepSizeType(const TimeSteppingType type)
Get/Set the type of approach used to update the time step size after every frame. ...
Compound Geometry.
void configure(const std::string &configFileName)
Configure the force model from external file.
void addModule(std::shared_ptr< Module > module) override
Add a module to run.
void setIntensity(const double intensity)
Set the light intensity. This value is unbounded.
Definition: imstkLight.h:71
std::shared_ptr<T> obj = std::make_shared<T>(); equivalent, convenience class for STL shared allocati...
Definition: imstkNew.h:29
void setModelGeometry(std::shared_ptr< Geometry > geometry)
Sets the model geometry.
void setPosition(const Vec3d p)
Set the local position.
std::shared_ptr< Camera > getActiveCamera() const
Get the active camera for the scene.
Definition: imstkScene.h:214
void setTimeIntegrator(std::shared_ptr< TimeIntegrator > timeIntegrator)
Get/Set time integrator.
std::shared_ptr< VisualModel > getVisualModel(const int index) const
Get/add visual model.
void addSceneObject(std::shared_ptr< Entity > entity)
Add a scene object.
Definition: imstkScene.cpp:307
void addLight(const std::string &name, std::shared_ptr< Light > newLight)
Add light from the scene.
Definition: imstkScene.cpp:388
void setFocalPoint(const Vec3d &p)
Get/Set the light focal point.
Definition: imstkLight.h:33
void setActiveScene(std::string newSceneName)
Sets the currently updating scene.
static LoggerG3 & startLogger()
Starts logger with default sinks, use getInstance to create a logger with no sinks.