MercuryDPM  Trunk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Vector.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2020, 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 
28 #ifndef VECTOR_H
32 #define VECTOR_H
33 
34 #include <cmath>
35 #include <sstream>
36 #include <iostream>
37 #include <cstdlib>
38 
39 #include "GeneralDefine.h"
40 #include "Logger.h"
41 
42 template<unsigned int N>
43 class SmallVector;
44 
49 class Vec3D
50 {
51 public:
52 
56  /*
57  * \todo: Make these private.
58  * \todo what is the idea of this constructor?
59  * These should be private so we can implement things like a cvec etc.
60  * Use getters / setters.
61  */
62 // Vec3D(int i);
63 
64  // private:
65  Mdouble X, Y, Z;
66 
71  { setZero(); }
72 
73  Vec3D(const SmallVector<3>& vector);
74 
82  Vec3D(const Mdouble x, const Mdouble y, const Mdouble z)
83  {
84  X = x;
85  Y = y;
86  Z = z;
87  }
88 
92  void setZero();
93 
97  void setNaN();
98 
102  bool isZero() const
103  { return X == 0.0 && Y == 0.0 && Z == 0.0; }
104 
108  bool isNaN() const;
109 
116  Vec3D operator+(const Vec3D& a) const
117  {
118  return Vec3D(X + a.X, Y + a.Y, Z + a.Z);
119  }
120 
127  inline Vec3D operator-(const Vec3D a) const
128  {
129  return Vec3D(X - a.X, Y - a.Y, Z - a.Z);
130  };
131 
132  Vec3D multiplyElementwise(const Vec3D& a) const {
133  return Vec3D(X*a.X, Y*a.Y, Z*a.Z);
134  }
135 
136  Vec3D divideElementwise(const Vec3D& a) const {
137  return Vec3D(X/a.X, Y/a.Y, Z/a.Z);
138  }
139 
140  Vec3D signedSquare() const {
141  return Vec3D(fabs(X)*X, fabs(Y)*Y, fabs(Z)*Z);
142  }
143 
150  Vec3D operator*(const Mdouble a) const
151  {
152  return Vec3D(X * a, Y * a, Z * a);
153  }
154 
162  return Vec3D(X / a, Y / a, Z / a);
163  }
164 
171  Vec3D& operator+=(const Vec3D& a)
172  {
173  X += a.X;
174  Y += a.Y;
175  Z += a.Z;
176  return *this;
177  }
178 
182  bool operator>=(const Vec3D& a) const {
183  return X>=a.X && Y>=a.Y && Z>=a.Z;
184  }
185 
186  bool operator<(const Vec3D& a) const {
187  return X<a.X && Y<a.Y && Z<a.Z;
188  }
189 
196  Vec3D& operator-=(const Vec3D& a)
197  {
198  X -= a.X;
199  Y -= a.Y;
200  Z -= a.Z;
201  return *this;
202  }
203 
211  X *= a;
212  Y *= a;
213  Z *= a;
214  return *this;
215  }
216 
224  {
225  X /= a;
226  Y /= a;
227  Z /= a;
228  return *this;
229  }
230 
234  static Mdouble dot(const Vec3D& a, const Vec3D& b);
235 
239  static Vec3D max(const Vec3D& a, const Vec3D& b);
240 
244  static Vec3D min(const Vec3D& a, const Vec3D& b);
245 
249  static double max(const Vec3D& a) {return std::max(std::max(a.X,a.Y),a.Z);}
250 
254  static double min(const Vec3D& a) {return std::min(std::min(a.X,a.Y),a.Z);}
255 
259  static Vec3D square(const Vec3D& a);
260 
264  void normalise();
265 
269  void setLength(Mdouble length);
270 
274  static Vec3D sqrt(const Vec3D& a);
275 
279  static Vec3D cross(const Vec3D& a, const Vec3D& b);
280 
290  static Mdouble getDistance(const Vec3D& a, const Vec3D& b);
291 
295  static Mdouble getDistanceSquared(const Vec3D& a, const Vec3D& b) {
296  const double X = a.X-b.X;
297  const double Y = a.Y-b.Y;
298  const double Z = a.Z-b.Z;
299  return (X * X + Y * Y + Z * Z);
300  //return getLengthSquared(a - b);
301  }
302 
303 
307  static Mdouble getLength(const Vec3D& a);
308 
316  static Mdouble getLengthSquared(const Vec3D& a)
317  {
318  return (a.X * a.X + a.Y * a.Y + a.Z * a.Z);
319  }
320 
324  Mdouble getLength() const;
325 
329  Mdouble getLengthSquared() const;
330 
334  Mdouble getComponent(int index) const;
335 
339  void setComponent(int index, double val);
340 
344  inline Mdouble& x()
345  { return X; }
346 
350  inline Mdouble x() const
351  { return X; }
352 
356  inline Mdouble& y()
357  { return Y; }
358 
362  inline Mdouble y() const
363  { return Y; }
364 
368  inline Mdouble& z()
369  { return Z; }
370 
374  inline Mdouble z() const
375  { return Z; }
376 
377  inline void setX(Mdouble x)
378  { X = x; }
379 
380  inline void setY(Mdouble y)
381  { Y = y; }
382 
383  inline void setZ(Mdouble z)
384  { Z = z; }
385 
386  inline Mdouble getX()
387  { return X; }
388 
389  inline Mdouble getY()
390  { return Y; }
391 
392  inline Mdouble getZ()
393  { return Z; }
394 
395  inline void set(Mdouble x, Mdouble y, Mdouble z)
396  {
397  X = x;
398  Y = y;
399  Z = z;
400  }
401 
406 
411 
416 
421 
425  Vec3D getCylindricalTensorField(const Vec3D& position) const;
426 
430  bool isEqualTo(const Vec3D& other, double tol) const;
431 
435  static Vec3D getUnitVector(const Vec3D& a);
436 
440  friend std::ostream& operator<<(std::ostream& os, const Vec3D& a);
441 
445  friend std::istream& operator>>(std::istream& is, Vec3D& a);
446 
450  inline friend Vec3D operator-(const Vec3D& a) {
451  return Vec3D(-a.X, -a.Y, -a.Z);
452  }
453 
463  friend Vec3D operator*(Mdouble a, const Vec3D& b) {
464  return Vec3D(b.X * a, b.Y * a, b.Z * a);
465  }
466 
467 
468 
469 };
470 
471 #endif
Vec3D getCylindricalCoordinates() const
Returns the representation of this Vec3D in cylindrical coordinates.
Definition: Vector.cc:251
Mdouble X
the vector components
Definition: Vector.h:65
void setY(Mdouble y)
Definition: Vector.h:380
static Vec3D getUnitVector(const Vec3D &a)
Returns a unit Vec3D based on a.
Definition: Vector.cc:345
Vec3D()
constructor
Definition: Vector.h:70
friend Vec3D operator-(const Vec3D &a)
Reverts the direction of a vector.
Definition: Vector.h:450
Vec3D getFromCylindricalCoordinates() const
Returns the representation of this Vec3D in cylindrical coordinates.
Definition: Vector.cc:279
double Mdouble
Definition: GeneralDefine.h:34
void normalise()
Makes this Vec3D unit length.
Definition: Vector.cc:123
bool isNaN() const
Checks if ALL elements are zero.
Definition: Vector.cc:64
void setLength(Mdouble length)
Make this Vec3D a certain length.
Definition: Vector.cc:138
Vec3D multiplyElementwise(const Vec3D &a) const
Definition: Vector.h:132
Vec3D operator*(const Mdouble a) const
Multiplies by a scalar.
Definition: Vector.h:150
Mdouble z() const
RO reference to Z.
Definition: Vector.h:374
Vec3D(const Mdouble x, const Mdouble y, const Mdouble z)
Alternative constructor, taking the three elements as arguments.
Definition: Vector.h:82
Implementation of a 3D vector (by Vitaliy).
Definition: SmallVector.h:61
Mdouble y() const
RO reference to Y.
Definition: Vector.h:362
Vec3D getCylindricalTensorField(const Vec3D &position) const
Returns this vector field at point p to cylindrical coordinates.
Definition: Vector.cc:261
void setZero()
Sets all elements to zero.
Definition: Vector.cc:43
Mdouble getRadialCoordinate() const
Returns the square of the radial cylindrical coordinate, r=sqrt(x^2+y^2).
Definition: Vector.cc:242
static Vec3D sqrt(const Vec3D &a)
Calculates the pointwise square root of a Vec3D.
Definition: Vector.cc:151
static Mdouble dot(const Vec3D &a, const Vec3D &b)
Calculates the dot product of two Vec3D: .
Definition: Vector.cc:76
Vec3D divideElementwise(const Vec3D &a) const
Definition: Vector.h:136
static Vec3D square(const Vec3D &a)
Calculates the pointwise square of a Vec3D.
Definition: Vector.cc:114
Vec3D & operator*=(Mdouble a)
Multiplies by a scalar.
Definition: Vector.h:210
bool isZero() const
Checks if ALL elements are zero.
Definition: Vector.h:102
Mdouble getLengthSquared() const
Calculates the squared length of this Vec3D: .
Definition: Vector.cc:184
Mdouble & z()
RW reference to Z.
Definition: Vector.h:368
void setComponent(int index, double val)
Sets the requested component of this Vec3D to the requested value.
Definition: Vector.cc:217
friend Vec3D operator*(Mdouble a, const Vec3D &b)
Multiplies all elements by a scalar.
Definition: Vector.h:463
Vec3D operator+(const Vec3D &a) const
Adds another vector.
Definition: Vector.h:116
Vec3D operator/(Mdouble a) const
Divides by a scalar.
Definition: Vector.h:161
Mdouble getRadialCoordinateSquared() const
Returns the square of the radial cylindrical coordinate, r^2=x^2+y^2.
Definition: Vector.cc:237
Vec3D & operator/=(const Mdouble a)
Divides by a scalar.
Definition: Vector.h:223
bool operator<(const Vec3D &a) const
Definition: Vector.h:186
Vec3D & operator+=(const Vec3D &a)
Adds another vector.
Definition: Vector.h:171
Vec3D & operator-=(const Vec3D &a)
Subtracts another vector.
Definition: Vector.h:196
static Vec3D min(const Vec3D &a, const Vec3D &b)
Calculates the pointwise minimum of two Vec3D.
Definition: Vector.cc:102
friend std::istream & operator>>(std::istream &is, Vec3D &a)
Adds elements to an input stream.
Definition: Vector.cc:374
Mdouble getComponent(int index) const
Returns the requested component of this Vec3D.
Definition: Vector.cc:194
Mdouble & x()
RW reference to X.
Definition: Vector.h:344
Mdouble getY()
Definition: Vector.h:389
static Mdouble getDistance(const Vec3D &a, const Vec3D &b)
Calculates the distance between two Vec3D: .
Definition: Vector.cc:175
Mdouble x() const
RO reference to X.
Definition: Vector.h:350
Mdouble getX()
Definition: Vector.h:386
static Vec3D cross(const Vec3D &a, const Vec3D &b)
Calculates the cross product of two Vec3D: .
Definition: Vector.cc:163
Mdouble getZ()
Definition: Vector.h:392
Mdouble & y()
RW reference to Y.
Definition: Vector.h:356
static Mdouble getDistanceSquared(const Vec3D &a, const Vec3D &b)
Calculates the squared distance between two Vec3D: .
Definition: Vector.h:295
Mdouble Y
Definition: Vector.h:65
void setZ(Mdouble z)
Definition: Vector.h:383
Vec3D operator-(const Vec3D a) const
Binary vector subtraction.
Definition: Vector.h:127
Mdouble getLength() const
Calculates the length of this Vec3D: .
Definition: Vector.cc:320
void set(Mdouble x, Mdouble y, Mdouble z)
Definition: Vector.h:395
static double max(const Vec3D &a)
Calculates the maximum coordinate of vector a.
Definition: Vector.h:249
bool operator>=(const Vec3D &a) const
Checks if all coordinates satisfy this>=a.
Definition: Vector.h:182
static Mdouble getLengthSquared(const Vec3D &a)
Calculates the squared length of a Vec3D: .
Definition: Vector.h:316
Definition: Vector.h:49
static double min(const Vec3D &a)
Calculates the minimum coordinate of vector a.
Definition: Vector.h:254
Mdouble Z
Definition: Vector.h:65
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
Vec3D signedSquare() const
Definition: Vector.h:140
void setNaN()
Sets all elements to NaN.
Definition: Vector.cc:53
friend std::ostream & operator<<(std::ostream &os, const Vec3D &a)
Adds elements to an output stream.
Definition: Vector.cc:361
static Vec3D max(const Vec3D &a, const Vec3D &b)
Calculates the pointwise maximum of two Vec3D.
Definition: Vector.cc:89
void setX(Mdouble x)
Definition: Vector.h:377