-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLookupTable.cpp
More file actions
74 lines (61 loc) · 1.45 KB
/
LookupTable.cpp
File metadata and controls
74 lines (61 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "LookupTable.h"
#include <algorithm>
bool PairLess(std::pair<double,double> x1, std::pair<double,double> x2) {
return x1.first < x2.first;
}
LookupTable::LookupTable(double below_min, double above_max)
{
m_below_min = below_min;
m_above_max = above_max;
}
void LookupTable::AddEntry(double x, double y)
{
for (int i = 0; i < m_yvals.size(); ++i)
{
// If this is really close to an existing x value
if (SortaEqual(m_yvals[i].first, x)) {
// Don't insert value, that would make divide-by-zero
// likely
return;
}
}
m_yvals.push_back(std::pair<double,double>(x,y));
std::sort(m_yvals.begin(), m_yvals.end(), PairLess);
}
double LookupTable::MinX()
{
if (m_yvals.size() == 0) {
return 0;
} else
return m_yvals.front().first;
}
double LookupTable::MaxX()
{
if (m_yvals.size() == 0) {
return 0;
} else
return m_yvals.back().first;
}
double LookupTable::Lookup(double x)
{
if (x < MinX()) return m_below_min;
if (x > MaxX()) return m_above_max;
double x1,y1,x2,y2;
for (int i = 0; i < m_yvals.size() - 1; ++i) {
if (m_yvals[i+1].first > x) {
x1 = m_yvals[i].first;
x2 = m_yvals[i+1].first;
y1 = m_yvals[i].second;
y2 = m_yvals[i+1].second;
break;
}
}
// Linearly interpolate between the two values
double y = (y2 - y1) * ((x - x1) / (x2 - x1)) + y1;
return y;
//TODO: Neural Network
}
void LookupTable::Clear()
{
m_yvals.clear();
}