Ocean
Loading...
Searching...
No Matches
Measurement.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_MEASUREMENT_H
9#define META_OCEAN_DEVICES_MEASUREMENT_H
10
13
14#include "ocean/base/Callback.h"
15#include "ocean/base/Value.h"
16
17namespace Ocean
18{
19
20namespace Devices
21{
22
23// Forward declaration.
24class Measurement;
25
26/**
27 * Definition of a smart object reference for a measurement.
28 * @see Measurement.
29 * @ingroup devices
30 */
32
33/**
34 * This class implements the base class for all devices providing measurement samples.
35 * Each measurement holds a container holding several most recent samples provided by the device.<br>
36 * Thus, depending on the number of stored samples a specific sample can be requested.<br>
37 * @ingroup devices
38 */
39class OCEAN_DEVICES_EXPORT Measurement : virtual public Device
40{
41 public:
42
43 /**
44 * Definition of an object id.
45 */
46 typedef unsigned int ObjectId;
47
48 /**
49 * Definition of a vector holding object ids.
50 */
51 typedef std::vector<ObjectId> ObjectIds;
52
53 /**
54 * Definition of an unordered set holding object ids.
55 */
56 typedef std::unordered_set<ObjectId> ObjectIdSet;
57
58 /**
59 * Definition of an unordered map mapping keys to values.
60 */
61 typedef std::unordered_map<std::string, Value> Metadata;
62
63 /**
64 * Definition of a sample holding a measurement.
65 */
66 class OCEAN_DEVICES_EXPORT Sample
67 {
68 friend class ObjectRef<Sample>;
69
70 public:
71
72 /**
73 * Returns the sample timestamp.
74 * @return Sample timestamp
75 */
76 inline const Timestamp& timestamp() const;
77
78 /**
79 * Returns the sample object ids specifying possible different measurement units.
80 * @return Object ids
81 */
82 inline const ObjectIds& objectIds() const;
83
84 /**
85 * Returns the metadata of this sample.
86 * @return The sample's meta data
87 */
88 inline const Metadata& metadata() const;
89
90 protected:
91
92 /**
93 * Creates a new measurement sample.
94 * @param timestamp Sample timestamp
95 * @param objectIds Object ids corresponding to different measurement units of one tracker
96 * @param metadata Optional metadata of the new sample
97 */
98 Sample(const Timestamp& timestamp, const ObjectIds& objectIds, const Metadata& metadata = Metadata());
99
100 /**
101 * Creates a new measurement sample.
102 * @param timestamp Sample timestamp
103 * @param objectIds Object ids corresponding to different measurement units of one tracker
104 * @param metadata Optional metadata of the new sample
105 */
106 Sample(const Timestamp& timestamp, ObjectIds&& objectIds, Metadata&& metadata = Metadata());
107
108 /**
109 * Destructs a sample.
110 */
111 virtual ~Sample();
112
113 protected:
114
115 /// Sample timestamp
117
118 /// Measurement unit object ids.
120
121 /// The metadata of this sample.
123 };
124
125 /**
126 * Definition of an object reference for samples.
127 */
129
130 /**
131 * Definition of a callback function to subscribe for new measurement sample events.
132 * The first parameter is the Measurement object sending the sample
133 * The second parameter is the sample
134 */
136
137 /**
138 * Definition of individual interpolation strategies for samples.
139 */
141 {
142 /// An invalid strategy.
144 /// The sample with nearest/closest timestamp is used.
146 /// The sample is interpolated based on two samples.
147 IS_TIMESTAMP_INTERPOLATE
148 };
149
150 /**
151 * This class manages the lifetime of an event subscription for sample events.
152 */
153 class OCEAN_DEVICES_EXPORT SampleEventSubscription
154 {
155 friend class Measurement;
156
157 public:
158
159 /**
160 * Default constructor for a not active subscription.
161 */
163
164 /**
165 * Move constructor.
166 * @param sampleEventSubscription The object to be moved
167 */
168 inline SampleEventSubscription(SampleEventSubscription&& sampleEventSubscription);
169
170 /**
171 * Destructs the subscription object and unsubscribes the object event.
172 */
174
175 /**
176 * Makes this subscription object weak so that is does not hold a reference to the actual measurement object.
177 */
178 inline void makeWeak();
179
180 /**
181 * Explicitly releases the subscription.
182 */
183 void release();
184
185 /**
186 * Returns whether this subscription object holds an active subscription.
187 * @return True, if so
188 */
189 explicit inline operator bool() const;
190
191 /**
192 * Replaces the current event subscription with a new event subscription.
193 * @param sampleEventSubscription The subscription object to assign
194 * @return Reference to this object
195 */
197
198 protected:
199
200 /**
201 * Creates an active subscription object.
202 * @param measurement The measurement object to which the event subscription belongs
203 * @param subscriptionId The subscription id of the event, must be valid
204 */
205 SampleEventSubscription(const Measurement& measurement, const SubscriptionId subscriptionId);
206
207 /**
208 * Disabled assign operator.
209 * @param sampleEventSubscription The subscription object to assign
210 * @return Reference to this object
211 */
212 SampleEventSubscription& operator=(const SampleEventSubscription& sampleEventSubscription) = delete;
213
214 protected:
215
216 /// The measurement object to which the event subscription belongs.
218
219 /// The pointer to the measurement object to which the event subscription belongs.
220 Measurement* weakMeasurement_ = nullptr;
221
222 /// The subscription id.
223 SubscriptionId subscriptionId_ = invalidSubscriptionId();
224 };
225
226 protected:
227
228 /**
229 * Definition of a map holding the most recent samples
230 */
231 typedef std::map<Timestamp, SampleRef> SampleMap;
232
233 /**
234 * This class implements a helper class to simplify the mapping between internal object ids (of the actual tracking implementation) and extern object ids (of the device system).
235 * @tparam TInternalId The data type of the internal object id.
236 */
237 template <typename TInternalId>
239 {
240 protected:
241
242 /**
243 * Definition of an unordered map mapping internal object ids to external object ids.
244 */
245 typedef std::unordered_map<TInternalId, ObjectId> InternalObjectIdMap;
246
247 /**
248 * Definition of an unordered map mapping external object ids to internal object ids.
249 */
250 typedef std::unordered_map<ObjectId, TInternalId> ExternalObjectIdMap;
251
252 public:
253
254 /**
255 * Default constructor.
256 */
257 explicit inline ObjectMapper(Measurement& owner);
258
259 /**
260 * Returns whether this mapping object holds a specific internal object.
261 * @param internalObjectId The id of the internal object to be checked
262 * @return True, if so
263 */
264 bool hasInternalObject(const TInternalId& internalObjectId) const;
265
266 /**
267 * Returns whether this mapping object holds a specific external object.
268 * @param externalObjectId The id of the external object to be checked, must be valid
269 * @return True, if so
270 */
271 bool hasExternalObject(const Measurement::ObjectId& externalObjectId) const;
272
273 /**
274 * Adds a new internal pattern id.
275 * @param internalObjectId The new internal object id to add
276 * @param description The description of the new pattern
277 * @return The corresponding external object id
278 */
279 ObjectId newInternalObjectId(const TInternalId& internalObjectId, const std::string& description);
280
281 /**
282 * Removes a mapping between internal and external object.
283 * @param internalObjectId The id of the internal object for which the mapping will be removed
284 */
285 void removeInternalObject(const TInternalId& internalObjectId);
286
287 /**
288 * Converts the tracker's internal object id to an external object id.
289 * @param internalObjectId The internal object id to translate
290 * @return The resulting external object id
291 */
292 ObjectId externalObjectIdFromInternalObjectId(const TInternalId& internalObjectId) const;
293
294 /**
295 * Converts an external object id to the tracker tracker's internal object id.
296 * @param externalObjectId The external object id to translate
297 * @param invalidInternalId An invalid internal id
298 * @return The resulting internal object id
299 */
300 TInternalId internalObjectIdFromExternalObjectId(const ObjectId externalObjectId, const TInternalId& invalidInternalId) const;
301
302 protected:
303
304 /**
305 * Disabled copy constructor.
306 * @param objectMapper Object which would be copied
307 */
308 ObjectMapper(const ObjectMapper& objectMapper) = delete;
309
310 /**
311 * Disabled copy operator.
312 * @param objectMapper Object which would be copied
313 * @return Reference to this object
314 */
315 ObjectMapper& operator=(const ObjectMapper& objectMapper) = delete;
316
317 protected:
318
319 /// The owner of this mapper.
321
322 /// The map mapping internal object ids to external object ids.
324
325 /// The map mapping external object ids to internal object ids.
327 };
328
329 /**
330 * Definition of an unordered map mapping descriptions to unique object ids.
331 */
332 typedef std::unordered_map<std::string, ObjectId> ObjectDescriptionToIdMap;
333
334 /**
335 * Definition of an unordered map mapping unique object ids to descriptions.
336 */
337 typedef std::unordered_map<ObjectId, std::string> ObjectIdToDescriptionMap;
338
339 /**
340 * Definition of a map mapping subscription ids to event sample callback functions.
341 */
342 typedef std::unordered_map<SubscriptionId, SampleCallback> SampleSubscriptionMap;
343
344 public:
345
346 /**
347 * Returns the capacity of the internal sample container.
348 * @return The sample container's capacity, with range [2, infinity)
349 * @see setSampleCapacity().
350 */
351 inline size_t sampleCapacity() const;
352
353 /**
354 * Sets the capacity of the internal sample container.
355 * @param capacity The number maximal number of samples to be stored, with range [2, infinity)
356 * @return True, if succeeded
357 */
358 bool setSampleCapacity(const size_t capacity);
359
360 /**
361 * Returns the most recent sample.
362 * @return Most recent sample as object reference
363 */
364 virtual SampleRef sample() const;
365
366 /**
367 * Returns the sample with a specific timestamp.
368 * If no sample exists with the given timestamp the most recent sample is returned.
369 * @param timestamp The timestamp of the sample to be returned
370 * @return Requested sample
371 */
372 virtual SampleRef sample(const Timestamp timestamp) const;
373
374 /**
375 * Returns the sample best matching with a specified timestamp.
376 * In case, the given timestamp does not fit to an existing sample, the resulting sample will be based on the specified interpolation strategy.
377 * @param timestamp The timestamp for which a sample will be determined, must be valid
378 * @param interpolationStrategy The interpolation strategy to be applied in case the timestamp does not fit with an existing sample
379 * @return The resulting sample, invalid if no sample could be determined
380 */
381 virtual SampleRef sample(const Timestamp& timestamp, const InterpolationStrategy interpolationStrategy) const;
382
383 /**
384 * Subscribes a callback event function for new measurement sample events.
385 * Do not subscribe or unsubscribe from inside an event thread.
386 * @param callback The callback function receiving the event calls, must be valid
387 * @return The resulting subscription object, dispose the object to unsubscribe from the event call
388 */
390
391 /**
392 * Returns the object id for an object description.
393 * @param description The description for which the object id will be returned
394 * @return The object id, an invalid id if the description is not known
395 * @see objectDescriptions().
396 */
397 ObjectId objectId(const std::string& description) const;
398
399 /**
400 * Returns descriptions of all objects currently available.
401 * @return All object descriptions
402 * @see objectId().
403 */
405
406 /**
407 * Returns the description of one object of this measurement.
408 * @param objectId The id of the object for which the description will be returned, must be valid
409 * @return The description of the object, empty if the object is unknown
410 */
411 std::string objectDescription(const ObjectId objectId) const;
412
413 /**
414 * Returns an invalid object id.
415 * @return Id of an invalid object
416 */
417 static constexpr ObjectId invalidObjectId();
418
419 protected:
420
421 /**
422 * Creates a new measurement object.
423 * @param name The name of the measurement device
424 * @param type Major and minor device type of the device
425 */
426 Measurement(const std::string& name, const DeviceType type);
427
428 /**
429 * Destructs a measurement object.
430 */
431 ~Measurement() override;
432
433 /**
434 * Posts a new measurement sample.
435 * @param newSample New sample to post
436 */
437 void postNewSample(const SampleRef& newSample);
438
439 /**
440 * Creates a unique object id for a new object (e.g., a tracking object like an image, a marker, or a location).
441 * @param description The description of the new object, must be valid
442 * @return The unique id which is unique across all devices
443 */
444 ObjectId addUniqueObjectId(const std::string& description);
445
446 /**
447 * Unsubscribes a sample event callback function.
448 * @param subscriptionId The id of the event subscription to unsubscribe
449 */
450 void unsubscribeSampleEvent(const SubscriptionId subscriptionId);
451
452 protected:
453
454 /// Sample lock.
456
457 /// Subscription lock.
459
460 private:
461
462 /// The most recent measurement samples.
464
465 /// The maximal number of samples this measurement object can hold.
467
468 /// Map holding all sample event subscriptions.
470
471 /// The subscription id of the next event subscription.
472 SubscriptionId nextSampleSubscriptionId_ = SubscriptionId(invalidSubscriptionId() + 1u);
473
474 /// The map mapping object descriptions to object ids.
476
477 /// The map mapping object ids to descriptions.
479};
480
482{
483 return timestamp_;
484}
485
487{
488 return objectIds_;
489}
490
492{
493 return metadata_;
494}
495
496template <typename TInternalId>
498 owner_(owner)
499{
500 // nothing to do here
501}
502
503template <typename TInternalId>
504bool Measurement::ObjectMapper<TInternalId>::hasInternalObject(const TInternalId& internalObjectId) const
505{
506 return internalObjectIdMap_.find(internalObjectId) != internalObjectIdMap_.cend();
507}
508
509template <typename TInternalId>
511{
512 ocean_assert(externalObjectId != invalidObjectId());
513
514 return externalObjectIdMap_.find(externalObjectId) != externalObjectIdMap_.cend();
515}
516
517template <typename TInternalId>
518Measurement::ObjectId Measurement::ObjectMapper<TInternalId>::newInternalObjectId(const TInternalId& internalObjectId, const std::string& description)
519{
520 const ObjectId externalObjectId = owner_.addUniqueObjectId(description);
521
522 internalObjectIdMap_.emplace(internalObjectId, externalObjectId);
523 externalObjectIdMap_.emplace(externalObjectId, internalObjectId);
524
525 return externalObjectId;
526}
527
528template <typename TInternalId>
530{
531 const ObjectId externalObjectId = externalObjectIdFromInternalObjectId(internalObjectId);
532
533 internalObjectIdMap_.erase(internalObjectId);
534 externalObjectIdMap_.erase(externalObjectId);
535}
536
537template <typename TInternalId>
539{
540 const typename InternalObjectIdMap::const_iterator i = internalObjectIdMap_.find(internalObjectId);
541
542 if (i == internalObjectIdMap_.cend())
543 {
544 ocean_assert(false && "This must never happen!");
545 return invalidObjectId();
546 }
547
548 return i->second;
549}
550
551template <typename TInternalId>
552TInternalId Measurement::ObjectMapper<TInternalId>::internalObjectIdFromExternalObjectId(const ObjectId externalObjectId, const TInternalId& invalidInternalId) const
553{
554 const typename ExternalObjectIdMap::const_iterator i = externalObjectIdMap_.find(externalObjectId);
555
556 if (i == externalObjectIdMap_.cend())
557 {
558 ocean_assert(false && "This must never happen!");
559 return invalidInternalId;
560 }
561
562 return i->second;
563}
564
566{
567 *this = std::move(sampleEventSubscription);
568}
569
574
576{
577 measurement_.release();
578}
579
580inline Measurement::SampleEventSubscription::operator bool() const
581{
582 return weakMeasurement_ != nullptr;
583}
584
585inline size_t Measurement::sampleCapacity() const
586{
587 const ScopedLock scopedLock(samplesLock_);
588
589 return sampleCapacity_;
590}
591
593{
594 return ObjectId(-1);
595}
596
597}
598
599}
600
601#endif // META_OCEAN_DEVICES_MEASUREMENT_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
This class is the base class for all devices of any type.
Definition devices/Device.h:28
unsigned int SubscriptionId
Definition of a subscription id for event callbacks.
Definition devices/Device.h:151
This class implements a helper class to simplify the mapping between internal object ids (of the actu...
Definition Measurement.h:239
InternalObjectIdMap internalObjectIdMap_
The map mapping internal object ids to external object ids.
Definition Measurement.h:323
ObjectMapper & operator=(const ObjectMapper &objectMapper)=delete
Disabled copy operator.
ObjectId newInternalObjectId(const TInternalId &internalObjectId, const std::string &description)
Adds a new internal pattern id.
Definition Measurement.h:518
bool hasExternalObject(const Measurement::ObjectId &externalObjectId) const
Returns whether this mapping object holds a specific external object.
Definition Measurement.h:510
ObjectMapper(Measurement &owner)
Default constructor.
Definition Measurement.h:497
ObjectMapper(const ObjectMapper &objectMapper)=delete
Disabled copy constructor.
std::unordered_map< ObjectId, TInternalId > ExternalObjectIdMap
Definition of an unordered map mapping external object ids to internal object ids.
Definition Measurement.h:250
bool hasInternalObject(const TInternalId &internalObjectId) const
Returns whether this mapping object holds a specific internal object.
Definition Measurement.h:504
ObjectId externalObjectIdFromInternalObjectId(const TInternalId &internalObjectId) const
Converts the tracker's internal object id to an external object id.
Definition Measurement.h:538
TInternalId internalObjectIdFromExternalObjectId(const ObjectId externalObjectId, const TInternalId &invalidInternalId) const
Converts an external object id to the tracker tracker's internal object id.
Definition Measurement.h:552
ExternalObjectIdMap externalObjectIdMap_
The map mapping external object ids to internal object ids.
Definition Measurement.h:326
std::unordered_map< TInternalId, ObjectId > InternalObjectIdMap
Definition of an unordered map mapping internal object ids to external object ids.
Definition Measurement.h:245
void removeInternalObject(const TInternalId &internalObjectId)
Removes a mapping between internal and external object.
Definition Measurement.h:529
Measurement & owner_
The owner of this mapper.
Definition Measurement.h:320
This class manages the lifetime of an event subscription for sample events.
Definition Measurement.h:154
SampleEventSubscription & operator=(const SampleEventSubscription &sampleEventSubscription)=delete
Disabled assign operator.
void release()
Explicitly releases the subscription.
SampleEventSubscription & operator=(SampleEventSubscription &&sampleEventSubscription)
Replaces the current event subscription with a new event subscription.
void makeWeak()
Makes this subscription object weak so that is does not hold a reference to the actual measurement ob...
Definition Measurement.h:575
~SampleEventSubscription()
Destructs the subscription object and unsubscribes the object event.
Definition Measurement.h:570
MeasurementRef measurement_
The measurement object to which the event subscription belongs.
Definition Measurement.h:217
SampleEventSubscription()=default
Default constructor for a not active subscription.
SampleEventSubscription(const Measurement &measurement, const SubscriptionId subscriptionId)
Creates an active subscription object.
Definition of a sample holding a measurement.
Definition Measurement.h:67
virtual ~Sample()
Destructs a sample.
const ObjectIds & objectIds() const
Returns the sample object ids specifying possible different measurement units.
Definition Measurement.h:486
ObjectIds objectIds_
Measurement unit object ids.
Definition Measurement.h:119
Metadata metadata_
The metadata of this sample.
Definition Measurement.h:122
const Metadata & metadata() const
Returns the metadata of this sample.
Definition Measurement.h:491
Sample(const Timestamp &timestamp, ObjectIds &&objectIds, Metadata &&metadata=Metadata())
Creates a new measurement sample.
const Timestamp & timestamp() const
Returns the sample timestamp.
Definition Measurement.h:481
Sample(const Timestamp &timestamp, const ObjectIds &objectIds, const Metadata &metadata=Metadata())
Creates a new measurement sample.
Timestamp timestamp_
Sample timestamp.
Definition Measurement.h:116
This class implements the base class for all devices providing measurement samples.
Definition Measurement.h:40
ObjectId objectId(const std::string &description) const
Returns the object id for an object description.
unsigned int ObjectId
Definition of an object id.
Definition Measurement.h:46
SampleEventSubscription subscribeSampleEvent(SampleCallback &&callback)
Subscribes a callback event function for new measurement sample events.
void postNewSample(const SampleRef &newSample)
Posts a new measurement sample.
virtual SampleRef sample() const
Returns the most recent sample.
static constexpr ObjectId invalidObjectId()
Returns an invalid object id.
Definition Measurement.h:592
std::unordered_map< std::string, Value > Metadata
Definition of an unordered map mapping keys to values.
Definition Measurement.h:61
Strings objectDescriptions() const
Returns descriptions of all objects currently available.
ObjectId addUniqueObjectId(const std::string &description)
Creates a unique object id for a new object (e.g., a tracking object like an image,...
std::unordered_set< ObjectId > ObjectIdSet
Definition of an unordered set holding object ids.
Definition Measurement.h:56
SampleSubscriptionMap sampleSubscriptionMap_
Map holding all sample event subscriptions.
Definition Measurement.h:469
virtual SampleRef sample(const Timestamp timestamp) const
Returns the sample with a specific timestamp.
SampleMap sampleMap_
The most recent measurement samples.
Definition Measurement.h:463
InterpolationStrategy
Definition of individual interpolation strategies for samples.
Definition Measurement.h:141
@ IS_INVALID
An invalid strategy.
Definition Measurement.h:143
@ IS_TIMESTAMP_NEAREST
The sample with nearest/closest timestamp is used.
Definition Measurement.h:145
virtual SampleRef sample(const Timestamp &timestamp, const InterpolationStrategy interpolationStrategy) const
Returns the sample best matching with a specified timestamp.
size_t sampleCapacity() const
Returns the capacity of the internal sample container.
Definition Measurement.h:585
Callback< void, const Measurement *, const SampleRef & > SampleCallback
Definition of a callback function to subscribe for new measurement sample events.
Definition Measurement.h:135
std::unordered_map< SubscriptionId, SampleCallback > SampleSubscriptionMap
Definition of a map mapping subscription ids to event sample callback functions.
Definition Measurement.h:342
std::map< Timestamp, SampleRef > SampleMap
Definition of a map holding the most recent samples.
Definition Measurement.h:231
std::vector< ObjectId > ObjectIds
Definition of a vector holding object ids.
Definition Measurement.h:51
ObjectDescriptionToIdMap objectDescriptionToIdMap_
The map mapping object descriptions to object ids.
Definition Measurement.h:475
ObjectRef< Sample > SampleRef
Definition of an object reference for samples.
Definition Measurement.h:128
Lock samplesLock_
Sample lock.
Definition Measurement.h:455
void unsubscribeSampleEvent(const SubscriptionId subscriptionId)
Unsubscribes a sample event callback function.
size_t sampleCapacity_
The maximal number of samples this measurement object can hold.
Definition Measurement.h:466
~Measurement() override
Destructs a measurement object.
bool setSampleCapacity(const size_t capacity)
Sets the capacity of the internal sample container.
std::string objectDescription(const ObjectId objectId) const
Returns the description of one object of this measurement.
ObjectIdToDescriptionMap objectIdToDescriptionMap_
The map mapping object ids to descriptions.
Definition Measurement.h:478
Measurement(const std::string &name, const DeviceType type)
Creates a new measurement object.
Lock subscriptionLock_
Subscription lock.
Definition Measurement.h:458
std::unordered_map< std::string, ObjectId > ObjectDescriptionToIdMap
Definition of an unordered map mapping descriptions to unique object ids.
Definition Measurement.h:332
std::unordered_map< ObjectId, std::string > ObjectIdToDescriptionMap
Definition of an unordered map mapping unique object ids to descriptions.
Definition Measurement.h:337
This class implements a smart device reference.
Definition DeviceRef.h:36
This class implements a recursive lock object.
Definition Lock.h:31
This template class implements a object reference with an internal reference counter.
Definition base/ObjectRef.h:58
This class implements a scoped lock object for recursive lock objects.
Definition Lock.h:135
This class implements a timestamp.
Definition Timestamp.h:36
std::vector< std::string > Strings
Definition of a vector holding strings.
Definition Base.h:162
SmartDeviceRef< Measurement > MeasurementRef
Definition of a smart object reference for a measurement.
Definition Measurement.h:31
The namespace covering the entire Ocean framework.
Definition Accessor.h:15