-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathMain.cpp
3382 lines (3240 loc) · 122 KB
/
Main.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 <cmath>
#include <ctime>
#include <iostream>
#include <memory>
#include <string>
#include "load_problem.h"
#include "ndcurves/bezier_curve.h"
#include "ndcurves/cubic_hermite_spline.h"
#include "ndcurves/curve_conversion.h"
#include "ndcurves/exact_cubic.h"
#include "ndcurves/fwd.h"
#include "ndcurves/helpers/effector_spline.h"
#include "ndcurves/helpers/effector_spline_rotation.h"
#include "ndcurves/optimization/definitions.h"
#include "ndcurves/piecewise_curve.h"
#include "ndcurves/polynomial.h"
#include "ndcurves/se3_curve.h"
#include "ndcurves/serialization/curves.hpp"
#include "ndcurves/so3_linear.h"
using namespace std;
namespace ndcurves {
typedef exact_cubic<double, double, true, Eigen::Matrix<double, 1, 1> >
exact_cubic_one;
typedef exact_cubic_t::spline_constraints spline_constraints_t;
typedef std::pair<double, pointX_t> Waypoint;
typedef std::vector<Waypoint> T_Waypoint;
typedef Eigen::Matrix<double, 1, 1> point_one;
typedef std::pair<double, point_one> WaypointOne;
typedef std::vector<WaypointOne> T_WaypointOne;
typedef std::pair<pointX_t, pointX_t> pair_point_tangent_t;
typedef std::vector<pair_point_tangent_t,
Eigen::aligned_allocator<pair_point_tangent_t> >
t_pair_point_tangent_t;
const double margin = 1e-3;
bool QuasiEqual(const double a, const double b) {
return std::fabs(a - b) < margin;
}
bool QuasiEqual(const point3_t a, const point3_t b) {
bool equal = true;
for (size_t i = 0; i < 3; ++i) {
equal = equal && QuasiEqual(a[i], b[i]);
}
return equal;
}
} // End namespace ndcurves
using namespace ndcurves;
ostream& operator<<(ostream& os, const point3_t& pt) {
os << "(" << pt.x() << ", " << pt.y() << ", " << pt.z() << ")";
return os;
}
void ComparePoints(const transform_t& pt1, const transform_t& pt2,
const std::string& errmsg, bool& error,
double prec = Eigen::NumTraits<double>::dummy_precision(),
bool notequal = false) {
if (!pt1.isApprox(pt2, prec) && !notequal) {
error = true;
std::cout << errmsg << " translation :" << pt1.translation() << " ; "
<< pt2.translation() << std::endl
<< "rotation : " << pt1.rotation() << " ; " << pt2.rotation()
<< std::endl;
}
}
void ComparePoints(const Eigen::MatrixXd& pt1, const Eigen::MatrixXd& pt2,
const std::string& errmsg, bool& error,
double prec = Eigen::NumTraits<double>::dummy_precision(),
bool notequal = false) {
if (!pt1.isApprox(pt2, prec) && !(pt1.isZero(prec) && pt2.isZero(prec)) &&
!notequal) {
error = true;
std::cout << errmsg << pt1 << " ; " << pt2 << std::endl;
}
}
template <typename curve1, typename curve2>
void CompareCurves(const curve1& c1, const curve2& c2,
const std::string& errMsg, bool& error,
double prec = Eigen::NumTraits<double>::dummy_precision()) {
double T_min = c1.min();
double T_max = c1.max();
if (!QuasiEqual(T_min, c2.min()) || !QuasiEqual(T_max, c2.max())) {
std::cout
<< errMsg
<< "CompareCurves, ERROR, time min and max of curves do not match ["
<< T_min << "," << T_max << "] "
<< " and [" << c2.min() << "," << c2.max() << "] " << std::endl;
error = true;
} else {
// derivative in T_min and T_max
ComparePoints(c1.derivate(T_min, 1), c2.derivate(T_min, 1),
errMsg + " Derivates at tMin do not match.", error, prec,
false);
ComparePoints(c1.derivate(T_max, 1), c2.derivate(T_max, 1),
errMsg + " Derivates at tMax do not match.", error, prec,
false);
// Test values on curves
for (double i = T_min; i <= T_max; i += 0.01) {
ComparePoints(c1(i), c2(i),
errMsg + " Curves evaluation do not match at t = " +
boost::lexical_cast<std::string>(i),
error, prec, false);
}
}
}
/*Cubic Function tests*/
void PolynomialCubicFunctionTest(bool& error) {
std::string errMsg("In test CubicFunctionTest ; unexpected result for x ");
point3_t a(1, 2, 3);
point3_t b(2, 3, 4);
point3_t c(3, 4, 5);
point3_t d(3, 6, 7);
t_pointX_t vec;
vec.push_back(a);
vec.push_back(b);
vec.push_back(c);
vec.push_back(d);
polynomial_t cf(vec.begin(), vec.end(), 0, 1);
point3_t res1;
res1 = cf(0);
point3_t x0(1, 2, 3);
ComparePoints(x0, res1, errMsg + "(0) ", error);
point3_t x1(9, 15, 19);
res1 = cf(1);
ComparePoints(x1, res1, errMsg + "(1) ", error);
point3_t x2(3.125, 5.25, 7.125);
res1 = cf(0.5);
ComparePoints(x2, res1, errMsg + "(0.5) ", error);
vec.clear();
vec.push_back(a);
vec.push_back(b);
vec.push_back(c);
vec.push_back(d);
polynomial_t cf2(vec, 0.5, 1);
res1 = cf2(0.5);
ComparePoints(x0, res1, errMsg + "x3 ", error);
error = true;
try {
cf2(0.4);
} catch (...) {
error = false;
}
if (error) {
std::cout << "Evaluation of cubic cf2 error, 0.4 should be an out of range "
"value\n";
}
error = true;
try {
cf2(1.1);
} catch (...) {
error = false;
}
if (error) {
std::cout << "Evaluation of cubic cf2 error, 1.1 should be an out of range "
"value\n";
}
if (!QuasiEqual(cf.max(), 1.0)) {
error = true;
std::cout
<< "Evaluation of cubic cf error, MaxBound should be equal to 1\n";
}
if (!QuasiEqual(cf.min(), 0.0)) {
error = true;
std::cout
<< "Evaluation of cubic cf error, MinBound should be equal to 1\n";
}
// Test derivate and compute_derivative
// Order 1
curve_abc_t* cf_derivated = cf.compute_derivate_ptr(1);
ComparePoints(cf.derivate(0, 1), (*cf_derivated)(0),
errMsg + " - derivate order 1 : ", error);
ComparePoints(cf.derivate(0.3, 1), (*cf_derivated)(0.3),
errMsg + " - derivate order 1 : ", error);
ComparePoints(cf.derivate(0.5, 1), (*cf_derivated)(0.5),
errMsg + " - derivate order 1 : ", error);
ComparePoints(cf.derivate(1, 1), (*cf_derivated)(1),
errMsg + " - derivate order 1 : ", error);
// Order 2
polynomial_t cf_derivated_2 = cf.compute_derivate(2);
ComparePoints(cf.derivate(0, 2), (cf_derivated_2)(0),
errMsg + " - derivate order 1 : ", error);
ComparePoints(cf.derivate(0.3, 2), (cf_derivated_2)(0.3),
errMsg + " - derivate order 1 : ", error);
ComparePoints(cf.derivate(0.5, 2), (cf_derivated_2)(0.5),
errMsg + " - derivate order 1 : ", error);
ComparePoints(cf.derivate(1, 2), (cf_derivated_2)(1),
errMsg + " - derivate order 1 : ", error);
}
/*bezier_curve Function tests*/
void BezierCurveTest(bool& error) {
std::string errMsg("In test BezierCurveTest ; unexpected result for x ");
point3_t a(1, 2, 3);
point3_t b(2, 3, 4);
point3_t c(3, 4, 5);
point3_t d(3, 6, 7);
std::vector<point3_t> params;
params.push_back(a);
// 1d curve in [0,1]
bezier_t cf1(params.begin(), params.end());
point3_t res1;
res1 = cf1(0);
point3_t x10 = a;
ComparePoints(x10, res1, errMsg + "1(0) ", error);
res1 = cf1(1);
ComparePoints(x10, res1, errMsg + "1(1) ", error);
// 2d curve in [0,1]
params.push_back(b);
bezier_t cf(params.begin(), params.end());
res1 = cf(0);
point3_t x20 = a;
ComparePoints(x20, res1, errMsg + "2(0) ", error);
point3_t x21 = b;
res1 = cf(1);
ComparePoints(x21, res1, errMsg + "2(1) ", error);
// 3d curve in [0,1]
params.push_back(c);
bezier_t cf3(params.begin(), params.end());
res1 = cf3(0);
ComparePoints(a, res1, errMsg + "3(0) ", error);
res1 = cf3(1);
ComparePoints(c, res1, errMsg + "3(1) ", error);
// 4d curve in [1,2]
params.push_back(d);
bezier_t cf4(params.begin(), params.end(), 1., 2.);
// testing bernstein polynomials
bezier_t cf5(params.begin(), params.end(), 1., 2.);
std::string errMsg2(
"In test BezierCurveTest ; Bernstein polynomials do not evaluate as "
"analytical evaluation");
bezier_t cf5_derivated = cf5.compute_derivate(1);
for (double d = 1.; d < 2.; d += 0.1) {
ComparePoints(cf5.evalBernstein(d), cf5(d), errMsg2, error);
ComparePoints(cf5.evalHorner(d), cf5(d), errMsg2, error);
ComparePoints(cf5_derivated.evalBernstein(d), cf5_derivated(d), errMsg2,
error);
ComparePoints(cf5_derivated.evalHorner(d), cf5_derivated(d), errMsg2,
error);
ComparePoints(cf5.derivate(d, 1), cf5_derivated(d), errMsg2, error);
}
bool error_in(true);
try {
cf(-0.4);
} catch (...) {
error_in = false;
}
if (error_in) {
std::cout << "Evaluation of bezier cf error, -0.4 should be an out of "
"range value\n";
error = true;
}
error_in = true;
try {
cf(1.1);
} catch (...) {
error_in = false;
}
if (error_in) {
std::cout << "Evaluation of bezier cf error, 1.1 should be an out of range "
"value\n";
error = true;
}
if (!QuasiEqual(cf.max(), 1.0)) {
error = true;
std::cout
<< "Evaluation of bezier cf error, MaxBound should be equal to 1\n";
}
if (!QuasiEqual(cf.min(), 0.0)) {
error = true;
std::cout
<< "Evaluation of bezier cf error, MinBound should be equal to 1\n";
}
}
void BezierCurveTestCompareHornerAndBernstein(bool&) // error
{
using namespace std;
std::vector<double> values;
for (int i = 0; i < 100000; ++i) {
values.push_back(rand() / (double)RAND_MAX);
}
// first compare regular evaluation (low dim pol)
point3_t a(1, 2, 3);
point3_t b(2, 3, 4);
point3_t c(3, 4, 5);
point3_t d(3, 6, 7);
point3_t e(3, 61, 7);
point3_t f(3, 56, 7);
point3_t g(3, 36, 7);
point3_t h(43, 6, 7);
point3_t i(3, 6, 77);
std::vector<point3_t> params;
params.push_back(a);
params.push_back(b);
params.push_back(c);
// 3d curve
bezier_t cf(params.begin(), params.end()); // defined in [0,1]
// Check all evaluation of bezier curve
clock_t s0, e0, s1, e1, s2, e2, s3, e3;
s0 = clock();
for (std::vector<double>::const_iterator cit = values.begin();
cit != values.end(); ++cit) {
cf(*cit);
}
e0 = clock();
s1 = clock();
for (std::vector<double>::const_iterator cit = values.begin();
cit != values.end(); ++cit) {
cf.evalBernstein(*cit);
}
e1 = clock();
s2 = clock();
for (std::vector<double>::const_iterator cit = values.begin();
cit != values.end(); ++cit) {
cf.evalHorner(*cit);
}
e2 = clock();
s3 = clock();
for (std::vector<double>::const_iterator cit = values.begin();
cit != values.end(); ++cit) {
cf.evalDeCasteljau(*cit);
}
e3 = clock();
std::cout << "time for analytical eval " << double(e0 - s0) / CLOCKS_PER_SEC
<< std::endl;
std::cout << "time for bernstein eval " << double(e1 - s1) / CLOCKS_PER_SEC
<< std::endl;
std::cout << "time for horner eval " << double(e2 - s2) / CLOCKS_PER_SEC
<< std::endl;
std::cout << "time for deCasteljau eval " << double(e3 - s3) / CLOCKS_PER_SEC
<< std::endl;
std::cout << "now with high order polynomial " << std::endl;
params.push_back(d);
params.push_back(e);
params.push_back(f);
params.push_back(g);
params.push_back(h);
params.push_back(i);
bezier_t cf2(params.begin(), params.end());
s1 = clock();
for (std::vector<double>::const_iterator cit = values.begin();
cit != values.end(); ++cit) {
cf2.evalBernstein(*cit);
}
e1 = clock();
s2 = clock();
for (std::vector<double>::const_iterator cit = values.begin();
cit != values.end(); ++cit) {
cf2.evalHorner(*cit);
}
e2 = clock();
s0 = clock();
for (std::vector<double>::const_iterator cit = values.begin();
cit != values.end(); ++cit) {
cf2(*cit);
}
e0 = clock();
s3 = clock();
for (std::vector<double>::const_iterator cit = values.begin();
cit != values.end(); ++cit) {
cf2.evalDeCasteljau(*cit);
}
e3 = clock();
std::cout << "time for analytical eval " << double(e0 - s0) / CLOCKS_PER_SEC
<< std::endl;
std::cout << "time for bernstein eval " << double(e1 - s1) / CLOCKS_PER_SEC
<< std::endl;
std::cout << "time for horner eval " << double(e2 - s2) / CLOCKS_PER_SEC
<< std::endl;
std::cout << "time for deCasteljau eval " << double(e3 - s3) / CLOCKS_PER_SEC
<< std::endl;
}
void BezierDerivativeCurveTest(bool& error) {
std::string errMsg(
"In test BezierDerivativeCurveTest ;, Error While checking value of "
"point on curve : ");
point3_t a(1, 2, 3);
point3_t b(2, 3, 4);
point3_t c(3, 4, 5);
std::vector<point3_t> params;
params.push_back(a);
params.push_back(b);
params.push_back(c);
bezier_t cf3(params.begin(), params.end());
ComparePoints(cf3(0), cf3.derivate(0., 1), errMsg, error, true);
ComparePoints(point3_t::Zero(), cf3.derivate(0., 100), errMsg, error);
}
void BezierDerivativeCurveTimeReparametrizationTest(bool& error) {
std::string errMsg(
"In test BezierDerivativeCurveTimeReparametrizationTest, Error While "
"checking value of point on curve : ");
point3_t a(1, 2, 3);
point3_t b(2, 3, 4);
point3_t c(3, 4, 5);
point3_t d(3, 4, 5);
point3_t e(3, 4, 5);
point3_t f(3, 4, 5);
std::vector<point3_t> params;
params.push_back(a);
params.push_back(b);
params.push_back(c);
params.push_back(d);
params.push_back(e);
params.push_back(f);
double Tmin = 0.;
double Tmax = 2.;
double diffT = Tmax - Tmin;
bezier_t cf(params.begin(), params.end());
bezier_t cfT(params.begin(), params.end(), Tmin, Tmax);
ComparePoints(cf(0.5), cfT(1), errMsg, error);
ComparePoints(cf.derivate(0.5, 1), cfT.derivate(1, 1) * (diffT), errMsg,
error);
ComparePoints(cf.derivate(0.5, 2), cfT.derivate(1, 2) * diffT * diffT, errMsg,
error);
}
void BezierDerivativeCurveConstraintTest(bool& error) {
std::string errMsg0(
"In test BezierDerivativeCurveConstraintTest, Error While checking value "
"of point on curve : ");
point3_t a(1, 2, 3);
point3_t b(2, 3, 4);
point3_t c(3, 4, 5);
bezier_t::curve_constraints_t constraints(3);
constraints.init_vel = point3_t(-1, -1, -1);
constraints.init_acc = point3_t(-2, -2, -2);
constraints.end_vel = point3_t(-10, -10, -10);
constraints.end_acc = point3_t(-20, -20, -20);
std::vector<point3_t> params;
params.push_back(a);
params.push_back(b);
params.push_back(c);
bezier_t::num_t T_min = 1.0;
bezier_t::num_t T_max = 3.0;
bezier_t cf(params.begin(), params.end(), constraints, T_min, T_max);
ComparePoints(a, cf(T_min), errMsg0, error);
ComparePoints(c, cf(T_max), errMsg0, error);
ComparePoints(constraints.init_vel, cf.derivate(T_min, 1), errMsg0, error);
ComparePoints(constraints.end_vel, cf.derivate(T_max, 1), errMsg0, error);
ComparePoints(constraints.init_acc, cf.derivate(T_min, 2), errMsg0, error);
ComparePoints(constraints.end_vel, cf.derivate(T_max, 1), errMsg0, error);
ComparePoints(constraints.end_acc, cf.derivate(T_max, 2), errMsg0, error);
std::string errMsg1(
"In test BezierDerivativeCurveConstraintTest, Error While checking "
"checking degree of bezier curve :");
std::string errMsg2(
"In test BezierDerivativeCurveConstraintTest, Error While checking "
"checking size of bezier curve :");
if (cf.degree_ != params.size() + 3) {
error = true;
std::cout << errMsg1 << cf.degree_ << " ; " << params.size() + 3
<< std::endl;
}
if (cf.size_ != params.size() + 4) {
error = true;
std::cout << errMsg2 << cf.size_ << " ; " << params.size() + 4 << std::endl;
}
}
void toPolynomialConversionTest(bool& error) {
// bezier to polynomial
std::string errMsg(
"In test BezierToPolynomialConversionTest, Error While checking value of "
"point on curve : ");
point3_t a(1, 2, 3);
point3_t b(2, 3, 4);
point3_t c(3, 4, 5);
point3_t d(3, 6, 7);
point3_t e(3, 61, 7);
point3_t f(3, 56, 7);
point3_t g(3, 36, 7);
point3_t h(43, 6, 7);
point3_t i(3, 6, 77);
std::vector<point3_t> control_points;
control_points.push_back(a);
control_points.push_back(b);
control_points.push_back(c);
control_points.push_back(d);
control_points.push_back(e);
control_points.push_back(f);
control_points.push_back(g);
control_points.push_back(h);
control_points.push_back(i);
bezier_t::num_t T_min = 1.0;
bezier_t::num_t T_max = 3.0;
bezier_t bc(control_points.begin(), control_points.end(), T_min, T_max);
polynomial_t pol = polynomial_from_curve<polynomial_t>(bc);
CompareCurves<polynomial_t, bezier_t>(pol, bc, errMsg, error);
}
void cubicConversionTest(bool& error) {
std::string errMsg0(
"In test CubicConversionTest - convert hermite to, Error While checking "
"value of point on curve : ");
std::string errMsg1(
"In test CubicConversionTest - convert bezier to, Error While checking "
"value of point on curve : ");
std::string errMsg2(
"In test CubicConversionTest - convert polynomial to, Error While "
"checking value of point on curve : ");
// Create cubic hermite spline : Test hermite to bezier/polynomial
point3_t p0(1, 2, 3);
point3_t m0(2, 3, 4);
point3_t p1(3, 4, 5);
point3_t m1(3, 6, 7);
pair_point_tangent_t pair0(p0, m0);
pair_point_tangent_t pair1(p1, m1);
t_pair_point_tangent_t control_points;
control_points.push_back(pair0);
control_points.push_back(pair1);
std::vector<double> time_control_points;
polynomial_t::num_t T_min = 1.0;
polynomial_t::num_t T_max = 3.0;
time_control_points.push_back(T_min);
time_control_points.push_back(T_max);
cubic_hermite_spline_t chs0(control_points.begin(), control_points.end(),
time_control_points);
// hermite to bezier
// std::cout<<"======================= \n";
// std::cout<<"hermite to bezier \n";
bezier_t bc0 = bezier_from_curve<bezier_t>(chs0);
CompareCurves<cubic_hermite_spline_t, bezier_t>(chs0, bc0, errMsg0, error);
// hermite to pol
// std::cout<<"======================= \n";
// std::cout<<"hermite to polynomial \n";
polynomial_t pol0 = polynomial_from_curve<polynomial_t>(chs0);
CompareCurves<cubic_hermite_spline_t, polynomial_t>(chs0, pol0, errMsg0,
error);
// pol to hermite
// std::cout<<"======================= \n";
// std::cout<<"polynomial to hermite \n";
cubic_hermite_spline_t chs1 =
hermite_from_curve<cubic_hermite_spline_t>(pol0);
CompareCurves<polynomial_t, cubic_hermite_spline_t>(pol0, chs1, errMsg2,
error);
// pol to bezier
// std::cout<<"======================= \n";
// std::cout<<"polynomial to bezier \n";
bezier_t bc1 = bezier_from_curve<bezier_t>(pol0);
CompareCurves<bezier_t, polynomial_t>(bc1, pol0, errMsg2, error);
// Bezier to pol
// std::cout<<"======================= \n";
// std::cout<<"bezier to polynomial \n";
polynomial_t pol1 = polynomial_from_curve<polynomial_t>(bc0);
CompareCurves<bezier_t, polynomial_t>(bc0, pol1, errMsg1, error);
// bezier => hermite
// std::cout<<"======================= \n";
// std::cout<<"bezier to hermite \n";
cubic_hermite_spline_t chs2 = hermite_from_curve<cubic_hermite_spline_t>(bc0);
CompareCurves<bezier_t, cubic_hermite_spline_t>(bc0, chs2, errMsg1, error);
// Test : compute derivative of bezier => Convert it to polynomial
curve_abc_t* bc_der = bc0.compute_derivate_ptr(1);
polynomial_t pol_test = polynomial_from_curve<polynomial_t>(*bc_der);
CompareCurves<curve_abc_t, polynomial_t>(*bc_der, pol_test, errMsg1, error);
// check that an error is correctly raised when degree > 3:
point3_t a(1, 2, 3);
point3_t b(2, 3, 4);
point3_t c(3, 4, 5);
point3_t d(3, 6, 7);
point3_t e(3, 6, 7);
t_pointX_t vec;
vec.push_back(a);
vec.push_back(b);
vec.push_back(c);
vec.push_back(d);
vec.push_back(e);
polynomial_t pol_4(vec.begin(), vec.end(), 0, 1);
if (pol_4.degree() != 4) {
std::cout << "In test CubicConversionTest - Error in the creatin of the "
"polynomial"
<< std::endl;
error = true;
}
try {
cubic_hermite_spline_t chs3 =
hermite_from_curve<cubic_hermite_spline_t>(pol_4);
std::cout << "In test CubicConversionTest - Cannot convert to hermite from "
"degree > 3, should raise an error"
<< std::endl;
error = true;
} catch (std::invalid_argument& /*e*/) {
}
try {
bezier_t b3 = bezier_from_curve<bezier_t>(pol_4);
std::cout << "In test CubicConversionTest - Cannot convert to bezier from "
"degree > 3, should raise an error"
<< std::endl;
error = true;
} catch (std::invalid_argument& /*e*/) {
}
}
/*Exact Cubic Function tests*/
void ExactCubicNoErrorTest(bool& error) {
// Create an exact cubic spline with 7 waypoints => 6 polynomials defined in
// [0.0,3.0]
ndcurves::T_Waypoint waypoints;
for (double i = 0.0; i <= 3.0; i = i + 0.5) {
waypoints.push_back(std::make_pair(i, point3_t(i, i, i)));
}
exact_cubic_t exactCubic(waypoints.begin(), waypoints.end());
// Test number of polynomials in exact cubic
std::size_t numberSegments = exactCubic.getNumberSplines();
if (numberSegments != 6) {
error = true;
std::cout
<< "In ExactCubicNoErrorTest, Error While checking number of splines"
<< numberSegments << " ; " << 6 << std::endl;
}
// Test getSplineAt function
for (std::size_t i = 0; i < numberSegments; i++) {
exactCubic.getSplineAt(i);
}
// Other tests
try {
exactCubic(0.0);
exactCubic(3.0);
} catch (...) {
error = true;
std::cout << "Evaluation of ExactCubicNoErrorTest error when testing value "
"on bounds\n";
}
error = true;
try {
exactCubic(3.2);
} catch (...) {
error = false;
}
if (error) {
std::cout << "Evaluation of exactCubic cf error, 3.2 should be an out of "
"range value\n";
}
if (!QuasiEqual(exactCubic.max(), 3.0)) {
error = true;
std::cout << "Evaluation of exactCubic error, MaxBound should be equal to "
"3 but is : "
<< exactCubic.max() << "\n";
}
if (!QuasiEqual(exactCubic.min(), 0.0)) {
error = true;
std::cout << "Evaluation of exactCubic error, MinBound should be equal to "
"0 but is : "
<< exactCubic.min() << "\n";
}
}
/*Exact Cubic Function tests*/
void ExactCubicTwoPointsTest(bool& error) {
// Create an exact cubic spline with 2 waypoints => 1 polynomial defined in
// [0.0,1.0]
ndcurves::T_Waypoint waypoints;
for (double i = 0.0; i < 2.0; ++i) {
waypoints.push_back(std::make_pair(i, point3_t(i, i, i)));
}
exact_cubic_t exactCubic(waypoints.begin(), waypoints.end());
point3_t res1 = exactCubic(0);
std::string errmsg0(
"in ExactCubicTwoPointsTest, Error While checking that given wayPoints "
"are crossed (expected / obtained)");
ComparePoints(point3_t(0, 0, 0), res1, errmsg0, error);
res1 = exactCubic(1);
ComparePoints(point3_t(1, 1, 1), res1, errmsg0, error);
// Test number of polynomials in exact cubic
std::size_t numberSegments = exactCubic.getNumberSplines();
if (numberSegments != 1) {
error = true;
std::cout
<< "In ExactCubicTwoPointsTest, Error While checking number of splines"
<< numberSegments << " ; " << 1 << std::endl;
}
// Test getSplineAt
std::string errmsg1(
"in ExactCubicTwoPointsTest, Error While checking value on curve");
ComparePoints(exactCubic(0.5), (exactCubic.getSplineAt(0))(0.5), errmsg1,
error);
}
void ExactCubicOneDimTest(bool& error) {
ndcurves::T_WaypointOne waypoints;
point_one zero;
zero(0, 0) = 9;
point_one one;
one(0, 0) = 14;
point_one two;
two(0, 0) = 25;
waypoints.push_back(std::make_pair(0., zero));
waypoints.push_back(std::make_pair(1., one));
waypoints.push_back(std::make_pair(2., two));
exact_cubic_one exactCubic(waypoints.begin(), waypoints.end());
point_one res1 = exactCubic(0);
std::string errmsg(
"in ExactCubicOneDim Error While checking that given wayPoints are "
"crossed (expected / obtained)");
ComparePoints(zero, res1, errmsg, error);
res1 = exactCubic(1);
ComparePoints(one, res1, errmsg, error);
}
void CheckWayPointConstraint(
const std::string& errmsg, const double step, const ndcurves::T_Waypoint&,
const exact_cubic_t* curve, bool& error,
double prec = Eigen::NumTraits<double>::dummy_precision()) {
point3_t res1;
for (double i = 0; i <= 1; i = i + step) {
res1 = (*curve)(i);
ComparePoints(point3_t(i, i, i), res1, errmsg, error, prec);
}
}
void ExactCubicPointsCrossedTest(bool& error) {
ndcurves::T_Waypoint waypoints;
for (double i = 0; i <= 1; i = i + 0.2) {
waypoints.push_back(std::make_pair(i, point3_t(i, i, i)));
}
exact_cubic_t exactCubic(waypoints.begin(), waypoints.end());
std::string errmsg(
"Error While checking that given wayPoints are crossed (expected / "
"obtained)");
CheckWayPointConstraint(errmsg, 0.2, waypoints, &exactCubic, error);
}
void ExactCubicVelocityConstraintsTest(bool& error) {
ndcurves::T_Waypoint waypoints;
for (double i = 0; i <= 1; i = i + 0.2) {
waypoints.push_back(std::make_pair(i, point3_t(i, i, i)));
}
std::string errmsg(
"Error in ExactCubicVelocityConstraintsTest (1); while checking that "
"given wayPoints are crossed (expected / "
"obtained)");
spline_constraints_t constraints(3);
constraints.end_vel = point3_t(0, 0, 0);
constraints.init_vel = point3_t(0, 0, 0);
constraints.end_acc = point3_t(0, 0, 0);
constraints.init_acc = point3_t(0, 0, 0);
exact_cubic_t exactCubic(waypoints.begin(), waypoints.end(), constraints);
// now check that init and end velocity are 0
CheckWayPointConstraint(errmsg, 0.2, waypoints, &exactCubic, error);
std::string errmsg3(
"Error in ExactCubicVelocityConstraintsTest (2); while checking "
"derivative (expected / obtained)");
// now check derivatives
ComparePoints(constraints.init_vel, exactCubic.derivate(0, 1), errmsg3, error,
1e-10);
ComparePoints(constraints.end_vel, exactCubic.derivate(1, 1), errmsg3, error,
1e-10);
ComparePoints(constraints.init_acc, exactCubic.derivate(0, 2), errmsg3, error,
1e-10);
ComparePoints(constraints.end_acc, exactCubic.derivate(1, 2), errmsg3, error,
1e-10);
constraints.end_vel = point3_t(1, 2, 3);
constraints.init_vel = point3_t(-1, -2, -3);
constraints.end_acc = point3_t(4, 5, 6);
constraints.init_acc = point3_t(-4, -4, -6);
std::string errmsg2(
"Error in ExactCubicVelocityConstraintsTest (3); while checking that "
"given wayPoints are crossed (expected / "
"obtained)");
exact_cubic_t exactCubic2(waypoints.begin(), waypoints.end(), constraints);
CheckWayPointConstraint(errmsg2, 0.2, waypoints, &exactCubic2, error, 1e-10);
std::string errmsg4(
"Error in ExactCubicVelocityConstraintsTest (4); while checking "
"derivative (expected / obtained)");
// now check derivatives
ComparePoints(constraints.init_vel, exactCubic2.derivate(0, 1), errmsg4,
error, 1e-10);
ComparePoints(constraints.end_vel, exactCubic2.derivate(1, 1), errmsg4, error,
1e-10);
ComparePoints(constraints.init_acc, exactCubic2.derivate(0, 2), errmsg4,
error, 1e-10);
ComparePoints(constraints.end_acc, exactCubic2.derivate(1, 2), errmsg4, error,
1e-10);
}
template <typename CurveType>
void CheckPointOnline(const std::string& errmsg, const point3_t& A,
const point3_t& B, const double target,
const CurveType* curve, bool& error) {
point3_t res1 = curve->operator()(target);
point3_t ar = (res1 - A);
ar.normalize();
point3_t rb = (B - res1);
rb.normalize();
if (ar.dot(rb) < 0.99999) {
error = true;
std::cout << errmsg << " ; " << A.transpose() << "\n ; " << B.transpose()
<< "\n ; " << target << " ; " << res1.transpose() << std::endl;
}
}
void EffectorTrajectoryTest(bool& error) {
// create arbitrary trajectory
ndcurves::T_Waypoint waypoints;
for (double i = 0; i <= 10; i = i + 2) {
waypoints.push_back(std::make_pair(i, point3_t(i, i, i)));
}
helpers::exact_cubic_t* eff_traj = helpers::effector_spline(
waypoints.begin(), waypoints.end(), Eigen::Vector3d::UnitZ(),
Eigen::Vector3d(0, 0, 2), 1, 0.02, 1, 0.5);
point3_t zero(0, 0, 0);
point3_t off1(0, 0, 1);
point3_t off2(10, 10, 10.02);
point3_t end(10, 10, 10);
std::string errmsg(
"Error in EffectorTrajectoryTest; while checking waypoints (expected / "
"obtained)");
std::string errmsg2(
"Error in EffectorTrajectoryTest; while checking derivative (expected / "
"obtained)");
// first check start / goal positions
ComparePoints(zero, (*eff_traj)(0), errmsg, error);
ComparePoints(off1, (*eff_traj)(1), errmsg, error);
ComparePoints(off2, (*eff_traj)(9.5), errmsg, error);
ComparePoints(end, (*eff_traj)(10), errmsg, error);
// now check derivatives
ComparePoints(zero, (*eff_traj).derivate(0, 1), errmsg2, error);
ComparePoints(zero, (*eff_traj).derivate(10, 1), errmsg2, error);
ComparePoints(zero, (*eff_traj).derivate(0, 2), errmsg2, error);
ComparePoints(zero, (*eff_traj).derivate(10, 2), errmsg2, error);
// check that end and init splines are line
std::string errmsg3(
"Error in EffectorTrajectoryTest; while checking that init/end splines "
"are line (point A/ point B, time value / "
"point obtained) \n");
for (double i = 0.1; i < 1; i += 0.1) {
CheckPointOnline<helpers::exact_cubic_t>(
errmsg3, (*eff_traj)(0), (*eff_traj)(1), i, eff_traj, error);
}
for (double i = 9.981; i < 10; i += 0.002) {
CheckPointOnline<helpers::exact_cubic_t>(
errmsg3, (*eff_traj)(9.5), (*eff_traj)(10), i, eff_traj, error);
}
delete eff_traj;
}
helpers::quat_t GetXRotQuat(const double theta) {
Eigen::AngleAxisd m(theta, Eigen::Vector3d::UnitX());
return helpers::quat_t(Eigen::Quaterniond(m).coeffs().data());
}
double GetXRotFromQuat(helpers::quat_ref_const_t q) {
Eigen::Quaterniond quat(q.data());
Eigen::AngleAxisd m(quat);
return m.angle() / M_PI * 180.;
}
void EffectorSplineRotationNoRotationTest(bool& error) {
// create arbitrary trajectory
ndcurves::T_Waypoint waypoints;
for (double i = 0; i <= 10; i = i + 2) {
waypoints.push_back(std::make_pair(i, point3_t(i, i, i)));
}
helpers::effector_spline_rotation eff_traj(waypoints.begin(),
waypoints.end());
helpers::config_t q_init;
q_init << 0., 0., 0., 0., 0., 0., 1.;
helpers::config_t q_end;
q_end << 10., 10., 10., 0., 0., 0., 1.;
helpers::config_t q_to;
q_to << 0., 0, 0.02, 0., 0., 0., 1.;
helpers::config_t q_land;
q_land << 10, 10, 10.02, 0, 0., 0., 1.;
helpers::config_t q_mod;
q_mod << 6., 6., 6., 0., 0., 0., 1.;
std::string errmsg(
"Error in EffectorSplineRotationNoRotationTest; while checking waypoints "
"(expected / obtained)");
ComparePoints(q_init, eff_traj(0), errmsg, error);
ComparePoints(q_to, eff_traj(0.02), errmsg, error);
ComparePoints(q_land, eff_traj(9.98), errmsg, error);
ComparePoints(q_mod, eff_traj(6), errmsg, error);
ComparePoints(q_end, eff_traj(10), errmsg, error);
}
void EffectorSplineRotationRotationTest(bool& error) {
// create arbitrary trajectory
ndcurves::T_Waypoint waypoints;
for (double i = 0; i <= 10; i = i + 2) {
waypoints.push_back(std::make_pair(i, point3_t(i, i, i)));
}
helpers::quat_t init_quat = GetXRotQuat(M_PI);
helpers::effector_spline_rotation eff_traj(waypoints.begin(), waypoints.end(),
init_quat);
helpers::config_t q_init = helpers::config_t::Zero();
q_init.tail<4>() = init_quat;
helpers::config_t q_end;
q_end << 10., 10., 10., 0., 0., 0., 1.;
helpers::config_t q_to = q_init;
q_to(2) += 0.02;
helpers::config_t q_land = q_end;
q_land(2) += 0.02;
helpers::quat_t q_mod = GetXRotQuat(M_PI_2);
;
std::string errmsg(
"Error in EffectorSplineRotationRotationTest; while checking waypoints "
"(expected / obtained)");
ComparePoints(q_init, eff_traj(0), errmsg, error);
ComparePoints(q_to, eff_traj(0.02), errmsg, error);
ComparePoints(q_land, eff_traj(9.98), errmsg, error);
ComparePoints(q_mod, eff_traj(5).tail<4>(), errmsg, error);
ComparePoints(q_end, eff_traj(10), errmsg, error);
}
void EffectorSplineRotationWayPointRotationTest(bool& error) {
// create arbitrary trajectory
ndcurves::T_Waypoint waypoints;
for (double i = 0; i <= 10; i = i + 2) {
waypoints.push_back(std::make_pair(i, point3_t(i, i, i)));
}
helpers::quat_t init_quat = GetXRotQuat(0);
helpers::t_waypoint_quat_t quat_waypoints_;
helpers::quat_t q_pi_0 = GetXRotQuat(0);
helpers::quat_t q_pi_2 = GetXRotQuat(M_PI_2);
helpers::quat_t q_pi = GetXRotQuat(M_PI);
quat_waypoints_.push_back(std::make_pair(0.4, q_pi_0));
quat_waypoints_.push_back(std::make_pair(6, q_pi_2));
quat_waypoints_.push_back(std::make_pair(8, q_pi));
helpers::effector_spline_rotation eff_traj(waypoints.begin(), waypoints.end(),
quat_waypoints_.begin(),
quat_waypoints_.end());
helpers::config_t q_init = helpers::config_t::Zero();
q_init.tail<4>() = init_quat;
helpers::config_t q_end;
q_end << 10., 10., 10., 0., 0., 0., 1.;
q_end.tail<4>() = q_pi;
helpers::config_t q_mod;
q_mod.head<3>() = point3_t(6, 6, 6);
q_mod.tail<4>() = q_pi_2;
helpers::config_t q_to = q_init;
q_to(2) += 0.02;
helpers::config_t q_land = q_end;
q_land(2) += 0.02;
std::string errmsg(
"Error in EffectorSplineRotationWayPointRotationTest; while checking "
"waypoints (expected / obtained)");
ComparePoints(q_init, eff_traj(0), errmsg, error);
ComparePoints(q_to, eff_traj(0.02), errmsg, error);
ComparePoints(q_land, eff_traj(9.98), errmsg, error);
ComparePoints(q_mod, eff_traj(6), errmsg, error);
ComparePoints(q_end, eff_traj(10), errmsg, error);
}
void TestReparametrization(bool& error) {
helpers::rotation_spline s;
const helpers::exact_cubic_constraint_one_dim& sp = s.time_reparam_;
if (!QuasiEqual(sp.min(), 0.0)) {
std::cout << "in TestReparametrization; min value is not 0, got "
<< sp.min() << std::endl;
error = true;
}
if (!QuasiEqual(sp.max(), 1.0)) {
std::cout << "in TestReparametrization; max value is not 1, got "
<< sp.max() << std::endl;
error = true;
}
if (!QuasiEqual(sp(1)[0], 1.0)) {
std::cout << "in TestReparametrization; end value is not 1, got "
<< sp(1)[0] << std::endl;
error = true;
}
if (!QuasiEqual(sp(0)[0], 0.0)) {
std::cout << "in TestReparametrization; init value is not 0, got "
<< sp(0)[0] << std::endl;
error = true;
}
for (double i = 0; i < 1; i += 0.002) {
if (sp(i)[0] > sp(i + 0.002)[0]) {
std::cout << "in TestReparametrization; reparametrization not monotonous "
<< sp.max() << std::endl;
error = true;
}
}
}
point3_t randomPoint(const double min, const double max) {
point3_t p;
for (size_t i = 0; i < 3; ++i) {
p[i] = (rand() / (double)RAND_MAX) * (max - min) + min;
}
return p;
}
void BezierEvalDeCasteljau(bool& error) {
using namespace std;
std::vector<double> values;
for (int i = 0; i < 100000; ++i) {