iMSTK
Interactive Medical Simulation Toolkit
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
imstkSimulationUtils.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 "imstkSimulationUtils.h"
8 #include "imstkEntity.h"
9 #include "imstkFpsTxtCounter.h"
10 #include "imstkKeyboardDeviceClient.h"
11 #include "imstkKeyboardSceneControl.h"
12 #include "imstkLogger.h"
13 #include "imstkMouseDeviceClient.h"
14 #include "imstkMouseSceneControl.h"
15 #include "imstkPerformanceGraph.h"
16 #include "imstkSceneControlText.h"
17 #include "imstkSceneManager.h"
18 #include "imstkSimulationManager.h"
19 #include "imstkViewer.h"
20 
21 namespace imstk
22 {
23 namespace SimulationUtils
24 {
25 std::shared_ptr<Entity>
26 createDefaultSceneControl(
27  std::shared_ptr<SimulationManager> driver)
28 {
29  std::shared_ptr<SceneManager> sceneManager = nullptr;
30  std::shared_ptr<Viewer> viewer = nullptr;
31  for (auto driverModule : driver->getModules())
32  {
33  if (auto viewerModule = std::dynamic_pointer_cast<Viewer>(driverModule))
34  {
35  viewer = viewerModule;
36  }
37  else if (auto sceneManagerModule = std::dynamic_pointer_cast<SceneManager>(driverModule))
38  {
39  sceneManager = sceneManagerModule;
40  }
41  }
42  CHECK(sceneManager != nullptr) << "The SimulationManager has no SceneManager";
43  CHECK(viewer != nullptr) << "The SimulationManager has no Viewer";
44 
45  // Setup a text to render the state of the simulation (if paused or not)
46  auto statusText = std::make_shared<SceneControlText>();
47  statusText->setSceneManager(sceneManager);
48 
49  // Setup a default key control scheme (commonly used in examples)
50  auto keyControl = std::make_shared<KeyboardSceneControl>();
51  keyControl->setDevice(viewer->getKeyboardDevice());
52  keyControl->setSceneManager(sceneManager);
53  keyControl->setModuleDriver(driver);
54  keyControl->setSceneControlText(statusText);
55 
56  // Setup a default mouse control scheme (commonly used in examples)
57  auto mouseControl = std::make_shared<MouseSceneControl>();
58  mouseControl->setDevice(viewer->getMouseDevice());
59  mouseControl->setSceneManager(sceneManager);
60 
61  // Setup an fps counter (toggled on info level of viewer)
62  auto fpsCounter = std::make_shared<FpsTxtCounter>();
63  fpsCounter->setSceneManager(sceneManager);
64  fpsCounter->setViewer(viewer);
65 
66  // Setup a task node performance graph (toggled on info level of viewer)
67  auto perfGraph = std::make_shared<PerformanceGraph>();
68  perfGraph->setSceneManager(sceneManager);
69  perfGraph->setViewer(viewer);
70 
71  auto entity = std::make_shared<Entity>("DefaultSceneControlEntity");
72  entity->addComponent(keyControl);
73  entity->addComponent(statusText);
74  entity->addComponent(mouseControl);
75  entity->addComponent(fpsCounter);
76  entity->addComponent(perfGraph);
77 
78  return entity;
79 }
80 } // namespace SimulationUtils
81 } // namespace imstk
Compound Geometry.