collision_data.h 14.7 KB
Newer Older
1
2
3
/*
 * Software License Agreement (BSD License)
 *
4
5
 *  Copyright (c) 2011-2014, Willow Garage, Inc.
 *  Copyright (c) 2014-2015, Open Source Robotics Foundation
6
7
8
9
10
11
12
13
14
15
16
17
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
18
 *   * Neither the name of Open Source Robotics Foundation nor the names of its
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 */

/** \author Jia Pan */


Joseph Mirabel's avatar
Joseph Mirabel committed
39
40
#ifndef HPP_FCL_COLLISION_DATA_H
#define HPP_FCL_COLLISION_DATA_H
sachinc's avatar
sachinc committed
41
42

#include <vector>
43
#include <set>
jpan's avatar
jpan committed
44
#include <limits>
sachinc's avatar
sachinc committed
45

Joseph Mirabel's avatar
Joseph Mirabel committed
46
47
48
49
#include <hpp/fcl/collision_object.h>
#include <hpp/fcl/config.hh>
#include <hpp/fcl/data_types.h>

sachinc's avatar
sachinc committed
50

51
52
namespace hpp
{
sachinc's avatar
sachinc committed
53
54
55
namespace fcl
{

Joseph Mirabel's avatar
Joseph Mirabel committed
56
57
const int GST_INDEP HPP_FCL_DEPRECATED = 0;

jpan's avatar
jpan committed
58
/// @brief Contact information returned by collision
sachinc's avatar
sachinc committed
59
60
struct Contact
{
jpan's avatar
jpan committed
61
  /// @brief collision object 1
62
  const CollisionGeometry* o1;
jpan's avatar
jpan committed
63
64

  /// @brief collision object 2
65
  const CollisionGeometry* o2;
jpan's avatar
jpan committed
66
67
68
69
70

  /// @brief contact primitive in object 1
  /// if object 1 is mesh or point cloud, it is the triangle or point id
  /// if object 1 is geometry shape, it is NONE (-1),
  /// if object 1 is octree, it is the id of the cell
sachinc's avatar
sachinc committed
71
  int b1;
jpan's avatar
jpan committed
72
73
74
75
76
77


  /// @brief contact primitive in object 2
  /// if object 2 is mesh or point cloud, it is the triangle or point id
  /// if object 2 is geometry shape, it is NONE (-1),
  /// if object 2 is octree, it is the id of the cell
sachinc's avatar
sachinc committed
78
  int b2;
jpan's avatar
jpan committed
79
80
81
82
83
84
85
86
87
88
89
90
 
  /// @brief contact normal, pointing from o1 to o2
  Vec3f normal;

  /// @brief contact position, in world space
  Vec3f pos;

  /// @brief penetration depth
  FCL_REAL penetration_depth;

 
  /// @brief invalid contact primitive information
jpan's avatar
   
jpan committed
91
  static const int NONE = -1;
sachinc's avatar
sachinc committed
92

jpan's avatar
jpan committed
93
94
95
96
97
  Contact() : o1(NULL),
              o2(NULL),
              b1(NONE),
              b2(NONE)
  {}
sachinc's avatar
sachinc committed
98

jpan's avatar
jpan committed
99
100
101
102
103
  Contact(const CollisionGeometry* o1_, const CollisionGeometry* o2_, int b1_, int b2_) : o1(o1_),
                                                                                          o2(o2_),
                                                                                          b1(b1_),
                                                                                          b2(b2_)
  {}
sachinc's avatar
sachinc committed
104

105
  Contact(const CollisionGeometry* o1_, const CollisionGeometry* o2_, int b1_, int b2_,
jpan's avatar
jpan committed
106
107
108
109
110
111
112
113
          const Vec3f& pos_, const Vec3f& normal_, FCL_REAL depth_) : o1(o1_),
                                                                      o2(o2_),
                                                                      b1(b1_),
                                                                      b2(b2_),
                                                                      normal(normal_),
                                                                      pos(pos_),
                                                                      penetration_depth(depth_)
  {}
jpan's avatar
   
jpan committed
114
115
116
117
118
119
120

  bool operator < (const Contact& other) const
  {
    if(b1 == other.b1)
      return b2 < other.b2;
    return b1 < other.b1;
  }
121
122
123
124
125
126
127
128
129
130
131

  bool operator == (const Contact& other) const
  {
    return o1 == other.o1
            && o2 == other.o2
            && b1 == other.b1
            && b2 == other.b2
            && normal == other.normal
            && pos == other.pos
            && penetration_depth == other.penetration_depth;
  }
132
133
134
135
136
  
  bool operator != (const Contact& other) const
  {
    return !(*this == other);
  }
sachinc's avatar
sachinc committed
137
138
};

139
140
struct CollisionResult;

141
142
143
144
145
146
147
148
/// @brief flag declaration for specifying required params in CollisionResult
enum CollisionRequestFlag
{
  CONTACT               = 0x00001,
  DISTANCE_LOWER_BOUND  = 0x00002,
  NO_REQUEST            = 0x01000
};

jpan's avatar
jpan committed
149
/// @brief request to the collision algorithm
150
struct CollisionRequest
151
{  
jpan's avatar
jpan committed
152
  /// @brief The maximum number of contacts will return
jpan's avatar
jpan committed
153
  size_t num_max_contacts;
jpan's avatar
jpan committed
154
155

  /// @brief whether the contact information (normal, penetration depth and contact position) will return
156
  bool enable_contact;
jpan's avatar
jpan committed
157

158
159
160
  /// Whether a lower bound on distance is returned when objects are disjoint
  bool enable_distance_lower_bound;

161
162
163
164
165
166
  /// @brief whether enable gjk intial guess
  bool enable_cached_gjk_guess;
  
  /// @brief the gjk intial guess set by user
  Vec3f cached_gjk_guess;

167
168
  /// @brief Distance below which objects are considered in collision.
  /// See \ref hpp_fcl_collision_and_distance_lower_bound_computation
169
170
  FCL_REAL security_margin;

171
172
  /// @brief Distance below which bounding volumes are broken down.
  /// See \ref hpp_fcl_collision_and_distance_lower_bound_computation
173
174
  FCL_REAL break_distance;

175
  explicit CollisionRequest(size_t num_max_contacts_,
176
                   bool enable_contact_ = false,
177
		   bool enable_distance_lower_bound_ = false,
jpan's avatar
jpan committed
178
                   size_t num_max_cost_sources_ = 1,
jpan's avatar
   
jpan committed
179
                   bool enable_cost_ = false,
180
                   bool use_approximate_cost_ = true)
181
182
  HPP_FCL_DEPRECATED;

183
  explicit CollisionRequest(const CollisionRequestFlag flag, size_t num_max_contacts_) :
184
    num_max_contacts(num_max_contacts_),
185
186
    enable_contact(flag & CONTACT),
    enable_distance_lower_bound (flag & DISTANCE_LOWER_BOUND),
187
188
    security_margin (0),
    break_distance (1e-3)
189
  {
190
191
    enable_cached_gjk_guess = false;
    cached_gjk_guess = Vec3f(1, 0, 0);
192
193
  }

194
195
196
197
198
199
200
201
202
203
204
  CollisionRequest() :
      num_max_contacts(1),
      enable_contact(false),
      enable_distance_lower_bound (false),
      security_margin (0),
      break_distance (1e-3)
    {
      enable_cached_gjk_guess = false;
      cached_gjk_guess = Vec3f(1, 0, 0);
    }

205
  bool isSatisfied(const CollisionResult& result) const;
206
207
};

jpan's avatar
jpan committed
208
/// @brief collision result
209
210
struct CollisionResult
{
211
private:
jpan's avatar
jpan committed
212
  /// @brief contact information
213
  std::vector<Contact> contacts;
214

215
216
217
public:
  Vec3f cached_gjk_guess;

218
219
220
221
  /// Lower bound on distance between objects if they are disjoint.
  /// See \ref hpp_fcl_collision_and_distance_lower_bound_computation
  /// @note computed only on request (or if it does not add any computational
  /// overhead).
222
223
  FCL_REAL distance_lower_bound;

224
public:
jpan's avatar
   
jpan committed
225
  CollisionResult()
226
    : distance_lower_bound (std::numeric_limits<FCL_REAL>::max())
jpan's avatar
   
jpan committed
227
  {
228
229
  }

230
231
232
233
234
235
  /// @brief Update the lower bound only if the distance in inferior.
  inline void updateDistanceLowerBound (const FCL_REAL& distance_lower_bound_)
  {
    if (distance_lower_bound_ < distance_lower_bound)
      distance_lower_bound = distance_lower_bound_;
  }
jpan's avatar
   
jpan committed
236

jpan's avatar
jpan committed
237
  /// @brief add one contact into result structure
jpan's avatar
   
jpan committed
238
239
240
241
242
  inline void addContact(const Contact& c) 
  {
    contacts.push_back(c);
  }

243
244
245
246
247
248
249
  /// @brief whether two CollisionResult are the same or not
  inline bool operator ==(const CollisionResult& other) const
  {
    return contacts == other.contacts 
            && distance_lower_bound == other.distance_lower_bound;
  }

jpan's avatar
jpan committed
250
  /// @brief return binary collision result
jpan's avatar
   
jpan committed
251
252
253
254
255
  bool isCollision() const
  {
    return contacts.size() > 0;
  }

jpan's avatar
jpan committed
256
  /// @brief number of contacts found
jpan's avatar
   
jpan committed
257
258
259
260
  size_t numContacts() const
  {
    return contacts.size();
  }
261

jpan's avatar
jpan committed
262
  /// @brief get the i-th contact calculated
263
264
  const Contact& getContact(size_t i) const
  {
265
266
267
    if(contacts.size() == 0)
      throw std::invalid_argument("The number of contacts is zero. No Contact can be returned.");
    
268
269
270
271
272
273
    if(i < contacts.size()) 
      return contacts[i];
    else
      return contacts.back();
  }

jpan's avatar
jpan committed
274
  /// @brief get all the contacts
Joseph Mirabel's avatar
Joseph Mirabel committed
275
  void getContacts(std::vector<Contact>& contacts_) const
276
277
278
279
280
  {
    contacts_.resize(contacts.size());
    std::copy(contacts.begin(), contacts.end(), contacts_.begin());
  }

jpan's avatar
jpan committed
281
  /// @brief clear the results obtained
282
283
284
285
  void clear()
  {
    contacts.clear();
  }
286
287
288
289

  /// @brief reposition Contact objects when fcl inverts them
  /// during their construction.
  friend void invertResults(CollisionResult& result);
290
291
};

292

293
struct DistanceResult;
sachinc's avatar
sachinc committed
294

jpan's avatar
jpan committed
295
/// @brief request to the distance computation
296
297
struct DistanceRequest
{
jpan's avatar
jpan committed
298
  /// @brief whether to return the nearest points
299
  bool enable_nearest_points;
300

301
302
303
304
  /// @brief error threshold for approximate distance
  FCL_REAL rel_err; // relative error, between 0 and 1
  FCL_REAL abs_err; // absoluate error

Joseph Mirabel's avatar
Joseph Mirabel committed
305
  /// \deprecated the last argument should be removed.
Joseph Mirabel's avatar
Joseph Mirabel committed
306
307
308
309
310
311
312
313
314
315
  DistanceRequest(bool enable_nearest_points_,
                  FCL_REAL rel_err_,
                  FCL_REAL abs_err_,
                  int /*unused*/) HPP_FCL_DEPRECATED :
    enable_nearest_points(enable_nearest_points_),
    rel_err(rel_err_),
    abs_err(abs_err_)
  {
  }

316
317
318
  /// \param enable_nearest_points_ enables the nearest points computation.
  /// \param rel_err_
  /// \param abs_err_
319
320
  DistanceRequest(bool enable_nearest_points_ = false,
                  FCL_REAL rel_err_ = 0.0,
321
322
323
324
                  FCL_REAL abs_err_ = 0.0) :
    enable_nearest_points(enable_nearest_points_),
    rel_err(rel_err_),
    abs_err(abs_err_)
325
326
  {
  }
327
328

  bool isSatisfied(const DistanceResult& result) const;
329
330
};

331

jpan's avatar
jpan committed
332
/// @brief distance result
333
334
struct DistanceResult
{
335
336

public:
337

338
  /// @brief minimum distance between two objects. if two objects are in collision, min_distance <= 0.
339
340
  FCL_REAL min_distance;

jpan's avatar
jpan committed
341
  /// @brief nearest points
342
  Vec3f nearest_points[2];
343

344
345
346
  /// In case both objects are in collision, store the normal
  Vec3f normal;

jpan's avatar
jpan committed
347
  /// @brief collision object 1
348
  const CollisionGeometry* o1;
jpan's avatar
jpan committed
349
350

  /// @brief collision object 2
351
  const CollisionGeometry* o2;
jpan's avatar
jpan committed
352
353
354
355
356

  /// @brief information about the nearest point in object 1
  /// if object 1 is mesh or point cloud, it is the triangle or point id
  /// if object 1 is geometry shape, it is NONE (-1),
  /// if object 1 is octree, it is the id of the cell
357
  int b1;
jpan's avatar
jpan committed
358
359
360
361
362

  /// @brief information about the nearest point in object 2
  /// if object 2 is mesh or point cloud, it is the triangle or point id
  /// if object 2 is geometry shape, it is NONE (-1),
  /// if object 2 is octree, it is the id of the cell
363
364
  int b2;

jpan's avatar
jpan committed
365
  /// @brief invalid contact primitive information
366
  static const int NONE = -1;
367
  
Florent Lamiraux's avatar
Florent Lamiraux committed
368
369
370
  DistanceResult(FCL_REAL min_distance_ =
                 std::numeric_limits<FCL_REAL>::max()):
  min_distance(min_distance_), o1(NULL), o2(NULL), b1(NONE), b2(NONE)
371
  {
372
    Vec3f nan (Vec3f::Constant(std::numeric_limits<FCL_REAL>::quiet_NaN()));
Florent Lamiraux's avatar
Florent Lamiraux committed
373
    nearest_points [0] = nearest_points [1] = normal = nan;
374
375
  }

376

jpan's avatar
jpan committed
377
  /// @brief add distance information into the result
378
379
380
381
382
383
384
385
386
387
388
389
  void update(FCL_REAL distance, const CollisionGeometry* o1_, const CollisionGeometry* o2_, int b1_, int b2_)
  {
    if(min_distance > distance)
    {
      min_distance = distance;
      o1 = o1_;
      o2 = o2_;
      b1 = b1_;
      b2 = b2_;
    }
  }

jpan's avatar
jpan committed
390
  /// @brief add distance information into the result
391
392
393
  void update(FCL_REAL distance, const CollisionGeometry* o1_,
              const CollisionGeometry* o2_, int b1_, int b2_,
              const Vec3f& p1, const Vec3f& p2, const Vec3f& normal_)
394
395
396
397
398
399
400
401
402
403
  {
    if(min_distance > distance)
    {
      min_distance = distance;
      o1 = o1_;
      o2 = o2_;
      b1 = b1_;
      b2 = b2_;
      nearest_points[0] = p1;
      nearest_points[1] = p2;
404
      normal = normal_;
405
406
407
    }
  }

jpan's avatar
jpan committed
408
  /// @brief add distance information into the result
409
410
411
412
413
414
415
416
417
418
419
  void update(const DistanceResult& other_result)
  {
    if(min_distance > other_result.min_distance)
    {
      min_distance = other_result.min_distance;
      o1 = other_result.o1;
      o2 = other_result.o2;
      b1 = other_result.b1;
      b2 = other_result.b2;
      nearest_points[0] = other_result.nearest_points[0];
      nearest_points[1] = other_result.nearest_points[1];
420
      normal = other_result.normal;
421
422
423
    }
  }

jpan's avatar
jpan committed
424
  /// @brief clear the result
425
426
427
  void clear()
  {
    min_distance = std::numeric_limits<FCL_REAL>::max();
428
429
    o1 = NULL;
    o2 = NULL;
jpan's avatar
jpan committed
430
431
    b1 = NONE;
    b2 = NONE;
432
  }
433
434
435
436

  /// @brief whether two DistanceResult are the same or not
  inline bool operator ==(const DistanceResult& other) const
  {
437
438
439
440
441
442
443
444
445
    bool is_same = min_distance == other.min_distance
                  && nearest_points[0] == other.nearest_points[0]
                  && nearest_points[1] == other.nearest_points[1]
                  && o1 == other.o1
                  && o2 == other.o2
                  && b1 == other.b1
                  && b2 == other.b2;

// TODO: check also that two GeometryObject are indeed equal.
446
    if ((o1 != NULL) xor (other.o1 != NULL)) return false;
447
448
449
    is_same &= (o1 == other.o1);
//    else if (o1 != NULL and other.o1 != NULL) is_same &= *o1 == *other.o1;

450
    if ((o2 != NULL) xor (other.o2 != NULL)) return false;
451
452
453
454
    is_same &= (o2 == other.o2);
//    else if (o2 != NULL and other.o2 != NULL) is_same &= *o2 == *other.o2;
  
    return is_same;
455
  }
456

sachinc's avatar
sachinc committed
457
458
};

459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480

inline CollisionRequestFlag operator~(CollisionRequestFlag a)
{return static_cast<CollisionRequestFlag>(~static_cast<const int>(a));}

inline CollisionRequestFlag operator|(CollisionRequestFlag a, CollisionRequestFlag b)
{return static_cast<CollisionRequestFlag>(static_cast<const int>(a) | static_cast<const int>(b));}

inline CollisionRequestFlag operator&(CollisionRequestFlag a, CollisionRequestFlag b)
{return static_cast<CollisionRequestFlag>(static_cast<const int>(a) & static_cast<const int>(b));}

inline CollisionRequestFlag operator^(CollisionRequestFlag a, CollisionRequestFlag b)
{return static_cast<CollisionRequestFlag>(static_cast<const int>(a) ^ static_cast<const int>(b));}

inline CollisionRequestFlag& operator|=(CollisionRequestFlag& a, CollisionRequestFlag b)
{return (CollisionRequestFlag&)((int&)(a) |= static_cast<const int>(b));}

inline CollisionRequestFlag& operator&=(CollisionRequestFlag& a, CollisionRequestFlag b)
{return (CollisionRequestFlag&)((int&)(a) &= static_cast<const int>(b));}

inline CollisionRequestFlag& operator^=(CollisionRequestFlag& a, CollisionRequestFlag b)
{return (CollisionRequestFlag&)((int&)(a) ^= static_cast<const int>(b));}

sachinc's avatar
sachinc committed
481
482
}

483
484
} // namespace hpp

sachinc's avatar
sachinc committed
485
#endif