hpp-fcl  1.7.0
HPP fork of FCL -- The Flexible Collision Library
collision_data.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011-2014, Willow Garage, Inc.
5  * Copyright (c) 2014-2015, Open Source Robotics Foundation
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Open Source Robotics Foundation nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
39 #ifndef HPP_FCL_COLLISION_DATA_H
40 #define HPP_FCL_COLLISION_DATA_H
41 
42 #include <vector>
43 #include <set>
44 #include <limits>
45 
47 #include <hpp/fcl/config.hh>
48 #include <hpp/fcl/data_types.h>
49 #include <hpp/fcl/timings.h>
50 
51 namespace hpp
52 {
53 namespace fcl
54 {
55 
58 {
61 
64 
69  int b1;
70 
75  int b2;
76 
79 
82 
85 
87  static const int NONE = -1;
88 
89  Contact() : o1(NULL),
90  o2(NULL),
91  b1(NONE),
92  b2(NONE)
93  {}
94 
95  Contact(const CollisionGeometry* o1_, const CollisionGeometry* o2_, int b1_, int b2_) : o1(o1_),
96  o2(o2_),
97  b1(b1_),
98  b2(b2_)
99  {}
100 
101  Contact(const CollisionGeometry* o1_, const CollisionGeometry* o2_, int b1_, int b2_,
102  const Vec3f& pos_, const Vec3f& normal_, FCL_REAL depth_) : o1(o1_),
103  o2(o2_),
104  b1(b1_),
105  b2(b2_),
106  normal(normal_),
107  pos(pos_),
108  penetration_depth(depth_)
109  {}
110 
111  bool operator < (const Contact& other) const
112  {
113  if(b1 == other.b1)
114  return b2 < other.b2;
115  return b1 < other.b1;
116  }
117 
118  bool operator == (const Contact& other) const
119  {
120  return o1 == other.o1
121  && o2 == other.o2
122  && b1 == other.b1
123  && b2 == other.b2
124  && normal == other.normal
125  && pos == other.pos
126  && penetration_depth == other.penetration_depth;
127  }
128 
129  bool operator != (const Contact& other) const
130  {
131  return !(*this == other);
132  }
133 };
134 
135 struct QueryResult;
136 
139 {
142 
145 
148 
150  enable_cached_gjk_guess (false),
151  cached_gjk_guess (1,0,0),
152  cached_support_func_guess(support_func_guess_t::Zero())
153  {}
154 
155  void updateGuess(const QueryResult& result);
156 
158  inline bool operator ==(const QueryRequest& other) const
159  {
160  return enable_cached_gjk_guess == other.enable_cached_gjk_guess
161  && cached_gjk_guess == other.cached_gjk_guess
162  && cached_support_func_guess == other.cached_support_func_guess;
163  }
164 };
165 
168 {
171 
174 
177 
179  : cached_gjk_guess(Vec3f::Zero())
180  , cached_support_func_guess(support_func_guess_t::Constant(-1))
181  {}
182 };
183 
184 inline void QueryRequest::updateGuess(const QueryResult& result)
185 {
186  if (enable_cached_gjk_guess) {
187  cached_gjk_guess = result.cached_gjk_guess;
188  cached_support_func_guess = result.cached_support_func_guess;
189  }
190 }
191 
192 struct CollisionResult;
193 
196 {
197  CONTACT = 0x00001,
199  NO_REQUEST = 0x01000
200 };
201 
204 {
207 
210 
213 
217 
221 
227 
228  explicit CollisionRequest(const CollisionRequestFlag flag, size_t num_max_contacts_) :
229  num_max_contacts(num_max_contacts_),
230  enable_contact(flag & CONTACT),
231  enable_distance_lower_bound (flag & DISTANCE_LOWER_BOUND),
232  security_margin (0),
233  break_distance (1e-3),
234  distance_upper_bound ((std::numeric_limits<FCL_REAL>::max)())
235  {
236  }
237 
239  num_max_contacts(1),
240  enable_contact(false),
241  enable_distance_lower_bound (false),
242  security_margin (0),
243  break_distance (1e-3),
244  distance_upper_bound ((std::numeric_limits<FCL_REAL>::max)())
245  {
246  }
247 
248  bool isSatisfied(const CollisionResult& result) const;
249 
251  inline bool operator ==(const CollisionRequest& other) const
252  {
253  return QueryRequest::operator==(other)
254  && num_max_contacts == other.num_max_contacts
255  && enable_contact == other.enable_contact
256  && enable_distance_lower_bound == other.enable_distance_lower_bound
257  && security_margin == other.security_margin
258  && break_distance == other.break_distance
259  && distance_upper_bound == other.distance_upper_bound;
260  }
261 };
262 
265 {
266 private:
268  std::vector<Contact> contacts;
269 
270 public:
276 
277 public:
279  : distance_lower_bound ((std::numeric_limits<FCL_REAL>::max)())
280  {
281  }
282 
284  inline void updateDistanceLowerBound (const FCL_REAL& distance_lower_bound_)
285  {
286  if (distance_lower_bound_ < distance_lower_bound)
287  distance_lower_bound = distance_lower_bound_;
288  }
289 
291  inline void addContact(const Contact& c)
292  {
293  contacts.push_back(c);
294  }
295 
297  inline bool operator ==(const CollisionResult& other) const
298  {
299  return contacts == other.contacts
300  && distance_lower_bound == other.distance_lower_bound;
301  }
302 
304  bool isCollision() const
305  {
306  return contacts.size() > 0;
307  }
308 
310  size_t numContacts() const
311  {
312  return contacts.size();
313  }
314 
316  const Contact& getContact(size_t i) const
317  {
318  if(contacts.size() == 0)
319  throw std::invalid_argument("The number of contacts is zero. No Contact can be returned.");
320 
321  if(i < contacts.size())
322  return contacts[i];
323  else
324  return contacts.back();
325  }
326 
328  void getContacts(std::vector<Contact>& contacts_) const
329  {
330  contacts_.resize(contacts.size());
331  std::copy(contacts.begin(), contacts.end(), contacts_.begin());
332  }
333 
334  const std::vector<Contact> & getContacts() const
335  {
336  return contacts;
337  }
338 
340  void clear()
341  {
342  distance_lower_bound = (std::numeric_limits<FCL_REAL>::max)();
343  contacts.clear();
344  distance_lower_bound = (std::numeric_limits<FCL_REAL>::max)();
345  timings.clear();
346  }
347 
350  void swapObjects();
351 };
352 
353 struct DistanceResult;
354 
357 {
360 
362  FCL_REAL rel_err; // relative error, between 0 and 1
363  FCL_REAL abs_err; // absoluate error
364 
368  DistanceRequest(bool enable_nearest_points_ = false,
369  FCL_REAL rel_err_ = 0.0,
370  FCL_REAL abs_err_ = 0.0) :
371  enable_nearest_points(enable_nearest_points_),
372  rel_err(rel_err_),
373  abs_err(abs_err_)
374  {
375  }
376 
377  bool isSatisfied(const DistanceResult& result) const;
378 
380  inline bool operator ==(const DistanceRequest& other) const
381  {
382  return QueryRequest::operator==(other)
383  && enable_nearest_points == other.enable_nearest_points
384  && rel_err == other.rel_err
385  && abs_err == other.abs_err;
386  }
387 };
388 
391 {
392 
393 public:
394 
397 
399  Vec3f nearest_points[2];
400 
403 
406 
409 
414  int b1;
415 
420  int b2;
421 
423  static const int NONE = -1;
424 
425  DistanceResult(FCL_REAL min_distance_ =
426  (std::numeric_limits<FCL_REAL>::max)()):
427  min_distance(min_distance_), o1(NULL), o2(NULL), b1(NONE), b2(NONE)
428  {
429  const Vec3f nan (Vec3f::Constant(std::numeric_limits<FCL_REAL>::quiet_NaN()));
430  nearest_points [0] = nearest_points [1] = normal = nan;
431  }
432 
433 
435  void update(FCL_REAL distance, const CollisionGeometry* o1_, const CollisionGeometry* o2_, int b1_, int b2_)
436  {
437  if(min_distance > distance)
438  {
439  min_distance = distance;
440  o1 = o1_;
441  o2 = o2_;
442  b1 = b1_;
443  b2 = b2_;
444  }
445  }
446 
449  const CollisionGeometry* o2_, int b1_, int b2_,
450  const Vec3f& p1, const Vec3f& p2, const Vec3f& normal_)
451  {
452  if(min_distance > distance)
453  {
454  min_distance = distance;
455  o1 = o1_;
456  o2 = o2_;
457  b1 = b1_;
458  b2 = b2_;
459  nearest_points[0] = p1;
460  nearest_points[1] = p2;
461  normal = normal_;
462  }
463  }
464 
466  void update(const DistanceResult& other_result)
467  {
468  if(min_distance > other_result.min_distance)
469  {
470  min_distance = other_result.min_distance;
471  o1 = other_result.o1;
472  o2 = other_result.o2;
473  b1 = other_result.b1;
474  b2 = other_result.b2;
475  nearest_points[0] = other_result.nearest_points[0];
476  nearest_points[1] = other_result.nearest_points[1];
477  normal = other_result.normal;
478  }
479  }
480 
482  void clear()
483  {
484  const Vec3f nan (Vec3f::Constant(std::numeric_limits<FCL_REAL>::quiet_NaN()));
485  min_distance = (std::numeric_limits<FCL_REAL>::max)();
486  o1 = NULL;
487  o2 = NULL;
488  b1 = NONE;
489  b2 = NONE;
490  nearest_points [0] = nearest_points [1] = normal = nan;
491  timings.clear();
492  }
493 
495  inline bool operator ==(const DistanceResult& other) const
496  {
497  bool is_same = min_distance == other.min_distance
498  && nearest_points[0] == other.nearest_points[0]
499  && nearest_points[1] == other.nearest_points[1]
500  && normal == other.normal
501  && o1 == other.o1
502  && o2 == other.o2
503  && b1 == other.b1
504  && b2 == other.b2;
505 
506 // TODO: check also that two GeometryObject are indeed equal.
507  if ((o1 != NULL) ^ (other.o1 != NULL)) return false;
508  is_same &= (o1 == other.o1);
509 // else if (o1 != NULL and other.o1 != NULL) is_same &= *o1 == *other.o1;
510 
511  if ((o2 != NULL) ^ (other.o2 != NULL)) return false;
512  is_same &= (o2 == other.o2);
513 // else if (o2 != NULL and other.o2 != NULL) is_same &= *o2 == *other.o2;
514 
515  return is_same;
516  }
517 
518 };
519 
521 {return static_cast<CollisionRequestFlag>(~static_cast<const int>(a));}
522 
524 {return static_cast<CollisionRequestFlag>(static_cast<const int>(a) | static_cast<const int>(b));}
525 
527 {return static_cast<CollisionRequestFlag>(static_cast<const int>(a) & static_cast<const int>(b));}
528 
530 {return static_cast<CollisionRequestFlag>(static_cast<const int>(a) ^ static_cast<const int>(b));}
531 
533 {return (CollisionRequestFlag&)((int&)(a) |= static_cast<const int>(b));}
534 
536 {return (CollisionRequestFlag&)((int&)(a) &= static_cast<const int>(b));}
537 
539 {return (CollisionRequestFlag&)((int&)(a) ^= static_cast<const int>(b));}
540 
541 }
542 
543 } // namespace hpp
544 
545 #endif
Contact(const CollisionGeometry *o1_, const CollisionGeometry *o2_, int b1_, int b2_)
Definition: collision_data.h:95
support_func_guess_t cached_support_func_guess
the support function intial guess set by user
Definition: collision_data.h:147
size_t num_max_contacts
The maximum number of contacts will return.
Definition: collision_data.h:206
CollisionRequestFlag operator|(CollisionRequestFlag a, CollisionRequestFlag b)
Definition: collision_data.h:523
request to the distance computation
Definition: collision_data.h:356
bool operator==(const QueryRequest &other) const
whether two QueryRequest are the same or not
Definition: collision_data.h:158
void update(const DistanceResult &other_result)
add distance information into the result
Definition: collision_data.h:466
support_func_guess_t cached_support_func_guess
stores the last support function vertex index, when relevant.
Definition: collision_data.h:173
const CollisionGeometry * o1
collision object 1
Definition: collision_data.h:405
base class for all query results
Definition: collision_data.h:167
FCL_REAL distance_upper_bound
Distance above which GJK solver makes an early stopping. GJK stops searching for the closest points w...
Definition: collision_data.h:226
Main namespace.
Definition: AABB.h:43
collision result
Definition: collision_data.h:264
int b2
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
Definition: collision_data.h:420
Vec3f nearest_points[2]
nearest points
Definition: collision_data.h:399
bool enable_cached_gjk_guess
whether enable gjk intial guess
Definition: collision_data.h:141
void updateDistanceLowerBound(const FCL_REAL &distance_lower_bound_)
Update the lower bound only if the distance in inferior.
Definition: collision_data.h:284
CollisionRequestFlag & operator|=(CollisionRequestFlag &a, CollisionRequestFlag b)
Definition: collision_data.h:532
QueryRequest()
Definition: collision_data.h:149
size_t numContacts() const
number of contacts found
Definition: collision_data.h:310
void addContact(const Contact &c)
add one contact into result structure
Definition: collision_data.h:291
FCL_REAL distance_lower_bound
Definition: collision_data.h:275
Definition: collision_data.h:197
void clear()
clear the result
Definition: collision_data.h:482
const CollisionGeometry * o1
collision object 1
Definition: collision_data.h:60
CollisionRequestFlag & operator &=(CollisionRequestFlag &a, CollisionRequestFlag b)
Definition: collision_data.h:535
CollisionRequestFlag
flag declaration for specifying required params in CollisionResult
Definition: collision_data.h:195
CollisionRequestFlag operator^(CollisionRequestFlag a, CollisionRequestFlag b)
Definition: collision_data.h:529
request to the collision algorithm
Definition: collision_data.h:203
void clear()
clear the results obtained
Definition: collision_data.h:340
FCL_REAL distance(const Matrix3f &R0, const Vec3f &T0, const kIOS &b1, const kIOS &b2, Vec3f *P=NULL, Vec3f *Q=NULL)
Approximate distance between two kIOS bounding volumes.
FCL_REAL penetration_depth
penetration depth
Definition: collision_data.h:84
double FCL_REAL
Definition: data_types.h:66
void updateGuess(const QueryResult &result)
Definition: collision_data.h:184
CollisionRequestFlag & operator^=(CollisionRequestFlag &a, CollisionRequestFlag b)
Definition: collision_data.h:538
Definition: timings.h:14
FCL_REAL min_distance
minimum distance between two objects. if two objects are in collision, min_distance <= 0...
Definition: collision_data.h:396
int b1
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
Definition: collision_data.h:414
Vec3f normal
In case both objects are in collision, store the normal.
Definition: collision_data.h:402
void getContacts(std::vector< Contact > &contacts_) const
get all the contacts
Definition: collision_data.h:328
Vec3f cached_gjk_guess
the gjk intial guess set by user
Definition: collision_data.h:144
Contact(const CollisionGeometry *o1_, const CollisionGeometry *o2_, int b1_, int b2_, const Vec3f &pos_, const Vec3f &normal_, FCL_REAL depth_)
Definition: collision_data.h:101
Eigen::Vector2i support_func_guess_t
Definition: data_types.h:71
Vec3f pos
contact position, in world space
Definition: collision_data.h:81
CPUTimes timings
timings for the given request
Definition: collision_data.h:176
DistanceResult(FCL_REAL min_distance_=(std::numeric_limits< FCL_REAL >::max)())
Definition: collision_data.h:425
FCL_REAL break_distance
Distance below which bounding volumes are broken down. See Collision detection and distance lower bou...
Definition: collision_data.h:220
FCL_REAL rel_err
error threshold for approximate distance
Definition: collision_data.h:362
CollisionRequest()
Definition: collision_data.h:238
bool enable_nearest_points
whether to return the nearest points
Definition: collision_data.h:359
bool enable_distance_lower_bound
Whether a lower bound on distance is returned when objects are disjoint.
Definition: collision_data.h:212
QueryResult()
Definition: collision_data.h:178
int b1
contact primitive in object 1 if object 1 is mesh or point cloud, it is the triangle or point id if o...
Definition: collision_data.h:69
CollisionResult()
Definition: collision_data.h:278
const Contact & getContact(size_t i) const
get the i-th contact calculated
Definition: collision_data.h:316
CollisionRequestFlag operator &(CollisionRequestFlag a, CollisionRequestFlag b)
Definition: collision_data.h:526
Definition: collision_data.h:198
bool enable_contact
whether the contact information (normal, penetration depth and contact position) will return ...
Definition: collision_data.h:209
Vec3f cached_gjk_guess
stores the last GJK ray when relevant.
Definition: collision_data.h:170
bool isCollision() const
return binary collision result
Definition: collision_data.h:304
base class for all query requests
Definition: collision_data.h:138
CollisionRequestFlag operator~(CollisionRequestFlag a)
Definition: collision_data.h:520
Definition: collision_data.h:199
Eigen::Matrix< FCL_REAL, 3, 1 > Vec3f
Definition: data_types.h:67
const CollisionGeometry * o2
collision object 2
Definition: collision_data.h:63
void update(FCL_REAL distance, const CollisionGeometry *o1_, const CollisionGeometry *o2_, int b1_, int b2_)
add distance information into the result
Definition: collision_data.h:435
Contact information returned by collision.
Definition: collision_data.h:57
void update(FCL_REAL distance, const CollisionGeometry *o1_, const CollisionGeometry *o2_, int b1_, int b2_, const Vec3f &p1, const Vec3f &p2, const Vec3f &normal_)
add distance information into the result
Definition: collision_data.h:448
int b2
contact primitive in object 2 if object 2 is mesh or point cloud, it is the triangle or point id if o...
Definition: collision_data.h:75
The geometry for the object for collision or distance computation.
Definition: collision_object.h:65
FCL_REAL abs_err
Definition: collision_data.h:363
const std::vector< Contact > & getContacts() const
Definition: collision_data.h:334
Vec3f normal
contact normal, pointing from o1 to o2
Definition: collision_data.h:78
const CollisionGeometry * o2
collision object 2
Definition: collision_data.h:408
DistanceRequest(bool enable_nearest_points_=false, FCL_REAL rel_err_=0.0, FCL_REAL abs_err_=0.0)
Definition: collision_data.h:368
#define HPP_FCL_DLLAPI
Definition: config.hh:64
CollisionRequest(const CollisionRequestFlag flag, size_t num_max_contacts_)
Definition: collision_data.h:228
distance result
Definition: collision_data.h:390
FCL_REAL security_margin
Distance below which objects are considered in collision. See Collision detection and distance lower ...
Definition: collision_data.h:216
Contact()
Definition: collision_data.h:89