-
Notifications
You must be signed in to change notification settings - Fork 17
/
Spline.cpp
144 lines (135 loc) · 3.38 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
#include <iomanip>
#include <stdexcept>
#include "Spline.hpp"
namespace Leph {
double Spline::pos(double t) const
{
return interpolation(t, &Polynom::pos);
}
double Spline::vel(double t) const
{
return interpolation(t, &Polynom::vel);
}
double Spline::acc(double t) const
{
return interpolation(t, &Polynom::acc);
}
double Spline::posMod(double t) const
{
return interpolationMod(t, &Polynom::pos);
}
double Spline::velMod(double t) const
{
return interpolationMod(t, &Polynom::vel);
}
double Spline::accMod(double t) const
{
return interpolationMod(t, &Polynom::acc);
}
double Spline::min() const
{
if (_splines.size() == 0) {
return 0.0;
} else {
return _splines.front().min;
}
}
double Spline::max() const
{
if (_splines.size() == 0) {
return 0.0;
} else {
return _splines.back().max;
}
}
void Spline::exportData(std::ostream& os) const
{
for (size_t i=0;i<_splines.size();i++) {
os << std::setprecision(10) << _splines[i].min << " ";
os << std::setprecision(10) << _splines[i].max << " ";
os << std::setprecision(10) <<
_splines[i].polynom.getCoefs().size() << " ";
for (size_t j=0;j<_splines[i].polynom.getCoefs().size();j++) {
os << std::setprecision(10) <<
_splines[i].polynom.getCoefs()[j] << " ";
}
}
os << std::endl;
}
void Spline::importData(std::istream& is)
{
bool isFormatError;
while (is.good()) {
isFormatError = true;
double min;
double max;
size_t size;
Polynom p;
//Load spline interval and degree
is >> min;
if (!is.good()) break;
is >> max;
if (!is.good()) break;
is >> size;
//Load polynom coeficients
p.getCoefs().resize(size);
for (size_t i=0;i<size;i++) {
if (!is.good()) break;
is >> p.getCoefs()[i];
}
//Save spline part
isFormatError = false;
_splines.push_back({p, min, max});
//Exit on line break
while (is.peek() == ' ') {
if (!is.good()) break;
is.ignore();
}
if (is.peek() == '\n') {
break;
}
}
if (isFormatError) {
throw std::logic_error(
"Spline import format invalid");
}
}
double Spline::interpolation(double x,
double(Polynom::*func)(double) const) const
{
//Bound asked abscisse into spline range
if (x <= _splines.front().min) {
x = _splines.front().min;
}
if (x >= _splines.back().max) {
x = _splines.back().max;
}
//Bijection spline search
size_t indexLow = 0;
size_t indexUp = _splines.size()-1;
while (indexLow != indexUp) {
size_t index = (indexUp+indexLow)/2;
if (x < _splines[index].min) {
indexUp = index-1;
} else if (x > _splines[index].max) {
indexLow = index+1;
} else {
indexUp = index;
indexLow = index;
}
}
//Compute and return spline value
return (_splines[indexUp].polynom.*func)
(x-_splines[indexUp].min);
}
double Spline::interpolationMod(double x,
double(Polynom::*func)(double) const) const
{
if (x < 0.0) {
x = 1.0 + (x - ((int)x/1));
} else if (x > 1.0) {
x = (x - ((int)x/1));
}
return interpolation(x, func);
}
}