interbody-distances-capsule.cc.in 8.64 KB
Newer Older
Florent Lamiraux's avatar
Florent Lamiraux committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Copyright (C) 2013, 2014 LAAS-CNRS
// Author: Florent Lamiraux
//
// This file is part of the test-hpp.
//
// test-hpp is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// test-hpp is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with test-hpp.  If not, see <http://www.gnu.org/licenses/>.

#include <sstream>
#include <ostream>
#include <fstream>
#include <vector>

#include <Eigen/Core>

26
#include <hpp/fcl/shape/geometric_shapes.h>
27

Florent Lamiraux's avatar
Florent Lamiraux committed
28
29
30
31
32
33
34
#include <hpp/util/debug.hh>
#include <hpp/model/body.hh>
#include <hpp/model/collision-object.hh>
#include <hpp/model/device.hh>
#include <hpp/model/joint.hh>
#include <hpp/model/urdf/util.hh>

35

Florent Lamiraux's avatar
Florent Lamiraux committed
36
37
38
#define BOOST_TEST_MODULE forward_kinematics
#include <boost/test/included/unit_test.hpp>

39
40
41
42
43
44
45
46
47
48
#define CHECK_CLOSE_TO_0(x, eps) BOOST_CHECK_CLOSE ((x + 1.0), (1.0), (eps))

typedef boost::shared_ptr <const fcl::Capsule> CapsuleConstPtr_t;
typedef hpp::model::matrix_t::Scalar value_type;
typedef hpp::model::CollisionObjectPtr_t CollisionObjectPtr_t;
typedef hpp::model::vector3_t vector3_t;
typedef hpp::model::DistanceResult DistanceResult_t;
typedef hpp::model::Transform3f Transform3f;
typedef hpp::model::vector3_t vector3_t;

Florent Lamiraux's avatar
Florent Lamiraux committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
static const unsigned int nbLines = 274;
static const std::string filename
("@PROJECT_SOURCE_DIR@/data/distances-capsule.data");

struct Distance_t {
  Distance_t () :d_ (0), x0_ (0), y0_ (0), z0_ (0), x1_ (0), y1_ (0), z1_ (0)
  {
  }
  Distance_t (double d, double x0, double y0, double z0,
	      double x1, double y1, double z1) : d_ (d),
						 x0_ (x0), y0_ (y0), z0_ (z0),
						 x1_ (x1), y1_ (y1), z1_ (z1)
  {
  }
  Distance_t (const Distance_t& other) :
    d_ (other.d_), x0_ (other.x0_), y0_ (other.y0_), z0_ (other.z0_),
    x1_ (other.x1_), y1_ (other.y1_), z1_ (other.z1_)
  {
  }
  Distance_t& swap ()
  {
    double tmp;
    tmp = x1_; x1_ = x0_; x0_ = tmp;
    tmp = y1_; y1_ = y0_; y0_ = tmp;
    tmp = z1_; z1_ = z0_; z0_ = tmp;
    return *this;
  }
  void checkClose (const Distance_t& other)
  {
78
79
80
81
82
83
84
    BOOST_CHECK_CLOSE (d_, other.d_, 1e-0);
    BOOST_CHECK_CLOSE (x0_, other.x0_, 1e-0);
    BOOST_CHECK_CLOSE (y0_, other.y0_, 1e-0);
    BOOST_CHECK_CLOSE (z0_, other.z0_, 1e-0);
    BOOST_CHECK_CLOSE (x1_, other.x1_, 1e-0);
    BOOST_CHECK_CLOSE (y1_, other.y1_, 1e-0);
    BOOST_CHECK_CLOSE (z1_, other.z1_, 1e-0);
Florent Lamiraux's avatar
Florent Lamiraux committed
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
  }
  // Distance and closest points on bodies.
  double d_, x0_, y0_, z0_, x1_, y1_, z1_;
}; // struct Distance_t

typedef std::map <std::pair <std::string, std::string>, Distance_t>
InterDistances_t;

InterDistances_t readDataFile ()
{
  InterDistances_t result;
  std::ifstream file;
  unsigned int line = 0;
  double d, x0, y0, z0, x1, y1, z1;
  std::string body1, body2;
  file.open (filename.c_str ());
  if (!file.good ()) {
    throw std::runtime_error ("failed to open file " + filename);
  }
  while (line < nbLines) {
    line++;
    file >> d >> body1 >> body2 >> x0 >> y0 >> z0 >> x1 >> y1 >> z1;
    // To make access in the map independant from the order in the pair key,
    // we insert both keys.
    std::pair <std::string, std::string> key1 (body1, body2);
    std::pair <std::string, std::string> key2 (body2, body1);
    Distance_t distance (d, x0, y0, z0, x1, y1, z1);
    result [key1] = distance;
    result [key2] = distance.swap ();
  }
  return  result;
}

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
void checkOptimalityConditions (const DistanceResult_t& distance)
{
  value_type d = distance.distance ();
  const vector3_t& p1 (distance.closestPointInner ());
  const vector3_t& p2 (distance.closestPointOuter ());

  // Get capsules
  CollisionObjectPtr_t o1 = distance.innerObject;
  CollisionObjectPtr_t o2 = distance.outerObject;
  CapsuleConstPtr_t c1 = HPP_DYNAMIC_PTR_CAST
    (const fcl::Capsule, distance.innerObject->fcl ()->collisionGeometry ());
  CapsuleConstPtr_t c2 = HPP_DYNAMIC_PTR_CAST
    (const fcl::Capsule, distance.outerObject->fcl ()->collisionGeometry ());
  // Get lengths and radii
  const value_type& l1 = c1->lz;
  const value_type& l2 = c2->lz;
  // Get centers and directions
  Transform3f tf1 = o1->getTransform ();
  Transform3f tf2 = o2->getTransform ();
  const vector3_t& center1 = tf1.getTranslation ();
  const vector3_t& center2 = tf2.getTranslation ();
  vector3_t dir1 = tf1.getRotation ().getColumn (2);
  vector3_t dir2 = tf2.getRotation ().getColumn (2);
  // Get closest point on axis
  value_type abs1 = dir1.dot (p1 - center1);
  value_type abs2 = dir2.dot (p2 - center2);
  vector3_t s1, s2;
  if (abs1 >= .5*l1) {
    s1 = center1 + (.5*l1)*dir1;
  } else if (abs1 <= -.5*l1) {
    s1 = center1 - (.5*l1)*dir1;
  } else {
    s1 = center1 + abs1*dir1;
  }
  if (abs2 >= .5*l2) {
    s2 = center2 + (.5*l2)*dir2;
  } else if (abs2 <= -.5*l2) {
    s2 = center2 - (.5*l2)*dir2;
  } else {
    s2 = center2 + abs2*dir2;
  }
  // Test orthogonality
  if (abs1 >= .5*l1) {
    BOOST_CHECK (dir1.dot (s2-s1) >= 0);
  } else if (abs1 <= -.5*l1) {
    BOOST_CHECK (dir1.dot (s2-s1) <= 0);
  } else {
    CHECK_CLOSE_TO_0 (dir1.dot (s2-s1), 1e-4);
  }
  if (abs2 >= .5*l2) {
    BOOST_CHECK (dir2.dot (s1-s2) >= 0);
  } else if (abs2 <= -.5*l2) {
    BOOST_CHECK (dir2.dot (s1-s2) <= 0);
  } else {
    CHECK_CLOSE_TO_0 (dir2.dot (s1-s2), 1e-4);
  }
  // Test distance
  BOOST_CHECK_CLOSE (d, (p2-p1).length (), 1e-4);
}

Florent Lamiraux's avatar
Florent Lamiraux committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
BOOST_AUTO_TEST_SUITE( test_suite )

BOOST_AUTO_TEST_CASE (interbody_distances_capsule)
{
  using hpp::model::Device;
  using hpp::model::Joint;
  using hpp::model::Body;
  typedef std::vector<double> vector_t;
  std::ifstream fileConfig;
  std::ofstream fileHrp2;
  vector_t::size_type configSize;
  fileConfig.open ("@PROJECT_SOURCE_DIR@/data/configuration.data");
  vector_t dofs;
  bool eof = false;
  double value;

  // Read configuration file
  while (!eof) {
    fileConfig >> value;
    if (fileConfig.fail ()) {
      eof = true;
    } else {
      dofs.push_back (value);
    }
  }
  fileConfig.close ();
  // Load hrp2
205
  hpp::model::HumanoidRobotPtr_t humanoidRobot =
206
207
208
209
    hpp::model::HumanoidRobot::create ("hrp2_14");
  hpp::model::urdf::loadHumanoidModel(humanoidRobot, "freeflyer",
				      "hrp2_14_description", "hrp2_14",
				      "_capsule", "_capsule");
210
  configSize = humanoidRobot->configSize ();
Florent Lamiraux's avatar
Florent Lamiraux committed
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
  // Check size of config
  if (dofs.size () != configSize) {
    std::ostringstream error;
    error << "wrong configuration size: " << dofs.size ()
	  << ", expecting " << configSize;
    throw std::runtime_error (error.str ());
  }
  hpp::model::Configuration_t config (configSize);
  for (vector_t::size_type i=0; i<configSize; ++i) {
    config [i] = dofs [i];
  }
  humanoidRobot->currentConfiguration (config);
  humanoidRobot->computeForwardKinematics ();
  hppDout (info, *humanoidRobot);
  humanoidRobot->computeDistances ();
  const hpp::model::DistanceResults_t& distances
    (humanoidRobot->distanceResults ());
  hppDout (info, "Number of distance computations: " << distances.size ());

  InterDistances_t interDistances = readDataFile ();
  for (hpp::model::DistanceResults_t::const_iterator it = distances.begin ();
       it != distances.end (); it++) {
    std::string body1 (it->innerObject->name ());
    std::string body2 (it->outerObject->name ());
    Distance_t distance (it->fcl.min_distance,
			 it->fcl.nearest_points [0][0],
			 it->fcl.nearest_points [0][1],
			 it->fcl.nearest_points [0][2],
			 it->fcl.nearest_points [1][0],
			 it->fcl.nearest_points [1][1],
			 it->fcl.nearest_points [1][2]);
242

Florent Lamiraux's avatar
Florent Lamiraux committed
243
244
245
246
247
248
249
250
251
252
    hppDout (info, it->fcl.min_distance << ",\t"
	     << it->innerObject->name () << ",\t"
	     << it->outerObject->name () << ",\t"
	     << it->fcl.nearest_points [0][0] << ",\t"
	     << it->fcl.nearest_points [0][1] << ",\t"
	     << it->fcl.nearest_points [0][2] << ",\t"
	     << it->fcl.nearest_points [1][0] << ",\t"
	     << it->fcl.nearest_points [1][1] << ",\t"
	     << it->fcl.nearest_points [1][2] << ",\t");
    std::pair <std::string, std::string> key (body1, body2);
253
    InterDistances_t::iterator itMap = interDistances.find (key);
Florent Lamiraux's avatar
Florent Lamiraux committed
254
    // Check that pair of bodies is in map
255
    if (itMap == interDistances.end ()) {
Florent Lamiraux's avatar
Florent Lamiraux committed
256
257
258
      throw std::runtime_error ("pair (" + body1 + "," + body2 +
				") is not in " + filename);
    }
259
260
    Distance_t distanceFile = itMap->second;
    std::cerr << "Checking pair " << body1 << ", " << body2 << std::endl;
Florent Lamiraux's avatar
Florent Lamiraux committed
261
    distance.checkClose (distanceFile);
262
    checkOptimalityConditions (*it);
Florent Lamiraux's avatar
Florent Lamiraux committed
263
264
265
266
  }
}

BOOST_AUTO_TEST_SUITE_END()