forked from gazebosim/gz-math
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spline.cc
385 lines (336 loc) · 11 KB
/
Spline.cc
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
* Copyright (C) 2012 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// Note: Originally cribbed from Ogre3d. Modified to implement Cardinal
// spline and catmull-rom spline
#include "SplinePrivate.hh"
#include "ignition/math/Helpers.hh"
#include "ignition/math/Vector4.hh"
#include "ignition/math/Spline.hh"
using namespace ignition;
using namespace math;
///////////////////////////////////////////////////////////
Spline::Spline()
: dataPtr(new SplinePrivate())
{
// Set up matrix
this->dataPtr->autoCalc = true;
this->dataPtr->tension = 0.0;
this->dataPtr->arcLength = INF_D;
}
///////////////////////////////////////////////////////////
Spline::~Spline()
{
delete this->dataPtr;
this->dataPtr = NULL;
}
///////////////////////////////////////////////////////////
void Spline::Tension(double _t)
{
this->dataPtr->tension = _t;
if (this->dataPtr->autoCalc)
this->RecalcTangents();
}
///////////////////////////////////////////////////////////
double Spline::Tension() const
{
return this->dataPtr->tension;
}
///////////////////////////////////////////////////////////
double Spline::ArcLength() const
{
return this->dataPtr->arcLength;
}
///////////////////////////////////////////////////////////
double Spline::ArcLength(const double _t) const
{
unsigned int fromIndex; double tFraction;
if (!this->MapToSegment(_t, fromIndex, tFraction))
return INF_D;
return (this->dataPtr->cumulativeArcLengths[fromIndex] +
this->ArcLength(fromIndex, tFraction));
}
///////////////////////////////////////////////////////////
double Spline::ArcLength(const unsigned int _index,
const double _t) const
{
if (_index >= this->dataPtr->segments.size())
return INF_D;
return this->dataPtr->segments[_index].ArcLength(_t);
}
///////////////////////////////////////////////////////////
void Spline::AddPoint(const Vector3d &_p)
{
this->AddPoint(
ControlPoint({_p, Vector3d(INF_D, INF_D, INF_D)}), false);
}
///////////////////////////////////////////////////////////
void Spline::AddPoint(const Vector3d &_p, const Vector3d &_t)
{
this->AddPoint(
ControlPoint({_p, _t}), true);
}
///////////////////////////////////////////////////////////
void Spline::AddPoint(const ControlPoint &_cp, const bool _fixed)
{
this->dataPtr->points.push_back(_cp);
this->dataPtr->fixings.push_back(_fixed);
if (this->dataPtr->autoCalc)
this->RecalcTangents();
else
this->Rebuild();
}
///////////////////////////////////////////////////////////
Vector3d Spline::InterpolateMthDerivative(const unsigned int _mth,
const double _t) const
{
unsigned int fromIndex; double tFraction;
this->MapToSegment(_t, fromIndex, tFraction);
return this->InterpolateMthDerivative(fromIndex, _mth, tFraction);
}
///////////////////////////////////////////////////////////
Vector3d Spline::InterpolateMthDerivative(const unsigned int _fromIndex,
const unsigned int _mth,
const double _t) const
{
// Bounds check
if (_fromIndex >= this->dataPtr->points.size())
return Vector3d(INF_D, INF_D, INF_D);
if (_fromIndex == this->dataPtr->segments.size())
{
// Duff request, cannot blend to nothing
// Just return source
return this->dataPtr->points[_fromIndex].MthDerivative(_mth);
}
// Interpolate derivative
return this->dataPtr->segments[_fromIndex].InterpolateMthDerivative(_mth, _t);
}
///////////////////////////////////////////////////////////
Vector3d Spline::Interpolate(const double _t) const
{
return this->InterpolateMthDerivative(0, _t);
}
///////////////////////////////////////////////////////////
Vector3d Spline::Interpolate(const unsigned int _fromIndex,
const double _t) const
{
return this->InterpolateMthDerivative(_fromIndex, 0, _t);
}
///////////////////////////////////////////////////////////
Vector3d Spline::InterpolateTangent(const double _t) const
{
return this->InterpolateMthDerivative(1, _t);
}
///////////////////////////////////////////////////////////
Vector3d Spline::InterpolateTangent(const unsigned int _fromIndex,
const double _t) const
{
return this->InterpolateMthDerivative(_fromIndex, 1, _t);
}
///////////////////////////////////////////////////////////
void Spline::RecalcTangents()
{
// Catmull-Rom approach
//
// tangent[i] = 0.5 * (point[i+1] - point[i-1])
//
// Assume endpoint tangents are parallel with line with neighbour
size_t i, numPoints;
bool isClosed;
numPoints = this->dataPtr->points.size();
if (numPoints < 2)
{
// Can't do anything yet
return;
}
// Closed or open?
if (this->dataPtr->points[0].MthDerivative(0) ==
this->dataPtr->points[numPoints-1].MthDerivative(0))
isClosed = true;
else
isClosed = false;
double t = 1.0 - this->dataPtr->tension;
for (i = 0; i < numPoints; ++i)
{
if (!this->dataPtr->fixings[i])
{
if (i == 0)
{
// Special case start
if (isClosed)
{
// Use this->dataPtr->points-2 since this->dataPtr->points-1
// is the last point and == [0]
this->dataPtr->points[i].MthDerivative(1) =
((this->dataPtr->points[1].MthDerivative(0) -
this->dataPtr->points[numPoints-2].MthDerivative(0)) * 0.5) * t;
}
else
{
this->dataPtr->points[i].MthDerivative(1) =
((this->dataPtr->points[1].MthDerivative(0) -
this->dataPtr->points[0].MthDerivative(0)) * 0.5) * t;
}
}
else if (i == numPoints-1)
{
// Special case end
if (isClosed)
{
// Use same tangent as already calculated for [0]
this->dataPtr->points[i].MthDerivative(1) =
this->dataPtr->points[0].MthDerivative(1);
}
else
{
this->dataPtr->points[i].MthDerivative(1) =
((this->dataPtr->points[i].MthDerivative(0) -
this->dataPtr->points[i-1].MthDerivative(0)) * 0.5) * t;
}
}
else
{
this->dataPtr->points[i].MthDerivative(1) =
((this->dataPtr->points[i+1].MthDerivative(0) -
this->dataPtr->points[i-1].MthDerivative(0)) * 0.5) * t;
}
}
}
this->Rebuild();
}
///////////////////////////////////////////////////////////
bool Spline::MapToSegment(const double _t,
unsigned int &_index,
double &_fraction) const
{
_index = 0;
_fraction = 0.0;
// Check corner cases
if (this->dataPtr->segments.empty())
return false;
if (equal(_t, 0.0))
return true;
if (equal(_t, 1.0))
{
_index = static_cast<unsigned int>(this->dataPtr->segments.size()-1);
_fraction = 1.0;
return true;
}
// Assume linear relationship between t and arclength
double tArc = _t * this->dataPtr->arcLength;
// Get segment index where t would lie
auto it = std::lower_bound(this->dataPtr->cumulativeArcLengths.begin(),
this->dataPtr->cumulativeArcLengths.end(),
tArc);
if (it != this->dataPtr->cumulativeArcLengths.begin())
_index = static_cast<unsigned int>(
(it - this->dataPtr->cumulativeArcLengths.begin() - 1));
// Get fraction of t, but renormalized to the segment
_fraction = (tArc - this->dataPtr->cumulativeArcLengths[_index])
/ this->dataPtr->segments[_index].ArcLength();
return true;
}
///////////////////////////////////////////////////////////
void Spline::Rebuild()
{
size_t numPoints = this->dataPtr->points.size();
if (numPoints < 2) {
// Can't do anything yet
return;
}
size_t numSegments = numPoints - 1;
this->dataPtr->segments.resize(numSegments);
this->dataPtr->cumulativeArcLengths.resize(numSegments);
for (size_t i = 0 ; i < numSegments ; ++i)
{
this->dataPtr->segments[i].SetPoints(this->dataPtr->points[i],
this->dataPtr->points[i+1]);
if (i > 0) {
this->dataPtr->cumulativeArcLengths[i] =
(this->dataPtr->segments[i-1].ArcLength()
+ this->dataPtr->cumulativeArcLengths[i-1]);
}
else
{
this->dataPtr->cumulativeArcLengths[i] = 0.0;
}
}
this->dataPtr->arcLength = (this->dataPtr->cumulativeArcLengths.back()
+ this->dataPtr->segments.back().ArcLength());
}
///////////////////////////////////////////////////////////
Vector3d Spline::Point(const unsigned int _index) const
{
return this->MthDerivative(_index, 0);
}
///////////////////////////////////////////////////////////
Vector3d Spline::Tangent(const unsigned int _index) const
{
return this->MthDerivative(_index, 1);
}
///////////////////////////////////////////////////////////
Vector3d Spline::MthDerivative(const unsigned int _index,
const unsigned int _mth) const
{
if (_index >= this->dataPtr->points.size())
return Vector3d(INF_D, INF_D, INF_D);
return this->dataPtr->points[_index].MthDerivative(_mth);
}
///////////////////////////////////////////////////////////
size_t Spline::PointCount() const
{
return this->dataPtr->points.size();
}
///////////////////////////////////////////////////////////
void Spline::Clear()
{
this->dataPtr->points.clear();
this->dataPtr->segments.clear();
this->dataPtr->fixings.clear();
}
///////////////////////////////////////////////////////////
bool Spline::UpdatePoint(const unsigned int _index,
const Vector3d &_point)
{
return this->UpdatePoint(_index, ControlPoint({_point}), false);
}
///////////////////////////////////////////////////////////
bool Spline::UpdatePoint(const unsigned int _index,
const Vector3d &_point,
const Vector3d &_tangent)
{
return this->UpdatePoint(_index, ControlPoint({_point, _tangent}), true);
}
///////////////////////////////////////////////////////////
bool Spline::UpdatePoint(const unsigned int _index,
const ControlPoint &_point,
const bool _fixed)
{
if (_index >= this->dataPtr->points.size())
return false;
this->dataPtr->points[_index].Match(_point);
this->dataPtr->fixings[_index] = _fixed;
if (this->dataPtr->autoCalc)
this->RecalcTangents();
else
this->Rebuild();
return true;
}
///////////////////////////////////////////////////////////
void Spline::AutoCalculate(bool _autoCalc)
{
this->dataPtr->autoCalc = _autoCalc;
}