FCL  0.6.0
Flexible Collision Library
distance-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_DISTANCE_INL_H
39 #define FCL_DISTANCE_INL_H
40 
41 #include "fcl/narrowphase/distance.h"
42 
43 namespace fcl
44 {
45 
46 //==============================================================================
47 extern template
48 double distance(
49  const CollisionObject<double>* o1,
50  const CollisionObject<double>* o2,
51  const DistanceRequest<double>& request,
52  DistanceResult<double>& result);
53 
54 //==============================================================================
55 extern template
56 double distance(
57  const CollisionGeometry<double>* o1, const Transform3<double>& tf1,
58  const CollisionGeometry<double>* o2, const Transform3<double>& tf2,
59  const DistanceRequest<double>& request, DistanceResult<double>& result);
60 
61 //==============================================================================
62 template <typename GJKSolver>
63 detail::DistanceFunctionMatrix<GJKSolver>& getDistanceFunctionLookTable()
64 {
65  static detail::DistanceFunctionMatrix<GJKSolver> table;
66  return table;
67 }
68 
69 //==============================================================================
70 template <typename NarrowPhaseSolver>
71 typename NarrowPhaseSolver::S distance(
72  const CollisionObject<typename NarrowPhaseSolver::S>* o1,
73  const CollisionObject<typename NarrowPhaseSolver::S>* o2,
74  const NarrowPhaseSolver* nsolver,
75  const DistanceRequest<typename NarrowPhaseSolver::S>& request,
76  DistanceResult<typename NarrowPhaseSolver::S>& result)
77 {
78  return distance<NarrowPhaseSolver>(
79  o1->collisionGeometry().get(),
80  o1->getTransform(),
81  o2->collisionGeometry().get(),
82  o2->getTransform(),
83  nsolver,
84  request,
85  result);
86 }
87 
88 //==============================================================================
89 template <typename NarrowPhaseSolver>
90 typename NarrowPhaseSolver::S distance(
91  const CollisionGeometry<typename NarrowPhaseSolver::S>* o1,
92  const Transform3<typename NarrowPhaseSolver::S>& tf1,
93  const CollisionGeometry<typename NarrowPhaseSolver::S>* o2,
94  const Transform3<typename NarrowPhaseSolver::S>& tf2,
95  const NarrowPhaseSolver* nsolver_,
96  const DistanceRequest<typename NarrowPhaseSolver::S>& request,
97  DistanceResult<typename NarrowPhaseSolver::S>& result)
98 {
99  using S = typename NarrowPhaseSolver::S;
100 
101  const NarrowPhaseSolver* nsolver = nsolver_;
102  if(!nsolver_)
103  nsolver = new NarrowPhaseSolver();
104 
105  const auto& looktable = getDistanceFunctionLookTable<NarrowPhaseSolver>();
106 
107  OBJECT_TYPE object_type1 = o1->getObjectType();
108  NODE_TYPE node_type1 = o1->getNodeType();
109  OBJECT_TYPE object_type2 = o2->getObjectType();
110  NODE_TYPE node_type2 = o2->getNodeType();
111 
112  S res = std::numeric_limits<S>::max();
113 
114  if(object_type1 == OT_GEOM && object_type2 == OT_BVH)
115  {
116  if(!looktable.distance_matrix[node_type2][node_type1])
117  {
118  std::cerr << "Warning: distance function between node type " << node_type1 << " and node type " << node_type2 << " is not supported" << std::endl;
119  }
120  else
121  {
122  res = looktable.distance_matrix[node_type2][node_type1](o2, tf2, o1, tf1, nsolver, request, result);
123  }
124  }
125  else
126  {
127  if(!looktable.distance_matrix[node_type1][node_type2])
128  {
129  std::cerr << "Warning: distance function between node type " << node_type1 << " and node type " << node_type2 << " is not supported" << std::endl;
130  }
131  else
132  {
133  res = looktable.distance_matrix[node_type1][node_type2](o1, tf1, o2, tf2, nsolver, request, result);
134  }
135  }
136 
137  if(!nsolver_)
138  delete nsolver;
139 
140  return res;
141 }
142 
143 //==============================================================================
144 template <typename S>
146  const CollisionObject<S>* o1,
147  const CollisionObject<S>* o2,
148  const DistanceRequest<S>& request,
149  DistanceResult<S>& result)
150 {
151  switch(request.gjk_solver_type)
152  {
153  case GST_LIBCCD:
154  {
156  return distance(o1, o2, &solver, request, result);
157  }
158  case GST_INDEP:
159  {
161  return distance(o1, o2, &solver, request, result);
162  }
163  default:
164  return -1; // error
165  }
166 }
167 
168 //==============================================================================
169 template <typename S>
170 S distance(
171  const CollisionGeometry<S>* o1, const Transform3<S>& tf1,
172  const CollisionGeometry<S>* o2, const Transform3<S>& tf2,
173  const DistanceRequest<S>& request, DistanceResult<S>& result)
174 {
175  switch(request.gjk_solver_type)
176  {
177  case GST_LIBCCD:
178  {
180  return distance(o1, tf1, o2, tf2, &solver, request, result);
181  }
182  case GST_INDEP:
183  {
185  return distance(o1, tf1, o2, tf2, &solver, request, result);
186  }
187  default:
188  return -1;
189  }
190 }
191 
192 } // namespace fcl
193 
194 #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
distance result
Definition: distance_request.h:48
The geometry for the object for collision or distance computation.
Definition: collision_geometry.h:59
collision and distance solver based on libccd library.
Definition: gjk_solver_libccd.h:53
OBJECT_TYPE
object type: BVH (mesh, points), basic geometry, octree
Definition: collision_geometry.h:51
GJKSolverType gjk_solver_type
narrow phase solver type
Definition: distance_request.h:62
the object for collision or distance computation, contains the geometry and the transform information...
Definition: collision_object.h:51
S distance(const Eigen::MatrixBase< DerivedA > &R0, const Eigen::MatrixBase< DerivedB > &T0, const kIOS< S > &b1, const kIOS< S > &b2, Vector3< S > *P, Vector3< S > *Q)
Approximate distance between two kIOS bounding volumes.
Definition: kIOS-inl.h:266
collision and distance solver based on GJK algorithm implemented in fcl (rewritten the code from the ...
Definition: gjk_solver_indep.h:53
request to the distance computation
Definition: distance_request.h:52