GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
Box.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_Box_hh_
31 #define _GranOO_LibShape_Box_hh_
32 
33 #include <iostream>
34 #include <string>
35 
36 #include <vtkUnstructuredGrid.h>
37 
38 #include "GranOO3/Shape/Volume.hpp"
39 #include "GranOO3/Geom/Point.hpp"
40 
41 #include "GranOO3/Core/String.hpp"
43 
44 namespace GranOO3
45 {
46  namespace Shape
47  {
48 
49  class Sphere;
50  class Box;
51 
52 
53 
54 
55  class Box : public Volume,
56  public Core::ObjectFactoryInterface< Base, Box>
57  {
58 
59  GRANOO_OBJECT_FACTORY(Box);
60 
61  public:
62  static std::string class_ID() {return std::string("Box");}
63  static std::string desc();
64 
65  public:
66  //CONSTRUCTORS & DESTRUCTORS
67  Box();
68  Box(double dimX, double dimY, double dimZ, const Geom::Frame& frame);
69  Box(const Box &);
70  Box(const Box &, const Geom::Frame&);
71  Box(const Geom::Frame&);
72  virtual ~Box();
73 
74  //IO
75  void read_xml_element(const TiXmlElement* el);
76  virtual std::ostream& write_ascii (std::ostream& out) const;
77  virtual std::istream& read_ascii (std::istream& in);
78  virtual void to_vtk(vtkUnstructuredGrid* data);
79  std::string info() const;
80 
81  //CLONING
82  void make_equal_to(const Box&);
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 
94  //CONTACT DETECTION v2
95  GRANOO_DECLARE_SHAPE_COLLISION();
96  bool collide_inside(const Geom::Point& p1, double radius, Collision::Data& info) const;
97  bool collide_outside(const Geom::Point& p1, double radius, Collision::Data& info) const;
99 
100 
101  //USEFULL
102  double get_volume() const {
103  return _lx*_ly*_lz;
104  }
105 
106  void compute_inertia_tensor(double, Geom::Tensor &) const;
107  void draw_gl() const;
108  void scale(double);
109 
110 
111  //ACCESSORS
112  void set_length(double x, double y, double z);
113  GRANOO_ACCESS_SHAPE_DIM(lx, double, _lx);
114  GRANOO_ACCESS_SHAPE_DIM(ly, double, _ly);
115  GRANOO_ACCESS_SHAPE_DIM(lz, double, _lz);
116 
123 
124  Geom::Point get_point1() const;
125  Geom::Point get_point2() const;
126  Geom::Point get_point3() const;
127  Geom::Point get_point4() const;
128  Geom::Point get_point5() const;
129  Geom::Point get_point6() const;
130  Geom::Point get_point7() const;
131  Geom::Point get_point8() const;
132 
133  std::ostream& to_povray(std::ostream& out, const Core::Color& ) const;
134 
135  private:
136  Box & operator=(const Box &); //Not allowed
137 
138  //SERIALIZATION
140  template<class Archive> void serialize(Archive&, const unsigned int);
141 
142  private:
143  // Data
144  double _lx;
145  double _ly;
146  double _lz;
147  };
148 
149 
150  inline
151  Box::Box(double dimX, double dimY, double dimZ, const Geom::Frame& frame)
152  : Volume(frame),
153  _lx(dimX),
154  _ly(dimY),
155  _lz(dimZ) {
156  set_face_ID("xMin", "xMax", "yMin", "yMax", "zMin", "zMax");
157  SafeModeAssert(_lx >= 0., "bad input value");
158  SafeModeAssert(_ly >= 0., "bad input value");
159  SafeModeAssert(_lz >= 0., "bad input value");
160 
161  }
162 
163  inline
165  : Box(0., 0., 0., Geom::Frame::global) {
166 
167  }
168 
169  inline
170  Box::Box(const Box &b)
171  : Box(b._lx, b._ly, b._lz, b._frame) {
172  }
173 
174  inline
175  Box::Box(const Box &b, const Geom::Frame& frame)
176  : Box(b._lx, b._ly, b._lz, frame) {
177  }
178 
179  inline
180  Box::Box(const Geom::Frame& frame)
181  : Box(0., 0., 0., frame) {
182  }
183 
184  inline void
185  Box::set_length(double x, double y, double z) {
186  SafeModeAssert(x>=0. && y>=0. && z>=0., "The value must be positive");
187 
188  _lx = x;
189  _ly = y;
190  _lz = z;
191  if (is_bounding_shape()==false)
193  }
194 
195  inline double
197  double max = get_lx();
198  if (get_ly() > max)
199  max = get_ly();
200  else if (get_lz() > max)
201  max = get_lz();
202 
203  return max;
204  }
205 
206  inline
207  AABB Box::aabb() const {
208 
209  double xMin = farthest_point_along(Geom::Vector(-1, 0, 0)).x();
210  double xMax = farthest_point_along(Geom::Vector( 1, 0, 0)).x();
211  double yMin = farthest_point_along(Geom::Vector( 0, -1, 0)).y();
212  double yMax = farthest_point_along(Geom::Vector( 0, 1, 0)).y();
213  double zMin = farthest_point_along(Geom::Vector( 0, 0, -1)).z();
214  double zMax = farthest_point_along(Geom::Vector( 0, 0, 1)).z();
215 
216  double minPoint[] = { xMin, yMin, zMin };
217  double maxPoint[] = { xMax, yMax, zMax };
218 
219  return StaticAABBTree::new_lonely_aabb(minPoint, maxPoint);
220  }
221 
222  inline void
223  Box::compute_inertia_tensor(double density, Geom::Tensor & m) const {
224  m.clear();
225  const double mass = density*get_volume();
226  m.xx() = mass*(get_ly()*get_ly()+get_lz()*get_lz())/12.;
227  m.yy() = mass*(get_lx()*get_lx()+get_lz()*get_lz())/12.;
228  m.zz() = mass*(get_lx()*get_lx()+get_ly()*get_ly())/12.;
229  }
230 
231  inline Geom::Point
232  Box::get_xmax_center(const Geom::Frame& frame) const {
233  Geom::Point p(get_lx()/2., 0., 0.);
234  return Geom::Point (p, local_frame(), frame);
235  }
236 
237  inline Geom::Point Box::get_xmin_center(const Geom::Frame& frame) const {
238  Geom::Point p(-get_lx()/2., 0., 0.);
239  return Geom::Point (p, local_frame(), frame);
240  }
241 
242  inline Geom::Point
243  Box::get_ymax_center(const Geom::Frame& frame) const {
244  Geom::Point p(0., get_ly()/2., 0.);
245  return Geom::Point (p, local_frame(), frame);
246  }
247 
248  inline Geom::Point
249  Box::get_ymin_center(const Geom::Frame& frame) const {
250  Geom::Point p(0., -get_ly()/2., 0.);
251  return Geom::Point (p, local_frame(), frame);
252  }
253 
254  inline Geom::Point
255  Box::get_zmax_center(const Geom::Frame& frame) const {
256  Geom::Point p(0., 0., get_lz()/2.);
257  return Geom::Point (p, local_frame(), frame);
258  }
259 
260  inline Geom::Point
261  Box::get_zmin_center(const Geom::Frame& frame) const {
262  Geom::Point p(0., 0., -get_lz()/2.);
263  return Geom::Point (p, local_frame(), frame);
264  }
265 
266  inline Geom::Point
267  Box::get_point1() const {
268  return Geom::Point(-_lx/2.,-_ly/2.,-_lz/2.);
269  }
270 
271  inline Geom::Point
272  Box::get_point2() const {
273  return Geom::Point(_lx/2.,-_ly/2.,-_lz/2.);
274  }
275 
276  inline Geom::Point
277  Box::get_point3() const {
278  return Geom::Point(_lx/2.,_ly/2.,-_lz/2.);
279  }
280 
281  inline Geom::Point
282  Box::get_point4() const {
283  return Geom::Point(-_lx/2.,_ly/2.,-_lz/2.);
284  }
285 
286  inline Geom::Point
287  Box::get_point5() const {
288  return Geom::Point(-_lx/2.,-_ly/2.,_lz/2.);
289  }
290 
291  inline Geom::Point
292  Box::get_point6() const {
293  return Geom::Point(_lx/2.,-_ly/2.,_lz/2.);
294  }
295 
296  inline Geom::Point
297  Box::get_point7() const {
298  return Geom::Point(_lx/2.,_ly/2.,_lz/2.);
299  }
300 
301  inline Geom::Point
302  Box::get_point8() const {
303  return Geom::Point(-_lx/2.,_ly/2.,_lz/2.);
304  }
305 
306  template<class Archive>
307  inline void
308  Box::serialize(Archive & ar, const unsigned int ) {
309  ar & boost::serialization::base_object<Volume>(*this);
310  ar & _lx;
311  ar & _ly;
312  ar & _lz;
313  }
314 
315  }
316 }
317 //Don't forget to export for dynamic serialization of child class
318 #include <boost/serialization/export.hpp>
320 
321 
322 
323 namespace GranOO3
324 {
325  extern template class Core::ObjectFactoryInterface<Shape::Base, Shape::Box>;
326 }
327 
328 
329 
330 #endif
BOOST_CLASS_EXPORT_KEY(GranOO3::Shape::Box) namespace GranOO3
Definition: Box.hpp:319
#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::Frame & local_frame() const
Definition: Base.hpp:133
Definition: Box.hpp:57
double _lz
Definition: Box.hpp:146
Geom::Point get_zmax_center(const Geom::Frame &frame=Geom::Frame::global) const
Definition: Box.hpp:255
double _ly
Definition: Box.hpp:145
void read_xml_element(const TiXmlElement *el)
static std::string class_ID()
Definition: Box.hpp:62
Geom::Point get_zmin_center(const Geom::Frame &frame=Geom::Frame::global) const
Definition: Box.hpp:261
Geom::Point get_ymin_center(const Geom::Frame &frame=Geom::Frame::global) const
Definition: Box.hpp:249
Geom::Point get_point3() const
Definition: Box.hpp:277
Geom::Point get_point8() const
Definition: Box.hpp:302
Geom::Point get_point4() const
Definition: Box.hpp:282
void scale(double)
Geom::Point get_point2() const
Definition: Box.hpp:272
Geom::Point get_xmax_center(const Geom::Frame &frame=Geom::Frame::global) const
Definition: Box.hpp:232
virtual void to_vtk(vtkUnstructuredGrid *data)
void update_bounding_sphere()
double _lx
Definition: Box.hpp:144
double get_volume() const
Definition: Box.hpp:102
static std::string desc()
std::ostream & to_povray(std::ostream &out, const Core::Color &) const
Geom::Point get_point6() const
Definition: Box.hpp:292
Geom::Point farthest_point_along(const Geom::Vector &) const
Box()
Definition: Box.hpp:164
std::string info() const
Interference locate(const Geom::Point &, double radius) const
void draw_gl() const
virtual std::istream & read_ascii(std::istream &in)
Geom::Point get_point1() const
Definition: Box.hpp:267
double get_greatest_dimension() const
Definition: Box.hpp:196
Geom::Point get_point7() const
Definition: Box.hpp:297
Interference locate(const Geom::Point &) const
bool collide_inside(const Geom::Point &p1, double radius, Collision::Data &info) const
Geom::Point get_point5() const
Definition: Box.hpp:287
void compute_inertia_tensor(double, Geom::Tensor &) const
Definition: Box.hpp:223
friend class boost::serialization::access
Definition: Box.hpp:139
Geom::Point get_xmin_center(const Geom::Frame &frame=Geom::Frame::global) const
Definition: Box.hpp:237
bool collide_outside(const Geom::Point &p1, double radius, Collision::Data &info) const
AABB aabb() const
Definition: Box.hpp:207
Box & operator=(const Box &)
virtual std::ostream & write_ascii(std::ostream &out) const
void serialize(Archive &, const unsigned int)
Definition: Box.hpp:308
Geom::Point get_ymax_center(const Geom::Frame &frame=Geom::Frame::global) const
Definition: Box.hpp:243
void make_equal_to(const Box &)
void set_length(double x, double y, double z)
Definition: Box.hpp:185
Definition: Volume.hpp:103
void update_bounding_shape()
Definition: Volume.cpp:96
void set_face_ID(const T &, Args... args)
Definition: Volume.hpp:185
bool is_bounding_shape() const
Definition: Volume.hpp:206
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
x y * z
Definition: Exprtk.hpp:11139
x y t t *t x y t t t x y t t t x *y t *t t x *y t *t t x y t t t x y t t t x(y+z)
static std::string data()
Definition: Exprtk.hpp:44228
Definition: StaticAABBTree.hpp:38
Definition: Data.hpp:43