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/string_utilities.h>
17 #include <iostream>
18 #include <cmath>
19 
20 namespace argos {
21 
25  class CVector2 {
26 
27  friend class CRotationMatrix2;
28  friend class CTransformationMatrix2;
29 
30  public:
31 
33  static const CVector2 X;
34 
36  static const CVector2 Y;
37 
44  m_fX(0.0),
45  m_fY(0.0) {
46  }
47 
56  Real f_y) :
57  m_fX(f_x),
58  m_fY(f_y) {
59  }
60 
68  CVector2(Real f_length,
69  const CRadians& f_angle) :
70  m_fX(Cos(f_angle) * f_length),
71  m_fY(Sin(f_angle) * f_length) {
72  }
73 
78  inline Real GetX() const {
79  return m_fX;
80  }
81 
86  inline void SetX(Real f_x) {
87  m_fX = f_x;
88  }
89 
94  inline Real GetY() const {
95  return m_fY;
96  }
97 
102  inline void SetY(Real f_y) {
103  m_fY = f_y;
104  }
105 
111  inline void Set(Real f_x, Real f_y) {
112  m_fX = f_x;
113  m_fY = f_y;
114  }
115 
123  inline void FromPolarCoordinates(Real f_length,
124  const CRadians& f_angle) {
125  m_fX = Cos(f_angle) * f_length;
126  m_fY = Sin(f_angle) * f_length;
127  }
128 
133  inline Real SquareLength() const {
134  return Square(m_fX) + Square(m_fY);
135  }
136 
141  inline Real Length() const {
142  return Sqrt(SquareLength());
143  }
144 
151  inline CVector2& Normalize() {
152  *this /= Length();
153  return *this;
154  }
155 
160  inline CRadians Angle() const {
161  return ATan2(m_fY, m_fX);
162  }
163 
169  inline CVector2& Rotate(const CRadians& c_angle) {
170  Real fSin, fCos;
171 #ifdef ARGOS_SINCOS
172  SinCos(c_angle, fSin, fCos);
173 #else
174  fSin = Sin(c_angle);
175  fCos = Cos(c_angle);
176 #endif
177  Real fX = m_fX * fCos - m_fY * fSin;
178  Real fY = m_fX * fSin + m_fY * fCos;
179  m_fX = fX;
180  m_fY = fY;
181  return *this;
182  }
183 
189  inline Real DotProduct(const CVector2& c_vector2) const {
190  return m_fX * c_vector2.m_fX + m_fY * c_vector2.m_fY;
191  }
192 
198  inline Real CrossProduct(const CVector2& c_vector2) const {
199  return m_fX * c_vector2.m_fY + m_fY * c_vector2.m_fX;
200  }
201 
211  inline CVector2& Scale(Real f_scale_x,
212  Real f_scale_y) {
213  m_fX *= f_scale_x;
214  m_fY *= f_scale_y;
215  return *this;
216  }
217 
223  Real fNewX = -m_fY;
224  m_fY = m_fX;
225  m_fX = fNewX;
226  return *this;
227  }
228 
233  inline CVector2& Absolute() {
234  m_fX = Abs(m_fX);
235  m_fY = Abs(m_fY);
236  return *this;
237  }
238 
245  inline bool operator==(const CVector2& c_vector2) const {
246  return (m_fX == c_vector2.m_fX && m_fY == c_vector2.m_fY);
247  }
248 
255  inline bool operator!=(const CVector2& c_vector2) const {
256  return (m_fX != c_vector2.m_fX || m_fY != c_vector2.m_fY);
257  }
258 
264  inline CVector2& operator+=(const CVector2& c_vector2) {
265  m_fX += c_vector2.m_fX;
266  m_fY += c_vector2.m_fY;
267  return *this;
268  }
269 
275  inline CVector2& operator-=(const CVector2& c_vector2) {
276  m_fX -= c_vector2.m_fX;
277  m_fY -= c_vector2.m_fY;
278  return *this;
279  }
280 
286  inline CVector2& operator*=(Real f_value) {
287  m_fX *= f_value;
288  m_fY *= f_value;
289  return *this;
290  }
291 
297  inline CVector2& operator/=(Real f_value) {
298  m_fX /= f_value;
299  m_fY /= f_value;
300  return *this;
301  }
302 
308  inline CVector2 operator+(const CVector2& c_vector2) const {
309  CVector2 cResult(*this);
310  cResult += c_vector2;
311  return cResult;
312  }
313 
319  inline CVector2 operator-(const CVector2& c_vector2) const {
320  CVector2 cResult(*this);
321  cResult -= c_vector2;
322  return cResult;
323  }
324 
330  inline CVector2 operator*(Real f_value) const {
331  CVector2 cResult(*this);
332  cResult *= f_value;
333  return cResult;
334  }
335 
341  inline CVector2 operator/(Real f_value) const {
342  CVector2 cResult(*this);
343  cResult /= f_value;
344  return cResult;
345  }
346 
353  inline friend CVector2 operator*(Real f_value,
354  const CVector2& c_vector2) {
355  return c_vector2 * f_value;
356  }
357 
358  inline CVector2 operator-() const {
359  return CVector2(-m_fX, -m_fY);
360  }
361 
368  inline friend std::ostream& operator<<(std::ostream& c_os,
369  const CVector2& c_vector2) {
370  c_os << c_vector2.m_fX << ","
371  << c_vector2.m_fY;
372  return c_os;
373  }
374 
381  inline friend std::istream& operator>>(std::istream& c_is,
382  CVector2& c_vector2) {
383  Real fValues[2];
384  ParseValues<Real> (c_is, 2, fValues, ',');
385  c_vector2.Set(fValues[0], fValues[1]);
386  return c_is;
387  }
388 
389  private:
390 
392  Real m_fX;
393 
395  Real m_fY;
396 
397  };
398 
399  /****************************************/
400  /****************************************/
401 
408  inline Real SquareDistance(const CVector2& c_v1, const CVector2& c_v2) {
409  return (c_v1 - c_v2).SquareLength();
410  }
411 
418  inline Real Distance(const CVector2& c_v1, const CVector2& c_v2) {
419  return (c_v1 - c_v2).Length();
420  }
421 
422  /****************************************/
423  /****************************************/
424 
425 }
426 
427 #endif
Real Distance(const CVector2 &c_v1, const CVector2 &c_v2)
Computes the distance between the passed vectors.
Definition: vector2.h:418
CVector2(Real f_length, const CRadians &f_angle)
Class constructor.
Definition: vector2.h:68
T Square(const T &t_v)
Returns the square of the value of the passed argument.
Definition: general.h:128
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:353
#define Sqrt
Definition: general.h:64
CVector2 operator*(Real f_value) const
Returns a new vector containing the multiplication between this vector and the passed value...
Definition: vector2.h:330
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
CRadians ATan2(const Real f_y, const Real f_x)
Computes the arctangent of the passed values.
Definition: angles.h:633
Real GetX() const
Returns the x coordinate of this vector.
Definition: vector2.h:78
Real DotProduct(const CVector2 &c_vector2) const
Returns the dot product between this vector and the passed one.
Definition: vector2.h:189
CVector2 operator-(const CVector2 &c_vector2) const
Returns a new vector containing the subtraction between this vector and the passed one...
Definition: vector2.h:319
CVector2 & operator+=(const CVector2 &c_vector2)
Sums the passed vector to this vector.
Definition: vector2.h:264
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:381
static const CVector2 X
The x axis.
Definition: vector2.h:33
CVector2 operator+(const CVector2 &c_vector2) const
Returns a new vector containing the sum between this vector and the passed one.
Definition: vector2.h:308
T Abs(const T &t_v)
Returns the absolute value of the passed argument.
Definition: general.h:25
CVector2 & Scale(Real f_scale_x, Real f_scale_y)
Scales the vector by the wanted values.
Definition: vector2.h:211
Real Cos(const CRadians &c_radians)
Computes the cosine of the passed value in radians.
Definition: angles.h:595
CVector2 & operator*=(Real f_value)
Multiplies this vector by the given value.
Definition: vector2.h:286
Real GetY() const
Returns the y coordinate of this vector.
Definition: vector2.h:94
CVector2 & Perpendicularize()
Transforms this vector into its ortogonal.
Definition: vector2.h:222
It defines the basic type CRadians, used to store an angle value in radians.
Definition: angles.h:42
Real SquareLength() const
Returns the square length of this vector.
Definition: vector2.h:133
CRadians Angle() const
Returns the angle of this vector.
Definition: vector2.h:160
bool operator==(const CVector2 &c_vector2) const
Returns true if this vector and the passed one are equal.
Definition: vector2.h:245
void SetX(Real f_x)
Sets the x coordinate of this vector.
Definition: vector2.h:86
void FromPolarCoordinates(Real f_length, const CRadians &f_angle)
Sets the vector contents from polar coordinates.
Definition: vector2.h:123
A 2D vector class.
Definition: vector2.h:25
CVector2()
Class constructor.
Definition: vector2.h:43
CVector2 & operator/=(Real f_value)
Divides this vector by the given value.
Definition: vector2.h:297
void SetY(Real f_y)
Sets the y coordinate of this vector.
Definition: vector2.h:102
Real Sin(const CRadians &c_radians)
Computes the sine of the passed value in radians.
Definition: angles.h:586
Real SquareDistance(const CVector2 &c_v1, const CVector2 &c_v2)
Computes the square distance between the passed vectors.
Definition: vector2.h:408
CVector2(Real f_x, Real f_y)
Class constructor.
Definition: vector2.h:55
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
CVector2 & Normalize()
Normalizes this vector.
Definition: vector2.h:151
CVector2 operator/(Real f_value) const
Returns a new vector containing the division between this vector and the passed value.
Definition: vector2.h:341
Real Length() const
Returns the length of this vector.
Definition: vector2.h:141
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:368
CVector2 & operator-=(const CVector2 &c_vector2)
Subtracts the passed vector from this vector.
Definition: vector2.h:275
Real CrossProduct(const CVector2 &c_vector2) const
Returns the cross product between this vector and the passed one.
Definition: vector2.h:198
void Set(Real f_x, Real f_y)
Sets the vector contents from Cartesian coordinates.
Definition: vector2.h:111
CVector2 operator-() const
Definition: vector2.h:358
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
static const CVector2 Y
The y axis.
Definition: vector2.h:36
CVector2 & Rotate(const CRadians &c_angle)
Rotates this vector by the wanted angle.
Definition: vector2.h:169
bool operator!=(const CVector2 &c_vector2) const
Returns true if this vector and the passed one are not equal.
Definition: vector2.h:255
CVector2 & Absolute()
Applies Abs() to the coordinates of this vector.
Definition: vector2.h:233