GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
Volume.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 #ifndef _GranOO_LibShape_Volume_hh_
31 #define _GranOO_LibShape_Volume_hh_
32 
33 #include <iostream>
34 #include <string>
35 #include <cmath>
36 #include <set>
37 
38 #include <boost/version.hpp>
39 
40 #if BOOST_VERSION >= 107400
41 #include <boost/serialization/library_version_type.hpp>
42 #endif
43 
44 #include <boost/serialization/serialization.hpp>
45 #include <boost/serialization/set.hpp>
46 
50 #include "GranOO3/Geom/Tensor.hpp"
51 #include "GranOO3/Geom/Point.hpp"
52 #include "GranOO3/Shape/Base.hpp"
53 #include "GranOO3/Core/String.hpp"
56 
57 // Macro the manage detection collision
58 // If a new volume shape class is added, these macro have to
59 // be addited
60 #define GRANOO_DECLARE_SHAPE_COLLISION() \
61  bool collide(const Volume&, Collision::Data& c) const; \
62  bool collide(const Sphere&, Collision::Data& c) const; \
63  bool collide(const Box&, Collision::Data& c) const; \
64  bool collide(const Cylinder&, Collision::Data& c) const; \
65  bool collide(const Cone&, Collision::Data& c) const; \
66  bool collide(const ConeTruncated&, Collision::Data& c) const; \
67  bool collide(const Polyhedron&, Collision::Data& c) const;
68 
69 #define GRANOO_IMPLEMENT_SHAPE_COLLISION(CLASS) \
70  bool CLASS::collide(const Volume& v, Collision::Data& c) const \
71  {return v.collide(*this, c);} \
72  bool CLASS::collide(const Sphere& v, Collision::Data& c) const \
73  {return Collision::Dispatcher::run(*this, v, c);} \
74  bool CLASS::collide(const Box& v, Collision::Data& c) const \
75  {return Collision::Dispatcher::run(*this, v, c);} \
76  bool CLASS::collide(const Cylinder& v, Collision::Data& c) const \
77  {return Collision::Dispatcher::run(*this, v, c);} \
78  bool CLASS::collide(const Cone& v, Collision::Data& c) const \
79  {return Collision::Dispatcher::run(*this, v, c);} \
80  bool CLASS::collide(const ConeTruncated& v, Collision::Data& c) const \
81  {return Collision::Dispatcher::run(*this, v, c);} \
82  bool CLASS::collide(const Polyhedron& v, Collision::Data& c) const \
83  {return Collision::Dispatcher::run(*this, v, c);}
84 
85 #define GRANOO_ACCESS_SHAPE_DIM(method, type, variable) \
86  void set_ ## method (const type& val) { \
87  SafeModeAssert(val >= 0., "The value must be positive"); \
88  variable = val; if (is_bounding_shape()==false) update_bounding_shape(); } \
89  GRANOO_ACCESS_GET (method, type, variable)\
90  GRANOO_ACCESS_REF (method, type, variable)
91 
92 
93 namespace GranOO3
94 {
95  namespace Shape
96  {
97 
98  class Sphere; class Box; class Cylinder; class Cone; class ConeTruncated; class Polyhedron;
99 
101 
102  class Volume : public Base
103  {
104  public:
105  static std::string class_ID() {return "Volume";}
106 
107  public:
108  //CONSTRUCTORS & DESTRUCTORS
109  explicit Volume(const Geom::Frame &frame);
110  virtual ~Volume();
111 
112  //USEFULL
113  virtual double get_volume() const = 0;
114  virtual void compute_inertia_tensor(double density, Geom::Tensor &) const = 0;
115  virtual Geom::Point farthest_point_along(const Geom::Vector& dir) const = 0;
116  bool is_surface() const {return false;}
117  bool is_volume() const {return true;}
118 
119  //BOUNDING SHAPE
120  const Box& get_bounding_box() const;
121  const Sphere& get_bounding_sphere() const;
124 
125  virtual double get_greatest_dimension() const = 0;
126  virtual void update_bounding_box() = 0;
127  virtual void update_bounding_sphere() = 0;
128  void update_bounding_shape(); // It updates both bounding box and shape
129  bool is_bounding_shape() const;
130  virtual AABB aabb() const = 0;
131 
132  //CONTACT DETECTION v1
133  template<Interference T>
134  bool overlap(const Sphere&, Geom::Vector& normal, double& penetration) const;
135  virtual Interference locate(const Geom::Point&) const = 0;
136  virtual Interference locate(const Geom::Point&, double radius) const = 0;
137  Interference locate(const Sphere&) const;
138 
139  //CONTACT DETECTION v2
140  virtual bool collide_inside (const Geom::Point& p1, double radius, Collision::Data& info) const = 0;
141  virtual bool collide_outside(const Geom::Point& p1, double radius, Collision::Data& info) const = 0;
142  bool collide_inside (const Geom::Point&, Collision::Data&) const;
143  bool collide_outside(const Geom::Point&, Collision::Data&) const;
144  virtual bool collide(const Volume&, Collision::Data&) const = 0;
145  virtual bool collide(const Sphere&, Collision::Data&) const = 0;
146  virtual bool collide(const Box&, Collision::Data&) const = 0;
147  virtual bool collide(const Cylinder&, Collision::Data&) const = 0;
148  virtual bool collide(const Cone&, Collision::Data&) const = 0;
149  virtual bool collide(const ConeTruncated&, Collision::Data&) const = 0;
150  virtual bool collide(const Polyhedron&, Collision::Data&) const = 0;
151 
152  GRANOO_ACCESS_GET(bounding_radius, double, _bounding_radius);
153 
154  //FACE & BOUNDARY MANAGEMENT
155  const std::set<std::string>& get_face_ID() const;
156 
157  protected:
158  template<typename T, typename ... Args> void set_face_ID(const T&, Args... args);
159  template<typename T> void set_face_ID(const T&);
160  void copy_face_ID(const std::set<std::string>&);
161  void add_face_ID(const std::string&);
162 
163  private:
164  Volume(const Volume &); // Copy constructor forbiden
165  Volume & operator=(const Volume &); // Assignement operator forbiden
166 
167  //SERIALIZATION
169  template<class Archive> void save(Archive&, const unsigned int ) const;
170  template<class Archive> void load(Archive&, const unsigned int);
172 
173  private:
178  std::set<std::string> _face;
179 
180  };
181 
182 
183  template<typename T, typename ... Args>
184  inline void
185  Volume::set_face_ID(const T& key, Args... args) {
186  const std::string k(key);
187  AssertMsg(_face.count(k)==0, "The face " + k + " was already registered");
188  _face.insert(k);
189  set_face_ID(args ...);
190  }
191 
192  template<typename T>
193  inline void
194  Volume::set_face_ID(const T& key) {
195  const std::string k(key);
196  AssertMsg(_face.count(k)==0, "The face " + k + " was already registered");
197  _face.insert(k);
198  }
199 
200  inline const std::set<std::string>&
202  return _face;
203  }
204 
205  inline bool
207  return _bounding_shape;
208  }
209 
210  inline void
211  Volume::add_face_ID(const std::string& name) {
212  AssertMsg(_face.count(name)==0, "The face " + name + " was already registered");
213  _face.insert(name);
214  }
215 
216  inline void
217  Volume::copy_face_ID(const std::set<std::string>& set) {
218  _face = set;
219  }
220 
221 
222  template<class Archive>
223  inline void
224  Volume::save(Archive& ar, const unsigned int) const {
225  ar << boost::serialization::base_object<Shape::Base>(*this);
226  ar << _face;
227  }
228 
229  template<class Archive>
230  inline void
231  Volume::load(Archive& ar, const unsigned int) {
232  ar >> boost::serialization::base_object<Shape::Base>(*this);
233  _face.clear();
234  ar >> _face;
235  }
236 
237  } // namespace Shape
238 }
239 
240 
241 #endif
#define AssertMsg(condition, message)
Definition: Macro.hpp:67
Definition: Frame.hpp:68
Definition: Point.hpp:62
Definition: Tensor.hpp:62
Definition: Vector.hpp:75
Definition: Base.hpp:97
virtual std::string info() const
Definition: Base.hpp:184
Definition: Box.hpp:57
Definition: Cone.hpp:48
Definition: ConeTruncated.hpp:47
Definition: Cylinder.hpp:53
Definition: Polyhedron.hpp:54
Definition: Sphere.hpp:50
Definition: Volume.hpp:103
virtual ~Volume()
Definition: Volume.cpp:48
void copy_face_ID(const std::set< std::string > &)
Definition: Volume.hpp:217
virtual bool collide(const Cone &, Collision::Data &) const =0
virtual bool collide_inside(const Geom::Point &p1, double radius, Collision::Data &info) const =0
virtual bool collide(const Polyhedron &, Collision::Data &) const =0
const Box & get_bounding_box() const
Definition: Volume.cpp:56
bool is_volume() const
Definition: Volume.hpp:117
virtual bool collide(const Sphere &, Collision::Data &) const =0
virtual bool collide_outside(const Geom::Point &p1, double radius, Collision::Data &info) const =0
virtual bool collide(const Box &, Collision::Data &) const =0
virtual AABB aabb() const =0
double _bounding_radius
Definition: Volume.hpp:177
virtual Geom::Point farthest_point_along(const Geom::Vector &dir) const =0
std::set< std::string > _face
Definition: Volume.hpp:178
Volume(const Geom::Frame &frame)
Definition: Volume.cpp:40
virtual bool collide(const ConeTruncated &, Collision::Data &) const =0
Volume(const Volume &)
Volume & operator=(const Volume &)
virtual void update_bounding_box()=0
Box * _bounding_box
Definition: Volume.hpp:174
void add_face_ID(const std::string &)
Definition: Volume.hpp:211
bool _bounding_shape
Definition: Volume.hpp:176
void load(Archive &, const unsigned int)
Definition: Volume.hpp:231
bool is_surface() const
Definition: Volume.hpp:116
void update_bounding_shape()
Definition: Volume.cpp:96
virtual double get_greatest_dimension() const =0
virtual Interference locate(const Geom::Point &, double radius) const =0
void save(Archive &, const unsigned int) const
Definition: Volume.hpp:224
virtual bool collide(const Cylinder &, Collision::Data &) const =0
const std::set< std::string > & get_face_ID() const
Definition: Volume.hpp:201
void set_face_ID(const T &, Args... args)
Definition: Volume.hpp:185
friend class boost::serialization::access
Definition: Volume.hpp:168
bool is_bounding_shape() const
Definition: Volume.hpp:206
virtual bool collide(const Volume &, Collision::Data &) const =0
static std::string class_ID()
Definition: Volume.hpp:105
virtual Interference locate(const Geom::Point &) const =0
const Sphere & get_bounding_sphere() const
Definition: Volume.cpp:67
virtual double get_volume() const =0
virtual void compute_inertia_tensor(double density, Geom::Tensor &) const =0
bool overlap(const Sphere &, Geom::Vector &normal, double &penetration) const
virtual void update_bounding_sphere()=0
Sphere * _bounding_sphere
Definition: Volume.hpp:175
Interference
Definition: Volume.hpp:100
@ OUTSIDE
Definition: Volume.hpp:100
@ INSIDE
Definition: Volume.hpp:100
@ OVERLAP
Definition: Volume.hpp:100
Definition: Common.hpp:198
Definition: StaticAABBTree.hpp:38
Definition: Data.hpp:43