GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
Point.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_libGeom_Point_hpp_
31 #define _GranOO_libGeom_Point_hpp_
32 
33 #include <iostream>
34 #include <string>
35 #include <vector>
36 #include <cmath>
37 #include "GranOO3/3rdParty/Eigen/Dense"
38 
39 #include <boost/archive/text_oarchive.hpp>
40 #include <boost/archive/text_iarchive.hpp>
41 
43 #include "GranOO3/Geom/Axis.hpp"
44 #include "GranOO3/Core/Macro.hpp"
45 
46 class TiXmlElement;
47 
48 namespace GranOO3
49 {
50  namespace Geom
51  {
52  class Point;
53  class Vector;
54  class Frame;
55 
56  bool operator==(const Point &, const Point &);
57  bool operator!=(const Point &, const Point &);
58  std::ostream& operator<<(std::ostream &, const Point &);
59 
60 
61  class Point
62  {
63 
64  friend class Vector;
65  friend bool operator==(const Point &, const Point &);
66  friend bool operator!=(const Point &, const Point &);
67  friend std::ostream& operator<<(std::ostream &, const Point &);
68 
69  public:
70  // Usefull for internal use only, NOT DOCUMENTED !
71  static Point& glob(const std::string& id);
72  static std::string class_ID() {return "Point";}
73  static Point* new_object(const TiXmlElement* el);
74  static const int N = 3;
75 
76  static const Point& global;
77  static const Point& null;
78 
79  static Point mid_point(const Point & p1, const Point & p2);
80 
81  public:
82  // Constructors and destructor
83  explicit Point();
84  Point(const Point &);
85  Point(const Point &, const Frame& from, const Frame& to);
86  explicit Point(double, double, double);
87  explicit Point(const double*);
88  ~Point();
89 
90  // Internal operators
91  Point & operator=(const Point &);
92 
93  // Miscellaneous Usefull methods
95  const Vector & to_vector() const;
96  double distance_with(const Point &) const;
97  void translate(const Vector& v);
98  void rotate(const Quaternion &q);
99 
100  // Component
101  double & x();
102  const double & x() const;
103  double & y();
104  const double & y() const;
105  double & z();
106  const double & z() const;
107 
108 
109  // Component (for python wrapper)
110  void set_x(const double&);
111  void set_y(const double&);
112  void set_z(const double&);
113 
114  // Component (for python wrapper)
115  double get_x() const;
116  double get_y() const;
117  double get_z() const;
118 
119 
120  template <typename Axis> double& val();
121  template <typename Axis> const double& val() const;
122  const double & operator() (unsigned int i) const;
123  double& operator() (unsigned int i);
124 
125  // Misc
126  bool is_nan() const;
127  std::string info() const;
128  void add_glob(const std::string& id);
129 
130  private:
131  // - BOOST SERIALIZATION - //
133  template<class Archive> void serialize(Archive&, const unsigned int );
134 
135  public:
136  Eigen::Matrix<double, 3, 1> coord;
137  };
138 #ifndef DOXYGEN_SHOULD_SKIP_THIS
139 
140 
141 
142  // - BOOST SERIALIZATION - //
143  template<class Archive>
144  void
145  Point::serialize(Archive & ar, const unsigned int ) {
146  ar & x(); ar & y(); ar & z();
147  }
148 
149  // Constructors ...
150  inline
151  Point::Point()
152  : coord() {
153  }
154 
155  inline
156  Point::Point(const Point &p)
157  : coord(p.coord) {
158  }
159 
160  inline
161  Point::Point(double x, double y, double z)
162  : coord(x, y, z){
163  }
164 
165  inline
166  Point::Point(const double* pt)
167  : coord(pt[0], pt[1], pt[2]){
168  }
169 
170  inline
171  Point::~Point() {
172  }
173 
174  // Accessors
175  inline Vector &
176  Point::to_vector() {
177  return *reinterpret_cast<Vector *>(this);
178  }
179 
180  inline const Vector &
181  Point::to_vector() const {
182  return *reinterpret_cast<const Vector *>(this);
183  }
184 
185 
186  // External Operators ...
187  inline
188  bool operator==(const Point & pt1, const Point & pt2) {
189  return (pt1.x() == pt2.x() && pt1.y() == pt2.y() && pt1.z() == pt2.z());
190  }
191 
192  inline
193  bool operator!=(const Point & pt1, const Point & pt2) {
194  return !(pt1==pt2);
195  }
196 
197  inline double&
198  Point::x() {
199  return coord(0);
200  }
201 
202  inline const double&
203  Point::x() const {
204  return coord(0);
205  }
206 
207  inline double&
208  Point::y() {
209  return coord(1);
210  }
211 
212  inline const double&
213  Point::y() const {
214  return coord(1);
215  }
216 
217  inline double&
218  Point::z() {
219  return coord(2);
220  }
221 
222  inline const double&
223  Point::z() const {
224  return coord(2);
225  }
226 
227  inline void
228  Point::set_x(const double& val) {
229  x() = val;
230  }
231 
232  inline void
233  Point::set_y(const double& val) {
234  y() = val;
235  }
236 
237  inline void
238  Point::set_z(const double& val) {
239  z() = val;
240  }
241 
242  inline double
243  Point::get_x() const {
244  return x();
245  }
246 
247  inline double
248  Point::get_y() const {
249  return y();
250  }
251 
252  inline double
253  Point::get_z() const {
254  return z();
255  }
256 
257  template<typename Axis> inline
258  double& Point::val() {
259  SafeModeAssert(0, "Non specilized method is forbidden");
260  return x();
261  }
262 
263  template<typename Axis> inline
264  const double& Point::val() const {
265  SafeModeAssert(0, "Non specilized method is forbidden");
266  return x();
267  }
268 
269  template<> inline
270  double& Point::val<X>() {
271  return x();
272  }
273 
274  template<> inline
275  const double& Point::val<X>() const {
276  return x();
277  }
278 
279  template<> inline
280  double& Point::val<Y>() {
281  return y();
282  }
283 
284  template<> inline
285  const double& Point::val<Y>() const {
286  return y();
287  }
288 
289  template<> inline
290  double& Point::val<Z>() {
291  return z();
292  }
293 
294  template<> inline
295  const double& Point::val<Z>() const {
296  return z();
297  }
298 
299  inline const double&
300  Point::operator() (unsigned int i) const {
301  SafeModeAssert(i < 3, "The component must be in [0, 2] range");
302  return coord(i);
303  }
304 
305  inline double&
306  Point::operator() (unsigned int i) {
307  SafeModeAssert(i < 3, "The component must be in [0, 2] range");
308  return coord(i);
309  }
310 
311  inline std::ostream &
312  operator<<(std::ostream& out, const Point& pt) {
313  return out << pt.x() << "\t" << pt.y() << "\t" << pt.z() <<"\t";
314  }
315 
316  inline Point &
317  Point::operator=(const Point & p) {
318  if (this == &p)
319  return *this;
320  coord = p.coord;
321  return *this;
322  }
323 
324  inline bool
325  Point::is_nan() const {
326  if (x()!=x())
327  return true;
328  if (y()!=y())
329  return true;
330  if (z()!=z())
331  return true;
332  return false;
333  }
334 
335  // Usefull Methods
336 
337  inline double
338  Point::distance_with(const Point & pt) const {
339  return sqrt( (x()-pt.x())*(x()-pt.x())+
340  (y()-pt.y())*(y()-pt.y())+
341  (z()-pt.z())*(z()-pt.z()));
342  }
343 
344  inline Point
345  Point::mid_point(const Point & p1, const Point & p2) {
346  return Point(0.5*(p1.x()+p2.x()),
347  0.5*(p1.y()+p2.y()),
348  0.5*(p1.z()+p2.z()));
349  }
350 
351 #endif
352  } // namespace Geom
353 }
354 
355 #endif
#define SafeModeAssert(condition, message)
Definition: Macro.hpp:47
Definition: Frame.hpp:68
Definition: Point.hpp:62
static Point & glob(const std::string &id)
Definition: Point.cpp:47
const double & y() const
void set_z(const double &)
const double & x() const
double get_z() const
Eigen::Matrix< double, 3, 1 > coord
Definition: Point.hpp:136
static const Point & global
Definition: Point.hpp:76
std::string info() const
Definition: Point.cpp:114
const double & z() const
friend bool operator==(const Point &, const Point &)
void translate(const Vector &v)
Definition: Point.cpp:88
friend bool operator!=(const Point &, const Point &)
void rotate(const Quaternion &q)
Definition: Point.cpp:95
double get_y() const
static Point mid_point(const Point &p1, const Point &p2)
const Vector & to_vector() const
const double & val() const
double get_x() const
static std::string class_ID()
Definition: Point.hpp:72
void add_glob(const std::string &id)
Definition: Point.cpp:121
void set_y(const double &)
friend class Vector
Definition: Point.hpp:64
const double & operator()(unsigned int i) const
static const int N
Definition: Point.hpp:74
Point(double, double, double)
void set_x(const double &)
Point(const double *)
static Point * new_object(const TiXmlElement *el)
Definition: Point.cpp:103
Point(const Point &)
friend class boost::serialization::access
Definition: Point.hpp:132
friend std::ostream & operator<<(std::ostream &, const Point &)
double distance_with(const Point &) const
void serialize(Archive &, const unsigned int)
bool is_nan() const
Point & operator=(const Point &)
Definition: Quaternion.hpp:54
Definition: Vector.hpp:75
bool operator!=(const EulerAngle &, const EulerAngle &)
std::ostream & operator<<(std::ostream &, const EulerAngle &)
bool operator==(const EulerAngle &, const EulerAngle &)
Definition: Common.hpp:198
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)