FCL  0.6.0
Flexible Collision Library
collision-inl.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011-2014, Willow Garage, Inc.
5  * Copyright (c) 2014-2016, 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 
38 #ifndef FCL_COLLISION_INL_H
39 #define FCL_COLLISION_INL_H
40 
41 #include "fcl/narrowphase/collision.h"
42 
43 #include "fcl/narrowphase/detail/collision_func_matrix.h"
44 #include "fcl/narrowphase/detail/gjk_solver_indep.h"
45 #include "fcl/narrowphase/detail/gjk_solver_libccd.h"
46 
47 namespace fcl
48 {
49 
50 //==============================================================================
51 extern template
52 std::size_t collide(
53  const CollisionObject<double>* o1,
54  const CollisionObject<double>* o2,
55  const CollisionRequest<double>& request,
56  CollisionResult<double>& result);
57 
58 //==============================================================================
59 extern template
60 std::size_t collide(
61  const CollisionGeometry<double>* o1,
62  const Transform3<double>& tf1,
63  const CollisionGeometry<double>* o2,
64  const Transform3<double>& tf2,
65  const CollisionRequest<double>& request,
66  CollisionResult<double>& result);
67 
68 //==============================================================================
69 template<typename GJKSolver>
70 detail::CollisionFunctionMatrix<GJKSolver>& getCollisionFunctionLookTable()
71 {
72  static detail::CollisionFunctionMatrix<GJKSolver> table;
73  return table;
74 }
75 
76 //==============================================================================
77 template <typename S, typename NarrowPhaseSolver>
78 std::size_t collide(
79  const CollisionObject<S>* o1,
80  const CollisionObject<S>* o2,
81  const NarrowPhaseSolver* nsolver,
82  const CollisionRequest<S>& request,
83  CollisionResult<S>& result)
84 {
85  return collide(o1->collisionGeometry().get(), o1->getTransform(), o2->collisionGeometry().get(), o2->getTransform(),
86  nsolver, request, result);
87 }
88 
89 //==============================================================================
90 template <typename S, typename NarrowPhaseSolver>
91 std::size_t collide(
92  const CollisionGeometry<S>* o1,
93  const Transform3<S>& tf1,
94  const CollisionGeometry<S>* o2,
95  const Transform3<S>& tf2,
96  const NarrowPhaseSolver* nsolver_,
97  const CollisionRequest<S>& request,
98  CollisionResult<S>& result)
99 {
100  const NarrowPhaseSolver* nsolver = nsolver_;
101  if(!nsolver_)
102  nsolver = new NarrowPhaseSolver();
103 
104  const auto& looktable = getCollisionFunctionLookTable<NarrowPhaseSolver>();
105 
106  std::size_t res;
107  if(request.num_max_contacts == 0)
108  {
109  std::cerr << "Warning: should stop early as num_max_contact is " << request.num_max_contacts << " !" << std::endl;
110  res = 0;
111  }
112  else
113  {
114  OBJECT_TYPE object_type1 = o1->getObjectType();
115  OBJECT_TYPE object_type2 = o2->getObjectType();
116  NODE_TYPE node_type1 = o1->getNodeType();
117  NODE_TYPE node_type2 = o2->getNodeType();
118 
119  if(object_type1 == OT_GEOM && object_type2 == OT_BVH)
120  {
121  if(!looktable.collision_matrix[node_type2][node_type1])
122  {
123  std::cerr << "Warning: collision function between node type " << node_type1 << " and node type " << node_type2 << " is not supported"<< std::endl;
124  res = 0;
125  }
126  else
127  res = looktable.collision_matrix[node_type2][node_type1](o2, tf2, o1, tf1, nsolver, request, result);
128  }
129  else
130  {
131  if(!looktable.collision_matrix[node_type1][node_type2])
132  {
133  std::cerr << "Warning: collision function between node type " << node_type1 << " and node type " << node_type2 << " is not supported"<< std::endl;
134  res = 0;
135  }
136  else
137  res = looktable.collision_matrix[node_type1][node_type2](o1, tf1, o2, tf2, nsolver, request, result);
138  }
139  }
140 
141  if(!nsolver_)
142  delete nsolver;
143 
144  return res;
145 }
146 
147 //==============================================================================
148 template <typename S>
149 std::size_t collide(const CollisionObject<S>* o1, const CollisionObject<S>* o2,
150  const CollisionRequest<S>& request, CollisionResult<S>& result)
151 {
152  switch(request.gjk_solver_type)
153  {
154  case GST_LIBCCD:
155  {
157  return collide(o1, o2, &solver, request, result);
158  }
159  case GST_INDEP:
160  {
162  return collide(o1, o2, &solver, request, result);
163  }
164  default:
165  return -1; // error
166  }
167 }
168 
169 //==============================================================================
170 template <typename S>
171 std::size_t collide(
172  const CollisionGeometry<S>* o1,
173  const Transform3<S>& tf1,
174  const CollisionGeometry<S>* o2,
175  const Transform3<S>& tf2,
176  const CollisionRequest<S>& request,
177  CollisionResult<S>& result)
178 {
179  switch(request.gjk_solver_type)
180  {
181  case GST_LIBCCD:
182  {
184  return collide(o1, tf1, o2, tf2, &solver, request, result);
185  }
186  case GST_INDEP:
187  {
189  return collide(o1, tf1, o2, tf2, &solver, request, result);
190  }
191  default:
192  std::cerr << "Warning! Invalid GJK solver" << std::endl;
193  return -1; // error
194  }
195 }
196 
197 } // namespace fcl
198 
199 #endif
NODE_TYPE
traversal node type: bounding volume (AABB, OBB, RSS, kIOS, OBBRSS, KDOP16, KDOP18, kDOP24), basic shape (box, sphere, ellipsoid, capsule, cone, cylinder, convex, plane, halfspace, triangle), and octree
Definition: collision_geometry.h:54
Main namespace.
Definition: broadphase_bruteforce-inl.h:45
collision result
Definition: collision_request.h:48
The geometry for the object for collision or distance computation.
Definition: collision_geometry.h:59
GJKSolverType gjk_solver_type
narrow phase solver
Definition: collision_request.h:70
collision and distance solver based on libccd library.
Definition: gjk_solver_libccd.h:53
request to the collision algorithm
Definition: collision_request.h:52
OBJECT_TYPE
object type: BVH (mesh, points), basic geometry, octree
Definition: collision_geometry.h:51
the object for collision or distance computation, contains the geometry and the transform information...
Definition: collision_object.h:51
collision and distance solver based on GJK algorithm implemented in fcl (rewritten the code from the ...
Definition: gjk_solver_indep.h:53