-
Notifications
You must be signed in to change notification settings - Fork 15
/
splines.inl
192 lines (156 loc) · 5.55 KB
/
splines.inl
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "splines.hpp"
#include <cassert>
#include <algorithm>
template<typename Point_t, typename Real_t>
Spline<Point_t, Real_t>::Spline(
int k,
spline::Node_e node_type)
:
_node_type(node_type),
_k(k),
_point( _k ),
_vec( _k-1 ),
_node( _k + _point.size() )
{
assert_splines();
}
// -----------------------------------------------------------------------------
template<typename Point_t, typename Real_t>
void Spline<Point_t, Real_t>::set_ctrl_points(const std::vector<Point_t>& point)
{
_point = point;
_vec.resize(_point.size() - 1);
for(int i = 0; i < (int)_vec.size(); ++i)
_vec[i] = _point[i + 1] - _point[i];
set_nodal_vector();
assert_splines();
for(int i = 0; i < (int)_vec.size(); ++i)
_vec[i] /= _node[_k+i] - _node[i+1];
}
// -----------------------------------------------------------------------------
template<typename Point_t, typename Real_t>
void Spline<Point_t, Real_t>::get_ctrl_points(std::vector<Point_t>& points) const
{
points = _point;
}
// -----------------------------------------------------------------------------
/// The the nodal vector type
template<typename Point_t, typename Real_t>
void Spline<Point_t, Real_t>::set_node_type( spline::Node_e type)
{
_node_type = type;
set_nodal_vector();
assert_splines();
}
// -----------------------------------------------------------------------------
template<typename Point_t, typename Real_t>
Point_t Spline<Point_t, Real_t>::eval_f(Real_t u) const
{
u = std::max(std::min(u, (Real_t)1), (Real_t)0); // clamp between [0 1]
return eval(u, _point, _k, _node);
}
// -----------------------------------------------------------------------------
template<typename Point_t, typename Real_t>
Point_t Spline<Point_t, Real_t>::eval_df(Real_t u) const
{
u = std::max(std::min(u, (Real_t)1), (Real_t)0); // clamp between [0 1]
return eval(u, _vec, (_k-1), _node, 1) * (Real_t)(_k-1);
}
// -----------------------------------------------------------------------------
template<typename Point_t, typename Real_t>
void Spline<Point_t, Real_t>::assert_splines() const
{
assert( _k > 1);
assert((int)_point.size() >= _k );
assert(_node. size() == (_k + _point.size()) );
assert(_point.size() == (_vec.size() + 1) );
}
// -----------------------------------------------------------------------------
template<typename Point_t, typename Real_t>
void Spline<Point_t, Real_t>::set_nodal_vector()
{
if( _node_type == spline::eOPEN_UNIFORM)
set_node_to_open_uniform();
else if( _node_type == spline::eUNIFORM )
set_node_to_uniform();
}
// -----------------------------------------------------------------------------
template<typename Point_t, typename Real_t>
void Spline<Point_t, Real_t>::set_node_to_uniform()
{
const int n = _point.size() - 1;
_node.resize( _k + n + 1 );
Real_t step = (Real_t)1 / (Real_t)(n-_k+2);
for (int i = 0; i < (int)_node.size(); ++i){
_node[i] = ((Real_t)i) * step - step * (Real_t)(_k-1);
}
}
// -----------------------------------------------------------------------------
template<typename Point_t, typename Real_t>
void Spline<Point_t, Real_t>::set_node_to_open_uniform()
{
_node.resize( _k + _point.size() );
int acc = 1;
for (int i = 0; i < (int)_node.size(); ++i)
{
if(i < _k)
_node[i] = 0.;
else if( i >= ((int)_point.size() + 1) )
_node[i] = 1.;
else{
_node[i] = (Real_t)acc / (Real_t)(_point.size() + 1 - _k);
acc++;
}
}
}
// -----------------------------------------------------------------------------
template<typename Point_t, typename Real_t>
Point_t Spline<Point_t, Real_t>::
eval(Real_t u,
const std::vector<Point_t>& point,
int k,
const std::vector<Real_t>& node,
int off) const
{
assert( k > 1);
assert((int)point.size() >= k );
assert_splines();
int dec = 0;
// TODO: better search with dychotomi ?
// TODO: check for overflow
while( u > node[dec + k + off] )
dec++;
// TODO: use buffers in attributes for better performances ?
std::vector<Point_t> p_rec(k, Point_t());
for(int i = dec, j = 0; i < (dec + k); ++i, ++j)
p_rec[j] = point[i];
std::vector<Real_t> node_rec(k + k - 2, (Real_t)0);
for(int i = (dec + 1), j = 0; i < (dec + k + k - 1); ++i, ++j)
node_rec[j] = node[i + off];
return eval_rec(u, p_rec, k, node_rec);
}
// -----------------------------------------------------------------------------
template<typename Point_t, typename Real_t>
Point_t Spline<Point_t, Real_t>::
eval_rec(Real_t u,
std::vector<Point_t> p_in,
int k,
std::vector<Real_t> node_in) const
{
if(p_in.size() == 1)
return p_in[0];
// TODO: use buffers in attributes for better performances ?
std::vector<Point_t> p_out(k - 1, Point_t());
for(int i = 0; i < (k - 1); ++i)
{
const Real_t n0 = node_in[i + k - 1];
const Real_t n1 = node_in[i];
const Real_t f0 = (n0 - u) / (n0 - n1);
const Real_t f1 = (u - n1) / (n0 - n1);
p_out[i] = p_in[i] * f0 + p_in[i + 1] * f1;
}
std::vector<Real_t> node_out(node_in.size() - 2);
for(int i = 1, j = 0; i < ((int)node_in.size()-1); ++i, ++j)
node_out[j] = node_in[i];
return eval_rec(u, p_out, (k - 1), node_out);
}