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 <algorithm>
21 
22 #include <ignition/math/Helpers.hh>
23 #include <ignition/math/config.hh>
24 
25 namespace ignition
26 {
27  namespace math
28  {
29  // Inline bracket to help doxygen filtering.
30  inline namespace IGNITION_MATH_VERSION_NAMESPACE {
31  //
34  template<typename T>
35  class Vector2
36  {
38  public: static const Vector2<T> Zero;
39 
41  public: static const Vector2<T> One;
42 
44  public: Vector2()
45  {
46  this->data[0] = 0;
47  this->data[1] = 0;
48  }
49 
53  public: Vector2(const T &_x, const T &_y)
54  {
55  this->data[0] = _x;
56  this->data[1] = _y;
57  }
58 
61  public: Vector2(const Vector2<T> &_v)
62  {
63  this->data[0] = _v[0];
64  this->data[1] = _v[1];
65  }
66 
68  public: virtual ~Vector2() {}
69 
72  public: T Sum() const
73  {
74  return this->data[0] + this->data[1];
75  }
76 
80  public: double Distance(const Vector2 &_pt) const
81  {
82  return sqrt((this->data[0]-_pt[0])*(this->data[0]-_pt[0]) +
83  (this->data[1]-_pt[1])*(this->data[1]-_pt[1]));
84  }
85 
88  public: T Length() const
89  {
90  return sqrt(this->SquaredLength());
91  }
92 
95  public: T SquaredLength() const
96  {
97  return std::pow(this->data[0], 2)
98  + std::pow(this->data[1], 2);
99  }
100 
102  public: void Normalize()
103  {
104  double d = this->Length();
105 
106  if (!equal<T>(d, static_cast<T>(0.0)))
107  {
108  this->data[0] /= d;
109  this->data[1] /= d;
110  }
111  }
112 
115  public: Vector2 Normalized() const
116  {
117  Vector2<T> result = *this;
118  result.Normalize();
119  return result;
120  }
121 
125  public: void Set(T _x, T _y)
126  {
127  this->data[0] = _x;
128  this->data[1] = _y;
129  }
130 
134  public: T Dot(const Vector2<T> &_v) const
135  {
136  return (this->data[0] * _v[0]) + (this->data[1] * _v[1]);
137  }
138 
141  public: Vector2 Abs() const
142  {
143  return Vector2(std::abs(this->data[0]),
144  std::abs(this->data[1]));
145  }
146 
155  public: T AbsDot(const Vector2<T> &_v) const
156  {
157  return std::abs(this->data[0] * _v[0]) +
158  std::abs(this->data[1] * _v[1]);
159  }
160 
162  public: inline void Correct()
163  {
164  // std::isfinite works with floating point values,
165  // need to explicit cast to avoid ambiguity in vc++.
166  if (!std::isfinite(static_cast<double>(this->data[0])))
167  this->data[0] = 0;
168  if (!std::isfinite(static_cast<double>(this->data[1])))
169  this->data[1] = 0;
170  }
171 
175  public: void Max(const Vector2<T> &_v)
176  {
177  this->data[0] = std::max(_v[0], this->data[0]);
178  this->data[1] = std::max(_v[1], this->data[1]);
179  }
180 
184  public: void Min(const Vector2<T> &_v)
185  {
186  this->data[0] = std::min(_v[0], this->data[0]);
187  this->data[1] = std::min(_v[1], this->data[1]);
188  }
189 
192  public: T Max() const
193  {
194  return std::max(this->data[0], this->data[1]);
195  }
196 
199  public: T Min() const
200  {
201  return std::min(this->data[0], this->data[1]);
202  }
203 
207  public: Vector2 &operator=(const Vector2 &_v)
208  {
209  this->data[0] = _v[0];
210  this->data[1] = _v[1];
211 
212  return *this;
213  }
214 
218  public: const Vector2 &operator=(T _v)
219  {
220  this->data[0] = _v;
221  this->data[1] = _v;
222 
223  return *this;
224  }
225 
229  public: Vector2 operator+(const Vector2 &_v) const
230  {
231  return Vector2(this->data[0] + _v[0], this->data[1] + _v[1]);
232  }
233 
236  // \return this
237  public: const Vector2 &operator+=(const Vector2 &_v)
238  {
239  this->data[0] += _v[0];
240  this->data[1] += _v[1];
241 
242  return *this;
243  }
244 
248  public: inline Vector2<T> operator+(const T _s) const
249  {
250  return Vector2<T>(this->data[0] + _s,
251  this->data[1] + _s);
252  }
253 
258  public: friend inline Vector2<T> operator+(const T _s,
259  const Vector2<T> &_v)
260  {
261  return _v + _s;
262  }
263 
267  public: const Vector2<T> &operator+=(const T _s)
268  {
269  this->data[0] += _s;
270  this->data[1] += _s;
271 
272  return *this;
273  }
274 
277  public: inline Vector2 operator-() const
278  {
279  return Vector2(-this->data[0], -this->data[1]);
280  }
281 
285  public: Vector2 operator-(const Vector2 &_v) const
286  {
287  return Vector2(this->data[0] - _v[0], this->data[1] - _v[1]);
288  }
289 
293  public: const Vector2 &operator-=(const Vector2 &_v)
294  {
295  this->data[0] -= _v[0];
296  this->data[1] -= _v[1];
297 
298  return *this;
299  }
300 
304  public: inline Vector2<T> operator-(const T _s) const
305  {
306  return Vector2<T>(this->data[0] - _s,
307  this->data[1] - _s);
308  }
309 
314  public: friend inline Vector2<T> operator-(const T _s,
315  const Vector2<T> &_v)
316  {
317  return {_s - _v.X(), _s - _v.Y()};
318  }
319 
323  public: const Vector2<T> &operator-=(T _s)
324  {
325  this->data[0] -= _s;
326  this->data[1] -= _s;
327 
328  return *this;
329  }
330 
335  public: const Vector2 operator/(const Vector2 &_v) const
336  {
337  return Vector2(this->data[0] / _v[0], this->data[1] / _v[1]);
338  }
339 
344  public: const Vector2 &operator/=(const Vector2 &_v)
345  {
346  this->data[0] /= _v[0];
347  this->data[1] /= _v[1];
348 
349  return *this;
350  }
351 
355  public: const Vector2 operator/(T _v) const
356  {
357  return Vector2(this->data[0] / _v, this->data[1] / _v);
358  }
359 
363  public: const Vector2 &operator/=(T _v)
364  {
365  this->data[0] /= _v;
366  this->data[1] /= _v;
367 
368  return *this;
369  }
370 
374  public: const Vector2 operator*(const Vector2 &_v) const
375  {
376  return Vector2(this->data[0] * _v[0], this->data[1] * _v[1]);
377  }
378 
383  public: const Vector2 &operator*=(const Vector2 &_v)
384  {
385  this->data[0] *= _v[0];
386  this->data[1] *= _v[1];
387 
388  return *this;
389  }
390 
394  public: const Vector2 operator*(T _v) const
395  {
396  return Vector2(this->data[0] * _v, this->data[1] * _v);
397  }
398 
403  public: friend inline const Vector2 operator*(const T _s,
404  const Vector2 &_v)
405  {
406  return Vector2(_v * _s);
407  }
408 
412  public: const Vector2 &operator*=(T _v)
413  {
414  this->data[0] *= _v;
415  this->data[1] *= _v;
416 
417  return *this;
418  }
419 
425  public: bool Equal(const Vector2 &_v, const T &_tol) const
426  {
427  return equal<T>(this->data[0], _v[0], _tol)
428  && equal<T>(this->data[1], _v[1], _tol);
429  }
430 
435  public: bool operator==(const Vector2 &_v) const
436  {
437  return this->Equal(_v, static_cast<T>(1e-6));
438  }
439 
442  public: bool operator!=(const Vector2 &_v) const
443  {
444  return !(*this == _v);
445  }
446 
449  public: bool IsFinite() const
450  {
451  // std::isfinite works with floating point values,
452  // need to explicit cast to avoid ambiguity in vc++.
453  return std::isfinite(static_cast<double>(this->data[0])) &&
454  std::isfinite(static_cast<double>(this->data[1]));
455  }
456 
460  public: T &operator[](const std::size_t _index)
461  {
462  return this->data[clamp(_index, IGN_ZERO_SIZE_T, IGN_ONE_SIZE_T)];
463  }
464 
468  public: T operator[](const std::size_t _index) const
469  {
470  return this->data[clamp(_index, IGN_ZERO_SIZE_T, IGN_ONE_SIZE_T)];
471  }
472 
475  public: inline T X() const
476  {
477  return this->data[0];
478  }
479 
482  public: inline T Y() const
483  {
484  return this->data[1];
485  }
486 
489  public: inline T &X()
490  {
491  return this->data[0];
492  }
493 
496  public: inline T &Y()
497  {
498  return this->data[1];
499  }
500 
503  public: inline void X(const T &_v)
504  {
505  this->data[0] = _v;
506  }
507 
510  public: inline void Y(const T &_v)
511  {
512  this->data[1] = _v;
513  }
514 
519  public: friend std::ostream
520  &operator<<(std::ostream &_out, const Vector2<T> &_pt)
521  {
522  _out << _pt[0] << " " << _pt[1];
523  return _out;
524  }
525 
530  public: bool operator<(const Vector2<T> &_pt) const
531  {
532  return this->data[0] < _pt[0] || this->data[1] < _pt[1];
533  }
534 
539  public: friend std::istream
541  {
542  T x, y;
543  // Skip white spaces
544  _in.setf(std::ios_base::skipws);
545  _in >> x >> y;
546  _pt.Set(x, y);
547  return _in;
548  }
549 
551  private: T data[2];
552  };
553 
554  template<typename T>
555  const Vector2<T> Vector2<T>::Zero(0, 0);
556 
557  template<typename T>
558  const Vector2<T> Vector2<T>::One(1, 1);
559 
563  }
564  }
565 }
566 #endif
void Max(const Vector2< T > &_v)
Set this vector&#39;s components to the maximum of itself and the passed in vector.
Definition: Vector2.hh:175
friend Vector2< T > operator-(const T _s, const Vector2< T > &_v)
Subtraction operators.
Definition: Vector2.hh:314
const Vector2 & operator*=(const Vector2 &_v)
Multiplication assignment operator.
Definition: Vector2.hh:383
const Vector2< T > & operator-=(T _s)
Subtraction assignment operator.
Definition: Vector2.hh:323
T setf(T... args)
const Vector2 & operator=(T _v)
Assignment operator.
Definition: Vector2.hh:218
T & operator[](const std::size_t _index)
Array subscript operator.
Definition: Vector2.hh:460
void Set(T _x, T _y)
Set the contents of the vector.
Definition: Vector2.hh:125
T Y() const
Return the y value.
Definition: Vector2.hh:482
const Vector2 & operator-=(const Vector2 &_v)
Subtraction assignment operator.
Definition: Vector2.hh:293
bool IsFinite() const
See if a point is finite (e.g., not nan)
Definition: Vector2.hh:449
const Vector2 operator*(const Vector2 &_v) const
Multiplication operators.
Definition: Vector2.hh:374
double Distance(const Vector2 &_pt) const
Calc distance to the given point.
Definition: Vector2.hh:80
void Normalize()
Normalize the vector length.
Definition: Vector2.hh:102
const Vector2 & operator+=(const Vector2 &_v)
Addition assignment operator.
Definition: Vector2.hh:237
friend Vector2< T > operator+(const T _s, const Vector2< T > &_v)
Addition operators.
Definition: Vector2.hh:258
Two dimensional (x, y) vector.
Definition: Vector2.hh:35
Vector2 operator-(const Vector2 &_v) const
Subtraction operator.
Definition: Vector2.hh:285
friend std::istream & operator>>(std::istream &_in, Vector2< T > &_pt)
Stream extraction operator.
Definition: Vector2.hh:540
static const size_t IGN_ONE_SIZE_T
size_t type with a value of 1
Definition: Helpers.hh:229
STL class.
T Dot(const Vector2< T > &_v) const
Get the dot product of this vector and _v.
Definition: Vector2.hh:134
const Vector2 & operator/=(T _v)
Division operator.
Definition: Vector2.hh:363
void Correct()
Corrects any nan values.
Definition: Vector2.hh:162
T & X()
Return a mutable x value.
Definition: Vector2.hh:489
T min(T... args)
static const Vector2< T > One
math::Vector2(1, 1)
Definition: Vector2.hh:41
T & Y()
Return a mutable y value.
Definition: Vector2.hh:496
T AbsDot(const Vector2< T > &_v) const
Return the absolute dot product of this vector and another vector. This is similar to the Dot functio...
Definition: Vector2.hh:155
Vector2 Normalized() const
Returns a normalized vector.
Definition: Vector2.hh:115
Vector2()
Default Constructor.
Definition: Vector2.hh:44
T X() const
Return the x value.
Definition: Vector2.hh:475
static const Vector2< T > Zero
math::Vector2(0, 0)
Definition: Vector2.hh:38
void X(const T &_v)
Set the x value.
Definition: Vector2.hh:503
T operator[](const std::size_t _index) const
Const-qualified array subscript operator.
Definition: Vector2.hh:468
T Sum() const
Return the sum of the values.
Definition: Vector2.hh:72
Vector2 Abs() const
Get the absolute value of the vector.
Definition: Vector2.hh:141
static const size_t IGN_ZERO_SIZE_T
size_t type with a value of 0
Definition: Helpers.hh:226
T isfinite(T... args)
bool Equal(const Vector2 &_v, const T &_tol) const
Equality test with tolerance.
Definition: Vector2.hh:425
T max(T... args)
Vector2(const T &_x, const T &_y)
Constructor.
Definition: Vector2.hh:53
T Max() const
Get the maximum value in the vector.
Definition: Vector2.hh:192
void Min(const Vector2< T > &_v)
Set this vector&#39;s components to the minimum of itself and the passed in vector.
Definition: Vector2.hh:184
bool operator==(const Vector2 &_v) const
Equal to operator.
Definition: Vector2.hh:435
void Y(const T &_v)
Set the y value.
Definition: Vector2.hh:510
T pow(T... args)
Vector2< double > Vector2d
Definition: Vector2.hh:561
const Vector2 operator/(const Vector2 &_v) const
Division operator.
Definition: Vector2.hh:335
Vector2< T > operator-(const T _s) const
Subtraction operators.
Definition: Vector2.hh:304
const Vector2 operator/(T _v) const
Division operator.
Definition: Vector2.hh:355
bool operator!=(const Vector2 &_v) const
Not equal to operator.
Definition: Vector2.hh:442
Vector2 operator-() const
Negation operator.
Definition: Vector2.hh:277
T Length() const
Returns the length (magnitude) of the vector.
Definition: Vector2.hh:88
const Vector2< T > & operator+=(const T _s)
Addition assignment operator.
Definition: Vector2.hh:267
Vector2< T > operator+(const T _s) const
Addition operators.
Definition: Vector2.hh:248
Vector2< int > Vector2i
Definition: Vector2.hh:560
const Vector2 & operator/=(const Vector2 &_v)
Division operator.
Definition: Vector2.hh:344
Vector2< float > Vector2f
Definition: Vector2.hh:562
const Vector2 operator*(T _v) const
Multiplication operators.
Definition: Vector2.hh:394
Definition: Angle.hh:42
const Vector2 & operator*=(T _v)
Multiplication assignment operator.
Definition: Vector2.hh:412
Vector2 operator+(const Vector2 &_v) const
Addition operator.
Definition: Vector2.hh:229
Vector2(const Vector2< T > &_v)
Copy constructor.
Definition: Vector2.hh:61
STL class.
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:405
virtual ~Vector2()
Destructor.
Definition: Vector2.hh:68
T SquaredLength() const
Returns the square of the length (magnitude) of the vector.
Definition: Vector2.hh:95
Vector2 & operator=(const Vector2 &_v)
Assignment operator.
Definition: Vector2.hh:207
T Min() const
Get the minimum value in the vector.
Definition: Vector2.hh:199
friend const Vector2 operator*(const T _s, const Vector2 &_v)
Scalar left multiplication operators.
Definition: Vector2.hh:403