Kernel/Math/Vector.h
Go to the documentation of this file.
1 // This file is part of the MercuryDPM project (https://www.mercurydpm.org).
2 // Copyright (c), The MercuryDPM Developers Team. All rights reserved.
3 // License: BSD 3-Clause License; see the LICENSE file in the root directory.
4 
10 #ifndef MECURYDPM_VECTOR_H
11 #define MECURYDPM_VECTOR_H
12 
13 #include <cmath>
14 #include <sstream>
15 #include <iostream>
16 #include <cstdlib>
17 #include <array>
18 
19 #include "GeneralDefine.h"
20 #include "Logger.h"
21 
22 template<unsigned int N>
23 class SmallVector;
24 
29 class Vec3D
30 {
31 public:
32 
36  /*
37  * \todo: Make these private.
38  * \todo what is the idea of this constructor?
39  * These should be private so we can implement things like a cvec etc.
40  * Use getters / setters.
41  */
42 // Vec3D(int i);
43 
44  // private:
45  Mdouble X, Y, Z;
46 
51  { setZero(); }
52 
53  Vec3D(const SmallVector<3>& vector);
54 
60  Vec3D(const std::array<Mdouble, 3>& vector)
61  {
62  X = vector[0];
63  Y = vector[1];
64  Z = vector[2];
65  };
66 
74  Vec3D(const Mdouble x, const Mdouble y, const Mdouble z)
75  {
76  X = x;
77  Y = y;
78  Z = z;
79  }
80 
84  void setZero();
85 
89  void setNaN();
90 
94  bool isZero() const
95  { return X == 0.0 && Y == 0.0 && Z == 0.0; }
96 
100  bool isNaN() const;
101 
108  Vec3D operator+(const Vec3D& a) const
109  {
110  return Vec3D(X + a.X, Y + a.Y, Z + a.Z);
111  }
112 
119  inline Vec3D operator-(const Vec3D a) const
120  {
121  return Vec3D(X - a.X, Y - a.Y, Z - a.Z);
122  };
123 
124  inline bool operator==(const Vec3D& a) const
125  {
126  return X == a.X and Y == a.Y and Z == a.Z;
127  };
128 
130  return Vec3D(X*a.X, Y*a.Y, Z*a.Z);
131  }
132 
133  Vec3D divideElementwise(const Vec3D& a) const {
134  return Vec3D(X/a.X, Y/a.Y, Z/a.Z);
135  }
136 
137  Vec3D signedSquare() const {
138  return Vec3D(fabs(X)*X, fabs(Y)*Y, fabs(Z)*Z);
139  }
140 
147  Vec3D operator*(const Mdouble a) const
148  {
149  return Vec3D(X * a, Y * a, Z * a);
150  }
151 
159  return Vec3D(X / a, Y / a, Z / a);
160  }
161 
169  {
170  X += a.X;
171  Y += a.Y;
172  Z += a.Z;
173  return *this;
174  }
175 
179  bool operator>=(const Vec3D& a) const {
180  return X>=a.X && Y>=a.Y && Z>=a.Z;
181  }
182 
183  bool operator<(const Vec3D& a) const {
184  return X<a.X && Y<a.Y && Z<a.Z;
185  }
186 
194  {
195  X -= a.X;
196  Y -= a.Y;
197  Z -= a.Z;
198  return *this;
199  }
200 
208  X *= a;
209  Y *= a;
210  Z *= a;
211  return *this;
212  }
213 
221  {
222  X /= a;
223  Y /= a;
224  Z /= a;
225  return *this;
226  }
227 
228 
232  static Mdouble dot(const Vec3D& a, const Vec3D& b);
233 
237  inline Mdouble dot(const Vec3D& b) const { return Vec3D::dot(*this, b); }
238 
242  static Vec3D max(const Vec3D& a, const Vec3D& b);
243 
247  static Vec3D min(const Vec3D& a, const Vec3D& b);
248 
252  static double max(const Vec3D& a) {return std::max(std::max(a.X,a.Y),a.Z);}
253 
257  static double min(const Vec3D& a) {return std::min(std::min(a.X,a.Y),a.Z);}
258 
262  static Vec3D square(const Vec3D& a);
263 
267  void normalise();
268 
272  void setLength(Mdouble length);
273 
277  static Vec3D sqrt(const Vec3D& a);
278 
282  static Vec3D cross(const Vec3D& a, const Vec3D& b);
283 
287  inline Vec3D cross(const Vec3D& b) const { return Vec3D::cross(*this, b); }
288 
298  static Mdouble getDistance(const Vec3D& a, const Vec3D& b);
299 
303  static Mdouble getDistanceSquared(const Vec3D& a, const Vec3D& b) {
304  const double X = a.X-b.X;
305  const double Y = a.Y-b.Y;
306  const double Z = a.Z-b.Z;
307  return (X * X + Y * Y + Z * Z);
308  //return getLengthSquared(a - b);
309  }
310 
311 
315  static Mdouble getLength(const Vec3D& a);
316 
325  {
326  return (a.X * a.X + a.Y * a.Y + a.Z * a.Z);
327  }
328 
332  Mdouble getLength() const;
333 
337  Mdouble getLengthSquared() const;
338 
342  Mdouble getComponent(int index) const;
343 
347  void setComponent(int index, double val);
348 
349 
353  Mdouble& operator[] (int index)
354  {
355  switch (index)
356  {
357  case 0:
358  return X;
359  case 1:
360  return Y;
361  case 2:
362  return Z;
363  default:
364  throw std::out_of_range("Index out of bounds");
365  }
366  }
367 
371  const Mdouble& operator[] (int index) const
372  {
373  switch (index)
374  {
375  case 0:
376  return X;
377  case 1:
378  return Y;
379  case 2:
380  return Z;
381  default:
382  throw std::out_of_range("Index out of bounds");
383  }
384  }
385 
389  inline Mdouble& x()
390  { return X; }
391 
395  inline Mdouble x() const
396  { return X; }
397 
401  inline Mdouble& y()
402  { return Y; }
403 
407  inline Mdouble y() const
408  { return Y; }
409 
413  inline Mdouble& z()
414  { return Z; }
415 
419  inline Mdouble z() const
420  { return Z; }
421 
422  inline void setX(Mdouble x)
423  { X = x; }
424 
425  inline void setY(Mdouble y)
426  { Y = y; }
427 
428  inline void setZ(Mdouble z)
429  { Z = z; }
430 
431  inline Mdouble getX() const
432  { return X; }
433 
434  inline Mdouble getY() const
435  { return Y; }
436 
437  inline Mdouble getZ() const
438  { return Z; }
439 
440  inline void set(Mdouble x, Mdouble y, Mdouble z)
441  {
442  X = x;
443  Y = y;
444  Z = z;
445  }
446 
451 
456 
461 
466 
471 
475  Vec3D getCylindricalTensorField(const Vec3D& position) const;
476 
480  bool isEqualTo(const Vec3D& other, double tol) const;
481 
485  static Vec3D getUnitVector(const Vec3D& a);
486 
490  friend std::ostream& operator<<(std::ostream& os, const Vec3D& a);
491 
495  friend std::istream& operator>>(std::istream& is, Vec3D& a);
496 
500  inline friend Vec3D operator-(const Vec3D& a) {
501  return Vec3D(-a.X, -a.Y, -a.Z);
502  }
503 
513  friend Vec3D operator*(Mdouble a, const Vec3D& b) {
514  return Vec3D(b.X * a, b.Y * a, b.Z * a);
515  }
516 
517 };
518 
519 #endif
double Mdouble
Definition: GeneralDefine.h:13
Scalar * b
Definition: benchVecAdd.cpp:17
Definition: SmallVector.h:42
Definition: Kernel/Math/Vector.h:30
Mdouble & y()
RW reference to Y.
Definition: Kernel/Math/Vector.h:401
Mdouble Y
Definition: Kernel/Math/Vector.h:45
static Vec3D square(const Vec3D &a)
Calculates the pointwise square of a Vec3D.
Definition: Vector.cc:94
friend std::istream & operator>>(std::istream &is, Vec3D &a)
Adds elements to an input stream.
Definition: Vector.cc:393
static Mdouble getDistance(const Vec3D &a, const Vec3D &b)
Calculates the distance between two Vec3D: .
Definition: Vector.cc:155
Vec3D getCylindricalTensorField(const Vec3D &position) const
Returns this vector field at point p to cylindrical coordinates.
Definition: Vector.cc:280
Vec3D operator/(Mdouble a) const
Divides by a scalar.
Definition: Kernel/Math/Vector.h:158
Vec3D operator*(const Mdouble a) const
Multiplies by a scalar.
Definition: Kernel/Math/Vector.h:147
Mdouble Z
Definition: Kernel/Math/Vector.h:45
Mdouble & z()
RW reference to Z.
Definition: Kernel/Math/Vector.h:413
Vec3D divideElementwise(const Vec3D &a) const
Definition: Kernel/Math/Vector.h:133
Mdouble getRadialCoordinateSquared() const
Returns the square of the radial cylindrical coordinate, r^2=x^2+y^2.
Definition: Vector.cc:217
static Vec3D max(const Vec3D &a, const Vec3D &b)
Calculates the pointwise maximum of two Vec3D.
Definition: Vector.cc:69
static Vec3D min(const Vec3D &a, const Vec3D &b)
Calculates the pointwise minimum of two Vec3D.
Definition: Vector.cc:82
Mdouble y() const
RO reference to Y.
Definition: Kernel/Math/Vector.h:407
Mdouble dot(const Vec3D &b) const
Calculates the dot product of this with another Vec3D: .
Definition: Kernel/Math/Vector.h:237
void setNaN()
Sets all elements to NaN.
Definition: Vector.cc:33
static Mdouble getLengthSquared(const Vec3D &a)
Calculates the squared length of a Vec3D: .
Definition: Kernel/Math/Vector.h:324
Vec3D & operator+=(const Vec3D &a)
Adds another vector.
Definition: Kernel/Math/Vector.h:168
bool operator==(const Vec3D &a) const
Definition: Kernel/Math/Vector.h:124
static Vec3D cross(const Vec3D &a, const Vec3D &b)
Calculates the cross product of two Vec3D: .
Definition: Vector.cc:143
Mdouble getComponent(int index) const
Returns the requested component of this Vec3D.
Definition: Vector.cc:174
Vec3D operator-(const Vec3D a) const
Binary vector subtraction.
Definition: Kernel/Math/Vector.h:119
Mdouble X
the vector components
Definition: Kernel/Math/Vector.h:45
Vec3D getSphericalCoordinates() const
Returns the representation of this Vec3D in spherical coordinates.
Definition: Vector.cc:231
Vec3D multiplyElementwise(const Vec3D &a) const
Definition: Kernel/Math/Vector.h:129
Vec3D & operator-=(const Vec3D &a)
Subtracts another vector.
Definition: Kernel/Math/Vector.h:193
Vec3D getCylindricalCoordinates() const
Returns the representation of this Vec3D in cylindrical coordinates.
Definition: Vector.cc:270
friend Vec3D operator-(const Vec3D &a)
Reverts the direction of a vector.
Definition: Kernel/Math/Vector.h:500
Vec3D(const std::array< Mdouble, 3 > &vector)
Alternative constructor, taking a const std::array<Mdouble, 3>
Definition: Kernel/Math/Vector.h:60
Vec3D()
constructor
Definition: Kernel/Math/Vector.h:50
bool operator<(const Vec3D &a) const
Definition: Kernel/Math/Vector.h:183
Vec3D signedSquare() const
Definition: Kernel/Math/Vector.h:137
static Mdouble getDistanceSquared(const Vec3D &a, const Vec3D &b)
Calculates the squared distance between two Vec3D: .
Definition: Kernel/Math/Vector.h:303
static double min(const Vec3D &a)
Calculates the minimum coordinate of vector a.
Definition: Kernel/Math/Vector.h:257
bool operator>=(const Vec3D &a) const
Checks if all coordinates satisfy this>=a.
Definition: Kernel/Math/Vector.h:179
Vec3D cross(const Vec3D &b) const
Calculates the cross product of this with another Vec3D: .
Definition: Kernel/Math/Vector.h:287
void setZero()
Sets all elements to zero.
Definition: Vector.cc:23
Vec3D(const Mdouble x, const Mdouble y, const Mdouble z)
Alternative constructor, taking the three elements as arguments.
Definition: Kernel/Math/Vector.h:74
Mdouble getLengthSquared() const
Calculates the squared length of this Vec3D: .
Definition: Vector.cc:164
static double max(const Vec3D &a)
Calculates the maximum coordinate of vector a.
Definition: Kernel/Math/Vector.h:252
Mdouble getZ() const
Definition: Kernel/Math/Vector.h:437
static Mdouble dot(const Vec3D &a, const Vec3D &b)
Calculates the dot product of two Vec3D: .
Definition: Vector.cc:56
void set(Mdouble x, Mdouble y, Mdouble z)
Definition: Kernel/Math/Vector.h:440
void setLength(Mdouble length)
Make this Vec3D a certain length.
Definition: Vector.cc:118
Mdouble & x()
RW reference to X.
Definition: Kernel/Math/Vector.h:389
void setComponent(int index, double val)
Sets the requested component of this Vec3D to the requested value.
Definition: Vector.cc:197
Mdouble getRadialCoordinate() const
Returns the square of the radial cylindrical coordinate, r=sqrt(x^2+y^2).
Definition: Vector.cc:222
Vec3D & operator/=(const Mdouble a)
Divides by a scalar.
Definition: Kernel/Math/Vector.h:220
bool isNaN() const
Checks if ALL elements are zero.
Definition: Vector.cc:44
void setY(Mdouble y)
Definition: Kernel/Math/Vector.h:425
friend std::ostream & operator<<(std::ostream &os, const Vec3D &a)
Adds elements to an output stream.
Definition: Vector.cc:380
Mdouble getLength() const
Calculates the length of this Vec3D: .
Definition: Vector.cc:339
Vec3D & operator*=(Mdouble a)
Multiplies by a scalar.
Definition: Kernel/Math/Vector.h:207
bool isEqualTo(const Vec3D &other, double tol) const
Checks if the length this Vec3D is equal the length of other with a certain tolerance.
Definition: Vector.cc:313
Mdouble x() const
RO reference to X.
Definition: Kernel/Math/Vector.h:395
Mdouble getX() const
Definition: Kernel/Math/Vector.h:431
Vec3D getFromCylindricalCoordinates() const
Returns the representation of this Vec3D in cylindrical coordinates.
Definition: Vector.cc:298
Mdouble & operator[](int index)
Implements operator [] to easier access to x, y, z.
Definition: Kernel/Math/Vector.h:353
void setZ(Mdouble z)
Definition: Kernel/Math/Vector.h:428
bool isZero() const
Checks if ALL elements are zero.
Definition: Kernel/Math/Vector.h:94
void normalise()
Makes this Vec3D unit length.
Definition: Vector.cc:103
friend Vec3D operator*(Mdouble a, const Vec3D &b)
Multiplies all elements by a scalar.
Definition: Kernel/Math/Vector.h:513
static Vec3D getUnitVector(const Vec3D &a)
Returns a unit Vec3D based on a.
Definition: Vector.cc:364
void setX(Mdouble x)
Definition: Kernel/Math/Vector.h:422
Mdouble z() const
RO reference to Z.
Definition: Kernel/Math/Vector.h:419
Mdouble getY() const
Definition: Kernel/Math/Vector.h:434
static Vec3D sqrt(const Vec3D &a)
Calculates the pointwise square root of a Vec3D.
Definition: Vector.cc:131
Vec3D operator+(const Vec3D &a) const
Adds another vector.
Definition: Kernel/Math/Vector.h:108
#define min(a, b)
Definition: datatypes.h:22
#define max(a, b)
Definition: datatypes.h:23
const Scalar * a
Definition: level2_cplx_impl.h:32
Real fabs(const Real &a)
Definition: boostmultiprec.cpp:117
val
Definition: calibrate.py:119