Ignition Math

API Reference

6.6.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 
124  public: Vector2 Round()
125  {
126  this->data[0] = nearbyint(this->data[0]);
127  this->data[1] = nearbyint(this->data[1]);
128  return *this;
129  }
130 
133  public: Vector2 Rounded() const
134  {
135  Vector2<T> result = *this;
136  result.Round();
137  return result;
138  }
139 
143  public: void Set(T _x, T _y)
144  {
145  this->data[0] = _x;
146  this->data[1] = _y;
147  }
148 
152  public: T Dot(const Vector2<T> &_v) const
153  {
154  return (this->data[0] * _v[0]) + (this->data[1] * _v[1]);
155  }
156 
159  public: Vector2 Abs() const
160  {
161  return Vector2(std::abs(this->data[0]),
162  std::abs(this->data[1]));
163  }
164 
173  public: T AbsDot(const Vector2<T> &_v) const
174  {
175  return std::abs(this->data[0] * _v[0]) +
176  std::abs(this->data[1] * _v[1]);
177  }
178 
180  public: inline void Correct()
181  {
182  // std::isfinite works with floating point values,
183  // need to explicit cast to avoid ambiguity in vc++.
184  if (!std::isfinite(static_cast<double>(this->data[0])))
185  this->data[0] = 0;
186  if (!std::isfinite(static_cast<double>(this->data[1])))
187  this->data[1] = 0;
188  }
189 
193  public: void Max(const Vector2<T> &_v)
194  {
195  this->data[0] = std::max(_v[0], this->data[0]);
196  this->data[1] = std::max(_v[1], this->data[1]);
197  }
198 
202  public: void Min(const Vector2<T> &_v)
203  {
204  this->data[0] = std::min(_v[0], this->data[0]);
205  this->data[1] = std::min(_v[1], this->data[1]);
206  }
207 
210  public: T Max() const
211  {
212  return std::max(this->data[0], this->data[1]);
213  }
214 
217  public: T Min() const
218  {
219  return std::min(this->data[0], this->data[1]);
220  }
221 
225  public: Vector2 &operator=(const Vector2 &_v)
226  {
227  this->data[0] = _v[0];
228  this->data[1] = _v[1];
229 
230  return *this;
231  }
232 
236  public: const Vector2 &operator=(T _v)
237  {
238  this->data[0] = _v;
239  this->data[1] = _v;
240 
241  return *this;
242  }
243 
247  public: Vector2 operator+(const Vector2 &_v) const
248  {
249  return Vector2(this->data[0] + _v[0], this->data[1] + _v[1]);
250  }
251 
254  // \return this
255  public: const Vector2 &operator+=(const Vector2 &_v)
256  {
257  this->data[0] += _v[0];
258  this->data[1] += _v[1];
259 
260  return *this;
261  }
262 
266  public: inline Vector2<T> operator+(const T _s) const
267  {
268  return Vector2<T>(this->data[0] + _s,
269  this->data[1] + _s);
270  }
271 
276  public: friend inline Vector2<T> operator+(const T _s,
277  const Vector2<T> &_v)
278  {
279  return _v + _s;
280  }
281 
285  public: const Vector2<T> &operator+=(const T _s)
286  {
287  this->data[0] += _s;
288  this->data[1] += _s;
289 
290  return *this;
291  }
292 
295  public: inline Vector2 operator-() const
296  {
297  return Vector2(-this->data[0], -this->data[1]);
298  }
299 
303  public: Vector2 operator-(const Vector2 &_v) const
304  {
305  return Vector2(this->data[0] - _v[0], this->data[1] - _v[1]);
306  }
307 
311  public: const Vector2 &operator-=(const Vector2 &_v)
312  {
313  this->data[0] -= _v[0];
314  this->data[1] -= _v[1];
315 
316  return *this;
317  }
318 
322  public: inline Vector2<T> operator-(const T _s) const
323  {
324  return Vector2<T>(this->data[0] - _s,
325  this->data[1] - _s);
326  }
327 
332  public: friend inline Vector2<T> operator-(const T _s,
333  const Vector2<T> &_v)
334  {
335  return {_s - _v.X(), _s - _v.Y()};
336  }
337 
341  public: const Vector2<T> &operator-=(T _s)
342  {
343  this->data[0] -= _s;
344  this->data[1] -= _s;
345 
346  return *this;
347  }
348 
353  public: const Vector2 operator/(const Vector2 &_v) const
354  {
355  return Vector2(this->data[0] / _v[0], this->data[1] / _v[1]);
356  }
357 
362  public: const Vector2 &operator/=(const Vector2 &_v)
363  {
364  this->data[0] /= _v[0];
365  this->data[1] /= _v[1];
366 
367  return *this;
368  }
369 
373  public: const Vector2 operator/(T _v) const
374  {
375  return Vector2(this->data[0] / _v, this->data[1] / _v);
376  }
377 
381  public: const Vector2 &operator/=(T _v)
382  {
383  this->data[0] /= _v;
384  this->data[1] /= _v;
385 
386  return *this;
387  }
388 
392  public: const Vector2 operator*(const Vector2 &_v) const
393  {
394  return Vector2(this->data[0] * _v[0], this->data[1] * _v[1]);
395  }
396 
401  public: const Vector2 &operator*=(const Vector2 &_v)
402  {
403  this->data[0] *= _v[0];
404  this->data[1] *= _v[1];
405 
406  return *this;
407  }
408 
412  public: const Vector2 operator*(T _v) const
413  {
414  return Vector2(this->data[0] * _v, this->data[1] * _v);
415  }
416 
421  public: friend inline const Vector2 operator*(const T _s,
422  const Vector2 &_v)
423  {
424  return Vector2(_v * _s);
425  }
426 
430  public: const Vector2 &operator*=(T _v)
431  {
432  this->data[0] *= _v;
433  this->data[1] *= _v;
434 
435  return *this;
436  }
437 
443  public: bool Equal(const Vector2 &_v, const T &_tol) const
444  {
445  return equal<T>(this->data[0], _v[0], _tol)
446  && equal<T>(this->data[1], _v[1], _tol);
447  }
448 
453  public: bool operator==(const Vector2 &_v) const
454  {
455  return this->Equal(_v, static_cast<T>(1e-6));
456  }
457 
460  public: bool operator!=(const Vector2 &_v) const
461  {
462  return !(*this == _v);
463  }
464 
467  public: bool IsFinite() const
468  {
469  // std::isfinite works with floating point values,
470  // need to explicit cast to avoid ambiguity in vc++.
471  return std::isfinite(static_cast<double>(this->data[0])) &&
472  std::isfinite(static_cast<double>(this->data[1]));
473  }
474 
478  public: T &operator[](const std::size_t _index)
479  {
480  return this->data[clamp(_index, IGN_ZERO_SIZE_T, IGN_ONE_SIZE_T)];
481  }
482 
486  public: T operator[](const std::size_t _index) const
487  {
488  return this->data[clamp(_index, IGN_ZERO_SIZE_T, IGN_ONE_SIZE_T)];
489  }
490 
493  public: inline T X() const
494  {
495  return this->data[0];
496  }
497 
500  public: inline T Y() const
501  {
502  return this->data[1];
503  }
504 
507  public: inline T &X()
508  {
509  return this->data[0];
510  }
511 
514  public: inline T &Y()
515  {
516  return this->data[1];
517  }
518 
521  public: inline void X(const T &_v)
522  {
523  this->data[0] = _v;
524  }
525 
528  public: inline void Y(const T &_v)
529  {
530  this->data[1] = _v;
531  }
532 
537  public: friend std::ostream
538  &operator<<(std::ostream &_out, const Vector2<T> &_pt)
539  {
540  _out << _pt[0] << " " << _pt[1];
541  return _out;
542  }
543 
548  public: bool operator<(const Vector2<T> &_pt) const
549  {
550  return this->data[0] < _pt[0] || this->data[1] < _pt[1];
551  }
552 
557  public: friend std::istream
559  {
560  T x, y;
561  // Skip white spaces
562  _in.setf(std::ios_base::skipws);
563  _in >> x >> y;
564  _pt.Set(x, y);
565  return _in;
566  }
567 
569  private: T data[2];
570  };
571 
572  template<typename T>
573  const Vector2<T> Vector2<T>::Zero(0, 0);
574 
575  template<typename T>
576  const Vector2<T> Vector2<T>::One(1, 1);
577 
581  }
582  }
583 }
584 #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:193
friend Vector2< T > operator-(const T _s, const Vector2< T > &_v)
Subtraction operators.
Definition: Vector2.hh:332
const Vector2 & operator*=(const Vector2 &_v)
Multiplication assignment operator.
Definition: Vector2.hh:401
const Vector2< T > & operator-=(T _s)
Subtraction assignment operator.
Definition: Vector2.hh:341
T setf(T... args)
const Vector2 & operator=(T _v)
Assignment operator.
Definition: Vector2.hh:236
T & operator[](const std::size_t _index)
Array subscript operator.
Definition: Vector2.hh:478
void Set(T _x, T _y)
Set the contents of the vector.
Definition: Vector2.hh:143
T Y() const
Return the y value.
Definition: Vector2.hh:500
const Vector2 & operator-=(const Vector2 &_v)
Subtraction assignment operator.
Definition: Vector2.hh:311
bool IsFinite() const
See if a point is finite (e.g., not nan)
Definition: Vector2.hh:467
const Vector2 operator*(const Vector2 &_v) const
Multiplication operators.
Definition: Vector2.hh:392
Vector2 Round()
Round to near whole number, return the result.
Definition: Vector2.hh:124
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:255
friend Vector2< T > operator+(const T _s, const Vector2< T > &_v)
Addition operators.
Definition: Vector2.hh:276
Two dimensional (x, y) vector.
Definition: Vector2.hh:35
Vector2 operator-(const Vector2 &_v) const
Subtraction operator.
Definition: Vector2.hh:303
friend std::istream & operator>>(std::istream &_in, Vector2< T > &_pt)
Stream extraction operator.
Definition: Vector2.hh:558
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:152
const Vector2 & operator/=(T _v)
Division operator.
Definition: Vector2.hh:381
void Correct()
Corrects any nan values.
Definition: Vector2.hh:180
T & X()
Return a mutable x value.
Definition: Vector2.hh:507
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:514
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:173
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:493
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:521
T operator[](const std::size_t _index) const
Const-qualified array subscript operator.
Definition: Vector2.hh:486
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:159
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:443
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:210
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:202
bool operator==(const Vector2 &_v) const
Equal to operator.
Definition: Vector2.hh:453
void Y(const T &_v)
Set the y value.
Definition: Vector2.hh:528
Vector2 Rounded() const
Get a rounded version of this vector.
Definition: Vector2.hh:133
T pow(T... args)
Vector2< double > Vector2d
Definition: Vector2.hh:579
const Vector2 operator/(const Vector2 &_v) const
Division operator.
Definition: Vector2.hh:353
Vector2< T > operator-(const T _s) const
Subtraction operators.
Definition: Vector2.hh:322
const Vector2 operator/(T _v) const
Division operator.
Definition: Vector2.hh:373
bool operator!=(const Vector2 &_v) const
Not equal to operator.
Definition: Vector2.hh:460
Vector2 operator-() const
Negation operator.
Definition: Vector2.hh:295
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:285
Vector2< T > operator+(const T _s) const
Addition operators.
Definition: Vector2.hh:266
Vector2< int > Vector2i
Definition: Vector2.hh:578
const Vector2 & operator/=(const Vector2 &_v)
Division operator.
Definition: Vector2.hh:362
Vector2< float > Vector2f
Definition: Vector2.hh:580
const Vector2 operator*(T _v) const
Multiplication operators.
Definition: Vector2.hh:412
Definition: Angle.hh:42
const Vector2 & operator*=(T _v)
Multiplication assignment operator.
Definition: Vector2.hh:430
Vector2 operator+(const Vector2 &_v) const
Addition operator.
Definition: Vector2.hh:247
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:406
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:225
T Min() const
Get the minimum value in the vector.
Definition: Vector2.hh:217
friend const Vector2 operator*(const T _s, const Vector2 &_v)
Scalar left multiplication operators.
Definition: Vector2.hh:421