Ocean
scenedescription/Manager.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_SCENEDESCRIPTION_MANAGER_H
9 #define META_OCEAN_SCENEDESCRIPTION_MANAGER_H
10 
16 
17 #include "ocean/base/Singleton.h"
18 
19 #include "ocean/io/FileManager.h"
20 
21 #include "ocean/rendering/View.h"
22 
23 #include <map>
24 #include <vector>
25 
26 namespace Ocean
27 {
28 
29 namespace SceneDescription
30 {
31 
32 // Forward declaration.
33 class EventNode;
34 
35 // Forward declaration.
36 class UpdateNode;
37 
38 // Forward declaration.
39 class SDXEventNode;
40 
41 // Forward declaration.
42 class SDXUpdateNode;
43 
44 /**
45  * This class implements the manager for all scene descriptions.
46  * The manager encapsulates all registered scene description libraries.<br>
47  * Use the load() function to load a new scene description object.<br>
48  *
49  * Example demonstrating the manager usage:
50  * @code
51  * // This example shows how to use the load() function and how to determine which type of scene description has been returned
52  *
53  * // Get the rendering engine from e.g. the application
54  * Rendering::EngineRef renderingEngine = application.getRenderingEngine();
55  * ocean_assert(renderingEngine);
56  *
57  * // Get the rendering framebuffer from e.g. the application
58  * Rendering::FramebufferRef renderingFramebuffer = application.getRenderingFramebuffer();
59  * ocean_assert(renderingFramebuffer);
60  *
61  * // Load a scene description file - a permanent scene description is preferred
62  * SceneRef scene = Manager::get().load("scenefile.x3dv", renderingEngine, SceneDescription::TYPE_PERMANENT);
63  *
64  * if (scene)
65  * {
66  * if (scene->descriptionType() == SceneDescription::TYPE_PERMANENT)
67  * {
68  * SDXSceneRef sdxScene(scene);
69  * ocean_assert(sdxScene);
70  *
71  * // The rendering scene object of the permanent scene description object can be added to the used rendering framebuffer directly
72  * renderingFramebuffer->addScene(sdxScene->renderingScene());
73  * }
74  * else
75  * {
76  * ocean_assert(scene->descriptionType() == SceneDescription::TYPE_TRANSIENT);
77  *
78  * SDLSceneRef sdlScene(scene);
79  * ocean_assert(sdlScene);
80  *
81  * // We have a transient scene description object, thus we have to apply the description to the rendering engine explicitly
82  * Rendering::SceneRef renderingScene = sdlScene->apply(renderingEngine);
83  *
84  * // If the created rendering scene object is valid is can be added to the used rendering framebuffer
85  * if (renderingScene)
86  * renderingFramebuffer->addScene(renderingScene);
87  * }
88  * }
89  * else
90  * {
91  * // The scene could not be loaded, e.g. the file did not exist, or an error occurred.
92  * Log::error() << "The scene description file could not be loaded.";
93  * }
94  *
95  * @endcode
96  * @ingroup scenedescription
97  */
98 class OCEAN_SCENEDESCRIPTION_EXPORT Manager :
99  public IO::FileManager,
100  public Singleton<Manager>
101 {
102  friend class Singleton<Manager>;
103  friend class Library;
104  friend class SDXEventNode;
105  friend class SDXUpdateNode;
106 
107  public:
108 
109  /**
110  * Definition of a vector holding library names.
111  */
112  typedef std::vector<std::string> LibraryNames;
113 
114  protected:
115 
116  /**
117  * Definition of a pair combining a library with a reference counter.
118  */
119  typedef std::pair<LibraryRef, unsigned int> LibraryCounterPair;
120 
121  /**
122  * Definition of a vector holding library pairs.
123  */
124  typedef std::vector<LibraryCounterPair> Libraries;
125 
126  /**
127  * Definition of a set holding library names.
128  */
129  typedef std::unordered_set<std::string> NameSet;
130 
131  /**
132  * Definition of a map mapping scene description node ids to event nodes.
133  */
134  typedef std::unordered_map<NodeId, SDXEventNode*> EventNodes;
135 
136  /**
137  * Definition of a map mapping scene description node ids to update nodes.
138  */
139  typedef std::unordered_map<NodeId, SDXUpdateNode*> UpdateNodes;
140 
141  /**
142  * Definition of a set holding permanent scene description objects.
143  */
144  typedef std::map<SceneId, SDXSceneRef> PermanentSceneMap;
145 
146  public:
147 
148  /**
149  * Loads a new scene and creates a scene description hierarchy.
150  * Two different scene description types are available.<br>
151  * First: A transient scene description creates a scene and object hierarchy and must be applied to a rendering engine afterwards explicitly.<br>
152  * After that all scene description objects are not needed anymore and can be disposed.<br>
153  *
154  * Second: a permanent scene description creates a scene and object hierarchy and directly creates corresponding rendering engine objects.<br>
155  * Therefore, all rendering objects exist as long as the corresponding scene description object exist.<br>
156  * The scene description object allows for easy scene manipulation without knowledge of the underling rendering structure.<br>
157  * This manager will hold a copy of all permanent scene description objects as long as they are unloaded explicitly.<br>
158  *
159  * Beware: Check the type of returned scene description object.<br>
160  * Depending on the available scene description libraries the description type may vary.<br>
161  * To not hold a reference of the returned object longer than necessary!<br>
162  * @param filename The filename of the scene to load
163  * @param engine Rendering engine to be connected with the scene description, must be defined for permanent scene description objects only
164  * @param timestamp The current timestamp, must be valid
165  * @param preferredDescriptionType Preferred description type of the scene to load
166  * @param progress The progress state receiving recurrently information about the load state with range [0, 1]
167  * @param cancel The cancel state allows the cancellation of a load process while loading process hasn't finished, if the cancel state is used the load process stops if the value is set to True
168  * @return Resulting scene
169  * @see unload().
170  */
171  SceneRef load(const std::string& filename, const Rendering::EngineRef& engine, const Timestamp& timestamp, const DescriptionType preferredDescriptionType = TYPE_PERMANENT, float* progress = nullptr, bool* cancel = nullptr);
172 
173  /**
174  * Unloads a given permanent scene description objects.
175  * Transient description objects must not be unloaded because the manager does not hold a copy of transient scene objects.<br>
176  * Beware: At the moment the scene is unloaded there should not be any other resource holding a reference to this object!
177  * @param sceneId Scene id of the permanent scene description object to be unloaded
178  * @return True, if succeeded
179  * @see load().
180  */
181  bool unload(const SceneId sceneId);
182 
183  /**
184  * Unloads all permanent scene description objects.
185  */
186  void unloadScenes();
187 
188  /**
189  * Returns a list of all registered libraries.
190  * @return Registered libraries
191  */
193 
194  /**
195  * Returns all currently supported file extensions.
196  * @see FileManager::supportedFormats().
197  */
199 
200  /**
201  * Returns whether currently at least one scene description node handles mouse events.
202  * The state change with every new frame.<br>
203  * Because mouse events can produce high computational overhead this test can safe computational time.
204  * @return True, if so
205  */
206  inline bool handlesMouseEvents() const;
207 
208  /**
209  * Sends a mouse event to be handled by the scene description.
210  * @param button The button type
211  * @param buttonEvent Type of the event
212  * @param screenPosition Screen device position
213  * @param objectPosition Object position
214  * @param objectId Id of a rendering object associated with the event
215  * @param timestamp Event timestamp
216  */
217  void mouseEvent(const ButtonType button, const ButtonEvent buttonEvent, const Vector2& screenPosition, const Vector3& objectPosition, const Rendering::ObjectId objectId, const Timestamp timestamp);
218 
219  /**
220  * Sends a key event to be handled by the scene description.
221  * @param key The key type
222  * @param buttonEvent Type of the event
223  * @param objectId Id of a rendering object associated with the event
224  * @param timestamp Event timestamp
225  */
226  void keyEvent(const int key, const ButtonEvent buttonEvent, const Rendering::ObjectId objectId, const Timestamp timestamp);
227 
228  /**
229  * Pre-updates all scene description objects needing regularly pre updates.
230  * @param view Rendering view to be used for rendering
231  * @param timestamp Preferred update timestamp
232  * @return Actually used update timestamp
233  */
234  Timestamp preUpdate(const Rendering::ViewRef& view, const Timestamp timestamp);
235 
236  /**
237  * Pre-updates all scene description objects of a specified library only.
238  * @param library Name of the library
239  * @param view Rendering view to be used for rendering
240  * @param timestamp Preferred Update timestamp
241  * @return Actually used update timestamp
242  */
243  Timestamp preUpdate(const std::string& library, const Rendering::ViewRef& view, const Timestamp timestamp);
244 
245  /**
246  * Updates all scene description objects needing regularly updates.
247  * @param view Rendering view to be used for rendering
248  * @param timestamp Update timestamp
249  */
250  void update(const Rendering::ViewRef& view, const Timestamp timestamp);
251 
252  /**
253  * Updates all scene description objects of a specified library only.
254  * @param library Name of the library
255  * @param view Rendering view to be used for rendering
256  * @param timestamp Update timestamp
257  */
258  void update(const std::string& library, const Rendering::ViewRef& view, const Timestamp timestamp);
259 
260  /**
261  * Returns the node reference of the first available scene description node with a specified name.
262  * This function searches in all libraries and all description types.
263  * @param name The name of the node to return
264  * @return Node reference of the node, the reference will be empty if the node does not exist
265  */
266  NodeRef node(const std::string& name) const;
267 
268  /**
269  * Returns the node reference of the first available scene description node with a specified name defined inside a specific library.
270  * @param library The library to be searched
271  * @param name The name of the node to return
272  * @return Node reference of the node, the reference will be empty if the node does not exist
273  */
274  NodeRef node(const std::string& library, const std::string& name) const;
275 
276  /**
277  * Returns all node references of all available scene description nodes with a specified name.
278  * This function searches in all libraries and all description types.
279  * @param name The name of the nodes to return
280  * @return All node references found
281  */
282  NodeRefs nodes(const std::string& name) const;
283 
284  /**
285  * Returns all node references of all available scene description nodes with a specified name defined inside a specific library.
286  * @param library The library to be searched
287  * @param name The name of the nodes to return
288  * @return All node references found
289  */
290  NodeRefs nodes(const std::string& library, const std::string& name) const;
291 
292  /**
293  * Releases all scene library.
294  */
295  void release();
296 
297  /**
298  * Registers a new library.
299  * With each register call, the reference counter for a specific library will be incremented.
300  * Each call to registerLibrary() needs to be balanced with a corresponding call of unregisterLibrary() before shutting down.
301  * @param name The name of the library to register, must be valid
302  * @return True, if the library has not been registered before
303  * @tparam T The data type of the library to register
304  * @see unregisterLibrary().
305  */
306  template <typename T>
307  bool registerLibrary(const std::string& name);
308 
309  /**
310  * Unregisters a library.
311  * With each unregister call, the reference counter for a specific library will be decremented and removed from the system if the counter reaches zero.
312  * Each call to registerLibrary() needs to be balanced with a corresponding call of unregisterLibrary() before shutting down.
313  * @param name The name of the library to unregister, must be valid
314  * @return True, if the library was actually removed from the system (as the reference counter reached zero); False, if the library is still used by someone else
315  * @see registerLibrary().
316  */
317  bool unregisterLibrary(const std::string& name);
318 
319  protected:
320 
321  /**
322  * Creates a new manager.
323  */
325 
326  /**
327  * Destructs a manager.
328  */
329  virtual ~Manager();
330 
331  /**
332  * Registers a new event node.
333  * @param node Event node to register
334  */
336 
337  /**
338  * Unregisters an event node.
339  * @param node Event node to unregister
340  */
342 
343  /**
344  * Registers a new update node.
345  * @param node Update node to register
346  */
348 
349  /**
350  * Unregisters an update node.
351  * @param node Update node to unregister
352  */
354 
355  protected:
356 
357  /// The vector holding all registered scene description libraries.
359 
360  /// Map holding all permanent scene description objects.
362 
363  /// Map holding all event nodes.
365 
366  /// Map holding all update nodes.
368 
369  /// Manager lock.
371 
372  /// Library lock.
374 };
375 
376 inline bool Manager::handlesMouseEvents() const
377 {
378  const ScopedLock scopedLock(managerLock_);
379 
380  return eventNodes_.empty() == false;
381 }
382 
383 template <typename T>
384 bool Manager::registerLibrary(const std::string& name)
385 {
386  const ScopedLock mLock(managerLock_);
387  const ScopedLock lLock(libraryLock_);
388 
389  // first we check whether the library has been registered already
390 
391  for (Libraries::iterator i = libraries_.begin(); i != libraries_.end(); ++i)
392  {
393  ocean_assert(i->first);
394 
395  if (i->first->name() == name)
396  {
397  ++i->second;
398  return false;
399  }
400  }
401 
402  // the library has not been registered before, so we insert the library based on the priority
403 
404  LibraryRef newLibrary = T::create();
405 
406  for (Libraries::iterator i = libraries_.begin(); i != libraries_.end(); ++i)
407  {
408  ocean_assert(i->first);
409 
410  if (i->first->priority() < newLibrary->priority())
411  {
412  libraries_.insert(i, std::make_pair(std::move(newLibrary), 1u));
413  return true;
414  }
415  }
416 
417  // the library goes to the end
418 
419  libraries_.emplace_back(std::make_pair(std::move(newLibrary), 1u));
420  return true;
421 }
422 
423 }
424 
425 }
426 
427 #endif // META_OCEAN_SCENEDESCRIPTION_MANAGER_H
This class is the base class for all plugin manager able to load and manager files.
Definition: FileManager.h:26
std::map< std::string, std::string > FileExtensions
Definition of a map mapping supported file extensions to file type descriptions.
Definition: FileManager.h:32
This class implements a recursive lock object.
Definition: Lock.h:31
This class is the base class for all scene description libraries.
Definition: scenedescription/Library.h:37
This class implements the manager for all scene descriptions.
Definition: scenedescription/Manager.h:101
Lock managerLock_
Manager lock.
Definition: scenedescription/Manager.h:370
Timestamp preUpdate(const std::string &library, const Rendering::ViewRef &view, const Timestamp timestamp)
Pre-updates all scene description objects of a specified library only.
Manager()
Creates a new manager.
UpdateNodes updateNodes_
Map holding all update nodes.
Definition: scenedescription/Manager.h:367
std::vector< std::string > LibraryNames
Definition of a vector holding library names.
Definition: scenedescription/Manager.h:112
bool unload(const SceneId sceneId)
Unloads a given permanent scene description objects.
std::unordered_map< NodeId, SDXUpdateNode * > UpdateNodes
Definition of a map mapping scene description node ids to update nodes.
Definition: scenedescription/Manager.h:139
std::unordered_set< std::string > NameSet
Definition of a set holding library names.
Definition: scenedescription/Manager.h:129
std::pair< LibraryRef, unsigned int > LibraryCounterPair
Definition of a pair combining a library with a reference counter.
Definition: scenedescription/Manager.h:119
bool unregisterLibrary(const std::string &name)
Unregisters a library.
void mouseEvent(const ButtonType button, const ButtonEvent buttonEvent, const Vector2 &screenPosition, const Vector3 &objectPosition, const Rendering::ObjectId objectId, const Timestamp timestamp)
Sends a mouse event to be handled by the scene description.
void registerEventNode(SDXEventNode &node)
Registers a new event node.
void unregisterUpdateNode(SDXUpdateNode &node)
Unregisters an update node.
void unregisterEventNode(SDXEventNode &node)
Unregisters an event node.
SceneRef load(const std::string &filename, const Rendering::EngineRef &engine, const Timestamp &timestamp, const DescriptionType preferredDescriptionType=TYPE_PERMANENT, float *progress=nullptr, bool *cancel=nullptr)
Loads a new scene and creates a scene description hierarchy.
PermanentSceneMap permanentSceneMap_
Map holding all permanent scene description objects.
Definition: scenedescription/Manager.h:361
std::vector< LibraryCounterPair > Libraries
Definition of a vector holding library pairs.
Definition: scenedescription/Manager.h:124
LibraryNames libraries()
Returns a list of all registered libraries.
NodeRef node(const std::string &library, const std::string &name) const
Returns the node reference of the first available scene description node with a specified name define...
void keyEvent(const int key, const ButtonEvent buttonEvent, const Rendering::ObjectId objectId, const Timestamp timestamp)
Sends a key event to be handled by the scene description.
std::unordered_map< NodeId, SDXEventNode * > EventNodes
Definition of a map mapping scene description node ids to event nodes.
Definition: scenedescription/Manager.h:134
bool handlesMouseEvents() const
Returns whether currently at least one scene description node handles mouse events.
Definition: scenedescription/Manager.h:376
NodeRefs nodes(const std::string &name) const
Returns all node references of all available scene description nodes with a specified name.
Libraries libraries_
The vector holding all registered scene description libraries.
Definition: scenedescription/Manager.h:358
void unloadScenes()
Unloads all permanent scene description objects.
Timestamp preUpdate(const Rendering::ViewRef &view, const Timestamp timestamp)
Pre-updates all scene description objects needing regularly pre updates.
NodeRefs nodes(const std::string &library, const std::string &name) const
Returns all node references of all available scene description nodes with a specified name defined in...
FileExtensions supportedExtensions()
Returns all currently supported file extensions.
void update(const Rendering::ViewRef &view, const Timestamp timestamp)
Updates all scene description objects needing regularly updates.
EventNodes eventNodes_
Map holding all event nodes.
Definition: scenedescription/Manager.h:364
Lock libraryLock_
Library lock.
Definition: scenedescription/Manager.h:373
bool registerLibrary(const std::string &name)
Registers a new library.
Definition: scenedescription/Manager.h:384
std::map< SceneId, SDXSceneRef > PermanentSceneMap
Definition of a set holding permanent scene description objects.
Definition: scenedescription/Manager.h:144
void update(const std::string &library, const Rendering::ViewRef &view, const Timestamp timestamp)
Updates all scene description objects of a specified library only.
NodeRef node(const std::string &name) const
Returns the node reference of the first available scene description node with a specified name.
void registerUpdateNode(SDXUpdateNode &node)
Registers a new update node.
virtual ~Manager()
Destructs a manager.
void release()
Releases all scene library.
This class implements the base class for all nodes receiving event calls regularly.
Definition: SDXEventNode.h:25
This class implements the base class for all nodes needing update calls regularly.
Definition: SDXUpdateNode.h:25
This class implements a scoped lock object for recursive lock objects.
Definition: Lock.h:135
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
This template class implements a smart object reference which is a specialization of an ObjectRef obj...
Definition: SmartObjectRef.h:90
This class implements a timestamp.
Definition: Timestamp.h:36
size_t ObjectId
Definition of a object id.
Definition: rendering/Rendering.h:59
ButtonEvent
Definition of different device events.
Definition: SceneDescription.h:92
DescriptionType
Definition of different scene description types.
Definition: SceneDescription.h:64
std::vector< NodeRef > NodeRefs
Definition of a vector holding scene description node references.
Definition: scenedescription/Node.h:42
ButtonType
Definition of different button types.
Definition: SceneDescription.h:76
@ TYPE_PERMANENT
A scene description holding a permanent scene hierarchy allowing permanent access.
Definition: SceneDescription.h:68
size_t SceneId
Definition of a unique scene id.
Definition: SceneDescription.h:52
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15