iMSTK
Interactive Medical Simulation Toolkit
RenderingDebugExample.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 "imstkDebugGeometryModel.h"
9 #include "imstkDirectionalLight.h"
10 #include "imstkKeyboardDeviceClient.h"
11 #include "imstkKeyboardSceneControl.h"
12 #include "imstkLogger.h"
13 #include "imstkMouseDeviceClient.h"
14 #include "imstkMouseSceneControl.h"
15 #include "imstkScene.h"
16 #include "imstkSceneManager.h"
17 #include "imstkSimulationManager.h"
18 #include "imstkSimulationUtils.h"
19 #include "imstkTextVisualModel.h"
20 #include "imstkVTKViewer.h"
21 
22 using namespace imstk;
23 
24 static Vec3d
25 getRandomPositions(const double radius)
26 {
27  return radius * Vec3d(
28  2.0 * static_cast<double>(rand()) / static_cast<double>(RAND_MAX) - 1.0,
29  2.0 * static_cast<double>(rand()) / static_cast<double>(RAND_MAX) - 1.0,
30  2.0 * static_cast<double>(rand()) / static_cast<double>(RAND_MAX) - 1.0);
31 }
32 
33 static Color
34 getRandomColor()
35 {
36  return Color(
37  static_cast<double>(rand()) / static_cast<double>(RAND_MAX),
38  static_cast<double>(rand()) / static_cast<double>(RAND_MAX),
39  static_cast<double>(rand()) / static_cast<double>(RAND_MAX),
40  1.0);
41 }
42 
44 {
45 protected:
46  void init() override
47  {
48  std::shared_ptr<Entity> entity = m_entity.lock();
49  if (!entity->containsComponent(m_debugGeometryModel))
50  {
51  m_debugGeometryModel = std::make_shared<DebugGeometryModel>();
52  entity->addComponent(m_debugGeometryModel);
53  }
54 
55  if (!entity->containsComponent(m_textVisualModel))
56  {
57  m_textVisualModel = std::make_shared<TextVisualModel>("StatusText");
58  m_textVisualModel->setPosition(TextVisualModel::DisplayPosition::UpperLeft);
59  m_textVisualModel->setFontSize(30.0);
60  m_textVisualModel->setTextColor(Color::Orange);
61  entity->addComponent(m_textVisualModel);
62  }
63  }
64 
65 public:
66  void visualUpdate(const double& dt) override
67  {
68  m_t += dt;
69  if (m_t > 1.0)
70  {
71  m_addPrimitive = true;
72  m_t = 0.0;
73  }
74 
75  if (m_addPrimitive)
76  {
77  m_mode++;
78  if (m_mode % 3 == 0)
79  {
80  m_debugGeometryModel->addPoint(
81  getRandomPositions(15.0),
82  getRandomColor());
83  m_addPrimitive = false;
84  }
85  else if (m_mode % 3 == 1)
86  {
87  Vec3d p = getRandomPositions(50.0);
88  Vec3d shift = getRandomPositions(1.0);
89  m_debugGeometryModel->addLine(p + shift, -p + shift, getRandomColor());
90  m_addPrimitive = false;
91  }
92  else if (m_mode % 3 == 2)
93  {
94  Vec3d shift = getRandomPositions(10.0);
95  m_debugGeometryModel->addTriangle(
96  getRandomPositions(5.0) + shift,
97  getRandomPositions(5.0) + shift,
98  getRandomPositions(5.0) + shift,
99  getRandomColor());
100  m_addPrimitive = false;
101  }
102  }
103 
104  m_textVisualModel->setText("Primitives: " +
105  std::to_string(m_debugGeometryModel->getNumPoints()) + " (points) | " +
106  std::to_string(m_debugGeometryModel->getNumLines()) + " (lines) | " +
107  std::to_string(m_debugGeometryModel->getNumTriangles()) + " (triangles)"
108  );
109  }
110 
111  bool m_addPrimitive = false;
112  int m_mode = -1;
113  double m_t = 0;
114  std::shared_ptr<DebugGeometryModel> m_debugGeometryModel;
115  std::shared_ptr<TextVisualModel> m_textVisualModel;
116 };
117 
121 int
122 main()
123 {
124  // Setup logger (write to file and stdout)
126 
127  // Create a scene
128  auto scene = std::make_shared<Scene>("Debug rendering example");
129  scene->getConfig()->debugCamBoundingBox = false;
130  scene->getCamera("debug")->setPosition(0.0, 0.0, 50.0);
131 
132  // Setup a viewer to render in its own thread
133  auto viewer = std::make_shared<VTKViewer>();
134  viewer->setActiveScene(scene);
135  viewer->setWindowTitle("Debug Rendering");
136  viewer->setSize(1920, 1080);
137 
138  // Seed with system time
139  srand(time(NULL));
140 
141  auto debugGeomObj = std::make_shared<Entity>();
142  debugGeomObj->addComponent<DebugGeometryGenerator>();
143  scene->addSceneObject(debugGeomObj);
144 
145  // Set Camera configuration
146  scene->getActiveCamera()->setPosition(Vec3d(0.0, 0.0, 50.0));
147 
148  // Light
149  auto light = std::make_shared<DirectionalLight>();
150  light->setFocalPoint(Vec3d(-1.0, -1.0, -1.0));
151  light->setIntensity(1.0);
152  scene->addLight("light", light);
153 
154  // Run the simulation
155  {
156  // Setup a scene manager to advance the scene in its own thread
157  auto sceneManager = std::make_shared<SceneManager>();
158  sceneManager->setSleepDelay(1.0);
159  sceneManager->setActiveScene(scene);
160 
161  auto driver = std::make_shared<SimulationManager>();
162  driver->addModule(viewer);
163  driver->addModule(sceneManager);
164  driver->setDesiredDt(0.1);
165 
166  // Add default mouse and keyboard controls to the viewer
167  std::shared_ptr<Entity> mouseAndKeyControls =
168  SimulationUtils::createDefaultSceneControl(driver);
169  scene->addSceneObject(mouseAndKeyControls);
170 
171  driver->start();
172  }
173 
174  return 0;
175 }
Compound Geometry.
Color in RGB space.
Definition: imstkColor.h:24
A SceneBehaviour represents a single component system that resides in the scene. It makes the assumpt...
static LoggerG3 & startLogger()
Starts logger with default sinks, use getInstance to create a logger with no sinks.