-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathBezierSpline.java
1246 lines (936 loc) · 27.7 KB
/
BezierSpline.java
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
package cadcore;
import java.awt.geom.Point2D;
import java.util.ArrayList;
public class BezierSpline implements Cloneable {
public static final double ZERO = 0.0000000001;
public static final double ONE = 0.9999999999;
static final int X = 0;
static final int Y = 1;
static final int MIN = 0;
static final int MAX = 1;
static final double ANGLE_TOLERANCE = 0.05*MathUtils.DEG_TO_RAD; //0.1degrees
static final double ANGLE_T_TOLERANCE = 0.000002;
static final int ANGLE_MAX_ITERATIONS = 50;
static final int ANGLE_SPLITS = 112;
static final double POS_TOLERANCE = 0.003; //0.03mm
static final int POS_MAX_ITERATIONS = 30;
static final double LENGTH_TOLERANCE = 0.001;
static final double MIN_MAX_TOLERANCE = 0.0001;
static final int MIN_MAX_SPLITS = 96;
static final double DISTANCE_TOLERANCE = 0.0001;
protected ArrayList<BezierCurve> mCurves = new ArrayList<BezierCurve>();
protected int mBestMatchPointIndex;
public BezierSpline()
{
}
public void set(BezierSpline spline)
{
// Remove extra control points
while (getNrOfControlPoints() > spline.getNrOfControlPoints()) {
remove(0);
}
// Add extra control points
while (getNrOfControlPoints() < spline.getNrOfControlPoints()) {
append(new BezierKnot());
}
int nrControlPoints = getNrOfControlPoints();
for (int i = 0; i < nrControlPoints; i++) {
getControlPoint(i).set(
spline.getControlPoint(i));
}
}
public void append(BezierKnot controlPoint)
{
if(mCurves.size() == 0)
{
mCurves.add(new BezierCurve(controlPoint, null));
}
else if(mCurves.size() == 1 && mCurves.get(0).getEndKnot() == null)
{
mCurves.get(0).setEndKnot(controlPoint);
}
else
{
mCurves.add(new BezierCurve(mCurves.get(mCurves.size()-1).getEndKnot(), controlPoint));
}
}
public void insert(int i, BezierKnot controlPoint)
{
BezierKnot next = null;
if(i < getNrOfControlPoints())
{
next = getControlPoint(i);
}
if(i > 0 && i-1 < mCurves.size())
{
BezierCurve prevCurve = mCurves.get(i-1);
prevCurve.setEndKnot(controlPoint);
}
BezierCurve newCurve = new BezierCurve(controlPoint, next);
mCurves.add(i, newCurve);
}
public void remove(BezierKnot controlPoint)
{
int i = indexOf(controlPoint);
remove(i);
}
public void remove(int i)
{
BezierCurve removeCurve = null;
if(i < mCurves.size())
{
removeCurve = mCurves.get(i);
}
if(i > 0 && i-1 < mCurves.size())
{
BezierCurve prevCurve = mCurves.get(i-1);
prevCurve.setEndKnot(removeCurve != null?removeCurve.getEndKnot():null);
}
mCurves.remove(removeCurve);
}
public BezierKnot getControlPoint(int i)
{
if(mCurves.size() == 0 || mCurves.size() < i-1)
{
return null;
}
if(i == 0)
{
return mCurves.get(0).getStartKnot();
}
else
{
return mCurves.get(i-1).getEndKnot();
}
}
public BezierCurve getCurve(int i)
{
return mCurves.get(i);
}
public boolean isLastControlPointNull()
{
if(mCurves.size() == 0)
{
return true;
}
return (mCurves.get(mCurves.size()-1).getEndKnot() == null);
}
public int indexOf(BezierKnot controlPoint)
{
for(int i = 0; i < mCurves.size()+1; i++)
{
if(controlPoint == getControlPoint(i))
return i;
}
return -1;
}
public int getNrOfControlPoints()
{
return mCurves.size()+(isLastControlPointNull()?0:1);
}
public int getNrOfCurves()
{
return mCurves.size();
}
public void clear()
{
mCurves.clear();
}
public double getValueAt(double pos)
{
int index = findMatchingBezierSegment(pos);
if(index == -1)
{
return 0.0;
}
double value = mCurves.get(index).getYForX(pos);
return value;
}
public double getValueAtReverse(double pos)
{
int index = findMatchingBezierSegmentReverse(pos);
if(index != -1)
{
BezierCurve curve = mCurves.get(index);
return curve.getYForX(pos);
}
return 0.0;
}
public double getCurvatureAt(double pos)
{
int index = findMatchingBezierSegment(pos);
if(index == -1) //Not within patch
return 0.0;
BezierCurve curve = mCurves.get(index);
return curve.getCurvature(pos);
}
public double getMaxX()
{
double max = -100000;
for(int i = 0; i < mCurves.size(); i++)
{
BezierCurve curve = mCurves.get(i);
double current = curve.getMaxX();
if(current > max)
max = current;
}
return max;
}
public Point2D.Double getPointByTT(double tt)
{
int index = (int)(tt*getNrOfCurves());
double t = (tt*getNrOfCurves()) - index;
if(tt >= 1)
{
index = getNrOfCurves()-1;
t = ONE;
}
BezierCurve curve = mCurves.get(index);
return curve.getValue(t);
}
public double getNormalByTT(double tt)
{
int index = (int)(tt*getNrOfCurves());
double t = (tt*getNrOfCurves()) - index;
if(tt >= 1)
{
index = getNrOfCurves()-1;
t = ONE;
}
// System.out.printf("getNormalByTT() tt:%f index:%d t:%f\n", tt, index, t);
BezierCurve curve = mCurves.get(index);
return curve.getTangent(t) + (Math.PI/2.0);
}
public double getTTByLineIntersect(final Point2D.Double a, final Point2D.Double b)
{
final double slope = (b.y - a.y)/(b.x-a.x);
final double p = a.y - (slope*a.x);
MathUtils.Function func = new MathUtils.Function(){
public double f(double tt){
//Bezier
Point2D.Double bezierPos = getPointByTT(tt);
//Line
double lineY = (slope*bezierPos.x) + p;
return bezierPos.y - lineY;
}
};
double aTT = getTTByX(a.x);
double bTT = getTTByX(b.x);
if(aTT == bTT)
{
return bTT;
}
double tt = MathUtils.RootFinder.getRoot(func, 0.0, Math.min(aTT, bTT), Math.max(aTT, bTT) );
return tt;
}
public double getTTByX(double x)
{
MathUtils.Function func = new MathUtils.Function(){public double f(double tt){return getPointByTT(tt).x;}};
double tt = MathUtils.RootFinder.getRoot(func, x);
return tt;
}
public double getTTByNormal(double angle)
{
MathUtils.Function func = new MathUtils.Function(){public double f(double tt){return getNormalByTT(tt);}};
double tt = MathUtils.RootFinder.getRoot(func, angle);
return tt;
}
public double getLengthByTT(double tt)
{
int index = (int)(tt*getNrOfControlPoints());
if(tt >= 1)
{
index = getNrOfControlPoints()-1;
}
double length = 0.0;
for(int i = 0; i < index; i++)
{
BezierCurve curve = mCurves.get(i);
length += curve.getLength();
}
BezierCurve curve = mCurves.get(index);
double t = tt - index;
length += curve.getLength(0,t);
return length;
}
public double getSByTT(double tt)
{
return getLengthByTT(tt)/getLength();
}
public double getMinX()
{
double min = Double.MAX_VALUE;
for(int i = 0; i < mCurves.size()-1; i++)
{
BezierCurve curve = mCurves.get(i);
double current = curve.getMinX();
if(current < min)
min = current;
}
return min;
}
public double getMaxY()
{
double max = -Double.MAX_VALUE;
for(int i = 0; i < mCurves.size()-1; i++)
{
BezierCurve curve = mCurves.get(i);
double current = curve.getMaxY();
if(current > max)
max = current;
}
return max;
}
public double getMaxYInRange(double x0, double x1)
{
double max = -100000;
double current = 0.0;
for(int i = 0; i < mCurves.size(); i++)
{
BezierCurve curve = mCurves.get(i);
double minX = curve.getMinMaxNumerical(0.0, 1.0, MIN_MAX_SPLITS, X, MIN);
double maxX = curve.getMinMaxNumerical(0.0, 1.0, MIN_MAX_SPLITS, X, MAX);
if(minX > x1)
continue;
if(maxX < x0)
continue;
double t0 = curve.getTForX(x0, 0.1);
double t1 = curve.getTForX(x1, 0.9);
current = curve.getMinMaxNumerical(t0, t1, MIN_MAX_SPLITS, Y, MAX);
if(current > max)
max = current;
}
return max;
}
public double getXForMaxY()
{
double max = -100000;
BezierCurve maxCurve = null;
for(int i = 0; i < mCurves.size(); i++)
{
BezierCurve curve = mCurves.get(i);
double current = curve.getMinMaxNumerical(Y, MAX);
if(current > max)
{
max = current;
maxCurve = curve;
}
}
double tMax = maxCurve.getTForMinMaxNumerical(0, 1, MIN_MAX_SPLITS, Y, MAX);
double x = maxCurve.getXValue(tMax);
return x;
}
public double getXForMaxYInRange(double x0, double x1)
{
double max = -100000;
BezierCurve maxCurve = null;
for(int i = 0; i < mCurves.size(); i++)
{
BezierCurve curve = mCurves.get(i);
double minX = curve.getMinMaxNumerical(0.0, 1.0, MIN_MAX_SPLITS, X, MIN);
double maxX = curve.getMinMaxNumerical(0.0, 1.0, MIN_MAX_SPLITS, X, MAX);
if(minX > x1)
continue;
if(maxX < x0)
continue;
double t0 = curve.getTForX(x0, 0.1);
double t1 = curve.getTForX(x1, 0.9);
double current = curve.getMinMaxNumerical(t0, t1, MIN_MAX_SPLITS, Y, MAX);
if(current > max)
{
maxCurve = curve;
max = current;
}
}
double tMax = maxCurve.getTForMinMaxNumerical(0, 1, MIN_MAX_SPLITS, Y, MAX);
double x = maxCurve.getXValue(tMax);
return x;
}
public double getMinY()
{
double min = 100000;
for(int i = 0; i < mCurves.size(); i++)
{
BezierCurve curve = mCurves.get(i);
double current = curve.getMinY();
if(current < min)
min = current;
}
return min;
}
public double getLengthByX(double pos)
{
double length = 0;
int index = findMatchingBezierSegment(pos);
if(index == -1) //Not within patch
{
return 0.0;
}
BezierCurve curve = mCurves.get(index);
double t = curve.getTForX(pos, ONE);
length += curve.getLength(ZERO,t);
for(int i = 0; i < index; i++)
{
curve = mCurves.get(i);
double currentLength = curve.getLength();
length += currentLength;
}
return length;
}
public double getSByX(double pos)
{
return getLengthByX(pos)/getLength();
}
public Point2D.Double getPointByS(double s)
{
return getPointByCurveLength(s*getLength());
}
public Point2D.Double getPointByCurveLength(double curveLength)
{
double l = curveLength;
double t = -1;
BezierCurve curve = null;
//Return endpoints if input curvelength is out of range
if(curveLength <= 0.0)
{
curve = mCurves.get(0);
return new Point2D.Double(curve.getXValue(ZERO), curve.getYValue(ZERO));
}
if(curveLength >= getLength())
{
curve = mCurves.get(mCurves.size()-1);
return new Point2D.Double(curve.getXValue(ONE), curve.getYValue(ONE));
}
for(int i = 0; i < mCurves.size(); i++)
{
curve = mCurves.get(i);
double currentLength = curve.getLength();
if(l < currentLength)
{
t = curve.getTForLength(l);
break;
}
l -= currentLength;
}
double x = curve.getXValue(t);
double y = curve.getYValue(t);
return new Point2D.Double(x,y);
}
//Returns the first point found that is the given distance from
public Point2D.Double getPointByDistance(Point2D.Double point, double distance)
{
Point2D.Double best = null;
double best_error = 10000;
for(int i = 0; i < mCurves.size(); i++)
{
BezierCurve curve = mCurves.get(i);
double t = curve.getTForDistance(point, distance);
Point2D.Double current = curve.getValue(t);
double error = Math.abs(VecMath.getVecLength(point,current));
if(error < best_error)
{
best = current;
best_error = error;
}
}
return best;
}
public Point2D.Double getPointByLineIntersection(Point2D.Double a, Point2D.Double b){
return getPointByTT(getTTByLineIntersect(a, b));
}
public double getSByNormal(double angle)
{
return getSByTangent(angle - Math.PI/2.0);
}
public double getSByTangent(double angle)
{
return getLengthByTangent(angle)/getLength();
}
public double getLengthByNormal(double angle)
{
return getLengthByTangent(angle - Math.PI/2.0)/getLength();
}
public double getLengthByTangent(double angle)
{
double length = 0;
double t = 0;
for(int i = 0; i < mCurves.size(); i++)
{
BezierCurve curve = mCurves.get(i);
synchronized(this)
{
double endAngle = curve.getTangent(ZERO);
double otherEndAngle = curve.getTangent(ONE);
double initial_t = (angle-endAngle)/(otherEndAngle-endAngle);
if(initial_t < 0.0)
initial_t = 0.0;
if(initial_t > 1.0)
initial_t = 1.0;
double last_t = initial_t +0.1;
if(last_t > 1.0)
last_t -= 0.2;
t = curve.getTForTangent2(angle,initial_t,last_t);
double tAngle = curve.getTangent(t);
if(Math.abs(tAngle-angle) < ANGLE_TOLERANCE)
{
length += curve.getLength(ZERO,t);
break;
}
length += curve.getLength();
}
}
return length;
}
public double getSByNormalReverse(double angle)
{
return getSByTangentReverse(angle - Math.PI/2.0);
}
public double getSByNormalReverse(double angle, boolean useMinimumAngleOnSharpCorners)
{
return getSByTangentReverse(angle - Math.PI/2.0,useMinimumAngleOnSharpCorners);
}
public double getSByTangentReverse(double angle)
{
double s = getLengthByTangentReverse(angle)/getLength();
s = MathUtils.clamp(s, ZERO, ONE);
return s;
}
public double getSByTangentReverse(double angle, boolean useMinimumAngleOnSharpCorners)
{
double s = getLengthByTangentReverse(angle, useMinimumAngleOnSharpCorners)/getLength();
s = MathUtils.clamp(s, ZERO, ONE);
return s;
}
public double getLengthByNormalReverse(double angle)
{
return getLengthByTangentReverse(angle - Math.PI/2.0)/getLength();
}
public double getLengthByNormalReverse(double angle, boolean useMinimumAngleOnSharpCorners)
{
return getLengthByTangentReverse(angle - Math.PI/2.0, useMinimumAngleOnSharpCorners)/getLength();
}
public double getLengthByTangentReverse(double angle)
{
return getLengthByTangentReverse(angle, true);
}
public double getLengthByTangentReverse(double targetAngle, boolean useMinimumAngleOnSharpCorners)
{
// System.out.printf("getLengthByTangentReverseScaled() targetAngle:%f\n", targetAngle/BezierBoard.DEG_TO_RAD);
double length = 0;
double t = 0;
double minAngleError = 10000;
double minErrorT = -1;
int minAngleErrorSection = -1;
boolean targetFound = false;
int i;
for(i = mCurves.size()-1; i >= 0; i--)
{
BezierCurve curve = mCurves.get(i);
double startAngle = curve.getTangent(ZERO);
double endAngle = curve.getTangent(ONE);
// System.out.printf("getLengthByTangentReverseScaled() StartAngle:%f EndAngle:%f TargetAngle:%f\n", startAngle/BezierBoard.DEG_TO_RAD +90.0, endAngle/BezierBoard.DEG_TO_RAD + 90.0, targetAngle/BezierBoard.DEG_TO_RAD +90.0);
if(startAngle >= targetAngle && endAngle <= targetAngle)
{
// System.out.printf("getLengthByTangentReverseScaled() Value should be in this span, StartAngle:%f EndAngle:%f TargetAngle:%f\n", startAngle/BezierBoard.DEG_TO_RAD +90.0, endAngle/BezierBoard.DEG_TO_RAD + 90.0, targetAngle/BezierBoard.DEG_TO_RAD +90.0);
}
// if(k1.mContinous == false)
// {
if(endAngle > targetAngle)
{
// System.out.printf("getLengthByTangentReverseScaled() i:%d, endAngle > targetAngle endAngle:%f targetAngle:%f\n",i, endAngle/BezierBoard.DEG_TO_RAD, targetAngle/BezierBoard.DEG_TO_RAD);
if(useMinimumAngleOnSharpCorners == true)
{
i += 1;
// System.out.printf("getLengthByTangentReverseScaled() Target found by endAngle(%f) > targetAngle(%f) with use minimun angle on shape corner\n", endAngle/BezierBoard.DEG_TO_RAD+90.0, targetAngle/BezierBoard.DEG_TO_RAD+90.0);
}
else
{
length = curve.getLength(ZERO, ONE-.05);
// System.out.printf("getLengthByTangentReverseScaled() Target found by endAngle(%f) > targetAngle(%f)\n", endAngle/BezierBoard.DEG_TO_RAD+90.0, targetAngle/BezierBoard.DEG_TO_RAD+90.0);
}
targetFound = true;
break;
}
// }
// t = getTForTangent(angle,ZERO,ONE, ANGLE_SPLITS);
double initial_t = (targetAngle-startAngle)/(endAngle-startAngle);
if(initial_t < 0.0)
initial_t = 0.0;
if(initial_t > 1.0)
initial_t = 1.0;
double last_t = initial_t +0.1;
if(last_t > 1.0)
last_t -= 0.2;
// System.out.printf("getLengthByTangentReverseScaled() i:%d startAngle:%f end_angle:%f initial_t:%f last_t:%f\n",i, startAngle/BezierBoard.DEG_TO_RAD, endAngle/BezierBoard.DEG_TO_RAD, initial_t, last_t);
t = curve.getTForTangent2(targetAngle,initial_t,last_t);
double tAngle = curve.getTangent(t);
double angleError = Math.abs(tAngle-targetAngle);
if(minAngleError > angleError)
{
minAngleError = angleError;
minErrorT = t;
minAngleErrorSection = i;
}
if(angleError <= ANGLE_TOLERANCE)
{
length = curve.getLength(ZERO,t);
targetFound = true;
// System.out.printf("getLengthByTangentReverseScaled() Target found by matching angle: %f, error: %f\n", tAngle/BezierBoard.DEG_TO_RAD+90.0, angleError/BezierBoard.DEG_TO_RAD);
break;
}
else if(startAngle >= targetAngle && endAngle <= targetAngle)
{
// System.out.printf("getLengthByTangentReverseScaled() Value should be in this span but error is too large, StartAngle:%f EndAngle:%f Target: %f Returned: %f t:%f\n", startAngle/BezierBoard.DEG_TO_RAD +90.0, endAngle/BezierBoard.DEG_TO_RAD +90.0, targetAngle/BezierBoard.DEG_TO_RAD +90.0, tAngle/BezierBoard.DEG_TO_RAD+90.0, t);
t = curve.getTForTangent2(targetAngle,initial_t,last_t);
}
}
if(targetFound == false && minAngleErrorSection != -1)
{
//Use the nearest target
BezierCurve curve = mCurves.get(minAngleErrorSection);
length = curve.getLength(ZERO, minErrorT);
}
length += getLengthByControlPointIndex(0,i);
//DEBUG sanity check
double tangent = getTangentByCurveLength(length);
if(Math.abs(tangent - targetAngle) > 0.5 && targetFound)
{
// System.out.printf("getLengthByTangentReverseScaled() getTangentByCurveLength() returned different angle. Target: %f Returned: %f length:%f curveLength:%f\n", targetAngle/BezierBoard.DEG_TO_RAD +90.0, tangent/BezierBoard.DEG_TO_RAD + 90.0, length, lengthScaled(scaleX, scaleY));
targetFound = !targetFound;
}
return length;
}
public double getLengthByControlPointIndex(int startIndex, int endIndex)
{
double length = 0;
for(int i = startIndex; i < endIndex; i++)
{
BezierCurve curve = mCurves.get(i);
length += curve.getLength();
}
return length;
}
public double getNormalAngle(double pos)
{
return getTangentAt(pos) + Math.PI/2.0;
}
public Point2D.Double getNormalAt(double pos)
{
double angle = getNormalAngle(pos);
double x = Math.sin(angle);
double y = Math.cos(angle);
return new Point2D.Double(x,y);
}
public double getNormalByS(double s)
{
return getNormalByCurveLength(s*getLength());
}
public double getNormalByCurveLength(double curveLength)
{
return getTangentByCurveLength(curveLength) + Math.PI/2.0;
}
public double getCurvatureByS(double s)
{
return getCurvatureByCurveLength(s*getLength());
}
public double getCurvatureByCurveLength(double curveLength)
{
double left = curveLength;
double t = -1;
if(curveLength <= 0.0)
{
return 0.0;
}
if(curveLength >= getLength())
{
return 0.0;
}
BezierCurve curve = null;
for(int i = 0; i < mCurves.size(); i++)
{
curve = mCurves.get(i);
double currentLength = curve.getLength();
if(left < currentLength)
{
break;
}
left -= currentLength;
}
t = curve.getTForLength(left);
double curvature = curve.getCurvature(t);
return curvature;
}
public double getTangentAt(double pos)
{
int index = findMatchingBezierSegment(pos);
if(index == -1)
{
return 0.0;
}
BezierCurve curve = mCurves.get(index);
return curve.getTangentAt(pos);
}
public double getTangentByS(double s)
{
return getTangentByCurveLength(s*getLength());
}
public double getTangentByCurveLength(double curveLength)
{
double l = curveLength;
double t = -1;
BezierCurve curve = null;
for(int i = 0; i < mCurves.size(); i++)
{
curve = mCurves.get(i);
double currentLength = curve.getLength();
if(l < currentLength)
{
t = curve.getTForLength(l);
break;
}
l -= currentLength;
}
return curve.getTangent(t);
}
public double getIntegral(double a, double b, int splits) //Numerical integration using composite simpsons rule
{
MathUtils.Function deckFunc = new MathUtils.Function(){public double f(double x){return getValueAt(x);}};
double result = MathUtils.Integral.getIntegral(deckFunc, a, b, splits);
return result;
}
public double getLength()
{
double length = 0;
for(int i = 0; i < mCurves.size(); i++)
{
BezierCurve curve = mCurves.get(i);
length += curve.getLength();
}
return length;
}
public void scale(double verticalScale, double horizontalScale)
{
for(int i = 0; i < mCurves.size(); i++)
{
if(i == 0)
{
BezierKnot startKnot = mCurves.get(i).getStartKnot();
startKnot.scale(horizontalScale, verticalScale);
}
BezierKnot endKnot = mCurves.get(i).getEndKnot();
if(endKnot != null)
{
endKnot.scale(horizontalScale, verticalScale);
}
}
}
public int getSplitControlPoint(Point2D.Double nearPoint, BezierKnot returned)
{
double nearestDist = 100000000;
int index = 0;
double t = 0;
for(int i = 0; i < mCurves.size(); i++)
{
BezierCurve curve = mCurves.get(i);
double tc = curve.getClosestT(nearPoint);
double x = curve.getXValue(tc);
double y = curve.getYValue(tc);