FCL  0.6.0
Flexible Collision Library
gjk.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_GJK_H
39 #define FCL_NARROWPHASE_DETAIL_GJK_H
40 
41 #include "fcl/common/types.h"
42 #include "fcl/narrowphase/detail/convexity_based_algorithm/minkowski_diff.h"
43 
44 namespace fcl
45 {
46 
47 namespace detail
48 {
49 
51 template <typename S>
52 struct GJK
53 {
54  struct SimplexV
55  {
57  Vector3<S> d;
59  Vector3<S> w;
60  };
61 
62  struct Simplex
63  {
65  SimplexV* c[4];
67  S p[4];
69  size_t rank;
70 
71  Simplex();
72  };
73 
74  enum Status {Valid, Inside, Failed};
75 
76  MinkowskiDiff<S> shape;
77  Vector3<S> ray;
78  S distance;
79  Simplex simplices[2];
80 
81  GJK(unsigned int max_iterations_, S tolerance_);
82 
83  void initialize();
84 
86  Status evaluate(const MinkowskiDiff<S>& shape_, const Vector3<S>& guess);
87 
89  void getSupport(const Vector3<S>& d, SimplexV& sv) const;
90 
92  void getSupport(const Vector3<S>& d, const Vector3<S>& v, SimplexV& sv) const;
93 
95  void removeVertex(Simplex& simplex);
96 
98  void appendVertex(Simplex& simplex, const Vector3<S>& v);
99 
101  bool encloseOrigin();
102 
104  Simplex* getSimplex() const;
105 
107  Vector3<S> getGuessFromSimplex() const;
108 
109 private:
110  SimplexV store_v[4];
111  SimplexV* free_v[4];
112  size_t nfree;
113  size_t current;
114  Simplex* simplex;
115  Status status;
116 
117  unsigned int max_iterations;
118  S tolerance;
119 
120 };
121 
122 using GJKf = GJK<float>;
123 using GJKd = GJK<double>;
124 
125 } // namespace detail
126 } // namespace fcl
127 
128 #include "fcl/narrowphase/detail/convexity_based_algorithm/gjk-inl.h"
129 
130 #endif
Main namespace.
Definition: broadphase_bruteforce-inl.h:45
Minkowski difference class of two shapes.
Definition: minkowski_diff.h:58
bool encloseOrigin()
whether the simplex enclose the origin
Definition: gjk-inl.h:236
void getSupport(const Vector3< S > &d, SimplexV &sv) const
apply the support function along a direction, the result is return in sv
Definition: gjk-inl.h:204
Vector3< S > getGuessFromSimplex() const
get the guess from current simplex
Definition: gjk-inl.h:75
Definition: gjk.h:62
size_t rank
size of simplex (number of vertices)
Definition: gjk.h:69
void appendVertex(Simplex &simplex, const Vector3< S > &v)
append one vertex to the simplex
Definition: gjk-inl.h:227
Definition: gjk.h:54
Vector3< S > d
support direction
Definition: gjk.h:57
Status evaluate(const MinkowskiDiff< S > &shape_, const Vector3< S > &guess)
GJK algorithm, given the initial value guess.
Definition: gjk-inl.h:82
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
Simplex * getSimplex() const
get the underlying simplex using in GJK, can be used for cache in next iteration
Definition: gjk-inl.h:302
class for GJK algorithm
Definition: gjk.h:52
Vector3< S > w
support vector (i.e., the furthest point on the shape along the support direction) ...
Definition: gjk.h:59
void removeVertex(Simplex &simplex)
discard one vertex from the simplex
Definition: gjk-inl.h:220