Ocean
SDXNode.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_SDX_NODE_H
9 #define META_OCEAN_SCENEDESCRIPTION_SDX_NODE_H
10 
14 
15 #include "ocean/rendering/Engine.h"
16 #include "ocean/rendering/Object.h"
17 #include "ocean/rendering/Scene.h"
18 
19 #include <map>
20 
21 namespace Ocean
22 {
23 
24 namespace SceneDescription
25 {
26 
27 // Forward declaration.
28 class SDXNode;
29 // Forward declaration.
30 class SDXScene;
31 
32 /**
33  * Definition of a smart object reference for SDX scene description nodes.
34  * @see SDXNode, Node.
35  * @ingroup scenedescription
36  */
38 
39 /**
40  * Definition of a set holding SDX node references.
41  * @ingroup scenedescription
42  */
43 typedef std::set<SDXNodeRef> SDXNodeSet;
44 
45 /**
46  * This class implements the base class for all SDX scene description nodes.
47  * @ingroup scenedescription
48  */
49 class OCEAN_SCENEDESCRIPTION_EXPORT SDXNode : virtual public Node
50 {
51  public:
52 
53  /**
54  * Definition of a map mapping node ids to reference counters.
55  */
56  typedef std::unordered_map<NodeId, unsigned int> NodeIdMap;
57 
58  /**
59  * This class implements a node environment container.
60  */
61  class OCEAN_SCENEDESCRIPTION_EXPORT SDXEnvironment
62  {
63  public:
64 
65  /**
66  * Creates an empty environment object.
67  */
68  SDXEnvironment() = default;
69 
70  /**
71  * Creates a new environment object.
72  * @param engine Rendering engine used in the environment
73  * @param library Library defining the node
74  * @param filename Name of the file defining the node
75  * @param sceneId Unique id defining the scene
76  */
77  inline SDXEnvironment(const Rendering::EngineRef& engine, const Library* library, const std::string& filename, const SceneId sceneId);
78 
79  /**
80  * Returns the rendering engine used in the environment.
81  * @return Environment rendering engine
82  */
83  inline const Rendering::EngineRef& engine() const;
84 
85  /**
86  * Returns the name of the scene description library defining the node.
87  * @return Library name
88  */
89  inline const Library* library() const;
90 
91  /**
92  * Returns the name of the file defining the node.
93  * @return Scene filename
94  */
95  inline const std::string& filename() const;
96 
97  /**
98  * Returns the unique id of the scene defining the node originally.
99  * @return Scene id
100  */
101  inline SceneId sceneId() const;
102 
103  private:
104 
105  /// Rendering engine the node is connected with.
107 
108  /// Owner library.
109  const Library* library_ = nullptr;
110 
111  /// Name of the file defining the node.
112  std::string filename_;
113 
114  /// Unique environment scene id.
116  };
117 
118  public:
119 
120  /**
121  * Returns the scene description type of this node.
122  * @see Node::descriptionType().
123  */
125 
126  /**
127  * Returns the rendering engine used by this node.
128  * @return Rendering engine
129  */
130  inline const Rendering::EngineRef& engine() const;
131 
132  /**
133  * Returns the name of the scene description library defining this node.
134  * @return Library name
135  */
136  inline const std::string& library() const;
137 
138  /**
139  * Returns the name of the file defining this node.
140  * Beware: A node can be defined outside a file context and thus does not have a file!
141  * @return Filename
142  */
143  inline const std::string& filename() const;
144 
145  /**
146  * Returns the unique id of the scene defining this node originally.
147  * @return Unique scene id
148  */
149  inline SceneId sceneId() const;
150 
151  /**
152  * Returns the associated rendering object.
153  * @return Associated rendering object
154  */
155  virtual const Rendering::ObjectRef& renderingObject() const;
156 
157  /**
158  * Sets the name of this node.
159  * @see Node::setName().
160  */
161  void setName(const std::string& name) override;
162 
163  /**
164  * Sets or changes a specified (standard) field of this node.
165  * @param fieldName Name of the field to set
166  * @param field Field to replace the current field
167  * @return True, if succeeded
168  */
169  virtual bool setField(const std::string& fieldName, const Field& field);
170 
171  /**
172  * Sets or changes a specified standard or dynamic field of this node.
173  * @param fieldName Name of the field to set
174  * @param field Field to replace the current field
175  * @return True, if succeeded
176  */
177  virtual bool setAnyField(const std::string& fieldName, const Field& field);
178 
179  /**
180  * Returns all parent nodes of this node.
181  * @return Vector holding all father nodes of this child node
182  */
184 
185  /**
186  * Returns all ancestor nodes of this node.
187  * @return Set holding all possible ancestor nodes towards the root node.
188  */
190 
191  /**
192  * Event function to inform the node that it has been initialized and can apply all internal values to corresponding rendering objects.
193  * @param scene Rendering scene node
194  * @param timestamp Initialization timestamp
195  * @param reinitialize True, if a reinitialization has to be done
196  */
197  virtual void initialize(const Rendering::SceneRef& scene, const Timestamp timestamp, const bool reinitialize = false);
198 
199  protected:
200 
201  /**
202  * Creates a new node object.
203  * @param environment Node environment
204  */
205  explicit SDXNode(const SDXEnvironment* environment);
206 
207  /**
208  * Destructs a node object.
209  */
210  ~SDXNode() override;
211 
212  /**
213  * Internal event function to inform the node that it has been initialized and can apply all internal values to corresponding rendering objects.
214  * @param scene Rendering scene node
215  * @param timestamp Initialization timestamp
216  */
217  virtual void onInitialize(const Rendering::SceneRef& scene, const Timestamp timestamp);
218 
219  /**
220  * Event function to inform the node about a changed field.
221  * @param fieldName Name of the changed field
222  */
223  virtual void onFieldChanged(const std::string& fieldName);
224 
225  /**
226  * Registers a new parent node for this (child) node.
227  * @param parentId Id of the parent node to register
228  */
229  inline void registerParentNode(const NodeId parentId);
230 
231  /**
232  * Unregisters a parent node for this (child) node.
233  * @param parentId Id of the parent node to unregister
234  */
235  inline void unregisterParentNode(const NodeId parentId);
236 
237  /**
238  * Registers this node at a child as parent node.
239  * @param child New child node for this node
240  */
242 
243  /**
244  * Unregisters this node from a child as parent.
245  * @param child Child node to unregister
246  */
248 
249  protected:
250 
251  /// Object specifying the environment of this node.
252  const SDXEnvironment* environment_ = nullptr;
253 
254  /// Map holding all parent nodes.
256 
257  /// Corresponding rendering object.
259 
260  /// State determining whether the node has been initialized already.
261  bool initialized_ = false;
262 
263  /// Node lock.
264  mutable Lock lock_;
265 };
266 
267 inline SDXNode::SDXEnvironment::SDXEnvironment(const Rendering::EngineRef& engine, const Library* library, const std::string& filename, const SceneId sceneId) :
268  engine_(engine),
269  library_(library),
270  filename_(filename),
271  sceneId_(sceneId)
272 {
273  // nothing to do here
274 }
275 
277 {
278  return engine_;
279 }
280 
282 {
283  return library_;
284 }
285 
286 inline const std::string& SDXNode::SDXEnvironment::filename() const
287 {
288  return filename_;
289 }
290 
292 {
293  return sceneId_;
294 }
295 
297 {
298  ocean_assert(environment_);
299  return environment_->engine();
300 }
301 
302 inline const std::string& SDXNode::library() const
303 {
304  ocean_assert(environment_);
305  ocean_assert(environment_->library());
306 
307  return environment_->library()->name();
308 }
309 
310 inline const std::string& SDXNode::filename() const
311 {
312  ocean_assert(environment_);
313  return environment_->filename();
314 }
315 
316 inline SceneId SDXNode::sceneId() const
317 {
318  ocean_assert(environment_);
319  return environment_->sceneId();
320 }
321 
322 inline void SDXNode::registerParentNode(const NodeId parentId)
323 {
324  const ScopedLock scopedLock(lock_);
325 
326  ++parents_.insert(std::make_pair(parentId, 0)).first->second;
327 }
328 
329 inline void SDXNode::unregisterParentNode(const NodeId parentId)
330 {
331  const ScopedLock scopedLock(lock_);
332 
333  NodeIdMap::iterator iNode = parents_.find(parentId);
334  ocean_assert(iNode != parents_.end());
335 
336  ocean_assert(iNode->second != 0u);
337 
338  if (--iNode->second == 0u)
339  {
340  parents_.erase(iNode);
341  }
342 }
343 
344 }
345 
346 }
347 
348 #endif // META_OCEAN_SCENEDESCRIPTION_SDX_NODE_H
This class implements a recursive lock object.
Definition: Lock.h:31
This class is the base class for all scene description fields.
Definition: Field.h:36
This class is the base class for all scene description libraries.
Definition: scenedescription/Library.h:37
const std::string & name() const
Returns the name of the library.
Definition: scenedescription/Library.h:196
This class is the base class for all scene description nodes.
Definition: scenedescription/Node.h:49
This class implements a node environment container.
Definition: SDXNode.h:62
SDXEnvironment()=default
Creates an empty environment object.
const std::string & filename() const
Returns the name of the file defining the node.
Definition: SDXNode.h:286
SceneId sceneId() const
Returns the unique id of the scene defining the node originally.
Definition: SDXNode.h:291
const Library * library() const
Returns the name of the scene description library defining the node.
Definition: SDXNode.h:281
const Rendering::EngineRef & engine() const
Returns the rendering engine used in the environment.
Definition: SDXNode.h:276
std::string filename_
Name of the file defining the node.
Definition: SDXNode.h:112
Rendering::EngineRef engine_
Rendering engine the node is connected with.
Definition: SDXNode.h:106
This class implements the base class for all SDX scene description nodes.
Definition: SDXNode.h:50
const Rendering::EngineRef & engine() const
Returns the rendering engine used by this node.
Definition: SDXNode.h:296
virtual void initialize(const Rendering::SceneRef &scene, const Timestamp timestamp, const bool reinitialize=false)
Event function to inform the node that it has been initialized and can apply all internal values to c...
NodeIdMap parents_
Map holding all parent nodes.
Definition: SDXNode.h:255
const std::string & library() const
Returns the name of the scene description library defining this node.
Definition: SDXNode.h:302
void setName(const std::string &name) override
Sets the name of this node.
void registerThisNodeAsParent(const SDXNodeRef &child)
Registers this node at a child as parent node.
std::unordered_map< NodeId, unsigned int > NodeIdMap
Definition of a map mapping node ids to reference counters.
Definition: SDXNode.h:56
NodeRefs parentNodes() const
Returns all parent nodes of this node.
void unregisterParentNode(const NodeId parentId)
Unregisters a parent node for this (child) node.
Definition: SDXNode.h:329
SDXNode(const SDXEnvironment *environment)
Creates a new node object.
DescriptionType descriptionType() const override
Returns the scene description type of this node.
void unregisterThisNodeAsParent(const SDXNodeRef &child)
Unregisters this node from a child as parent.
virtual void onInitialize(const Rendering::SceneRef &scene, const Timestamp timestamp)
Internal event function to inform the node that it has been initialized and can apply all internal va...
Rendering::ObjectRef renderingObject_
Corresponding rendering object.
Definition: SDXNode.h:258
const std::string & filename() const
Returns the name of the file defining this node.
Definition: SDXNode.h:310
virtual void onFieldChanged(const std::string &fieldName)
Event function to inform the node about a changed field.
SceneId sceneId() const
Returns the unique id of the scene defining this node originally.
Definition: SDXNode.h:316
const SDXEnvironment * environment_
Object specifying the environment of this node.
Definition: SDXNode.h:252
SDXNodeSet ancestorNodes() const
Returns all ancestor nodes of this node.
Lock lock_
Node lock.
Definition: SDXNode.h:264
void registerParentNode(const NodeId parentId)
Registers a new parent node for this (child) node.
Definition: SDXNode.h:322
virtual bool setField(const std::string &fieldName, const Field &field)
Sets or changes a specified (standard) field of this node.
virtual bool setAnyField(const std::string &fieldName, const Field &field)
Sets or changes a specified standard or dynamic field of this node.
virtual const Rendering::ObjectRef & renderingObject() const
Returns the associated rendering object.
~SDXNode() override
Destructs a node object.
This class implements a scoped lock object for recursive lock objects.
Definition: Lock.h:135
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
DescriptionType
Definition of different scene description types.
Definition: SceneDescription.h:64
std::set< SDXNodeRef > SDXNodeSet
Definition of a set holding SDX node references.
Definition: SDXNode.h:43
SmartObjectRef< SDXNode, Node > SDXNodeRef
Definition of a smart object reference for SDX scene description nodes.
Definition: SDXNode.h:30
std::vector< NodeRef > NodeRefs
Definition of a vector holding scene description node references.
Definition: scenedescription/Node.h:42
size_t NodeId
Definition of a object id.
Definition: SceneDescription.h:41
constexpr SceneId invalidSceneId
Definition of an invalid scene id.
Definition: SceneDescription.h:57
size_t SceneId
Definition of a unique scene id.
Definition: SceneDescription.h:52
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15