FCL  0.6.0
Flexible Collision Library
sphere_capsule-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_NARROWPHASE_DETAIL_SPHERECAPSULE_INL_H
39 #define FCL_NARROWPHASE_DETAIL_SPHERECAPSULE_INL_H
40 
41 #include "fcl/narrowphase/detail/primitive_shape_algorithm/sphere_capsule.h"
42 
43 namespace fcl
44 {
45 
46 namespace detail
47 {
48 
49 //==============================================================================
50 extern template
51 void lineSegmentPointClosestToPoint(
52  const Vector3<double> &p,
53  const Vector3<double> &s1,
54  const Vector3<double> &s2,
55  Vector3<double> &sp);
56 
57 //==============================================================================
58 extern template
59 bool sphereCapsuleIntersect(const Sphere<double>& s1, const Transform3<double>& tf1,
60  const Capsule<double>& s2, const Transform3<double>& tf2,
61  std::vector<ContactPoint<double>>* contacts);
62 
63 //==============================================================================
64 extern template
65 bool sphereCapsuleDistance(const Sphere<double>& s1, const Transform3<double>& tf1,
66  const Capsule<double>& s2, const Transform3<double>& tf2,
67  double* dist, Vector3<double>* p1, Vector3<double>* p2);
68 
69 //==============================================================================
70 template <typename S>
71 void lineSegmentPointClosestToPoint (const Vector3<S> &p, const Vector3<S> &s1, const Vector3<S> &s2, Vector3<S> &sp) {
72  Vector3<S> v = s2 - s1;
73  Vector3<S> w = p - s1;
74 
75  S c1 = w.dot(v);
76  S c2 = v.dot(v);
77 
78  if (c1 <= 0) {
79  sp = s1;
80  } else if (c2 <= c1) {
81  sp = s2;
82  } else {
83  S b = c1/c2;
84  Vector3<S> Pb = s1 + v * b;
85  sp = Pb;
86  }
87 }
88 
89 //==============================================================================
90 template <typename S>
91 bool sphereCapsuleIntersect(const Sphere<S>& s1, const Transform3<S>& tf1,
92  const Capsule<S>& s2, const Transform3<S>& tf2,
93  std::vector<ContactPoint<S>>* contacts)
94 {
95  const Vector3<S> pos1(0., 0., 0.5 * s2.lz);
96  const Vector3<S> pos2(0., 0., -0.5 * s2.lz);
97  const Vector3<S> s_c = tf2.inverse(Eigen::Isometry) * tf1.translation();
98 
99  Vector3<S> segment_point;
100 
101  lineSegmentPointClosestToPoint (s_c, pos1, pos2, segment_point);
102  Vector3<S> diff = s_c - segment_point;
103 
104  const S distance = diff.norm() - s1.radius - s2.radius;
105 
106  if (distance > 0)
107  return false;
108 
109  const Vector3<S> local_normal = -diff.normalized();
110 
111  if (contacts)
112  {
113  const Vector3<S> normal = tf2.linear() * local_normal;
114  const Vector3<S> point = tf2 * (segment_point + local_normal * distance);
115  const S penetration_depth = -distance;
116 
117  contacts->emplace_back(normal, point, penetration_depth);
118  }
119 
120  return true;
121 }
122 
123 //==============================================================================
124 template <typename S>
125 bool sphereCapsuleDistance(const Sphere<S>& s1, const Transform3<S>& tf1,
126  const Capsule<S>& s2, const Transform3<S>& tf2,
127  S* dist, Vector3<S>* p1, Vector3<S>* p2)
128 {
129  Vector3<S> pos1(0., 0., 0.5 * s2.lz);
130  Vector3<S> pos2(0., 0., -0.5 * s2.lz);
131  Vector3<S> s_c = tf2.inverse(Eigen::Isometry) * tf1.translation();
132 
133  Vector3<S> segment_point;
134 
135  lineSegmentPointClosestToPoint (s_c, pos1, pos2, segment_point);
136  Vector3<S> diff = s_c - segment_point;
137 
138  S distance = diff.norm() - s1.radius - s2.radius;
139 
140  if(distance <= 0)
141  return false;
142 
143  if(dist) *dist = distance;
144 
145  if(p1 || p2) diff.normalize();
146  if(p1)
147  {
148  *p1 = s_c - diff * s1.radius;
149  *p1 = tf1.inverse(Eigen::Isometry) * tf2 * (*p1);
150  }
151 
152  if(p2) *p2 = segment_point + diff * s1.radius;
153 
154  return true;
155 }
156 
157 } // namespace detail
158 } // namespace fcl
159 
160 #endif
Main namespace.
Definition: broadphase_bruteforce-inl.h:45
std::chrono::system_clock::time_point point
Representation of a point in time.
Definition: time.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