-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spline.cpp
148 lines (127 loc) · 2.57 KB
/
Spline.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "Spline.hpp"
#include <cmath>
HermiteSpline::HermiteSpline()
{}
HermiteSpline::~HermiteSpline()
{}
void HermiteSpline::push_back(float x, float y)
{
this->x.push_back(x);
this->y.push_back(y);
}
void HermiteSpline::build()
{
// check length issues
size_t length = std::min(x.size(), y.size());
if(x.size() != y.size())
{
x.resize(length);
y.resize(length);
x.shrink_to_fit();
y.shrink_to_fit();
}
if(length == 0)
return;
std::vector<float> slope;
for(size_t i = 0; i < length - 1; i++)
slope.push_back(0);
for(size_t i = 0; i < length; i++)
m.push_back(0);
// slope of secant lines
for(size_t k = 0; k < length - 1; k++)
{
slope[k] = (y[k+1]-y[k])/(x[k+1]-x[k]);
}
// slope of tangent lines
m[0] = slope[0];
m[length] = slope[length-1];
for(size_t k = 1; k < length - 1; k++)
{
if((slope[k-1] < 0 && slope[k] > 0)
|| (slope[k-1] > 0 && slope[k] < 0))
{
m[k] = 0;
}
else
{
m[k] = 0.5f * (slope[k-1] + slope[k]);
}
}
// check for monotonicity
for(size_t k = 1; k < length - 1; k++)
{
if((slope[k-1] < 0 && slope[k] > 0)
|| (slope[k-1] > 0 && slope[k] < 0))
continue;
float a = m[k]/slope[k];
float b = m[k]/slope[k-1];
if(a < 0 || b < 0)
m[k] = 0;
}
// prevent overshooting
for(size_t k = 0; k < length - 1; k++)
{
if((slope[k-1] < 0 && slope[k] > 0)
|| (slope[k-1] > 0 && slope[k] < 0))
continue;
float a = m[k]/slope[k];
float b = m[k+1]/slope[k];
float x = a*a + b*b;
if(x > 9)
{
m[k] = 3*a*slope[k]/std::sqrt(x);
m[k+1] = m[k];
}
}
}
void HermiteSpline::build(std::vector<float> const &x_values, std::vector<float> const &y_values)
{
this->x = std::vector<float>(x_values);
this->y = std::vector<float>(y_values);
build();
}
// basis functions for hermite spline
float HermiteSpline::h00(float t) const
{
float tm = 1-t;
return (1+2*t)*tm*tm;
}
float HermiteSpline::h10(float t) const
{
float tm = 1-t;
return t*tm*tm;
}
float HermiteSpline::h01(float t) const
{
float tm = 3-2*t;
return t*t*tm;
}
float HermiteSpline::h11(float t) const
{
return t*t*(t-1);
}
float HermiteSpline::operator()(float t) const
{
if(x.size() == 0)
return 0;
if(x.size() == 1)
return y.front();
size_t k_lower = 0;
size_t k_upper = 0;
if(t < x.front())
return x.front();
else if(t > x.back())
return x.back();
for(size_t k = 0; k < x.size(); k++)
{
if(x[k] > t)
{
k_lower = k-1;
k_upper = k;
break;
}
}
float h = x[k_upper] - x[k_lower];
float ts = (t - x[k_lower]) / h;
return y[k_lower]*h00(ts) + h*m[k_lower]*h10(ts) + y[k_upper]*h01(ts) + h*m[k_upper]*h11(ts);
}