MercuryDPM  Beta
 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-2014, 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 #include "Vector.h"
26 #include <Logger.h>
27 
32 {
33  setZero();
34 }
35 
42 Vec3D::Vec3D(const Mdouble x, const Mdouble y, const Mdouble z)
43 {
44  X = x;
45  Y = y;
46  Z = z;
47 }
48 
53 {
54  X = 0.0;
55  Y = 0.0;
56  Z = 0.0;
57 }
58 
63 bool Vec3D::isZero() const
64 {
65  return X == 0.0 && Y == 0.0 && Z == 0.0;
66 }
67 
73 Vec3D Vec3D::operator +(const Vec3D& a) const
74  {
75  return Vec3D(X + a.X, Y + a.Y, Z + a.Z);
76 }
77 
83 Vec3D Vec3D::operator -(const Vec3D& a) const
84  {
85  return Vec3D(X - a.X, Y - a.Y, Z - a.Z);
86 }
87 
94  {
95  return Vec3D(X + a, Y + a, Z + a);
96 }
97 
104  {
105  return Vec3D(X - a, Y - a, Z - a);
106 }
107 
114  {
115  return Vec3D(X * a, Y * a, Z * a);
116 }
117 
124  {
125  return Vec3D(X / a, Y / a, Z / a);
126 }
127 
134 {
135  X += a.X;
136  Y += a.Y;
137  Z += a.Z;
138  return *this;
139 }
140 
147 {
148  X -= a.X;
149  Y -= a.Y;
150  Z -= a.Z;
151  return *this;
152 }
153 
160 {
161  X *= a;
162  Y *= a;
163  Z *= a;
164  return *this;
165 }
166 
173 {
174  X /= a;
175  Y /= a;
176  Z /= a;
177  return *this;
178 }
179 
187 Mdouble Vec3D::dot(const Vec3D& a, const Vec3D& b)
188 {
189  return a.X * b.X + a.Y * b.Y + a.Z * b.Z;
190 }
191 
200 Vec3D Vec3D::max(const Vec3D& a, const Vec3D& b)
201 {
202  return Vec3D(std::max(a.X, b.X), std::max(a.Y, b.Y), std::max(a.Z, b.Z));
203 }
204 
213 Vec3D Vec3D::min(const Vec3D& a, const Vec3D& b)
214 {
215  return Vec3D(std::min(a.X, b.X), std::min(a.Y, b.Y), std::min(a.Z, b.Z));
216 }
217 
226 {
227  return Vec3D(a.X * a.X, a.Y * a.Y, a.Z * a.Z);
228 }
229 
235 {
236  *this /= this->getLength();
237 }
238 
245 {
246  *this /= this->getLength() * length;
247 }
248 
257 {
258  return Vec3D(std::sqrt(a.X), std::sqrt(a.Y), std::sqrt(a.Z));
259 }
260 
268 Vec3D Vec3D::cross(const Vec3D& a, const Vec3D& b)
269 {
270  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);
271 }
272 
281 {
282  return std::sqrt(getDistanceSquared(a, b));
283 }
284 
294 {
295  return ((a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y) + (a.Z - b.Z) * (a.Z - b.Z));
296 }
297 
305 {
306  return (a.X * a.X + a.Y * a.Y + a.Z * a.Z);
307 }
308 
314 {
315  return (X * X + Y * Y + Z * Z);
316 }
317 
323 Mdouble Vec3D::getComponent(const int index) const
324  {
325  switch (index)
326  {
327  case 0:
328  return X;
329  case 1:
330  return Y;
331  case 2:
332  return Z;
333  default:
334  logger(ERROR, "[Vector::getComponent] Index = %, which is too high for a 3D vector (should be 0-2).", index);
335  return 0;
336  }
337 }
338 
345 void Vec3D::setComponent(const int index, const double val)
346 {
347  switch (index)
348  {
349  case 0:
350  X = val;
351  break;
352  case 1:
353  Y = val;
354  break;
355  case 2:
356  Z = val;
357  break;
358  default:
359  logger(ERROR, "[Vector::setComponent] Index = %, which is too high for a 3D vector (should be 0-2).", index);
360  }
361 }
362 
368 {
369  return Vec3D(std::sqrt(X * X + Y * Y), std::atan2(Y, X), Z);
370 }
371 
377 {
378  return Vec3D(X * std::cos(Y), X * std::sin(Y), Z);
379 }
380 
390 bool Vec3D::isEqualTo(const Vec3D& other, const double tol) const
391  {
392  if ((Vec3D::getLengthSquared(*this - other)) <= tol * tol)
393  {
394  return true;
395  }
396  else
397  {
398  return false;
399  }
400 }
401 
402 // void ConvertToCylindricalCoordinates()
403 // {
404 // double R = sqrt(X*X+Y*Y); Y = atan2(Y,X); X = R; return;
405 // }
406 //
407 // void ConvertFromCylindricalCoordinates()
408 // {
409 // double Xnew = X * cos(Y); Y = X * sin(Y); X = Xnew; return;
410 // }
411 
417 {
418  return std::sqrt(getLengthSquared());
419 }
420 
428 {
429  return a.getLength();
430 }
431 
442 {
443  Mdouble Length2 = a.getLengthSquared();
444  if (Length2 != 0.0)
445  return a / std::sqrt(Length2);
446  else
447  return Vec3D(0, 0, 0);
448 }
449 
457 std::ostream& operator<<(std::ostream& os, const Vec3D& a)
458 {
459  os << a.X << ' ' << a.Y << ' ' << a.Z;
460  return os;
461 }
462 
470 std::istream& operator>>(std::istream& is, Vec3D& a)
471 {
472  is >> a.X >> a.Y >> a.Z;
473  return is;
474 }
475 
484 Vec3D operator+(const Mdouble a, const Vec3D& b)
485 {
486  return Vec3D(b.X + a, b.Y + a, b.Z + a);
487 }
488 
497 Vec3D operator-(const Mdouble a, const Vec3D& b)
498 {
499  return Vec3D(a - b.X, a - b.Y, a - b.Z);
500 }
501 
510 {
511  return Vec3D(-a.X, -a.Y, -a.Z);
512 }
513 
522 Vec3D operator*(const Mdouble a, const Vec3D& b)
523 {
524  return Vec3D(b.X * a, b.Y * a, b.Z * a);
525 }
Vec3D getCylindricalCoordinates() const
Returns the representation of this Vec3D in cylindrical coordinates.
Definition: Vector.cc:367
static Mdouble getLengthSquared(const Vec3D &a)
Calculates the squared length of a Vec3D: .
Definition: Vector.cc:304
Mdouble X
the vector components
Definition: Vector.h:52
static Vec3D getUnitVector(const Vec3D &a)
Returns a unit Vec3D based on a.
Definition: Vector.cc:441
Vec3D()
constructor
Definition: Vector.cc:31
Vec3D getFromCylindricalCoordinates() const
Returns the representation of this Vec3D in cartesian coordinates.
Definition: Vector.cc:376
std::istream & operator>>(std::istream &is, Vec3D &a)
Definition: Vector.cc:470
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
Vec3D operator-(const Mdouble a, const Vec3D &b)
Definition: Vector.cc:497
void normalize()
Makes this Vec3D unit length.
Definition: Vector.cc:234
void setLength(Mdouble length)
Make this Vec3D a certain length.
Definition: Vector.cc:244
Vec3D operator*(const Mdouble a) const
Multiplies by a scalar.
Definition: Vector.cc:113
double Mdouble
Vec3D & operator-=(const Vec3D &a)
Subtracts another vector.
Definition: Vector.cc:146
Vec3D & operator+=(const Vec3D &a)
Adds another vector.
Definition: Vector.cc:133
void setZero()
Sets all elements to zero.
Definition: Vector.cc:52
static Vec3D sqrt(const Vec3D &a)
Calculates the pointwise square root of a Vec3D.
Definition: Vector.cc:256
static Mdouble dot(const Vec3D &a, const Vec3D &b)
Calculates the dot product of two Vec3D: .
Definition: Vector.cc:187
Vec3D operator/(const Mdouble a) const
Divides by a scalar.
Definition: Vector.cc:123
static Vec3D square(const Vec3D &a)
Calculates the pointwise square of a Vec3D.
Definition: Vector.cc:225
bool isEqualTo(const Vec3D &other, const double tol) const
Checks if the length this Vec3D is equal the length of other with a certain tolerance.
Definition: Vector.cc:390
bool isZero() const
Checks if ALL elements are zero.
Definition: Vector.cc:63
Mdouble getLengthSquared() const
Calculates the squared length of this Vec3D: .
Definition: Vector.cc:313
static Mdouble getLength(const Vec3D &a)
Calculates the length of a Vec3D: .
Definition: Vector.cc:427
Vec3D operator+(const Vec3D &a) const
Adds another vector.
Definition: Vector.cc:73
Vec3D operator*(const Mdouble a, const Vec3D &b)
Definition: Vector.cc:522
static Vec3D min(const Vec3D &a, const Vec3D &b)
Calculates the pointwise minimum of two Vec3D.
Definition: Vector.cc:213
std::ostream & operator<<(std::ostream &os, const Vec3D &a)
Definition: Vector.cc:457
static Mdouble getDistance(const Vec3D &a, const Vec3D &b)
Calculates the distance between two Vec3D: .
Definition: Vector.cc:280
static Vec3D cross(const Vec3D &a, const Vec3D &b)
Calculates the cross product of two Vec3D: .
Definition: Vector.cc:268
void setComponent(const int index, const double val)
Sets the requested component of this Vec3D to the requested value.
Definition: Vector.cc:345
Mdouble Y
Definition: Vector.h:52
Vec3D & operator/=(const Mdouble a)
Divides by a scalar.
Definition: Vector.cc:172
Vec3D operator+(const Mdouble a, const Vec3D &b)
Definition: Vector.cc:484
Mdouble getLength() const
Calculates the length of this Vec3D: .
Definition: Vector.cc:416
static Mdouble getDistanceSquared(const Vec3D &a, const Vec3D &b)
Calculates the squared distance between two Vec3D: .
Definition: Vector.cc:293
Vec3D operator-(const Vec3D &a) const
Subtracts another vector.
Definition: Vector.cc:83
Implementation of a 3D vector (by Vitaliy).
Definition: Vector.h:45
Mdouble Z
Definition: Vector.h:52
Mdouble getComponent(const int index) const
Returns the requested component of this Vec3D.
Definition: Vector.cc:323
static Vec3D max(const Vec3D &a, const Vec3D &b)
Calculates the pointwise maximum of two Vec3D.
Definition: Vector.cc:200
Vec3D & operator*=(const Mdouble a)
Multiplies by a scalar.
Definition: Vector.cc:159