/
spline.hpp
594 lines (506 loc) · 19.4 KB
/
spline.hpp
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/* celtic_knots - generate and animate random Celtic knots
* Copyright (C) 2008 Lewis Van Winkle
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __SPLINE_HPP__
#define __SPLINE_HPP__
#include <cassert>
#include <cmath>
#include <cstring>
///Hosts various interpolation functions.
namespace Spline
{
///Functions used by splines.
namespace Function
{
///The sign of a C++ % is not clearly defined.
inline int Imod(int i, int j)
{
return (i % j) < 0 ? (i % j) + (j < 0 ? -j : j) : i % j;
}
///Mod to loop a float around within a range.
template <typename FT>
FT Mod(FT i, FT start, FT end)
{
const FT range = end - start;
FT d = std::fabs((i - start) / range);
d -= std::floor(d);
d *= range;
if (i >= start)
d += start;
else
d = end - d;
assert(d >= start);
assert(d < end);
return d;
}
///Hermite basis function.
template <typename FT>
FT h1(FT t)
{
const FT t2 = t * t;
const FT t3 = t2 * t;
return 2 * t3 - 3 * t2 + 1;
}
template <typename FT>
FT h2(FT t)
{
const FT t2 = t * t;
const FT t3 = t2 * t;
return -2 * t3 + 3 * t2;
}
template <typename FT>
FT h3(FT t)
{
const FT t2 = t * t;
const FT t3 = t2 * t;
return t3 - 2 * t2 + t;
}
template <typename FT>
FT h4(FT t)
{
const FT t2 = t * t;
const FT t3 = t2 * t;
return t3 - t2;
}
///Interpolates linearly between two points.
struct Linear
{
template <typename ST, typename FT>
ST operator()(ST y0, ST y1, FT t)
{
return y0 + (y1 - y0) * t;
}
};
///A smooth polynomial acceleration.
struct Accel
{
template <typename ST, typename FT>
ST operator()(ST y0, ST y1, FT t)
{
return Linear()(y0, y1, t * t);
}
};
///Cardinal spline, interpolates between y1 and y2 using surrounding points y0 and y3 with weight c. Assumes uniform spacing of knots.
template <typename ST, typename FT>
ST Cardinal(ST y0, ST y1, ST y2, ST y3, FT c, FT t)
{
const FT m0 = c * (y2 - y0); //Tangent for y1.
const FT m1 = c * (y3 - y1); //Tangent for y2.
return Hermite(m0, y1, y2, m1, t);
}
///Catmull-Rom, interpolates between y1 and y2 using surrounding points y0 and y3 with weight 0.5. Assumes uniform spacing of knots.
template <typename ST, typename FT>
ST CatmullRom(ST y0, ST y1, ST y2, ST y3, FT t)
{
const FT t2 = t * t;
const FT t3 = t2 * t;
return ((y1 * 2) +
(-y0 + y2) * t +
(y0 * 2 - y1 * 5 + y2 * 4 - y3) * t2 +
(-y0 + y1 * 3 - y2 * 3 + y3) * t3) * 0.5;
}
///Cosine interpolation between two points.
struct Cosine
{
template <typename ST, typename FT>
ST operator()(ST y0, ST y1, FT t)
{
const FT pi = std::acos(-1.0);
return Linear()(y0, y1, -std::cos(t * pi) / 2 + 0.5);
}
};
///A smooth polynomial deceleration.
struct Decel
{
template <typename ST, typename FT>
ST operator()(ST y0, ST y1, FT t)
{
return Linear()(y0, y1, 1 - (1 - t) * (1 - t));
}
};
///Cubic Hermite spline, taking two points and their tangents.
/**A Cardinal splines calculates these tangents based on surrounding points and a weight parameter.
* A Catmull-Rom spline is a cardinal spline with weight of 0.5.
*/
template <typename ST, typename FT>
ST Hermite(ST m0, ST y0, ST y1, ST m1, FT t)
{
return
m0 * h3(t) +
y0 * h1(t) +
y1 * h2(t) +
m1 * h4(t);
}
///Step interpolation, jumps at t >= 1.0.
struct LateStep
{
template <typename ST, typename FT>
ST operator()(ST y0, ST y1, FT t)
{
return t < 1.0 ? y0 : y1;
}
};
///Step interpolation, jumps at t 0.5.
struct NearestNeighbor
{
template <typename ST, typename FT>
ST operator()(ST y0, ST y1, FT t)
{
return t < .5 ? y0 : y1;
}
};
typedef NearestNeighbor Step;
///A smooth polynomial similar to Cosine.
struct SmoothStep
{
template <typename ST, typename FT>
ST operator()(ST y0, ST y1, FT t)
{
return Linear()(y0, y1, t * t * (3 - 2 * t));
}
};
}
///Useful antiderivatives of some functions.
namespace Antiderivatives
{
template <typename FT>
FT h1(FT t)
{
const FT t2 = t * t;
const FT t3 = t2 * t;
const FT t4 = t3 * t;
return t4 / 2 - t3 + t;
}
template <typename FT>
FT h2(FT t)
{
const FT t2 = t * t;
const FT t3 = t2 * t;
const FT t4 = t3 * t;
return t3 - t4 / 2;
}
template <typename FT>
FT h3(FT t)
{
const FT t2 = t * t;
const FT t3 = t2 * t;
const FT t4 = t3 * t;
return t4 / 4 - 2 * t3 / 3 + t2 / 2;
}
template <typename FT>
FT h4(FT t)
{
const FT t2 = t * t;
const FT t3 = t2 * t;
const FT t4 = t3 * t;
return t4 / 4 - t3 / 3;
}
}
///Useful derivatives of some functions.
namespace Derivatives
{
template <typename FT>
FT h1(FT t)
{
const FT t2 = t * t;
return 6 * t2 - 6 * t;
}
template <typename FT>
FT h2(FT t)
{
const FT t2 = t * t;
return 6 * t - 6 * t2;
}
template <typename FT>
FT h3(FT t)
{
const FT t2 = t * t;
return 3 * t2 - 4 * t + 1;
}
template <typename FT>
FT h4(FT t)
{
const FT t2 = t * t;
return 3 * t2 - 2 * t;
}
template <typename ST, typename FT>
ST Hermite(ST m0, ST y0, ST y1, ST m1, FT t)
{
return
m0 * h3(t) +
y0 * h1(t) +
y1 * h2(t) +
m1 * h4(t);
}
}
///Interface for interpolation between several points.
template <typename ST, typename FT = double>
class Spline
{
public:
/**\brief ABC for spline.
* \param xs X values. Must always be monotonic, some splines require uniform spacing.
* \param ys Y value for each x.
* \param n Number of knots (size of x and y array).
* \param loop True if the spline should loop around when given at values outside of xs.
* In general, this should be true if values >= xs[n-1] || values < xs[0] will be given.
* If true, the last y value should match the first y value.
* \param copy If true xs and ys are copied, if false they are not (and the original data must remain valid for the life of the spline).
*/
Spline(const FT* xs, const ST* ys, size_t n, bool loop, bool copy)
:mN(n), mLoop(loop), mCopy(copy)
{
assert(mN > 1);
if (!mCopy)
{
mXs = xs;
mYs = ys;
}
else
{
FT* tempXs = new FT[n];
std::memcpy(tempXs, xs, sizeof(FT) * n);
mXs = tempXs;
ST* tempYs = new ST[n];
std::memcpy(tempYs, ys, sizeof(ST) * n);
mYs = tempYs;
}
#ifndef NDEBUG
FT last = mXs[0];
for (size_t i = 1; i < mN; ++i)
{
assert(mXs[i] > last);
last = mXs[i];
}
#endif
}
virtual ~Spline()
{
if (mCopy)
{
delete[] mXs;
delete[] mYs;
}
}
ST operator()(FT x) const {return Y(x);}
virtual ST Y(FT x) const = 0;
size_t GetKnotCount() const {return mN;}
protected:
const size_t mN : 30; ///<Number of data points.
const bool mLoop : 1; ///<If true, loop outside of the x range, otherwise continue in given direction.
const bool mCopy : 1; ///<If true, the data was copied and should not be freed here.
///Given an x value, returns the index before it. x can loop around.
size_t GetIndex(FT x) const
{
int i = mLastIndex;
//Convert x to be between mXs[0] and mXs[mN-1].
if (mLoop)
x = Function::Mod(x, mXs[0], mXs[mN-1]);
while (true)
{
i = Function::Imod(i, mN);
const FT& current = GetX(i);
const FT& next = GetX(i+1);
if (current <= x)
{
if (next > x)
break;
if (i == int(mN)-2 && !mLoop)
break;
++i;
}
else //current > x
{
if (i == 0 && !mLoop)
break;
--i;
}
}
mLastIndex = i;
return mLastIndex;
}
///Returns the amount that an x is between ranges.
FT GetSubRange(int index, FT x) const
{
//Convert x to be between mXs[0] and mXs[mN-1].
FT d = mLoop ? LoopInRange(x) : x;
//Normalize sub range.
const FT& sr = GetX(index);
const FT& er = GetX(index + 1);
const FT& r = er - sr;
d -= sr;
d /= r;
return d;
}
///Returns an x value. Index will loop around.
FT GetX(int index) const
{
index = Function::Imod(index, mN);
assert(index >= 0);
assert(index < int(mN));
return mXs[index];
}
///Returns a y value. Index will loop around.
ST GetY(int index) const
{
index = Function::Imod(index, mN);
assert(index >= 0);
assert(index < int(mN));
return mYs[index];
}
///Loops x within the range of this spline.
FT LoopInRange(FT x) const
{
return Function::Mod(x, mXs[0], mXs[mN-1]);
}
private:
mutable size_t mLastIndex; ///<Accelerates index lookup.
const FT *mXs;
const ST *mYs;
};
///This supports non-uniform control points.
template <typename ST = double, typename FT = double>
class Cardinal : public Spline<ST, FT>
{
public:
Cardinal(const FT* xs, const ST* ys, size_t n, bool loop, FT tension = 0.5, bool copy = true)
:Spline<ST, FT>(xs, ys, n, loop, copy), mC(tension){}
virtual ~Cardinal(){}
virtual ST Y(FT x) const
{
const size_t i = GetIndex(x);
const FT t = GetSubRange(i, x);
//We interpolate between y1 and y2. y0 and y3 are needed to calculate tangents.
//If loop is on, we should be careful to see if y0 or y3 should loop around.
const ST y0 = i == 0 ? (this->mLoop ? this->GetY(i-2) : this->GetY(i+1)) : this->GetY(i-1);
const ST y1 = this->GetY(i);
const ST y2 = this->GetY(i+1);
const ST y3 = i + 2 == this->mN ?
(this->mLoop ? this->GetY(i+3) : this->GetY(i)) :
this->GetY(i+2);
//The x values are needed in case of non-uniform spacing.
const FT x1 = this->GetX(i);
const FT x2 = this->GetX(i+1);
//Find the change in x for the x0-x1 interval (dx1) and the x2-x3 interval (dx2).
//Looping around makes this a little bit tricky.
const FT dx1 = (i == 0 ? (this->mLoop ? this->GetX(this->mN-1) - this->GetX(this->mN-2) : 0 ) : x1 - this->GetX(i-1));
const FT dx2 = (i + 2 == this->mN ?
(this->mLoop ? this->GetX(1) - this->GetX(0) : 0 ) :
this->GetX(i+2) - x2);
const FT dx = x2 - x1; //The x spacing we are going to interpolate in.
//Tangent scaling factors.
//(If the xs are uniform then dx = dx1 = dx2, which makes h1 = h2 = 1).
const FT h1 = (dx / (dx1 + dx)) * 2;
const FT h2 = (dx / (dx + dx2)) * 2;
//Final tangents, these will be the actual slope at y1 and y2.
//A cardinal spline sets it tangents based on the slope to surrounding points.
const ST m1 = (y2 - y0) * h1 * mC; //y1 tangent.
const ST m2 = (y3 - y1) * h2 * mC; //y2 tangent.
return Function::Hermite<ST, FT>(m1, y1, y2, m2, t);
}
protected:
const FT mC;
};
///This only supports uniformly spaced control points. For non-uniform points use a Cardinal Spline with tension 0.5.
template <typename ST = double, typename FT = double>
class CatmullRom : public Spline<ST, FT>
{
public:
/**If loop is false, then the first and last points use a zero tangent. So Y(xs[0]) and Y(xs[n-1]) have zero slope.
* If more control is needed, it is recommended to add an additional point before and after the data set.
*/
CatmullRom(const FT* xs, const ST* ys, size_t n, bool loop, bool copy = true)
:Spline<ST, FT>(xs, ys, n, loop, copy){}
virtual ~CatmullRom(){};
virtual ST Y(FT x) const
{
const size_t i = GetIndex(x);
const FT t = GetSubRange(i, x);
const ST prev = i == 0 ? (this->mLoop ? this->GetY(i-2) : this->GetY(i+1)) : this->GetY(i-1);
const ST next = i == this->mN-2 ? (this->mLoop ? this->GetY(i+3) : this->GetY(i)) : this->GetY(i+2);
return Function::CatmullRom<ST, FT>(prev, this->GetY(i), this->GetY(i+1), next, t);
}
protected:
};
///This creates a cubic Hermite spline and allows the user to define each tangent.
template <typename ST = double, typename FT = double>
class Hermite : public Spline<ST, FT>
{
public:
Hermite(const FT* xs, const ST* ys, const ST* ms, size_t n, bool loop, bool copy = true)
:Spline<ST, FT>(xs, ys, n, loop, copy)
{
if (!copy)
{
mMs = ms;
}
else
{
ST* tempMs = new ST[n];
std::memcpy(tempMs, ms, sizeof(ST) * n);
mMs = tempMs;
}
}
virtual ~Hermite()
{
if (this->mCopy)
delete mMs;
}
virtual ST Y(FT x) const
{
const size_t i = this->GetIndex(x);
const FT t = this->GetSubRange(i, x);
const ST y1 = this->GetY(i);
const ST y2 = this->GetY(i+1);
const ST m1 = GetM(i);
const ST m2 = GetM(i+1);
return Function::Hermite<ST, FT>(m1, y1, y2, m2, t);
}
protected:
///Returns a m value. Index will loop around.
ST GetM(int index) const
{
index = Function::Imod(index, this->mN);
assert(index >= 0);
assert(index < int(this->mN));
return mMs[index];
}
private:
const ST *mMs;
};
///General spline that calls a function of the form f(y1, y2, t). These splines are local in that they only
///ever consider the two nearest points.
template<typename T, typename ST = double, typename FT = double>
class LocalSpline : public Spline<ST, FT>
{
public:
LocalSpline(const FT* xs, const ST* ys, size_t n, bool loop, bool copy = true)
:Spline<ST, FT>(xs, ys, n, loop, copy){}
virtual ~LocalSpline(){};
virtual ST Y(FT x) const
{
const size_t i = this->GetIndex(x);
const FT t = this->GetSubRange(i, x);
return T()(this->GetY(i), this->GetY(i+1), t);
}
};
typedef LocalSpline<Function::Cosine> Cosine;
typedef LocalSpline<Function::Linear> Linear;
typedef LocalSpline<Function::LateStep> LateStep;
typedef LocalSpline<Function::NearestNeighbor> Step;
typedef LocalSpline<Function::SmoothStep> SmoothStep;
}
#endif