8 #ifndef META_OCEAN_MATH_SAMPLE_MAP_H
9 #define META_OCEAN_MATH_SAMPLE_MAP_H
36 typedef std::map<double, T>
Map;
91 bool sample(T& value,
double* sampleTimestamp =
nullptr) const;
99 bool sample(const
double& timestamp, T& value) const;
111 bool sample(const
double& timestamp, const
InterpolationStrategy interpolationStrategy, T& value,
double* timestampDistance =
nullptr,
double* sampleTimestamp =
nullptr) const;
117 std::vector<std::pair<
double, T>>
samples() const;
174 template <typename T>
181 template <
typename T>
188 template <
typename T>
190 map_(sampleMap.map_),
196 template <
typename T>
199 *
this = std::move(sampleMap);
202 template <
typename T>
209 typename Map::iterator i =
map_.begin();
210 ocean_assert(i !=
map_.end());
218 template <
typename T>
224 typename Map::const_reverse_iterator i =
map_.rbegin();
226 if (i ==
map_.rend())
235 *sampleTimestamp = i->first;
241 template <
typename T>
246 const typename Map::const_iterator i =
map_.find(timestamp);
248 if (i ==
map_.cend())
258 template <
typename T>
268 if (
map_.size() == 1)
272 ocean_assert(
map_.rbegin() !=
map_.rend());
273 value =
map_.rbegin()->second;
275 if (timestampDistance)
283 *sampleTimestamp =
map_.rbegin()->first;
290 const typename Map::const_iterator iUpper =
map_.upper_bound(timestamp);
292 if (iUpper ==
map_.cend())
296 ocean_assert(
map_.rbegin() !=
map_.rend());
297 value =
map_.rbegin()->second;
299 if (timestampDistance)
307 *sampleTimestamp =
map_.rbegin()->first;
313 ocean_assert(iUpper->first > timestamp);
315 if (iUpper ==
map_.cbegin())
319 ocean_assert(
map_.begin() !=
map_.end());
321 value =
map_.begin()->second;
323 if (timestampDistance)
331 *sampleTimestamp =
map_.begin()->first;
337 typename Map::const_iterator iLower(iUpper);
339 ocean_assert(iLower !=
map_.begin());
342 ocean_assert(iLower->first <= timestamp);
344 const double lowerDelta = timestamp - iLower->first;
345 const double upperDelta = iUpper->first - timestamp;
346 ocean_assert(lowerDelta >= 0.0 && upperDelta >= 0.0);
350 const double delta = lowerDelta + upperDelta;
355 value = iLower->second;
357 if (timestampDistance)
360 *timestampDistance = 0.0;
365 *sampleTimestamp = iLower->first;
371 const double interpolationFactor = lowerDelta / delta;
372 ocean_assert(interpolationFactor >= 0.0 && interpolationFactor <= 1.0);
374 const T& lowerSample = iLower->second;
375 const T& upperSample = iUpper->second;
379 if (timestampDistance)
382 *timestampDistance = std::min(iUpper->first - timestamp, timestamp - iLower->first);
387 *sampleTimestamp = iLower->first * interpolationFactor + iUpper->first * (1.0 - interpolationFactor);
397 if (lowerDelta < upperDelta)
399 value = iLower->second;
401 if (timestampDistance)
404 *timestampDistance = std::min(iUpper->first - timestamp, timestamp - iLower->first);
409 *sampleTimestamp = iLower->first;
415 value = iUpper->second;
417 if (timestampDistance)
420 *timestampDistance = std::min(iUpper->first - timestamp, timestamp - iLower->first);
425 *sampleTimestamp = iUpper->first;
431 template <
typename T>
436 std::vector<std::pair<double, T>> result;
437 result.reserve(
map_.size());
439 for (
typename Map::const_iterator i =
map_.cbegin(); i !=
map_.cend(); ++i)
441 result.emplace_back(i->first, i->second);
447 template <
typename T>
457 template <
typename T>
465 template <
typename T>
473 template <
typename T>
481 template <
typename T>
489 template <
typename T>
492 map_ = sampleMap.map_;
498 template <
typename T>
501 if (
this != &sampleMap)
503 map_ = std::move(sampleMap.map_);
506 sampleMap.capacity_ = 0;
static T linear(const T &v0, const T &v1, const TFactor &t)
Performs a linear interpolation between two values.
Definition: Interpolation.h:508
This class implements a recursive lock object.
Definition: Lock.h:31
static T abs(const T value)
Returns the absolute value of a given value.
Definition: Numeric.h:1220
static constexpr bool isEqualEps(const T value)
Returns whether a value is smaller than or equal to a small epsilon.
Definition: Numeric.h:2087
This class stores samples of e.g., sensor or tracking data in a map.
Definition: SampleMap.h:30
Lock lock_
The lock of this map.
Definition: SampleMap.h:171
size_t size() const
Returns the number of samples currently stored in this map.
Definition: SampleMap.h:458
Map data() const
Returns all samples stores in this map.
Definition: SampleMap.h:448
std::map< double, T > Map
Definition of a map mapping timestamps to samples.
Definition: SampleMap.h:36
bool isEmpty() const
Returns whether this map is empty.
Definition: SampleMap.h:474
Map map_
The map holding the actual samples.
Definition: SampleMap.h:165
void clear()
Removes all samples from this map.
Definition: SampleMap.h:482
std::vector< std::pair< double, T > > samples() const
Returns all samples stores in this map.
Definition: SampleMap.h:432
void insert(const T &sample, const double ×tamp)
Inserts a new sample with corresponding timestamp.
Definition: SampleMap.h:203
SampleMap< T > & operator=(const SampleMap< T > &sampleMap) noexcept
Copy operator.
Definition: SampleMap.h:490
size_t capacity() const
Returns the capacity of this map (the number of samples this map can store).
Definition: SampleMap.h:466
bool sample(T &value, double *sampleTimestamp=nullptr) const
Returns the most recent sample.
Definition: SampleMap.h:219
SampleMap() noexcept
Creates a new map with default capacity.
Definition: SampleMap.h:175
InterpolationStrategy
Definition of individual interpolation strategies for samples.
Definition: SampleMap.h:42
@ IS_TIMESTAMP_NEAREST
The sample with nearest/closest timestamp is used.
Definition: SampleMap.h:46
@ IS_INVALID
An invalid strategy.
Definition: SampleMap.h:44
@ IS_TIMESTAMP_INTERPOLATE
The sample is interpolated based on two samples.
Definition: SampleMap.h:48
size_t capacity_
The capacity of this map, with range [1, infinity).
Definition: SampleMap.h:168
This class implements a scoped lock object for recursive lock objects.
Definition: Lock.h:135
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15