hpp-fcl  1.4.4
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 
50 
51 namespace hpp
52 {
53 namespace fcl
54 {
55 
56 const int GST_INDEP HPP_FCL_DEPRECATED = 0;
57 
59 struct Contact
60 {
63 
66 
71  int b1;
72 
73 
78  int b2;
79 
82 
85 
88 
89 
91  static const int NONE = -1;
92 
93  Contact() : o1(NULL),
94  o2(NULL),
95  b1(NONE),
96  b2(NONE)
97  {}
98 
99  Contact(const CollisionGeometry* o1_, const CollisionGeometry* o2_, int b1_, int b2_) : o1(o1_),
100  o2(o2_),
101  b1(b1_),
102  b2(b2_)
103  {}
104 
105  Contact(const CollisionGeometry* o1_, const CollisionGeometry* o2_, int b1_, int b2_,
106  const Vec3f& pos_, const Vec3f& normal_, FCL_REAL depth_) : o1(o1_),
107  o2(o2_),
108  b1(b1_),
109  b2(b2_),
110  normal(normal_),
111  pos(pos_),
112  penetration_depth(depth_)
113  {}
114 
115  bool operator < (const Contact& other) const
116  {
117  if(b1 == other.b1)
118  return b2 < other.b2;
119  return b1 < other.b1;
120  }
121 
122  bool operator == (const Contact& other) const
123  {
124  return o1 == other.o1
125  && o2 == other.o2
126  && b1 == other.b1
127  && b2 == other.b2
128  && normal == other.normal
129  && pos == other.pos
130  && penetration_depth == other.penetration_depth;
131  }
132 
133  bool operator != (const Contact& other) const
134  {
135  return !(*this == other);
136  }
137 };
138 
139 struct CollisionResult;
140 
143 {
144  CONTACT = 0x00001,
146  NO_REQUEST = 0x01000
147 };
148 
151 {
154 
157 
160 
163 
166 
170 
174 
175  explicit CollisionRequest(size_t num_max_contacts_,
176  bool enable_contact_ = false,
177  bool enable_distance_lower_bound_ = false,
178  size_t num_max_cost_sources_ = 1,
179  bool enable_cost_ = false,
180  bool use_approximate_cost_ = true)
181  HPP_FCL_DEPRECATED;
182 
183  explicit CollisionRequest(const CollisionRequestFlag flag, size_t num_max_contacts_) :
184  num_max_contacts(num_max_contacts_),
185  enable_contact(flag & CONTACT),
186  enable_distance_lower_bound (flag & DISTANCE_LOWER_BOUND),
187  security_margin (0),
188  break_distance (1e-3)
189  {
190  enable_cached_gjk_guess = false;
191  cached_gjk_guess = Vec3f(1, 0, 0);
192  }
193 
195  num_max_contacts(1),
196  enable_contact(false),
197  enable_distance_lower_bound (false),
198  security_margin (0),
199  break_distance (1e-3)
200  {
201  enable_cached_gjk_guess = false;
202  cached_gjk_guess = Vec3f(1, 0, 0);
203  }
204 
205  bool isSatisfied(const CollisionResult& result) const;
206 };
207 
210 {
211 private:
213  std::vector<Contact> contacts;
214 
215 public:
217 
223 
224 public:
226  : distance_lower_bound (std::numeric_limits<FCL_REAL>::max())
227  {
228  }
229 
231  inline void updateDistanceLowerBound (const FCL_REAL& distance_lower_bound_)
232  {
233  if (distance_lower_bound_ < distance_lower_bound)
234  distance_lower_bound = distance_lower_bound_;
235  }
236 
238  inline void addContact(const Contact& c)
239  {
240  contacts.push_back(c);
241  }
242 
244  inline bool operator ==(const CollisionResult& other) const
245  {
246  return contacts == other.contacts
247  && distance_lower_bound == other.distance_lower_bound;
248  }
249 
251  bool isCollision() const
252  {
253  return contacts.size() > 0;
254  }
255 
257  size_t numContacts() const
258  {
259  return contacts.size();
260  }
261 
263  const Contact& getContact(size_t i) const
264  {
265  if(contacts.size() == 0)
266  throw std::invalid_argument("The number of contacts is zero. No Contact can be returned.");
267 
268  if(i < contacts.size())
269  return contacts[i];
270  else
271  return contacts.back();
272  }
273 
275  void getContacts(std::vector<Contact>& contacts_) const
276  {
277  contacts_.resize(contacts.size());
278  std::copy(contacts.begin(), contacts.end(), contacts_.begin());
279  }
280 
282  void clear()
283  {
284  contacts.clear();
285  }
286 
289  friend void invertResults(CollisionResult& result);
290 };
291 
292 
293 struct DistanceResult;
294 
297 {
300 
302  FCL_REAL rel_err; // relative error, between 0 and 1
303  FCL_REAL abs_err; // absoluate error
304 
306  DistanceRequest(bool enable_nearest_points_,
307  FCL_REAL rel_err_,
308  FCL_REAL abs_err_,
309  int /*unused*/) HPP_FCL_DEPRECATED :
310  enable_nearest_points(enable_nearest_points_),
311  rel_err(rel_err_),
312  abs_err(abs_err_)
313  {
314  }
315 
319  DistanceRequest(bool enable_nearest_points_ = false,
320  FCL_REAL rel_err_ = 0.0,
321  FCL_REAL abs_err_ = 0.0) :
322  enable_nearest_points(enable_nearest_points_),
323  rel_err(rel_err_),
324  abs_err(abs_err_)
325  {
326  }
327 
328  bool isSatisfied(const DistanceResult& result) const;
329 };
330 
331 
334 {
335 
336 public:
337 
340 
342  Vec3f nearest_points[2];
343 
346 
349 
352 
357  int b1;
358 
363  int b2;
364 
366  static const int NONE = -1;
367 
368  DistanceResult(FCL_REAL min_distance_ =
369  std::numeric_limits<FCL_REAL>::max()):
370  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()));
373  nearest_points [0] = nearest_points [1] = normal = nan;
374  }
375 
376 
378  void update(FCL_REAL distance, const CollisionGeometry* o1_, const CollisionGeometry* o2_, int b1_, int b2_)
379  {
380  if(min_distance > distance)
381  {
382  min_distance = distance;
383  o1 = o1_;
384  o2 = o2_;
385  b1 = b1_;
386  b2 = b2_;
387  }
388  }
389 
392  const CollisionGeometry* o2_, int b1_, int b2_,
393  const Vec3f& p1, const Vec3f& p2, const Vec3f& normal_)
394  {
395  if(min_distance > distance)
396  {
397  min_distance = distance;
398  o1 = o1_;
399  o2 = o2_;
400  b1 = b1_;
401  b2 = b2_;
402  nearest_points[0] = p1;
403  nearest_points[1] = p2;
404  normal = normal_;
405  }
406  }
407 
409  void update(const DistanceResult& other_result)
410  {
411  if(min_distance > other_result.min_distance)
412  {
413  min_distance = other_result.min_distance;
414  o1 = other_result.o1;
415  o2 = other_result.o2;
416  b1 = other_result.b1;
417  b2 = other_result.b2;
418  nearest_points[0] = other_result.nearest_points[0];
419  nearest_points[1] = other_result.nearest_points[1];
420  normal = other_result.normal;
421  }
422  }
423 
425  void clear()
426  {
427  min_distance = std::numeric_limits<FCL_REAL>::max();
428  o1 = NULL;
429  o2 = NULL;
430  b1 = NONE;
431  b2 = NONE;
432  }
433 
435  inline bool operator ==(const DistanceResult& other) const
436  {
437  bool is_same = min_distance == other.min_distance
438  && nearest_points[0] == other.nearest_points[0]
439  && nearest_points[1] == other.nearest_points[1]
440  && o1 == other.o1
441  && o2 == other.o2
442  && b1 == other.b1
443  && b2 == other.b2;
444 
445 // TODO: check also that two GeometryObject are indeed equal.
446  if ((o1 != NULL) xor (other.o1 != NULL)) return false;
447  is_same &= (o1 == other.o1);
448 // else if (o1 != NULL and other.o1 != NULL) is_same &= *o1 == *other.o1;
449 
450  if ((o2 != NULL) xor (other.o2 != NULL)) return false;
451  is_same &= (o2 == other.o2);
452 // else if (o2 != NULL and other.o2 != NULL) is_same &= *o2 == *other.o2;
453 
454  return is_same;
455  }
456 
457 };
458 
459 
461 {return static_cast<CollisionRequestFlag>(~static_cast<const int>(a));}
462 
464 {return static_cast<CollisionRequestFlag>(static_cast<const int>(a) | static_cast<const int>(b));}
465 
467 {return static_cast<CollisionRequestFlag>(static_cast<const int>(a) & static_cast<const int>(b));}
468 
470 {return static_cast<CollisionRequestFlag>(static_cast<const int>(a) ^ static_cast<const int>(b));}
471 
473 {return (CollisionRequestFlag&)((int&)(a) |= static_cast<const int>(b));}
474 
476 {return (CollisionRequestFlag&)((int&)(a) &= static_cast<const int>(b));}
477 
479 {return (CollisionRequestFlag&)((int&)(a) ^= static_cast<const int>(b));}
480 
481 }
482 
483 } // namespace hpp
484 
485 #endif
Contact(const CollisionGeometry *o1_, const CollisionGeometry *o2_, int b1_, int b2_)
Definition: collision_data.h:99
size_t num_max_contacts
The maximum number of contacts will return.
Definition: collision_data.h:153
CollisionRequestFlag operator|(CollisionRequestFlag a, CollisionRequestFlag b)
Definition: collision_data.h:463
DistanceRequest(bool enable_nearest_points_, FCL_REAL rel_err_, FCL_REAL abs_err_, int) HPP_FCL_DEPRECATED
Definition: collision_data.h:306
const int GST_INDEP HPP_FCL_DEPRECATED
Definition: collision_data.h:56
request to the distance computation
Definition: collision_data.h:296
void update(const DistanceResult &other_result)
add distance information into the result
Definition: collision_data.h:409
const CollisionGeometry * o1
collision object 1
Definition: collision_data.h:348
Main namespace.
Definition: AABB.h:43
collision result
Definition: collision_data.h:209
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:363
Vec3f nearest_points[2]
nearest points
Definition: collision_data.h:342
bool operator==(const Contact &other) const
Definition: collision_data.h:122
void updateDistanceLowerBound(const FCL_REAL &distance_lower_bound_)
Update the lower bound only if the distance in inferior.
Definition: collision_data.h:231
CollisionRequestFlag & operator|=(CollisionRequestFlag &a, CollisionRequestFlag b)
Definition: collision_data.h:472
size_t numContacts() const
number of contacts found
Definition: collision_data.h:257
void addContact(const Contact &c)
add one contact into result structure
Definition: collision_data.h:238
DistanceResult(FCL_REAL min_distance_=std::numeric_limits< FCL_REAL >::max())
Definition: collision_data.h:368
bool operator!=(const Contact &other) const
Definition: collision_data.h:133
FCL_REAL distance_lower_bound
Definition: collision_data.h:222
Definition: collision_data.h:144
void clear()
clear the result
Definition: collision_data.h:425
const CollisionGeometry * o1
collision object 1
Definition: collision_data.h:62
CollisionRequestFlag & operator &=(CollisionRequestFlag &a, CollisionRequestFlag b)
Definition: collision_data.h:475
CollisionRequestFlag
flag declaration for specifying required params in CollisionResult
Definition: collision_data.h:142
CollisionRequestFlag operator^(CollisionRequestFlag a, CollisionRequestFlag b)
Definition: collision_data.h:469
request to the collision algorithm
Definition: collision_data.h:150
void clear()
clear the results obtained
Definition: collision_data.h:282
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:87
double FCL_REAL
Definition: data_types.h:68
CollisionRequestFlag & operator^=(CollisionRequestFlag &a, CollisionRequestFlag b)
Definition: collision_data.h:478
static const int NONE
invalid contact primitive information
Definition: collision_data.h:91
bool operator<(const Contact &other) const
Definition: collision_data.h:115
FCL_REAL min_distance
minimum distance between two objects. if two objects are in collision, min_distance <= 0...
Definition: collision_data.h:339
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:357
Vec3f normal
In case both objects are in collision, store the normal.
Definition: collision_data.h:345
void getContacts(std::vector< Contact > &contacts_) const
get all the contacts
Definition: collision_data.h:275
Contact(const CollisionGeometry *o1_, const CollisionGeometry *o2_, int b1_, int b2_, const Vec3f &pos_, const Vec3f &normal_, FCL_REAL depth_)
Definition: collision_data.h:105
Vec3f cached_gjk_guess
Definition: collision_data.h:216
Vec3f pos
contact position, in world space
Definition: collision_data.h:84
FCL_REAL break_distance
Distance below which bounding volumes are broken down. See Collision detection and distance lower bou...
Definition: collision_data.h:173
FCL_REAL rel_err
error threshold for approximate distance
Definition: collision_data.h:302
CollisionRequest()
Definition: collision_data.h:194
bool enable_nearest_points
whether to return the nearest points
Definition: collision_data.h:299
bool enable_distance_lower_bound
Whether a lower bound on distance is returned when objects are disjoint.
Definition: collision_data.h:159
Vec3f cached_gjk_guess
the gjk intial guess set by user
Definition: collision_data.h:165
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:71
CollisionResult()
Definition: collision_data.h:225
const Contact & getContact(size_t i) const
get the i-th contact calculated
Definition: collision_data.h:263
CollisionRequestFlag operator &(CollisionRequestFlag a, CollisionRequestFlag b)
Definition: collision_data.h:466
Definition: collision_data.h:145
bool enable_contact
whether the contact information (normal, penetration depth and contact position) will return ...
Definition: collision_data.h:156
bool isCollision() const
return binary collision result
Definition: collision_data.h:251
bool enable_cached_gjk_guess
whether enable gjk intial guess
Definition: collision_data.h:162
CollisionRequestFlag operator~(CollisionRequestFlag a)
Definition: collision_data.h:460
Definition: collision_data.h:146
Eigen::Matrix< FCL_REAL, 3, 1 > Vec3f
Definition: data_types.h:73
const CollisionGeometry * o2
collision object 2
Definition: collision_data.h:65
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:378
Contact information returned by collision.
Definition: collision_data.h:59
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:391
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:78
The geometry for the object for collision or distance computation.
Definition: collision_object.h:63
FCL_REAL abs_err
Definition: collision_data.h:303
Vec3f normal
contact normal, pointing from o1 to o2
Definition: collision_data.h:81
const CollisionGeometry * o2
collision object 2
Definition: collision_data.h:351
DistanceRequest(bool enable_nearest_points_=false, FCL_REAL rel_err_=0.0, FCL_REAL abs_err_=0.0)
Definition: collision_data.h:319
distance result
Definition: collision_data.h:333
FCL_REAL security_margin
Distance below which objects are considered in collision. See Collision detection and distance lower ...
Definition: collision_data.h:169
Contact()
Definition: collision_data.h:93