sphere_sphere-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_SPHERESPHERE_INL_H
39 #define FCL_NARROWPHASE_DETAIL_SPHERESPHERE_INL_H
40 
42 
43 namespace fcl
44 {
45 
46 namespace detail
47 {
48 
49 //==============================================================================
50 extern template
51 FCL_EXPORT
52 bool sphereSphereIntersect(const Sphere<double>& s1, const Transform3<double>& tf1,
53  const Sphere<double>& s2, const Transform3<double>& tf2,
54  std::vector<ContactPoint<double>>* contacts);
55 
56 //==============================================================================
57 extern template
58 FCL_EXPORT
59 bool sphereSphereDistance(const Sphere<double>& s1, const Transform3<double>& tf1,
60  const Sphere<double>& s2, const Transform3<double>& tf2,
61  double* dist, Vector3<double>* p1, Vector3<double>* p2);
62 
63 //==============================================================================
64 template <typename S>
65 FCL_EXPORT
66 bool sphereSphereIntersect(const Sphere<S>& s1, const Transform3<S>& tf1,
67  const Sphere<S>& s2, const Transform3<S>& tf2,
68  std::vector<ContactPoint<S>>* contacts)
69 {
70  Vector3<S> diff = tf2.translation() - tf1.translation();
71  S len = diff.norm();
72  if(len > s1.radius + s2.radius)
73  return false;
74 
75  if(contacts)
76  {
77  // If the centers of two sphere are at the same position, the normal is (0, 0, 0).
78  // Otherwise, normal is pointing from center of object 1 to center of object 2
79  const Vector3<S> normal = len > 0 ? (diff / len).eval() : diff;
80  const Vector3<S> point = tf1.translation() + diff * s1.radius / (s1.radius + s2.radius);
81  const S penetration_depth = s1.radius + s2.radius - len;
82  contacts->emplace_back(normal, point, penetration_depth);
83  }
84 
85  return true;
86 }
87 
88 //==============================================================================
89 template <typename S>
90 FCL_EXPORT
91 bool sphereSphereDistance(const Sphere<S>& s1, const Transform3<S>& tf1,
92  const Sphere<S>& s2, const Transform3<S>& tf2,
93  S* dist, Vector3<S>* p1, Vector3<S>* p2)
94 {
95  Vector3<S> o1 = tf1.translation();
96  Vector3<S> o2 = tf2.translation();
97  Vector3<S> diff = o1 - o2;
98  S len = diff.norm();
99  if(len > s1.radius + s2.radius)
100  {
101  if(dist) *dist = len - (s1.radius + s2.radius);
102  if(p1) *p1 = (o1 - diff * (s1.radius / len));
103  if(p2) *p2 = (o2 + diff * (s2.radius / len));
104  return true;
105  }
106 
107  if(dist) *dist = -1;
108  return false;
109 }
110 
111 } // namespace detail
112 } // namespace fcl
113 
114 #endif
fcl::Sphere< double >
template class FCL_EXPORT Sphere< double >
fcl::Transform3
Eigen::Transform< S, 3, Eigen::Isometry > Transform3
Definition: types.h:91
fcl::ContactPoint
Minimal contact information returned by collision.
Definition: contact_point.h:48
fcl::detail::sphereSphereIntersect
template FCL_EXPORT bool sphereSphereIntersect(const Sphere< double > &s1, const Transform3< double > &tf1, const Sphere< double > &s2, const Transform3< double > &tf2, std::vector< ContactPoint< double >> *contacts)
fcl::Vector3
Eigen::Matrix< S, 3, 1 > Vector3
Definition: types.h:70
fcl::detail::sphereSphereDistance
template FCL_EXPORT bool sphereSphereDistance(const Sphere< double > &s1, const Transform3< double > &tf1, const Sphere< double > &s2, const Transform3< double > &tf2, double *dist, Vector3< double > *p1, Vector3< double > *p2)
fcl::Sphere::radius
S radius
Radius of the sphere.
Definition: sphere.h:60
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
Main namespace.
Definition: broadphase_bruteforce-inl.h:45
sphere_sphere.h


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