RIT VEXU Core API
Loading...
Searching...
No Matches
interpolating_map.h
1#pragma once
2
3#include <map>
4
5#include "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
64 void clear() { map_.clear(); }
65
66 private:
67 MapType map_;
68};
Definition interpolating_map.h:16
VALUE operator[](const KEY &key)
Definition interpolating_map.h:38
void clear()
Definition interpolating_map.h:64
void insert(const KEY &key, const VALUE &value)
Definition interpolating_map.h:26