vector2.h
Go to the documentation of this file.
1 
7 #ifndef VECTOR2_H
8 #define VECTOR2_H
9 
10 namespace argos {
11  class CRotationMatrix2;
12 }
13 
14 #include <argos3/core/utility/math/general.h>
15 #include <argos3/core/utility/math/angles.h>
16 #include <argos3/core/utility/math/matrix/matrix.h>
17 #include <argos3/core/utility/string_utilities.h>
18 #include <array>
19 #include <iostream>
20 #include <cmath>
21 
22 namespace argos {
23 
27  class CVector2 {
28 
29  friend class CRotationMatrix2;
30  friend class CTransformationMatrix2;
31  friend class CPose2;
32 
33  public:
34 
36  static const CVector2 X;
37 
39  static const CVector2 Y;
40 
42  static const CVector2 ZERO;
43 
50  m_fX(0.0),
51  m_fY(0.0) {
52  }
53 
62  Real f_y) :
63  m_fX(f_x),
64  m_fY(f_y) {
65  }
66 
73  CVector2(const std::array<Real, 2>& arr_coordinates) {
74  Set(arr_coordinates);
75  }
76 
84  CVector2(Real f_length,
85  const CRadians& f_angle) :
86  m_fX(Cos(f_angle) * f_length),
87  m_fY(Sin(f_angle) * f_length) {
88  }
89 
94  inline Real GetX() const {
95  return m_fX;
96  }
97 
102  inline void SetX(Real f_x) {
103  m_fX = f_x;
104  }
105 
110  inline Real GetY() const {
111  return m_fY;
112  }
113 
118  inline void SetY(Real f_y) {
119  m_fY = f_y;
120  }
121 
127  inline void Set(Real f_x, Real f_y) {
128  m_fX = f_x;
129  m_fY = f_y;
130  }
131 
136  inline void Set(const std::array<Real, 2>& arr_coordinates) {
137  m_fX = arr_coordinates[0];
138  m_fY = arr_coordinates[1];
139  }
140 
148  inline void FromPolarCoordinates(Real f_length,
149  const CRadians& f_angle) {
150  m_fX = Cos(f_angle) * f_length;
151  m_fY = Sin(f_angle) * f_length;
152  }
153 
158  inline Real SquareLength() const {
159  return Square(m_fX) + Square(m_fY);
160  }
161 
166  inline Real Length() const {
167  return Sqrt(SquareLength());
168  }
169 
176  inline CVector2& Normalize() {
177  *this /= Length();
178  return *this;
179  }
180 
185  inline CRadians Angle() const {
186  return ATan2(m_fY, m_fX);
187  }
188 
194  inline CVector2& Rotate(const CRadians& c_angle) {
195  Real fSin, fCos;
196 #ifdef ARGOS_SINCOS
197  SinCos(c_angle, fSin, fCos);
198 #else
199  fSin = Sin(c_angle);
200  fCos = Cos(c_angle);
201 #endif
202  Real fX = m_fX * fCos - m_fY * fSin;
203  Real fY = m_fX * fSin + m_fY * fCos;
204  m_fX = fX;
205  m_fY = fY;
206  return *this;
207  }
208 
214  inline Real DotProduct(const CVector2& c_vector2) const {
215  return m_fX * c_vector2.m_fX + m_fY * c_vector2.m_fY;
216  }
217 
223  inline Real CrossProduct(const CVector2& c_vector2) const {
224  return m_fX * c_vector2.m_fY - m_fY * c_vector2.m_fX;
225  }
226 
236  inline CVector2& Scale(Real f_scale_x,
237  Real f_scale_y) {
238  m_fX *= f_scale_x;
239  m_fY *= f_scale_y;
240  return *this;
241  }
242 
248  Real fNewX = -m_fY;
249  m_fY = m_fX;
250  m_fX = fNewX;
251  return *this;
252  }
253 
258  inline CVector2& Absolute() {
259  m_fX = Abs(m_fX);
260  m_fY = Abs(m_fY);
261  return *this;
262  }
263 
270  inline bool operator==(const CVector2& c_vector2) const {
271  return (m_fX == c_vector2.m_fX && m_fY == c_vector2.m_fY);
272  }
273 
280  inline bool operator!=(const CVector2& c_vector2) const {
281  return (m_fX != c_vector2.m_fX || m_fY != c_vector2.m_fY);
282  }
283 
289  inline CVector2& operator+=(const CVector2& c_vector2) {
290  m_fX += c_vector2.m_fX;
291  m_fY += c_vector2.m_fY;
292  return *this;
293  }
294 
300  inline CVector2& operator-=(const CVector2& c_vector2) {
301  m_fX -= c_vector2.m_fX;
302  m_fY -= c_vector2.m_fY;
303  return *this;
304  }
305 
311  inline CVector2& operator*=(Real f_value) {
312  m_fX *= f_value;
313  m_fY *= f_value;
314  return *this;
315  }
316 
322  inline CVector2& operator/=(Real f_value) {
323  m_fX /= f_value;
324  m_fY /= f_value;
325  return *this;
326  }
327 
333  inline CVector2 operator+(const CVector2& c_vector2) const {
334  CVector2 cResult(*this);
335  cResult += c_vector2;
336  return cResult;
337  }
338 
344  inline CVector2 operator-(const CVector2& c_vector2) const {
345  CVector2 cResult(*this);
346  cResult -= c_vector2;
347  return cResult;
348  }
349 
355  inline CVector2 operator*(Real f_value) const {
356  CVector2 cResult(*this);
357  cResult *= f_value;
358  return cResult;
359  }
360 
366  inline CVector2 operator/(Real f_value) const {
367  CVector2 cResult(*this);
368  cResult /= f_value;
369  return cResult;
370  }
371 
375  operator CMatrix<1,2>() const {
376  CMatrix<1,2> cMatrix;
377  cMatrix(0,0) = m_fX;
378  cMatrix(0,1) = m_fY;
379  return cMatrix;
380  }
381 
385  operator CMatrix<2,1>() const {
386  CMatrix<2,1> cMatrix;
387  cMatrix(0,0) = m_fX;
388  cMatrix(1,0) = m_fY;
389  return cMatrix;
390  }
391 
398  inline friend CVector2 operator*(Real f_value,
399  const CVector2& c_vector2) {
400  return c_vector2 * f_value;
401  }
402 
403  inline CVector2 operator-() const {
404  return CVector2(-m_fX, -m_fY);
405  }
406 
413  inline friend std::ostream& operator<<(std::ostream& c_os,
414  const CVector2& c_vector2) {
415  c_os << c_vector2.m_fX << ","
416  << c_vector2.m_fY;
417  return c_os;
418  }
419 
426  inline friend std::istream& operator>>(std::istream& c_is,
427  CVector2& c_vector2) {
428  Real fValues[2];
429  ParseValues<Real> (c_is, 2, fValues, ',');
430  c_vector2.Set(fValues[0], fValues[1]);
431  return c_is;
432  }
433 
434  private:
435 
437  Real m_fX;
438 
440  Real m_fY;
441 
442  };
443 
444  /****************************************/
445  /****************************************/
446 
453  inline Real SquareDistance(const CVector2& c_v1, const CVector2& c_v2) {
454  return (c_v1 - c_v2).SquareLength();
455  }
456 
463  inline Real Distance(const CVector2& c_v1, const CVector2& c_v2) {
464  return (c_v1 - c_v2).Length();
465  }
466 
467  /****************************************/
468  /****************************************/
469 
470 }
471 
472 #endif
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
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
T Abs(const T &t_v)
Returns the absolute value of the passed argument.
Definition: general.h:25
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 Length() const
Returns the length of this vector.
Definition: vector2.h:166
CVector2(const std::array< Real, 2 > &arr_coordinates)
Class constructor.
Definition: vector2.h:73
Real DotProduct(const CVector2 &c_vector2) const
Returns the dot product between this vector and the passed one.
Definition: vector2.h:214
Real GetY() const
Returns the y coordinate of this vector.
Definition: vector2.h:110
CVector2 operator-(const CVector2 &c_vector2) const
Returns a new vector containing the subtraction between this vector and the passed one.
Definition: vector2.h:344
bool operator==(const CVector2 &c_vector2) const
Returns true if this vector and the passed one are equal.
Definition: vector2.h:270
Real SquareLength() const
Returns the square length of this vector.
Definition: vector2.h:158
Real CrossProduct(const CVector2 &c_vector2) const
Returns the cross product between this vector and the passed one.
Definition: vector2.h:223
CVector2(Real f_length, const CRadians &f_angle)
Class constructor.
Definition: vector2.h:84
CVector2(Real f_x, Real f_y)
Class constructor.
Definition: vector2.h:61
friend std::istream & operator>>(std::istream &c_is, CVector2 &c_vector2)
Deserializes the contents of a stream and stores them into the passed vector.
Definition: vector2.h:426
void Set(const std::array< Real, 2 > &arr_coordinates)
Sets the vector contents from Cartesian coordinates.
Definition: vector2.h:136
CRadians Angle() const
Returns the angle of this vector.
Definition: vector2.h:185
CVector2 & Scale(Real f_scale_x, Real f_scale_y)
Scales the vector by the wanted values.
Definition: vector2.h:236
CVector2 operator/(Real f_value) const
Returns a new vector containing the division between this vector and the passed value.
Definition: vector2.h:366
bool operator!=(const CVector2 &c_vector2) const
Returns true if this vector and the passed one are not equal.
Definition: vector2.h:280
CVector2 operator-() const
Definition: vector2.h:403
CVector2 & Perpendicularize()
Transforms this vector into its ortogonal.
Definition: vector2.h:247
friend std::ostream & operator<<(std::ostream &c_os, const CVector2 &c_vector2)
Serializes the contents of the passed vector onto a stream.
Definition: vector2.h:413
static const CVector2 ZERO
The zero vector (0,0)
Definition: vector2.h:42
CVector2 & operator/=(Real f_value)
Divides this vector by the given value.
Definition: vector2.h:322
CVector2 & operator-=(const CVector2 &c_vector2)
Subtracts the passed vector from this vector.
Definition: vector2.h:300
CVector2 & Rotate(const CRadians &c_angle)
Rotates this vector by the wanted angle.
Definition: vector2.h:194
CVector2 & Absolute()
Applies Abs() to the coordinates of this vector.
Definition: vector2.h:258
void FromPolarCoordinates(Real f_length, const CRadians &f_angle)
Sets the vector contents from polar coordinates.
Definition: vector2.h:148
CVector2()
Class constructor.
Definition: vector2.h:49
CVector2 & Normalize()
Normalizes this vector.
Definition: vector2.h:176
void SetY(Real f_y)
Sets the y coordinate of this vector.
Definition: vector2.h:118
static const CVector2 X
The x axis.
Definition: vector2.h:36
friend CVector2 operator*(Real f_value, const CVector2 &c_vector2)
Returns a new vector containing the multiplication between the passed value and the passed vector.
Definition: vector2.h:398
CVector2 operator+(const CVector2 &c_vector2) const
Returns a new vector containing the sum between this vector and the passed one.
Definition: vector2.h:333
CVector2 operator*(Real f_value) const
Returns a new vector containing the multiplication between this vector and the passed value.
Definition: vector2.h:355
void Set(Real f_x, Real f_y)
Sets the vector contents from Cartesian coordinates.
Definition: vector2.h:127
CVector2 & operator+=(const CVector2 &c_vector2)
Sums the passed vector to this vector.
Definition: vector2.h:289
void SetX(Real f_x)
Sets the x coordinate of this vector.
Definition: vector2.h:102
Real GetX() const
Returns the x coordinate of this vector.
Definition: vector2.h:94
static const CVector2 Y
The y axis.
Definition: vector2.h:39
CVector2 & operator*=(Real f_value)
Multiplies this vector by the given value.
Definition: vector2.h:311