collision_data.h 16.6 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
Guilhem Saurel's avatar
Guilhem Saurel committed
59
struct HPP_FCL_DLLAPI Contact
sachinc's avatar
sachinc committed
60
{
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
141
struct QueryResult;

/// @brief base class for all query requests
Guilhem Saurel's avatar
Guilhem Saurel committed
142
struct HPP_FCL_DLLAPI QueryRequest
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
{
  /// @brief whether enable gjk intial guess
  bool enable_cached_gjk_guess;
  
  /// @brief the gjk intial guess set by user
  Vec3f cached_gjk_guess;

  /// @brief the support function intial guess set by user
  support_func_guess_t cached_support_func_guess;

  QueryRequest () :
    enable_cached_gjk_guess (false),
    cached_gjk_guess (1,0,0),
    cached_support_func_guess(support_func_guess_t::Zero())
  {}

  void updateGuess(const QueryResult& result);
160
161
162
163
164
165
166
167

  /// @brief whether two QueryRequest are the same or not
  inline bool operator ==(const QueryRequest& other) const
  {
    return enable_cached_gjk_guess == other.enable_cached_gjk_guess
      && cached_gjk_guess == other.cached_gjk_guess
      && cached_support_func_guess == other.cached_support_func_guess;
  }
168
169
170
};

/// @brief base class for all query results
Guilhem Saurel's avatar
Guilhem Saurel committed
171
struct HPP_FCL_DLLAPI QueryResult
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
{
  /// @brief stores the last GJK ray when relevant.
  Vec3f cached_gjk_guess;

  /// @brief stores the last support function vertex index, when relevant.
  support_func_guess_t cached_support_func_guess;
};

inline void QueryRequest::updateGuess(const QueryResult& result)
{
  if (enable_cached_gjk_guess) {
    cached_gjk_guess = result.cached_gjk_guess;
    cached_support_func_guess = result.cached_support_func_guess;
  }
}

188
189
struct CollisionResult;

190
191
192
193
194
195
196
197
/// @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
198
/// @brief request to the collision algorithm
Guilhem Saurel's avatar
Guilhem Saurel committed
199
struct HPP_FCL_DLLAPI CollisionRequest : QueryRequest
200
{  
jpan's avatar
jpan committed
201
  /// @brief The maximum number of contacts will return
jpan's avatar
jpan committed
202
  size_t num_max_contacts;
jpan's avatar
jpan committed
203
204

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

207
208
209
  /// Whether a lower bound on distance is returned when objects are disjoint
  bool enable_distance_lower_bound;

210
211
  /// @brief Distance below which objects are considered in collision.
  /// See \ref hpp_fcl_collision_and_distance_lower_bound_computation
212
213
  FCL_REAL security_margin;

214
215
  /// @brief Distance below which bounding volumes are broken down.
  /// See \ref hpp_fcl_collision_and_distance_lower_bound_computation
216
217
  FCL_REAL break_distance;

218
  explicit CollisionRequest(size_t num_max_contacts_,
219
                   bool enable_contact_ = false,
220
		   bool enable_distance_lower_bound_ = false,
jpan's avatar
jpan committed
221
                   size_t num_max_cost_sources_ = 1,
jpan's avatar
   
jpan committed
222
                   bool enable_cost_ = false,
223
                   bool use_approximate_cost_ = true)
224
225
  HPP_FCL_DEPRECATED;

226
  explicit CollisionRequest(const CollisionRequestFlag flag, size_t num_max_contacts_) :
227
    num_max_contacts(num_max_contacts_),
228
229
    enable_contact(flag & CONTACT),
    enable_distance_lower_bound (flag & DISTANCE_LOWER_BOUND),
230
231
    security_margin (0),
    break_distance (1e-3)
232
233
234
  {
  }

235
236
237
238
239
240
241
242
243
  CollisionRequest() :
      num_max_contacts(1),
      enable_contact(false),
      enable_distance_lower_bound (false),
      security_margin (0),
      break_distance (1e-3)
    {
    }

244
  bool isSatisfied(const CollisionResult& result) const;
245
246
247
248
249
250
251
252
253
254
255

  /// @brief whether two CollisionRequest are the same or not
  inline bool operator ==(const CollisionRequest& other) const
  {
    return QueryRequest::operator==(other)
      && num_max_contacts == other.num_max_contacts
      && enable_contact == other.enable_contact
      && enable_distance_lower_bound == other.enable_distance_lower_bound
      && security_margin == other.security_margin
      && break_distance == other.break_distance;
  }
256
257
};

jpan's avatar
jpan committed
258
/// @brief collision result
Guilhem Saurel's avatar
Guilhem Saurel committed
259
struct HPP_FCL_DLLAPI CollisionResult : QueryResult
260
{
261
private:
jpan's avatar
jpan committed
262
  /// @brief contact information
263
  std::vector<Contact> contacts;
264

265
public:
266
267
268
269
  /// 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).
270
271
  FCL_REAL distance_lower_bound;

272
public:
jpan's avatar
   
jpan committed
273
  CollisionResult()
274
    : distance_lower_bound ((std::numeric_limits<FCL_REAL>::max)())
jpan's avatar
   
jpan committed
275
  {
276
277
  }

278
279
280
281
282
283
  /// @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
284

jpan's avatar
jpan committed
285
  /// @brief add one contact into result structure
jpan's avatar
   
jpan committed
286
287
288
289
290
  inline void addContact(const Contact& c) 
  {
    contacts.push_back(c);
  }

291
292
293
294
295
296
297
  /// @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
298
  /// @brief return binary collision result
jpan's avatar
   
jpan committed
299
300
301
302
303
  bool isCollision() const
  {
    return contacts.size() > 0;
  }

jpan's avatar
jpan committed
304
  /// @brief number of contacts found
jpan's avatar
   
jpan committed
305
306
307
308
  size_t numContacts() const
  {
    return contacts.size();
  }
309

jpan's avatar
jpan committed
310
  /// @brief get the i-th contact calculated
311
312
  const Contact& getContact(size_t i) const
  {
313
314
315
    if(contacts.size() == 0)
      throw std::invalid_argument("The number of contacts is zero. No Contact can be returned.");
    
316
317
318
319
320
321
    if(i < contacts.size()) 
      return contacts[i];
    else
      return contacts.back();
  }

jpan's avatar
jpan committed
322
  /// @brief get all the contacts
Joseph Mirabel's avatar
Joseph Mirabel committed
323
  void getContacts(std::vector<Contact>& contacts_) const
324
325
326
327
328
  {
    contacts_.resize(contacts.size());
    std::copy(contacts.begin(), contacts.end(), contacts_.begin());
  }

jpan's avatar
jpan committed
329
  /// @brief clear the results obtained
330
331
332
333
  void clear()
  {
    contacts.clear();
  }
334
335
336
337

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

340
struct DistanceResult;
sachinc's avatar
sachinc committed
341

jpan's avatar
jpan committed
342
/// @brief request to the distance computation
Guilhem Saurel's avatar
Guilhem Saurel committed
343
struct HPP_FCL_DLLAPI DistanceRequest : QueryRequest
344
{
jpan's avatar
jpan committed
345
  /// @brief whether to return the nearest points
346
  bool enable_nearest_points;
347

348
349
350
351
  /// @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
352
  /// \deprecated the last argument should be removed.
Joseph Mirabel's avatar
Joseph Mirabel committed
353
354
355
356
357
358
359
360
361
362
  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_)
  {
  }

363
364
365
  /// \param enable_nearest_points_ enables the nearest points computation.
  /// \param rel_err_
  /// \param abs_err_
366
367
  DistanceRequest(bool enable_nearest_points_ = false,
                  FCL_REAL rel_err_ = 0.0,
368
369
370
371
                  FCL_REAL abs_err_ = 0.0) :
    enable_nearest_points(enable_nearest_points_),
    rel_err(rel_err_),
    abs_err(abs_err_)
372
373
  {
  }
374
375

  bool isSatisfied(const DistanceResult& result) const;
376
377
378
379
380
381
382
383
384

  /// @brief whether two DistanceRequest are the same or not
  inline bool operator ==(const DistanceRequest& other) const
  {
    return QueryRequest::operator==(other)
      && enable_nearest_points == other.enable_nearest_points
      && rel_err == other.rel_err
      && abs_err == other.abs_err;
  }
385
386
};

jpan's avatar
jpan committed
387
/// @brief distance result
Guilhem Saurel's avatar
Guilhem Saurel committed
388
struct HPP_FCL_DLLAPI DistanceResult : QueryResult
389
{
390
391

public:
392

393
  /// @brief minimum distance between two objects. if two objects are in collision, min_distance <= 0.
394
395
  FCL_REAL min_distance;

jpan's avatar
jpan committed
396
  /// @brief nearest points
397
  Vec3f nearest_points[2];
398

399
400
401
  /// In case both objects are in collision, store the normal
  Vec3f normal;

jpan's avatar
jpan committed
402
  /// @brief collision object 1
403
  const CollisionGeometry* o1;
jpan's avatar
jpan committed
404
405

  /// @brief collision object 2
406
  const CollisionGeometry* o2;
jpan's avatar
jpan committed
407
408
409
410
411

  /// @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
412
  int b1;
jpan's avatar
jpan committed
413
414
415
416
417

  /// @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
418
419
  int b2;

jpan's avatar
jpan committed
420
  /// @brief invalid contact primitive information
421
  static const int NONE = -1;
422
  
Florent Lamiraux's avatar
Florent Lamiraux committed
423
  DistanceResult(FCL_REAL min_distance_ =
424
                 (std::numeric_limits<FCL_REAL>::max)()):
Florent Lamiraux's avatar
Florent Lamiraux committed
425
  min_distance(min_distance_), o1(NULL), o2(NULL), b1(NONE), b2(NONE)
426
  {
427
    Vec3f nan (Vec3f::Constant(std::numeric_limits<FCL_REAL>::quiet_NaN()));
Florent Lamiraux's avatar
Florent Lamiraux committed
428
    nearest_points [0] = nearest_points [1] = normal = nan;
429
430
  }

431

jpan's avatar
jpan committed
432
  /// @brief add distance information into the result
433
434
435
436
437
438
439
440
441
442
443
444
  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
445
  /// @brief add distance information into the result
446
447
448
  void update(FCL_REAL distance, const CollisionGeometry* o1_,
              const CollisionGeometry* o2_, int b1_, int b2_,
              const Vec3f& p1, const Vec3f& p2, const Vec3f& normal_)
449
450
451
452
453
454
455
456
457
458
  {
    if(min_distance > distance)
    {
      min_distance = distance;
      o1 = o1_;
      o2 = o2_;
      b1 = b1_;
      b2 = b2_;
      nearest_points[0] = p1;
      nearest_points[1] = p2;
459
      normal = normal_;
460
461
462
    }
  }

jpan's avatar
jpan committed
463
  /// @brief add distance information into the result
464
465
466
467
468
469
470
471
472
473
474
  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];
475
      normal = other_result.normal;
476
477
478
    }
  }

jpan's avatar
jpan committed
479
  /// @brief clear the result
480
481
  void clear()
  {
482
    min_distance = (std::numeric_limits<FCL_REAL>::max)();
483
484
    o1 = NULL;
    o2 = NULL;
jpan's avatar
jpan committed
485
486
    b1 = NONE;
    b2 = NONE;
487
  }
488
489
490
491

  /// @brief whether two DistanceResult are the same or not
  inline bool operator ==(const DistanceResult& other) const
  {
492
493
494
495
496
497
498
499
500
    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.
501
    if ((o1 != NULL) xor (other.o1 != NULL)) return false;
502
503
504
    is_same &= (o1 == other.o1);
//    else if (o1 != NULL and other.o1 != NULL) is_same &= *o1 == *other.o1;

505
    if ((o2 != NULL) xor (other.o2 != NULL)) return false;
506
507
508
509
    is_same &= (o2 == other.o2);
//    else if (o2 != NULL and other.o2 != NULL) is_same &= *o2 == *other.o2;
  
    return is_same;
510
  }
511

sachinc's avatar
sachinc committed
512
513
};

514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
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
535
536
}

537
538
} // namespace hpp

sachinc's avatar
sachinc committed
539
#endif