iMSTK
Interactive Medical Simulation Toolkit
imstkScene.h
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 #pragma once
8 
9 #include "imstkAccumulationBuffer.h"
10 #include "imstkEventObject.h"
11 #include "imstkMath.h"
12 
13 #include <atomic>
14 #include <string>
15 #include <thread>
16 #include <unordered_map>
17 #include <unordered_set>
18 #include <vector>
19 
20 namespace imstk
21 {
22 class Camera;
23 class CameraController;
24 class DeviceControl;
25 class Entity;
26 class IblProbe;
27 class Light;
28 class TaskGraph;
29 class TaskGraphController;
30 class TrackingDeviceControl;
31 
32 namespace ParallelUtils { class SpinLock; }
33 
35 {
36  // Keep track of the fps for the scene
37  bool trackFPS = false;
38 
39  // If off, tasks will run sequentially
40  bool taskParallelizationEnabled = false;
41 
42  // If on, elapsed times for computational steps will be reported in map
43  bool taskTimingEnabled = false;
44 
45  // If on, the task graph will be written to a file
46  bool writeTaskGraph = false;
47 
48  // If on, non functional nodes and redundant edges will be removed from final graph
49  bool graphReductionEnabled = true;
50 
51  // If on, debug camera is positioned at scene bounding box
52  bool debugCamBoundingBox = true;
53 };
54 
60 class Scene : public EventObject
61 {
62 public:
63  template<class T>
64  using NamedMap = std::unordered_map<std::string, std::shared_ptr<T>>;
65 
66  Scene(const std::string& name, std::shared_ptr<SceneConfig> config = std::make_shared<SceneConfig>());
67  ~Scene() override = default;
68 
69  // *INDENT-OFF*
70  SIGNAL(Scene, configureTaskGraph);
75  SIGNAL(Scene, modified);
76  // *INDENT-ON*
77 
81  virtual bool initialize();
82 
86  void computeBoundingBox(Vec3d& lowerCorner, Vec3d& upperCorner, const double paddingPercent = 0.0);
87 
91  void buildTaskGraph();
92 
96  void initTaskGraph();
97 
101  void reset();
102 
106  void resetSceneObjects();
107 
111  virtual void advance(const double dt);
112 
116  virtual void updateVisuals(const double dt);
117 
122  void setEnableTaskTiming(const bool enabled);
123 
127  const std::unordered_set<std::shared_ptr<Entity>>& getSceneObjects() const { return m_sceneEntities; }
128 
132  std::shared_ptr<Entity> getSceneObject(const std::string& name) const;
133 
137  void addInteraction(std::shared_ptr<Entity> interaction);
138 
142  bool hasEntity(std::shared_ptr<Entity> entity) { return m_sceneEntities.find(entity) != m_sceneEntities.end(); }
143 
147  void addSceneObject(std::shared_ptr<Entity> entity);
148 
152  void removeSceneObject(const std::string& name);
153 
157  void removeSceneObject(std::shared_ptr<Entity> sceneObject);
158 
162  const std::vector<std::shared_ptr<Light>> getLights() const;
163 
167  std::shared_ptr<Light> getLight(const std::string& lightName) const;
168 
172  const std::unordered_map<std::string, std::shared_ptr<Camera>>& getCameras() const { return m_cameras; }
173 
177  void addLight(const std::string& name, std::shared_ptr<Light> newLight);
178 
182  void removeLight(const std::string& lightName);
183 
187  void setGlobalIBLProbe(std::shared_ptr<IblProbe> newIBLProbe) { m_globalIBLProbe = newIBLProbe; }
188 
192  std::shared_ptr<IblProbe> getGlobalIBLProbe() { return m_globalIBLProbe; }
193 
197  const std::string& getName() const { return m_name; }
198 
204  std::string getUniqueName(const std::string& name) const;
205 
209  std::shared_ptr<TaskGraph> getTaskGraph() const { return m_taskGraph; }
210 
214  std::shared_ptr<Camera> getActiveCamera() const { return m_activeCamera; }
215 
219  std::string getCameraName(const std::shared_ptr<Camera> cam) const;
220 
224  std::shared_ptr<Camera> getCamera(const std::string name) const;
225 
229  void addCamera(const std::string& name, std::shared_ptr<Camera> cam);
230 
235  void setActiveCamera(const std::string name);
236 
240  void removeCamera(const std::string name);
241 
245  void addControl(std::shared_ptr<DeviceControl> control);
246 
250  double getFPS() const { return m_fps; }
251 
252  double getFrameTime() const { return m_frameTimes.getAverage(); }
253 
257  double getSceneTime() const { return m_sceneTime; }
258 
262  const std::unordered_map<std::string, double>& getTaskComputeTimes() const { return m_nodeComputeTimes; }
263 
267  void lockComputeTimes();
268  void unlockComputeTimes();
270 
274  std::shared_ptr<SceneConfig> getConfig() const { return m_config; };
275 
276 protected:
277  std::shared_ptr<SceneConfig> m_config;
278 
279  std::string m_name;
280  std::unordered_set<std::shared_ptr<Entity>> m_sceneEntities;
281  std::unordered_map<std::string, std::shared_ptr<Light>> m_lightsMap;
282  std::shared_ptr<IblProbe> m_globalIBLProbe = nullptr;
283 
284  std::unordered_map<std::string, std::shared_ptr<Camera>> m_cameras;
285  std::shared_ptr<Camera> m_activeCamera;
286 
287  std::shared_ptr<TaskGraph> m_taskGraph;
288  std::shared_ptr<TaskGraphController> m_taskGraphController = nullptr;
289  std::function<void(Scene*)> m_postTaskGraphConfigureCallback = nullptr;
290 
291  std::shared_ptr<ParallelUtils::SpinLock> m_computeTimesLock;
292  std::unordered_map<std::string, double> m_nodeComputeTimes;
293 
295  double m_fps = 0.0;
296  double m_sceneTime = 0.0;
297 
298  std::atomic<bool> m_resetRequested = ATOMIC_VAR_INIT(false);
299 };
300 } // namespace imstk
bool hasEntity(std::shared_ptr< Entity > entity)
Check if Entity exists in scene.
Definition: imstkScene.h:142
std::unordered_map< std::string, double > m_nodeComputeTimes
Map of ComputeNode names to elapsed times for benchmarking.
Definition: imstkScene.h:292
void setGlobalIBLProbe(std::shared_ptr< IblProbe > newIBLProbe)
Set global IBL probe.
Definition: imstkScene.h:187
Compound Geometry.
const std::string & getName() const
Get the name of the scene.
Definition: imstkScene.h:197
const std::unordered_map< std::string, double > & getTaskComputeTimes() const
Get the map of elapsed times.
Definition: imstkScene.h:262
std::shared_ptr< TaskGraph > m_taskGraph
Computational graph.
Definition: imstkScene.h:287
std::shared_ptr< TaskGraph > getTaskGraph() const
Get the computational graph of the scene.
Definition: imstkScene.h:209
std::shared_ptr< Camera > getActiveCamera() const
Get the active camera for the scene.
Definition: imstkScene.h:214
std::shared_ptr< IblProbe > getGlobalIBLProbe()
Return global IBL probe.
Definition: imstkScene.h:192
std::string m_name
Name of the scene.
Definition: imstkScene.h:279
A collection of SceneObjects and interactions.
Definition: imstkScene.h:60
EventObject is the base class for all objects in iMSTK that can receive and emit events. It supports direct and queued observer functions. Direct observers receive events immediately on the same thread This can either be posted on an object or be a function pointer Queued observers receive events within their queue which they can process whenever they like. These can be connected with the connect/queuedConnect/disconnect functions Lambda recievers cannot be disconnected unless all receivers to a signal are removed.
double getFPS() const
Set/Get the frames per second (FPS)
Definition: imstkScene.h:250
std::shared_ptr< SceneConfig > getConfig() const
Get the configuration.
Definition: imstkScene.h:274
const std::unordered_map< std::string, std::shared_ptr< Camera > > & getCameras() const
Get and unordered map of cameras with names.
Definition: imstkScene.h:172
const std::unordered_set< std::shared_ptr< Entity > > & getSceneObjects() const
Return the SceneObjects of the scene.
Definition: imstkScene.h:127
double getSceneTime() const
Get the total scene time passed (accumulated deltatime)
Definition: imstkScene.h:257