vector3.h
Go to the documentation of this file.
1 
7 #ifndef VECTOR3_H
8 #define VECTOR3_H
9 
10 namespace argos {
11  class CVector3;
12  class CVector2;
13  class CQuaternion;
14  class CRotationMatrix3;
15 }
16 
17 #include <argos3/core/utility/math/general.h>
18 #include <argos3/core/utility/math/angles.h>
19 #include <argos3/core/utility/math/vector2.h>
20 #include <argos3/core/utility/math/matrix/matrix.h>
21 #include <argos3/core/utility/string_utilities.h>
22 #include <array>
23 #include <iostream>
24 #include <cmath>
25 
26 namespace argos {
27 
31  class CVector3 {
32 
33  public:
34 
36  static const CVector3 X;
37 
39  static const CVector3 Y;
40 
42  static const CVector3 Z;
43 
45  static const CVector3 ZERO;
46 
47  public:
48 
55  m_fX(0.0),
56  m_fY(0.0),
57  m_fZ(0.0) {
58  }
59 
69  Real f_y,
70  Real f_z) :
71  m_fX(f_x),
72  m_fY(f_y),
73  m_fZ(f_z) {
74  }
75 
82  CVector3(const std::array<Real, 3>& arr_coordinates) {
83  Set(arr_coordinates);
84  }
85 
95  inline CVector3(Real f_length,
96  const CRadians& c_inclination,
97  const CRadians& c_azimuth) {
98  FromSphericalCoords(f_length, c_inclination, c_azimuth);
99  }
100 
105  inline Real GetX() const {
106  return m_fX;
107  }
108 
113  inline void SetX(const Real f_x) {
114  m_fX = f_x;
115  }
116 
121  inline Real GetY() const {
122  return m_fY;
123  }
124 
129  inline void SetY(const Real f_y) {
130  m_fY = f_y;
131  }
132 
137  inline Real GetZ() const {
138  return m_fZ;
139  }
140 
145  inline void SetZ(const Real f_z) {
146  m_fZ = f_z;
147  }
148 
155  inline void Set(const Real f_x,
156  const Real f_y,
157  const Real f_z) {
158  m_fX = f_x;
159  m_fY = f_y;
160  m_fZ = f_z;
161  }
162 
167  inline void Set(const std::array<Real, 3>& arr_coordinates) {
168  m_fX = arr_coordinates[0];
169  m_fY = arr_coordinates[1];
170  m_fZ = arr_coordinates[2];
171  }
172 
181  const CRadians& c_inclination,
182  const CRadians& c_azimuth) {
183  Real fInclinationSin, fInclinationCos;
184  Real fAzimuthSin, fAzimuthCos;
185 #ifdef ARGOS_SINCOS
186  SinCos(c_inclination, fInclinationSin, fInclinationCos);
187  SinCos(c_azimuth, fAzimuthSin, fAzimuthCos);
188 #else
189  fInclinationSin = Sin(c_inclination);
190  fInclinationCos = Cos(c_inclination);
191  fAzimuthSin = Sin(c_azimuth);
192  fAzimuthCos = Cos(c_azimuth);
193 #endif
194  m_fX = f_length * fInclinationSin * fAzimuthCos;
195  m_fY = f_length * fInclinationSin * fAzimuthSin;
196  m_fZ = f_length * fInclinationCos;
197  return *this;
198  }
199 
207  inline void ToSphericalCoords(Real& f_radius,
208  CRadians& c_inclination,
209  CRadians& c_azimuth) const {
210  f_radius = Length();
211  c_inclination = ACos(m_fZ / f_radius);
212  c_azimuth = ATan2(m_fY, m_fX);
213  }
214 
219  inline Real SquareLength() const {
220  return Square(m_fX) + Square(m_fY) + Square(m_fZ);
221  }
222 
227  inline Real Length() const {
228  return Sqrt(SquareLength());
229  }
230 
237  inline CVector3& Normalize() {
238  *this /= Length();
239  return *this;
240  }
241 
247  inline CVector3& RotateX(const CRadians& c_angle) {
248  Real fSin, fCos;
249 #ifdef ARGOS_SINCOS
250  SinCos(c_angle, fSin, fCos);
251 #else
252  fSin = Sin(c_angle);
253  fCos = Cos(c_angle);
254 #endif
255  Real fNewY = m_fY * fCos - m_fZ * fSin;
256  Real fNewZ = m_fY * fSin + m_fZ * fCos;
257  m_fY = fNewY;
258  m_fZ = fNewZ;
259  return *this;
260  }
261 
267  inline CVector3& RotateY(const CRadians& c_angle) {
268  Real fSin, fCos;
269 #ifdef ARGOS_SINCOS
270  SinCos(c_angle, fSin, fCos);
271 #else
272  fSin = Sin(c_angle);
273  fCos = Cos(c_angle);
274 #endif
275  Real fNewX = m_fX * fCos + m_fZ * fSin;
276  Real fNewZ = m_fZ * fCos - m_fX * fSin;
277  m_fX = fNewX;
278  m_fZ = fNewZ;
279  return *this;
280  }
281 
287  inline CVector3& RotateZ(const CRadians& c_angle) {
288  Real fSin, fCos;
289 #ifdef ARGOS_SINCOS
290  SinCos(c_angle, fSin, fCos);
291 #else
292  fSin = Sin(c_angle);
293  fCos = Cos(c_angle);
294 #endif
295  Real fNewX = m_fX * fCos - m_fY * fSin;
296  Real fNewY = m_fX * fSin + m_fY * fCos;
297  m_fX = fNewX;
298  m_fY = fNewY;
299  return *this;
300  }
301 
314  inline CVector3& RotateZ(const CVector2& c_vector) {
315  Real fNewX = m_fX * c_vector.GetX() - m_fY * c_vector.GetY();
316  Real fNewY = m_fX * c_vector.GetY() + m_fY * c_vector.GetX();
317  m_fX = fNewX;
318  m_fY = fNewY;
319  return *this;
320  }
321 
328  CVector3& Rotate(const CQuaternion& c_quaternion);
329 
335  inline CRadians GetAngleWith(const CVector3& c_other) {
336  return ACos(DotProduct(c_other) /
337  (Length() * c_other.Length()));
338  }
339 
344  inline CRadians GetXAngle() const {
345  return ATan2(m_fZ, m_fY);
346  }
347 
352  inline CRadians GetYAngle() const {
353  return ATan2(m_fX, m_fZ);
354  }
355 
360  inline CRadians GetZAngle() const {
361  return ATan2(m_fY, m_fX);
362  }
363 
369  inline Real DotProduct(const CVector3& c_vector3) const {
370  return
371  m_fX * c_vector3.m_fX +
372  m_fY * c_vector3.m_fY +
373  m_fZ * c_vector3.m_fZ;
374  }
375 
382  inline CVector3& CrossProduct(const CVector3& c_vector3) {
383  Real fNewX, fNewY, fNewZ;
384  fNewX = m_fY * c_vector3.m_fZ - m_fZ * c_vector3.m_fY;
385  fNewY = m_fZ * c_vector3.m_fX - m_fX * c_vector3.m_fZ;
386  fNewZ = m_fX * c_vector3.m_fY - m_fY * c_vector3.m_fX;
387  m_fX = fNewX;
388  m_fY = fNewY;
389  m_fZ = fNewZ;
390  return *this;
391  }
392 
398  inline CVector2& ProjectOntoXY(CVector2& c_proj) const {
399  c_proj.Set(m_fX,m_fY);
400  return c_proj;
401  }
402 
408  inline CVector2& ProjectOntoYZ(CVector2& c_proj) const {
409  c_proj.Set(m_fY,m_fZ);
410  return c_proj;
411  }
412 
418  inline CVector2& ProjectOntoXZ(CVector2& c_proj) const {
419  c_proj.Set(m_fX,m_fZ);
420  return c_proj;
421  }
422 
428  inline CVector3& Negate() {
429  m_fX = -m_fX;
430  m_fY = -m_fY;
431  m_fZ = -m_fZ;
432  return *this;
433  }
434 
442  inline Real operator[](UInt32 un_index) const {
443  switch(un_index) {
444  case 0: return m_fX;
445  case 1: return m_fY;
446  case 2: return m_fZ;
447  default: THROW_ARGOSEXCEPTION("Real Vector3::operator[]: index " << un_index << " out of bounds");
448  }
449  }
450 
458  inline Real& operator[](UInt32 un_index) {
459  switch(un_index) {
460  case 0: return m_fX;
461  case 1: return m_fY;
462  case 2: return m_fZ;
463  default: THROW_ARGOSEXCEPTION("Real Vector3::operator[]: index " << un_index << " out of bounds");
464  }
465  }
466 
473  inline bool operator==(const CVector3& c_vector3) const {
474  return m_fX == c_vector3.m_fX && m_fY == c_vector3.m_fY && m_fZ == c_vector3.m_fZ;
475  }
476 
483  inline bool operator!=(const CVector3& c_vector3) const {
484  return !((*this) == c_vector3);
485  }
486 
494  inline bool operator<(const CVector3& c_vector3) const {
495  return m_fX < c_vector3.m_fX && m_fY < c_vector3.m_fY && m_fZ < c_vector3.m_fZ;
496  }
497 
505  inline bool operator<=(const CVector3& c_vector3) const {
506  return m_fX <= c_vector3.m_fX && m_fY <= c_vector3.m_fY && m_fZ <= c_vector3.m_fZ;
507  }
508 
516  inline bool operator>(const CVector3& c_vector3) const {
517  return m_fX > c_vector3.m_fX && m_fY > c_vector3.m_fY && m_fZ > c_vector3.m_fZ;
518  }
519 
527  inline bool operator>=(const CVector3& c_vector3) const {
528  return m_fX >= c_vector3.m_fX && m_fY >= c_vector3.m_fY && m_fZ >= c_vector3.m_fZ;
529  }
530 
535  inline CVector3 operator-() const {
536  return CVector3(-m_fX, -m_fY, -m_fZ);
537  }
538 
544  inline CVector3& operator+=(const CVector3& c_vector3) {
545  m_fX += c_vector3.m_fX;
546  m_fY += c_vector3.m_fY;
547  m_fZ += c_vector3.m_fZ;
548  return *this;
549  }
550 
556  inline CVector3& operator-=(const CVector3& c_vector3) {
557  m_fX -= c_vector3.m_fX;
558  m_fY -= c_vector3.m_fY;
559  m_fZ -= c_vector3.m_fZ;
560  return *this;
561  }
562 
568  inline CVector3& operator*=(Real f_value) {
569  m_fX *= f_value;
570  m_fY *= f_value;
571  m_fZ *= f_value;
572  return *this;
573  }
574 
580  inline CVector3& operator/=(Real f_value) {
581  m_fX /= f_value;
582  m_fY /= f_value;
583  m_fZ /= f_value;
584  return *this;
585  }
586 
592  inline CVector3 operator+(const CVector3& c_vector3) const {
593  CVector3 cResult(*this);
594  cResult += c_vector3;
595  return cResult;
596  }
597 
603  inline CVector3 operator-(const CVector3& c_vector3) const {
604  CVector3 cResult(*this);
605  cResult -= c_vector3;
606  return cResult;
607  }
608 
614  inline CVector3 operator*(Real f_value) const {
615  CVector3 cResult(*this);
616  cResult *= f_value;
617  return cResult;
618  }
619 
625  inline CVector3 operator/(const Real f_value) const {
626  CVector3 cResult(*this);
627  cResult /= f_value;
628  return cResult;
629  }
630 
634  operator CMatrix<1,3>() const {
635  CMatrix<1,3> cMatrix;
636  cMatrix(0,0) = m_fX;
637  cMatrix(0,1) = m_fY;
638  cMatrix(0,2) = m_fZ;
639  return cMatrix;
640  }
641 
645  operator CMatrix<3,1>() const {
646  CMatrix<3,1> cMatrix;
647  cMatrix(0,0) = m_fX;
648  cMatrix(1,0) = m_fY;
649  cMatrix(2,0) = m_fZ;
650  return cMatrix;
651  }
652 
659  inline friend CVector3 operator*(Real f_value,
660  const CVector3& c_vector3) {
661  return c_vector3 * f_value;
662  }
663 
670  inline friend std::ostream& operator<<(std::ostream& c_os,
671  const CVector3& c_vector3) {
672  c_os << c_vector3.m_fX << ","
673  << c_vector3.m_fY << ","
674  << c_vector3.m_fZ;
675  return c_os;
676  }
677 
684  inline friend std::istream& operator>>(std::istream& c_is,
685  CVector3& c_vector3) {
686  Real fValues[3];
687  ParseValues<Real>(c_is, 3, fValues, ',');
688  c_vector3.Set(fValues[0], fValues[1], fValues[2]);
689  return c_is;
690  }
691 
692  private:
693 
695  Real m_fX;
696 
698  Real m_fY;
699 
701  Real m_fZ;
702 
703  friend class CRotationMatrix3;
705 
706  };
707 
708  /****************************************/
709  /****************************************/
710 
717  inline Real SquareDistance(const CVector3& c_v1, const CVector3& c_v2) {
718  return (c_v1 - c_v2).SquareLength();
719  }
720 
727  inline Real Distance(const CVector3& c_v1, const CVector3& c_v2) {
728  return (c_v1 - c_v2).Length();
729  }
730 
731 /****************************************/
732 /****************************************/
733 
734 }
735 
736 #endif
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
#define Sqrt
Definition: general.h:64
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
T Square(const T &t_v)
Returns the square of the value of the passed argument.
Definition: general.h:128
CRadians ACos(Real f_value)
Computes the arccosine of the passed value.
Definition: angles.h:622
Real SquareDistance(const CVector2 &c_v1, const CVector2 &c_v2)
Computes the square distance between the passed vectors.
Definition: vector2.h:453
Real Cos(const CRadians &c_radians)
Computes the cosine of the passed value in radians.
Definition: angles.h:595
Real Distance(const CVector2 &c_v1, const CVector2 &c_v2)
Computes the distance between the passed vectors.
Definition: vector2.h:463
void SinCos(const CRadians &c_radians, Real &f_sin, Real &f_cos)
Computes the sine and cosine of the passed value in radians.
Definition: angles.h:574
Real Sin(const CRadians &c_radians)
Computes the sine of the passed value in radians.
Definition: angles.h:586
CRadians ATan2(const Real f_y, const Real f_x)
Computes the arctangent of the passed values.
Definition: angles.h:633
It defines the basic type CRadians, used to store an angle value in radians.
Definition: angles.h:42
A 2D vector class.
Definition: vector2.h:27
Real GetY() const
Returns the y coordinate of this vector.
Definition: vector2.h:110
void Set(Real f_x, Real f_y)
Sets the vector contents from Cartesian coordinates.
Definition: vector2.h:127
Real GetX() const
Returns the x coordinate of this vector.
Definition: vector2.h:94
A 3D vector class.
Definition: vector3.h:31
Real operator[](UInt32 un_index) const
Returns a Cartesian coordinate of this vector.
Definition: vector3.h:442
Real Length() const
Returns the length of this vector.
Definition: vector3.h:227
CVector3 & operator+=(const CVector3 &c_vector3)
Sums the passed vector to this vector.
Definition: vector3.h:544
static const CVector3 Y
The y axis.
Definition: vector3.h:39
CVector3 & CrossProduct(const CVector3 &c_vector3)
Calculates the cross product between this vector and the passed one.
Definition: vector3.h:382
void SetY(const Real f_y)
Sets the y coordinate of this vector.
Definition: vector3.h:129
CVector3 operator-(const CVector3 &c_vector3) const
Returns a new vector containing the subtraction between this vector and the passed one.
Definition: vector3.h:603
CVector3 & Rotate(const CQuaternion &c_quaternion)
Rotates this vector by the given quaternion.
Definition: vector3.cpp:23
CVector3 operator/(const Real f_value) const
Returns a new vector containing the division between this vector and the passed value.
Definition: vector3.h:625
Real SquareLength() const
Returns the square length of this vector.
Definition: vector3.h:219
Real GetX() const
Returns the x coordinate of this vector.
Definition: vector3.h:105
CVector3 operator*(Real f_value) const
Returns a new vector containing the multiplication between this vector and the passed value.
Definition: vector3.h:614
CRadians GetZAngle() const
Returns the angle between this vector and the z axis.
Definition: vector3.h:360
friend CVector3 operator*(Real f_value, const CVector3 &c_vector3)
Returns a new vector containing the multiplication between the passed value and the passed vector.
Definition: vector3.h:659
CVector3 & operator/=(Real f_value)
Divides this vector by the given value.
Definition: vector3.h:580
void SetX(const Real f_x)
Sets the x coordinate of this vector.
Definition: vector3.h:113
CVector3 & operator-=(const CVector3 &c_vector3)
Subtracts the passed vector from this vector.
Definition: vector3.h:556
CRadians GetYAngle() const
Returns the angle between this vector and the y axis.
Definition: vector3.h:352
bool operator!=(const CVector3 &c_vector3) const
Returns true if this vector and the passed one are not equal.
Definition: vector3.h:483
Real DotProduct(const CVector3 &c_vector3) const
Returns the dot product between this vector and the passed one.
Definition: vector3.h:369
CVector3 operator-() const
Returns a negated copy of this vector.
Definition: vector3.h:535
CVector3()
Class constructor.
Definition: vector3.h:54
CVector3 & RotateX(const CRadians &c_angle)
Rotates this vector wrt the x axis.
Definition: vector3.h:247
bool operator>=(const CVector3 &c_vector3) const
Returns true if this vector is greater than or equal to the passed one.
Definition: vector3.h:527
bool operator==(const CVector3 &c_vector3) const
Returns true if this vector and the passed one are equal.
Definition: vector3.h:473
CVector2 & ProjectOntoXZ(CVector2 &c_proj) const
Calculates the projection of this vector onto the xz plane.
Definition: vector3.h:418
bool operator<=(const CVector3 &c_vector3) const
Returns true if this vector is smaller than or equal to the passed one.
Definition: vector3.h:505
CVector3 & Normalize()
Normalizes this vector.
Definition: vector3.h:237
CVector3 & Negate()
Negates this vector.
Definition: vector3.h:428
void Set(const Real f_x, const Real f_y, const Real f_z)
Sets the vector contents from Cartesian coordinates.
Definition: vector3.h:155
CVector3 & FromSphericalCoords(Real f_length, const CRadians &c_inclination, const CRadians &c_azimuth)
Sets the vector contents from spherical coordinates.
Definition: vector3.h:180
void SetZ(const Real f_z)
Sets the z coordinate of this vector.
Definition: vector3.h:145
static const CVector3 X
The x axis.
Definition: vector3.h:36
CRadians GetXAngle() const
Returns the angle between this vector and the x axis.
Definition: vector3.h:344
Real GetY() const
Returns the y coordinate of this vector.
Definition: vector3.h:121
CVector3 & operator*=(Real f_value)
Multiplies this vector by the given value.
Definition: vector3.h:568
bool operator<(const CVector3 &c_vector3) const
Returns true if this vector is smaller than the passed one.
Definition: vector3.h:494
static const CVector3 Z
The z axis.
Definition: vector3.h:42
Real GetZ() const
Returns the z coordinate of this vector.
Definition: vector3.h:137
CVector3(Real f_length, const CRadians &c_inclination, const CRadians &c_azimuth)
Class constructor.
Definition: vector3.h:95
void Set(const std::array< Real, 3 > &arr_coordinates)
Sets the vector contents from Cartesian coordinates.
Definition: vector3.h:167
CVector3 & RotateZ(const CRadians &c_angle)
Rotates this vector wrt the z axis.
Definition: vector3.h:287
Real & operator[](UInt32 un_index)
Returns a Cartesian coordinate of this vector.
Definition: vector3.h:458
CVector2 & ProjectOntoXY(CVector2 &c_proj) const
Calculates the projection of this vector onto the xy plane.
Definition: vector3.h:398
void ToSphericalCoords(Real &f_radius, CRadians &c_inclination, CRadians &c_azimuth) const
Returns the vector contents as spherical coordinates.
Definition: vector3.h:207
CVector3 & RotateZ(const CVector2 &c_vector)
Rotates this vector wrt the z axis.
Definition: vector3.h:314
CVector3 operator+(const CVector3 &c_vector3) const
Returns a new vector containing the sum between this vector and the passed one.
Definition: vector3.h:592
static const CVector3 ZERO
The zero vector (0,0,0)
Definition: vector3.h:45
friend std::ostream & operator<<(std::ostream &c_os, const CVector3 &c_vector3)
Serializes the contents of the passed vector onto a stream.
Definition: vector3.h:670
bool operator>(const CVector3 &c_vector3) const
Returns true if this vector is greater than the passed one.
Definition: vector3.h:516
friend std::istream & operator>>(std::istream &c_is, CVector3 &c_vector3)
Deserializes the contents of a stream and stores them into the passed vector.
Definition: vector3.h:684
CVector2 & ProjectOntoYZ(CVector2 &c_proj) const
Calculates the projection of this vector onto the yz plane.
Definition: vector3.h:408
CVector3(Real f_x, Real f_y, Real f_z)
Class constructor.
Definition: vector3.h:68
CVector3 & RotateY(const CRadians &c_angle)
Rotates this vector wrt the y axis.
Definition: vector3.h:267
CVector3(const std::array< Real, 3 > &arr_coordinates)
Class constructor.
Definition: vector3.h:82
CRadians GetAngleWith(const CVector3 &c_other)
Returns the angle between this vector and the passed vector.
Definition: vector3.h:335