scenario.cpp 28.4 KB
Newer Older
Justin Carpentier's avatar
Justin Carpentier committed
1
2
// Copyright (c) 2015-2018, CNRS
// Authors: Justin Carpentier <jcarpent@laas.fr>
Guilhem Saurel's avatar
Guilhem Saurel committed
3

Justin Carpentier's avatar
Justin Carpentier committed
4
5
6
7
8
9
#include <iostream>

#define BOOST_TEST_MODULE StatsTests
#include <boost/test/unit_test.hpp>
#include <boost/utility/binary.hpp>

Pierre Fernbach's avatar
Pierre Fernbach committed
10
#include "multicontact-api/scenario/contact-model-planar.hpp"
Guilhem Saurel's avatar
Guilhem Saurel committed
11
#include "multicontact-api/scenario/contact-patch.hpp"
12
#include "multicontact-api/scenario/contact-phase.hpp"
Pierre Fernbach's avatar
Pierre Fernbach committed
13
//#include "multicontact-api/scenario/contact-sequence.hpp"
Justin Carpentier's avatar
Justin Carpentier committed
14

Pierre Fernbach's avatar
Pierre Fernbach committed
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
#include <curves/so3_linear.h>
#include <curves/se3_curve.h>
#include <curves/polynomial.h>
#include <curves/bezier_curve.h>
#include <curves/piecewise_curve.h>



typedef Eigen::Vector3d point3_t;
typedef Eigen::Matrix<double, 6, 1> point6_t;
typedef Eigen::Matrix<double, 12, 1> point12_t;
typedef Eigen::VectorXd pointX_t;
typedef std::vector<pointX_t, Eigen::aligned_allocator<pointX_t> > t_pointX_t;
typedef Eigen::Matrix<double, 3, 3> matrix3_t;
typedef Eigen::Quaternion<double> quaternion_t;
typedef curves::curve_abc<double, double, true, pointX_t> curve_abc_t;
typedef boost::shared_ptr<curve_abc_t> curve_ptr_t;
typedef curves::polynomial<double, double, true, pointX_t, t_pointX_t> polynomial_t;
typedef curves::bezier_curve<double, double, true, pointX_t> bezier_curve_t;
typedef curves::piecewise_curve<double, double, true, pointX_t, t_pointX_t, polynomial_t> piecewise_polynomial_curve_t;
typedef Eigen::Transform<double, 3, Eigen::Affine> transform_t;
typedef curves::SO3Linear<double, double, true> SO3Linear_t;
typedef curves::SE3Curve<double, double, true> SE3Curve_t;
typedef curves::piecewise_curve<double, double, true, SE3Curve_t::point_t, t_pointX_t, SE3Curve_t,SE3Curve_t::point_derivate_t> piecewise_SE3_curve_t;
typedef curves::curve_abc<double, double, true, transform_t,point6_t> curve_SE3_t;
typedef boost::shared_ptr<curve_SE3_t> curve_SE3_ptr;

Guilhem Saurel's avatar
Guilhem Saurel committed
42
using namespace multicontact_api::scenario;
Justin Carpentier's avatar
Justin Carpentier committed
43

Guilhem Saurel's avatar
Guilhem Saurel committed
44
45
template <typename Scalar>
struct ATpl {
46
  typedef pinocchio::SE3Tpl<Scalar> SE3;
Guilhem Saurel's avatar
Guilhem Saurel committed
47

Justin Carpentier's avatar
Justin Carpentier committed
48
  explicit ATpl() : data() {}
Guilhem Saurel's avatar
Guilhem Saurel committed
49
  explicit ATpl(const ATpl& other) : data(other.data){};
Guilhem Saurel's avatar
Guilhem Saurel committed
50

Guilhem Saurel's avatar
Guilhem Saurel committed
51
  bool operator==(const ATpl& other) { return data == other.data; }
Guilhem Saurel's avatar
Guilhem Saurel committed
52

Guilhem Saurel's avatar
Guilhem Saurel committed
53
 protected:
Justin Carpentier's avatar
Justin Carpentier committed
54
55
56
57
  SE3 data;
};

typedef ATpl<double> Ad;
Pierre Fernbach's avatar
Pierre Fernbach committed
58
59
typedef pinocchio::SE3Tpl<double> SE3;

Pierre Fernbach's avatar
Pierre Fernbach committed
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
curve_ptr_t buildPiecewisePolynomialC2(){
  point3_t p0(0., 0., 0.);
  point3_t p1(1., 2., 3.);
  point3_t p2(4., 4., 4.);
  point3_t p3(10., 10., 10.);
  point3_t d0(1., 1., 1.);
  point3_t d1(2., 2., 2.);
  point3_t d2(3., 3., 3.);
  point3_t d3(5., 5., 5.);
  point3_t dd0(1.5, 1.5, 1.5);
  point3_t dd1(2.5, 2.5, 2.5);
  point3_t dd2(3.5, 3.5, 3.5);
  point3_t dd3(5.5, 5.5, 5.5);
  double t0 = 1.0;
  double t1 = 1.5;
  double t2 = 3.0;
  double t3 = 10.0;
  t_pointX_t points;
  points.push_back(p0);
  points.push_back(p1);
  points.push_back(p2);
  points.push_back(p3);
  t_pointX_t points_derivative;
  points_derivative.push_back(d0);
  points_derivative.push_back(d1);
  points_derivative.push_back(d2);
  points_derivative.push_back(d3);
  t_pointX_t points_second_derivative;
  points_second_derivative.push_back(dd0);
  points_second_derivative.push_back(dd1);
  points_second_derivative.push_back(dd2);
  points_second_derivative.push_back(dd3);
  std::vector<double> time_points;
  time_points.push_back(t0);
  time_points.push_back(t1);
  time_points.push_back(t2);
  time_points.push_back(t3);

  piecewise_polynomial_curve_t ppc_C2 = piecewise_polynomial_curve_t::convert_discrete_points_to_polynomial<polynomial_t>(
          points, points_derivative, points_second_derivative, time_points);
  curve_ptr_t res(new piecewise_polynomial_curve_t(ppc_C2));
  return res;
}


template <typename Point>
curve_ptr_t buildRandomPolynomial(){
  pointX_t a = Point::Random();
  pointX_t b = Point::Random();
  pointX_t c = Point::Random();
  pointX_t d = Point::Random();
  const double t1 = Eigen::internal::random<double>(0.,10.);
  const double t2 = Eigen::internal::random<double>(0.,10.);
  const double min = std::min(t1,t2);
  const double max = std::max(t1,t2);
  t_pointX_t vec;
  vec.push_back(a);
  vec.push_back(b);
  vec.push_back(c);
  vec.push_back(d);
  curve_ptr_t res (new polynomial_t(vec.begin(), vec.end(), min, max));
  return  res;
}

curve_ptr_t buildRandomPolynomial3D(){
  return buildRandomPolynomial<point3_t>();
}

curve_ptr_t buildRandomPolynomial12D(){
  return buildRandomPolynomial<point12_t>();
}



curve_SE3_ptr buildPiecewiseSE3(){
  const double min = 0.5;
  const double mid = 1.2;
  const double max = 2.;
  quaternion_t q0(1, 0, 0, 0);
  quaternion_t q1(0., 1., 0, 0);
  pointX_t p0 = point3_t(1., 1.5, -2.);
  pointX_t p1 = point3_t(3., 0, 1.);
  SE3Curve_t cLinear(p0, p1, q0, q1, min, mid);
  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);
  params.push_back(b);
  params.push_back(c);
  params.push_back(d);
  boost::shared_ptr<bezier_curve_t> translation_bezier(new bezier_curve_t(params.begin(), params.end(), mid, max));
  SE3Curve_t cBezier(translation_bezier, q0.toRotationMatrix(), q1.toRotationMatrix());

  piecewise_SE3_curve_t piecewiseSE3(cLinear);
  piecewiseSE3.add_curve(cBezier);
  curve_SE3_ptr res = boost::make_shared<piecewise_SE3_curve_t>(piecewiseSE3);
  return res;

}

quaternion_t randomQuaternion(){ // already included in newest eigen release
  const double u1 = Eigen::internal::random<double>(0, 1),
      u2 = Eigen::internal::random<double>(0, 2*EIGEN_PI),
      u3 = Eigen::internal::random<double>(0, 2*EIGEN_PI);
  const double a = sqrt(1 - u1),
      b = sqrt(u1);
  return quaternion_t(a * sin(u2), a * cos(u2), b * sin(u3), b * cos(u3)).normalized();
}

curve_SE3_ptr buildRandomSE3LinearTraj(const double min, const double max){
  quaternion_t q0 = randomQuaternion();
  quaternion_t q1 = randomQuaternion();
  pointX_t p0 = point3_t::Random();
  pointX_t p1 = point3_t::Random();

  curve_SE3_ptr res(new SE3Curve_t(p0, p1, q0, q1, min, max));
  return res;
}


Justin Carpentier's avatar
Justin Carpentier committed
182
183
184

BOOST_AUTO_TEST_SUITE(BOOST_TEST_MODULE)

Guilhem Saurel's avatar
Guilhem Saurel committed
185
BOOST_AUTO_TEST_CASE(contact_model) {
Justin Carpentier's avatar
Justin Carpentier committed
186
187
  const double mu = 0.3;
  const double ZMP_radius = 0.01;
Guilhem Saurel's avatar
Guilhem Saurel committed
188

Guilhem Saurel's avatar
Guilhem Saurel committed
189
  ContactModelPlanar mp1(mu, ZMP_radius);
Justin Carpentier's avatar
Justin Carpentier committed
190
  ContactModelPlanar mp2(mp1);
Guilhem Saurel's avatar
Guilhem Saurel committed
191

Justin Carpentier's avatar
Justin Carpentier committed
192
193
194
  BOOST_CHECK(mp1 == mp2);
}

Guilhem Saurel's avatar
Guilhem Saurel committed
195
BOOST_AUTO_TEST_CASE(contact_patch) {
Pierre Fernbach's avatar
Pierre Fernbach committed
196
  // check default constructor :
Justin Carpentier's avatar
Justin Carpentier committed
197
  ContactPatch cp;
Pierre Fernbach's avatar
Pierre Fernbach committed
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
  BOOST_CHECK(cp.placement() == SE3::Identity());
  BOOST_CHECK(cp.friction() == -1.);
  SE3 p = SE3::Identity();
  p.setRandom();
  cp.placement() = p;
  BOOST_CHECK(cp.placement() == p);
  cp.friction() = 0.7;
  BOOST_CHECK(cp.friction() == 0.7);

  // constructor with placement :
  p.setRandom();
  ContactPatch cp1(p);
  BOOST_CHECK(cp1.placement() == p);
  BOOST_CHECK(cp1.friction() == -1.);

  // constructor with placement and friction
  p.setRandom();
  ContactPatch cp2(p,0.9);
  BOOST_CHECK(cp2.placement() == p);
  BOOST_CHECK(cp2.friction() == 0.9);

  // check comparison operator
  BOOST_CHECK(cp1 != cp2);
  ContactPatch cp3(p,0.9);
  BOOST_CHECK(cp3 == cp2);
  cp2.friction() = 0.1;
  BOOST_CHECK(cp3 != cp2);

  // copy constructor
  ContactPatch cp4(cp3);
  BOOST_CHECK(cp4 == cp3);
  BOOST_CHECK(cp4.placement() == p);
  BOOST_CHECK(cp4.friction() == 0.9);
  cp4.placement() = SE3::Identity();
  BOOST_CHECK(cp3 != cp4);

Pierre Fernbach's avatar
Pierre Fernbach committed
234
235
236
237
238
239
  // operator = 
  ContactPatch cp5 = cp4;
  BOOST_CHECK(cp4 == cp5);
  cp5.friction() = 2.;
  BOOST_CHECK(cp4 != cp5);

Pierre Fernbach's avatar
Pierre Fernbach committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
  // serialization :
  std::string fileName("fileTest");
  cp3.saveAsText(fileName);
  ContactPatch cp_from_text;
  cp_from_text.loadFromText(fileName);
  BOOST_CHECK(cp3 == cp_from_text);

  cp3.saveAsXML(fileName,"ContactPatch");
  ContactPatch cp_from_xml;
  cp_from_xml.loadFromXML(fileName,"ContactPatch");
  BOOST_CHECK(cp3 == cp_from_xml);

  cp3.saveAsBinary(fileName);
  ContactPatch cp_from_bin;
  cp_from_bin.loadFromBinary(fileName);
  BOOST_CHECK(cp3 == cp_from_bin);
Justin Carpentier's avatar
Justin Carpentier committed
256
257
}

258
259
BOOST_AUTO_TEST_CASE(contact_phase)
{
Pierre Fernbach's avatar
Pierre Fernbach committed
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
  // default constructor
  ContactPhase cp1;
  // test timings setter/getter
  BOOST_CHECK(cp1.timeInitial() == -1);
  BOOST_CHECK(cp1.timeFinal() == -1);
  BOOST_CHECK(cp1.duration() == 0);
  //cp1.timeInitial() = 2; // should not compile : only const return for times
  //cp1.timeInitial() = 2; // should not compile : only const return for times
  cp1.timeInitial(0.5);
  cp1.timeFinal(2.);
  BOOST_CHECK(cp1.timeInitial() == 0.5);
  BOOST_CHECK(cp1.timeFinal() == 2.);
  BOOST_CHECK(cp1.duration() == 1.5);
  cp1.duration(3.);
  BOOST_CHECK(cp1.timeInitial() == 0.5);
  BOOST_CHECK(cp1.timeFinal() == 3.5);
  BOOST_CHECK(cp1.duration() == 3.);
  // check exeptions related to wrong timing 
  BOOST_CHECK_THROW(cp1.timeFinal(0.1),std::invalid_argument);
  BOOST_CHECK_THROW(cp1.duration(-2.),std::invalid_argument);
  // constructor with timings, should throw : 
  BOOST_CHECK_THROW(ContactPhase(0.9,0.1),std::invalid_argument);
  BOOST_CHECK_THROW(ContactPhase(0.,-1),std::invalid_argument);

  // Constructor with timings : 
  ContactPhase cp2(0.5,2.);
  BOOST_CHECK(cp2.timeInitial() == 0.5);
  BOOST_CHECK(cp2.timeFinal() == 2.);
  BOOST_CHECK(cp2.duration() == 1.5);

  // Check contacts creation / removal
  ContactPatch patchL(SE3::Identity().setRandom());
  bool newContact =  cp2.addContact("left_leg",patchL);
  BOOST_CHECK(newContact);
  BOOST_CHECK(cp2.isEffectorInContact("left_leg"));  BOOST_CHECK(!cp2.isEffectorInContact("other"));
  BOOST_CHECK(cp2.contactPatch("left_leg") == patchL);
  BOOST_CHECK(cp2.contactPatches()["left_leg"] == patchL);
  BOOST_CHECK_THROW(cp2.contactPatch("other"),std::invalid_argument);

  ContactPatch patchR(SE3::Identity().setRandom());
  newContact =  cp2.addContact("right_leg",patchR);
  BOOST_CHECK(newContact);
  BOOST_CHECK(cp2.isEffectorInContact("left_leg"));
  BOOST_CHECK(cp2.isEffectorInContact("right_leg"));
  BOOST_CHECK(cp2.contactPatch("left_leg") == patchL);
  BOOST_CHECK(cp2.contactPatch("right_leg") == patchR);
  BOOST_CHECK(cp2.contactPatches()["left_leg"] == patchL);
  BOOST_CHECK(cp2.contactPatches()["right_leg"] == patchR);

  cp2.contactPatches().insert(std::pair<std::string,ContactPatch>("other",patchR)); // Why does it compile ? there is only const getter
  BOOST_CHECK(cp2.contactPatches().count("other") == 0); // previous line should have no effect, only const getter
  BOOST_CHECK(!cp2.isEffectorInContact("other"));
  ContactPatch patchR2(SE3::Identity().setRandom(),1.5);
//  cp2.contactPatches

  newContact = cp2.addContact("left_leg",ContactPatch(SE3::Identity().setRandom(),0.5));
  BOOST_CHECK(!newContact);
  BOOST_CHECK(cp2.isEffectorInContact("left_leg"));
  BOOST_CHECK(cp2.isEffectorInContact("right_leg"));
  BOOST_CHECK(cp2.contactPatch("left_leg") != patchL);

  bool exist = cp2.removeContact("right_leg");
  BOOST_CHECK(exist);
  BOOST_CHECK(cp2.isEffectorInContact("left_leg"));
  BOOST_CHECK(!cp2.isEffectorInContact("right_leg"));
  exist = cp2.removeContact("other");
  BOOST_CHECK(!exist);
  exist = cp2.removeContact("right_leg");
  BOOST_CHECK(!exist);

  // check adding forces trajectory :
  pointX_t f0 = point12_t::Random();
  pointX_t f1 = point12_t::Random();
  const double min = 0.5;
  const double max = 2.;
  curve_ptr_t force12d(new polynomial_t(f0, f1, min, max));
  bool newTraj = cp2.addContactForceTrajectory("left_leg",force12d);
  BOOST_CHECK(newTraj);
  newTraj = cp2.addContactForceTrajectory("left_leg",force12d);
  BOOST_CHECK(!newTraj);
  BOOST_CHECK((*cp2.contactForces()["left_leg"])(min) == f0);
  BOOST_CHECK((*cp2.contactForces()["left_leg"])(max) == (*force12d)(max));
  BOOST_CHECK((*cp2.contactForces()["left_leg"])(1.2) == (*force12d)(1.2));
  BOOST_CHECK((*cp2.contactForces("left_leg"))(min) == f0);
  BOOST_CHECK((*cp2.contactForces("left_leg"))(max) == (*force12d)(max));
  BOOST_CHECK((*cp2.contactForces("left_leg"))(1.2) == (*force12d)(1.2));
  pointX_t f0_1 = point12_t::Random();
  pointX_t f1_1 = point12_t::Random();
  curve_ptr_t force12d_1(new polynomial_t(f0_1, f1_1, min, max));
  cp2.contactForces().insert(std::pair<std::string,curve_ptr_t>("right_leg",force12d_1)); // should not have any effect only const getter
  BOOST_CHECK(cp2.contactForces().count("right_leg") == 0);
  cp2.contactForces().erase("left_leg"); // should not have any effect
  BOOST_CHECK((*cp2.contactForces("left_leg"))(min) == f0);
  BOOST_CHECK_THROW(cp2.addContactForceTrajectory("right_leg",force12d),std::invalid_argument); // right leg is not in contact, cannot add force
  BOOST_CHECK_THROW(cp2.contactForces("right_leg"),std::invalid_argument);

  // check with piecewise curve :
  curve_ptr_t force_C2 = buildPiecewisePolynomialC2();
  cp2.addContactForceTrajectory("left_leg",force_C2);
  BOOST_CHECK((*cp2.contactForces("left_leg"))(1.) == (*force_C2)(1.));
  BOOST_CHECK((*cp2.contactForces("left_leg"))(10.) == (*force_C2)(10.));
  BOOST_CHECK((*cp2.contactForces("left_leg"))(5.7) == (*force_C2)(5.7));


  pointX_t nf0(1);
  nf0 << 56.3;
  pointX_t nf1(1);
  nf1 << 5893.2;
  curve_ptr_t force1d(new polynomial_t(nf0, nf1, min, max));
  newTraj = cp2.addContactNormalForceTrajectory("left_leg",force1d);
  BOOST_CHECK(newTraj);
  newTraj = cp2.addContactNormalForceTrajectory("left_leg",force1d);
  BOOST_CHECK(!newTraj);
  BOOST_CHECK((*cp2.contactNormalForces()["left_leg"])(min) == nf0);
  BOOST_CHECK((*cp2.contactNormalForces()["left_leg"])(max) == (*force1d)(max));
  BOOST_CHECK((*cp2.contactNormalForces()["left_leg"])(1.2) == (*force1d)(1.2));
  BOOST_CHECK((*cp2.contactNormalForces("left_leg"))(min) == nf0);
  BOOST_CHECK((*cp2.contactNormalForces("left_leg"))(max) == (*force1d)(max));
  BOOST_CHECK((*cp2.contactNormalForces("left_leg"))(1.2) == (*force1d)(1.2));
  pointX_t nf0_1(1);
  nf0_1 << 147.2;
  pointX_t nf1_1(1);
  nf1_1 << 562;
  curve_ptr_t force1d_1(new polynomial_t(nf0_1, nf1_1, min, max));
  cp2.contactNormalForces().insert(std::pair<std::string,curve_ptr_t>("right_leg",force1d_1)); // should not have any effect only const getter
  BOOST_CHECK(cp2.contactNormalForces().count("right_leg") == 0);
  cp2.contactNormalForces().erase("left_leg"); // should not have any effect
  BOOST_CHECK((*cp2.contactNormalForces("left_leg"))(min) == nf0);
  BOOST_CHECK_THROW(cp2.contactNormalForces("right_leg"),std::invalid_argument);
  BOOST_CHECK_THROW(cp2.addContactNormalForceTrajectory("right_leg",force12d),std::invalid_argument); // right leg is not in contact, cannot add force
  BOOST_CHECK_THROW(cp2.addContactNormalForceTrajectory("left_leg",force12d),std::invalid_argument); // should be of dimension 1 

  // Check effector trajectory : 
  curve_SE3_ptr effR = buildPiecewiseSE3();
  newTraj = cp2.addEffectorTrajectory("right_leg",effR);
  BOOST_CHECK(newTraj);
  newTraj = cp2.addEffectorTrajectory("right_leg",effR);
  BOOST_CHECK(!newTraj);
  BOOST_CHECK((*cp2.effectorTrajectories()["right_leg"])(min).isApprox((*effR)(min)));
  BOOST_CHECK((*cp2.effectorTrajectories()["right_leg"])(max).isApprox((*effR)(max)));
  BOOST_CHECK((*cp2.effectorTrajectories()["right_leg"])(1.2).isApprox((*effR)(1.2)));
  BOOST_CHECK((*cp2.effectorTrajectories("right_leg"))(min).isApprox((*effR)(min)));
  BOOST_CHECK((*cp2.effectorTrajectories("right_leg"))(max).isApprox((*effR)(max)));
  BOOST_CHECK((*cp2.effectorTrajectories("right_leg"))(1.2).isApprox((*effR)(1.2)));
  BOOST_CHECK_THROW(cp2.effectorTrajectories("left_leg"),std::invalid_argument);
  BOOST_CHECK_THROW(cp2.addEffectorTrajectory("left_leg",effR),std::invalid_argument); // right leg is not in contact, cannot add force
  //cp2.addEffectorTrajectory("right_leg",force12d); // should not compile : not the right return type for the curve

  // check that when removing the contact the trajectory are correctly deleted : 
  cp2.removeContact("left_leg");
  BOOST_CHECK(!cp2.isEffectorInContact("left_leg"));
  BOOST_CHECK(cp2.contactForces().count("left_leg") == 0);
  BOOST_CHECK(cp2.contactNormalForces().count("left_leg") == 0);
  // check that when adding a contact the effector trajectory is correctly deleted :
  cp2.addContact("right_leg",patchR);
  BOOST_CHECK(cp2.effectorTrajectories().count("right_leg") == 0);

  // check setting init / final data :
  // public members :
  /*
  point3_t m_c_init;
  point3_t m_dc_init;
  point3_t m_ddc_init;
  point3_t m_L_init;
  point3_t m_dL_init;
  pointX_t m_q_init;
  point3_t m_c_final;
  point3_t m_dc_final;
  point3_t m_ddc_final;
  point3_t m_L_final;
  point3_t m_dL_final;
  pointX_t m_q_final;
  */

  ContactPhase cp3(0.5,2.); // public members :
  point3_t c_init= point3_t::Random();
  point3_t dc_init= point3_t::Random();
  pointX_t ddc_init= point3_t::Random(); // test with point X for automatic conversion
  pointX_t L_init= point3_t::Random();
  point3_t dL_init= point3_t::Random();
  pointX_t q_init= point12_t::Random();
  pointX_t c_final= point3_t::Random();
  pointX_t dc_final= point3_t::Random();
  point3_t ddc_final= point3_t::Random();
  pointX_t L_final= point3_t::Random();
  point3_t dL_final= point3_t::Random();
  pointX_t q_final= point12_t::Random();
  cp3.m_c_init = c_init;
  cp3.m_dc_init = dc_init;
  cp3.m_ddc_init = ddc_init;
  cp3.m_L_init = L_init;
  cp3.m_dL_init = dL_init;
  cp3.m_q_init = q_init;
  cp3.m_c_final = c_final;
  cp3.m_dc_final = dc_final;
  cp3.m_ddc_final = ddc_final;
  cp3.m_L_final = L_final;
  cp3.m_dL_final = dL_final;
  cp3.m_q_final = q_final;
  BOOST_CHECK(cp3.m_c_init == c_init);
  BOOST_CHECK(cp3.m_dc_init == dc_init);
  BOOST_CHECK(cp3.m_ddc_init == ddc_init);
  BOOST_CHECK(cp3.m_L_init == L_init);
  BOOST_CHECK(cp3.m_dL_init == dL_init);
  BOOST_CHECK(cp3.m_q_init == q_init);
  BOOST_CHECK(cp3.m_c_final == c_final);
  BOOST_CHECK(cp3.m_dc_final == dc_final);
  BOOST_CHECK(cp3.m_ddc_final == ddc_final);
  BOOST_CHECK(cp3.m_L_final == L_final);
  BOOST_CHECK(cp3.m_dL_final == dL_final);
  BOOST_CHECK(cp3.m_q_final == q_final);

  // check adding trajectory to public members :
/*
  curve_ptr m_q;
  curve_ptr m_dq;
  curve_ptr m_ddq;
  curve_ptr m_tau;
  curve_ptr m_c;
  curve_ptr m_dc;
  curve_ptr m_ddc;
  curve_ptr m_L;
  curve_ptr m_dL;
  curve_ptr m_wrench;
  curve_ptr m_zmp;
  curve_SE3_ptr m_root;
  */
  { // inner scope to check that the curves are not destroyed
  curve_ptr_t q = buildRandomPolynomial12D();
  curve_ptr_t dq = buildRandomPolynomial12D();
  curve_ptr_t ddq = buildRandomPolynomial12D();
  curve_ptr_t tau = buildRandomPolynomial12D();
  curve_ptr_t c = buildRandomPolynomial3D();
  curve_ptr_t dc = buildRandomPolynomial3D();
  curve_ptr_t ddc = buildRandomPolynomial3D();
  curve_ptr_t L = buildRandomPolynomial3D();
  curve_ptr_t dL = buildRandomPolynomial3D();
  curve_ptr_t wrench = buildRandomPolynomial3D();
  curve_ptr_t zmp = buildRandomPolynomial3D();
  curve_SE3_ptr root = buildRandomSE3LinearTraj(1,5.5);
  cp3.m_q = q;
  cp3.m_dq = dq;
  cp3.m_ddq = ddq;
  cp3.m_tau = tau;
  cp3.m_c = c;
  cp3.m_dc = dc;
  cp3.m_ddc = ddc;
  cp3.m_L = L;
  cp3.m_dL = dL;
  cp3.m_wrench = wrench;
  cp3.m_zmp = zmp;
  cp3.m_root = root;

  BOOST_CHECK((*cp3.m_q)(q->min()) == (*q)(q->min()));
  BOOST_CHECK((*cp3.m_dq)(dq->min()) == (*dq)(dq->min()));
  BOOST_CHECK((*cp3.m_ddq)(ddq->min()) == (*ddq)(ddq->min()));
  BOOST_CHECK((*cp3.m_tau)(tau->min()) == (*tau)(tau->min()));
  BOOST_CHECK((*cp3.m_c)(c->min()) == (*c)(c->min()));
  BOOST_CHECK((*cp3.m_dc)(dc->min()) == (*dc)(dc->min()));
  BOOST_CHECK((*cp3.m_ddc)(ddc->min()) == (*ddc)(ddc->min()));
  BOOST_CHECK((*cp3.m_L)(L->min()) == (*L)(L->min()));
  BOOST_CHECK((*cp3.m_dL)(dL->min()) == (*dL)(dL->min()));
  BOOST_CHECK((*cp3.m_wrench)(wrench->min()) == (*wrench)(wrench->min()));
  BOOST_CHECK((*cp3.m_zmp)(zmp->min()) == (*zmp)(zmp->min()));
  BOOST_CHECK((*cp3.m_root)(root->min()).isApprox((*root)(root->min())));
  BOOST_CHECK((*cp3.m_q)(q->max()) == (*q)(q->max()));
  BOOST_CHECK((*cp3.m_dq)(dq->max()) == (*dq)(dq->max()));
  BOOST_CHECK((*cp3.m_ddq)(ddq->max()) == (*ddq)(ddq->max()));
  BOOST_CHECK((*cp3.m_tau)(tau->max()) == (*tau)(tau->max()));
  BOOST_CHECK((*cp3.m_c)(c->max()) == (*c)(c->max()));
  BOOST_CHECK((*cp3.m_dc)(dc->max()) == (*dc)(dc->max()));
  BOOST_CHECK((*cp3.m_ddc)(ddc->max()) == (*ddc)(ddc->max()));
  BOOST_CHECK((*cp3.m_L)(L->max()) == (*L)(L->max()));
  BOOST_CHECK((*cp3.m_dL)(dL->max()) == (*dL)(dL->max()));
  BOOST_CHECK((*cp3.m_wrench)(wrench->max()) == (*wrench)(wrench->max()));
  BOOST_CHECK((*cp3.m_zmp)(zmp->max()) == (*zmp)(zmp->max()));
  BOOST_CHECK((*cp3.m_root)(root->max()).isApprox((*root)(root->max())));
  }
  BOOST_CHECK_NO_THROW(cp3.m_q->operator()(cp3.m_q->min())); // check that the curves still exist after leaving the scope
  BOOST_CHECK_NO_THROW(cp3.m_dq->operator()(cp3.m_dq->min())); // check that the curves still exist after leaving the scope
  BOOST_CHECK_NO_THROW(cp3.m_ddq->operator()(cp3.m_ddq->min())); // check that the curves still exist after leaving the scope
  BOOST_CHECK_NO_THROW(cp3.m_tau->operator()(cp3.m_tau->min())); // check that the curves still exist after leaving the scope
  BOOST_CHECK_NO_THROW(cp3.m_c->operator()(cp3.m_c->min())); // check that the curves still exist after leaving the scope
  BOOST_CHECK_NO_THROW(cp3.m_dc->operator()(cp3.m_dc->min())); // check that the curves still exist after leaving the scope
  BOOST_CHECK_NO_THROW(cp3.m_ddc->operator()(cp3.m_ddc->min())); // check that the curves still exist after leaving the scope
  BOOST_CHECK_NO_THROW(cp3.m_L->operator()(cp3.m_L->min())); // check that the curves still exist after leaving the scope
  BOOST_CHECK_NO_THROW(cp3.m_dL->operator()(cp3.m_dL->min())); // check that the curves still exist after leaving the scope
  BOOST_CHECK_NO_THROW(cp3.m_wrench->operator()(cp3.m_wrench->min())); // check that the curves still exist after leaving the scope
  BOOST_CHECK_NO_THROW(cp3.m_root->operator()(cp3.m_root->min())); // check that the curves still exist after leaving the scope
  BOOST_CHECK_NO_THROW(cp3.m_zmp->operator()(cp3.m_zmp->min())); // check that the curves still exist after leaving the scope


  // check copy constructor and operator ==
  ContactPhase cp4(cp3);
  ContactPhase cp5 = cp3;
  BOOST_CHECK(cp2 != cp3);
  BOOST_CHECK(cp4 == cp3);
  BOOST_CHECK(cp5 == cp3);
  BOOST_CHECK(cp4.m_c_init == c_init);
  BOOST_CHECK(cp4.m_dc_init == dc_init);
  BOOST_CHECK(cp4.m_ddc_init == ddc_init);
  BOOST_CHECK(cp4.m_L_init == L_init);
  BOOST_CHECK(cp4.m_dL_init == dL_init);
  BOOST_CHECK(cp4.m_q_init == q_init);
  BOOST_CHECK(cp4.m_c_final == c_final);
  BOOST_CHECK(cp4.m_dc_final == dc_final);
  BOOST_CHECK(cp4.m_ddc_final == ddc_final);
  BOOST_CHECK(cp4.m_L_final == L_final);
  BOOST_CHECK(cp4.m_dL_final == dL_final);
  BOOST_CHECK(cp4.m_q_final == q_final);
  BOOST_CHECK(cp5.m_c_init == c_init);
  BOOST_CHECK(cp5.m_dc_init == dc_init);
  BOOST_CHECK(cp5.m_ddc_init == ddc_init);
  BOOST_CHECK(cp5.m_L_init == L_init);
  BOOST_CHECK(cp5.m_dL_init == dL_init);
  BOOST_CHECK(cp5.m_q_init == q_init);
  BOOST_CHECK(cp5.m_c_final == c_final);
  BOOST_CHECK(cp5.m_dc_final == dc_final);
  BOOST_CHECK(cp5.m_ddc_final == ddc_final);
  BOOST_CHECK(cp5.m_L_final == L_final);
  BOOST_CHECK(cp5.m_dL_final == dL_final);
  BOOST_CHECK(cp5.m_q_final == q_final);
  BOOST_CHECK((*cp5.m_q)(cp3.m_q->min())==(*cp3.m_q)(cp3.m_q->min()));
  BOOST_CHECK((*cp5.m_dq)(cp3.m_dq->min())==(*cp3.m_dq)(cp3.m_dq->min()));
  BOOST_CHECK((*cp5.m_ddq)(cp3.m_ddq->min())==(*cp3.m_ddq)(cp3.m_ddq->min()));
  BOOST_CHECK((*cp5.m_tau)(cp3.m_tau->min())==(*cp3.m_tau)(cp3.m_tau->min()));
  BOOST_CHECK((*cp5.m_c)(cp3.m_c->min())==(*cp3.m_c)(cp3.m_c->min()));
  BOOST_CHECK((*cp5.m_dc)(cp3.m_dc->min())==(*cp3.m_dc)(cp3.m_dc->min()));
  BOOST_CHECK((*cp5.m_ddc)(cp3.m_ddc->min())==(*cp3.m_ddc)(cp3.m_ddc->min()));
  BOOST_CHECK((*cp5.m_L)(cp3.m_L->min())==(*cp3.m_L)(cp3.m_L->min()));
  BOOST_CHECK((*cp5.m_dL)(cp3.m_dL->min())==(*cp3.m_dL)(cp3.m_dL->min()));
  BOOST_CHECK((*cp5.m_wrench)(cp3.m_wrench->min())==(*cp3.m_wrench)(cp3.m_wrench->min()));
  BOOST_CHECK((*cp5.m_zmp)(cp3.m_zmp->min())==(*cp3.m_zmp)(cp3.m_zmp->min()));
  BOOST_CHECK((*cp5.m_root)(cp3.m_root->min()).isApprox((*cp3.m_root)(cp3.m_root->min())));
  pointX_t p = point3_t::Random();
  cp5.m_c_init = p;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_c_init == p);
  BOOST_CHECK(cp3.m_c_init != p);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  p = point3_t::Random();
  cp5.m_dc_init = p;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_dc_init == p);
  BOOST_CHECK(cp3.m_dc_init != p);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  p = point3_t::Random();
  cp5.m_ddc_init = p;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_ddc_init == p);
  BOOST_CHECK(cp3.m_ddc_init != p);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  p = point3_t::Random();
  cp5.m_L_init = p;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_L_init == p);
  BOOST_CHECK(cp3.m_L_init != p);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  p = point3_t::Random();
  cp5.m_dL_init = p;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_dL_init == p);
  BOOST_CHECK(cp3.m_dL_init != p);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  p = point12_t::Random();
  cp5.m_q_init = p;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_q_init == p);
  BOOST_CHECK(cp3.m_q_init != p);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  p = point3_t::Random();
  cp5.m_c_final = p;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_c_final == p);
  BOOST_CHECK(cp3.m_c_final != p);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  p = point3_t::Random();
  cp5.m_dc_final = p;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_dc_final == p);
  BOOST_CHECK(cp3.m_dc_final != p);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  p = point3_t::Random();
  cp5.m_ddc_final = p;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_ddc_final == p);
  BOOST_CHECK(cp3.m_ddc_final != p);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  p = point3_t::Random();
  cp5.m_L_final = p;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_L_final == p);
  BOOST_CHECK(cp3.m_L_final != p);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  p = point3_t::Random();
  cp5.m_dL_final = p;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_dL_final == p);
  BOOST_CHECK(cp3.m_dL_final != p);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  p = point12_t::Random();
  cp5.m_q_final = p;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_q_final == p);
  BOOST_CHECK(cp3.m_q_final != p);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
678

Pierre Fernbach's avatar
Pierre Fernbach committed
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
  curve_ptr_t curve;
  curve = buildRandomPolynomial12D();
  cp5.m_q = curve;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_q == curve);
  BOOST_CHECK(cp3.m_q != curve);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  curve = buildRandomPolynomial12D();
  cp5.m_dq = curve;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_dq == curve);
  BOOST_CHECK(cp3.m_dq != curve);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  curve = buildRandomPolynomial12D();
  cp5.m_ddq = curve;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_ddq == curve);
  BOOST_CHECK(cp3.m_ddq != curve);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  curve = buildRandomPolynomial12D();
  cp5.m_tau = curve;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_tau == curve);
  BOOST_CHECK(cp3.m_tau != curve);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  curve = buildRandomPolynomial3D();
  cp5.m_c = curve;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_c == curve);
  BOOST_CHECK(cp3.m_c != curve);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  curve = buildRandomPolynomial3D();
  cp5.m_dc = curve;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_dc == curve);
  BOOST_CHECK(cp3.m_dc != curve);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  curve = buildRandomPolynomial3D();
  cp5.m_ddc = curve;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_ddc == curve);
  BOOST_CHECK(cp3.m_ddc != curve);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  curve = buildRandomPolynomial3D();
  cp5.m_L = curve;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_L == curve);
  BOOST_CHECK(cp3.m_L != curve);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  curve = buildRandomPolynomial3D();
  cp5.m_dL = curve;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_dL == curve);
  BOOST_CHECK(cp3.m_dL != curve);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  curve = buildRandomPolynomial3D();
  cp5.m_wrench = curve;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_wrench == curve);
  BOOST_CHECK(cp3.m_wrench != curve);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  curve = buildRandomPolynomial3D();
  cp5.m_zmp = curve;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_zmp == curve);
  BOOST_CHECK(cp3.m_zmp != curve);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  curve_SE3_ptr curveSE3 = buildRandomSE3LinearTraj(1.5,2.9);
  cp5.m_root = curveSE3;
  BOOST_CHECK(cp5 != cp3);
  BOOST_CHECK(cp5.m_root == curveSE3);
  BOOST_CHECK(cp3.m_root != curveSE3);
  cp5 = cp3;
  BOOST_CHECK(cp5 == cp3);
  // check serialization
765
766

}
Pierre Fernbach's avatar
Pierre Fernbach committed
767
768
769
770
771
772
773
774
775
776
777
778
779

//BOOST_AUTO_TEST_CASE(contact_sequence)
//{
//  ContactPhase4 cp;
//  ContactSequence4 cs(1);
//  ContactSequence4 cs_test(0);
//  cs.m_contact_phases[0] = cp;
//  // test serialization
//  cs.saveAsText("serialization_cs_test.test");
//  cs_test.loadFromText("serialization_cs_test.test");
//  remove("serialization_cp_test.test");
//  BOOST_CHECK(cs == cs_test);
//}
780

Justin Carpentier's avatar
Justin Carpentier committed
781
BOOST_AUTO_TEST_SUITE_END()