new geo functions
changed the walkers added moving average fixed the interpolator new test-cases
This commit is contained in:
@@ -2,6 +2,10 @@
|
||||
#define INTERPOLATOR_H
|
||||
|
||||
#include "../Assertions.h"
|
||||
#include "../Exception.h"
|
||||
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
/**
|
||||
* interpolate between two adjacent values based on their key
|
||||
@@ -11,40 +15,55 @@ template <typename Key, typename Value> class Interpolator {
|
||||
public:
|
||||
|
||||
/** value at key */
|
||||
struct Entry {
|
||||
struct InterpolatorEntry {
|
||||
Key key;
|
||||
Value value;
|
||||
Entry(const Key key, const Value& pos) : key(key), value(value) {;}
|
||||
InterpolatorEntry(const Key key, const Value& value) : key(key), value(value) {;}
|
||||
};
|
||||
|
||||
protected:
|
||||
|
||||
/** all added entries, SORTED by their key */
|
||||
std::vector<Entry> entries;
|
||||
std::vector<InterpolatorEntry> entries;
|
||||
|
||||
public:
|
||||
|
||||
/** add a new value with its key. entries must be added in sorted order! */
|
||||
void add(const Key key, const Value& value) {
|
||||
Assert::isTrue (entries.empty() || entries.back().key < key, "entries must be ordered!");
|
||||
entries.push_back(Entry(key, value));
|
||||
entries.push_back(InterpolatorEntry(key, value));
|
||||
}
|
||||
|
||||
/** get the interpolated value for the given key */
|
||||
Value get(const Key key) const {
|
||||
const int idx1 = getIdx(key);
|
||||
const int idx2 = idx1+1;
|
||||
|
||||
const int idx2 = getIdxAfter(key);
|
||||
if (idx2 == 0) {return entries.front().value;} // key < everything within the list
|
||||
if (idx2 == -1) {return entries.back().value;} // key > everything within the list
|
||||
|
||||
// interpolate
|
||||
const int idx1 = (idx2 > 0) ? (idx2 - 1) : (idx2);
|
||||
const float percent = (key - entries[idx1].key) / (float) (entries[idx2].key - entries[idx1].key);
|
||||
return entries[idx1].value + (entries[idx2].value - entries[idx1].value) * percent;
|
||||
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
/** get the nearest index for the given key */
|
||||
int getIdx(const Key key) const {
|
||||
// TODO: use O(log(n)) search here!
|
||||
for (size_t i = 0; i < entries.size(); ++i) {
|
||||
if (entries[i].key > key) {return i-1;}
|
||||
}
|
||||
throw "should not happen";
|
||||
int getIdxAfter(const Key key) const {
|
||||
|
||||
// compare key against one entry
|
||||
static auto comp = [] (const Key& key, const InterpolatorEntry& ie1) {return key < ie1.key;};
|
||||
|
||||
// find the next entry > than key
|
||||
const auto it = std::upper_bound(entries.begin(), entries.end(), key, comp);
|
||||
|
||||
// none found? (key > everything within the list)
|
||||
return (it == entries.end()) ? (-1) : (it-entries.begin());
|
||||
//if (it == entries.end()) {throw Exception("key not found: " + std::to_string(key));}
|
||||
//return it - entries.begin();
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user