sphere_capsule-inl.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-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 
42 
43 namespace fcl
44 {
45 
46 namespace detail
47 {
48 
49 //==============================================================================
50 extern template
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  // NOTE: By assigning this a negative value, it allows the ultimately
142  // calling code in distance-inl.h (distance() method) to use collision to
143  // determine penetration depth and contact points. NOTE: This is a
144  // *horrible* thing.
145  // TODO(SeanCurtis-TRI): Create a *signed* distance variant of this and use
146  // it to determined signed distance, penetration, and distance.
147  if (dist) *dist = -1;
148  return false;
149  }
150 
151  if(dist) *dist = distance;
152 
153  if(p1 || p2) diff.normalize();
154  if(p1)
155  {
156  *p1 = s_c - diff * s1.radius;
157  *p1 = tf2 * (*p1);
158  }
159 
160  if(p2)
161  {
162  *p2 = segment_point + diff * s2.radius;
163  *p2 = tf2 * (*p2);
164  }
165 
166  return true;
167 }
168 
169 } // namespace detail
170 } // namespace fcl
171 
172 #endif
fcl::detail::sphereCapsuleIntersect
template bool sphereCapsuleIntersect(const Sphere< double > &s1, const Transform3< double > &tf1, const Capsule< double > &s2, const Transform3< double > &tf2, std::vector< ContactPoint< double >> *contacts)
fcl::Sphere< double >
template class FCL_EXPORT Sphere< double >
fcl::Transform3
Eigen::Transform< S, 3, Eigen::Isometry > Transform3
Definition: types.h:91
fcl::Capsule::radius
S radius
Radius of capsule.
Definition: capsule.h:61
fcl::ContactPoint
Minimal contact information returned by collision.
Definition: contact_point.h:48
fcl::detail::lineSegmentPointClosestToPoint
template void lineSegmentPointClosestToPoint(const Vector3< double > &p, const Vector3< double > &s1, const Vector3< double > &s2, Vector3< double > &sp)
fcl::Capsule::lz
S lz
Length along z axis.
Definition: capsule.h:64
fcl::Vector3
Eigen::Matrix< S, 3, 1 > Vector3
Definition: types.h:70
fcl::Sphere::radius
S radius
Radius of the sphere.
Definition: sphere.h:60
sphere_capsule.h
fcl::detail::distance
template void distance(DistanceTraversalNodeBase< double > *node, BVHFrontList *front_list, int qsize)
fcl::time::point
std::chrono::system_clock::time_point point
Representation of a point in time.
Definition: time.h:52
fcl::Sphere
Center at zero point sphere.
Definition: sphere.h:51
fcl::Capsule< double >
template class FCL_EXPORT Capsule< double >
fcl::detail::sphereCapsuleDistance
template bool sphereCapsuleDistance(const Sphere< double > &s1, const Transform3< double > &tf1, const Capsule< double > &s2, const Transform3< double > &tf2, double *dist, Vector3< double > *p1, Vector3< double > *p2)
fcl
Main namespace.
Definition: broadphase_bruteforce-inl.h:45
fcl::Capsule< S >


fcl
Author(s):
autogenerated on Tue Dec 5 2023 03:40:48