Ignition Math

API Reference

6.8.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 #include <limits>
22 
23 #include <ignition/math/Helpers.hh>
24 #include <ignition/math/config.hh>
25 
26 namespace ignition
27 {
28  namespace math
29  {
30  // Inline bracket to help doxygen filtering.
31  inline namespace IGNITION_MATH_VERSION_NAMESPACE {
32  //
35  template<typename T>
36  class Vector2
37  {
39  public: static const Vector2<T> Zero;
40 
42  public: static const Vector2<T> One;
43 
45  public: static const Vector2 NaN;
46 
48  public: Vector2()
49  {
50  this->data[0] = 0;
51  this->data[1] = 0;
52  }
53 
57  public: Vector2(const T &_x, const T &_y)
58  {
59  this->data[0] = _x;
60  this->data[1] = _y;
61  }
62 
65  public: Vector2(const Vector2<T> &_v)
66  {
67  this->data[0] = _v[0];
68  this->data[1] = _v[1];
69  }
70 
72  public: virtual ~Vector2() {}
73 
76  public: T Sum() const
77  {
78  return this->data[0] + this->data[1];
79  }
80 
84  public: double Distance(const Vector2 &_pt) const
85  {
86  return sqrt((this->data[0]-_pt[0])*(this->data[0]-_pt[0]) +
87  (this->data[1]-_pt[1])*(this->data[1]-_pt[1]));
88  }
89 
92  public: T Length() const
93  {
94  return static_cast<T>(sqrt(this->SquaredLength()));
95  }
96 
99  public: T SquaredLength() const
100  {
101  return
102  this->data[0] * this->data[0] +
103  this->data[1] * this->data[1];
104  }
105 
107  public: void Normalize()
108  {
109  double d = this->Length();
110 
111  if (!equal<T>(d, static_cast<T>(0.0)))
112  {
113  this->data[0] /= d;
114  this->data[1] /= d;
115  }
116  }
117 
120  public: Vector2 Normalized() const
121  {
122  Vector2<T> result = *this;
123  result.Normalize();
124  return result;
125  }
126 
129  public: Vector2 Round()
130  {
131  this->data[0] = nearbyint(this->data[0]);
132  this->data[1] = nearbyint(this->data[1]);
133  return *this;
134  }
135 
138  public: Vector2 Rounded() const
139  {
140  Vector2<T> result = *this;
141  result.Round();
142  return result;
143  }
144 
148  public: void Set(T _x, T _y)
149  {
150  this->data[0] = _x;
151  this->data[1] = _y;
152  }
153 
157  public: T Dot(const Vector2<T> &_v) const
158  {
159  return (this->data[0] * _v[0]) + (this->data[1] * _v[1]);
160  }
161 
164  public: Vector2 Abs() const
165  {
166  return Vector2(std::abs(this->data[0]),
167  std::abs(this->data[1]));
168  }
169 
178  public: T AbsDot(const Vector2<T> &_v) const
179  {
180  return std::abs(this->data[0] * _v[0]) +
181  std::abs(this->data[1] * _v[1]);
182  }
183 
185  public: inline void Correct()
186  {
187  // std::isfinite works with floating point values,
188  // need to explicit cast to avoid ambiguity in vc++.
189  if (!std::isfinite(static_cast<double>(this->data[0])))
190  this->data[0] = 0;
191  if (!std::isfinite(static_cast<double>(this->data[1])))
192  this->data[1] = 0;
193  }
194 
198  public: void Max(const Vector2<T> &_v)
199  {
200  this->data[0] = std::max(_v[0], this->data[0]);
201  this->data[1] = std::max(_v[1], this->data[1]);
202  }
203 
207  public: void Min(const Vector2<T> &_v)
208  {
209  this->data[0] = std::min(_v[0], this->data[0]);
210  this->data[1] = std::min(_v[1], this->data[1]);
211  }
212 
215  public: T Max() const
216  {
217  return std::max(this->data[0], this->data[1]);
218  }
219 
222  public: T Min() const
223  {
224  return std::min(this->data[0], this->data[1]);
225  }
226 
230  public: Vector2 &operator=(const Vector2 &_v)
231  {
232  this->data[0] = _v[0];
233  this->data[1] = _v[1];
234 
235  return *this;
236  }
237 
241  public: const Vector2 &operator=(T _v)
242  {
243  this->data[0] = _v;
244  this->data[1] = _v;
245 
246  return *this;
247  }
248 
252  public: Vector2 operator+(const Vector2 &_v) const
253  {
254  return Vector2(this->data[0] + _v[0], this->data[1] + _v[1]);
255  }
256 
259  // \return this
260  public: const Vector2 &operator+=(const Vector2 &_v)
261  {
262  this->data[0] += _v[0];
263  this->data[1] += _v[1];
264 
265  return *this;
266  }
267 
271  public: inline Vector2<T> operator+(const T _s) const
272  {
273  return Vector2<T>(this->data[0] + _s,
274  this->data[1] + _s);
275  }
276 
281  public: friend inline Vector2<T> operator+(const T _s,
282  const Vector2<T> &_v)
283  {
284  return _v + _s;
285  }
286 
290  public: const Vector2<T> &operator+=(const T _s)
291  {
292  this->data[0] += _s;
293  this->data[1] += _s;
294 
295  return *this;
296  }
297 
300  public: inline Vector2 operator-() const
301  {
302  return Vector2(-this->data[0], -this->data[1]);
303  }
304 
308  public: Vector2 operator-(const Vector2 &_v) const
309  {
310  return Vector2(this->data[0] - _v[0], this->data[1] - _v[1]);
311  }
312 
316  public: const Vector2 &operator-=(const Vector2 &_v)
317  {
318  this->data[0] -= _v[0];
319  this->data[1] -= _v[1];
320 
321  return *this;
322  }
323 
327  public: inline Vector2<T> operator-(const T _s) const
328  {
329  return Vector2<T>(this->data[0] - _s,
330  this->data[1] - _s);
331  }
332 
337  public: friend inline Vector2<T> operator-(const T _s,
338  const Vector2<T> &_v)
339  {
340  return {_s - _v.X(), _s - _v.Y()};
341  }
342 
346  public: const Vector2<T> &operator-=(T _s)
347  {
348  this->data[0] -= _s;
349  this->data[1] -= _s;
350 
351  return *this;
352  }
353 
358  public: const Vector2 operator/(const Vector2 &_v) const
359  {
360  return Vector2(this->data[0] / _v[0], this->data[1] / _v[1]);
361  }
362 
367  public: const Vector2 &operator/=(const Vector2 &_v)
368  {
369  this->data[0] /= _v[0];
370  this->data[1] /= _v[1];
371 
372  return *this;
373  }
374 
378  public: const Vector2 operator/(T _v) const
379  {
380  return Vector2(this->data[0] / _v, this->data[1] / _v);
381  }
382 
386  public: const Vector2 &operator/=(T _v)
387  {
388  this->data[0] /= _v;
389  this->data[1] /= _v;
390 
391  return *this;
392  }
393 
397  public: const Vector2 operator*(const Vector2 &_v) const
398  {
399  return Vector2(this->data[0] * _v[0], this->data[1] * _v[1]);
400  }
401 
406  public: const Vector2 &operator*=(const Vector2 &_v)
407  {
408  this->data[0] *= _v[0];
409  this->data[1] *= _v[1];
410 
411  return *this;
412  }
413 
417  public: const Vector2 operator*(T _v) const
418  {
419  return Vector2(this->data[0] * _v, this->data[1] * _v);
420  }
421 
426  public: friend inline const Vector2 operator*(const T _s,
427  const Vector2 &_v)
428  {
429  return Vector2(_v * _s);
430  }
431 
435  public: const Vector2 &operator*=(T _v)
436  {
437  this->data[0] *= _v;
438  this->data[1] *= _v;
439 
440  return *this;
441  }
442 
448  public: bool Equal(const Vector2 &_v, const T &_tol) const
449  {
450  return equal<T>(this->data[0], _v[0], _tol)
451  && equal<T>(this->data[1], _v[1], _tol);
452  }
453 
458  public: bool operator==(const Vector2 &_v) const
459  {
460  return this->Equal(_v, static_cast<T>(1e-6));
461  }
462 
465  public: bool operator!=(const Vector2 &_v) const
466  {
467  return !(*this == _v);
468  }
469 
472  public: bool IsFinite() const
473  {
474  // std::isfinite works with floating point values,
475  // need to explicit cast to avoid ambiguity in vc++.
476  return std::isfinite(static_cast<double>(this->data[0])) &&
477  std::isfinite(static_cast<double>(this->data[1]));
478  }
479 
483  public: T &operator[](const std::size_t _index)
484  {
485  return this->data[clamp(_index, IGN_ZERO_SIZE_T, IGN_ONE_SIZE_T)];
486  }
487 
491  public: T operator[](const std::size_t _index) const
492  {
493  return this->data[clamp(_index, IGN_ZERO_SIZE_T, IGN_ONE_SIZE_T)];
494  }
495 
498  public: inline T X() const
499  {
500  return this->data[0];
501  }
502 
505  public: inline T Y() const
506  {
507  return this->data[1];
508  }
509 
512  public: inline T &X()
513  {
514  return this->data[0];
515  }
516 
519  public: inline T &Y()
520  {
521  return this->data[1];
522  }
523 
526  public: inline void X(const T &_v)
527  {
528  this->data[0] = _v;
529  }
530 
533  public: inline void Y(const T &_v)
534  {
535  this->data[1] = _v;
536  }
537 
542  public: friend std::ostream
543  &operator<<(std::ostream &_out, const Vector2<T> &_pt)
544  {
545  _out << _pt[0] << " " << _pt[1];
546  return _out;
547  }
548 
553  public: bool operator<(const Vector2<T> &_pt) const
554  {
555  return this->data[0] < _pt[0] || this->data[1] < _pt[1];
556  }
557 
562  public: friend std::istream
564  {
565  T x, y;
566  // Skip white spaces
567  _in.setf(std::ios_base::skipws);
568  _in >> x >> y;
569  if (!_in.fail())
570  {
571  _pt.Set(x, y);
572  }
573  return _in;
574  }
575 
577  private: T data[2];
578  };
579 
580  template<typename T>
581  const Vector2<T> Vector2<T>::Zero(0, 0);
582 
583  template<typename T>
584  const Vector2<T> Vector2<T>::One(1, 1);
585 
586  template<typename T>
590 
594  }
595  }
596 }
597 #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:198
friend Vector2< T > operator-(const T _s, const Vector2< T > &_v)
Subtraction operators.
Definition: Vector2.hh:337
const Vector2 & operator*=(const Vector2 &_v)
Multiplication assignment operator.
Definition: Vector2.hh:406
const Vector2< T > & operator-=(T _s)
Subtraction assignment operator.
Definition: Vector2.hh:346
T setf(T... args)
const Vector2 & operator=(T _v)
Assignment operator.
Definition: Vector2.hh:241
T & operator[](const std::size_t _index)
Array subscript operator.
Definition: Vector2.hh:483
void Set(T _x, T _y)
Set the contents of the vector.
Definition: Vector2.hh:148
T Y() const
Return the y value.
Definition: Vector2.hh:505
const Vector2 & operator-=(const Vector2 &_v)
Subtraction assignment operator.
Definition: Vector2.hh:316
bool IsFinite() const
See if a point is finite (e.g., not nan)
Definition: Vector2.hh:472
const Vector2 operator*(const Vector2 &_v) const
Multiplication operators.
Definition: Vector2.hh:397
Vector2 Round()
Round to near whole number, return the result.
Definition: Vector2.hh:129
double Distance(const Vector2 &_pt) const
Calc distance to the given point.
Definition: Vector2.hh:84
void Normalize()
Normalize the vector length.
Definition: Vector2.hh:107
const Vector2 & operator+=(const Vector2 &_v)
Addition assignment operator.
Definition: Vector2.hh:260
friend Vector2< T > operator+(const T _s, const Vector2< T > &_v)
Addition operators.
Definition: Vector2.hh:281
T fail(T... args)
Two dimensional (x, y) vector.
Definition: Vector2.hh:36
Vector2 operator-(const Vector2 &_v) const
Subtraction operator.
Definition: Vector2.hh:308
friend std::istream & operator>>(std::istream &_in, Vector2< T > &_pt)
Stream extraction operator.
Definition: Vector2.hh:563
static const size_t IGN_ONE_SIZE_T
size_t type with a value of 1
Definition: Helpers.hh:230
STL class.
T Dot(const Vector2< T > &_v) const
Get the dot product of this vector and _v.
Definition: Vector2.hh:157
const Vector2 & operator/=(T _v)
Division operator.
Definition: Vector2.hh:386
void Correct()
Corrects any nan values.
Definition: Vector2.hh:185
T & X()
Return a mutable x value.
Definition: Vector2.hh:512
T min(T... args)
static const Vector2< T > One
math::Vector2(1, 1)
Definition: Vector2.hh:42
T & Y()
Return a mutable y value.
Definition: Vector2.hh:519
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:178
Vector2 Normalized() const
Returns a normalized vector.
Definition: Vector2.hh:120
Vector2()
Default Constructor.
Definition: Vector2.hh:48
T X() const
Return the x value.
Definition: Vector2.hh:498
static const Vector2< T > Zero
math::Vector2(0, 0)
Definition: Vector2.hh:39
void X(const T &_v)
Set the x value.
Definition: Vector2.hh:526
T operator[](const std::size_t _index) const
Const-qualified array subscript operator.
Definition: Vector2.hh:491
T Sum() const
Return the sum of the values.
Definition: Vector2.hh:76
Vector2 Abs() const
Get the absolute value of the vector.
Definition: Vector2.hh:164
static const size_t IGN_ZERO_SIZE_T
size_t type with a value of 0
Definition: Helpers.hh:227
T isfinite(T... args)
bool Equal(const Vector2 &_v, const T &_tol) const
Equality test with tolerance.
Definition: Vector2.hh:448
T max(T... args)
Vector2(const T &_x, const T &_y)
Constructor.
Definition: Vector2.hh:57
T Max() const
Get the maximum value in the vector.
Definition: Vector2.hh:215
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:207
bool operator==(const Vector2 &_v) const
Equal to operator.
Definition: Vector2.hh:458
void Y(const T &_v)
Set the y value.
Definition: Vector2.hh:533
Vector2 Rounded() const
Get a rounded version of this vector.
Definition: Vector2.hh:138
Vector2< double > Vector2d
Definition: Vector2.hh:592
const Vector2 operator/(const Vector2 &_v) const
Division operator.
Definition: Vector2.hh:358
Vector2< T > operator-(const T _s) const
Subtraction operators.
Definition: Vector2.hh:327
const Vector2 operator/(T _v) const
Division operator.
Definition: Vector2.hh:378
bool operator!=(const Vector2 &_v) const
Not equal to operator.
Definition: Vector2.hh:465
Vector2 operator-() const
Negation operator.
Definition: Vector2.hh:300
T Length() const
Returns the length (magnitude) of the vector.
Definition: Vector2.hh:92
const Vector2< T > & operator+=(const T _s)
Addition assignment operator.
Definition: Vector2.hh:290
Vector2< T > operator+(const T _s) const
Addition operators.
Definition: Vector2.hh:271
Vector2< int > Vector2i
Definition: Vector2.hh:591
const Vector2 & operator/=(const Vector2 &_v)
Division operator.
Definition: Vector2.hh:367
Vector2< float > Vector2f
Definition: Vector2.hh:593
const Vector2 operator*(T _v) const
Multiplication operators.
Definition: Vector2.hh:417
Definition: Angle.hh:42
const Vector2 & operator*=(T _v)
Multiplication assignment operator.
Definition: Vector2.hh:435
Vector2 operator+(const Vector2 &_v) const
Addition operator.
Definition: Vector2.hh:252
Vector2(const Vector2< T > &_v)
Copy constructor.
Definition: Vector2.hh:65
STL class.
static const Vector2 NaN
math::Vector2(NaN, NaN, NaN)
Definition: Vector2.hh:45
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:406
virtual ~Vector2()
Destructor.
Definition: Vector2.hh:72
T SquaredLength() const
Returns the square of the length (magnitude) of the vector.
Definition: Vector2.hh:99
Vector2 & operator=(const Vector2 &_v)
Assignment operator.
Definition: Vector2.hh:230
T Min() const
Get the minimum value in the vector.
Definition: Vector2.hh:222
friend const Vector2 operator*(const T _s, const Vector2 &_v)
Scalar left multiplication operators.
Definition: Vector2.hh:426