iMSTK
Interactive Medical Simulation Toolkit
taskGraphTimingExample.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 "imstkCamera.h"
8 #include "imstkGeometryUtilities.h"
9 #include "imstkKeyboardDeviceClient.h"
10 #include "imstkKeyboardSceneControl.h"
11 #include "imstkLineMesh.h"
12 #include "imstkLogger.h"
13 #include "imstkMouseDeviceClient.h"
14 #include "imstkMouseSceneControl.h"
15 #include "imstkNew.h"
16 #include "imstkPbdModel.h"
17 #include "imstkPbdModelConfig.h"
18 #include "imstkPbdObject.h"
19 #include "imstkRenderMaterial.h"
20 #include "imstkScene.h"
21 #include "imstkSceneManager.h"
22 #include "imstkSimulationManager.h"
23 #include "imstkSimulationUtils.h"
24 #include "imstkTaskGraphVizWriter.h"
25 #include "imstkVisualModel.h"
26 #include "imstkVTKViewer.h"
27 
28 using namespace imstk;
29 
33 static std::shared_ptr<PbdObject>
34 makePbdString(
35  const std::string& name,
36  const Vec3d& pos,
37  const int numVerts,
38  const double stringLength,
39  const double bendStiffness,
40  const Color& color)
41 {
42  imstkNew<PbdObject> stringObj(name);
43 
44  // Setup the Geometry
45  std::shared_ptr<LineMesh> stringMesh =
46  GeometryUtils::toLineGrid(pos, Vec3d(0.0, -1.0, 0.0), stringLength, numVerts);
47 
48  // Setup the Parameters
49  imstkNew<PbdModelConfig> pbdParams;
50  pbdParams->enableConstraint(PbdModelConfig::ConstraintGenType::Distance, 1e7);
51  pbdParams->enableConstraint(PbdModelConfig::ConstraintGenType::Bend, bendStiffness);
52  pbdParams->m_gravity = Vec3d(0, -9.8, 0);
53  pbdParams->m_dt = 0.0005;
54  pbdParams->m_iterations = 5;
55 
56  // Setup the Model
57  imstkNew<PbdModel> pbdModel;
58  pbdModel->configure(pbdParams);
59 
60  // Setup the VisualModel
61  imstkNew<RenderMaterial> material;
62  material->setBackFaceCulling(false);
63  material->setColor(color);
64  material->setLineWidth(2.0f);
65  material->setDisplayMode(RenderMaterial::DisplayMode::Wireframe);
66 
67  imstkNew<VisualModel> visualModel;
68  visualModel->setGeometry(stringMesh);
69  visualModel->setRenderMaterial(material);
70 
71  // Setup the Object
72  stringObj->addVisualModel(visualModel);
73  stringObj->setPhysicsGeometry(stringMesh);
74  stringObj->setDynamicalModel(pbdModel);
75  stringObj->getPbdBody()->fixedNodeIds = { 0 };
76  stringObj->getPbdBody()->uniformMassValue = 5.0;
77 
78  return stringObj;
79 }
80 
81 static std::vector<std::shared_ptr<PbdObject>>
82 makePbdStrings(const int numStrings,
83  const int numVerts,
84  const double stringSpacing,
85  const double stringLength,
86  const Color& startColor,
87  const Color& endColor)
88 {
89  std::vector<std::shared_ptr<PbdObject>> pbdStringObjs(numStrings);
90 
91  const double size = stringSpacing * (numStrings - 1);
92 
93  for (int i = 0; i < numStrings; i++)
94  {
95  const Vec3d tipPos = Vec3d(static_cast<double>(i) * stringSpacing - size * 0.5, stringLength * 0.5, 0.0);
96  const double t = static_cast<double>(i) / (numStrings - 1);
97 
98  pbdStringObjs[i] = makePbdString(
99  "String " + std::to_string(i),
100  tipPos,
101  numVerts,
102  stringLength,
103  (static_cast<double>(i) * 0.1 / numStrings + 0.001) * 1e6,
104  Color::lerpRgb(startColor, endColor, t));
105  }
106 
107  return pbdStringObjs;
108 }
109 
110 const double radius = 1.5;
111 const int numStrings = 8; // Number of strings
112 const int numVerts = 30; // Number of vertices on each string
113 const double stringSpacing = 2.0; // How far each string is apart
114 const double stringLength = 10.0; // Total length of string
115 const Color startColor = Color(1.0, 0.0, 0.0); // Color of first string
116 const Color endColor = Color(0.0, 1.0, 0.0); // Color of last string
117 
123 int
124 main()
125 {
126  // Setup logger (write to file and stdout)
128 
129  imstkNew<Scene> scene("PBDString");
130  scene->getActiveCamera()->setPosition(0.0, 0.0, 15.0);
131 
132  // Setup N separate strings with varying bend stiffnesses
133  std::vector<std::shared_ptr<PbdObject>> pbdStringObjs =
134  makePbdStrings(numStrings, numVerts, stringSpacing, stringLength, startColor, endColor);
135  // Add the string scene objects to the scene
136  for (std::shared_ptr<PbdObject> obj : pbdStringObjs)
137  {
138  scene->addSceneObject(obj);
139  }
140 
141  // Run the simulation
142  {
143  // Setup a viewer to render
144  imstkNew<VTKViewer> viewer;
145  viewer->setActiveScene(scene);
146 
147  // Setup a scene manager to advance the scene
148  imstkNew<SceneManager> sceneManager;
149  sceneManager->setActiveScene(scene);
150  sceneManager->pause();
151  double t = 0.0;
152  connect<Event>(sceneManager, &SceneManager::postUpdate, [&](Event*)
153  {
154  const double dt = sceneManager->getDt();
155  for (size_t i = 0; i < pbdStringObjs.size(); i++)
156  {
157  std::shared_ptr<PbdModel> model = pbdStringObjs[i]->getPbdModel();
158  model->getConfig()->m_dt = dt;
159  std::shared_ptr<VecDataArray<double, 3>> positions = pbdStringObjs[i]->getPbdBody()->vertices;
160  (*positions)[0] += Vec3d(
161  -std::sin(t) * radius * dt,
162  0.0,
163  std::cos(t) * radius * dt);
164  }
165  t += dt;
166  });
167 
169  driver->addModule(viewer);
170  driver->addModule(sceneManager);
171  driver->setDesiredDt(0.005);
172 
173  // Add default mouse and keyboard controls to the viewer
174  std::shared_ptr<Entity> mouseAndKeyControls =
175  SimulationUtils::createDefaultSceneControl(driver);
176  scene->addSceneObject(mouseAndKeyControls);
177 
178  driver->start();
179  }
180 
181  // Write the graph, highlighting the critical path and putting the completion time in the name
183  writer->setInput(scene->getTaskGraph());
184  writer->setFileName("taskGraphBenchmarkExample.svg");
185  writer->setHighlightCriticalPath(true);
186  writer->setWriteNodeComputeTimesColor(true);
187  writer->setWriteNodeComputeTimesText(true);
188  writer->write();
189 
190  return 0;
191 }
void configure(std::shared_ptr< PbdModelConfig > params)
Set simulation parameters.
void setActiveScene(std::shared_ptr< Scene > scene) override
Set scene to be rendered.
Base class for events which contain a type, priority, and data priority defaults to 0 and uses a grea...
void setWriteNodeComputeTimesColor(bool writeNodeComputeTimesColor)
If on, will write the time the node took to complete as a color.
Compound Geometry.
void enableConstraint(ConstraintGenType type, const double stiffness, const int bodyId=2)
Enables a constraint of type defined by ConstraintGenType with given stiffness. If constraint of that...
void write()
Writes the graph to a file given the filename.
void setFileName(std::string fileName)
The filename and path to write too.
unsigned int m_iterations
Internal constraints pbd solver iterations.
void setHighlightCriticalPath(bool highlightCriticalPath)
If on, will highlight the critical path in red.
double getDt() const
Get/Set the time step.
Definition: imstkModule.h:63
std::shared_ptr<T> obj = std::make_shared<T>(); equivalent, convenience class for STL shared allocati...
Definition: imstkNew.h:29
std::shared_ptr< LineMesh > toLineGrid(const Vec3d &start, const Vec3d &dir, const double length, const int dim)
Creates a set of connected lines.
void setRenderMaterial(std::shared_ptr< RenderMaterial > renderMaterial)
Set/Get render material.
void setWriteNodeComputeTimesText(bool writeNodeComputeTimesText)
If on, will write the time the node took to complete in name as text.
Color in RGB space.
Definition: imstkColor.h:24
double m_dt
Time step size.
void setInput(std::shared_ptr< TaskGraph > graph)
The graph to write.
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.
Vec3d m_gravity
Gravity acceleration.