RIT VEXU Core API
Loading...
Searching...
No Matches
interpolating_map.h
1#pragma once
2
3#include <map>
4
5#include "core/math/eigen_interface.h"
6
16template <typename KEY, typename VALUE> class InterpolatingMap {
17 public:
18 using MapType = std::map<KEY, VALUE, std::less<KEY>, Eigen::aligned_allocator<std::pair<const KEY, VALUE>>>;
19
26 void insert(const KEY &key, const VALUE &value) { map_.insert({key, value}); }
27
38 VALUE operator[](const KEY &key) {
39 // Get iterator for the upper bound of our key.
40 typename MapType::const_iterator upper = map_.upper_bound(key);
41
42 // If our key greater than the largest key return its value.
43 if (upper == map_.end()) {
44 return (--upper)->second;
45 }
46
47 // If our key less than or equal to the smallest key return its value.
48 if (upper == map_.begin()) {
49 return upper->second;
50 }
51
52 // Get an iterator to the previous entry.
53 typename MapType::const_iterator lower = upper;
54 --lower;
55
56 // Linear interpolate between the values of the first and second.
57 const double delta = (key - lower->first) / (upper->first - lower->first);
58 return delta * upper->second + (1.0 - delta) * lower->second;
59 }
60
61
63 void clear() { map_.clear(); }
64
65 private:
66 MapType map_;
67};
Definition interpolating_map.h:16
VALUE operator[](const KEY &key)
Definition interpolating_map.h:38
void clear()
Clears the contents of the map.
Definition interpolating_map.h:63
void insert(const KEY &key, const VALUE &value)
Definition interpolating_map.h:26