scenario.cpp 54 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"
13
#include "multicontact-api/scenario/contact-sequence.hpp"
Justin Carpentier's avatar
Justin Carpentier committed
14

15
#include <curves/fwd.h>
Pierre Fernbach's avatar
Pierre Fernbach committed
16
17
18
19
20
#include <curves/so3_linear.h>
#include <curves/se3_curve.h>
#include <curves/polynomial.h>
#include <curves/bezier_curve.h>
#include <curves/piecewise_curve.h>
21
22
#include <curves/exact_cubic.h>
#include <curves/cubic_hermite_spline.h>
Pierre Fernbach's avatar
Pierre Fernbach committed
23

24
25


26
typedef Eigen::Matrix<double, 1, 1> point1_t;
27
28
using curves::point3_t;
using curves::point6_t;
Pierre Fernbach's avatar
Pierre Fernbach committed
29
typedef Eigen::Matrix<double, 12, 1> point12_t;
30
31
32
33
using curves::pointX_t;
using curves::matrix3_t;
using curves::quaternion_t;
using curves::t_pointX_t;
34
using curves::t_point3_t;
35
36
using curves::curve_ptr_t;
using curves::curve_SE3_ptr_t;
Guilhem Saurel's avatar
Guilhem Saurel committed
37
using namespace multicontact_api::scenario;
38
typedef ContactSequence::ContactPhaseVector ContactPhaseVector;
Justin Carpentier's avatar
Justin Carpentier committed
39

Guilhem Saurel's avatar
Guilhem Saurel committed
40
41
template <typename Scalar>
struct ATpl {
42
  typedef pinocchio::SE3Tpl<Scalar> SE3;
Guilhem Saurel's avatar
Guilhem Saurel committed
43

Justin Carpentier's avatar
Justin Carpentier committed
44
  explicit ATpl() : data() {}
Guilhem Saurel's avatar
Guilhem Saurel committed
45
  explicit ATpl(const ATpl& other) : data(other.data){};
Guilhem Saurel's avatar
Guilhem Saurel committed
46

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

Guilhem Saurel's avatar
Guilhem Saurel committed
49
 protected:
Justin Carpentier's avatar
Justin Carpentier committed
50
51
52
53
  SE3 data;
};

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

Pierre Fernbach's avatar
Pierre Fernbach committed
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
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);

94
  curves::piecewise_t ppc_C2 = curves::piecewise_t::convert_discrete_points_to_polynomial<curves::polynomial_t>(
Pierre Fernbach's avatar
Pierre Fernbach committed
95
          points, points_derivative, points_second_derivative, time_points);
96
  curve_ptr_t res(new curves::piecewise_t(ppc_C2));
Pierre Fernbach's avatar
Pierre Fernbach committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  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);
116
  curve_ptr_t res (new curves::polynomial_t(vec.begin(), vec.end(), min, max));
Pierre Fernbach's avatar
Pierre Fernbach committed
117
118
119
120
121
122
123
124
125
126
127
  return  res;
}

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

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

128
129
130
131
132
curve_ptr_t buildRandomPolynomial1D(){
  return buildRandomPolynomial<point1_t>();
}


Pierre Fernbach's avatar
Pierre Fernbach committed
133
134


135
curve_SE3_ptr_t buildPiecewiseSE3(){
Pierre Fernbach's avatar
Pierre Fernbach committed
136
137
138
139
140
141
142
  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.);
143
  curve_SE3_ptr_t cLinear(new curves::SE3Curve_t(p0, p1, q0, q1, min, mid));
Pierre Fernbach's avatar
Pierre Fernbach committed
144
145
146
147
148
149
150
151
152
  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);
153
154
  boost::shared_ptr<curves::bezier_t> translation_bezier(new curves::bezier_t(params.begin(), params.end(), mid, max));
  curve_SE3_ptr_t cBezier(new curves::SE3Curve_t(translation_bezier, q0.toRotationMatrix(), q1.toRotationMatrix()));
Pierre Fernbach's avatar
Pierre Fernbach committed
155

156
157
158
  curves::piecewise_SE3_t piecewiseSE3(cLinear);
  piecewiseSE3.add_curve_ptr(cBezier);
  curve_SE3_ptr_t res = boost::make_shared<curves::piecewise_SE3_t>(piecewiseSE3);
Pierre Fernbach's avatar
Pierre Fernbach committed
159
160
161
162
163
164
165
166
167
168
169
170
171
  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();
}

172
curve_SE3_ptr_t buildRandomSE3LinearTraj(const double min, const double max){
Pierre Fernbach's avatar
Pierre Fernbach committed
173
174
  quaternion_t q0 = randomQuaternion();
  quaternion_t q1 = randomQuaternion();
175
176
  q0.normalize();
  q1.normalize();
Pierre Fernbach's avatar
Pierre Fernbach committed
177
178
179
  pointX_t p0 = point3_t::Random();
  pointX_t p1 = point3_t::Random();

180
  curve_SE3_ptr_t res(new curves::SE3Curve_t(p0, p1, q0, q1, min, max));
Pierre Fernbach's avatar
Pierre Fernbach committed
181
182
183
  return res;
}

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
void addRandomPointsValues(ContactPhase& cp){
  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();
  cp.m_c_init = c_init;
  cp.m_dc_init = dc_init;
  cp.m_ddc_init = ddc_init;
  cp.m_L_init = L_init;
  cp.m_dL_init = dL_init;
  cp.m_q_init = q_init;
  cp.m_c_final = c_final;
  cp.m_dc_final = dc_final;
  cp.m_ddc_final = ddc_final;
  cp.m_L_final = L_final;
  cp.m_dL_final = dL_final;
  cp.m_q_final = q_final;
}

void addRandomCurvesValues(ContactPhase& cp){
  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_t root = buildRandomSE3LinearTraj(1,5.5);
  cp.m_q = q;
  cp.m_dq = dq;
  cp.m_ddq = ddq;
  cp.m_tau = tau;
  cp.m_c = c;
  cp.m_dc = dc;
  cp.m_ddc = ddc;
  cp.m_L = L;
  cp.m_dL = dL;
  cp.m_wrench = wrench;
  cp.m_zmp = zmp;
  cp.m_root = root;
}

void addRandomContacts(ContactPhase& cp){
  cp.addContact("right_hand",ContactPatch(SE3::Identity().setRandom()));
  cp.addContact("left_foot",ContactPatch(SE3::Identity().setRandom()));
}

void addRandomForcesTrajs(ContactPhase& cp){
  cp.addContactForceTrajectory("right_hand",buildRandomPolynomial12D());
  cp.addContactForceTrajectory("left_foot",buildRandomPolynomial12D());
  cp.addContactNormalForceTrajectory("right_hand",buildRandomPolynomial1D());
  cp.addContactNormalForceTrajectory("left_foot",buildRandomPolynomial1D());
}

void addRandomEffectorTrajectories(ContactPhase& cp){
  cp.addEffectorTrajectory("left_hand",buildRandomSE3LinearTraj(0,2));
  cp.addEffectorTrajectory("right_foot",buildRandomSE3LinearTraj(0,2));
}

ContactPhase buildRandomContactPhase(const double min = 0. , const double max = 2.){
    ContactPhase cp(min,max);
    addRandomPointsValues(cp);
    addRandomCurvesValues(cp);
    addRandomContacts(cp);
    addRandomForcesTrajs(cp);
    addRandomEffectorTrajectories(cp);
    return cp;
}
Pierre Fernbach's avatar
Pierre Fernbach committed
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
void explicitContactPhaseAssertEqual(ContactPhase& cp1, ContactPhase& cp2){
  BOOST_CHECK(cp1.m_c_init == cp2.m_c_init);
  BOOST_CHECK(cp1.m_dc_init == cp2.m_dc_init);
  BOOST_CHECK(cp1.m_ddc_init == cp2.m_ddc_init);
  BOOST_CHECK(cp1.m_L_init == cp2.m_L_init);
  BOOST_CHECK(cp1.m_dL_init == cp2.m_dL_init);
  BOOST_CHECK(cp1.m_q_init == cp2.m_q_init);
  BOOST_CHECK(cp1.m_c_final == cp2.m_c_final);
  BOOST_CHECK(cp1.m_dc_final == cp2.m_dc_final);
  BOOST_CHECK(cp1.m_ddc_final == cp2.m_ddc_final);
  BOOST_CHECK(cp1.m_L_final == cp2.m_L_final);
  BOOST_CHECK(cp1.m_dL_final == cp2.m_dL_final);
  BOOST_CHECK(cp1.m_q_final == cp2.m_q_final);
  BOOST_CHECK(cp1.m_c_init == cp2.m_c_init);
  BOOST_CHECK(cp1.m_dc_init == cp2.m_dc_init);
  BOOST_CHECK(cp1.m_ddc_init == cp2.m_ddc_init);
  BOOST_CHECK(cp1.m_L_init == cp2.m_L_init);
  BOOST_CHECK(cp1.m_dL_init == cp2.m_dL_init);
  BOOST_CHECK(cp1.m_q_init == cp2.m_q_init);
  BOOST_CHECK(cp1.m_c_final == cp2.m_c_final);
  BOOST_CHECK(cp1.m_dc_final == cp2.m_dc_final);
  BOOST_CHECK(cp1.m_ddc_final == cp2.m_ddc_final);
  BOOST_CHECK(cp1.m_L_final == cp2.m_L_final);
  BOOST_CHECK(cp1.m_dL_final == cp2.m_dL_final);
  BOOST_CHECK(cp1.m_q_final == cp2.m_q_final);
  BOOST_CHECK((*cp1.m_q)(cp2.m_q->min())==(*cp2.m_q)(cp2.m_q->min()));
  BOOST_CHECK((*cp1.m_dq)(cp2.m_dq->min())==(*cp2.m_dq)(cp2.m_dq->min()));
  BOOST_CHECK((*cp1.m_ddq)(cp2.m_ddq->min())==(*cp2.m_ddq)(cp2.m_ddq->min()));
  BOOST_CHECK((*cp1.m_tau)(cp2.m_tau->min())==(*cp2.m_tau)(cp2.m_tau->min()));
  BOOST_CHECK((*cp1.m_c)(cp2.m_c->min())==(*cp2.m_c)(cp2.m_c->min()));
  BOOST_CHECK((*cp1.m_dc)(cp2.m_dc->min())==(*cp2.m_dc)(cp2.m_dc->min()));
  BOOST_CHECK((*cp1.m_ddc)(cp2.m_ddc->min())==(*cp2.m_ddc)(cp2.m_ddc->min()));
  BOOST_CHECK((*cp1.m_L)(cp2.m_L->min())==(*cp2.m_L)(cp2.m_L->min()));
  BOOST_CHECK((*cp1.m_dL)(cp2.m_dL->min())==(*cp2.m_dL)(cp2.m_dL->min()));
  BOOST_CHECK((*cp1.m_wrench)(cp2.m_wrench->min())==(*cp2.m_wrench)(cp2.m_wrench->min()));
  BOOST_CHECK((*cp1.m_zmp)(cp2.m_zmp->min())==(*cp2.m_zmp)(cp2.m_zmp->min()));
  BOOST_CHECK((*cp1.m_root)(cp2.m_root->min()).isApprox((*cp2.m_root)(cp2.m_root->min())));
302
303
304
305
306
307
308
309
310
311
312
313
  BOOST_CHECK(cp1.m_q->isApprox(cp2.m_q.get()));
  BOOST_CHECK(cp1.m_dq->isApprox(cp2.m_dq.get()));
  BOOST_CHECK(cp1.m_ddq->isApprox(cp2.m_ddq.get()));
  BOOST_CHECK(cp1.m_tau->isApprox(cp2.m_tau.get()));
  BOOST_CHECK(cp1.m_c->isApprox(cp2.m_c.get()));
  BOOST_CHECK(cp1.m_dc->isApprox(cp2.m_dc.get()));
  BOOST_CHECK(cp1.m_ddc->isApprox(cp2.m_ddc.get()));
  BOOST_CHECK(cp1.m_L->isApprox(cp2.m_L.get()));
  BOOST_CHECK(cp1.m_dL->isApprox(cp2.m_dL.get()));
  BOOST_CHECK(cp1.m_wrench->isApprox(cp2.m_wrench.get()));
  BOOST_CHECK(cp1.m_zmp->isApprox(cp2.m_zmp.get()));
  BOOST_CHECK(cp1.m_root->isApprox(cp2.m_root.get()));
314
315
316
317
318
  BOOST_CHECK(cp1.contactForces() == cp2.contactForces());
  BOOST_CHECK(cp1.contactNormalForces() == cp2.contactNormalForces());
  BOOST_CHECK(cp1.effectorTrajectories() == cp2.effectorTrajectories());
  BOOST_CHECK(cp1.effectorsInContact() == cp2.effectorsInContact());
  BOOST_CHECK(cp1.contactPatches() == cp2.contactPatches());
319
320
  const ContactPhase::t_strings eeNames = cp2.effectorsInContact();
  for(ContactPhase::t_strings::const_iterator ee = eeNames.begin() ; ee != eeNames.end() ; ++ee){
321
    BOOST_CHECK(cp2.isEffectorInContact(*ee));
322
    BOOST_CHECK(cp1.contactPatch(*ee) == cp2.contactPatch(*ee));
323
324
    if(cp1.contactForces().count(*ee)){
      BOOST_CHECK(cp2.contactForces().count(*ee));
325
      BOOST_CHECK(cp1.contactForces(*ee)->isApprox(cp2.contactForces(*ee).get()));
326
327
328
329
330
      BOOST_CHECK((*cp1.contactForces(*ee))(cp1.contactForces(*ee)->min()) == (*cp2.contactForces(*ee))(cp2.contactForces(*ee)->min()));
      BOOST_CHECK((*cp1.contactForces(*ee))(cp1.contactForces(*ee)->max()) == (*cp2.contactForces(*ee))(cp2.contactForces(*ee)->max()));
    }
    if(cp1.contactNormalForces().count(*ee)){
      BOOST_CHECK(cp2.contactNormalForces().count(*ee));
331
      BOOST_CHECK(cp1.contactNormalForces(*ee)->isApprox(cp2.contactNormalForces(*ee).get()));
332
333
334
335
      BOOST_CHECK((*cp1.contactNormalForces(*ee))(cp1.contactNormalForces(*ee)->min()) == (*cp2.contactNormalForces(*ee))(cp2.contactNormalForces(*ee)->min()));
      BOOST_CHECK((*cp1.contactNormalForces(*ee))(cp1.contactNormalForces(*ee)->max()) == (*cp2.contactNormalForces(*ee))(cp2.contactNormalForces(*ee)->max()));
    }
  }
336
337
  const ContactPhase::CurveSE3Map_t trajMap = cp2.effectorTrajectories();
  for (ContactPhase::CurveSE3Map_t::const_iterator mit = trajMap.begin() ; mit != trajMap.end(); ++mit)
338
339
  {
    BOOST_CHECK(cp2.effectorTrajectories().count(mit->first));
340
    BOOST_CHECK(cp1.effectorTrajectories(mit->first)->isApprox(cp2.effectorTrajectories(mit->first).get()));
341
342
    BOOST_CHECK((*cp1.effectorTrajectories(mit->first))(cp1.effectorTrajectories(mit->first)->min()).isApprox((*cp2.effectorTrajectories(mit->first))(cp2.effectorTrajectories(mit->first)->min())));
    BOOST_CHECK((*cp1.effectorTrajectories(mit->first))(cp1.effectorTrajectories(mit->first)->max()).isApprox((*cp2.effectorTrajectories(mit->first))(cp2.effectorTrajectories(mit->first)->max())));
343
344
345
  }
}

Justin Carpentier's avatar
Justin Carpentier committed
346
347
348

BOOST_AUTO_TEST_SUITE(BOOST_TEST_MODULE)

Guilhem Saurel's avatar
Guilhem Saurel committed
349
BOOST_AUTO_TEST_CASE(contact_model) {
Justin Carpentier's avatar
Justin Carpentier committed
350
351
  const double mu = 0.3;
  const double ZMP_radius = 0.01;
Guilhem Saurel's avatar
Guilhem Saurel committed
352

Guilhem Saurel's avatar
Guilhem Saurel committed
353
  ContactModelPlanar mp1(mu, ZMP_radius);
Justin Carpentier's avatar
Justin Carpentier committed
354
  ContactModelPlanar mp2(mp1);
355
356
  BOOST_CHECK(mp1.m_mu == mu);
  BOOST_CHECK(mp1.m_ZMP_radius == ZMP_radius);
Guilhem Saurel's avatar
Guilhem Saurel committed
357

Justin Carpentier's avatar
Justin Carpentier committed
358
  BOOST_CHECK(mp1 == mp2);
359
360
361
362
  mp1.m_mu = 0.5;
  BOOST_CHECK(mp1 != mp2);

  //TODO : check serialization
Justin Carpentier's avatar
Justin Carpentier committed
363
364
}

Guilhem Saurel's avatar
Guilhem Saurel committed
365
BOOST_AUTO_TEST_CASE(contact_patch) {
Pierre Fernbach's avatar
Pierre Fernbach committed
366
  // check default constructor :
Justin Carpentier's avatar
Justin Carpentier committed
367
  ContactPatch cp;
Pierre Fernbach's avatar
Pierre Fernbach committed
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
  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);

404
  // operator =
Pierre Fernbach's avatar
Pierre Fernbach committed
405
406
407
408
409
  ContactPatch cp5 = cp4;
  BOOST_CHECK(cp4 == cp5);
  cp5.friction() = 2.;
  BOOST_CHECK(cp4 != cp5);

Pierre Fernbach's avatar
Pierre Fernbach committed
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
  // 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
426
427
}

428
429
BOOST_AUTO_TEST_CASE(contact_phase)
{
Pierre Fernbach's avatar
Pierre Fernbach committed
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
  // 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.);
447
448
  BOOST_CHECK(cp1.numContacts() == 0);

Pierre Fernbach's avatar
Pierre Fernbach committed
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
  // 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);
469
  BOOST_CHECK(cp2.numContacts() == 1);
Pierre Fernbach's avatar
Pierre Fernbach committed
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
  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);
493
  BOOST_CHECK(cp2.numContacts() == 2);
Pierre Fernbach's avatar
Pierre Fernbach committed
494
495
496
497
498

  bool exist = cp2.removeContact("right_leg");
  BOOST_CHECK(exist);
  BOOST_CHECK(cp2.isEffectorInContact("left_leg"));
  BOOST_CHECK(!cp2.isEffectorInContact("right_leg"));
499
  BOOST_CHECK(cp2.numContacts() == 1);
Pierre Fernbach's avatar
Pierre Fernbach committed
500
501
502
503
504
505
506
507
508
509
  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.;
510
  curve_ptr_t force12d(new curves::polynomial_t(f0, f1, min, max));
Pierre Fernbach's avatar
Pierre Fernbach committed
511
512
513
514
515
516
517
518
519
520
521
522
  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();
523
  curve_ptr_t force12d_1(new curves::polynomial_t(f0_1, f1_1, min, max));
Pierre Fernbach's avatar
Pierre Fernbach committed
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
  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;
543
  curve_ptr_t force1d(new curves::polynomial_t(nf0, nf1, min, max));
Pierre Fernbach's avatar
Pierre Fernbach committed
544
545
546
547
548
549
550
551
552
553
554
555
556
557
  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;
558
  curve_ptr_t force1d_1(new curves::polynomial_t(nf0_1, nf1_1, min, max));
Pierre Fernbach's avatar
Pierre Fernbach committed
559
560
561
562
563
564
  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
565
566
  BOOST_CHECK_THROW(cp2.addContactNormalForceTrajectory("left_leg",force12d),std::invalid_argument); // should be of dimension 1

Pierre Fernbach's avatar
Pierre Fernbach committed
567
568

  // Check effector trajectory : 
569
  curve_SE3_ptr_t effR = buildPiecewiseSE3();
Pierre Fernbach's avatar
Pierre Fernbach committed
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
  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 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;
  */

  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();
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
  cp2.m_c_init = c_init;
  cp2.m_dc_init = dc_init;
  cp2.m_ddc_init = ddc_init;
  cp2.m_L_init = L_init;
  cp2.m_dL_init = dL_init;
  cp2.m_q_init = q_init;
  cp2.m_c_final = c_final;
  cp2.m_dc_final = dc_final;
  cp2.m_ddc_final = ddc_final;
  cp2.m_L_final = L_final;
  cp2.m_dL_final = dL_final;
  cp2.m_q_final = q_final;
  BOOST_CHECK(cp2.m_c_init == c_init);
  BOOST_CHECK(cp2.m_dc_init == dc_init);
  BOOST_CHECK(cp2.m_ddc_init == ddc_init);
  BOOST_CHECK(cp2.m_L_init == L_init);
  BOOST_CHECK(cp2.m_dL_init == dL_init);
  BOOST_CHECK(cp2.m_q_init == q_init);
  BOOST_CHECK(cp2.m_c_final == c_final);
  BOOST_CHECK(cp2.m_dc_final == dc_final);
  BOOST_CHECK(cp2.m_ddc_final == ddc_final);
  BOOST_CHECK(cp2.m_L_final == L_final);
  BOOST_CHECK(cp2.m_dL_final == dL_final);
  BOOST_CHECK(cp2.m_q_final == q_final);
Pierre Fernbach's avatar
Pierre Fernbach committed
637
638
639
640
641
642
643
644
645
646
647
648
649
650

  // 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;
651
  curve_SE3_ptr_t m_root;
Pierre Fernbach's avatar
Pierre Fernbach committed
652
653
654
655
656
657
658
659
660
661
662
663
664
  */
  { // 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();
665
  curve_SE3_ptr_t root = buildRandomSE3LinearTraj(1,5.5);
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
  cp2.m_q = q;
  cp2.m_dq = dq;
  cp2.m_ddq = ddq;
  cp2.m_tau = tau;
  cp2.m_c = c;
  cp2.m_dc = dc;
  cp2.m_ddc = ddc;
  cp2.m_L = L;
  cp2.m_dL = dL;
  cp2.m_wrench = wrench;
  cp2.m_zmp = zmp;
  cp2.m_root = root;

  BOOST_CHECK((*cp2.m_q)(q->min()) == (*q)(q->min()));
  BOOST_CHECK((*cp2.m_dq)(dq->min()) == (*dq)(dq->min()));
  BOOST_CHECK((*cp2.m_ddq)(ddq->min()) == (*ddq)(ddq->min()));
  BOOST_CHECK((*cp2.m_tau)(tau->min()) == (*tau)(tau->min()));
  BOOST_CHECK((*cp2.m_c)(c->min()) == (*c)(c->min()));
  BOOST_CHECK((*cp2.m_dc)(dc->min()) == (*dc)(dc->min()));
  BOOST_CHECK((*cp2.m_ddc)(ddc->min()) == (*ddc)(ddc->min()));
  BOOST_CHECK((*cp2.m_L)(L->min()) == (*L)(L->min()));
  BOOST_CHECK((*cp2.m_dL)(dL->min()) == (*dL)(dL->min()));
  BOOST_CHECK((*cp2.m_wrench)(wrench->min()) == (*wrench)(wrench->min()));
  BOOST_CHECK((*cp2.m_zmp)(zmp->min()) == (*zmp)(zmp->min()));
  BOOST_CHECK((*cp2.m_root)(root->min()).isApprox((*root)(root->min())));
  BOOST_CHECK((*cp2.m_q)(q->max()) == (*q)(q->max()));
  BOOST_CHECK((*cp2.m_dq)(dq->max()) == (*dq)(dq->max()));
  BOOST_CHECK((*cp2.m_ddq)(ddq->max()) == (*ddq)(ddq->max()));
  BOOST_CHECK((*cp2.m_tau)(tau->max()) == (*tau)(tau->max()));
  BOOST_CHECK((*cp2.m_c)(c->max()) == (*c)(c->max()));
  BOOST_CHECK((*cp2.m_dc)(dc->max()) == (*dc)(dc->max()));
  BOOST_CHECK((*cp2.m_ddc)(ddc->max()) == (*ddc)(ddc->max()));
  BOOST_CHECK((*cp2.m_L)(L->max()) == (*L)(L->max()));
  BOOST_CHECK((*cp2.m_dL)(dL->max()) == (*dL)(dL->max()));
  BOOST_CHECK((*cp2.m_wrench)(wrench->max()) == (*wrench)(wrench->max()));
  BOOST_CHECK((*cp2.m_zmp)(zmp->max()) == (*zmp)(zmp->max()));
  BOOST_CHECK((*cp2.m_root)(root->max()).isApprox((*root)(root->max())));
Pierre Fernbach's avatar
Pierre Fernbach committed
703
  }
704
705
706
707
708
709
710
711
712
713
714
715
  BOOST_CHECK_NO_THROW(cp2.m_q->operator()(cp2.m_q->min())); // check that the curves still exist after leaving the scope
  BOOST_CHECK_NO_THROW(cp2.m_dq->operator()(cp2.m_dq->min())); // check that the curves still exist after leaving the scope
  BOOST_CHECK_NO_THROW(cp2.m_ddq->operator()(cp2.m_ddq->min())); // check that the curves still exist after leaving the scope
  BOOST_CHECK_NO_THROW(cp2.m_tau->operator()(cp2.m_tau->min())); // check that the curves still exist after leaving the scope
  BOOST_CHECK_NO_THROW(cp2.m_c->operator()(cp2.m_c->min())); // check that the curves still exist after leaving the scope
  BOOST_CHECK_NO_THROW(cp2.m_dc->operator()(cp2.m_dc->min())); // check that the curves still exist after leaving the scope
  BOOST_CHECK_NO_THROW(cp2.m_ddc->operator()(cp2.m_ddc->min())); // check that the curves still exist after leaving the scope
  BOOST_CHECK_NO_THROW(cp2.m_L->operator()(cp2.m_L->min())); // check that the curves still exist after leaving the scope
  BOOST_CHECK_NO_THROW(cp2.m_dL->operator()(cp2.m_dL->min())); // check that the curves still exist after leaving the scope
  BOOST_CHECK_NO_THROW(cp2.m_wrench->operator()(cp2.m_wrench->min())); // check that the curves still exist after leaving the scope
  BOOST_CHECK_NO_THROW(cp2.m_root->operator()(cp2.m_root->min())); // check that the curves still exist after leaving the scope
  BOOST_CHECK_NO_THROW(cp2.m_zmp->operator()(cp2.m_zmp->min())); // check that the curves still exist after leaving the scope
Pierre Fernbach's avatar
Pierre Fernbach committed
716
717


718
  //add more contact and trajectories :
719
720
721
  cp2.addContact("right_hand",ContactPatch(SE3::Identity().setRandom()));
  cp2.addContactForceTrajectory("right_hand",buildRandomPolynomial12D());
  cp2.addContactNormalForceTrajectory("right_hand",buildRandomPolynomial1D());
722
  int num_ctc = 0;
723
  for (ContactPhase::CurveMap_t::const_iterator mit = cp2.contactForces().begin() ; mit != cp2.contactForces().end(); ++mit)
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
  {
    BOOST_CHECK(mit->first == "right_hand" || mit->first == "left_leg");
    num_ctc ++;
  }
  BOOST_CHECK(num_ctc == 2);

  curve_SE3_ptr_t eff_knee(buildRandomSE3LinearTraj(cp2.timeInitial(),cp2.timeFinal()));
  double min_knee = eff_knee->min();
  double max_knee = eff_knee->max();
  newTraj = cp2.addEffectorTrajectory("knee",eff_knee);
  BOOST_CHECK(newTraj);
  BOOST_CHECK((*cp2.effectorTrajectories()["knee"])(min_knee).isApprox((*eff_knee)(min_knee)));
  BOOST_CHECK((*cp2.effectorTrajectories()["knee"])(max_knee).isApprox((*eff_knee)(max_knee)));
  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().size() == 2);
  int num_eff_traj = 0;
742
743
  const ContactPhase::CurveSE3Map_t trajMap = cp2.effectorTrajectories();
  for (ContactPhase::CurveSE3Map_t::const_iterator mit = trajMap.begin() ; mit != trajMap.end(); ++mit)
744
745
746
747
748
749
750
751
752
  {
    BOOST_CHECK(mit->first == "knee" || mit->first == "right_leg");
    num_eff_traj ++;
  }
  BOOST_CHECK(num_eff_traj == 2);
  BOOST_CHECK(cp2.effectorsWithTrajectory().size() == 2);
  BOOST_CHECK(cp2.effectorHaveAtrajectory("knee"));
  BOOST_CHECK(cp2.effectorHaveAtrajectory("right_leg"));
  BOOST_CHECK(!cp2.effectorHaveAtrajectory("left_leg"));
753

754
  // check copy constructor and operator ==
755
756
757
758
759
  ContactPhase cp4(cp2);
  ContactPhase cp5 = cp2;
  BOOST_CHECK(cp2 != cp1);
  BOOST_CHECK(cp4 == cp2);
  BOOST_CHECK(cp5 == cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
  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);
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
  BOOST_CHECK((*cp5.m_q)(cp2.m_q->min())==(*cp2.m_q)(cp2.m_q->min()));
  BOOST_CHECK((*cp5.m_dq)(cp2.m_dq->min())==(*cp2.m_dq)(cp2.m_dq->min()));
  BOOST_CHECK((*cp5.m_ddq)(cp2.m_ddq->min())==(*cp2.m_ddq)(cp2.m_ddq->min()));
  BOOST_CHECK((*cp5.m_tau)(cp2.m_tau->min())==(*cp2.m_tau)(cp2.m_tau->min()));
  BOOST_CHECK((*cp5.m_c)(cp2.m_c->min())==(*cp2.m_c)(cp2.m_c->min()));
  BOOST_CHECK((*cp5.m_dc)(cp2.m_dc->min())==(*cp2.m_dc)(cp2.m_dc->min()));
  BOOST_CHECK((*cp5.m_ddc)(cp2.m_ddc->min())==(*cp2.m_ddc)(cp2.m_ddc->min()));
  BOOST_CHECK((*cp5.m_L)(cp2.m_L->min())==(*cp2.m_L)(cp2.m_L->min()));
  BOOST_CHECK((*cp5.m_dL)(cp2.m_dL->min())==(*cp2.m_dL)(cp2.m_dL->min()));
  BOOST_CHECK((*cp5.m_wrench)(cp2.m_wrench->min())==(*cp2.m_wrench)(cp2.m_wrench->min()));
  BOOST_CHECK((*cp5.m_zmp)(cp2.m_zmp->min())==(*cp2.m_zmp)(cp2.m_zmp->min()));
  BOOST_CHECK((*cp5.m_root)(cp2.m_root->min()).isApprox((*cp2.m_root)(cp2.m_root->min())));
  BOOST_CHECK(cp5.contactForces() == cp2.contactForces());
  BOOST_CHECK(cp5.contactNormalForces() == cp2.contactNormalForces());
  BOOST_CHECK(cp5.effectorTrajectories() == cp2.effectorTrajectories());
  BOOST_CHECK(cp5.effectorsInContact() == cp2.effectorsInContact());
  BOOST_CHECK(cp5.contactPatches() == cp2.contactPatches());
  BOOST_CHECK(cp5.contactForces("left_leg") == cp2.contactForces("left_leg"));
  BOOST_CHECK(cp5.contactNormalForces("left_leg") == cp2.contactNormalForces("left_leg"));
  BOOST_CHECK(cp5.contactForces("right_hand") == cp2.contactForces("right_hand"));
  BOOST_CHECK(cp5.contactNormalForces("right_hand") == cp2.contactNormalForces("right_hand"));
  BOOST_CHECK(cp5.effectorTrajectories("right_leg") == cp2.effectorTrajectories("right_leg"));
  BOOST_CHECK(cp5.effectorTrajectories("knee") == cp2.effectorTrajectories("knee"));
  BOOST_CHECK(cp5.contactPatch("left_leg") == cp2.contactPatch("left_leg"));
  BOOST_CHECK(cp5.contactPatch("right_hand") == cp2.contactPatch("right_hand"));

  // check operator != and ==
Pierre Fernbach's avatar
Pierre Fernbach committed
811
812
  pointX_t p = point3_t::Random();
  cp5.m_c_init = p;
813
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
814
  BOOST_CHECK(cp5.m_c_init == p);
815
816
817
  BOOST_CHECK(cp2.m_c_init != p);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
818
819
  p = point3_t::Random();
  cp5.m_dc_init = p;
820
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
821
  BOOST_CHECK(cp5.m_dc_init == p);
822
823
824
  BOOST_CHECK(cp2.m_dc_init != p);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
825
826
  p = point3_t::Random();
  cp5.m_ddc_init = p;
827
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
828
  BOOST_CHECK(cp5.m_ddc_init == p);
829
830
831
  BOOST_CHECK(cp2.m_ddc_init != p);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
832
833
  p = point3_t::Random();
  cp5.m_L_init = p;
834
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
835
  BOOST_CHECK(cp5.m_L_init == p);
836
837
838
  BOOST_CHECK(cp2.m_L_init != p);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
839
840
  p = point3_t::Random();
  cp5.m_dL_init = p;
841
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
842
  BOOST_CHECK(cp5.m_dL_init == p);
843
844
845
  BOOST_CHECK(cp2.m_dL_init != p);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
846
847
  p = point12_t::Random();
  cp5.m_q_init = p;
848
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
849
  BOOST_CHECK(cp5.m_q_init == p);
850
851
852
  BOOST_CHECK(cp2.m_q_init != p);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
853
854
  p = point3_t::Random();
  cp5.m_c_final = p;
855
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
856
  BOOST_CHECK(cp5.m_c_final == p);
857
858
859
  BOOST_CHECK(cp2.m_c_final != p);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
860
861
  p = point3_t::Random();
  cp5.m_dc_final = p;
862
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
863
  BOOST_CHECK(cp5.m_dc_final == p);
864
865
866
  BOOST_CHECK(cp2.m_dc_final != p);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
867
868
  p = point3_t::Random();
  cp5.m_ddc_final = p;
869
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
870
  BOOST_CHECK(cp5.m_ddc_final == p);
871
872
873
  BOOST_CHECK(cp2.m_ddc_final != p);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
874
875
  p = point3_t::Random();
  cp5.m_L_final = p;
876
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
877
  BOOST_CHECK(cp5.m_L_final == p);
878
879
880
  BOOST_CHECK(cp2.m_L_final != p);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
881
882
  p = point3_t::Random();
  cp5.m_dL_final = p;
883
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
884
  BOOST_CHECK(cp5.m_dL_final == p);
885
886
887
  BOOST_CHECK(cp2.m_dL_final != p);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
888
889
  p = point12_t::Random();
  cp5.m_q_final = p;
890
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
891
  BOOST_CHECK(cp5.m_q_final == p);
892
893
894
  BOOST_CHECK(cp2.m_q_final != p);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
895

Pierre Fernbach's avatar
Pierre Fernbach committed
896
897
898
  curve_ptr_t curve;
  curve = buildRandomPolynomial12D();
  cp5.m_q = curve;
899
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
900
  BOOST_CHECK(cp5.m_q == curve);
901
902
903
  BOOST_CHECK(cp2.m_q != curve);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
904
905
  curve = buildRandomPolynomial12D();
  cp5.m_dq = curve;
906
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
907
  BOOST_CHECK(cp5.m_dq == curve);
908
909
910
  BOOST_CHECK(cp2.m_dq != curve);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
911
912
  curve = buildRandomPolynomial12D();
  cp5.m_ddq = curve;
913
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
914
  BOOST_CHECK(cp5.m_ddq == curve);
915
916
917
  BOOST_CHECK(cp2.m_ddq != curve);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
918
919
  curve = buildRandomPolynomial12D();
  cp5.m_tau = curve;
920
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
921
  BOOST_CHECK(cp5.m_tau == curve);
922
923
924
  BOOST_CHECK(cp2.m_tau != curve);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
925
926
  curve = buildRandomPolynomial3D();
  cp5.m_c = curve;
927
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
928
  BOOST_CHECK(cp5.m_c == curve);
929
930
931
  BOOST_CHECK(cp2.m_c != curve);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
932
933
  curve = buildRandomPolynomial3D();
  cp5.m_dc = curve;
934
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
935
  BOOST_CHECK(cp5.m_dc == curve);
936
937
938
  BOOST_CHECK(cp2.m_dc != curve);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
939
940
  curve = buildRandomPolynomial3D();
  cp5.m_ddc = curve;
941
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
942
  BOOST_CHECK(cp5.m_ddc == curve);
943
944
945
  BOOST_CHECK(cp2.m_ddc != curve);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
946
947
  curve = buildRandomPolynomial3D();
  cp5.m_L = curve;
948
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
949
  BOOST_CHECK(cp5.m_L == curve);
950
951
952
  BOOST_CHECK(cp2.m_L != curve);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
953
954
  curve = buildRandomPolynomial3D();
  cp5.m_dL = curve;
955
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
956
  BOOST_CHECK(cp5.m_dL == curve);
957
958
959
  BOOST_CHECK(cp2.m_dL != curve);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
960
961
  curve = buildRandomPolynomial3D();
  cp5.m_wrench = curve;
962
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
963
  BOOST_CHECK(cp5.m_wrench == curve);
964
965
966
  BOOST_CHECK(cp2.m_wrench != curve);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
967
968
  curve = buildRandomPolynomial3D();
  cp5.m_zmp = curve;
969
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
970
  BOOST_CHECK(cp5.m_zmp == curve);
971
972
973
  BOOST_CHECK(cp2.m_zmp != curve);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
974
  curve_SE3_ptr_t curveSE3 = buildRandomSE3LinearTraj(1.5,2.9);
Pierre Fernbach's avatar
Pierre Fernbach committed
975
  cp5.m_root = curveSE3;
976
  BOOST_CHECK(cp5 != cp2);
Pierre Fernbach's avatar
Pierre Fernbach committed
977
  BOOST_CHECK(cp5.m_root == curveSE3);
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
  BOOST_CHECK(cp2.m_root != curveSE3);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);

  cp5.removeContact("left_leg");
  BOOST_CHECK(cp5 != cp2);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
  cp5.addContact("test",ContactPatch(SE3::Identity().setRandom()));
  BOOST_CHECK(cp5 != cp2);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
  cp5.addEffectorTrajectory("knee",buildRandomSE3LinearTraj(cp5.timeInitial(),cp5.timeFinal()));
  BOOST_CHECK(cp5 != cp2);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
  cp5.addContactForceTrajectory("left_leg",buildRandomPolynomial12D());
  BOOST_CHECK(cp5 != cp2);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);
  cp5.addContactNormalForceTrajectory("left_leg",buildRandomPolynomial1D());
  BOOST_CHECK(cp5 != cp2);
  cp5 = cp2;
  BOOST_CHECK(cp5 == cp2);


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

Pierre Fernbach's avatar
Pierre Fernbach committed
1013
  // check serialization
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
  std::string fileName("fileTest");
  ContactPhase cp_from_txt,cp_from_xml,cp_from_bin;
  std::cout<<"cp2 before serialization : "<<std::endl<<cp2<<std::endl;

  cp2.saveAsText(fileName+".txt");
  cp_from_txt.loadFromText(fileName+".txt");
  BOOST_CHECK(cp2 == cp_from_txt);
  std::cout<<"cp2 after deserialization : "<<std::endl<<cp_from_txt<<std::endl;

  cp2.saveAsXML(fileName+".xml","ContactPhase");
  cp_from_xml.loadFromXML(fileName+".xml","ContactPhase");
  BOOST_CHECK(cp2 == cp_from_xml);

  cp2.saveAsBinary(fileName);
  cp_from_bin.loadFromBinary(fileName);
  BOOST_CHECK(cp2 == cp_from_bin);

  explicitContactPhaseAssertEqual(cp2,cp_from_txt);
1032
1033

}
Pierre Fernbach's avatar
Pierre Fernbach committed
1034

1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
BOOST_AUTO_TEST_CASE(contact_phase_helpers){
  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);

  // check COM trajectory :
  ContactPhase cp;
  cp.setCOMtrajectoryFromPoints(points,points_derivative,points_second_derivative,time_points);
  BOOST_CHECK_EQUAL(cp.m_c->dim(),3);
  BOOST_CHECK_EQUAL(cp.m_dc->dim(),3);
  BOOST_CHECK_EQUAL(cp.m_ddc->dim(),3);
  BOOST_CHECK_EQUAL(cp.m_c->min(),t0);
  BOOST_CHECK_EQUAL(cp.m_dc->min(),t0);
  BOOST_CHECK_EQUAL(cp.m_ddc->min(),t0);
  BOOST_CHECK_EQUAL(cp.m_c->max(),t3);
  BOOST_CHECK_EQUAL(cp.m_dc->max(),t3);
  BOOST_CHECK_EQUAL(cp.m_ddc->max(),t3);
  BOOST_CHECK((*cp.m_c)(t0).isApprox(p0));
  BOOST_CHECK((*cp.m_c)(t1).isApprox(p1));
  BOOST_CHECK((*cp.m_c)(t2).isApprox(p2));
  BOOST_CHECK((*cp.m_c)(t3).isApprox(p3));
  BOOST_CHECK((*cp.m_dc)(t0).isApprox(d0));
  BOOST_CHECK((*cp.m_dc)(t1).isApprox(d1));
  BOOST_CHECK((*cp.m_dc)(t2).isApprox(d2));
  BOOST_CHECK((*cp.m_dc)(t3).isApprox(d3));
  BOOST_CHECK((*cp.m_ddc)(t0).isApprox(dd0));
  BOOST_CHECK((*cp.m_ddc)(t1).isApprox(dd1));
  BOOST_CHECK((*cp.m_ddc)(t2).isApprox(dd2));
  BOOST_CHECK((*cp.m_ddc)(t3).isApprox(dd3));

  BOOST_CHECK(cp.m_c_init.isApprox(p0));
  BOOST_CHECK(cp.m_c_final.isApprox(p3));
  BOOST_CHECK(cp.m_dc_init.isApprox(d0));
  BOOST_CHECK(cp.m_dc_final.isApprox(d3));
  BOOST_CHECK(cp.m_ddc_init.isApprox(dd0));
  BOOST_CHECK(cp.m_ddc_final.isApprox(dd3));

  // check that init/final are not modified if they ere already set :
  ContactPhase cp2 = buildRandomContactPhase();
  point3_t c_init,c_final,dc_init,dc_final,ddc_init,ddc_final;
  c_init = cp2.m_c_init;
  dc_init = cp2.m_dc_init;
  ddc_init = cp2.m_ddc_init;
  c_final = cp2.m_c_final;
  dc_final = cp2.m_dc_final;
  ddc_final = cp2.m_ddc_final;
  cp2.setCOMtrajectoryFromPoints(points,points_derivative,points_second_derivative,time_points);
  BOOST_CHECK(cp2.m_c_init.isApprox(c_init));
  BOOST_CHECK(cp2.m_c_final.isApprox(c_final));
  BOOST_CHECK(cp2.m_dc_init.isApprox(dc_init));
  BOOST_CHECK(cp2.m_dc_final.isApprox(dc_final));
  BOOST_CHECK(cp2.m_ddc_init.isApprox(ddc_init));
  BOOST_CHECK(cp2.m_ddc_final.isApprox(ddc_final));
  BOOST_CHECK(!cp2.m_c_init.isApprox(p0));
  BOOST_CHECK(!cp2.m_c_final.isApprox(p3));
  BOOST_CHECK(!cp2.m_dc_init.isApprox(d0));
  BOOST_CHECK(!cp2.m_dc_final.isApprox(d3));
  BOOST_CHECK(!cp2.m_ddc_init.isApprox(dd0));
  BOOST_CHECK(!cp2.m_ddc_final.isApprox(dd3));

  //check AM trajectory

  cp.setAMtrajectoryFromPoints(points,points_derivative,time_points);
  BOOST_CHECK_EQUAL(cp.m_L->dim(),3);
  BOOST_CHECK_EQUAL(cp.m_dL->dim(),3);
  BOOST_CHECK_EQUAL(cp.m_L->min(),t0);
  BOOST_CHECK_EQUAL(cp.m_dL->min(),t0);
  BOOST_CHECK_EQUAL(cp.m_L->max(),t3);
  BOOST_CHECK_EQUAL(cp.m_dL->max(),t3);
  BOOST_CHECK((*cp.m_L)(t0).isApprox(p0));
  BOOST_CHECK((*cp.m_L)(t1).isApprox(p1));
  BOOST_CHECK((*cp.m_L)(t2).isApprox(p2));
  BOOST_CHECK((*cp.m_L)(t3).isApprox(p3));
  BOOST_CHECK((*cp.m_dL)(t0).isApprox(d0));
  BOOST_CHECK((*cp.m_dL)(t1).isApprox(d1));
  BOOST_CHECK((*cp.m_dL)(t2).isApprox(d2));
  BOOST_CHECK((*cp.m_dL)(t3).isApprox(d3));

  BOOST_CHECK(cp.m_L_init.isApprox(p0));
  BOOST_CHECK(cp.m_L_final.isApprox(p3));
  BOOST_CHECK(cp.m_dL_init.isApprox(d0));
  BOOST_CHECK(cp.m_dL_final.isApprox(d3));

  // check that init/final are not modified if they ere already set :
  point3_t L_init,L_final,dL_init,dL_final;
  L_init = cp2.m_L_init;
  dL_init = cp2.m_dL_init;
  L_final = cp2.m_L_final;
  dL_final = cp2.m_dL_final;
  cp2.setAMtrajectoryFromPoints(points,points_derivative,time_points);
  BOOST_CHECK(cp2.m_L_init.isApprox(L_init));
  BOOST_CHECK(cp2.m_L_final.isApprox(L_final));
  BOOST_CHECK(cp2.m_dL_init.isApprox(dL_init));
  BOOST_CHECK(cp2.m_dL_final.isApprox(dL_final));
  BOOST_CHECK(!cp2.m_L_init.isApprox(p0));
  BOOST_CHECK(!cp2.m_L_final.isApprox(p3));
  BOOST_CHECK(!cp2.m_dL_init.isApprox(d0));
  BOOST_CHECK(!cp2.m_dL_final.isApprox(d3));


  // check q trajectory :
  pointX_t q0 = point12_t::Random();
  pointX_t q1 = point12_t::Random();
  pointX_t q2 = point12_t::Random();
  pointX_t q3 = point12_t::Random();
  pointX_t dq0 = point12_t::Random();
  pointX_t dq1 = point12_t::Random();
  pointX_t dq2 = point12_t::Random();
  pointX_t dq3 = point12_t::Random();
  pointX_t ddq0 = point12_t::Random();
  pointX_t ddq1 = point12_t::Random();
  pointX_t ddq2 = point12_t::Random();
  pointX_t ddq3 = point12_t::Random();
  t_pointX_t points_q;
  points_q.push_back(q0);
  points_q.push_back(q1);
  points_q.push_back(q2);
  points_q.push_back(q3);
  t_pointX_t points_derivative_q;
  points_derivative_q.push_back(dq0);
  points_derivative_q.push_back(dq1);
  points_derivative_q.push_back(dq2);
  points_derivative_q.push_back(dq3);
  t_pointX_t points_second_derivative_q;
  points_second_derivative_q.push_back(ddq0);
  points_second_derivative_q.push_back(ddq1);
  points_second_derivative_q.push_back(ddq2);
  points_second_derivative_q.push_back(ddq3);

  cp.setJointsTrajectoryFromPoints(points_q,points_derivative_q,points_second_derivative_q,time_points);
  BOOST_CHECK_EQUAL(cp.m_q->dim(),12);
  BOOST_CHECK_EQUAL(cp.m_dq->dim(),12);
  BOOST_CHECK_EQUAL(cp.m_ddq->dim(),12);
  BOOST_CHECK_EQUAL(cp.m_q->min(),t0);
  BOOST_CHECK_EQUAL(cp.m_dq->min(),t0);
  BOOST_CHECK_EQUAL(cp.m_ddq->min(),t0);
  BOOST_CHECK_EQUAL(cp.m_q->max(),t3);
  BOOST_CHECK_EQUAL(cp.m_dq->max(),t3);
  BOOST_CHECK_EQUAL(cp.m_ddq->max(),t3);
  BOOST_CHECK((*cp.m_q)(t0).isApprox(q0));
  BOOST_CHECK((*cp.m_q)(t1).isApprox(q1));
  BOOST_CHECK((*cp.m_q)(t2).isApprox(q2));
  BOOST_CHECK((*cp.m_q)(t3).isApprox(q3));
  BOOST_CHECK((*cp.m_dq)(t0).isApprox(dq0));
  BOOST_CHECK((*cp.m_dq)(t1).isApprox(dq1));
  BOOST_CHECK((*cp.m_dq)(t2).isApprox(dq2));
  BOOST_CHECK((*cp.m_dq)(t3).isApprox(dq3));
  BOOST_CHECK((*cp.m_ddq)(t0).isApprox(ddq0));
  BOOST_CHECK((*cp.m_ddq)(t1).isApprox(ddq1));
  BOOST_CHECK((*cp.m_ddq)(t2).isApprox(ddq2));
  BOOST_CHECK((*cp.m_ddq)(t3).isApprox(ddq3));

  BOOST_CHECK(cp.m_q_init.isApprox(q0));
  BOOST_CHECK(cp.m_q_final.isApprox(q3));

  // check that init/final are not modified if they ere already set :
  pointX_t q_init,q_final;
  q_init = cp2.m_q_init;
  q_final = cp2.m_q_final;
  cp2.setJointsTrajectoryFromPoints(points_q,points_derivative_q,points_second_derivative_q,time_points);
  BOOST_CHECK(cp2.m_q_init.isApprox(q_init));
  BOOST_CHECK(cp2.m_q_final.isApprox(q_final));
  BOOST_CHECK(!cp2.m_q_init.isApprox(q0));
  BOOST_CHECK(!cp2.m_q_final.isApprox(q3));


  // check that errors are correctly throw when required :
  // not the correct size
  BOOST_CHECK_THROW(cp.setCOMtrajectoryFromPoints(points_q,points_derivative,points_second_derivative,time_points),std::invalid_argument);
  BOOST_CHECK_THROW(cp.setCOMtrajectoryFromPoints(points,points_derivative_q,points_second_derivative,time_points),std::invalid_argument);
  BOOST_CHECK_THROW(cp.setCOMtrajectoryFromPoints(points,points_derivative,points_second_derivative_q,time_points),std::invalid_argument);
  BOOST_CHECK_THROW(cp.setCOMtrajectoryFromPoints(points_q,points_derivative_q,points_second_derivative_q,time_points),std::invalid_argument);
  BOOST_CHECK_THROW(cp.setAMtrajectoryFromPoints(points_q,points_derivative_q,time_points),std::invalid_argument);
  BOOST_CHECK_THROW(cp.setAMtrajectoryFromPoints(points,points_derivative_q,time_points),std::invalid_argument);
  BOOST_CHECK_THROW(cp.setAMtrajectoryFromPoints(points_q,points_derivative,time_points),std::invalid_argument);
  // not the same number of points :
  t_pointX_t points2 = points;
  points2.push_back(d0);
  t_pointX_t points_derivative2 = points_derivative;
  points_derivative2.push_back(d0);
  t_pointX_t points_second_derivative2 = points_second_derivative;
  points_second_derivative2.push_back(d0);
  std::vector<double> time_points2 = time_points;
  time_points2.push_back(50.);
  BOOST_CHECK_THROW(cp.setCOMtrajectoryFromPoints(points2,points_derivative,points_second_derivative,time_points),std::invalid_argument);
  BOOST_CHECK_THROW(cp.setCOMtrajectoryFromPoints(points,points_derivative2,points_second_derivative,time_points),std::invalid_argument);
  BOOST_CHECK_THROW(cp.setCOMtrajectoryFromPoints(points,points_derivative,points_second_derivative2,time_points),std::invalid_argument);
  BOOST_CHECK_THROW(cp.setCOMtrajectoryFromPoints(points,points_derivative,points_second_derivative,time_points2),std::invalid_argument);
  BOOST_CHECK_THROW(cp.setAMtrajectoryFromPoints(points2,points_derivative,time_points),std::invalid_argument);
  BOOST_CHECK_THROW(cp.setAMtrajectoryFromPoints(points,points_derivative2,time_points),std::invalid_argument);
  BOOST_CHECK_THROW(cp.setAMtrajectoryFromPoints(points,points_derivative,time_points2),std::invalid_argument);
  t_pointX_t points_q2 = points_q;
  points_q2.push_back(q0);
  t_pointX_t points_derivative_q2 = points_derivative_q;
  points_derivative_q2.push_back(q0);
  t_pointX_t points_second_derivative_q2 = points_second_derivative_q;
  points_second_derivative_q2.push_back(q0);
  BOOST_CHECK_THROW(cp.setJointsTrajectoryFromPoints(points_q2,points_derivative_q,points_second_derivative_q,time_points),std::invalid_argument);
  BOOST_CHECK_THROW(cp.setJointsTrajectoryFromPoints(points_q,points_derivative_q2,points_second_derivative_q,time_points),std::invalid_argument);
  BOOST_CHECK_THROW(cp.setJointsTrajectoryFromPoints(points_q,points_derivative_q,points_second_derivative_q2,time_points),std::invalid_argument);
  BOOST_CHECK_THROW(cp.setJointsTrajectoryFromPoints(points_q,points_derivative_q,points_second_derivative_q,time_points2),std::invalid_argument);


}

1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
BOOST_AUTO_TEST_CASE(contact_sequence)
{
  // test without specifying size and using 'append' :
  ContactSequence cs1 = ContactSequence(0);
  BOOST_CHECK(cs1.size() == 0);
  ContactPhase cp0 = buildRandomContactPhase(0,2);
  ContactPhase cp1 = buildRandomContactPhase(2,4.);
  size_t id = cs1.append(cp0);
  BOOST_CHECK(cs1.size() == 1);
  BOOST_CHECK(id == 0);
  BOOST_CHECK(cs1.contactPhase(0) == cp0);
  id = cs1.append(cp1);
  BOOST_CHECK(cs1.size() == 2);
  BOOST_CHECK(id == 1);
  BOOST_CHECK(cs1.contactPhase(0) == cp0);
  BOOST_CHECK(cs1.contactPhase(1) == cp1);
  // check with accessor to the contact phases vector
  ContactPhaseVector phases = cs1.contactPhases();
  BOOST_CHECK(phases.size() == 2);
  BOOST_CHECK(phases[0] == cp0);
  BOOST_CHECK(phases[1] == cp1);

  // check that the accessor to contactPhases() create a copy :
  ContactPhase cp2 = buildRandomContactPhase(0,2);
  phases.push_back(cp2);
  BOOST_CHECK(phases.size() == 3);
  BOOST_CHECK(cs1.size() == 2 ); // original contact sequence should not be modified
  phases[1].duration(3.);
  BOOST_CHECK(cs1.contactPhase(1) == cp1); // original contact sequence should not be modified

  // check that contactPhase(id) getter return non const reference :
  cs1.contactPhase(1).timeFinal(10.);
  BOOST_CHECK(cs1.contactPhase(1) != cp1);
  BOOST_CHECK(cs1.contactPhase(1).timeFinal() == 10.);

  cs1.contactPhase(0) = cp2;
  BOOST_CHECK(cs1.contactPhase(0) == cp2);

  // check with creating a contact sequence with a given size :
  ContactPhase cp_default;
  ContactSequence cs2 = ContactSequence(3);
  BOOST_CHECK(cs2.size() == 3);
  for(size_t i = 0 ; i < 3 ; ++i)
    BOOST_CHECK(cs2.contactPhase(i) == cp_default);

  // try to modify the uninitialized contact phase inside the sequence from the reference
  ContactPhase& cp2_0 = cs2.contactPhase(0);
  point3_t c_init = point3_t::Random();
  cp2_0.m_c_init = c_init;
  cp2_0.duration(10);
  BOOST_CHECK(cs2.contactPhase(0) != cp_default);
  BOOST_CHECK(cs2.contactPhase(0).duration() == 10);
  BOOST_CHECK(cs2.contactPhase(0).m_c_init == c_init);

  cs2.contactPhase(1) = cp1;
  BOOST_CHECK(cs2.contactPhase(1) == cp1);

  // try resize method :
  // with smaller value than current :
  cs2.resize(1);
  BOOST_CHECK(cs2.size() == 1);
  BOOST_CHECK(cs2.contactPhase(0).duration() == 10);
  BOOST_CHECK(cs2.contactPhase(0).m_c_init == c_init);

  // check with greater size than current :
  cs2.resize(4);
  BOOST_CHECK(cs2.size() == 4);
  BOOST_CHECK(cs2.contactPhase(0).duration() == 10);
  BOOST_CHECK(cs2.contactPhase(0).m_c_init == c_init);
  for(size_t i = 1 ; i < 4 ; ++i)
    BOOST_CHECK(cs2.contactPhase(i) == cp_default);

1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
  // test operator == :
  ContactSequence cs3;
  ContactSequence cs4;

  BOOST_CHECK(cs3 == cs4);
  ContactPhase cp3_0 = buildRandomContactPhase();
  cs3.append(cp3_0);
  BOOST_CHECK(cs3 != cs4);
  BOOST_CHECK(!(cs3 == cs4));
  cs4.append(cp3_0);
  BOOST_CHECK(cs3 == cs4);
  ContactPhase cp3_1 = buildRandomContactPhase();
  cs3.append(cp3_1);
  BOOST_CHECK(cs3 != cs4);
  cs4.append(cp3_1);
  BOOST_CHECK(cs3 == cs4);
  cs4.contactPhase(1).duration(10);
  BOOST_CHECK(cs4.contactPhase(1) != cp3_1);
  BOOST_CHECK(cs3 != cs4);
  ContactSequence cs5(2);
  cs5.contactPhase(0) = cp3_0;
  BOOST_CHECK(cs3 != cs5);
  cs5.contactPhase(1) = cp3_1;
  BOOST_CHECK(cs3 == cs5);


  // test copy constructor :
  ContactSequence cs6;
  for(size_t i = 0 ; i < 10 ; ++i){
    ContactPhase cp6 = buildRandomContactPhase();
    cs6.append(cp6);
  }
  BOOST_CHECK(cs6.size() == 10);

  ContactSequence cs7(cs6);
  BOOST_CHECK(cs7 == cs6);
  for(size_t i = 0 ; i < 10 ; ++i){
    BOOST_CHECK(cs6.contactPhase(i) == cs7.contactPhase(i));
  }


  // test serialization :
  std::string fileName("fileTest");
  ContactSequence cs_from_txt,cs_from_xml,cs_from_bin;

  cs6.saveAsText(fileName+".txt");
  cs_from_txt.loadFromText(fileName+".txt");
  BOOST_CHECK(cs6 == cs_from_txt);

  cs6.saveAsXML(fileName+".xml","ContactSequence");
  cs_from_xml.loadFromXML(fileName+".xml","ContactSequence");
  BOOST_CHECK(cs6 == cs_from_xml);

  cs6.saveAsBinary(fileName);
  cs_from_bin.loadFromBinary(fileName);
  BOOST_CHECK(cs6 == cs_from_bin);
1401
}
1402

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