-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spline.cpp
2630 lines (2046 loc) · 72.5 KB
/
Spline.cpp
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
#include "Spline.hpp"
#include <float.h>
#include <math.h>
using namespace SplineLib;
namespace
{
// Mini VL
inline float sqr(float x) { return x * x; }
inline float lerp(float a, float b, float t) { return (1.0f - t) * a + t * b; }
inline Vec2f operator+(Vec2f a, Vec2f b) { return { a.x + b.x, a.y + b.y }; }
inline Vec2f operator-(Vec2f a, Vec2f b) { return { a.x - b.x, a.y - b.y }; }
inline Vec2f operator*(float s, Vec2f a) { return { s * a.x, s * a.y }; }
inline float dot(Vec2f a, Vec2f b) { return a.x * b.x + a.y * b.y; }
inline float len (Vec2f v) { return sqrtf(v.x * v.x + v.y * v.y); }
inline float sqrlen (Vec2f v) { return v.x * v.x + v.y * v.y; }
inline Vec2f norm_safe(Vec2f v) { return (1.0f / (len(v) + 1e-8f)) * v; }
inline Vec2f cross (Vec2f v) { return { -v.y, v.x }; }
inline Vec2f abs (Vec2f v) { return { fabsf(v.x), fabsf(v.y) }; }
inline Vec3f operator-(Vec3f v) { return { -v.x, -v.y, -v.z }; }
inline Vec3f operator+(Vec3f a, Vec3f b) { return { a.x + b.x, a.y + b.y, a.z + b.z}; }
inline Vec3f operator-(Vec3f a, Vec3f b) { return { a.x - b.x, a.y - b.y, a.z - b.z}; }
inline Vec3f operator*(float s, Vec3f a) { return { s * a.x, s * a.y, s * a.z}; }
inline float dot (Vec3f a, Vec3f b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
inline float len (Vec3f v) { return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z); }
inline float sqrlen (Vec3f v) { return v.x * v.x + v.y * v.y + v.z * v.z; }
inline Vec3f norm (Vec3f v) { return (1.0f / len(v)) * v; }
inline Vec3f norm_safe(Vec3f v) { return (1.0f / (len(v) + 1e-8f)) * v; }
inline Vec3f abs (Vec3f v) { return { fabsf(v.x), fabsf(v.y), fabsf(v.z) }; }
inline Vec3f cross (Vec3f a, Vec3f b) { return { a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x }; }
inline Vec3f cross_z (Vec3f v) { return { v.y, -v.x, 0.0f }; }
inline Vec4f operator-(Vec4f v) { return { -v.x, -v.y, -v.z, -v.w }; }
inline Vec4f operator+(Vec4f a, Vec4f b) { return { a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w}; }
inline Vec4f operator-(Vec4f a, Vec4f b) { return { a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w}; }
inline Vec4f operator*(float s, Vec4f a) { return { s * a.x, s * a.y, s * a.z, s * a.w}; }
inline float dot (Vec4f a, Vec4f b) { return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; }
inline float Max(float a, float b) { return b < a ? a : b; }
inline float Min(float a, float b) { return a < b ? a : b; }
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 Bounds2f& bb, float t) { Vec2f d = bb.max - bb.min; return d.x > t || d.y > t; }
inline bool Larger(const Bounds3f& bb, float t) { Vec3f d = bb.max - bb.min; return d.x > t || d.y > t || d.z > t; }
inline bool Intersects(const Bounds2f& a, const Bounds2f& b)
{
return a.max.x >= b.min.x && a.min.x <= b.max.x
&& a.max.y >= b.min.y && a.min.y <= b.max.y;
}
inline bool Intersects(const Bounds3f& a, const Bounds3f& b)
{
return a.max.x >= b.min.x && a.min.x <= b.max.x
&& a.max.y >= b.min.y && a.min.y <= b.max.y
&& a.max.z >= b.min.z && a.min.z <= b.max.z;
}
inline void Add(Bounds2f& a, const Bounds2f& b)
{
if (a.min.x > b.min.x) a.min.x = b.min.x; else if (a.max.x < b.max.x) a.max.x = b.max.x;
if (a.min.y > b.min.y) a.min.y = b.min.y; else if (a.max.y < b.max.y) a.max.y = b.max.y;
}
inline void Add(Bounds3f& a, const Bounds3f& b)
{
if (a.min.x > b.min.x) a.min.x = b.min.x; else if (a.max.x < b.max.x) a.max.x = b.max.x;
if (a.min.y > b.min.y) a.min.y = b.min.y; else if (a.max.y < b.max.y) a.max.y = b.max.y;
if (a.min.z > b.min.z) a.min.z = b.min.z; else if (a.max.z < b.max.z) a.max.z = b.max.z;
}
template<class T> inline int size_i(const T& container) { return int(container.size()); }
}
namespace
{
// Utilities
inline Vec4f BezierWeights(float t)
/// Returns Bezier basis weights for 't'
{
float s = 1.0f - t;
float t2 = t * t;
float t3 = t2 * t;
float s2 = s * s;
float s3 = s2 * s;
return Vec4f(s3, 3.0f * s2 * t, 3.0f * s * t2, t3);
}
inline Vec4f BezierWeights(const Vec4f& t)
/// Vector version useful for derivatives
{
return Vec4f
(
t.x - 3.0f * t.y + 3.0f * t.z - t.w,
3.0f * t.y - 6.0f * t.z + 3.0f * t.w,
3.0f * t.z - 3.0f * t.w,
t.w
);
}
inline Vec4f CubicCoeffs(const Vec4f& b)
/// Returns cubic coefficients for the given Bezier weights
{
return Vec4f
(
b.x ,
-3.0f * b.x + 3.0f * b.y ,
3.0f * b.x - 6.0f * b.y + 3.0f * b.z ,
-b.x + 3.0f * b.y - 3.0f * b.z + b.w
);
}
inline Vec2f HullBounds(const Vec4f& 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 Vec2f
(
Min(b01.x, b23.x),
Max(b01.y, b23.y)
);
}
Vec2f ExactBounds(const Vec4f& spline)
/// Returns accurate bounds taking extrema into account.
{
Vec2f 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;
}
// This is based on one step of De Casteljau's algorithm
inline void Split(float t, const Vec4f& spline, Vec4f* spline0, Vec4f* spline1)
{
// assumption: seg = (P0, P1, P2, P3)
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; // support aliasing
float sw = spline.w;
*spline0 = Vec4f(sx, q0, r0, s0);
*spline1 = Vec4f(s0, r1, q2, sw);
}
// Optimised for t=0.5
inline void Split(const Vec4f& spline, Vec4f* spline0, Vec4f* spline1)
{
float q0 = (spline.x + spline.y) * 0.5f; // x + y / 2
float q1 = (spline.y + spline.z) * 0.5f; // y + z / 2
float q2 = (spline.z + spline.w) * 0.5f; // z + w / 2
float r0 = (q0 + q1) * 0.5f; // x + 2y + z / 4
float r1 = (q1 + q2) * 0.5f; // y + 2z + w / 4
float s0 = (r0 + r1) * 0.5f; // q0 + 2q1 + q2 / 4 = x+y + 2(y+z) + z+w / 8 = x + 3y + 3z + w
float sx = spline.x; // support aliasing
float sw = spline.w;
*spline0 = Vec4f(sx, q0, r0, s0);
*spline1 = Vec4f(s0, r1, q2, sw);
}
bool Join(const Vec4f& s0, const Vec4f& s1, Vec4f* sOut)
{
if (s0.w != s1.x) // early out
return false;
// assumes t = 0.5
// 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);
float dp = sqr(x0 - x1) + sqr(y0 - y1) + sqr(z0 - z1) + sqr(w0 - w1);
if (dp < 1e-4f) // do left and right reconstructions agree?
{
*sOut = Vec4f(x0, y0, z1, w1); // use most stable terms
return true;
}
return false;
}
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);
}
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;
}
}
////////////////////////////////////////////////////////////////////////////////
// 2D
////////////////////////////////////////////////////////////////////////////////
cSpline2 SplineLib::BezierSpline(const Vec2f& p0, const Vec2f& p1, const Vec2f& p2, const Vec2f& p3)
{
return cSpline2
{
Vec4f(p0.x, p1.x, p2.x, p3.x),
Vec4f(p0.y, p1.y, p2.y, p3.y),
};
}
cSpline2 SplineLib::HermiteSpline(const Vec2f& p0, const Vec2f& p1, const Vec2f& v0, const Vec2f& v1)
{
Vec2f pb1 = p0 + (1.0f / 3.0f) * v0;
Vec2f pb2 = p1 - (1.0f / 3.0f) * v1;
return BezierSpline(p0, pb1, pb2, p1);
}
cSpline2 SplineLib::CatmullRomSpline(const Vec2f& p0, const Vec2f& p1, const Vec2f& p2, const 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);
}
namespace
{
const float kCircleOffset = 4.0f / 3.0f * (sqrtf(2.0f) - 1.0f);
const Vec4f kQuarterB0(1.0f, 1.0f, kCircleOffset, 0.0f);
const Vec4f kQuarterB1(0.0f, kCircleOffset, 1.0f, 1.0f);
}
cSpline2 SplineLib::QuadrantSpline(const Vec2f& p, float r, int quadrant)
{
SL_ASSERT(quadrant >= 0 && quadrant < 4);
cSpline2 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 = r * s.xb + Vec4f(p.x, p.x, p.x, p.x);
s.yb = r * s.yb + Vec4f(p.y, p.y, p.y, p.y);
return s;
}
void SplineLib::CircleSplines(const Vec2f& p, float r, cSpline2 splines[4])
{
for (int i = 0; i < 4; i++)
splines[i] = QuadrantSpline(p, r, i);
}
namespace
{
inline cSpline2 SplineFromPoints2(const char* p8, size_t stride, int i0, int i1, int i2, int i3, float tension)
{
Vec2f p0 = *(Vec2f*) (p8 + i0 * stride);
Vec2f p1 = *(Vec2f*) (p8 + i1 * stride);
Vec2f p2 = *(Vec2f*) (p8 + i2 * stride);
Vec2f p3 = *(Vec2f*) (p8 + i3 * stride);
float s = (1.0f - tension) * (1.0f / 6.0f);
Vec2f pb1 = p1 + s * (p2 - p0);
Vec2f pb2 = p2 - s * (p3 - p1);
return BezierSpline(p1, pb1, pb2, p2);
}
}
int SplineLib::SplinesFromPoints(int numPoints, const Vec2f pi[], int, cSpline2 splines[], float tension, size_t stride)
{
SL_ASSERT(numPoints >= 0);
SL_ASSERT(maxSplines >= 0 && maxSplines >= NumSplinesForPoints(numPoints));
const char* p8 = (const char*) pi;
switch (numPoints)
{
case 0:
return 0;
case 1:
*splines = SplineFromPoints2(p8, stride, 0, 0, 0, 0, tension);
return 1;
case 2:
*splines = SplineFromPoints2(p8, stride, 0, 0, 1, 1, tension);
return 1;
}
*splines++ = SplineFromPoints2(p8, stride, 0, 0, 1, 2, tension);
for (int i = 0; i < numPoints - 3; i++)
{
*splines++ = SplineFromPoints2(p8, stride, 0, 1, 2, 3, tension);
p8 += stride;
}
*splines++ = SplineFromPoints2(p8, stride, 0, 1, 2, 2, tension);
return numPoints - 1;
}
int SplineLib::SplinesFromBezier(int numPoints, const Vec2f points[], const Vec2f hullPoints[], cSpline2 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 SplineLib::SplinesFromHermite(int numPoints, const Vec2f points[], const Vec2f tangents [], cSpline2 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;
}
namespace
{
inline Vec2f Evaluate(const cSpline2& spline, const Vec4f& w)
/// Evaluate spline with given weights
{
return Vec2f
(
dot(spline.xb, w),
dot(spline.yb, w)
);
}
}
Vec2f SplineLib::Position(const cSpline2& spline, float t)
{
return Evaluate(spline, BezierWeights(t));
}
Vec2f SplineLib::Velocity(const cSpline2& spline, float t)
{
Vec4f dt4(0, 1, 2 * t, 3 * t * t);
return Evaluate(spline, BezierWeights(dt4));
}
Vec2f SplineLib::Acceleration(const cSpline2& spline, float t)
{
Vec4f ddt4(0, 0, 2, 6 * t);
return Evaluate(spline, BezierWeights(ddt4));
}
float SplineLib::Curvature(const cSpline2& 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 1e10f;
return avCrossLen / (vLen * vLen * vLen);
}
void SplineLib::Frame(const cSpline2& spline, float t, Mat2f* frameOut)
{
Vec2f v = Velocity (spline, t);
Mat2f& frame = *frameOut;
frame.rows[0] = norm_safe(v);
frame.rows[1] = cross(frame.rows[0]);
}
float SplineLib::LengthEstimate(const cSpline2& 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 SplineLib::Length(const cSpline2& s, float maxError)
{
float error;
float length = LengthEstimate(s, &error);
if (error > maxError)
{
cSpline2 s0;
cSpline2 s1;
Split(s, &s0, &s1);
return Length(s0, maxError) + Length(s1, maxError);
}
return length;
}
float SplineLib::Length(const cSpline2& 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);
cSpline2 s0, s1;
if (t0 == 0.0f)
{
if (t1 == 1.0f)
return Length(s, maxError);
Split(s, t1, &s0, &s1);
return Length(s0, 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);
}
}
Bounds2f SplineLib::FastBounds(const cSpline2& spline)
{
Vec2f bx = HullBounds(spline.xb);
Vec2f by = HullBounds(spline.yb);
Bounds2f result = { { bx.x, by.x }, { bx.y, by.y } };
return result;
}
Bounds2f SplineLib::ExactBounds(const cSpline2& spline)
{
Vec2f bx = ::ExactBounds(spline.xb);
Vec2f by = ::ExactBounds(spline.yb);
Bounds2f result = { { bx.x, by.x }, { bx.y, by.y } };
return result;
}
void SplineLib::Split(const cSpline2& spline, cSpline2* spline0, cSpline2* spline1)
{
::Split(spline.xb, &spline0->xb, &spline1->xb);
::Split(spline.yb, &spline0->yb, &spline1->yb);
}
void SplineLib::Split(const cSpline2& spline, float t, cSpline2* spline0, cSpline2* spline1)
{
::Split(t, spline.xb, &spline0->xb, &spline1->xb);
::Split(t, spline.yb, &spline0->yb, &spline1->yb);
}
bool SplineLib::Join(const cSpline2& s0, const cSpline2& s1, cSpline2* splineOut)
{
return
::Join(s0.xb, s1.xb, &splineOut->xb)
&& ::Join(s0.yb, s1.yb, &splineOut->yb);
}
void SplineLib::Split(vector<cSpline2>* splinesIn)
{
vector<cSpline2> splines;
for (const cSpline2& s : *splinesIn)
{
cSpline2 s0, s1;
Split(s, &s0, &s1);
splines.push_back(s0);
splines.push_back(s1);
}
splinesIn->swap(splines);
}
void SplineLib::Split(vector<cSpline2>* splinesIn, int n)
{
vector<cSpline2> splines;
for (const cSpline2& s : *splinesIn)
{
cSpline2 ss(s);
cSpline2 s0, s1;
for (int i = n; i > 1; i--)
{
Split(ss, 1.0f / i, &s0, &ss);
splines.push_back(s0);
}
splines.push_back(ss);
}
splinesIn->swap(splines);
}
void SplineLib::Join(vector<cSpline2>* splinesIn)
{
vector<cSpline2> splines;
const cSpline2* prevS = 0;
for (const cSpline2& s : *splinesIn)
{
if (!prevS)
{
prevS = &s;
continue;
}
cSpline2 sj;
if (Join(*prevS, s, &sj))
splines.push_back(sj);
else
{
splines.push_back(*prevS);
splines.push_back(s);
}
prevS = 0;
}
if (prevS)
splines.push_back(*prevS);
splinesIn->swap(splines);
}
namespace
{
void SubdivideForLength(const cSpline2& s, vector<cSpline2>* splines, float tolerance)
{
float error;
float length = LengthEstimate(s, &error);
if (error <= tolerance * length)
splines->push_back(s);
else
{
cSpline2 s1, s2;
Split(s, &s1, &s2);
SubdivideForLength(s1, splines, tolerance);
SubdivideForLength(s2, splines, tolerance);
}
}
}
void SplineLib::SubdivideForLength(vector<cSpline2>* splinesIn, float tolerance)
{
vector<cSpline2> splines;
for (const cSpline2& s : *splinesIn)
::SubdivideForLength(s, &splines, tolerance);
splinesIn->swap(splines);
}
namespace
{
inline float ArcError(const cSpline2& s, float* tSplit)
{
Vec2f ex = ArcError2(s.xb);
Vec2f ey = ArcError2(s.yb);
float e0 = ex.x + ey.x;
float e1 = ex.y + ey.y;
float es2 = e0 + e1;
float f = (es2 < 1e-6f) ? 0.5f : sqrtf(e0 / es2);
*tSplit = (1.0f / 3.0f) * (1.0f + f);
return sqrtf(es2);
}
void SubdivideForT(const cSpline2& s, vector<cSpline2>* splines, float tolerance)
{
float splitT;
float err = ArcError(s, &splitT);
if (err <= tolerance)
splines->push_back(s);
else
{
cSpline2 s1, s2;
Split(s, splitT, &s1, &s2);
SubdivideForT(s1, splines, tolerance);
SubdivideForT(s2, splines, tolerance);
}
}
}
void SplineLib::SubdivideForT(vector<cSpline2>* splinesIn, float tolerance)
{
vector<cSpline2> splines;
for (const cSpline2& s : *splinesIn)
::SubdivideForT(s, &splines, tolerance);
splinesIn->swap(splines);
}
namespace
{
inline float ClosestPoint(const Vec2f& p, const Vec2f& p0, const Vec2f& p1)
{
Vec2f w = p1 - p0;
Vec2f v = p - p0;
float dvw = dot(v, w);
if (dvw <= 0.0f)
return 0.0f;
float dww = dot(w, w);
if (dvw >= dww)
return 1.0f;
return dvw / dww;
}
void FindClosestPointNewtonRaphson(const cSpline2& spline, Vec2f p, float sIn, int maxIterations, float* tOut, float* dOut)
{
SL_ASSERT(sIn >= 0.0f && sIn <= 1.0f);
const float maxS = 1.0f - 1e-6f;
float skLast = sIn;
float sk = sIn;
float dk = len(Position(spline, sk) - p);
constexpr float width = 1e-3f;
float maxJump = 0.5f; // avoid jumping too far, leads to oscillation
for (int i = 0; i < maxIterations; i++)
{
float ss = Clamp(sk, width, 1.0f - width); // so can interpolate points for Newtons method
float d1 = len(Position(spline, ss - width) - p);
float d2 = len(Position(spline, ss ) - p);
float d3 = len(Position(spline, ss + width) - p);
float g1 = (d2 - d1) / width;
float g2 = (d3 - d2) / width;
float grad = (d3 - d1) / (2.0f * width);
float curv = (g2 - g1) / width;
float sn; // next candidate
if (curv > 0.0f) // if d' is heading towards a minima, apply NR for d'
sn = ss - grad / curv;
else if (grad != 0.0f)
sn = ss - d2 / grad; // otherwise, apply for D.
else
sn = sk;
sn = Clamp(sn, sk - maxJump, sk + maxJump); // avoid large steps, often unstable.
// only update our estimate if the new value is in range and closer.
if (sn >= 0.0f && sn < maxS)
{
float dn = len(Position(spline, sn) - p);
if (dn < dk) // only update sk if d actually gets smaller
{
sk = sn;
dk = dn;
}
}
maxJump *= 0.5f; // reduce on a schedule -- helps binary search back towards a jump that is valid.
skLast = sk;
}
(*tOut) = sk;
(*dOut) = dk;
}
}
float SplineLib::FindClosestPoint(const Vec2f& p, const cSpline2& spline)
{
// Approximate s from straight line between the start and end.
float s = ClosestPoint(p, Position0(spline), Position1(spline));
// Use this as starting point for Newton-Raphson solve.
float d;
FindClosestPointNewtonRaphson(spline, p, s, 8, &s, &d);
return s;
}
float SplineLib::FindClosestPoint(const Vec2f& p, int numSplines, const cSpline2 splines[], int* index)
{
vector<cSubSpline2> nearbyInfo;
FindNearbySplines(p, numSplines, splines, &nearbyInfo);
return FindClosestPoint(p, numSplines, splines, nearbyInfo, index);
}
namespace
{
void FindMinMaxDistance2s(const Vec2f& p, const Bounds2f& bbox, float* minD2, float* maxD2)
{
const Vec2f& p0 = bbox.min;
const Vec2f& p1 = bbox.max;
// Find the nearest point to p inside the bbox
// This can be a bbox vertex, a point on an edge or face, or p itself if it's inside the box
float minX = Clamp(p.x, p0.x, p1.x);
float minY = Clamp(p.y, p0.y, p1.y);
// Find the farthest point from p inside the bbox
// This is always a bbox vertex.
Vec2f d0(abs(p - p0));
Vec2f d1(abs(p - p1));
float maxX = d0.x > d1.x ? p0.x : p1.x; // select the coordinate we're farthest from
float maxY = d0.y > d1.y ? p0.y : p1.y;
// return len2
*minD2 = sqr(p.x - minX) + sqr(p.y - minY);
*maxD2 = sqr(p.x - maxX) + sqr(p.y - maxY);
}
void FindMinMaxDistance2s(const Vec2f& p, const cSpline2& spline, float* minD2, float* maxD2)
{
Bounds2f bbox = FastBounds(spline);
FindMinMaxDistance2s(p, bbox, minD2, maxD2);
}
void Split(const cSubSpline2& s, cSubSpline2* s0, cSubSpline2* s1)
{
::Split(s.mSpline.xb, &s0->mSpline.xb, &s1->mSpline.xb);
::Split(s.mSpline.yb, &s0->mSpline.yb, &s1->mSpline.yb);
s0->mParent = s.mParent;
s1->mParent = s.mParent;
}
}
int SplineLib::FindNearbySplines(const Vec2f& p, int numSplines, const cSpline2 splines[], vector<cSubSpline2>* results, float* smallestFarOut, int numIter)
{
vector<cSubSpline2>& nearSplines = *results;
nearSplines.clear();
float smallestFar = FLT_MAX;
float smallestNear = FLT_MAX;
// Find initial list
int maxSize = 0;
for (int i = 0; i < numSplines; i++)
{
float near;
float far;
FindMinMaxDistance2s(p, splines[i], &near, &far);
if (near < smallestFar)
{
// we at least overlap the current set.
if (near < smallestNear)
smallestNear = near;
if (far < smallestFar)
{
// we bring in the 'best' far distance
smallestFar = far;
// compact list to reject any segments that now cannot be closest.
int dj = 0;
for (int j = 0, nj = size_i(nearSplines); j < nj; j++)
if (nearSplines[j].mD2 < smallestFar)
{
if (dj < j)
nearSplines[dj] = nearSplines[j];
dj++;
}
nearSplines.resize(dj);
}
cSubSpline2 ss = { splines[i], i, near };
nearSplines.push_back(ss);
if (maxSize < size_i(nearSplines))
maxSize = size_i(nearSplines);
}
}
// Subdivide + refine
int numNearSplines = size_i(nearSplines);
for (int i = 0; i < numIter; i++)
{
int numNearSplines2 = numNearSplines * 2;
nearSplines.resize(numNearSplines2);
for (int i = numNearSplines - 1; i >= 0; i--)
::Split(nearSplines[i], &nearSplines[2 * i], &nearSplines[2 * i + 1]);
smallestNear = FLT_MAX; // this may actually increase on subdivision.
for (int i = 0; i < numNearSplines2; i++)
{