Ocean
AnchoredContentManager.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_INTERACTION_EXPERIENCES_ANCHORED_CONTENT_MANAGER_H
9 #define META_OCEAN_INTERACTION_EXPERIENCES_ANCHORED_CONTENT_MANAGER_H
10 
13 
14 #include "ocean/base/Lock.h"
15 #include "ocean/base/Triple.h"
16 
19 
20 #include "ocean/rendering/Scene.h"
22 
23 #include <functional>
24 
25 namespace Ocean
26 {
27 
28 namespace Interaction
29 {
30 
31 namespace Experiences
32 {
33 
34 /**
35  * This class implements a manager for anchored content and allows to simplify creating experiences intensively using virtual content anchored based on 6-DOF trackers.
36  * The manager handles visibility of anchored content and updates the underlying rendering transformations to ensure that virtual content shows up at the intended location.
37  * @ingroup interactionexperiences
38  */
39 class OCEAN_INTERACTION_EXPERIENCES_EXPORT AnchoredContentManager
40 {
41  public:
42 
43  /**
44  * Definition of an id defining anchored content.
45  */
46  typedef unsigned int ContentId;
47 
48  /**
49  * Returns an invalid content id.
50  * @return Invalid content id
51  */
52  static constexpr inline ContentId invalidContentId();
53 
54  /**
55  * This class implements a container for anchored content.
56  * Anchored content is mainly defined by the actual rendering object (e.g., a geometry/shape/mesh etc.), the tracker used to anchor the content, and the tracker's object id associated with the content.
57  * Further, a visibility radius specifies within which radius the anchored content is visible.<br>
58  * If the anchor leaves the engagement radius, the content will be removed automatically from the manger to improve resource management.
59  */
60  class OCEAN_INTERACTION_EXPERIENCES_EXPORT ContentObject
61  {
62  friend class AnchoredContentManager;
63 
64  public:
65 
66  /**
67  * Returns the unique id of this content object.
68  * @return The object's unique content id
69  */
70  inline ContentId contentId() const;
71 
72  /**
73  * Returns the rendering node holding the content to be rendered.
74  * This object needs to be valid.
75  * @return The object's rendering node
76  */
77  inline Rendering::NodeRef renderingObjectNode() const;
78 
79  /**
80  * Returns whether the anchor is currently tracked.
81  * This object needs to be valid.
82  * @return True, if so
83  */
84  inline bool isTracked() const;
85 
86  /**
87  * Returns the current square distance to the anchor.
88  * This object needs to be valid.
89  * @return The square distance to the anchor when the anchor was tracked the last time, with range [0, infinity), -1 if unknown
90  */
91  inline Scalar sqrDistance() const;
92 
93  /**
94  * Returns the transformation between object and world.
95  * This object needs to be valid.
96  * @return The object's transformation
97  */
99 
100  /**
101  * Updates or changes the rendering node holding the content to be rendered
102  * @param renderingObjectNode The new rendering object node, must be valid
103  */
104  void setRenderingObjectNode(Rendering::NodeRef renderingObjectNode);
105 
106  /**
107  * Returns whether this content object is valid.
108  * @return True, if so
109  */
110  inline bool isValid() const;
111 
112  protected:
113 
114  /**
115  * Creates a new content object.
116  * @param renderingObjectNode The rendering object to take care of, will be added to the manager's rendering scene, must be valid
117  * @param devicesTracker The 6-DOF tracker which provides the anchor to which the rendering object is connected, must be valid
118  * @param devicesObjectId The tracker object id of the anchor to which the rendering object is connected, must be valid
119  * @param contentId The object's unique content id, must be valid
120  * @param visibilityRadius The radius in which the new content will be visible (maximal distance between anchor and user), in meter, with range (0, infinity)
121  * @param engagementRadius The radius defining the engagement area of the new content, whenever the user leaves this area, the content will be removed from this manager automatically, with range [visibilityRadius, infinity)
122  */
123  ContentObject(const Rendering::NodeRef& renderingObjectNode, const Devices::Tracker6DOFRef& devicesTracker, const Devices::Tracker6DOF::ObjectId& devicesObjectId, const ContentId contentId, const Scalar visibilityRadius, const Scalar engagementRadius);
124 
125  protected:
126 
127  /// The object's unique content id.
128  ContentId contentId_ = invalidContentId();
129 
130  /// The rendering node holding the content to be rendered.
132 
133  /// The transform between scene and rendering node which will receive constant transformation updates from the tracker.
135 
136  /// The tracker providing the pose updates for the anchor.
138 
139  /// The tracker object id of the anchor.
141 
142  /// The radius in which the content is visible, in meter.
143  Scalar visibilityRadius_ = -1;
144 
145  /// The radius in which the content is managed by this owning manager.
146  Scalar engagementRadius_ = -1;
147 
148  /// True, if the content is currently tracked (e.g., visible if the anchor is based on a visual tracker); False, if the content is currently not tracked.
149  bool isTracked_ = false;
150 
151  /// The current square distance to the anchor.
152  Scalar sqrDistance_ = -1;
153 
154  /// The content's lock.
155  mutable Lock lock_;
156  };
157 
158  /**
159  * Definition of a shared content object.
160  */
161  typedef std::shared_ptr<ContentObject> SharedContentObject;
162 
163  /**
164  * Definition of an unordered set holding content objects.
165  */
166  typedef std::unordered_set<SharedContentObject> SharedContentObjectSet;
167 
168  /**
169  * Definition of a callback function for removed object events.
170  */
172 
173  protected:
174 
175  /**
176  * Definition of an unordered map mapping content ids to content objects.
177  */
178  typedef std::unordered_map<ContentId, SharedContentObject> ContentMap;
179 
180  /**
181  * Definition of an unordered multimap mapping object ids to content objects.
182  */
183  typedef std::unordered_multimap<Devices::Measurement::ObjectId, SharedContentObject> ObjectIdToContentObjectMultiMap;
184 
185  /**
186  * Definition of an unordered map mapping trackers to content objects.
187  */
188  typedef std::unordered_map<const Devices::Tracker6DOF*, SharedContentObject> TrackerToContentObjectMap;
189 
190  /**
191  * Definition of pair combining a subscription id and a usage counter.
192  */
193  typedef std::pair<Devices::Tracker::TrackerObjectEventSubscription, unsigned int> SubscriptionPair;
194 
195  /**
196  * Definition of an unordered map mapping trackers to subscription pairs.
197  */
198  typedef std::unordered_map<Devices::Tracker6DOF*, SubscriptionPair> SubscriptionMap;
199 
200  public:
201 
202  /**
203  * Creates a new manager object.
204  * Experiences can create several independent manager if necessary.<br>
205  * A manager needs to be initialized before it can be used.
206  * @see initialize().
207  */
209 
210  /**
211  * Destructs the manager and releases all resources.
212  * @see release().
213  */
215 
216  /**
217  * Initializes the manager.
218  * @param removedContentCallbackFunction The callback function which will be called whenever the manager removes a content object e.g., because the object left the engagement area, must be valid
219  * @param scene The rendering scene to which the manager will add all rendering elements of the content objects, must be valid
220  * @return True, if succeeded
221  */
222  bool initialize(RemovedContentCallbackFunction removedContentCallbackFunction, const Rendering::SceneRef& scene);
223 
224  /**
225  * Releases this manager explicitly before.
226  */
227  void release();
228 
229  /**
230  * Adds a new anchored content so that the manager will take care of visibility and pose updates.
231  * @param renderingObjectNode The rendering object to take care of, will be added to the manager's rendering scene, must be valid
232  * @param devicesTracker The 6-DOF tracker which provides the anchor to which the rendering object is connected, must be valid
233  * @param devicesObjectId The tracker object id of the anchor to which the rendering object is connected, must be valid
234  * @param visibilityRadius The radius in which the new content will be visible (maximal distance between anchor and user), in meter, with range (0, infinity)
235  * @param engagementRadius The radius defining the engagement area of the new content, whenever the user leaves this area, the content will be removed from this manager automatically, with range [visibilityRadius, infinity)
236  * @return The id of the new content
237  */
238  ContentId addContent(const Rendering::NodeRef& renderingObjectNode, const Devices::Tracker6DOFRef& devicesTracker, const Devices::Tracker6DOF::ObjectId& devicesObjectId, const Scalar visibilityRadius, const Scalar engagementRadius);
239 
240  /**
241  * Removes a content object from this manager.
242  * The manager will remove the associated rendering object from the scene and will not take care of visibility and pose updates anymore.
243  * @return True, if succeeded
244  */
245  bool removeContent(const ContentId contentId);
246 
247  /**
248  * Removes all content object currently managed by this manager.
249  * The manager will remove all associated rendering objects from the scene and will not take care of visibility and pose updates anymore.
250  * @return True, if succeeded
251  */
253 
254  /**
255  * Returns a specific content.
256  * @param contentId The id of the content to return, must be valid
257  * @return The resulting content, nullptr if the content is currently not managed
258  */
259  SharedContentObject content(const ContentId contentId) const;
260 
261  /**
262  * Returns all contents which are currently visible.
263  * @return All visible content objects
264  */
266 
267  /**
268  * Returns all contents which is closer than a given distance.
269  * @param maxSqrDistance The maximal square distance to the anchor so that the object will be returned, with range [0, infinity)
270  * @return All visible content objects closer than the specified distance
271  */
272  SharedContentObjectSet closeContents(const Scalar maxSqrDistance) const;
273 
274  /**
275  * Pre update event function in which all visibility and pose updates will be handled.
276  * @see Library::preUpdate().
277  */
278  Timestamp preUpdate(const Rendering::EngineRef& engine, const Rendering::ViewRef& view, const Timestamp timestamp);
279 
280  /**
281  * Returns the scene to which the rendering objects of new content will be added.
282  * @return The manager's scene, invalid if not yet initialized
283  */
284  inline Rendering::SceneRef scene() const;
285 
286  /**
287  * Returns whether this manager is initialized and ready to use.
288  * @return True, if so
289  */
290  explicit inline operator bool() const;
291 
292  protected:
293 
294  /**
295  * Internal event function for tracker object events.
296  * @param tracker The sender of the event, will be valid
297  * @param found True, if all provided objects have been found at the specified timestamp; False, if all provided objects have been lost
298  * @param objectIds The ids of all tracker objects for which the found/lost event is intended
299  * @param timestamp The event timestamp
300  */
301  void onTrackerObjects(const Devices::Tracker* tracker, const bool found, const Devices::Measurement::ObjectIdSet& objectIds, const Timestamp& timestamp);
302 
303  protected:
304 
305  /// The map containing the currently managed content objects.
307 
308  /// The multi map mapping tracker object ids to content objects.
310 
311  /// The tracker mapping trackers to content objects.
313 
314  /// The map mapping trackers to subscription ids.
316 
317  /// The counter for unique content ids.
318  unsigned int contentIdCounter_ = 0u;
319 
320  /// The scene to which all rendering nodes will be added.
322 
323  /// The callback function for removed content events.
325 
326  /// The manager's lock.
327  mutable Lock lock_;
328 };
329 
331 {
332  return ContentId(-1);
333 }
334 
336 {
337  return contentId_;
338 }
339 
341 {
342  ocean_assert(isValid());
343 
344  return renderingObjectNode_;
345 }
346 
348 {
349  ocean_assert(isValid());
350 
351  return isTracked_;
352 }
353 
355 {
356  ocean_assert(isValid());
357 
358  return sqrDistance_;
359 }
360 
362 {
363  return bool(renderingObjectNode_);
364 }
365 
367 {
368  return renderingScene_;
369 }
370 
371 inline AnchoredContentManager::operator bool() const
372 {
374 }
375 
376 } // namespace Experiences
377 
378 } // namespace Interaction
379 
380 } // namespace Ocean
381 
382 #endif // META_OCEAN_INTERACTION_EXPERIENCES_ANCHORED_CONTENT_MANAGER_H
unsigned int ObjectId
Definition of an object id.
Definition: Measurement.h:46
std::unordered_set< ObjectId > ObjectIdSet
Definition of an unordered set holding object ids.
Definition: Measurement.h:56
This class implements the base class for all tracker devices.
Definition: devices/Tracker.h:39
This class implements a container for anchored content.
Definition: AnchoredContentManager.h:61
Lock lock_
The content's lock.
Definition: AnchoredContentManager.h:155
void setRenderingObjectNode(Rendering::NodeRef renderingObjectNode)
Updates or changes the rendering node holding the content to be rendered.
Devices::Tracker6DOF::ObjectId devicesObjectId_
The tracker object id of the anchor.
Definition: AnchoredContentManager.h:140
Rendering::NodeRef renderingObjectNode_
The rendering node holding the content to be rendered.
Definition: AnchoredContentManager.h:131
bool isValid() const
Returns whether this content object is valid.
Definition: AnchoredContentManager.h:361
Rendering::TransformRef renderingAnchorTransform_
The transform between scene and rendering node which will receive constant transformation updates fro...
Definition: AnchoredContentManager.h:134
Scalar sqrDistance() const
Returns the current square distance to the anchor.
Definition: AnchoredContentManager.h:354
Devices::Tracker6DOFRef devicesTracker_
The tracker providing the pose updates for the anchor.
Definition: AnchoredContentManager.h:137
Rendering::NodeRef renderingObjectNode() const
Returns the rendering node holding the content to be rendered.
Definition: AnchoredContentManager.h:340
bool isTracked() const
Returns whether the anchor is currently tracked.
Definition: AnchoredContentManager.h:347
ContentObject(const Rendering::NodeRef &renderingObjectNode, const Devices::Tracker6DOFRef &devicesTracker, const Devices::Tracker6DOF::ObjectId &devicesObjectId, const ContentId contentId, const Scalar visibilityRadius, const Scalar engagementRadius)
Creates a new content object.
ContentId contentId_
The object's unique content id.
Definition: AnchoredContentManager.h:128
ContentId contentId() const
Returns the unique id of this content object.
Definition: AnchoredContentManager.h:335
HomogenousMatrix4 world_T_object() const
Returns the transformation between object and world.
This class implements a manager for anchored content and allows to simplify creating experiences inte...
Definition: AnchoredContentManager.h:40
std::shared_ptr< ContentObject > SharedContentObject
Definition of a shared content object.
Definition: AnchoredContentManager.h:161
unsigned int ContentId
Definition of an id defining anchored content.
Definition: AnchoredContentManager.h:46
bool initialize(RemovedContentCallbackFunction removedContentCallbackFunction, const Rendering::SceneRef &scene)
Initializes the manager.
bool removeContent(const ContentId contentId)
Removes a content object from this manager.
static constexpr ContentId invalidContentId()
Returns an invalid content id.
Definition: AnchoredContentManager.h:330
TrackerToContentObjectMap trackerToContentObjectMap_
The tracker mapping trackers to content objects.
Definition: AnchoredContentManager.h:312
std::unordered_map< Devices::Tracker6DOF *, SubscriptionPair > SubscriptionMap
Definition of an unordered map mapping trackers to subscription pairs.
Definition: AnchoredContentManager.h:198
SharedContentObjectSet closeContents(const Scalar maxSqrDistance) const
Returns all contents which is closer than a given distance.
bool removeAllContent()
Removes all content object currently managed by this manager.
Timestamp preUpdate(const Rendering::EngineRef &engine, const Rendering::ViewRef &view, const Timestamp timestamp)
Pre update event function in which all visibility and pose updates will be handled.
~AnchoredContentManager()
Destructs the manager and releases all resources.
ContentMap contentMap_
The map containing the currently managed content objects.
Definition: AnchoredContentManager.h:306
Lock lock_
The manager's lock.
Definition: AnchoredContentManager.h:327
std::pair< Devices::Tracker::TrackerObjectEventSubscription, unsigned int > SubscriptionPair
Definition of pair combining a subscription id and a usage counter.
Definition: AnchoredContentManager.h:193
Rendering::SceneRef renderingScene_
The scene to which all rendering nodes will be added.
Definition: AnchoredContentManager.h:321
SharedContentObject content(const ContentId contentId) const
Returns a specific content.
std::unordered_set< SharedContentObject > SharedContentObjectSet
Definition of an unordered set holding content objects.
Definition: AnchoredContentManager.h:166
RemovedContentCallbackFunction removedContentCallbackFunction_
The callback function for removed content events.
Definition: AnchoredContentManager.h:324
AnchoredContentManager()
Creates a new manager object.
std::unordered_map< const Devices::Tracker6DOF *, SharedContentObject > TrackerToContentObjectMap
Definition of an unordered map mapping trackers to content objects.
Definition: AnchoredContentManager.h:188
std::unordered_multimap< Devices::Measurement::ObjectId, SharedContentObject > ObjectIdToContentObjectMultiMap
Definition of an unordered multimap mapping object ids to content objects.
Definition: AnchoredContentManager.h:183
ContentId addContent(const Rendering::NodeRef &renderingObjectNode, const Devices::Tracker6DOFRef &devicesTracker, const Devices::Tracker6DOF::ObjectId &devicesObjectId, const Scalar visibilityRadius, const Scalar engagementRadius)
Adds a new anchored content so that the manager will take care of visibility and pose updates.
ObjectIdToContentObjectMultiMap objectIdToContentObjectMultiMap_
The multi map mapping tracker object ids to content objects.
Definition: AnchoredContentManager.h:309
void release()
Releases this manager explicitly before.
Rendering::SceneRef scene() const
Returns the scene to which the rendering objects of new content will be added.
Definition: AnchoredContentManager.h:366
std::unordered_map< ContentId, SharedContentObject > ContentMap
Definition of an unordered map mapping content ids to content objects.
Definition: AnchoredContentManager.h:178
SharedContentObjectSet visibleContents() const
Returns all contents which are currently visible.
SubscriptionMap subscriptionMap_
The map mapping trackers to subscription ids.
Definition: AnchoredContentManager.h:315
void onTrackerObjects(const Devices::Tracker *tracker, const bool found, const Devices::Measurement::ObjectIdSet &objectIds, const Timestamp &timestamp)
Internal event function for tracker object events.
std::function< void(SharedContentObjectSet &&)> RemovedContentCallbackFunction
Definition of a callback function for removed object events.
Definition: AnchoredContentManager.h:171
This class implements a recursive lock object.
Definition: Lock.h:31
This class implements a timestamp.
Definition: Timestamp.h:36
unsigned int sqrDistance(const char first, const char second)
Returns the square distance between two values.
Definition: base/Utilities.h:1089
float Scalar
Definition of a scalar type.
Definition: Math.h:128
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15