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
|
#pragma once
#include "StarMathCommon.hpp"
#include "StarArray.hpp"
#include "StarAlgorithm.hpp"
namespace Star {
enum class BoundMode {
Clamp,
Extrapolate,
Wrap
};
enum class InterpolationMode {
HalfStep,
Linear,
Cubic
};
template <typename T1, typename T2>
T2 angleLerp(T1 const& offset, T2 const& f0, T2 const& f1) {
return f0 + angleDiff(f0, f1) * offset;
}
template <typename T1, typename T2>
T2 sinEase(T1 const& offset, T2 const& f0, T2 const& f1) {
T1 w = (sin(offset * Constants::pi - Constants::pi / 2) + 1) / 2;
return f0 * (1 - w) + f1 * w;
}
template <typename T1, typename T2>
T2 lerp(T1 const& offset, T2 const& f0, T2 const& f1) {
return f0 * (1 - offset) + f1 * (offset);
}
template <typename T1, typename T2>
T2 lerpWithLimit(Maybe<T2> const& limit, T1 const& offset, T2 const& f0, T2 const& f1) {
if (limit && abs(f1 - f0) > *limit)
return f1;
return lerp(offset, f0, f1);
}
template <typename T1, typename T2>
T2 step(T1 threshold, T1 x, T2 a, T2 b) {
if (x < threshold)
return a;
else
return b;
}
template <typename T1, typename T2>
T2 halfStep(T1 x, T2 a, T2 b) {
if (x < 0.5)
return a;
else
return b;
}
template <typename T1, typename T2>
T2 cubic4(T1 const& x, T2 const& f0, T2 const& f1, T2 const& f2, T2 const& f3) {
// (-1/2 * f0 + 3/2 * f1 + -3/2 * f2 + 1/2 * f3) * x * x * x +
// ( 1 * f0 + -5/2 * f1 + 2 * f2 + -1/2 * f3) * x * x +
// (-1/2 * f0 + 0 * f1 + 1/2 * f2 + 0 * f3) * x +
// ( 0 * f0 + 1 * f1 + 0 * f2 + 0 * f3) * 1.0
return f1 + (f2 - f0 + (f0 * 2.0 - f1 * 5.0 + f2 * 4.0 - f3 + ((f1 - f2) * 3.0 + f3 - f0) * x) * x) * x * 0.5;
}
template <typename T1, typename T2>
T2 catmulRom4(T1 const& x, T2 const& f0, T2 const& f1, T2 const& f2, T2 const& f3) {
return ((f1 * 2) + (-f0 + f2) * x + (f0 * 2 - f1 * 5 + f2 * 4 - f3) * x * x
+ (-f0 + f1 * 3 - f2 * 3 + f3) * x * x * x)
* 0.5;
}
template <typename T1, typename T2>
T2 hermite2(T1 const& x, T2 const& a, T2 const& b) {
return a + (b - a) * x * x * (3 - 2 * x);
}
template <typename T1, typename T2>
T2 quintic2(T1 const& x, T2 const& a, T2 const& b) {
return a + (b - a) * x * x * x * (x * (x * 6 - 15) + 10);
}
template <typename WeightT>
struct LinearWeightOperator {
typedef WeightT Weight;
typedef Array<Weight, 2> WeightVec;
WeightVec operator()(Weight x) const {
return {1 - x, x};
}
};
template <typename WeightT>
struct StepWeightOperator {
typedef WeightT Weight;
typedef Array<Weight, 2> WeightVec;
StepWeightOperator(Weight threshold = 0.5) : threshold(threshold) {}
WeightVec operator()(Weight x) const {
if (x < threshold)
return {1, 0};
else
return {0, 1};
}
Weight threshold;
};
template <typename WeightT>
struct SinWeightOperator {
typedef WeightT Weight;
typedef Array<Weight, 2> WeightVec;
WeightVec operator()(Weight x) const {
Weight w = (sin(x * Constants::pi - Constants::pi / 2) + 1) / 2;
return {1 - w, w};
}
};
template <typename WeightT>
struct Hermite2WeightOperator {
typedef WeightT Weight;
typedef Array<Weight, 2> WeightVec;
WeightVec operator()(Weight x) const {
Weight w = x * x * (3 - 2 * x);
return {1 - w, w};
}
};
template <typename WeightT>
struct Quintic2WeightOperator {
typedef WeightT Weight;
typedef Array<Weight, 2> WeightVec;
WeightVec operator()(Weight x) const {
Weight w = x * x * x * (x * (x * 6 - 15) + 10);
return {1 - w, w};
}
};
// Setting 'LinearExtrapolate' flag to true changes the weights to be linear
// when x is outside of the range [0.0, 1.0]
template <typename WeightT>
struct Cubic4WeightOperator {
typedef WeightT Weight;
typedef Array<Weight, 4> WeightVec;
Cubic4WeightOperator(bool le = false) : linearExtrapolate(le) {}
WeightVec operator()(Weight x) const {
if (linearExtrapolate && x > 1) {
return {0, 0, 2 - x, x - 1};
} else if (linearExtrapolate && x < 0) {
return {-x, 1 + x, 0, 0};
} else {
// (-1/2 * f0 + 3/2 * f1 + -3/2 * f2 + 1/2 * f3) * x*x*x +
// ( 1 * f0 + -5/2 * f1 + 2 * f2 + -1/2 * f3) * x*x +
// (-1/2 * f0 + 0 * f1 + 1/2 * f2 + 0 * f3) * x +
// ( 0 * f0 + 1 * f1 + 0 * f2 + 0 * f3) * 1.0
Weight x2 = x * x;
Weight x3 = x2 * x;
return WeightVec(-0.5 * x3 + 1 * x2 - 0.5 * x,
1.5 * x3 + -2.5 * x2 + 1.0,
-1.5 * x3 + 2.0 * x2 + 0.5 * x,
0.5 * x3 - 0.5 * x2);
}
}
bool linearExtrapolate;
};
// Setting 'LinearExtrapolate' flag to true changes the weights to be linear
// when x is outside of the range [0.0, 1.0]
template <typename WeightT>
struct Catmul4WeightOperator {
typedef WeightT Weight;
typedef Array<Weight, 4> WeightVec;
Catmul4WeightOperator(bool le = false) : linearExtrapolate(le) {}
WeightVec operator()(Weight x) const {
if (linearExtrapolate && x > 1) {
return {0, 0, 2 - x, x - 1};
} else if (linearExtrapolate && x < 0) {
return {-x, 1 + x, 0, 0};
} else {
Weight x2 = x * x;
Weight x3 = x * x * x;
return {(-x3 + x2 * 2 - x) / 2, (x3 * 3 - x2 * 5 + 2) / 2, (-x3 * 3 + x2 * 4 + x) / 2, (x3 - x2) / 2};
}
}
bool linearExtrapolate;
};
template <typename Loctype, typename IndexType>
struct Bound2 {
IndexType i0;
IndexType i1;
Loctype offset;
};
// loc should be in "index space", meaning that 0 points exactly to the first
// element and extent - 1
// points exactly to the last element.
template <typename LocType, typename IndexType>
Bound2<LocType, IndexType> getBound2(LocType loc, IndexType extent, BoundMode bmode) {
Bound2<LocType, IndexType> bound;
if (extent <= 1) {
bound.i0 = bound.i1 = bound.offset = 0;
return bound;
}
bound.offset = 0;
if (bmode == BoundMode::Wrap) {
loc = pfmod<LocType>(loc, extent);
} else {
LocType newLoc = clamp<LocType>(loc, 0, extent - 1);
if (bmode == BoundMode::Extrapolate)
bound.offset += loc - newLoc;
loc = newLoc;
}
bound.i0 = IndexType(loc);
if (bound.i0 == extent - 1) {
if (bmode == BoundMode::Wrap) {
bound.i1 = 0;
} else {
bound.i1 = bound.i0;
bound.i0 -= 1;
}
} else {
bound.i1 = bound.i0 + 1;
}
bound.offset += loc - bound.i0;
return bound;
}
template <typename Loctype, typename IndexType>
struct Bound4 {
Bound4() {}
IndexType i0;
IndexType i1;
IndexType i2;
IndexType i3;
Loctype offset;
};
// loc should be in "index space", meaning that 0 points exactly to the first
// element and extent - 1
// points exactly to the last element.
template <typename LocType, typename IndexType>
Bound4<LocType, IndexType> getBound4(LocType loc, IndexType extent, BoundMode bmode) {
Bound4<LocType, IndexType> bound;
if (extent <= 1) {
bound.i0 = bound.i1 = bound.i2 = bound.i3 = bound.offset = 0;
return bound;
}
bound.offset = 0;
if (bmode == BoundMode::Wrap) {
loc = pfmod<LocType>(loc, extent);
} else {
LocType newLoc = clamp<LocType>(loc, 0, extent - 1);
if (bmode == BoundMode::Extrapolate)
bound.offset += loc - newLoc;
loc = newLoc;
}
bound.i1 = IndexType(loc);
if (bound.i1 == extent - 1) {
if (bmode == BoundMode::Wrap) {
bound.i0 = bound.i1 - 1;
bound.i2 = 0;
bound.i3 = 1;
} else {
bound.i1 = bound.i1 - 2;
bound.i0 = bound.i1 - 1;
bound.i2 = bound.i1 + 1;
bound.i3 = bound.i2 + 1;
}
} else if (bound.i1 == extent - 2) {
if (bmode == BoundMode::Wrap) {
bound.i0 = bound.i1 - 1;
bound.i2 = bound.i1 + 1;
bound.i3 = 0;
} else {
bound.i1 = bound.i1 - 1;
bound.i0 = bound.i1 - 1;
bound.i2 = bound.i1 + 1;
bound.i3 = bound.i2 + 1;
}
} else if (bound.i1 == 0) {
if (bmode == BoundMode::Wrap) {
bound.i0 = extent - 1;
bound.i2 = bound.i1 + 1;
bound.i3 = bound.i2 + 1;
} else {
bound.i1 = bound.i1 + 1;
bound.i0 = bound.i1 - 1;
bound.i2 = bound.i1 + 1;
bound.i3 = bound.i2 + 1;
}
} else {
bound.i0 = bound.i1 - 1;
bound.i2 = bound.i1 + 1;
bound.i3 = bound.i1 + 2;
}
bound.offset += loc - bound.i1;
return bound;
}
template <typename Container, typename Pos, typename WeightOp>
typename Container::value_type listInterpolate2(
Container const& cont, Pos x, WeightOp weightOp, BoundMode bmode = BoundMode::Clamp) {
if (cont.size() == 0) {
return typename Container::value_type();
} else if (cont.size() == 1) {
return cont[0];
} else {
auto bound = getBound2(x, cont.size(), bmode);
auto weights = weightOp(bound.offset);
return cont[bound.i0] * weights[0] + cont[bound.i1] * weights[1];
}
}
template <typename Container, typename Pos, typename WeightOp>
typename Container::value_type listInterpolate4(
Container const& cont, Pos x, WeightOp weightOp, BoundMode bmode = BoundMode::Clamp) {
if (cont.size() == 0) {
return typename Container::value_type();
} else if (cont.size() == 1) {
return cont[0];
} else {
auto bound = getBound4(x, cont.size(), bmode);
auto weights = weightOp(bound.offset);
return cont[bound.i0] * weights[0] + cont[bound.i1] * weights[1] + cont[bound.i2] * weights[2]
+ cont[bound.i3] * weights[3];
}
}
// Returns an index value (not integer) that represents the value that, if
// passed in as an index to a simple linear interpolation of the given
// container, would yield the given value. (In other words, this goes from
// function space to index space on a list of points). Useful for doing
// interpolation on functions that are unevenly spaced. Given container must
// be sorted. If there is an ambiguity on points due to repeat points, will
// choose the lower-most of the points.
template <typename Iterator, typename Pos, typename Comp, typename PosGetter>
Pos inverseLinearInterpolateLower(Iterator begin, Iterator end, Pos t, Comp&& comp, PosGetter&& posGetter) {
// Container must be at least size 2 for this to make sense.
if (begin == end || std::next(begin) == end)
return Pos();
Iterator i = std::lower_bound(std::next(begin), std::prev(end), t, std::forward<Comp>(comp));
--i;
Pos min = posGetter(*i);
Pos max = posGetter(*(++i));
Pos ipos = Pos(std::distance(begin, --i));
Pos dist = max - min;
if (dist == 0)
return ipos;
else
return ipos + (t - min) / dist;
}
template <typename Iterator, typename Pos>
Pos inverseLinearInterpolateLower(Iterator begin, Iterator end, Pos t) {
return inverseLinearInterpolateLower(begin, end, t, std::less<Pos>(), identity());
}
// Same as inverseLinearInterpolateLower, except chooses the upper most of the
// points in the ambiguous case.
template <typename Iterator, typename Pos, typename Comp, typename PosGetter>
Pos inverseLinearInterpolateUpper(Iterator begin, Iterator end, Pos t, Comp&& comp, PosGetter&& posGetter) {
// Container must be at least size 2 for this to make sense.
if (begin == end || std::next(begin) == end)
return Pos();
Iterator i = std::upper_bound(std::next(begin), std::prev(end), t, std::forward<Comp>(comp));
--i;
Pos min = posGetter(*i);
Pos max = posGetter(*(++i));
Pos ipos = Pos(std::distance(begin, --i));
Pos dist = max - min;
if (dist == 0)
return ipos + 1;
else
return ipos + (t - min) / dist;
}
template <typename Iterator, typename Pos>
Pos inverseLinearInterpolateUpper(Iterator begin, Iterator end, Pos t) {
return inverseLinearInterpolateUpper(begin, end, t, std::less<Pos>(), identity());
}
template <typename XContainer, typename YContainer, typename PositionType, typename WeightOp>
typename YContainer::value_type parametricInterpolate2(XContainer const& xvals,
YContainer const& yvals,
PositionType const& position,
WeightOp weightOp,
BoundMode bmode) {
starAssert(xvals.size() != 0);
starAssert(xvals.size() == yvals.size());
if (yvals.size() == 1)
return yvals[0];
PositionType ipos = inverseLinearInterpolateLower(xvals.begin(), xvals.end(), position);
return listInterpolate2(yvals, ipos, weightOp, bmode);
}
template <typename XContainer, typename YContainer, typename PositionType, typename WeightOp>
typename YContainer::value_type parametricInterpolate4(XContainer const& xvals,
YContainer const& yvals,
PositionType const& position,
WeightOp weightOp,
BoundMode bmode) {
starAssert(xvals.size() != 0);
starAssert(xvals.size() == yvals.size());
if (yvals.size() == 1)
return yvals[0];
PositionType ipos = inverseLinearInterpolateLower(xvals.begin(), xvals.end(), position);
return listInterpolate4(yvals, ipos, weightOp, bmode);
}
}
|