imstkSimulationManager.h 5.58 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
36
37
38
39
40
#include "imstkViewer.h"

#ifdef iMSTK_USE_Vulkan
#include "imstkVulkanViewer.h"
#else
#include "imstkVTKViewer.h"
#endif
41

42
43
namespace imstk
{
44
45
using SimulationStatus = ModuleStatus;

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

59
60
61
    ///
    /// \brief Default destructor
    ///
62
63
    ~SimulationManager() = default;

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

    // Scene
70
71

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

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

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

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

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

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

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

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

Alexis Girault's avatar
Alexis Girault committed
113
    // Modules
114
115

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

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

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

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

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

138
    // Simulation
139
140
141
142
143
144
145
146
147

    ///
    /// \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);

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

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

    ///
160
    /// \brief Pause the simulation
161
    ///
162
    void pauseSimulation();
163
164

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

    ///
170
    /// \brief End the simulation
171
    ///
172
    void endSimulation();
173

174
private:
Alexis Girault's avatar
Alexis Girault committed
175

176
177
178
179
180
181
182
    ///
    /// \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();

183
184
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
    ///
    void printUserControlsInfo();

193
    void startModuleInNewThread(std::shared_ptr<Module> module);
194

195
196
    SimulationStatus m_status = SimulationStatus::INACTIVE;

197
    std::string m_activeSceneName = "";
198
    std::unordered_map<std::string, std::shared_ptr<SceneManager>> m_sceneManagerMap;
199

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

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

204
    std::shared_ptr<Viewer> m_viewer = nullptr;
205
    std::shared_ptr<LogUtility> m_logUtil = std::make_shared<LogUtility>();
206
207

    bool m_simThreadLaunched = false;
208
};
209
} // imstk
210

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