Ignition Math

API Reference

6.4.0
Vector2.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef IGNITION_MATH_VECTOR2_HH_
18 #define IGNITION_MATH_VECTOR2_HH_
19 
20 #include <ignition/math/Helpers.hh>
21 #include <ignition/math/config.hh>
22 
23 namespace ignition
24 {
25  namespace math
26  {
27  // Inline bracket to help doxygen filtering.
28  inline namespace IGNITION_MATH_VERSION_NAMESPACE {
29  //
32  template<typename T>
33  class Vector2
34  {
36  public: static const Vector2<T> Zero;
37 
39  public: static const Vector2<T> One;
40 
42  public: Vector2()
43  {
44  this->data[0] = 0;
45  this->data[1] = 0;
46  }
47 
51  public: Vector2(const T &_x, const T &_y)
52  {
53  this->data[0] = _x;
54  this->data[1] = _y;
55  }
56 
59  public: Vector2(const Vector2<T> &_v)
60  {
61  this->data[0] = _v[0];
62  this->data[1] = _v[1];
63  }
64 
66  public: virtual ~Vector2() {}
67 
71  public: double Distance(const Vector2 &_pt) const
72  {
73  return sqrt((this->data[0]-_pt[0])*(this->data[0]-_pt[0]) +
74  (this->data[1]-_pt[1])*(this->data[1]-_pt[1]));
75  }
76 
79  public: T Length() const
80  {
81  return sqrt(this->SquaredLength());
82  }
83 
86  public: T SquaredLength() const
87  {
88  return std::pow(this->data[0], 2)
89  + std::pow(this->data[1], 2);
90  }
91 
93  public: void Normalize()
94  {
95  double d = this->Length();
96 
97  if (!equal<T>(d, static_cast<T>(0.0)))
98  {
99  this->data[0] /= d;
100  this->data[1] /= d;
101  }
102  }
103 
107  public: void Set(T _x, T _y)
108  {
109  this->data[0] = _x;
110  this->data[1] = _y;
111  }
112 
116  public: T Dot(const Vector2<T> &_v) const
117  {
118  return (this->data[0] * _v[0]) + (this->data[1] * _v[1]);
119  }
120 
124  public: Vector2 &operator=(const Vector2 &_v)
125  {
126  this->data[0] = _v[0];
127  this->data[1] = _v[1];
128 
129  return *this;
130  }
131 
135  public: const Vector2 &operator=(T _v)
136  {
137  this->data[0] = _v;
138  this->data[1] = _v;
139 
140  return *this;
141  }
142 
146  public: Vector2 operator+(const Vector2 &_v) const
147  {
148  return Vector2(this->data[0] + _v[0], this->data[1] + _v[1]);
149  }
150 
153  // \return this
154  public: const Vector2 &operator+=(const Vector2 &_v)
155  {
156  this->data[0] += _v[0];
157  this->data[1] += _v[1];
158 
159  return *this;
160  }
161 
165  public: inline Vector2<T> operator+(const T _s) const
166  {
167  return Vector2<T>(this->data[0] + _s,
168  this->data[1] + _s);
169  }
170 
175  public: friend inline Vector2<T> operator+(const T _s,
176  const Vector2<T> &_v)
177  {
178  return _v + _s;
179  }
180 
184  public: const Vector2<T> &operator+=(const T _s)
185  {
186  this->data[0] += _s;
187  this->data[1] += _s;
188 
189  return *this;
190  }
191 
194  public: inline Vector2 operator-() const
195  {
196  return Vector2(-this->data[0], -this->data[1]);
197  }
198 
202  public: Vector2 operator-(const Vector2 &_v) const
203  {
204  return Vector2(this->data[0] - _v[0], this->data[1] - _v[1]);
205  }
206 
210  public: const Vector2 &operator-=(const Vector2 &_v)
211  {
212  this->data[0] -= _v[0];
213  this->data[1] -= _v[1];
214 
215  return *this;
216  }
217 
221  public: inline Vector2<T> operator-(const T _s) const
222  {
223  return Vector2<T>(this->data[0] - _s,
224  this->data[1] - _s);
225  }
226 
231  public: friend inline Vector2<T> operator-(const T _s,
232  const Vector2<T> &_v)
233  {
234  return {_s - _v.X(), _s - _v.Y()};
235  }
236 
240  public: const Vector2<T> &operator-=(T _s)
241  {
242  this->data[0] -= _s;
243  this->data[1] -= _s;
244 
245  return *this;
246  }
247 
252  public: const Vector2 operator/(const Vector2 &_v) const
253  {
254  return Vector2(this->data[0] / _v[0], this->data[1] / _v[1]);
255  }
256 
261  public: const Vector2 &operator/=(const Vector2 &_v)
262  {
263  this->data[0] /= _v[0];
264  this->data[1] /= _v[1];
265 
266  return *this;
267  }
268 
272  public: const Vector2 operator/(T _v) const
273  {
274  return Vector2(this->data[0] / _v, this->data[1] / _v);
275  }
276 
280  public: const Vector2 &operator/=(T _v)
281  {
282  this->data[0] /= _v;
283  this->data[1] /= _v;
284 
285  return *this;
286  }
287 
291  public: const Vector2 operator*(const Vector2 &_v) const
292  {
293  return Vector2(this->data[0] * _v[0], this->data[1] * _v[1]);
294  }
295 
300  public: const Vector2 &operator*=(const Vector2 &_v)
301  {
302  this->data[0] *= _v[0];
303  this->data[1] *= _v[1];
304 
305  return *this;
306  }
307 
311  public: const Vector2 operator*(T _v) const
312  {
313  return Vector2(this->data[0] * _v, this->data[1] * _v);
314  }
315 
320  public: friend inline const Vector2 operator*(const T _s,
321  const Vector2 &_v)
322  {
323  return Vector2(_v * _s);
324  }
325 
329  public: const Vector2 &operator*=(T _v)
330  {
331  this->data[0] *= _v;
332  this->data[1] *= _v;
333 
334  return *this;
335  }
336 
342  public: bool Equal(const Vector2 &_v, const T &_tol) const
343  {
344  return equal<T>(this->data[0], _v[0], _tol)
345  && equal<T>(this->data[1], _v[1], _tol);
346  }
347 
352  public: bool operator==(const Vector2 &_v) const
353  {
354  return this->Equal(_v, static_cast<T>(1e-6));
355  }
356 
359  public: bool operator!=(const Vector2 &_v) const
360  {
361  return !(*this == _v);
362  }
363 
366  public: bool IsFinite() const
367  {
368  // std::isfinite works with floating point values,
369  // need to explicit cast to avoid ambiguity in vc++.
370  return std::isfinite(static_cast<double>(this->data[0])) &&
371  std::isfinite(static_cast<double>(this->data[1]));
372  }
373 
377  public: T &operator[](const std::size_t _index)
378  {
379  return this->data[clamp(_index, IGN_ZERO_SIZE_T, IGN_ONE_SIZE_T)];
380  }
381 
385  public: T operator[](const std::size_t _index) const
386  {
387  return this->data[clamp(_index, IGN_ZERO_SIZE_T, IGN_ONE_SIZE_T)];
388  }
389 
392  public: inline T X() const
393  {
394  return this->data[0];
395  }
396 
399  public: inline T Y() const
400  {
401  return this->data[1];
402  }
403 
406  public: inline T &X()
407  {
408  return this->data[0];
409  }
410 
413  public: inline T &Y()
414  {
415  return this->data[1];
416  }
417 
420  public: inline void X(const T &_v)
421  {
422  this->data[0] = _v;
423  }
424 
427  public: inline void Y(const T &_v)
428  {
429  this->data[1] = _v;
430  }
431 
436  public: friend std::ostream
437  &operator<<(std::ostream &_out, const Vector2<T> &_pt)
438  {
439  _out << _pt[0] << " " << _pt[1];
440  return _out;
441  }
442 
447  public: bool operator<(const Vector2<T> &_pt) const
448  {
449  return this->data[0] < _pt[0] || this->data[1] < _pt[1];
450  }
451 
456  public: friend std::istream
458  {
459  T x, y;
460  // Skip white spaces
461  _in.setf(std::ios_base::skipws);
462  _in >> x >> y;
463  _pt.Set(x, y);
464  return _in;
465  }
466 
468  private: T data[2];
469  };
470 
471  template<typename T>
472  const Vector2<T> Vector2<T>::Zero(0, 0);
473 
474  template<typename T>
475  const Vector2<T> Vector2<T>::One(1, 1);
476 
480  }
481  }
482 }
483 #endif
friend Vector2< T > operator-(const T _s, const Vector2< T > &_v)
Subtraction operators.
Definition: Vector2.hh:231
const Vector2 & operator*=(const Vector2 &_v)
Multiplication assignment operator.
Definition: Vector2.hh:300
const Vector2< T > & operator-=(T _s)
Subtraction assignment operator.
Definition: Vector2.hh:240
T setf(T... args)
const Vector2 & operator=(T _v)
Assignment operator.
Definition: Vector2.hh:135
T & operator[](const std::size_t _index)
Array subscript operator.
Definition: Vector2.hh:377
void Set(T _x, T _y)
Set the contents of the vector.
Definition: Vector2.hh:107
T Y() const
Return the y value.
Definition: Vector2.hh:399
const Vector2 & operator-=(const Vector2 &_v)
Subtraction assignment operator.
Definition: Vector2.hh:210
bool IsFinite() const
See if a point is finite (e.g., not nan)
Definition: Vector2.hh:366
static const static double const static double const static double const static double const static float const static float const static float const static float const static uint16_t const static uint16_t const static uint16_t const static uint16_t const static int16_t const static int16_t const static int16_t const static int16_t const static uint32_t const static uint32_t const static uint32_t const static uint32_t const static int32_t const static int32_t const static int32_t const static int32_t const static uint64_t const static uint64_t const static uint64_t const static uint64_t const static int64_t const static int64_t const static int64_t const int64_t T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:403
const Vector2 operator*(const Vector2 &_v) const
Multiplication operators.
Definition: Vector2.hh:291
double Distance(const Vector2 &_pt) const
Calc distance to the given point.
Definition: Vector2.hh:71
void Normalize()
Normalize the vector length.
Definition: Vector2.hh:93
const Vector2 & operator+=(const Vector2 &_v)
Addition assignment operator.
Definition: Vector2.hh:154
friend Vector2< T > operator+(const T _s, const Vector2< T > &_v)
Addition operators.
Definition: Vector2.hh:175
Two dimensional (x, y) vector.
Definition: Vector2.hh:33
Vector2 operator-(const Vector2 &_v) const
Subtraction operator.
Definition: Vector2.hh:202
friend std::istream & operator>>(std::istream &_in, Vector2< T > &_pt)
Stream extraction operator.
Definition: Vector2.hh:457
static const size_t IGN_ONE_SIZE_T
size_t type with a value of 1
Definition: Helpers.hh:227
STL class.
T Dot(const Vector2< T > &_v) const
Get the dot product of this vector and _v.
Definition: Vector2.hh:116
const Vector2 & operator/=(T _v)
Division operator.
Definition: Vector2.hh:280
T & X()
Return a mutable x value.
Definition: Vector2.hh:406
static const Vector2< T > One
math::Vector2(1, 1)
Definition: Vector2.hh:39
T & Y()
Return a mutable y value.
Definition: Vector2.hh:413
Vector2()
Default Constructor.
Definition: Vector2.hh:42
T X() const
Return the x value.
Definition: Vector2.hh:392
static const Vector2< T > Zero
math::Vector2(0, 0)
Definition: Vector2.hh:36
void X(const T &_v)
Set the x value.
Definition: Vector2.hh:420
T operator[](const std::size_t _index) const
Const-qualified array subscript operator.
Definition: Vector2.hh:385
static const size_t IGN_ZERO_SIZE_T
size_t type with a value of 0
Definition: Helpers.hh:224
T isfinite(T... args)
bool Equal(const Vector2 &_v, const T &_tol) const
Equality test with tolerance.
Definition: Vector2.hh:342
Vector2(const T &_x, const T &_y)
Constructor.
Definition: Vector2.hh:51
bool operator==(const Vector2 &_v) const
Equal to operator.
Definition: Vector2.hh:352
void Y(const T &_v)
Set the y value.
Definition: Vector2.hh:427
T pow(T... args)
Vector2< double > Vector2d
Definition: Vector2.hh:478
const Vector2 operator/(const Vector2 &_v) const
Division operator.
Definition: Vector2.hh:252
Vector2< T > operator-(const T _s) const
Subtraction operators.
Definition: Vector2.hh:221
const Vector2 operator/(T _v) const
Division operator.
Definition: Vector2.hh:272
bool operator!=(const Vector2 &_v) const
Not equal to operator.
Definition: Vector2.hh:359
Vector2 operator-() const
Negation operator.
Definition: Vector2.hh:194
T Length() const
Returns the length (magnitude) of the vector.
Definition: Vector2.hh:79
const Vector2< T > & operator+=(const T _s)
Addition assignment operator.
Definition: Vector2.hh:184
Vector2< T > operator+(const T _s) const
Addition operators.
Definition: Vector2.hh:165
Vector2< int > Vector2i
Definition: Vector2.hh:477
const Vector2 & operator/=(const Vector2 &_v)
Division operator.
Definition: Vector2.hh:261
Vector2< float > Vector2f
Definition: Vector2.hh:479
const Vector2 operator*(T _v) const
Multiplication operators.
Definition: Vector2.hh:311
Definition: Angle.hh:42
const Vector2 & operator*=(T _v)
Multiplication assignment operator.
Definition: Vector2.hh:329
Vector2 operator+(const Vector2 &_v) const
Addition operator.
Definition: Vector2.hh:146
Vector2(const Vector2< T > &_v)
Copy constructor.
Definition: Vector2.hh:59
STL class.
virtual ~Vector2()
Destructor.
Definition: Vector2.hh:66
T SquaredLength() const
Returns the square of the length (magnitude) of the vector.
Definition: Vector2.hh:86
Vector2 & operator=(const Vector2 &_v)
Assignment operator.
Definition: Vector2.hh:124
friend const Vector2 operator*(const T _s, const Vector2 &_v)
Scalar left multiplication operators.
Definition: Vector2.hh:320