iMSTK
Interactive Medical Simulation Toolkit
imstkSceneManager.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 "imstkSceneManager.h"
8 #include "imstkScene.h"
9 #include "imstkLogger.h"
10 #include "imstkDeviceControl.h"
11 
12 namespace imstk
13 {
14 SceneManager::SceneManager(std::string name) : m_activeScene(nullptr),
15  m_mode(Mode::Simulation), m_prevCamName("default")
16 {
17  // Set the preferred execution mode
18  m_executionType = ExecutionType::ADAPTIVE;
19 }
20 
21 void
23 {
24  if (m_mode == Mode::Simulation && mode == Mode::Debug)
25  {
26  // Store the previous camera name and switch to debug cam
27  m_prevCamName =
28  m_activeScene->getCameraName(m_activeScene->getActiveCamera());
29  m_activeScene->setActiveCamera("debug");
30  }
31  else if (m_mode == Mode::Debug && mode == Mode::Simulation)
32  {
33  // Switch back
34  m_activeScene->setActiveCamera(m_prevCamName);
35  }
36  m_mode = mode;
37 }
38 
39 bool
40 SceneManager::containsScene(std::string name) const
41 {
42  return (m_sceneMap.find(name) != m_sceneMap.end());
43 }
44 
45 void
47 {
48  // Check if the requested scene exists
49  if (!containsScene(name))
50  {
51  LOG(WARNING) << "Scene '" << name << "' not registered! Please register before setting active";
52  return;
53  }
54  // Check if the scene is already active
55  if (m_activeScene != nullptr && name == m_activeScene->getName())
56  {
57  LOG(INFO) << "Scene '" << name << "' is already active!";
58  return;
59  }
60 
61  auto newScene = this->getScene(name);
62 
63  m_activeScene = newScene;
64 }
65 
66 void
67 SceneManager::setActiveScene(std::shared_ptr<Scene> scene)
68 {
69  std::string name = scene->getName();
70 
71  // Check if the requested scene exists
72  if (!containsScene(name))
73  {
74  addScene(scene);
75  }
76  setActiveScene(name);
77 }
78 
79 bool
81 {
82  if (m_activeScene != nullptr)
83  {
84  return m_activeScene->initialize();
85  }
86  return true;
87 }
88 
89 void
91 {
92  // Advance the scene
93  if (m_activeScene != nullptr)
94  {
95  // Process events given to this module
96  this->doAllEvents();
97 
98  m_activeScene->advance(getDt());
99  }
100 }
101 
102 void
103 SceneManager::addScene(std::shared_ptr<Scene> scene)
104 {
105  if (scene == nullptr)
106  {
107  LOG(WARNING) << "Tried to add null scene!";
108  return;
109  }
110  m_sceneMap[scene->getName()] = scene;
111 }
112 
113 void
114 SceneManager::removeScene(std::string name)
115 {
116  if (containsScene(name))
117  {
118  std::shared_ptr<Scene> scene = m_sceneMap[name];
119  if (m_activeScene == scene)
120  {
121  m_activeScene = nullptr;
122  }
123  m_sceneMap.erase(name);
124  }
125 }
126 } // namespace imstk
void doAllEvents()
Do all the events in the event queue.
void updateModule() override
Run the thread.
Compound Geometry.
void removeScene(std::string name)
Remove a scene from the manager.
void setMode(Mode mode)
Switches the mode of the scene manager This alters the active scene.
void addScene(std::shared_ptr< Scene > scene)
Adds scene to the manager.
bool initModule() override
Initialize the thread.
void setActiveScene(std::string newSceneName)
Sets the currently updating scene.
bool containsScene(std::string name) const
Returns if the SceneManager contains the scene or not.