Collision Checker
fcl_collision_queries.h
Go to the documentation of this file.
1 #ifndef CPP_COLLISION_FCL_FCL_PRIMITIVE_COLLISION_TESTS_H_
2 #define CPP_COLLISION_FCL_FCL_PRIMITIVE_COLLISION_TESTS_H_
3 
5 
6 #include <fcl/broadphase/broadphase_dynamic_AABB_tree.h>
7 #include <fcl/math/constants.h>
8 #include <fcl/narrowphase/collision.h>
9 #include <fcl/narrowphase/collision_object.h>
10 
12 
13 #include <Eigen/Dense>
14 
21 
24 
27 
28 namespace collision {
29 namespace solvers {
30 namespace solverFCL {
31 
32 namespace fcl_primitive_queries {
33 
35  const collision::RectangleAABB &rect) {
36  SolverEntity_FCL *obj_entity;
38  get_object_fcl_entity_type(obj, obj_entity);
39  if (enttype == COLLISION_ENTITY_TYPE_FCL_OBJECT) {
40  const FCLCollisionObject *obj_fcl =
41  static_cast<const FCLCollisionObject *>(obj_entity);
42 
44  obj_fcl->getCollisionObject_fcl().get();
45  if (!fcl_obj) {
46  throw;
47  }
48  const fcl::AABB<double> &aabb1 = fcl_obj->getAABB();
49 
50  Eigen::Vector2d min_ = rect.min();
51  Eigen::Vector2d max_ = rect.max();
52 
53  fcl::Vector3d min_3d(min_[0], min_[1], 0);
54  fcl::Vector3d max_3d(max_[0], max_[1], 0);
55 
56  fcl::AABB<double> aabb2(min_3d, max_3d);
57  return aabb1.overlap(aabb2);
58  }
59  return true;
60 }
61 
63  std::shared_ptr<fcl::CollisionGeometry<FCL_PRECISION>>;
64 
65 inline fcl::Transform3<FCL_PRECISION> fcl_get_3d_translation(
66  const Eigen::Vector2d &pos) {
67  return fcl::Transform3<FCL_PRECISION>(fcl::Translation3<FCL_PRECISION>(
68  fcl::Vector3<FCL_PRECISION>(pos.x(), pos.y(), 0)));
69 }
71  const fcl::CollisionObject<FCL_PRECISION> &object_B) {
72  fcl::CollisionRequest<FCL_PRECISION> collisionRequest(
73  1, false); // Disable finding of contact points
74  fcl::CollisionResult<FCL_PRECISION> collisionResult;
75  collisionRequest.enable_cost = false;
76  collisionRequest.gjk_solver_type = FCL_SOLVER_TYPE;
77  fcl::collide(&object_A, &object_B, collisionRequest, collisionResult);
78  return collisionResult.isCollision();
79 }
80 
82  const FCLCollisionObject &obj_second)
83 
84 {
85  std::shared_ptr<fcl::CollisionObject<double>> col_obj_1;
86 
87  col_obj_1 = obj_first.getCollisionObject_fcl();
88 
89  std::shared_ptr<fcl::CollisionObject<double>> col_obj_2;
90  col_obj_2 = obj_second.getCollisionObject_fcl();
91 
92  return fcl_collide(*col_obj_1, *col_obj_2);
93 }
94 #if ENABLE_COLLISION_TESTS
95 // TODO: throw exception on malloc failure
97  const FCLCollisionObjectGroup &group_first,
98  const FCLCollisionObject &obj_second, bool test_enable = true) {
99  if (test_enable) {
101  sg_test.run_test_collide(
102  obj_second.getParent()->shared_from_this(),
103  static_cast<const ShapeGroup *>(group_first.getParent()));
104 #if ENABLE_COLLISION_TESTS_NARROWPHASE
105  sg_test.run_test_narrowphase(
106  obj_second.getParent()->shared_from_this(),
107  static_cast<const ShapeGroup *>(group_first.getParent()));
108 #endif
109  }
110 #else
112  const FCLCollisionObjectGroup &group_first,
113  const FCLCollisionObject &obj_second) {
114 #endif
115 
116  bool result = false;
117  fcl::BroadPhaseCollisionManager<FCL_PRECISION> *mngr;
118  group_first.getManager_fcl(mngr);
119  fcl::CollisionResult<FCL_PRECISION> collisionResult;
120  CollisionData<FCL_PRECISION> self_data_obstacles1;
121  fcl::CollisionRequest<FCL_PRECISION> collisionRequest(
122  1, false); // Disable finding of contact points
123  collisionRequest.enable_cost = false;
124  collisionRequest.gjk_solver_type = FCL_SOLVER_TYPE;
125  self_data_obstacles1.request = collisionRequest;
126  std::shared_ptr<fcl::CollisionObject<double>> col_obj_1;
127  col_obj_1 = obj_second.getCollisionObject_fcl();
128 
129  mngr->collide(col_obj_1.get(), &self_data_obstacles1,
130  defaultCollisionFunction<FCL_PRECISION>);
131  result = self_data_obstacles1.result.isCollision();
132  return result;
133 }
134 
135 inline int fcl_group_overlap(
136  const FCLCollisionObjectGroup &group_first,
137  const FCLCollisionObjectGroup &group_second,
138  std::vector<std::pair<int, int>> &retlist,
139  std::vector<std::pair<int, int>> *missed_col_naive_in = 0) {
140  // bool result=false;
141 
142  fcl::BroadPhaseCollisionManager<FCL_PRECISION> *mngr1;
143  group_first.getManager_fcl(mngr1);
144 
145  fcl::BroadPhaseCollisionManager<FCL_PRECISION> *mngr2;
146  group_second.getManager_fcl(mngr2);
147 
148  fcl::CollisionResult<FCL_PRECISION> collisionResult;
149 
150  CollisionRequestDataOverlap rdata(group_first.getContainer(),
151  group_second.getContainer());
152 
153  CollisionDataOverlap<FCL_PRECISION> self_data_obstacles(rdata);
154  fcl::CollisionRequest<FCL_PRECISION> collisionRequest(
155  1, false); // Disable finding of contact points
156  collisionRequest.gjk_solver_type = FCL_SOLVER_TYPE;
157  collisionRequest.enable_cost = false;
158 
159  self_data_obstacles.request = collisionRequest;
160 
161  mngr1->collide(mngr2, &self_data_obstacles,
162  defaultCollisionFunctionOverlap<FCL_PRECISION>);
163  auto res = self_data_obstacles.self_reqData.getRequestResultPairs();
164  for (auto el : res) {
165  retlist.push_back(el);
166  }
167 
168  return 0;
169 }
170 
171 #if ENABLE_COLLISION_TESTS
173  const FCLCollisionObjectGroup &group_first,
174  const FCLCollisionObjectGroup &group_second, bool enable_test = true) {
175  if (enable_test) {
177  const ShapeGroup *gr_first =
178  static_cast<const ShapeGroup *>(group_first.getParent());
179  const ShapeGroup *gr_second =
180  static_cast<const ShapeGroup *>(group_second.getParent());
181  sg_test.run_test_collide(gr_first, gr_second);
182 #if ENABLE_COLLISION_TESTS_NARROWPHASE
183  sg_test.run_test_narrowphase(gr_first, gr_second);
184 #endif
185  }
186 #else
188  const FCLCollisionObjectGroup &group_first,
189  const FCLCollisionObjectGroup &group_second) {
190 #endif
191  bool result = false;
192  fcl::BroadPhaseCollisionManager<FCL_PRECISION> *mngr1;
193  group_first.getManager_fcl(mngr1);
194 
195  fcl::BroadPhaseCollisionManager<FCL_PRECISION> *mngr2;
196  group_second.getManager_fcl(mngr2);
197 
198  fcl::CollisionResult<FCL_PRECISION> collisionResult;
199  CollisionData<FCL_PRECISION> self_data_obstacles;
200 
201  fcl::CollisionRequest<FCL_PRECISION> collisionRequest(
202  1, false); // Disable finding of contact points
203  collisionRequest.gjk_solver_type = FCL_SOLVER_TYPE;
204  collisionRequest.enable_cost = false;
205 
206  self_data_obstacles.request = collisionRequest;
207 
208  mngr1->collide(mngr2, &self_data_obstacles,
209  defaultCollisionFunction<FCL_PRECISION>);
210  result = self_data_obstacles.result.isCollision();
211  return result;
212 }
213 
214 inline bool fcl_collisionDetection(const Point &point,
215  const RectangleAABB &aabb) {
217  (const FCLCollisionObject &)aabb);
218 }
219 
220 inline bool fcl_collisionDetection(const Point &point,
221  const RectangleOBB &obb) {
223  (const FCLCollisionObject &)obb);
224 }
225 inline bool fcl_collisionDetection(const Point &point, const Sphere &sphere) {
227  (const FCLCollisionObject &)sphere);
228 }
229 
230 inline bool fcl_collisionDetection(const Point &point1, const Point &point2) {
231  return fcl_generic_collisionDetection((const FCLCollisionObject &)point1,
232  (const FCLCollisionObject &)point2);
233 }
234 
235 inline bool fcl_collisionDetection(const Point &point,
236  const Triangle &triangle) {
238  (const FCLCollisionObject &)triangle);
239 }
240 
241 inline bool fcl_collisionDetection(const Polygon &polyg1,
242  const Polygon &polyg2) {
243  return fcl_generic_collisionDetection((const FCLCollisionObject &)polyg1,
244  (const FCLCollisionObject &)polyg2);
245 }
246 
247 inline bool fcl_collisionDetection(const Polygon &polyg, const Shape &shape) {
249  (const FCLCollisionObject &)shape);
250 }
251 
252 inline bool fcl_collisionDetection(const ShapeGroup &shg,
253  const Polygon &polyg) {
255  (const FCLCollisionObject &)polyg);
256 }
257 
258 inline bool fcl_collisionDetection(const Sphere &sphere_first,
259  const Sphere &sphere_second) {
261  (const FCLCollisionObject &)sphere_first,
262  (const FCLCollisionObject &)sphere_second);
263 }
264 inline bool fcl_collisionDetection(const RectangleAABB &aabb_first,
265  const RectangleAABB &aabb_second) {
267  (const FCLCollisionObject &)aabb_first,
268  (const FCLCollisionObject &)aabb_second);
269 }
270 
271 inline bool fcl_collisionDetection(const RectangleAABB &aabb,
272  const RectangleOBB &obb) {
274  (const FCLCollisionObject &)obb);
275 }
276 
277 inline bool fcl_collisionDetection(const ShapeGroup &shg, const Shape &shape) {
279  (const FCLCollisionObject &)shape);
280 }
281 
282 inline bool fcl_collisionDetection(const ShapeGroup &shg1,
283  const ShapeGroup &shg2) {
285  (const FCLCollisionObjectGroup &)shg2);
286 }
287 
288 inline bool fcl_collisionDetection(const RectangleAABB &aabb,
289  const Sphere &sphere) {
291  (const FCLCollisionObject &)sphere);
292 }
293 inline bool fcl_collisionDetection(const RectangleOBB &obb_first,
294  const RectangleOBB &obb_second) {
295  return fcl_generic_collisionDetection((const FCLCollisionObject &)obb_first,
296  (const FCLCollisionObject &)obb_second);
297 }
298 inline bool fcl_collisionDetection(const RectangleOBB &obb,
299  const Sphere &sphere) {
301  (const FCLCollisionObject &)sphere);
302 }
303 
304 inline bool fcl_collisionDetection(const Sphere &sphere,
305  const Triangle &triangle) {
306  return fcl_generic_collisionDetection((const FCLCollisionObject &)sphere,
307  (const FCLCollisionObject &)triangle);
308 }
309 inline bool fcl_collisionDetection(const Triangle &triangle_a,
310  const Triangle &triangle_b) {
311  return fcl_generic_collisionDetection((const FCLCollisionObject &)triangle_a,
312  (const FCLCollisionObject &)triangle_b);
313 }
314 inline bool fcl_collisionDetection(const RectangleOBB &obb,
315  const Triangle &triangle) {
317  (const FCLCollisionObject &)triangle);
318 }
319 inline bool fcl_collisionDetection(const RectangleAABB &aabb,
320  const Triangle &triangle) {
322  (const FCLCollisionObject &)triangle);
323 }
324 
325 } // namespace fcl_primitive_queries
326 } // namespace solverFCL
327 } // namespace solvers
328 } // namespace collision
329 
330 #endif /* CPP_COLLISION_FCL_FCL_PRIMITIVE_COLLISION_TESTS_H_ */
Oriented rectangle.
Definition: rectangle_obb.h:19
const CollisionObject * getParent(void) const
fcl::CollisionResult< S > result
Collision result.
bool fcl_testAABB(CollisionObjectConstPtr obj, const collision::RectangleAABB &rect)
Triangle.
Definition: triangle.h:17
ShapeGroup can contain simple shapes.
Definition: shape_group.h:33
std::shared_ptr< const CollisionObject > CollisionObjectConstPtr
Eigen::Vector2d min() const
bool run_test_collide(CollisionObjectConstPtr co, const ShapeGroup *sg)
FCL_COLLISION_ENTITY_TYPE get_object_fcl_entity_type(const CollisionObject *obj2)
Definition: fcl_helpers.h:11
Circle.
Definition: sphere.h:14
std::shared_ptr< fcl::CollisionObject< FCL_PRECISION > > getCollisionObject_fcl(void) const
fcl::CollisionRequest< S > request
Collision request.
bool fcl_generic_collisionDetection(const FCLCollisionObject &obj_first, const FCLCollisionObject &obj_second)
bool fcl_collisionDetection(const Point &point, const RectangleAABB &aabb)
#define FCL_SOLVER_TYPE
Definition: fcl_decl.h:11
bool fcl_collide(const fcl::CollisionObject< FCL_PRECISION > &object_A, const fcl::CollisionObject< FCL_PRECISION > &object_B)
Axis-aligned rectangle.
Base prototype for the shape of an obstacle.
Definition: shape.h:25
int fcl_group_overlap(const FCLCollisionObjectGroup &group_first, const FCLCollisionObjectGroup &group_second, std::vector< std::pair< int, int >> &retlist, std::vector< std::pair< int, int >> *missed_col_naive_in=0)
bool run_test_narrowphase(CollisionObjectConstPtr co, const ShapeGroup *sg)
Collision data stores the collision request and the result given by collision algorithm.
std::shared_ptr< fcl::CollisionGeometry< FCL_PRECISION > > CollisionGeometryPtr_t
A point in space.
Definition: point.h:18
Polygon contains Triangles and Vertices.
Definition: polygon.h:29
Eigen::Vector2d max() const
int getManager_fcl(fcl::BroadPhaseCollisionManager< FCL_PRECISION > *&) const
fcl::Transform3< FCL_PRECISION > fcl_get_3d_translation(const Eigen::Vector2d &pos)