MercuryDPM  Alpha
 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  X += a.X;
116  Y += a.Y;
117  Z += a.Z;
118  return *this;
119 }
120 
127 {
128  X -= a.X;
129  Y -= a.Y;
130  Z -= a.Z;
131  return *this;
132 }
133 
140 {
141  X *= a;
142  Y *= a;
143  Z *= a;
144  return *this;
145 }
146 
153 {
154  X /= a;
155  Y /= a;
156  Z /= a;
157  return *this;
158 }
159 
167 Mdouble Vec3D::dot(const Vec3D& a, const Vec3D& b)
168 {
169  return a.X * b.X + a.Y * b.Y + a.Z * b.Z;
170 }
171 
180 Vec3D Vec3D::max(const Vec3D& a, const Vec3D& b)
181 {
182  return Vec3D(std::max(a.X, b.X), std::max(a.Y, b.Y), std::max(a.Z, b.Z));
183 }
184 
193 Vec3D Vec3D::min(const Vec3D& a, const Vec3D& b)
194 {
195  return Vec3D(std::min(a.X, b.X), std::min(a.Y, b.Y), std::min(a.Z, b.Z));
196 }
197 
206 {
207  return Vec3D(a.X * a.X, a.Y * a.Y, a.Z * a.Z);
208 }
209 
215 {
216  Mdouble length = this->getLength();
217  if (length!=0)
218  {
219  *this /= this->getLength();
220  } else {
221  logger(WARN, "Normalizing a vector of length 0");
222  }
223 }
224 
231 {
232  this->normalize();
233  *this *= length;
234 }
235 
244 {
245  return Vec3D(std::sqrt(a.X), std::sqrt(a.Y), std::sqrt(a.Z));
246 }
247 
255 Vec3D Vec3D::cross(const Vec3D& a, const Vec3D& b)
256 {
257  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);
258 }
259 
268 {
269  return std::sqrt(getDistanceSquared(a, b));
270 }
271 
281 {
282  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));
283 }
284 
292 {
293  return (a.X * a.X + a.Y * a.Y + a.Z * a.Z);
294 }
295 
301 {
302  return (X * X + Y * Y + Z * Z);
303 }
304 
310 Mdouble Vec3D::getComponent(const int index) const
311  {
312  switch (index)
313  {
314  case 0:
315  return X;
316  case 1:
317  return Y;
318  case 2:
319  return Z;
320  default:
321  logger(ERROR, "[Vector::getComponent] Index = %, which is too high for a 3D vector (should be 0-2).", index);
322  return 0;
323  }
324 }
325 
332 void Vec3D::setComponent(const int index, const double val)
333 {
334  switch (index)
335  {
336  case 0:
337  X = val;
338  break;
339  case 1:
340  Y = val;
341  break;
342  case 2:
343  Z = val;
344  break;
345  default:
346  logger(ERROR, "[Vector::setComponent] Index = %, which is too high for a 3D vector (should be 0-2).", index);
347  }
348 }
349 
355 {
356  return Vec3D(std::sqrt(X * X + Y * Y), std::atan2(Y, X), Z);
357 }
358 
364 {
365  return Vec3D(X * std::cos(Y), X * std::sin(Y), Z);
366 }
367 
377 bool Vec3D::isEqualTo(const Vec3D& other, const double tol) const
378  {
379  if ((Vec3D::getLengthSquared(*this - other)) <= tol * tol)
380  {
381  return true;
382  }
383  else
384  {
385  return false;
386  }
387 }
388 
389 // void ConvertToCylindricalCoordinates()
390 // {
391 // double R = sqrt(X*X+Y*Y); Y = atan2(Y,X); X = R; return;
392 // }
393 //
394 // void ConvertFromCylindricalCoordinates()
395 // {
396 // double Xnew = X * cos(Y); Y = X * sin(Y); X = Xnew; return;
397 // }
398 
404 {
405  return std::sqrt(getLengthSquared());
406 }
407 
415 {
416  return a.getLength();
417 }
418 
429 {
430  Mdouble Length2 = a.getLengthSquared();
431  if (Length2 != 0.0)
432  return a / std::sqrt(Length2);
433  else
434  return Vec3D(0, 0, 0);
435 }
436 
444 std::ostream& operator<<(std::ostream& os, const Vec3D& a)
445 {
446  os << a.X << ' ' << a.Y << ' ' << a.Z;
447  return os;
448 }
449 
457 std::istream& operator>>(std::istream& is, Vec3D& a)
458 {
459  is >> a.X >> a.Y >> a.Z;
460  return is;
461 }
462 
471 {
472  return Vec3D(-a.X, -a.Y, -a.Z);
473 }
474 
483 Vec3D operator*(const Mdouble a, const Vec3D& b)
484 {
485  return Vec3D(b.X * a, b.Y * a, b.Z * a);
486 }
Vec3D getCylindricalCoordinates() const
Returns the representation of this Vec3D in cylindrical coordinates.
Definition: Vector.cc:354
static Mdouble getLengthSquared(const Vec3D &a)
Calculates the squared length of a Vec3D: .
Definition: Vector.cc:291
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:428
Vec3D()
constructor
Definition: Vector.cc:31
Vec3D getFromCylindricalCoordinates() const
Returns the representation of this Vec3D in cartesian coordinates.
Definition: Vector.cc:363
std::istream & operator>>(std::istream &is, Vec3D &a)
Definition: Vector.cc:457
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
void normalize()
Makes this Vec3D unit length.
Definition: Vector.cc:214
void setLength(Mdouble length)
Make this Vec3D a certain length.
Definition: Vector.cc:230
Vec3D operator*(const Mdouble a) const
Adds a scalar.
Definition: Vector.cc:93
double Mdouble
Vec3D & operator-=(const Vec3D &a)
Subtracts another vector.
Definition: Vector.cc:126
Vec3D & operator+=(const Vec3D &a)
Adds another vector.
Definition: Vector.cc:113
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:243
static Mdouble dot(const Vec3D &a, const Vec3D &b)
Calculates the dot product of two Vec3D: .
Definition: Vector.cc:167
Vec3D operator/(const Mdouble a) const
Divides by a scalar.
Definition: Vector.cc:103
static Vec3D square(const Vec3D &a)
Calculates the pointwise square of a Vec3D.
Definition: Vector.cc:205
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:377
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:300
static Mdouble getLength(const Vec3D &a)
Calculates the length of a Vec3D: .
Definition: Vector.cc:414
Mdouble cos(Mdouble x)
Definition: ExtendedMath.cc:60
Vec3D operator+(const Vec3D &a) const
Adds another vector.
Definition: Vector.cc:73
Vec3D operator*(const Mdouble a, const Vec3D &b)
Definition: Vector.cc:483
Mdouble sin(Mdouble x)
Definition: ExtendedMath.cc:42
static Vec3D min(const Vec3D &a, const Vec3D &b)
Calculates the pointwise minimum of two Vec3D.
Definition: Vector.cc:193
std::ostream & operator<<(std::ostream &os, const Vec3D &a)
Definition: Vector.cc:444
static Mdouble getDistance(const Vec3D &a, const Vec3D &b)
Calculates the distance between two Vec3D: .
Definition: Vector.cc:267
static Vec3D cross(const Vec3D &a, const Vec3D &b)
Calculates the cross product of two Vec3D: .
Definition: Vector.cc:255
void setComponent(const int index, const double val)
Sets the requested component of this Vec3D to the requested value.
Definition: Vector.cc:332
Mdouble Y
Definition: Vector.h:52
Vec3D & operator/=(const Mdouble a)
Divides by a scalar.
Definition: Vector.cc:152
Vec3D operator-(const Vec3D &a)
Definition: Vector.cc:470
Mdouble getLength() const
Calculates the length of this Vec3D: .
Definition: Vector.cc:403
static Mdouble getDistanceSquared(const Vec3D &a, const Vec3D &b)
Calculates the squared distance between two Vec3D: .
Definition: Vector.cc:280
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:310
static Vec3D max(const Vec3D &a, const Vec3D &b)
Calculates the pointwise maximum of two Vec3D.
Definition: Vector.cc:180
Vec3D & operator*=(const Mdouble a)
Multiplies by a scalar.
Definition: Vector.cc:139