Ocean
devices/Tracker.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_DEVICES_TRACKER_H
9 #define META_OCEAN_DEVICES_TRACKER_H
10 
11 #include "ocean/devices/Devices.h"
14 
15 #include "ocean/math/Math.h"
17 
18 namespace Ocean
19 {
20 
21 namespace Devices
22 {
23 
24 // Forward declaration.
25 class Tracker;
26 
27 /**
28  * Definition of a smart object reference for a tracker.
29  * @see Tracker.
30  * @ingroup devices
31  */
33 
34 /**
35  * This class implements the base class for all tracker devices.
36  * @ingroup devices
37  */
38 class OCEAN_DEVICES_EXPORT Tracker : virtual public Measurement
39 {
40  public:
41 
42  /**
43  * Definition of different minor device types.
44  */
45  enum TrackerType : uint32_t
46  {
47  /// Invalid tracker.
48  TRACKER_INVALID = MINOR_INVALID,
49  /// 3DOF orientation tracker.
50  TRACKER_ORIENTATION_3DOF = (1u << 0u),
51  /// 3DOF position tracker.
52  TRACKER_POSITION_3DOF = (1u << 1u),
53  /// 6DOF orientation and position tracker.
54  TRACKER_6DOF = TRACKER_ORIENTATION_3DOF | TRACKER_POSITION_3DOF,
55  /// 6DOF scene tracker.
56  SCENE_TRACKER_6DOF = (1u << 2u) | TRACKER_6DOF,
57  /// GPS tracker.
58  TRACKER_GPS = (1u << 3u),
59  /// Tracker using a magnetic measurements.
60  TRACKER_MAGNETIC = (1u << 4u),
61  /// Tracker using a visual input for their measurements.
62  TRACKER_VISUAL = (1u << 5u),
63  /// Tracker allowing to register tracking objects at runtime.
64  TRACKER_OBJECT = (1u << 6u)
65  };
66 
67  /**
68  * Definition of different tracking reference system.
69  * Beware: Both reference systems are mutual inverse.
70  */
72  {
73  /// Returning tracking values are given in the coordinate system of the device.
75  /// Returning tracking values are given in the coordinate system of the object.
76  RS_DEVICE_IN_OBJECT
77  };
78 
79  /**
80  * Definition of a tracker frequency in Hz.
81  */
82  typedef float Frequency;
83 
84  /**
85  * Definition of a sample holding one measurement values of a tracker.
86  */
87  class OCEAN_DEVICES_EXPORT TrackerSample : virtual public Measurement::Sample
88  {
89  public:
90 
91  /**
92  * Returns the reference system of the tracker.
93  * @return Tracking reference system
94  */
95  inline ReferenceSystem referenceSystem() const;
96 
97  protected:
98 
99  /**
100  * Creates a new tracker sample.
101  * @param timestamp Sample timestamp
102  * @param referenceSystem Tracking reference system used by the underlying tracker
103  * @param objectIds Object ids corresponding to different measurement units of one tracker
104  * @param metadata Optional metadata of the new sample
105  */
106  TrackerSample(const Timestamp& timestamp, const ReferenceSystem referenceSystem, const ObjectIds& objectIds, const Metadata& metadata);
107 
108  /**
109  * Creates a new tracker sample.
110  * @param timestamp Sample timestamp
111  * @param referenceSystem Tracking reference system used by the underlying tracker
112  * @param objectIds Object ids corresponding to different measurement units of one tracker
113  * @param metadata Optional metadata of the new sample
114  */
115  TrackerSample(const Timestamp& timestamp, const ReferenceSystem referenceSystem, ObjectIds&& objectIds, Metadata&& metadata);
116 
117  protected:
118 
119  /// Tracking reference system.
121  };
122 
123  /**
124  * Definition of a smart object reference for tracker samples.
125  */
127 
128  /**
129  * Definition of a callback function to subscribe for lost and found tracker events.
130  * The first parameter is the tracker producing the event.<br>
131  * The second parameter determines whether a tracker object has been found (true) or lost (false).
132  * The third parameter hold the ids of the tracker object.
133  * The last parameter hold the event timestamp.
134  */
136 
137  /**
138  * This class manages the lifetime of an event subscription for tracker object events.
139  */
140  class OCEAN_DEVICES_EXPORT TrackerObjectEventSubscription
141  {
142  friend class Tracker;
143 
144  public:
145 
146  /**
147  * Default constructor for a not active subscription.
148  */
150 
151  /**
152  * Move constructor.
153  * @param trackerObjectEventSubscription The object to be moved
154  */
155  inline TrackerObjectEventSubscription(TrackerObjectEventSubscription&& trackerObjectEventSubscription);
156 
157  /**
158  * Destructs the subscription object and unsubscribes the object event.
159  */
161 
162  /**
163  * Makes this subscription object weak so that is does not hold a reference to the actual measurement object.
164  */
165  inline void makeWeak();
166 
167  /**
168  * Explicitly releases the subscription.
169  */
170  void release();
171 
172  /**
173  * Returns whether this subscription object holds an active subscription.
174  * @return True, if so
175  */
176  explicit inline operator bool() const;
177 
178  /**
179  * Replaces the current event subscription with a new event subscription.
180  * @param trackerObjectEventSubscription The subscription object to assign
181  * @return Reference to this object
182  */
184 
185  protected:
186 
187  /**
188  * Creates an active subscription object.
189  * @param tracker The tracker to which the event subscription belongs
190  * @param subscriptionId The subscription id of the event, must be valid
191  */
192  TrackerObjectEventSubscription(const Tracker& tracker, const SubscriptionId subscriptionId);
193 
194  /**
195  * Disabled assign operator.
196  * @param trackerObjectEventSubscription The subscription object to assign
197  * @return Reference to this object
198  */
199  TrackerObjectEventSubscription& operator=(const TrackerObjectEventSubscription& trackerObjectEventSubscription) = delete;
200 
201  protected:
202 
203  /// The tracker to which the event subscription belongs.
205 
206  /// The pointer to the tracker object to which the event subscription belongs.
207  Tracker* weakTracker_ = nullptr;
208 
209  /// The subscription id.
210  SubscriptionId subscriptionId_ = invalidSubscriptionId();
211  };
212 
213  protected:
214 
215  /**
216  * Definition of a map mapping subscription ids to tracker object event callback functions.
217  */
218  typedef std::unordered_map<SubscriptionId, TrackerObjectCallback> TrackerObjectSubscriptionMap;
219 
220  public:
221 
222  /**
223  * Returns the frequency of this tracker.
224  * @return The tracker's frequency in Hz, with range (0, infinity), -1 if unknown
225  */
226  virtual Frequency frequency() const;
227 
228  /**
229  * Returns whether a specific object is currently actively tracked by this tracker.
230  * @param objectId The id of the object to be checked, must be valid
231  * @return True, if so
232  */
233  virtual bool isObjectTracked(const ObjectId& objectId) const;
234 
235  /**
236  * Subscribes a callback event function for tracker object (found or lost) events.
237  * Do not subscribe or unsubscribe from inside an event thread.
238  * @param callback The callback function receiving the event calls, must be valid
239  * @return The resulting subscription object, dispose the object to unsubscribe from the event call
240  */
242 
243  /**
244  * Returns the reference coordinate system of this tracker.
245  * Whenever a reference coordinate system is defined, all tracker samples are defined in relation to this reference coordinate system.
246  * @return The tracker's reference coordinate system, invalid if the tracker does not use a reference system
247  */
248  [[nodiscard]] virtual HomogenousMatrixD4 reference() const;
249 
250  /**
251  * Translates the tracker type to a readable string.
252  * @param trackerType The tracker type to translate
253  * @return The readable string, empty if the tracker type is unknown
254  */
255  static std::string translateTrackerType(const TrackerType trackerType);
256 
257  /**
258  * Translates the tracker type from a readable string to a value.
259  * @param trackerType The tracker type to translate
260  * @return The translated value
261  */
262  static TrackerType translateTrackerType(const std::string& trackerType);
263 
264  /**
265  * Definition of a constant as unknown frequency.
266  */
267  static constexpr Frequency unknownFrequency();
268 
269  protected:
270 
271  /**
272  * Creates a new tracker object.
273  * @param name The name of the tracker, must be valid
274  * @param type Major and minor device type of the device, must be valid
275  */
276  Tracker(const std::string& name, const DeviceType type);
277 
278  /**
279  * Destructs a tracker object.
280  */
281  ~Tracker() override;
282 
283  /**
284  * Posts a new found tracker objects event.
285  * @param objectIds The ids of all objects which have been found recently, nothing will be done if empty
286  * @param timestamp Event timestamp
287  */
288  void postFoundTrackerObjects(const ObjectIdSet& objectIds, const Timestamp& timestamp);
289 
290  /**
291  * Posts a new lost tracker objects event.
292  * @param objectIds The ids of all objects which have been lost recently, nothing will be done if empty
293  * @param timestamp Event timestamp
294  */
295  void postLostTrackerObjects(const ObjectIdSet& objectIds, const Timestamp& timestamp);
296 
297  /**
298  * Unsubscribes a tracker object event callback function.
299  * @param subscriptionId The id of the event subscription to unsubscribe
300  */
301  void unsubscribeTrackerObjectEvent(const SubscriptionId subscriptionId);
302 
303  /**
304  * Determines the ids which were not tracked in the previous iteration but tracked in the current iteration
305  * @param previousObjects The ids of all objects tracked in the previous iteration
306  * @param currentObjects The ids of all objects tracked in the current iteration
307  * @return The ids of the objects which have been found in the current iteration
308  */
309  static ObjectIdSet determineFoundObjects(const ObjectIdSet& previousObjects, const ObjectIdSet& currentObjects);
310 
311  /**
312  * Determines the ids which were tracked in the previous iteration but not tracked in the current iteration
313  * @param previousObjects The ids of all objects tracked in the previous iteration
314  * @param currentObjects The ids of all objects tracked in the current iteration
315  * @return The ids of the objects which have been lost in the current iteration
316  */
317  static ObjectIdSet determineLostObjects(const ObjectIdSet& previousObjects, const ObjectIdSet& currentObjects);
318 
319  protected:
320 
321  /// Map holding all tracker object event subscriptions.
323 
324  /// The subscription id of the next event subscription.
325  SubscriptionId nextTrackerObjectSubscriptionId_ = SubscriptionId(invalidSubscriptionId() + 1u);
326 };
327 
329 {
330  return referenceSystem_;
331 }
332 
334 {
335  *this = std::move(trackerObjectEventSubscription);
336 }
337 
339 {
340  release();
341 }
342 
344 {
345  tracker_.release();
346 }
347 
348 
349 inline Tracker::TrackerObjectEventSubscription::operator bool() const
350 {
351  return weakTracker_ != nullptr;
352 }
353 
355 {
356  return -1.0f;
357 }
358 
359 }
360 
361 }
362 
363 #endif // META_OCEAN_DEVICES_TRACKER_H
This class implements a container for callback functions.
Definition: Callback.h:3456
Definition of a class holding the major and minor device type.
Definition: devices/Device.h:62
unsigned int SubscriptionId
Definition of a subscription id for event callbacks.
Definition: devices/Device.h:151
Definition of a sample holding a measurement.
Definition: Measurement.h:67
This class implements the base class for all devices providing measurement samples.
Definition: Measurement.h:40
unsigned int ObjectId
Definition of an object id.
Definition: Measurement.h:46
std::unordered_map< std::string, Value > Metadata
Definition of an unordered map mapping keys to values.
Definition: Measurement.h:61
std::unordered_set< ObjectId > ObjectIdSet
Definition of an unordered set holding object ids.
Definition: Measurement.h:56
std::vector< ObjectId > ObjectIds
Definition of a vector holding object ids.
Definition: Measurement.h:51
This class manages the lifetime of an event subscription for tracker object events.
Definition: devices/Tracker.h:141
TrackerObjectEventSubscription & operator=(const TrackerObjectEventSubscription &trackerObjectEventSubscription)=delete
Disabled assign operator.
TrackerObjectEventSubscription & operator=(TrackerObjectEventSubscription &&trackerObjectEventSubscription)
Replaces the current event subscription with a new event subscription.
~TrackerObjectEventSubscription()
Destructs the subscription object and unsubscribes the object event.
Definition: devices/Tracker.h:338
void release()
Explicitly releases the subscription.
TrackerObjectEventSubscription()=default
Default constructor for a not active subscription.
void makeWeak()
Makes this subscription object weak so that is does not hold a reference to the actual measurement ob...
Definition: devices/Tracker.h:343
TrackerRef tracker_
The tracker to which the event subscription belongs.
Definition: devices/Tracker.h:204
TrackerObjectEventSubscription(const Tracker &tracker, const SubscriptionId subscriptionId)
Creates an active subscription object.
Definition of a sample holding one measurement values of a tracker.
Definition: devices/Tracker.h:88
TrackerSample(const Timestamp &timestamp, const ReferenceSystem referenceSystem, ObjectIds &&objectIds, Metadata &&metadata)
Creates a new tracker sample.
ReferenceSystem referenceSystem() const
Returns the reference system of the tracker.
Definition: devices/Tracker.h:328
TrackerSample(const Timestamp &timestamp, const ReferenceSystem referenceSystem, const ObjectIds &objectIds, const Metadata &metadata)
Creates a new tracker sample.
ReferenceSystem referenceSystem_
Tracking reference system.
Definition: devices/Tracker.h:120
This class implements the base class for all tracker devices.
Definition: devices/Tracker.h:39
TrackerType
Definition of different minor device types.
Definition: devices/Tracker.h:46
~Tracker() override
Destructs a tracker object.
Callback< void, const Tracker *, const bool, const ObjectIdSet &, const Timestamp & > TrackerObjectCallback
Definition of a callback function to subscribe for lost and found tracker events.
Definition: devices/Tracker.h:135
Tracker(const std::string &name, const DeviceType type)
Creates a new tracker object.
TrackerObjectSubscriptionMap trackerObjectSubscriptionMap_
Map holding all tracker object event subscriptions.
Definition: devices/Tracker.h:322
void unsubscribeTrackerObjectEvent(const SubscriptionId subscriptionId)
Unsubscribes a tracker object event callback function.
virtual bool isObjectTracked(const ObjectId &objectId) const
Returns whether a specific object is currently actively tracked by this tracker.
virtual HomogenousMatrixD4 reference() const
Returns the reference coordinate system of this tracker.
ReferenceSystem
Definition of different tracking reference system.
Definition: devices/Tracker.h:72
@ RS_OBJECT_IN_DEVICE
Returning tracking values are given in the coordinate system of the device.
Definition: devices/Tracker.h:74
static ObjectIdSet determineLostObjects(const ObjectIdSet &previousObjects, const ObjectIdSet &currentObjects)
Determines the ids which were tracked in the previous iteration but not tracked in the current iterat...
static std::string translateTrackerType(const TrackerType trackerType)
Translates the tracker type to a readable string.
std::unordered_map< SubscriptionId, TrackerObjectCallback > TrackerObjectSubscriptionMap
Definition of a map mapping subscription ids to tracker object event callback functions.
Definition: devices/Tracker.h:218
void postFoundTrackerObjects(const ObjectIdSet &objectIds, const Timestamp &timestamp)
Posts a new found tracker objects event.
static TrackerType translateTrackerType(const std::string &trackerType)
Translates the tracker type from a readable string to a value.
void postLostTrackerObjects(const ObjectIdSet &objectIds, const Timestamp &timestamp)
Posts a new lost tracker objects event.
virtual Frequency frequency() const
Returns the frequency of this tracker.
TrackerObjectEventSubscription subscribeTrackerObjectEvent(TrackerObjectCallback &&callback)
Subscribes a callback event function for tracker object (found or lost) events.
static constexpr Frequency unknownFrequency()
Definition of a constant as unknown frequency.
Definition: devices/Tracker.h:354
static ObjectIdSet determineFoundObjects(const ObjectIdSet &previousObjects, const ObjectIdSet &currentObjects)
Determines the ids which were not tracked in the previous iteration but tracked in the current iterat...
SmartObjectRef< TrackerSample, Sample > TrackerSampleRef
Definition of a smart object reference for tracker samples.
Definition: devices/Tracker.h:126
float Frequency
Definition of a tracker frequency in Hz.
Definition: devices/Tracker.h:82
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
SmartDeviceRef< Tracker > TrackerRef
Definition of a smart object reference for a tracker.
Definition: devices/Tracker.h:25
void release(T *object)
This functions allows to release a DirectShow object if it does exist.
Definition: DSObject.h:266
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15