GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
Cylinder.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 #ifndef _GranOO_LibShape_Cylinder_hh_
30 #define _GranOO_LibShape_Cylinder_hh_
31 
32 #include <iostream>
33 #include <string>
34 
35 #include <vtkUnstructuredGrid.h>
36 
37 #include "GranOO3/Geom/Point.hpp"
38 #include "GranOO3/Shape/Volume.hpp"
39 #include "GranOO3/Shape/Sphere.hpp"
40 
41 #include "GranOO3/Core/String.hpp"
43 
44 
45 namespace GranOO3
46 {
47  namespace Shape
48  {
49 
50 
51  class Cylinder : public Volume,
52  public Core::ObjectFactoryInterface< Base, Cylinder>
53  {
54 
55  GRANOO_OBJECT_FACTORY(Cylinder);
56 
57  private:
58  enum ContactRegion {XMIN = 1 << 0, RADIUS = 1 << 1, XMAX = 1 << 2};
59 
60  public:
61  static std::string class_ID() {return std::string("Cylinder");}
62  static std::string desc();
63  static void draw_gl(double radius, double length, double xshift=0., bool draw_end_disk = true);
64 
65  public:
66  //CONSTRUCTORS & DESTRUCTORS
67  Cylinder(double radius = 0., double length = 0., const Geom::Frame& frame = Geom::Frame::global);
68  Cylinder(const Cylinder &);
69  Cylinder(const Cylinder &, const Geom::Frame& frame);
70  Cylinder(const Geom::Frame& frame);
71  virtual ~Cylinder();
72 
73  //CLONING
74  void make_equal_to(const Cylinder&);
75 
76  //IO
77  void read_xml_element(const TiXmlElement* el);
78  virtual std::ostream& write_ascii (std::ostream& out) const;
79  virtual std::istream& read_ascii (std::istream& in);
80  virtual void to_vtk(vtkUnstructuredGrid* data);
81 
82  std::string info() const;
83 
84  //BOUNDING SHAPE
87  double get_greatest_dimension() const;
88  AABB aabb() const;
89 
90  //CONTACT DETECTION
92  Interference locate(const Geom::Point&, double radius) const;
93  void set_contact_regions(const std::string &regions);
94 
95  //CONTACT DETECTION v2
96  GRANOO_DECLARE_SHAPE_COLLISION();
97  bool collide_inside(const Geom::Point& p1, double radius, Collision::Data& info) const;
98  bool collide_outside(const Geom::Point& p1, double radius, Collision::Data& info) const;
99 
100  //USEFULL
101  double get_volume() const;
102  void compute_inertia_tensor(double density, Geom::Tensor &) const;
104  void draw_gl() const;
105  void scale(double);
106 
107 
108  //ACCESSORS
109  GRANOO_ACCESS_SHAPE_DIM(radius, double, _radius);
110  GRANOO_ACCESS_SHAPE_DIM(length, double, _length);
111 
112  std::ostream& to_povray (std::ostream& out, const Core::Color&) const;
113 
114  private:
115  Cylinder & operator=(const Cylinder &); // copy constructor forbidden
116 
117  //SERIALIZATION
119  template<class Archive> void serialize(Archive&, const unsigned int);
120 
121  private:
122  double _radius;
123  double _length;
125  };
126 
127  inline
128  Cylinder::Cylinder(double radius, double length, const Geom::Frame& frame)
129  : Volume(frame),
130  _radius(radius),
131  _length(length),
132  _cRegion(ContactRegion(XMIN | RADIUS | XMAX)) {
133  set_face_ID("xMin", "xMax", "radius");
134  SafeModeAssert(_radius>=0, "bad input value");
135  SafeModeAssert(_length>=0, "bad input value");
136  }
137 
138  inline
140  : Cylinder(s._radius, s._length, s._frame) {
141  }
142 
143  inline
144  Cylinder::Cylinder(const Cylinder& s, const Geom::Frame& frame)
145  : Cylinder(s._radius, s._length, frame) {
146  }
147 
148  inline
150  : Cylinder(0., 0., frame) {
151  }
152 
153  inline double
155  return M_PI*_radius*_radius*_length;
156  }
157 
158  inline double
160  double max = get_radius()*2.;
161  if (get_length() > max)
162  max = get_length();
163 
164  return max;
165  }
166 
167  inline
169 
170  double xMin = farthest_point_along(Geom::Vector(-1, 0, 0)).x();
171  double xMax = farthest_point_along(Geom::Vector( 1, 0, 0)).x();
172  double yMin = farthest_point_along(Geom::Vector( 0, -1, 0)).y();
173  double yMax = farthest_point_along(Geom::Vector( 0, 1, 0)).y();
174  double zMin = farthest_point_along(Geom::Vector( 0, 0, -1)).z();
175  double zMax = farthest_point_along(Geom::Vector( 0, 0, 1)).z();
176 
177  double minPoint[] = { xMin, yMin, zMin };
178  double maxPoint[] = { xMax, yMax, zMax };
179 
180  return StaticAABBTree::new_lonely_aabb(minPoint, maxPoint);
181  }
182 
183  inline void
184  Cylinder::compute_inertia_tensor(double density, Geom::Tensor &m) const {
185  m.clear();
186  const double xx = density*get_volume()*_radius*_radius/2.;
187  const double yy_zz = density*get_volume()*(_radius*_radius/4. + _length*_length/12.);
188  m.xx() = xx;
189  m.yy() = m.zz() = yy_zz;
190  }
191 
192  template<class Archive>
193  inline void
194  Cylinder::serialize(Archive& ar, const unsigned int ) {
195  ar & boost::serialization::base_object<Volume>(*this);
196  ar & _radius;
197  ar & _length;
198  ar & _cRegion;
199  }
200 
201  }
202 }
203 
204 //Don't forget to export for dynamic serialization of child class
205 #include <boost/serialization/export.hpp>
207 
208 #include <boost/serialization/version.hpp>
210 GRANOO_SERIALIZE_SHAPE(Cylinder)
211 
212 namespace GranOO3
213 {
214  extern template class Core::ObjectFactoryInterface<Shape::Base, Shape::Cylinder>;
215 }
216 
217 
218 
219 #endif
BOOST_CLASS_EXPORT_KEY(GranOO3::Shape::Box) namespace GranOO3
Definition: Box.hpp:319
BOOST_CLASS_VERSION(GranOO3::Core::Base, 1) namespace GranOO3
Definition: Base.hpp:277
#define SafeModeAssert(condition, message)
Definition: Macro.hpp:47
Definition: Color.hpp:44
Definition: ObjectFactory.hpp:235
Definition: Frame.hpp:68
static const Frame & global
Definition: Frame.hpp:76
Definition: Point.hpp:62
Definition: Tensor.hpp:62
Definition: Vector.hpp:75
Definition: Cylinder.hpp:53
virtual std::istream & read_ascii(std::istream &in)
virtual void to_vtk(vtkUnstructuredGrid *data)
void make_equal_to(const Cylinder &)
Cylinder(double radius=0., double length=0., const Geom::Frame &frame=Geom::Frame::global)
Definition: Cylinder.hpp:128
void read_xml_element(const TiXmlElement *el)
Interference locate(const Geom::Point &, double radius) const
Cylinder & operator=(const Cylinder &)
ContactRegion
Definition: Cylinder.hpp:58
@ XMAX
Definition: Cylinder.hpp:58
@ RADIUS
Definition: Cylinder.hpp:58
@ XMIN
Definition: Cylinder.hpp:58
double get_greatest_dimension() const
Definition: Cylinder.hpp:159
static std::string desc()
std::string info() const
double _radius
Definition: Cylinder.hpp:122
void set_contact_regions(const std::string &regions)
Geom::Point farthest_point_along(const Geom::Vector &) const
std::ostream & to_povray(std::ostream &out, const Core::Color &) const
bool collide_inside(const Geom::Point &p1, double radius, Collision::Data &info) const
double _length
Definition: Cylinder.hpp:123
AABB aabb() const
Definition: Cylinder.hpp:168
void compute_inertia_tensor(double density, Geom::Tensor &) const
Definition: Cylinder.hpp:184
void serialize(Archive &, const unsigned int)
Definition: Cylinder.hpp:194
Interference locate(const Geom::Point &) const
static void draw_gl(double radius, double length, double xshift=0., bool draw_end_disk=true)
friend class boost::serialization::access
Definition: Cylinder.hpp:118
static std::string class_ID()
Definition: Cylinder.hpp:61
bool collide_outside(const Geom::Point &p1, double radius, Collision::Data &info) const
double get_volume() const
Definition: Cylinder.hpp:154
virtual std::ostream & write_ascii(std::ostream &out) const
ContactRegion _cRegion
Definition: Cylinder.hpp:124
Definition: Volume.hpp:103
void set_face_ID(const T &, Args... args)
Definition: Volume.hpp:185
static AABB new_lonely_aabb(double minPoint[3], double maxPoint[3], unsigned int primtiveIndex=-1)
Creates a new 'lonely' AABB with given extreme points.
Definition: StaticAABBTree.cpp:415
Interference
Definition: Volume.hpp:100
Definition: Common.hpp:198
T max(const T v0, const T v1)
Definition: Exprtk.hpp:1463
static std::string data()
Definition: Exprtk.hpp:44228
Definition: StaticAABBTree.hpp:38
Definition: Data.hpp:43