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