imstkSimulationManager.h 5.86 KB
Newer Older
1
2
/*=========================================================================

Alexis Girault's avatar
Alexis Girault committed
3
   Library: iMSTK
4

Alexis Girault's avatar
Alexis Girault committed
5
6
   Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
   & Imaging in Medicine, Rensselaer Polytechnic Institute.
7

Alexis Girault's avatar
Alexis Girault committed
8
9
10
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
11
12
13

      http://www.apache.org/licenses/LICENSE-2.0.txt

Alexis Girault's avatar
Alexis Girault committed
14
15
16
17
18
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
19

20
=========================================================================*/
21

22
23
24
#ifndef imstkSimulationManager_h
#define imstkSimulationManager_h

Alexis Girault's avatar
Alexis Girault committed
25
#include <unordered_map>
26
27
#include <vector>
#include <thread>
28
#include <memory>
29

30
#include "imstkScene.h"
Alexis Girault's avatar
Alexis Girault committed
31
#include "imstkModule.h"
32
#include "imstkSceneManager.h"
33
#include "imstkLogUtility.h"
Nicholas Milef's avatar
Nicholas Milef committed
34
35
#include "imstkViewer.h"

36
#ifdef iMSTK_WITH_RENDERING
Nicholas Milef's avatar
Nicholas Milef committed
37
38
39
40
41
#ifdef iMSTK_USE_Vulkan
#include "imstkVulkanViewer.h"
#else
#include "imstkVTKViewer.h"
#endif
42
#endif
43

44
45
namespace imstk
{
46
47
using SimulationStatus = ModuleStatus;

48
49
50
///
/// \class SimulationManager
///
51
/// \brief This class manages the overall simulation. The simulation can contain multiple scenes.
52
///
53
54
55
class SimulationManager
{
public:
56
57
58
    ///
    /// \brief Constructor
    ///
59
    SimulationManager(const bool disableRendering = false, const bool enableVR = false);
60

61
62
63
    ///
    /// \brief Default destructor
    ///
64
65
    ~SimulationManager() = default;

66
    ///
67
    /// \brief Returns the simulation status
68
    ///
69
70
71
    const SimulationStatus& getStatus() const;

    // Scene
72
73

    ///
74
    /// \brief Returns true if the scene is registered, else false
75
    ///
76
    bool isSceneRegistered(const std::string& sceneName) const;
77
78

    ///
79
    /// \brief Returns the scene manager given the scene
80
    ///
81
82
    std::shared_ptr<SceneManager> getSceneManager(const std::string& sceneName) const;
    std::shared_ptr<SceneManager> getSceneManager(std::shared_ptr<Scene> scene) const;
83
84

    ///
85
    /// \brief Returns the scene with a given name
86
    ///
87
    std::shared_ptr<Scene> getScene(const std::string& sceneName) const;
88
89

    ///
90
    /// \brief Returns the scene that is currently active
91
    ///
92
    std::shared_ptr<Scene> getActiveScene() const;
93
94

    ///
95
    /// \brief Create a new scene with a given name
96
    ///
97
98
    std::shared_ptr<Scene> createNewScene(const std::string& newSceneName);
    std::shared_ptr<Scene> createNewScene(std::string&& newSceneName);
99
100

    ///
101
    /// \brief Create a new scene with default name
102
    ///
103
    std::shared_ptr<Scene> createNewScene();
104
105

    ///
106
    /// \brief Add a new scene with given name to the scene list
107
    ///
108
    void addScene(std::shared_ptr<Scene> newScene);
109
110

    ///
111
    /// \brief Remove the scene with given name from the scene list
112
    ///
113
    void removeScene(const std::string& sceneName);
114

Alexis Girault's avatar
Alexis Girault committed
115
    // Modules
116
117

    ///
118
    /// \brief Returns true if the modules is registered, else false
119
    ///
120
    bool isModuleRegistered(const std::string& moduleName) const;
121
122

    ///
123
    /// \brief Returns the module given the name
124
    ///
125
    std::shared_ptr<Module> getModule(const std::string& moduleName) const;
126
127

    ///
128
    /// \brief Add a new module with a given name
129
    ///
Alexis Girault's avatar
Alexis Girault committed
130
    void addModule(std::shared_ptr<Module> newModule);
131
132

    ///
133
    /// \brief Remove the module with a given name
134
    ///
135
    void removeModule(const std::string& moduleName);
136

Nicholas Milef's avatar
Nicholas Milef committed
137
138
    // Viewer
    std::shared_ptr<Viewer> getViewer() const;
139

140
    // Simulation
141
142
143
144
145
146
147
148
149

    ///
    /// \brief Set the current scene to the one with the supplied name
    ///
    void setActiveScene(const std::string& newSceneName,
                        const bool unloadCurrentScene = false);
    void setActiveScene(std::shared_ptr<Scene> scene,
                        const bool unloadCurrentScene = false);

150
    ///
151
    /// \brief Start the simulation by initializing the active scene
152
    ///
153
154
    void startSimulation(const SimulationStatus simStatus = SimulationStatus::PAUSED,
                         const Renderer::Mode renderMode = Renderer::Mode::SIMULATION);
155

156
    ///
157
    /// \brief Run the simulation from a paused state
158
    ///
159
    void runSimulation();
160
161

    ///
162
    /// \brief Pause the simulation
163
    ///
164
    void pauseSimulation();
165
166

    ///
167
    /// \brief Reset the simulation to initial state
168
    ///
169
170
171
    void resetSimulation();

    ///
172
    /// \brief End the simulation
173
    ///
174
    void endSimulation();
175

176
private:
Alexis Girault's avatar
Alexis Girault committed
177

178
179
180
181
182
183
184
    ///
    /// \brief Launch simulation for the first time.
    /// 1. Initialize the active scene if not initialized already.
    /// 2. Launches separate threads for each module.
    ///
    void launchSimulation();

185
186
187
188
189
190
191
192
    ///
    /// \brief Start the viewer
    ///
    void startViewer(const Renderer::Mode renderMode = Renderer::Mode::DEBUG);

    ///
    /// \brief Print user keyboard controls
    ///
193
    void printUserControlsInfo(const bool isRendering = true) const;
194

195
    void startModuleInNewThread(std::shared_ptr<Module> module);
196

197
    ///
198
    /// \brief Keeps things in an infinite loop if rendering is disabled
199
200
201
202
    /// The same keys can be used to trigger PAUSE, RUNNING, RESET of the simulation
    ///
    void infiniteLoopNoRenderingMode();

203
204
    SimulationStatus m_status = SimulationStatus::INACTIVE;

205
    std::string m_activeSceneName = "";
206
    std::unordered_map<std::string, std::shared_ptr<SceneManager>> m_sceneManagerMap;
207

Alexis Girault's avatar
Alexis Girault committed
208
    std::unordered_map<std::string, std::shared_ptr<Module>> m_modulesMap;
209

Alexis Girault's avatar
Alexis Girault committed
210
    std::unordered_map<std::string, std::thread> m_threadMap;
211

212
    std::shared_ptr<Viewer> m_viewer = nullptr;
213
    std::shared_ptr<LogUtility> m_logUtil = std::make_shared<LogUtility>();
214
215

    bool m_simThreadLaunched = false;
216
};
217
} // imstk
218

Alexis Girault's avatar
Alexis Girault committed
219
#endif // ifndef imstkSimulationManager_h