GranOO 3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
Loading...
Searching...
No Matches
Vector.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
31#ifndef _GranOO_libGeom_Vector_hpp_
32#define _GranOO_libGeom_Vector_hpp_
33
34#include <vector>
35#include <cmath>
36#include <iostream>
37#include <string>
38#include <cmath>
39#include "GranOO3/3rdParty/Eigen/Dense"
40
44#include "GranOO3/Geom/Axis.hpp"
45
46#include <boost/archive/text_oarchive.hpp>
47#include <boost/archive/text_iarchive.hpp>
48
49
50namespace GranOO3
51{
52 namespace Geom
53 {
54 class Vector;
55 class Point;
56 class Tensor;
57
58 // Extern operators
59 std::ostream& operator<< (std::ostream& os, const Vector& v);
60 std::istream& operator>> (std::istream& in, Vector& v);
61 Vector operator+ (const Vector& v1, const Vector& v2);
62 Vector operator- (const Vector& v1, const Vector& v2);
64 Vector operator* (const Vector& v, const double& a);
65 Vector operator* (const double& a, const Vector& v);
66 Vector operator/ (const Vector& v, const double& a);
67 double operator* (const Vector& v1, const Vector& v2);
68 Vector operator^ (const Vector& v1, const Vector& v2);
69 bool operator!= (const Vector& v1, const Vector& v2);
70 bool operator== (const Vector& v1, const Vector& v2);
71 Vector double_cross(const Vector& v1, const Vector& v2);
72 double get_angle_between_unit_vector (const Vector &a, const Vector &b);
73
74 class Vector
75 {
76 public:
77 // Usefull for internal use only, NOT DOCUMENTED !
78 static Vector& glob(const std::string& id);
79 static std::string class_ID() {return "Vector";}
80 static Vector* new_object(const TiXmlElement* el);
81 static const Vector& null() {return null::vector;}
82 static const int N = 3;
83
84
85 public:
86 // Constructors and destructor
88 Vector (const Vector& v);
89 Vector (const Vector& v, const Frame& from, const Frame& to);
90 explicit Vector (const Point& p);
91 explicit Vector (const Point& p1, const Point& p2);
92 explicit Vector (double x, double y, double z);
93 explicit Vector (const double* pt);
94
95
96
97 // Miscellaneous Usefull methods
98 void clear();
99 bool is_null() const;
100 bool is_nan() const;
101 bool is_unit() const;
103 const Point & to_point() const;
104
105 // Component
106 double & x();
107 const double & x() const;
108 double & y();
109 const double & y() const;
110 double & z();
111 const double & z() const;
112
113
114 // Component (for python wrapper)
115 void set_x(const double&);
116 void set_y(const double&);
117 void set_z(const double&);
118
119 // Component (for python wrapper)
120 double get_x() const;
121 double get_y() const;
122 double get_z() const;
123
124
125 template <typename Axis> double& val();
126 template <typename Axis> const double& val() const;
127
128 const double & operator() (unsigned int i) const;
129 double& operator() (unsigned int i);
130
131 // Internal operators
133 void operator+= (const Vector& v);
134 void operator-= (const Vector& v);
135 void operator*= (double k);
136 void operator/= (double k);
137
138 // Normalizing
139 double squared_norm() const;
140 double norm() const;
141 double Norm1() const;
142 double normalize();
143 Vector unit() const;
144
145 // Projection
146 void project_on_axis (const Vector& axis);
147 void project_on_plane (const Vector& normal);
149
150 // Misc
151 std::string info() const;
152 void add_glob(const std::string& id);
153
154 private:
156 template<class Archive>
157 void serialize(Archive & ar, const unsigned int);
158
159 public:
160 Eigen::Matrix<double, 3, 1> coord;
161 };
162
163#ifndef DOXYGEN_SHOULD_SKIP_THIS
164
165
166 template<class Archive>
167 void Vector::serialize(Archive& ar, const unsigned int) {
168 ar & x();
169 ar & y();
170 ar & z();
171 }
172
173 inline
175 : coord(){
176 }
177
178 inline
179 Vector::Vector (const Vector & v)
180 : coord(v.coord) {
181 }
182
183 inline
184 Vector::Vector (double x, double y, double z)
185 : coord(x, y, z) {
186 }
187
188 inline
189 Vector::Vector (const double* pt)
190 : coord(pt[0], pt[1], pt[2]){
191 }
192
193 // Conversions ...
194 inline Point&
196 return *reinterpret_cast<Point*> (this);
197 }
198
199 inline const Point&
200 Vector::to_point() const {
201 return *reinterpret_cast<const Point*> (this);
202 }
203
204 inline double&
205 Vector::x() {
206 return coord(0);
207 }
208
209 inline const double&
210 Vector::x() const {
211 return coord(0);
212 }
213
214 inline double&
215 Vector::y() {
216 return coord(1);
217 }
218
219 inline const double&
220 Vector::y() const {
221 return coord(1);
222 }
223
224 inline double&
225 Vector::z() {
226 return coord(2);
227 }
228
229 inline const double&
230 Vector::z() const {
231 return coord(2);
232 }
233
234 inline void
235 Vector::set_x(const double& val) {
236 x() = val;
237 }
238
239 inline void
240 Vector::set_y(const double& val) {
241 y() = val;
242 }
243
244 inline void
245 Vector::set_z(const double& val) {
246 z() = val;
247 }
248
249 inline double
250 Vector::get_x() const {
251 return x();
252 }
253
254 inline double
255 Vector::get_y() const {
256 return y();
257 }
258
259 inline double
260 Vector::get_z() const {
261 return z();
262 }
263
264 template <typename Axis> inline
265 double& Vector::val() {
266 SafeModeAssert(0, "Non specilized method is forbidden");
267 return x();
268 }
269
270 template <typename Axis> inline
271 const double& Vector::val() const {
272 SafeModeAssert(0, "Non specilized method is forbidden");
273 return x();
274 }
275
276 template<> inline
277 double& Vector::val<X>() {
278 return x();
279 }
280
281 template<> inline
282 const double& Vector::val<X>() const {
283 return x();
284 }
285
286 template<> inline
287 double& Vector::val<Y>() {
288 return y();
289 }
290
291 template<> inline
292 const double& Vector::val<Y>() const {
293 return y();
294 }
295
296 template<> inline
297 double& Vector::val<Z>() {
298 return z();
299 }
300
301 template<> inline
302 const double& Vector::val<Z>() const {
303 return z();
304 }
305
306 inline const double&
307 Vector::operator() (unsigned int i) const {
308 SafeModeAssert(i < 3, "The component must be in [0, 2] range");
309 return coord(i);
310 }
311
312 inline double&
313 Vector::operator() (unsigned int i) {
314 SafeModeAssert(i < 3, "The component must be in [0, 2] range");
315 return coord(i);
316 }
317
318 // External Operators ...
319
320 inline bool
321 operator== (const Vector &a, const Vector &b) {
322 return (a - b).squared_norm() < constant::epsilon;
323 }
324
325 inline bool
326 operator!= (const Vector &a, const Vector &b) {
327 return !(a == b);
328 }
329
330 inline std::ostream&
331 operator<< (std::ostream &out, const Vector &v) {
332 return out << v.x() << "\t" << v.y() << "\t" << v.z() <<"\t";
333 }
334
335 inline std::istream&
336 operator>> (std::istream &in, Vector &v) {
337 return in >> v.x() >> v.y() >> v.z();
338 }
339
340 inline Vector
341 operator* (const double & k, const Vector &v) {
342 return v*k;
343 }
344
345 inline double
346 operator* (const Vector &a, const Vector &b) {
347 return a.x()*b.x() + a.y()*b.y() + a.z()*b.z();
348 }
349
350 inline double
351 get_angle_between_unit_vector (const Vector &a, const Vector &b) {
352 return acos(a*b);
353 }
354
355 inline Vector
356 double_cross(const Vector& v1, const Vector& v2) {
357 return ((v1^v2)^v1);
358 }
359
360 // Class Operators ...
361 inline Vector&
362 Vector::operator=(const Vector& v) {
363 x() = v.x();
364 y() = v.y();
365 z() = v.z();
366 return *this;
367 }
368
369 inline void
370 Vector::operator+=(const Vector& a) {
371 x() += a.x();
372 y() += a.y();
373 z() += a.z();
374 }
375
376 inline void
377 Vector::operator-=(const Vector& a) {
378 x() -= a.x();
379 y() -= a.y();
380 z() -= a.z();
381 }
382
383 inline void
384 Vector::operator*=(double k) {
385 x() *= k;
386 y() *= k;
387 z() *= k;
388 }
389
390 inline void
391 Vector::operator/= (double k) {
392 x() /= k;
393 y() /= k;
394 z() /= k;
395 }
396
397 inline double
398 Vector::squared_norm() const {
399 return x()*x() + y()*y() + z()*z();
400 }
401
402 inline double
403 Vector::Norm1() const {
404 return fabs(x()) + fabs(y()) + fabs(z());
405 }
406
407 // Other Methods
408 inline void
409 Vector::clear() {
410 x() = 0.;
411 y() = 0.;
412 z() = 0.;
413 }
414
415 inline bool
416 Vector::is_null() const {
417 return (norm() < constant::epsilon);
418 }
419
420 inline bool
421 Vector::is_nan() const {
422 if (x()!=x())
423 return true;
424 if (y()!=y())
425 return true;
426 if (z()!=z())
427 return true;
428 return false;
429 }
430
431 inline bool
432 Vector::is_unit() const {
433 const double n = norm();
434 return (n > 1 - constant::epsilon && n < 1 + constant::epsilon);
435 }
436
437 inline Vector
438 Vector::unit() const {
439 Vector v1 (*this);
440 v1.normalize();
441 return v1;
442 }
443
444 // External Operators ...
445 inline Vector
446 operator+ (const Vector &a, const Vector &b) {
447 return Vector (a.x() + b.x(), a.y() + b.y(), a.z() + b.z());
448 }
449
450 inline Vector
451 operator- (const Vector &a, const Vector &b) {
452 return Vector (a.x() - b.x(), a.y() - b.y(), a.z() - b.z());
453 }
454
455 inline Vector
456 operator- (const Vector &a) {
457 return Vector (-a.x(), -a.y(), -a.z());
458 }
459
460 inline Vector
461 operator* (const Vector &a, const double &d) {
462 return Vector (d*a.x(), d*a.y(), d*a.z());
463 }
464
465 inline Vector
466 operator/ (const Vector &a, const double &k) {
467 SafeModeAssert(k != 0., "can't divide a vector by a null value");
468 return Vector (a.x() / k, a.y() / k, a.z() / k);
469 }
470
471 inline double
472 Vector::norm() const {
473 return sqrt(x()*x() + y()*y() + z()*z());
474 }
475
476 inline Vector
477 operator^ (const Vector &a, const Vector &b) {
478 return Vector (a.y()*b.z() - a.z()*b.y(),
479 a.z()*b.x() - a.x()*b.z(),
480 a.x()*b.y() - a.y()*b.x());
481 }
482
483 inline double
485 const double n = norm();
486 if (n == 0.) {
487 x() = 0.;
488 y() = 0.;
489 z() = 0.;
490 return 0.;
491 } else {
492 *this /= n;
493 return n;
494 }
495 }
496
497#endif
498
499 }
500}
501
502
503#endif
#define SafeModeAssert(condition, message)
Definition Macro.hpp:47
Definition Frame.hpp:68
Definition Point.hpp:62
Definition Tensor.hpp:62
Definition Vector.hpp:75
static Vector & glob(const std::string &id)
Definition Vector.cpp:46
const double & z() const
const Point & to_point() const
void operator+=(const Vector &v)
void project_on_plane(const Vector &normal)
Definition Vector.cpp:102
const double & y() const
void project_on_axis(const Vector &axis)
Definition Vector.cpp:93
void set_x(const double &)
void serialize(Archive &ar, const unsigned int)
static const Vector & null()
Definition Vector.hpp:81
void operator*=(double k)
bool is_null() const
double get_y() const
Vector orthogonal_vector() const
Definition Vector.cpp:111
static Vector * new_object(const TiXmlElement *el)
Definition Vector.cpp:75
void set_z(const double &)
double get_z() const
double norm() const
bool is_unit() const
double squared_norm() const
double get_x() const
void operator/=(double k)
void add_glob(const std::string &id)
Definition Vector.cpp:123
void operator-=(const Vector &v)
const double & val() const
Vector(const double *pt)
static const int N
Definition Vector.hpp:82
void set_y(const double &)
Eigen::Matrix< double, 3, 1 > coord
Definition Vector.hpp:160
double Norm1() const
std::string info() const
Definition Vector.cpp:86
Vector(const Vector &v)
static std::string class_ID()
Definition Vector.hpp:79
friend class boost::serialization::access
Definition Vector.hpp:155
const double & x() const
Vector & operator=(const Vector &v)
const double & operator()(unsigned int i) const
Vector(double x, double y, double z)
Vector unit() const
This namespace group the entire Geom library.
Vector operator^(const Vector &v1, const Vector &v2)
Vector double_cross(const Vector &v1, const Vector &v2)
std::ostream & operator<<(std::ostream &, const EulerAngle &)
SymTensor operator+(const SymTensor &m1, const SymTensor &m2)
double get_angle_between_unit_vector(const Vector &a, const Vector &b)
SymTensor operator-(const SymTensor &m1, const SymTensor &m2)
bool operator!=(const EulerAngle &, const EulerAngle &)
SymTensor operator/(const SymTensor &m, const double &d)
std::istream & operator>>(std::istream &in, Vector &v)
Vector operator*(const SymTensor &m, const Vector &v)
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)
Definition Constant.hpp:48
static double epsilon
Definition Constant.hpp:58
static const Geom::Vector vector
Definition Constant.hpp:75