-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathSplines.cpp
More file actions
3695 lines (2892 loc) · 100 KB
/
Copy pathSplines.cpp
File metadata and controls
3695 lines (2892 loc) · 100 KB
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Splines.cpp
//
// Cubic spline utilities for 1D/2D/3D
//
// Andrew Willmott
//
#include "Splines.hpp"
#include <float.h>
using namespace SL;
using std::vector;
#define SL_ASSERT_INDEX(M_I, M_N) \
SL_ASSERT((unsigned int)(M_I) < (unsigned int)(M_N))
namespace
{
inline float Clamp(float x, float minX, float maxX)
{
if (x < minX)
return minX;
if (x > maxX)
return maxX;
return x;
}
inline float InvSqrtFast(float x)
{
float xhalf = 0.5f * x;
int32_t i = (int32_t&) x;
i = 0x5f375a86 - (i >> 1);
x = (float&) i;
x = x * (1.5f - xhalf * x * x);
return x;
}
inline bool Larger(const Bounds2& bb, float t) { Vec2f d = bb.mMax - bb.mMin; return d.x > t || d.y > t; }
inline bool Larger(const Bounds3& bb, float t) { Vec3f d = bb.mMax - bb.mMin; return d.x > t || d.y > t || d.z > t; }
inline bool Intersects(const Bounds2& a, const Bounds2& b)
{
return a.mMax.x >= b.mMin.x && a.mMin.x <= b.mMax.x
&& a.mMax.y >= b.mMin.y && a.mMin.y <= b.mMax.y;
}
inline bool Intersects(const Bounds3& a, const Bounds3& b)
{
return a.mMax.x >= b.mMin.x && a.mMin.x <= b.mMax.x
&& a.mMax.y >= b.mMin.y && a.mMin.y <= b.mMax.y
&& a.mMax.z >= b.mMin.z && a.mMin.z <= b.mMax.z;
}
inline void Add(Bounds2& a, const Bounds2& b)
{
if (a.mMin.x > b.mMin.x) a.mMin.x = b.mMin.x; else if (a.mMax.x < b.mMax.x) a.mMax.x = b.mMax.x;
if (a.mMin.y > b.mMin.y) a.mMin.y = b.mMin.y; else if (a.mMax.y < b.mMax.y) a.mMax.y = b.mMax.y;
}
inline void Add(Bounds3& a, const Bounds3& b)
{
if (a.mMin.x > b.mMin.x) a.mMin.x = b.mMin.x; else if (a.mMax.x < b.mMax.x) a.mMax.x = b.mMax.x;
if (a.mMin.y > b.mMin.y) a.mMin.y = b.mMin.y; else if (a.mMax.y < b.mMax.y) a.mMax.y = b.mMax.y;
if (a.mMin.z > b.mMin.z) a.mMin.z = b.mMin.z; else if (a.mMax.z < b.mMax.z) a.mMax.z = b.mMax.z;
}
template<class T> inline int size_i(const T& container) { return int(container.size()); }
}
namespace
{
// Utilities
inline Vec2f ArcError2(Vec4f s)
// Returns squared displacement from linear (b0_b3) for hull points b1/b2
{
float w = s.w - s.x;
float ty = s.x + w * 1.0f / 3.0f - s.y;
float tz = s.x + w * 2.0f / 3.0f - s.z;
float d2 = 1.0f / (sqr(w) + 1.0f);
return Vec2f(sqr(ty) * d2, sqr(tz) * d2);
}
template<class T, class S> inline S SplineFromPointsT(const char* p8, size_t stride, int i0, int i1, int i2, int i3, float tension)
{
T p0 = *(T*) (p8 + i0 * stride);
T p1 = *(T*) (p8 + i1 * stride);
T p2 = *(T*) (p8 + i2 * stride);
T p3 = *(T*) (p8 + i3 * stride);
float s = (1.0f - tension) * (1.0f / 6.0f);
T pb1 = p1 + s * (p2 - p0);
T pb2 = p2 - s * (p3 - p1);
return BezierSpline(p1, pb1, pb2, p2);
}
template<class T, class S> inline S SplineFromPointsDynamicT(const char* p8, size_t stride, int i0, int i1, int i2, int i3, float tension, float ratio = 0.666f)
{
// The standard algorithm above has an issue when a small segment is followed by a very large one.
// ||pb1|| can wind up being much larger than ||p0_p1||, which leads to the spline rep looping
// back on itself. Basically, the shared tangent's length is much larger than the first segment's size.
//
// One approach to fixing is to virtually split the spline, which effectively means
// using p2' = p1 + t(p2 - p1) with t << 1.
// Another approach is to adjust 's' on the fly to ensure p2 - p0 is not >> ||p1 - p0||
// We need
T p0 = *(T*) (p8 + i0 * stride);
T p1 = *(T*) (p8 + i1 * stride);
T p2 = *(T*) (p8 + i2 * stride);
T p3 = *(T*) (p8 + i3 * stride);
float s = (1.0f - tension) * (1.0f / 6.0f);
T d1 = s * (p2 - p0);
T d2 = s * (p3 - p1);
float l2d1 = sqrlen(d1);
float l2d2 = sqrlen(d2);
ratio *= ratio;
float l1 = sqrlen(p1 - p0) * ratio;
float l2 = sqrlen(p2 - p1) * ratio;
float l3 = sqrlen(p3 - p2) * ratio;
if (l1 > 0)
l1 = vl_min(l1, l2);
else
l1 = l2;
if (l3 > 0)
l3 = vl_min(l3, l2);
else
l3 = l2;
if (l2d1 > l1)
d1 *= sqrtf(l1 / l2d1);
if (l2d2 > l3)
d2 *= sqrtf(l3 / l2d2);
T pb1 = p1 + d1;
T pb2 = p2 - d2;
return BezierSpline(p1, pb1, pb2, p2);
}
template<typename T, typename S> int SplinesFromSamples(int numPoints, const T pi[], S splines[], float tension, size_t stride)
{
SL_ASSERT(numPoints >= 0);
const char* p8 = (const char*) pi;
switch (numPoints)
{
case 0:
return 0;
case 1:
*splines = SplineFromPointsT<T, S>(p8, stride, 0, 0, 0, 0, tension);
return 1;
case 2:
*splines = SplineFromPointsT<T, S>(p8, stride, 0, 0, 1, 1, tension);
return 1;
}
*splines++ = SplineFromPointsT<T, S>(p8, stride, 0, 0, 1, 2, tension);
for (int i = 0; i < numPoints - 3; i++)
{
*splines++ = SplineFromPointsT<T, S>(p8, stride, 0, 1, 2, 3, tension);
p8 += stride;
}
*splines++ = SplineFromPointsT<T, S>(p8, stride, 0, 1, 2, 2, tension);
return numPoints - 1;
}
template<typename T, typename S> int SplinesFromSamplesDynamic(int numPoints, const T pi[], S splines[], float tension, float ratio, size_t stride)
{
SL_ASSERT(numPoints >= 0);
const char* p8 = (const char*) pi;
switch (numPoints)
{
case 0:
return 0;
case 1:
*splines = SplineFromPointsT<T, S>(p8, stride, 0, 0, 0, 0, tension);
return 1;
case 2:
*splines = SplineFromPointsT<T, S>(p8, stride, 0, 0, 1, 1, tension);
return 1;
}
*splines++ = SplineFromPointsDynamicT<T, S>(p8, stride, 0, 0, 1, 2, tension, ratio);
for (int i = 0; i < numPoints - 3; i++)
{
*splines++ = SplineFromPointsDynamicT<T, S>(p8, stride, 0, 1, 2, 3, tension, ratio);
p8 += stride;
}
*splines++ = SplineFromPointsDynamicT<T, S>(p8, stride, 0, 1, 2, 2, tension, ratio);
return numPoints - 1;
}
// Monotone Piecewise Cubic Interpolation
inline int TriSign(float x, float eps = 1e-6f)
{
if (x < -eps)
return -1;
if (x > +eps)
return +1;
return 0;
}
// Given dv = Bezier delta from corresponding adjacent endpoint, and the
// corresponding end and hull values, return a clamped version of dv that avoids overshoots
float CubicMonoDelta(float dv, float d0, float d1)
{
if (TriSign(d0) != TriSign(d1))
return 0.0f;
return Clamp(dv, vl_max(-fabsf(d0), -fabsf(d1)), vl_min(+fabsf(d0), +fabsf(d1)));
}
// Reminder that Spline1 holds Bezier coefficients, so
// .x = left endpoint
// .w = right endpoint
inline Spline1 MonotonicLeft(float x0, Spline1 s1)
{
float dv0 = s1.y - s1.x;
float d0 = s1.x - x0;
float d1 = s1.w - s1.x;
dv0 = CubicMonoDelta(dv0, d0, d1);
s1.y = s1.x + dv0;
return s1;
}
inline Spline1 MonotonicRight(Spline1 s1, float x2)
{
float dv1 = s1.w - s1.z;
float d1 = s1.w - s1.x;
float d2 = x2 - s1.w;
dv1 = CubicMonoDelta(dv1, d1, d2);
s1.z = s1.w - dv1;
return s1;
}
inline Spline1 Monotonic(float x0, Spline1 s1, float x2)
{
float dv0 = s1.y - s1.x;
float dv1 = s1.w - s1.z;
float d0 = s1.x - x0;
float d1 = s1.w - s1.x;
float d2 = x2 - s1.w;
dv0 = CubicMonoDelta(dv0, d0, d1);
dv1 = CubicMonoDelta(dv1, d1, d2);
s1.y = s1.x + dv0;
s1.z = s1.w - dv1;
return s1;
}
bool AdvanceAgent(int* indexInOut, float* tInOut, int numSplines)
// Update index for t if necessary, but don't run off array
{
int& index = *indexInOut;
float& t = *tInOut;
while (t < 0.0f)
{
if (index <= 0)
return false;
t += 1.0f;
index--;
}
while (t > 1.0f)
{
if (index >= numSplines - 1)
return false;
t -= 1.0f;
index++;
}
return true;
}
}
////////////////////////////////////////////////////////////////////////////////
// 1D
////////////////////////////////////////////////////////////////////////////////
Spline1 SL::BezierSpline(float p0, float p1, float p2, float p3)
{
return Spline1(p0, p1, p2, p3);
}
Spline1 SL::HermiteSpline(float p0, float p1, float v0, float v1)
{
float pb1 = p0 + (1.0f / 3.0f) * v0;
float pb2 = p1 - (1.0f / 3.0f) * v1;
return Spline1(p0, pb1, pb2, p1);
}
Spline1 SL::CatmullRomSpline(float p0, float p1, float p2, float p3)
{
float pb1 = p1 + (1.0f / 6.0f) * (p2 - p0);
float pb2 = p2 - (1.0f / 6.0f) * (p3 - p1);
return Spline1(p1, pb1, pb2, p2);
}
Spline1 SL::InterpolatingSpline(float p0, float p1, float p2, float p3)
{
float pb1 = (1.0f / 3.0f) * p3 - (3.0f / 2.0f) * p2 + 3.0f * p1 - (5.0f / 6.0f) * p0;
float pb2 = (1.0f / 3.0f) * p0 - (3.0f / 2.0f) * p1 + 3.0f * p2 - (5.0f / 6.0f) * p3;
return Spline1(p0, pb1, pb2, p3);
}
Spline1 SL::LineSpline(float p0, float p1)
{
float v = (p1 - p0);
float pb1 = p0 + (1.0f / 3.0f) * v;
float pb2 = p1 - (1.0f / 3.0f) * v;
return Spline1(p0, pb1, pb2, p1);
}
Spline1 SL::CubicSpline(const Vec4f& c)
//Returns Bezier weights for the given cubic coeffs
{
return Spline1
(
c.x,
c.x + (1.0f / 3.0f) * c.y,
c.x + (2.0f / 3.0f) * c.y + (1.0f / 3.0f) * c.z,
c.x + c.y + c.z + c.w
);
}
int SL::SplinesFromPoints(int numPoints, const float pi[], Spline1 splines[], float tension, size_t stride)
{
return ::SplinesFromSamples(numPoints, pi, splines, tension, stride);
}
int SL::SplinesFromPointsDynamic(int numPoints, const float pi[], Spline1 splines[], float tension, float ratio, size_t stride)
{
return ::SplinesFromSamplesDynamic(numPoints, pi, splines, tension, ratio, stride);
}
int SL::SplinesFromBezier(int numPoints, const float points[], const float hullPoints[], Spline1 splines[], bool split)
{
int numSplines = split ? numPoints / 2 : numPoints - 1;
int advance = split ? 2 : 1;
for (int i = 0; i < numSplines; i++)
{
splines[i] = BezierSpline(points[0], hullPoints[0], hullPoints[1], points[1]);
points += advance;
hullPoints += advance;
}
return numSplines;
}
int SL::SplinesFromHermite(int numPoints, const float points[], const float tangents [], Spline1 splines[], bool split)
{
int numSplines = split ? numPoints / 2 : numPoints - 1;
int advance = split ? 2 : 1;
for (int i = 0; i < numSplines; i++)
{
splines[i] = HermiteSpline(points[0], points[1], tangents[0], tangents[1]);
points += advance;
tangents += advance;
}
return numSplines;
}
void SL::MakeMonotonic(int n, Spline1 splines[], bool closed)
{
if (n >= 2)
{
if (closed)
splines[0] = Monotonic(splines[n - 1].x, splines[0], splines[1].w);
else
splines[0] = MonotonicRight(splines[0], splines[1].w);
}
else if (n >= 1)
splines[0] = splines[0];
for (int i = 1; i < n - 1; i++)
splines[i] = Monotonic(splines[i - 1].x, splines[i], splines[i + 1].w);
if (n >= 2)
{
if (closed)
splines[n - 1] = Monotonic(splines[n - 2].x, splines[n - 1], splines[0].w);
else
splines[n - 1] = MonotonicLeft(splines[n - 2].x, splines[n - 1]);
}
}
Vec2f SL::FastBounds(const Spline1& s)
//Returns bounds of the convex hull
{
Vec2f b01;
if (s.x <= s.y)
b01 = Vec2f(s.x, s.y);
else
b01 = Vec2f(s.y, s.x);
Vec2f b23;
if (s.z <= s.w)
b23 = Vec2f(s.z, s.w);
else
b23 = Vec2f(s.w, s.z);
return Bounds1
(
vl_min(b01.x, b23.x),
vl_max(b01.y, b23.y)
);
}
Bounds1 SL::ExactBounds(const Spline1& spline)
//Returns accurate bounds taking extrema into account.
{
Bounds1 bounds;
// First take endpoints into account
if (spline.x <= spline.w)
{
bounds.x = spline.x;
bounds.y = spline.w;
}
else
{
bounds.x = spline.w;
bounds.y = spline.x;
}
// Now find extrema via standard quadratic equation: c.t' = 0
Vec4f c = CubicCoeffs(spline);
float c33 = 3.0f * c.w;
float cx2 = c.z * c.z - c33 * c.y;
if (cx2 < 0.0f)
return bounds; // no roots!
float invC33 = 1.0f / c33;
float ct = -c.z * invC33;
float cx = sqrtf(cx2) * invC33;
float t0 = ct + cx;
float t1 = ct - cx;
// Must make sure the roots are within the spline interval
if (t0 > 0.0f && t0 < 1.0f)
{
float x = c.x + (c.y + (c.z + c.w * t0) * t0) * t0;
if (bounds.x > x)
bounds.x = x;
else if (bounds.y < x)
bounds.y = x;
}
if (t1 > 0.0f && t1 < 1.0f)
{
float x = c.x + (c.y + (c.z + c.w * t1) * t1) * t1;
if (bounds.x > x)
bounds.x = x;
else if (bounds.y < x)
bounds.y = x;
}
return bounds;
}
void SL::Split(const Spline1& spline, Spline1* spline0, Spline1* spline1)
{
float q0 = (spline.x + spline.y) * 0.5f;
float q1 = (spline.y + spline.z) * 0.5f;
float q2 = (spline.z + spline.w) * 0.5f;
float r0 = (q0 + q1) * 0.5f;
float r1 = (q1 + q2) * 0.5f;
float s0 = (r0 + r1) * 0.5f;
float sx = spline.x;
float sw = spline.w;
*spline0 = Spline1(sx, q0, r0, s0);
*spline1 = Spline1(s0, r1, q2, sw);
}
void SL::Split(const Spline1& spline, float t, Spline1* spline0, Spline1* spline1)
{
// This is based on one step of De Casteljau's algorithm
float q0 = lerp(spline.x, spline.y, t);
float q1 = lerp(spline.y, spline.z, t);
float q2 = lerp(spline.z, spline.w, t);
float r0 = lerp(q0, q1, t);
float r1 = lerp(q1, q2, t);
float s0 = lerp(r0, r1, t);
float sx = spline.x;
float sw = spline.w;
*spline0 = Spline1(sx, q0, r0, s0);
*spline1 = Spline1(s0, r1, q2, sw);
}
bool SL::Join(const Spline1& s0, const Spline1& s1, Spline1* sOut)
{
if (s0.w != s1.x) // early out
return false;
// backwards solve from left
float x0 = s0.x;
float y0 = 2 * s0.y - x0;
float z0 = 4 * s0.z - x0 - 2 * y0;
float w0 = 8 * s0.w - x0 - 3 * (y0 + z0);
// backwards solve from right
float w1 = s1.w;
float z1 = 2 * s1.z - w1;
float y1 = 4 * s1.y - w1 - 2 * z1;
float x1 = 8 * s1.x - w1 - 3 * (y1 + z1);
*sOut = Spline1(x0, y0, z1, w1); // use most stable terms
float e2 = sqr(x0 - x1) + sqr(y0 - y1) + sqr(z0 - z1) + sqr(w0 - w1);
return e2 <= 1e-4f * sqr(s0.x - s1.w); // sq error relative to overall size
}
bool SL::Join(const Spline1& s0, const Spline1& s1, float t, Spline1* s)
{
if (t <= 0.0f)
{
*s = s1;
return true;
}
if (t >= 1.0f)
{
*s = s0;
return true;
}
float u = 1 - t;
float it = 1.0f / t;
float iu = 1.0f / u;
float q0 = s0.y;
float q2 = s1.z;
float x0 = s0.x;
float y0 = (q0 - u * x0) * it; // backsolve
float w1 = s1.w;
float z1 = (q2 - t * w1) * iu; // backsolve
*s = Spline1(x0, y0, z1, w1);
float e2 = sqr(s0.w - s1.x);
float r0 = s0.z;
float r1 = s1.y;
// To calculate internal error robustly, we look at the reconstruction of q1 from s0/s1, and weight the differences by their size.
// This helps avoid epsilon errors when t gets close to 0 or 1.
float q1 = u * y0 + t * z1;
float q10 = (r0 - u * q0) * it; // r0 = u q0 + t q1 -> q1' = (r0 - u q0) / t
float q11 = (r1 - t * q2) * iu; // r1 = u q1 + t q2 -> q1' = (r1 - t q2) / u
float e0 = (q10 - q1) * (s0.x - s0.w);
float e1 = (q11 - q1) * (s1.x - s1.w);
e2 += sqr(e0) + sqr(e1);
return e2 <= 1e-4f * sqr(s0.x - s1.w); // sq error relative to overall size
}
Spline1 SL::Trim(const Spline1& splineIn, float t0, float t1)
{
SL_ASSERT(t0 <= t1);
Spline1 spline(splineIn);
if (t0 > 0.0f)
{
float t = t0;
float q0 = lerp(spline.x, spline.y, t);
float q1 = lerp(spline.y, spline.z, t);
float q2 = lerp(spline.z, spline.w, t);
float r0 = lerp(q0, q1, t);
float r1 = lerp(q1, q2, t);
float s0 = lerp(r0, r1, t);
spline = Spline1(s0, r1, q2, spline.w);
}
if (t1 < 1.0f)
{
float t = (t1 - t0) / (1.0f - t0);
float q0 = lerp(spline.x, spline.y, t);
float q1 = lerp(spline.y, spline.z, t);
float q2 = lerp(spline.z, spline.w, t);
float r0 = lerp(q0, q1, t);
float r1 = lerp(q1, q2, t);
float s0 = lerp(r0, r1, t);
spline = Spline1(spline.x, q0, r0, s0);
}
return spline;
}
////////////////////////////////////////////////////////////////////////////////
// 2D
////////////////////////////////////////////////////////////////////////////////
Spline2 SL::BezierSpline(Vec2f p0, Vec2f p1, Vec2f p2, Vec2f p3)
{
return Spline2
{
Spline1(p0.x, p1.x, p2.x, p3.x),
Spline1(p0.y, p1.y, p2.y, p3.y),
};
}
Spline2 SL::HermiteSpline(Vec2f p0, Vec2f p1, Vec2f v0, Vec2f v1)
{
Vec2f pb1 = p0 + (1.0f / 3.0f) * v0;
Vec2f pb2 = p1 - (1.0f / 3.0f) * v1;
return BezierSpline(p0, pb1, pb2, p1);
}
Spline2 SL::CatmullRomSpline(Vec2f p0, Vec2f p1, Vec2f p2, Vec2f p3)
{
Vec2f pb1 = p1 + (1.0f / 6.0f) * (p2 - p0);
Vec2f pb2 = p2 - (1.0f / 6.0f) * (p3 - p1);
return BezierSpline(p1, pb1, pb2, p2);
}
Spline2 SL::InterpolatingSpline(Vec2f p0, Vec2f p1, Vec2f p2, Vec2f p3)
{
Vec2f pb1 = (1.0f / 3.0f) * p3 - (3.0f / 2.0f) * p2 + 3.0f * p1 - (5.0f / 6.0f) * p0;
Vec2f pb2 = (1.0f / 3.0f) * p0 - (3.0f / 2.0f) * p1 + 3.0f * p2 - (5.0f / 6.0f) * p3;
return BezierSpline(p0, pb1, pb2, p3);
}
Spline2 SL::LineSpline(Vec2f p0, Vec2f p1)
{
Vec2f v = (p1 - p0);
Vec2f pb1 = p0 + (1.0f / 3.0f) * v;
Vec2f pb2 = p1 - (1.0f / 3.0f) * v;
return BezierSpline(p0, pb1, pb2, p1);
}
namespace
{
const float kCircleOffset = 4.0f / 3.0f * (sqrtf(2.0f) - 1.0f);
const Spline1 kQuarterB0(1.0f, 1.0f, kCircleOffset, 0.0f);
const Spline1 kQuarterB1(0.0f, kCircleOffset, 1.0f, 1.0f);
}
Spline2 SL::QuadrantSpline(Vec2f p, float r, int quadrant)
{
SL_ASSERT(quadrant >= 0 && quadrant < 4);
Spline2 s;
switch (quadrant)
{
case 0:
s.xb = kQuarterB0;
s.yb = kQuarterB1;
break;
case 1:
s.xb = -kQuarterB1;
s.yb = kQuarterB0;
break;
case 2:
s.xb = -kQuarterB0;
s.yb = -kQuarterB1;
break;
case 3:
s.xb = kQuarterB1;
s.yb = -kQuarterB0;
break;
}
s.xb = s.xb * r + Spline1(p.x, p.x, p.x, p.x);
s.yb = s.yb * r + Spline1(p.y, p.y, p.y, p.y);
return s;
}
void SL::CircleSplines(Vec2f p, float r, Spline2 splines[4])
{
for (int i = 0; i < 4; i++)
splines[i] = QuadrantSpline(p, r, i);
}
int SL::SplinesFromPoints(int numPoints, const Vec2f pi[], Spline2 splines[], float tension, size_t stride)
{
return ::SplinesFromSamples(numPoints, pi, splines, tension, stride);
}
int SL::SplinesFromPointsDynamic(int numPoints, const Vec2f pi[], Spline2 splines[], float tension, float ratio, size_t stride)
{
return ::SplinesFromSamplesDynamic(numPoints, pi, splines, tension, ratio, stride);
}
int SL::SplinesFromBezier(int numPoints, const Vec2f points[], const Vec2f hullPoints[], Spline2 splines[], bool split)
{
int numSplines = split ? numPoints / 2 : numPoints - 1;
int advance = split ? 2 : 1;
for (int i = 0; i < numSplines; i++)
{
splines[i] = BezierSpline(points[0], hullPoints[0], hullPoints[1], points[1]);
points += advance;
hullPoints += advance;
}
return numSplines;
}
int SL::SplinesFromHermite(int numPoints, const Vec2f points[], const Vec2f tangents [], Spline2 splines[], bool split)
{
int numSplines = split ? numPoints / 2 : numPoints - 1;
int advance = split ? 2 : 1;
for (int i = 0; i < numSplines; i++)
{
splines[i] = HermiteSpline(points[0], points[1], tangents[0], tangents[1]);
points += advance;
tangents += advance;
}
return numSplines;
}
void SL::MakeMonotonic(int n, Spline2 splines[], bool closed)
{
if (n >= 2)
{
if (closed)
{
splines[0].xb = Monotonic(splines[n - 1].xb.x, splines[0].xb, splines[1].xb.w);
splines[0].yb = Monotonic(splines[n - 1].yb.x, splines[0].yb, splines[1].yb.w);
}
else
{
splines[0].xb = MonotonicRight(splines[0].xb, splines[1].xb.w);
splines[0].yb = MonotonicRight(splines[0].yb, splines[1].yb.w);
}
}
else if (n >= 1)
splines[0] = splines[0];
for (int i = 1; i < n - 1; i++)
{
splines[i].xb = Monotonic(splines[i - 1].xb.x, splines[i].xb, splines[i + 1].xb.w);
splines[i].yb = Monotonic(splines[i - 1].yb.x, splines[i].yb, splines[i + 1].yb.w);
}
if (n >= 2)
{
if (closed)
{
splines[n - 1].xb = Monotonic(splines[n - 2].xb.x, splines[n - 1].xb, splines[0].xb.w);
splines[n - 1].yb = Monotonic(splines[n - 2].yb.x, splines[n - 1].yb, splines[0].yb.w);
}
else
{
splines[n - 1].xb = MonotonicLeft(splines[n - 2].xb.x, splines[n - 1].xb);
splines[n - 1].yb = MonotonicLeft(splines[n - 2].yb.x, splines[n - 1].yb);
}
}
}
namespace
{
inline Vec2f Evaluate(const Spline2& spline, const Vec4f& w)
// Evaluate spline with given weights
{
return Vec2f
(
dot(spline.xb, w),
dot(spline.yb, w)
);
}
}
Vec2f SL::Position(const Spline2& spline, float t)
{
return Evaluate(spline, BezierWeights(t));
}
Vec2f SL::Velocity(const Spline2& spline, float t)
{
return Evaluate(spline, BezierWeightsD1(t));
}
Vec2f SL::Acceleration(const Spline2& spline, float t)
{
return Evaluate(spline, BezierWeightsD2(t));
}
Vec2f SL::Jerk(const Spline2& spline, float)
{
return Evaluate(spline, kBezierWeightsD3);
}
float SL::Curvature(const Spline2& spline, float t)
{
Vec2f v = Velocity (spline, t);
Vec2f a = Acceleration(spline, t);
float avCrossLen = fabsf(v.x * a.y - v.y * a.x);
float vLen = len(v);
if (vLen == 0.0f)
return 0.0f;
return avCrossLen / (vLen * vLen * vLen);
}
Mat2f SL::Frame(const Spline2& spline, float t)
{
Vec2f v = Velocity (spline, t);
Vec2f a = Acceleration(spline, t);
Mat2f frame;
frame.x = norm_safe(v); // x = forward
frame.y = norm_safe(a - frame.x * dot(frame.x, a)); // y = left or right, pointing in the direction of path turn
return frame;
}
void SL::Frame(const Spline2& spline, float t, Mat2f* frameInOut)
{
// Issues:
// 'b' can flip sign as 'a' moves from one side of the curve to the other
// 'a' can go to zero on flat sections of the curve. (In which case we should maintain previous values.)
// 'v' can go to zero at inflection points (ditto).
// Generally we want to preserve the tangent direction above everything, then the up vector, and last the right vector.
// E.g., if agent is going into a point and coming back out, we desire that the right vector flips.
Vec2f v = Velocity (spline, t);
Mat2f& frame = *frameInOut;
Vec2f T;
if (sqrlen(v) >= 1e-3f)
T = norm(v);
else
T = frame.x; // fall back to keeping existing tangent
Vec2f N = cross(T);
if (dot(N, frame.y) < 0.0f)
N = -N; // correct b to continue to point same way
frame.x = T; // x = forward
frame.y = N; // y = left/right
}
Mat2f SL::FrameX(const Spline2& spline, float t)
{
Vec2f v = Velocity(spline, t);
Mat2f frame;
frame.x = norm_safe(v); // x = forward
frame.y = cross(frame.x); // y = left always
return frame;
}
float SL::LengthEstimate(const Spline2& s, float* error)
{
// Our convex hull is p0, p1, p2, p3, so p0_p3 is our minimum possible length, and p0_p1 + p1_p2 + p2_p3 our maximum.
float d03 = sqr(s.xb.x - s.xb.w) + sqr(s.yb.x - s.yb.w);
float d01 = sqr(s.xb.x - s.xb.y) + sqr(s.yb.x - s.yb.y);
float d12 = sqr(s.xb.y - s.xb.z) + sqr(s.yb.y - s.yb.z);
float d23 = sqr(s.xb.z - s.xb.w) + sqr(s.yb.z - s.yb.w);
float minLength = sqrtf(d03);
float maxLength = sqrtf(d01) + sqrtf(d12) + sqrtf(d23);
minLength *= 0.5f;
maxLength *= 0.5f;
*error = maxLength - minLength;
return minLength + maxLength;
}
float SL::Length(const Spline2& s, float maxError)
{
float error;
float length = LengthEstimate(s, &error);
if (error > maxError)
{
Spline2 s0;
Spline2 s1;
Split(s, &s0, &s1);
return Length(s0, maxError) + Length(s1, maxError);
}
return length;
}
float SL::Length(const Spline2& s, float t0, float t1, float maxError)
{
SL_ASSERT(t0 >= 0.0f && t0 <= 1.0f);
SL_ASSERT(t1 >= 0.0f && t1 <= 1.0f);
SL_ASSERT(t0 <= t1);
Spline2 s0, s1;
if (t0 <= 0.0f)
{
if (t1 == 1.0f)
return Length(s, maxError);
Split(s, t1, &s0, &s1);
return Length(s0, maxError);
}
else if (t1 >= 1.0f)
{
Split(s, t0, &s0, &s1);
return Length(s1, maxError);
}
else
{
Split(s, t0, &s0, &s1);
if (t1 == 1.0f)
return Length(s1, maxError);
Split(s1, (t1 - t0) / (1.0f - t0), &s0, &s1);
return Length(s0, maxError);
}
}
Bounds2 SL::FastBounds(const Spline2& spline)