GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
GJK.hpp
Go to the documentation of this file.
1 // This file is part of GranOO, a workbench for DEM simulation.
2 //
3 // Author(s) : - Damien Andre IRCER/UNILIM, Limoges France
4 // <damien.andre@unilim.fr>
5 // - Jean-luc Charles Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
6 // <jean-luc.charles@ensam.eu>
7 // - Jeremie Girardot Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
8 // <jeremie.girardot@ensam.eu>
9 // - Cedric Hubert LAMIH/UPHF, Valenciennes France
10 // <cedric.hubert@uphf.fr>
11 // - Ivan Iordanoff Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
12 // <ivan.iordanoff@ensam.eu>
13 //
14 // Copyright (C) 2008-2019 D. Andre, JL. Charles, J. Girardot, C. Hubert, I. Iordanoff
15 //
16 // This program is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 // This program is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 // GNU General Public License for more details.
25 //
26 // You should have received a copy of the GNU General Public License
27 // along with this program. If not, see <http://www.gnu.org/licenses/>.
28 
29 
30 
31 
32 #ifndef _LibCollision_GJK_H
33 #define _LibCollision_GJK_H
34 
35 #include <array>
36 
37 #include "GranOO3/Geom/Vector.hpp"
40 
41 namespace GranOO3
42 {
43 
44  namespace Shape {class Volume;}
45 
46  namespace Collision
47  {
48 
49  namespace GJK
50  {
51 
52 
53  // The SupportPoint struct help us to store points
54  // in the minkowsky space and world space
55  struct SupportPoint
56  {
57  SupportPoint(const Geom::Vector& v1, const Geom::Vector& v2);
58  SupportPoint();
59  bool operator==(const SupportPoint&) const;
60 
61  //public data
62  Geom::Vector v; // the point in the minkowsky space
63  std::array<Geom::Vector, 2> local; // local points shape1 and shape2 in world space
64 
65  };
66 
67  // inlined definitions
68  inline
70  : v(v1-v2), local({{v1,v2}}) {
71  }
72 
73  inline
75  : v(), local() {
76  }
77 
78  inline
79  bool SupportPoint::operator==(const SupportPoint &r) const {
80  return v == r.v;
81  }
82 
83  struct Simplex
84  {
85  public:
86  Simplex();
87  void clear();
88  void set(const SupportPoint& a, const SupportPoint& b, const SupportPoint& c, const SupportPoint& d);
89  void set(const SupportPoint& a, const SupportPoint& b, const SupportPoint& c);
90  void set(const SupportPoint& a, const SupportPoint& b);
91  void set(const SupportPoint& a);
92 
93  void Push(const SupportPoint& p);
94 
95  //public data
96  int num;
101 
102  private:
104 
105  };
106 
107  inline
109  a(point[0]), b(point[1]), c(point[2]), d(point[3]) {
110  clear();
111  }
112 
113  inline void
115  num = 0;
116  }
117 
118  inline void
119  Simplex::set(const SupportPoint& a, const SupportPoint& b, const SupportPoint& c, const SupportPoint& d) {
120  num = 4;
121  this->a = a;
122  this->b = b;
123  this->c = c;
124  this->d = d;
125  }
126 
127  inline void
128  Simplex::set(const SupportPoint& a, const SupportPoint& b, const SupportPoint& c) {
129  num = 3;
130  this->a = a;
131  this->b = b;
132  this->c = c;
133  }
134 
135  inline void
136  Simplex::set(const SupportPoint& a, const SupportPoint& b) {
137  num = 2;
138  this->a = a;
139  this->b = b;
140  }
141 
142  inline void
144  num = 1;
145  this->a = a;
146  }
147 
148  inline void
150  num = (std::min)(num+1,4);
151  for (int i = num-1; i > 0; i--)
152  point[i] = point[i-1];
153 
154  point[0] = p;
155  }
156 
157  } // End of EPA namespace
158 
159 
160  // The GJK algoritm : it allows to detect if two convex shapes are colliding
162  {
163  public:
164  static unsigned int max_iter;
168 
169  public:
170  static void random_direction(Geom::Vector&);
171 
172  public:
173  GJK_Algorithm(const Shape::Volume& v1, const Shape::Volume& v2);
174  ~GJK_Algorithm();
175 
176  bool is_shape_colliding();
177 
178  GJK::SupportPoint support(const Geom::Vector&) const;
179 
180  private:
181  bool is_support_direction_valid(const GJK::SupportPoint& s, const Geom::Vector& dir) const;
182 
183  public:
187  };
188 
189  }
190 }
191 
192 
193 #endif
Definition: GJK.hpp:162
bool is_shape_colliding()
Definition: GJK.cpp:75
static Math::UniRandom rand_dir_x
Definition: GJK.hpp:165
static Math::UniRandom rand_dir_z
Definition: GJK.hpp:167
bool is_support_direction_valid(const GJK::SupportPoint &s, const Geom::Vector &dir) const
Definition: GJK.cpp:57
static void random_direction(Geom::Vector &)
Definition: GJK.cpp:49
static unsigned int max_iter
Definition: GJK.hpp:164
GJK::Simplex sim
Definition: GJK.hpp:186
const Shape::Volume & shape2
Definition: GJK.hpp:185
~GJK_Algorithm()
Definition: GJK.cpp:68
const Shape::Volume & shape1
Definition: GJK.hpp:184
static Math::UniRandom rand_dir_y
Definition: GJK.hpp:166
GJK_Algorithm(const Shape::Volume &v1, const Shape::Volume &v2)
Definition: GJK.cpp:63
GJK::SupportPoint support(const Geom::Vector &) const
Definition: GJK.cpp:251
Definition: Vector.hpp:75
Definition: UniRandom.hpp:45
Definition: Volume.hpp:103
Definition: Common.hpp:198
T min(const T v0, const T v1)
Definition: Exprtk.hpp:1456
Definition: GJK.hpp:84
SupportPoint & d
Definition: GJK.hpp:100
void Push(const SupportPoint &p)
Definition: GJK.hpp:149
void set(const SupportPoint &a, const SupportPoint &b, const SupportPoint &c, const SupportPoint &d)
Definition: GJK.hpp:119
SupportPoint & b
Definition: GJK.hpp:98
void clear()
Definition: GJK.hpp:114
SupportPoint & c
Definition: GJK.hpp:99
int num
Definition: GJK.hpp:96
Simplex()
Definition: GJK.hpp:108
SupportPoint point[4]
Definition: GJK.hpp:103
SupportPoint & a
Definition: GJK.hpp:97
bool operator==(const SupportPoint &) const
Definition: GJK.hpp:79
Geom::Vector v
Definition: GJK.hpp:62
SupportPoint()
Definition: GJK.hpp:74
std::array< Geom::Vector, 2 > local
Definition: GJK.hpp:63