Vector.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2023, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
31 #ifndef MECURYDPM_VECTOR_H
32 #define MECURYDPM_VECTOR_H
33 
34 #include <cmath>
35 #include <sstream>
36 #include <iostream>
37 #include <cstdlib>
38 #include <array>
39 
40 #include "GeneralDefine.h"
41 #include "Logger.h"
42 
43 template<unsigned int N>
44 class SmallVector;
45 
50 class Vec3D
51 {
52 public:
53 
57  /*
58  * \todo: Make these private.
59  * \todo what is the idea of this constructor?
60  * These should be private so we can implement things like a cvec etc.
61  * Use getters / setters.
62  */
63 // Vec3D(int i);
64 
65  // private:
66  Mdouble X, Y, Z;
67 
72  { setZero(); }
73 
74  Vec3D(const SmallVector<3>& vector);
75 
83  Vec3D(const Mdouble x, const Mdouble y, const Mdouble z)
84  {
85  X = x;
86  Y = y;
87  Z = z;
88  }
89 
93  Vec3D(std::array<double, 3> a)
94  {
95  X = a[0];
96  Y = a[1];
97  Z = a[2];
98  }
99 
103  void setZero();
104 
108  void setNaN();
109 
113  bool isZero() const
114  { return X == 0.0 && Y == 0.0 && Z == 0.0; }
115 
119  bool isNaN() const;
120 
127  Vec3D operator+(const Vec3D& a) const
128  {
129  return Vec3D(X + a.X, Y + a.Y, Z + a.Z);
130  }
131 
138  inline Vec3D operator-(const Vec3D a) const
139  {
140  return Vec3D(X - a.X, Y - a.Y, Z - a.Z);
141  };
142 
143  inline bool operator==(const Vec3D& a) const
144  {
145  return X == a.X and Y == a.Y and Z == a.Z;
146  };
147 
148  Vec3D multiplyElementwise(const Vec3D& a) const {
149  return Vec3D(X*a.X, Y*a.Y, Z*a.Z);
150  }
151 
152  Vec3D divideElementwise(const Vec3D& a) const {
153  return Vec3D(X/a.X, Y/a.Y, Z/a.Z);
154  }
155 
156  Vec3D signedSquare() const {
157  return Vec3D(fabs(X)*X, fabs(Y)*Y, fabs(Z)*Z);
158  }
159 
166  Vec3D operator*(const Mdouble a) const
167  {
168  return Vec3D(X * a, Y * a, Z * a);
169  }
170 
178  return Vec3D(X / a, Y / a, Z / a);
179  }
180 
187  Vec3D& operator+=(const Vec3D& a)
188  {
189  X += a.X;
190  Y += a.Y;
191  Z += a.Z;
192  return *this;
193  }
194 
198  bool operator>=(const Vec3D& a) const {
199  return X>=a.X && Y>=a.Y && Z>=a.Z;
200  }
201 
202  bool operator<(const Vec3D& a) const {
203  return X<a.X && Y<a.Y && Z<a.Z;
204  }
205 
212  Vec3D& operator-=(const Vec3D& a)
213  {
214  X -= a.X;
215  Y -= a.Y;
216  Z -= a.Z;
217  return *this;
218  }
219 
227  X *= a;
228  Y *= a;
229  Z *= a;
230  return *this;
231  }
232 
240  {
241  X /= a;
242  Y /= a;
243  Z /= a;
244  return *this;
245  }
246 
250  static Mdouble dot(const Vec3D& a, const Vec3D& b);
251 
255  static Vec3D max(const Vec3D& a, const Vec3D& b);
256 
260  static Vec3D min(const Vec3D& a, const Vec3D& b);
261 
265  static double max(const Vec3D& a) {return std::max(std::max(a.X,a.Y),a.Z);}
266 
270  static double min(const Vec3D& a) {return std::min(std::min(a.X,a.Y),a.Z);}
271 
275  static Vec3D square(const Vec3D& a);
276 
280  void normalise();
281 
285  void setLength(Mdouble length);
286 
290  static Vec3D sqrt(const Vec3D& a);
291 
295  static Vec3D cross(const Vec3D& a, const Vec3D& b);
296 
306  static Mdouble getDistance(const Vec3D& a, const Vec3D& b);
307 
311  static Mdouble getDistanceSquared(const Vec3D& a, const Vec3D& b) {
312  const double X = a.X-b.X;
313  const double Y = a.Y-b.Y;
314  const double Z = a.Z-b.Z;
315  return (X * X + Y * Y + Z * Z);
316  //return getLengthSquared(a - b);
317  }
318 
319 
323  static Mdouble getLength(const Vec3D& a);
324 
332  static Mdouble getLengthSquared(const Vec3D& a)
333  {
334  return (a.X * a.X + a.Y * a.Y + a.Z * a.Z);
335  }
336 
340  Mdouble getLength() const;
341 
345  Mdouble getLengthSquared() const;
346 
350  Mdouble getComponent(int index) const;
351 
355  void setComponent(int index, double val);
356 
360  inline Mdouble& x()
361  { return X; }
362 
366  inline Mdouble x() const
367  { return X; }
368 
372  inline Mdouble& y()
373  { return Y; }
374 
378  inline Mdouble y() const
379  { return Y; }
380 
384  inline Mdouble& z()
385  { return Z; }
386 
390  inline Mdouble z() const
391  { return Z; }
392 
393  inline void setX(Mdouble x)
394  { X = x; }
395 
396  inline void setY(Mdouble y)
397  { Y = y; }
398 
399  inline void setZ(Mdouble z)
400  { Z = z; }
401 
402  inline Mdouble getX() const
403  { return X; }
404 
405  inline Mdouble getY() const
406  { return Y; }
407 
408  inline Mdouble getZ() const
409  { return Z; }
410 
411  inline void set(Mdouble x, Mdouble y, Mdouble z)
412  {
413  X = x;
414  Y = y;
415  Z = z;
416  }
417 
422 
427 
432 
437 
441  Vec3D getCylindricalTensorField(const Vec3D& position) const;
442 
446  bool isEqualTo(const Vec3D& other, double tol) const;
447 
451  static Vec3D getUnitVector(const Vec3D& a);
452 
456  friend std::ostream& operator<<(std::ostream& os, const Vec3D& a);
457 
461  friend std::istream& operator>>(std::istream& is, Vec3D& a);
462 
466  inline friend Vec3D operator-(const Vec3D& a) {
467  return Vec3D(-a.X, -a.Y, -a.Z);
468  }
469 
479  friend Vec3D operator*(Mdouble a, const Vec3D& b) {
480  return Vec3D(b.X * a, b.Y * a, b.Z * a);
481  }
482 
483 
484 
485 };
486 
487 #endif
double Mdouble
Definition: GeneralDefine.h:34
Definition: SmallVector.h:62
Definition: Vector.h:51
Mdouble & y()
RW reference to Y.
Definition: Vector.h:372
Mdouble Y
Definition: Vector.h:66
static Vec3D square(const Vec3D &a)
Calculates the pointwise square of a Vec3D.
Definition: Vector.cc:114
friend std::istream & operator>>(std::istream &is, Vec3D &a)
Adds elements to an input stream.
Definition: Vector.cc:374
static Mdouble getDistance(const Vec3D &a, const Vec3D &b)
Calculates the distance between two Vec3D: .
Definition: Vector.cc:175
Vec3D getCylindricalTensorField(const Vec3D &position) const
Returns this vector field at point p to cylindrical coordinates.
Definition: Vector.cc:261
Vec3D operator/(Mdouble a) const
Divides by a scalar.
Definition: Vector.h:177
Vec3D operator*(const Mdouble a) const
Multiplies by a scalar.
Definition: Vector.h:166
Mdouble Z
Definition: Vector.h:66
Mdouble & z()
RW reference to Z.
Definition: Vector.h:384
Vec3D divideElementwise(const Vec3D &a) const
Definition: Vector.h:152
Mdouble getRadialCoordinateSquared() const
Returns the square of the radial cylindrical coordinate, r^2=x^2+y^2.
Definition: Vector.cc:237
static Vec3D max(const Vec3D &a, const Vec3D &b)
Calculates the pointwise maximum of two Vec3D.
Definition: Vector.cc:89
static Vec3D min(const Vec3D &a, const Vec3D &b)
Calculates the pointwise minimum of two Vec3D.
Definition: Vector.cc:102
Mdouble y() const
RO reference to Y.
Definition: Vector.h:378
void setNaN()
Sets all elements to NaN.
Definition: Vector.cc:53
static Mdouble getLengthSquared(const Vec3D &a)
Calculates the squared length of a Vec3D: .
Definition: Vector.h:332
Vec3D & operator+=(const Vec3D &a)
Adds another vector.
Definition: Vector.h:187
bool operator==(const Vec3D &a) const
Definition: Vector.h:143
static Vec3D cross(const Vec3D &a, const Vec3D &b)
Calculates the cross product of two Vec3D: .
Definition: Vector.cc:163
Mdouble getComponent(int index) const
Returns the requested component of this Vec3D.
Definition: Vector.cc:194
Vec3D operator-(const Vec3D a) const
Binary vector subtraction.
Definition: Vector.h:138
Mdouble X
the vector components
Definition: Vector.h:66
Vec3D multiplyElementwise(const Vec3D &a) const
Definition: Vector.h:148
Vec3D & operator-=(const Vec3D &a)
Subtracts another vector.
Definition: Vector.h:212
Vec3D getCylindricalCoordinates() const
Returns the representation of this Vec3D in cylindrical coordinates.
Definition: Vector.cc:251
friend Vec3D operator-(const Vec3D &a)
Reverts the direction of a vector.
Definition: Vector.h:466
Vec3D()
constructor
Definition: Vector.h:71
bool operator<(const Vec3D &a) const
Definition: Vector.h:202
Vec3D signedSquare() const
Definition: Vector.h:156
static Mdouble getDistanceSquared(const Vec3D &a, const Vec3D &b)
Calculates the squared distance between two Vec3D: .
Definition: Vector.h:311
static double min(const Vec3D &a)
Calculates the minimum coordinate of vector a.
Definition: Vector.h:270
bool operator>=(const Vec3D &a) const
Checks if all coordinates satisfy this>=a.
Definition: Vector.h:198
void setZero()
Sets all elements to zero.
Definition: Vector.cc:43
Vec3D(const Mdouble x, const Mdouble y, const Mdouble z)
Alternative constructor, taking the three elements as arguments.
Definition: Vector.h:83
Mdouble getLengthSquared() const
Calculates the squared length of this Vec3D: .
Definition: Vector.cc:184
static double max(const Vec3D &a)
Calculates the maximum coordinate of vector a.
Definition: Vector.h:265
Mdouble getZ() const
Definition: Vector.h:408
static Mdouble dot(const Vec3D &a, const Vec3D &b)
Calculates the dot product of two Vec3D: .
Definition: Vector.cc:76
void set(Mdouble x, Mdouble y, Mdouble z)
Definition: Vector.h:411
void setLength(Mdouble length)
Make this Vec3D a certain length.
Definition: Vector.cc:138
Vec3D(std::array< double, 3 > a)
Definition: Vector.h:93
Mdouble & x()
RW reference to X.
Definition: Vector.h:360
void setComponent(int index, double val)
Sets the requested component of this Vec3D to the requested value.
Definition: Vector.cc:217
Mdouble getRadialCoordinate() const
Returns the square of the radial cylindrical coordinate, r=sqrt(x^2+y^2).
Definition: Vector.cc:242
Vec3D & operator/=(const Mdouble a)
Divides by a scalar.
Definition: Vector.h:239
bool isNaN() const
Checks if ALL elements are zero.
Definition: Vector.cc:64
void setY(Mdouble y)
Definition: Vector.h:396
friend std::ostream & operator<<(std::ostream &os, const Vec3D &a)
Adds elements to an output stream.
Definition: Vector.cc:361
Mdouble getLength() const
Calculates the length of this Vec3D: .
Definition: Vector.cc:320
Vec3D & operator*=(Mdouble a)
Multiplies by a scalar.
Definition: Vector.h:226
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:294
Mdouble x() const
RO reference to X.
Definition: Vector.h:366
Mdouble getX() const
Definition: Vector.h:402
Vec3D getFromCylindricalCoordinates() const
Returns the representation of this Vec3D in cylindrical coordinates.
Definition: Vector.cc:279
void setZ(Mdouble z)
Definition: Vector.h:399
bool isZero() const
Checks if ALL elements are zero.
Definition: Vector.h:113
void normalise()
Makes this Vec3D unit length.
Definition: Vector.cc:123
friend Vec3D operator*(Mdouble a, const Vec3D &b)
Multiplies all elements by a scalar.
Definition: Vector.h:479
static Vec3D getUnitVector(const Vec3D &a)
Returns a unit Vec3D based on a.
Definition: Vector.cc:345
void setX(Mdouble x)
Definition: Vector.h:393
Mdouble z() const
RO reference to Z.
Definition: Vector.h:390
Mdouble getY() const
Definition: Vector.h:405
static Vec3D sqrt(const Vec3D &a)
Calculates the pointwise square root of a Vec3D.
Definition: Vector.cc:151
Vec3D operator+(const Vec3D &a) const
Adds another vector.
Definition: Vector.h:127