GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
Sphere.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_Sphere_hh_
31 #define _GranOO_LibShape_Sphere_hh_
32 
33 #include <iostream>
34 #include <string>
35 
36 #include "GranOO3/Shape/Volume.hpp"
37 
38 #include "GranOO3/Geom/Point.hpp"
39 
40 #include "GranOO3/Core/String.hpp"
42 
43 namespace GranOO3
44 {
45  namespace Shape
46  {
47 
48  class Sphere : public Volume,
49  public Core::ObjectFactoryInterface< Base, Sphere>
50  {
51 
52  GRANOO_OBJECT_FACTORY(Sphere);
53 
54  public:
55  static std::string class_ID() {return std::string("Sphere");}
56  static std::string desc();
57 
58  public:
59  //CONSTRUCTORS & DESTRUCTORS
60  Sphere();
61  Sphere(double radius);
62  Sphere(double radius, const Geom::Frame& frame);
63  Sphere(const Sphere &);
64  Sphere(const Sphere &, const Geom::Frame&);
65  Sphere(const Geom::Frame&);
66  virtual ~Sphere();
67 
68  //CLONING
69  void make_equal_to(const Sphere&);
70 
71  //IO
72  void read_xml_element(const TiXmlElement* el);
73  virtual std::ostream& write_ascii (std::ostream& out) const;
74  virtual std::istream& read_ascii (std::istream& in);
75  virtual void to_vtk(vtkUnstructuredGrid* data);
76  std::string info() const;
77 
78  //BOUNDING SHAPE
81  double get_greatest_dimension() const;
82  AABB aabb() const;
83 
84  //CONTACT DETECTION
86  Interference locate(const Geom::Point&, double radius) const;
87 
88  //CONTACT DETECTION v2
89  GRANOO_DECLARE_SHAPE_COLLISION();
90  bool collide_inside(const Geom::Point& p1, double radius, Collision::Data& info) const;
91  bool collide_outside(const Geom::Point& p1, double radius, Collision::Data& info) const;
93 
94  //USEFULL
95  double get_volume() const;
96  void compute_inertia_tensor(double density, Geom::Tensor &) const;
97  void draw_gl() const;
98  void scale(double);
99 
100  //ACCESSORS
101  GRANOO_ACCESS_SHAPE_DIM(radius, double, _radius);
102 
103  std::ostream& to_povray(std::ostream& out, const Core::Color& y) const;
104 
105  private:
106  Sphere & operator=(const Sphere &); // copy constructor forbidden
107 
108  //SERIALIZATION
110  template<class Archive> void serialize(Archive&, const unsigned int);
111 
112  private:
113  double _radius;
114  };
115 
116 
117  inline
118  Sphere::Sphere(double radius, const Geom::Frame& frame)
119  : Volume(frame),
120  _radius(radius) {
121  set_face_ID("radius");
122  SafeModeAssert(_radius>=0, "bad input value");
123  }
124 
125  inline
127  : Sphere(0., Geom::Frame::global) {
128  }
129 
130  inline
131  Sphere::Sphere(double radius)
132  : Sphere(radius, Geom::Frame::global) {
133  }
134 
135  inline
137  : Sphere(s._radius, s._frame) {
138  }
139 
140  inline
141  Sphere::Sphere(const Sphere &s, const Geom::Frame& frame)
142  : Sphere(s._radius, frame) {
143  }
144 
145  inline
147  : Sphere(0., frame) {
148  }
149 
150  inline double
152  return get_radius()*2.;
153  }
154 
155  inline
156  AABB Sphere::aabb() const {
157 
158  Geom::Point globalCentre(get_center(), local_frame().ref, Geom::Frame::global);
159  double minPoint[] = {globalCentre.x()-_radius, globalCentre.y()-_radius, globalCentre.z()-_radius};
160  double maxPoint[] = {globalCentre.x()+_radius, globalCentre.y()+_radius, globalCentre.z()+_radius};
161 
162  return StaticAABBTree::new_lonely_aabb(minPoint, maxPoint);
163  }
164 
165  inline double
167  return 4.*M_PI*pow(_radius,3)/3.;
168  }
169 
170  inline void
171  Sphere::compute_inertia_tensor(double density, Geom::Tensor &m) const {
172  m.clear();
173  const double i = 2.*density*get_volume()*_radius*_radius/5.;
174  m.xx() = m.yy() = m.zz() = i;
175  }
176 
177  template<class Archive>
178  inline void
179  Sphere::serialize(Archive& ar, const unsigned int v) {
180  ar & boost::serialization::base_object<Volume>(*this);
181  ar & _radius;
182  if(v >=1)
184  }
185 
186  }
187 }
188 
189 //Don't forget to export for dynamic serialization of child class
190 #include <boost/serialization/export.hpp>
192 
193 
194 #include <boost/serialization/version.hpp>
196 GRANOO_SERIALIZE_SHAPE(Sphere)
197 
198 
199 namespace GranOO3
200 {
201  extern template class Core::ObjectFactoryInterface<Shape::Base, Shape::Sphere>;
202 }
203 
204 
205 #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
const Geom::Point & get_center() const
Definition: Base.hpp:134
const Geom::Frame & local_frame() const
Definition: Base.hpp:133
Definition: Sphere.hpp:50
Interference locate(const Geom::Point &, double radius) const
std::string info() const
static std::string class_ID()
Definition: Sphere.hpp:55
double get_greatest_dimension() const
Definition: Sphere.hpp:151
Geom::Point farthest_point_along(const Geom::Vector &) const
Interference locate(const Geom::Point &) const
Sphere & operator=(const Sphere &)
AABB aabb() const
Definition: Sphere.hpp:156
double _radius
Definition: Sphere.hpp:113
static std::string desc()
void compute_inertia_tensor(double density, Geom::Tensor &) const
Definition: Sphere.hpp:171
double get_volume() const
Definition: Sphere.hpp:166
void read_xml_element(const TiXmlElement *el)
std::ostream & to_povray(std::ostream &out, const Core::Color &y) const
void serialize(Archive &, const unsigned int)
Definition: Sphere.hpp:179
virtual std::istream & read_ascii(std::istream &in)
bool collide_outside(const Geom::Point &p1, double radius, Collision::Data &info) const
bool collide_inside(const Geom::Point &p1, double radius, Collision::Data &info) const
friend class boost::serialization::access
Definition: Sphere.hpp:109
void make_equal_to(const Sphere &)
Sphere()
Definition: Sphere.hpp:126
virtual void to_vtk(vtkUnstructuredGrid *data)
virtual std::ostream & write_ascii(std::ostream &out) const
Definition: Volume.hpp:103
void update_bounding_shape()
Definition: Volume.cpp:96
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 pow(const T v0, const T v1)
Definition: Exprtk.hpp:1491
static std::string data()
Definition: Exprtk.hpp:44228
Definition: StaticAABBTree.hpp:38
Definition: Data.hpp:43