MercuryDPM  Trunk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Vector.cc
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 
26 #include "Vector.h"
27 #include "SmallVector.h"
28 
34 {
35  X = vector[0];
36  Y = vector[1];
37  Z = vector[2];
38 }
39 
44 {
45  X = 0.0;
46  Y = 0.0;
47  Z = 0.0;
48 }
49 
54 {
55  X = constants::NaN;
56  Y = constants::NaN;
57  Z = constants::NaN;
58 }
59 
64 bool Vec3D::isNaN() const
65 {
66  return std::isnan(X) || std::isnan(Y) || std::isnan(Z);
67 }
68 
76 Mdouble Vec3D::dot(const Vec3D& a, const Vec3D& b)
77 {
78  return a.X * b.X + a.Y * b.Y + a.Z * b.Z;
79 }
80 
89 Vec3D Vec3D::max(const Vec3D& a, const Vec3D& b)
90 {
91  return Vec3D(std::max(a.X, b.X), std::max(a.Y, b.Y), std::max(a.Z, b.Z));
92 }
93 
102 Vec3D Vec3D::min(const Vec3D& a, const Vec3D& b)
103 {
104  return Vec3D(std::min(a.X, b.X), std::min(a.Y, b.Y), std::min(a.Z, b.Z));
105 }
106 
115 {
116  return Vec3D(a.X * a.X, a.Y * a.Y, a.Z * a.Z);
117 }
118 
124 {
125  Mdouble length2 = this->getLengthSquared();
126  if (length2 == 0)
127  {
128  logger(ERROR, "Normalizing a vector of length 0");
129  }
130  *this /= std::sqrt(length2);
131 }
132 
139 {
140  this->normalise();
141  *this *= length;
142 }
143 
152 {
153  return Vec3D(std::sqrt(a.X), std::sqrt(a.Y), std::sqrt(a.Z));
154 }
155 
163 Vec3D Vec3D::cross(const Vec3D& a, const Vec3D& b)
164 {
165  return Vec3D(a.Y * b.Z - a.Z * b.Y, a.Z * b.X - a.X * b.Z, a.X * b.Y - a.Y * b.X);
166 }
167 
176 {
177  return std::sqrt(getDistanceSquared(a, b));
178 }
179 
185 {
186  return (X * X + Y * Y + Z * Z);
187 }
188 
194 Mdouble Vec3D::getComponent(const int index) const
195 {
196  switch (index)
197  {
198  case 0:
199  return X;
200  case 1:
201  return Y;
202  case 2:
203  return Z;
204  default:
205  logger(ERROR, "[Vector::getComponent] Index = %, which is too high for a 3D vector (should be 0-2).",
206  index);
207  return 0;
208  }
209 }
210 
217 void Vec3D::setComponent(const int index, const double val)
218 {
219  switch (index)
220  {
221  case 0:
222  X = val;
223  break;
224  case 1:
225  Y = val;
226  break;
227  case 2:
228  Z = val;
229  break;
230  default:
231  logger(ERROR, "[Vector::setComponent] Index = %, which is too high for a 3D vector (should be 0-2).",
232  index);
233  }
234 }
235 
236 
238 {
239  return X * X + Y * Y;
240 }
241 
243 {
244  return std::sqrt(X * X + Y * Y);
245 }
246 
252 {
253  return Vec3D(std::sqrt(X * X + Y * Y), std::atan2(Y, X), Z);
254 }
255 
262 {
263  //define sin(A)=y/r, cos(A)=x/r
264  Mdouble r = std::sqrt(p.X * p.X + p.Y * p.Y);
265  Mdouble s = p.Y / r;
266  Mdouble c = p.X / r;
267  if (r == 0)
268  {
269  s = 0;
270  c = 1;
271  }
272  return Vec3D(X * c + Y * s, -X * s + Y * c, Z);
273 }
274 
280 {
282  return Vec3D(X * std::cos(Y), X * std::sin(Y), Z);
283 }
284 
294 bool Vec3D::isEqualTo(const Vec3D& other, const double tol) const
295 {
296  if ((Vec3D::getLengthSquared(*this - other)) <= tol * tol)
297  {
298  return true;
299  }
300  else
301  {
302  return false;
303  }
304 }
305 
306 // void ConvertToCylindricalCoordinates()
307 // {
308 // double R = sqrt(X*X+Y*Y); Y = atan2(Y,X); X = R; return;
309 // }
310 //
311 // void ConvertFromCylindricalCoordinates()
312 // {
313 // double Xnew = X * cos(Y); Y = X * sin(Y); X = Xnew; return;
314 // }
315 
321 {
322  return std::sqrt(getLengthSquared());
323 }
324 
332 {
333  return a.getLength();
334 }
335 
346 {
347  Mdouble Length2 = a.getLengthSquared();
348  if (Length2 != 0.0)
349  return a / std::sqrt(Length2);
350  else
351  return Vec3D(0, 0, 0);
352 }
353 
361 std::ostream& operator<<(std::ostream& os, const Vec3D& a)
362 {
363  os << a.X << ' ' << a.Y << ' ' << a.Z;
364  return os;
365 }
366 
374 std::istream& operator>>(std::istream& is, Vec3D& a)
375 {
376  //TW: clearing the stream avoids the nasty problem that the failbit is set to true if numbers below DBL_MIN=1e-308 are read.
377  is >> a.X; is.clear();
378  is >> a.Y; is.clear();
379  is >> a.Z; //is.clear();
380  return is;
381 }
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
const Mdouble NaN
Definition: GeneralDefine.h:43
static Vec3D getUnitVector(const Vec3D &a)
Returns a unit Vec3D based on a.
Definition: Vector.cc:345
Vec3D()
constructor
Definition: Vector.h:70
Vec3D getFromCylindricalCoordinates() const
Returns the representation of this Vec3D in cylindrical coordinates.
Definition: Vector.cc:279
std::istream & operator>>(std::istream &is, Vec3D &a)
Definition: Vector.cc:374
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
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
Implementation of a 3D vector (by Vitaliy).
Definition: SmallVector.h:61
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
static Vec3D square(const Vec3D &a)
Calculates the pointwise square of a Vec3D.
Definition: Vector.cc:114
Mdouble getLengthSquared() const
Calculates the squared length of this Vec3D: .
Definition: Vector.cc:184
static Mdouble getLength(const Vec3D &a)
Calculates the length of a Vec3D: .
Definition: Vector.cc:331
Mdouble cos(Mdouble x)
Definition: ExtendedMath.cc:64
void setComponent(int index, double val)
Sets the requested component of this Vec3D to the requested value.
Definition: Vector.cc:217
Mdouble getRadialCoordinateSquared() const
Returns the square of the radial cylindrical coordinate, r^2=x^2+y^2.
Definition: Vector.cc:237
Mdouble sin(Mdouble x)
Definition: ExtendedMath.cc:44
static Vec3D min(const Vec3D &a, const Vec3D &b)
Calculates the pointwise minimum of two Vec3D.
Definition: Vector.cc:102
Mdouble getComponent(int index) const
Returns the requested component of this Vec3D.
Definition: Vector.cc:194
std::ostream & operator<<(std::ostream &os, const Vec3D &a)
Definition: Vector.cc:361
static Mdouble getDistance(const Vec3D &a, const Vec3D &b)
Calculates the distance between two Vec3D: .
Definition: Vector.cc:175
static Vec3D cross(const Vec3D &a, const Vec3D &b)
Calculates the cross product of two Vec3D: .
Definition: Vector.cc:163
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
Mdouble getLength() const
Calculates the length of this Vec3D: .
Definition: Vector.cc:320
static Mdouble getLengthSquared(const Vec3D &a)
Calculates the squared length of a Vec3D: .
Definition: Vector.h:316
Definition: Vector.h:49
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
void setNaN()
Sets all elements to NaN.
Definition: Vector.cc:53
static Vec3D max(const Vec3D &a, const Vec3D &b)
Calculates the pointwise maximum of two Vec3D.
Definition: Vector.cc:89