hpp-fcl  1.5.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 
57 
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 QueryResult;
140 
143 {
146 
149 
152 
154  enable_cached_gjk_guess (false),
155  cached_gjk_guess (1,0,0),
156  cached_support_func_guess(support_func_guess_t::Zero())
157  {}
158 
159  void updateGuess(const QueryResult& result);
160 
162  inline bool operator ==(const QueryRequest& other) const
163  {
164  return enable_cached_gjk_guess == other.enable_cached_gjk_guess
165  && cached_gjk_guess == other.cached_gjk_guess
166  && cached_support_func_guess == other.cached_support_func_guess;
167  }
168 };
169 
172 {
175 
178 };
179 
180 inline void QueryRequest::updateGuess(const QueryResult& result)
181 {
182  if (enable_cached_gjk_guess) {
183  cached_gjk_guess = result.cached_gjk_guess;
184  cached_support_func_guess = result.cached_support_func_guess;
185  }
186 }
187 
188 struct CollisionResult;
189 
192 {
193  CONTACT = 0x00001,
195  NO_REQUEST = 0x01000
196 };
197 
200 {
203 
206 
209 
213 
217 
218  explicit CollisionRequest(size_t num_max_contacts_,
219  bool enable_contact_ = false,
220  bool enable_distance_lower_bound_ = false,
221  size_t num_max_cost_sources_ = 1,
222  bool enable_cost_ = false,
223  bool use_approximate_cost_ = true)
224  HPP_FCL_DEPRECATED;
225 
226  explicit CollisionRequest(const CollisionRequestFlag flag, size_t num_max_contacts_) :
227  num_max_contacts(num_max_contacts_),
228  enable_contact(flag & CONTACT),
229  enable_distance_lower_bound (flag & DISTANCE_LOWER_BOUND),
230  security_margin (0),
231  break_distance (1e-3)
232  {
233  }
234 
236  num_max_contacts(1),
237  enable_contact(false),
238  enable_distance_lower_bound (false),
239  security_margin (0),
240  break_distance (1e-3)
241  {
242  }
243 
244  bool isSatisfied(const CollisionResult& result) const;
245 
247  inline bool operator ==(const CollisionRequest& other) const
248  {
249  return QueryRequest::operator==(other)
250  && num_max_contacts == other.num_max_contacts
251  && enable_contact == other.enable_contact
252  && enable_distance_lower_bound == other.enable_distance_lower_bound
253  && security_margin == other.security_margin
254  && break_distance == other.break_distance;
255  }
256 };
257 
260 {
261 private:
263  std::vector<Contact> contacts;
264 
265 public:
271 
272 public:
274  : distance_lower_bound ((std::numeric_limits<FCL_REAL>::max)())
275  {
276  }
277 
279  inline void updateDistanceLowerBound (const FCL_REAL& distance_lower_bound_)
280  {
281  if (distance_lower_bound_ < distance_lower_bound)
282  distance_lower_bound = distance_lower_bound_;
283  }
284 
286  inline void addContact(const Contact& c)
287  {
288  contacts.push_back(c);
289  }
290 
292  inline bool operator ==(const CollisionResult& other) const
293  {
294  return contacts == other.contacts
295  && distance_lower_bound == other.distance_lower_bound;
296  }
297 
299  bool isCollision() const
300  {
301  return contacts.size() > 0;
302  }
303 
305  size_t numContacts() const
306  {
307  return contacts.size();
308  }
309 
311  const Contact& getContact(size_t i) const
312  {
313  if(contacts.size() == 0)
314  throw std::invalid_argument("The number of contacts is zero. No Contact can be returned.");
315 
316  if(i < contacts.size())
317  return contacts[i];
318  else
319  return contacts.back();
320  }
321 
323  void getContacts(std::vector<Contact>& contacts_) const
324  {
325  contacts_.resize(contacts.size());
326  std::copy(contacts.begin(), contacts.end(), contacts_.begin());
327  }
328 
330  void clear()
331  {
332  contacts.clear();
333  }
334 
337  friend void invertResults(CollisionResult& result);
338 };
339 
340 struct DistanceResult;
341 
344 {
347 
349  FCL_REAL rel_err; // relative error, between 0 and 1
350  FCL_REAL abs_err; // absoluate error
351 
353  DistanceRequest(bool enable_nearest_points_,
354  FCL_REAL rel_err_,
355  FCL_REAL abs_err_,
356  int /*unused*/) HPP_FCL_DEPRECATED :
357  enable_nearest_points(enable_nearest_points_),
358  rel_err(rel_err_),
359  abs_err(abs_err_)
360  {
361  }
362 
366  DistanceRequest(bool enable_nearest_points_ = false,
367  FCL_REAL rel_err_ = 0.0,
368  FCL_REAL abs_err_ = 0.0) :
369  enable_nearest_points(enable_nearest_points_),
370  rel_err(rel_err_),
371  abs_err(abs_err_)
372  {
373  }
374 
375  bool isSatisfied(const DistanceResult& result) const;
376 
378  inline bool operator ==(const DistanceRequest& other) const
379  {
380  return QueryRequest::operator==(other)
381  && enable_nearest_points == other.enable_nearest_points
382  && rel_err == other.rel_err
383  && abs_err == other.abs_err;
384  }
385 };
386 
389 {
390 
391 public:
392 
395 
397  Vec3f nearest_points[2];
398 
401 
404 
407 
412  int b1;
413 
418  int b2;
419 
421  static const int NONE = -1;
422 
423  DistanceResult(FCL_REAL min_distance_ =
424  (std::numeric_limits<FCL_REAL>::max)()):
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()));
428  nearest_points [0] = nearest_points [1] = normal = nan;
429  }
430 
431 
433  void update(FCL_REAL distance, const CollisionGeometry* o1_, const CollisionGeometry* o2_, int b1_, int b2_)
434  {
435  if(min_distance > distance)
436  {
437  min_distance = distance;
438  o1 = o1_;
439  o2 = o2_;
440  b1 = b1_;
441  b2 = b2_;
442  }
443  }
444 
447  const CollisionGeometry* o2_, int b1_, int b2_,
448  const Vec3f& p1, const Vec3f& p2, const Vec3f& normal_)
449  {
450  if(min_distance > distance)
451  {
452  min_distance = distance;
453  o1 = o1_;
454  o2 = o2_;
455  b1 = b1_;
456  b2 = b2_;
457  nearest_points[0] = p1;
458  nearest_points[1] = p2;
459  normal = normal_;
460  }
461  }
462 
464  void update(const DistanceResult& other_result)
465  {
466  if(min_distance > other_result.min_distance)
467  {
468  min_distance = other_result.min_distance;
469  o1 = other_result.o1;
470  o2 = other_result.o2;
471  b1 = other_result.b1;
472  b2 = other_result.b2;
473  nearest_points[0] = other_result.nearest_points[0];
474  nearest_points[1] = other_result.nearest_points[1];
475  normal = other_result.normal;
476  }
477  }
478 
480  void clear()
481  {
482  min_distance = (std::numeric_limits<FCL_REAL>::max)();
483  o1 = NULL;
484  o2 = NULL;
485  b1 = NONE;
486  b2 = NONE;
487  }
488 
490  inline bool operator ==(const DistanceResult& other) const
491  {
492  bool is_same = min_distance == other.min_distance
493  && nearest_points[0] == other.nearest_points[0]
494  && nearest_points[1] == other.nearest_points[1]
495  && o1 == other.o1
496  && o2 == other.o2
497  && b1 == other.b1
498  && b2 == other.b2;
499 
500 // TODO: check also that two GeometryObject are indeed equal.
501  if ((o1 != NULL) xor (other.o1 != NULL)) return false;
502  is_same &= (o1 == other.o1);
503 // else if (o1 != NULL and other.o1 != NULL) is_same &= *o1 == *other.o1;
504 
505  if ((o2 != NULL) xor (other.o2 != NULL)) return false;
506  is_same &= (o2 == other.o2);
507 // else if (o2 != NULL and other.o2 != NULL) is_same &= *o2 == *other.o2;
508 
509  return is_same;
510  }
511 
512 };
513 
515 {return static_cast<CollisionRequestFlag>(~static_cast<const int>(a));}
516 
518 {return static_cast<CollisionRequestFlag>(static_cast<const int>(a) | static_cast<const int>(b));}
519 
521 {return static_cast<CollisionRequestFlag>(static_cast<const int>(a) & static_cast<const int>(b));}
522 
524 {return static_cast<CollisionRequestFlag>(static_cast<const int>(a) ^ static_cast<const int>(b));}
525 
527 {return (CollisionRequestFlag&)((int&)(a) |= static_cast<const int>(b));}
528 
530 {return (CollisionRequestFlag&)((int&)(a) &= static_cast<const int>(b));}
531 
533 {return (CollisionRequestFlag&)((int&)(a) ^= static_cast<const int>(b));}
534 
535 }
536 
537 } // namespace hpp
538 
539 #endif
Contact(const CollisionGeometry *o1_, const CollisionGeometry *o2_, int b1_, int b2_)
Definition: collision_data.h:99
support_func_guess_t cached_support_func_guess
the support function intial guess set by user
Definition: collision_data.h:151
size_t num_max_contacts
The maximum number of contacts will return.
Definition: collision_data.h:202
CollisionRequestFlag operator|(CollisionRequestFlag a, CollisionRequestFlag b)
Definition: collision_data.h:517
request to the distance computation
Definition: collision_data.h:343
bool operator==(const QueryRequest &other) const
whether two QueryRequest are the same or not
Definition: collision_data.h:162
void update(const DistanceResult &other_result)
add distance information into the result
Definition: collision_data.h:464
support_func_guess_t cached_support_func_guess
stores the last support function vertex index, when relevant.
Definition: collision_data.h:177
const CollisionGeometry * o1
collision object 1
Definition: collision_data.h:403
base class for all query results
Definition: collision_data.h:171
Main namespace.
Definition: AABB.h:43
collision result
Definition: collision_data.h:259
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:418
Vec3f nearest_points[2]
nearest points
Definition: collision_data.h:397
bool enable_cached_gjk_guess
whether enable gjk intial guess
Definition: collision_data.h:145
void updateDistanceLowerBound(const FCL_REAL &distance_lower_bound_)
Update the lower bound only if the distance in inferior.
Definition: collision_data.h:279
CollisionRequestFlag & operator|=(CollisionRequestFlag &a, CollisionRequestFlag b)
Definition: collision_data.h:526
QueryRequest()
Definition: collision_data.h:153
size_t numContacts() const
number of contacts found
Definition: collision_data.h:305
void addContact(const Contact &c)
add one contact into result structure
Definition: collision_data.h:286
FCL_REAL distance_lower_bound
Definition: collision_data.h:270
Definition: collision_data.h:193
void clear()
clear the result
Definition: collision_data.h:480
const CollisionGeometry * o1
collision object 1
Definition: collision_data.h:62
DistanceRequest(bool enable_nearest_points_, FCL_REAL rel_err_, FCL_REAL abs_err_, int)
Definition: collision_data.h:353
CollisionRequestFlag & operator &=(CollisionRequestFlag &a, CollisionRequestFlag b)
Definition: collision_data.h:529
CollisionRequestFlag
flag declaration for specifying required params in CollisionResult
Definition: collision_data.h:191
CollisionRequestFlag operator^(CollisionRequestFlag a, CollisionRequestFlag b)
Definition: collision_data.h:523
request to the collision algorithm
Definition: collision_data.h:199
void clear()
clear the results obtained
Definition: collision_data.h:330
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:66
void updateGuess(const QueryResult &result)
Definition: collision_data.h:180
CollisionRequestFlag & operator^=(CollisionRequestFlag &a, CollisionRequestFlag b)
Definition: collision_data.h:532
FCL_REAL min_distance
minimum distance between two objects. if two objects are in collision, min_distance <= 0...
Definition: collision_data.h:394
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:412
Vec3f normal
In case both objects are in collision, store the normal.
Definition: collision_data.h:400
void getContacts(std::vector< Contact > &contacts_) const
get all the contacts
Definition: collision_data.h:323
Vec3f cached_gjk_guess
the gjk intial guess set by user
Definition: collision_data.h:148
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
Eigen::Vector2i support_func_guess_t
Definition: data_types.h:69
Vec3f pos
contact position, in world space
Definition: collision_data.h:84
DistanceResult(FCL_REAL min_distance_=(std::numeric_limits< FCL_REAL >::max)())
Definition: collision_data.h:423
FCL_REAL break_distance
Distance below which bounding volumes are broken down. See Collision detection and distance lower bou...
Definition: collision_data.h:216
FCL_REAL rel_err
error threshold for approximate distance
Definition: collision_data.h:349
CollisionRequest()
Definition: collision_data.h:235
bool enable_nearest_points
whether to return the nearest points
Definition: collision_data.h:346
bool enable_distance_lower_bound
Whether a lower bound on distance is returned when objects are disjoint.
Definition: collision_data.h:208
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:273
const Contact & getContact(size_t i) const
get the i-th contact calculated
Definition: collision_data.h:311
CollisionRequestFlag operator &(CollisionRequestFlag a, CollisionRequestFlag b)
Definition: collision_data.h:520
Definition: collision_data.h:194
bool enable_contact
whether the contact information (normal, penetration depth and contact position) will return ...
Definition: collision_data.h:205
Vec3f cached_gjk_guess
stores the last GJK ray when relevant.
Definition: collision_data.h:174
bool isCollision() const
return binary collision result
Definition: collision_data.h:299
base class for all query requests
Definition: collision_data.h:142
CollisionRequestFlag operator~(CollisionRequestFlag a)
Definition: collision_data.h:514
Definition: collision_data.h:195
Eigen::Matrix< FCL_REAL, 3, 1 > Vec3f
Definition: data_types.h:67
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:433
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:446
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:350
Vec3f normal
contact normal, pointing from o1 to o2
Definition: collision_data.h:81
const CollisionGeometry * o2
collision object 2
Definition: collision_data.h:406
#define HPP_FCL_DEPRECATED
Definition: deprecated.hh:32
DistanceRequest(bool enable_nearest_points_=false, FCL_REAL rel_err_=0.0, FCL_REAL abs_err_=0.0)
Definition: collision_data.h:366
#define HPP_FCL_DLLAPI
Definition: config.hh:64
distance result
Definition: collision_data.h:388
FCL_REAL security_margin
Distance below which objects are considered in collision. See Collision detection and distance lower ...
Definition: collision_data.h:212
Contact()
Definition: collision_data.h:93
const int GST_INDEP
Definition: collision_data.h:56