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 
22 namespace ignition
23 {
24  namespace math
25  {
28  template<typename T>
29  class Vector2
30  {
32  public: static const Vector2<T> Zero;
33 
35  public: static const Vector2<T> One;
36 
38  public: Vector2()
39  {
40  this->data[0] = 0;
41  this->data[1] = 0;
42  }
43 
47  public: Vector2(const T &_x, const T &_y)
48  {
49  this->data[0] = _x;
50  this->data[1] = _y;
51  }
52 
55  public: Vector2(const Vector2<T> &_v)
56  {
57  this->data[0] = _v[0];
58  this->data[1] = _v[1];
59  }
60 
62  public: virtual ~Vector2() {}
63 
67  public: double Distance(const Vector2 &_pt) const
68  {
69  return sqrt((this->data[0]-_pt[0])*(this->data[0]-_pt[0]) +
70  (this->data[1]-_pt[1])*(this->data[1]-_pt[1]));
71  }
72 
75  public: T Length() const
76  {
77  return sqrt(this->SquaredLength());
78  }
79 
82  public: T SquaredLength() const
83  {
84  return std::pow(this->data[0], 2)
85  + std::pow(this->data[1], 2);
86  }
87 
89  public: void Normalize()
90  {
91  double d = this->Length();
92 
93  if (!equal<T>(d, static_cast<T>(0.0)))
94  {
95  this->data[0] /= d;
96  this->data[1] /= d;
97  }
98  }
99 
103  public: void Set(T _x, T _y)
104  {
105  this->data[0] = _x;
106  this->data[1] = _y;
107  }
108 
112  public: T Dot(const Vector2<T> &_v) const
113  {
114  return (this->data[0] * _v[0]) + (this->data[1] * _v[1]);
115  }
116 
120  public: Vector2 &operator=(const Vector2 &_v)
121  {
122  this->data[0] = _v[0];
123  this->data[1] = _v[1];
124 
125  return *this;
126  }
127 
131  public: const Vector2 &operator=(T _v)
132  {
133  this->data[0] = _v;
134  this->data[1] = _v;
135 
136  return *this;
137  }
138 
142  public: Vector2 operator+(const Vector2 &_v) const
143  {
144  return Vector2(this->data[0] + _v[0], this->data[1] + _v[1]);
145  }
146 
149  // \return this
150  public: const Vector2 &operator+=(const Vector2 &_v)
151  {
152  this->data[0] += _v[0];
153  this->data[1] += _v[1];
154 
155  return *this;
156  }
157 
161  public: inline Vector2<T> operator+(const T _s) const
162  {
163  return Vector2<T>(this->data[0] + _s,
164  this->data[1] + _s);
165  }
166 
171  public: friend inline Vector2<T> operator+(const T _s,
172  const Vector2<T> &_v)
173  {
174  return _v + _s;
175  }
176 
180  public: const Vector2<T> &operator+=(const T _s)
181  {
182  this->data[0] += _s;
183  this->data[1] += _s;
184 
185  return *this;
186  }
187 
190  public: inline Vector2 operator-() const
191  {
192  return Vector2(-this->data[0], -this->data[1]);
193  }
194 
198  public: Vector2 operator-(const Vector2 &_v) const
199  {
200  return Vector2(this->data[0] - _v[0], this->data[1] - _v[1]);
201  }
202 
206  public: const Vector2 &operator-=(const Vector2 &_v)
207  {
208  this->data[0] -= _v[0];
209  this->data[1] -= _v[1];
210 
211  return *this;
212  }
213 
217  public: inline Vector2<T> operator-(const T _s) const
218  {
219  return Vector2<T>(this->data[0] - _s,
220  this->data[1] - _s);
221  }
222 
227  public: friend inline Vector2<T> operator-(const T _s,
228  const Vector2<T> &_v)
229  {
230  return {_s - _v.X(), _s - _v.Y()};
231  }
232 
236  public: const Vector2<T> &operator-=(T _s)
237  {
238  this->data[0] -= _s;
239  this->data[1] -= _s;
240 
241  return *this;
242  }
243 
248  public: const Vector2 operator/(const Vector2 &_v) const
249  {
250  return Vector2(this->data[0] / _v[0], this->data[1] / _v[1]);
251  }
252 
257  public: const Vector2 &operator/=(const Vector2 &_v)
258  {
259  this->data[0] /= _v[0];
260  this->data[1] /= _v[1];
261 
262  return *this;
263  }
264 
268  public: const Vector2 operator/(T _v) const
269  {
270  return Vector2(this->data[0] / _v, this->data[1] / _v);
271  }
272 
276  public: const Vector2 &operator/=(T _v)
277  {
278  this->data[0] /= _v;
279  this->data[1] /= _v;
280 
281  return *this;
282  }
283 
287  public: const Vector2 operator*(const Vector2 &_v) const
288  {
289  return Vector2(this->data[0] * _v[0], this->data[1] * _v[1]);
290  }
291 
296  public: const Vector2 &operator*=(const Vector2 &_v)
297  {
298  this->data[0] *= _v[0];
299  this->data[1] *= _v[1];
300 
301  return *this;
302  }
303 
307  public: const Vector2 operator*(T _v) const
308  {
309  return Vector2(this->data[0] * _v, this->data[1] * _v);
310  }
311 
316  public: friend inline const Vector2 operator*(const T _s,
317  const Vector2 &_v)
318  {
319  return Vector2(_v * _s);
320  }
321 
325  public: const Vector2 &operator*=(T _v)
326  {
327  this->data[0] *= _v;
328  this->data[1] *= _v;
329 
330  return *this;
331  }
332 
338  public: bool Equal(const Vector2 &_v, const T &_tol) const
339  {
340  return equal<T>(this->data[0], _v[0], _tol)
341  && equal<T>(this->data[1], _v[1], _tol);
342  }
343 
348  public: bool operator==(const Vector2 &_v) const
349  {
350  return this->Equal(_v, static_cast<T>(1e-6));
351  }
352 
355  public: bool operator!=(const Vector2 &_v) const
356  {
357  return !(*this == _v);
358  }
359 
362  public: bool IsFinite() const
363  {
364  // std::isfinite works with floating point values,
365  // need to explicit cast to avoid ambiguity in vc++.
366  return std::isfinite(static_cast<double>(this->data[0])) &&
367  std::isfinite(static_cast<double>(this->data[1]));
368  }
369 
373  public: inline T operator[](const size_t _index) const
374  {
375  return this->data[clamp(_index, IGN_ZERO_SIZE_T, IGN_ONE_SIZE_T)];
376  }
377 
380  public: inline T X() const
381  {
382  return this->data[0];
383  }
384 
387  public: inline T Y() const
388  {
389  return this->data[1];
390  }
391 
394  public: inline T &X()
395  {
396  return this->data[0];
397  }
398 
401  public: inline T &Y()
402  {
403  return this->data[1];
404  }
405 
408  public: inline void X(const T &_v)
409  {
410  this->data[0] = _v;
411  }
412 
415  public: inline void Y(const T &_v)
416  {
417  this->data[1] = _v;
418  }
419 
424  public: friend std::ostream
425  &operator<<(std::ostream &_out, const Vector2<T> &_pt)
426  {
427  _out << _pt[0] << " " << _pt[1];
428  return _out;
429  }
430 
435  public: bool operator<(const Vector2<T> &_pt) const
436  {
437  return this->data[0] < _pt[0] || this->data[1] < _pt[1];
438  }
439 
444  public: friend std::istream
445  &operator>>(std::istream &_in, Vector2<T> &_pt)
446  {
447  T x, y;
448  // Skip white spaces
449  _in.setf(std::ios_base::skipws);
450  _in >> x >> y;
451  _pt.Set(x, y);
452  return _in;
453  }
454 
456  private: T data[2];
457  };
458 
459  template<typename T>
460  const Vector2<T> Vector2<T>::Zero(0, 0);
461 
462  template<typename T>
463  const Vector2<T> Vector2<T>::One(1, 1);
464 
468  }
469 }
470 #endif
friend Vector2< T > operator-(const T _s, const Vector2< T > &_v)
Subtraction operators.
Definition: Vector2.hh:227
const Vector2 & operator+=(const Vector2 &_v)
Addition assignment operator.
Definition: Vector2.hh:150
const Vector2 operator/(const Vector2 &_v) const
Division operator.
Definition: Vector2.hh:248
Vector2 operator-(const Vector2 &_v) const
Subtraction operator.
Definition: Vector2.hh:198
static const size_t IGN_ONE_SIZE_T
size_t type with a value of 1
Definition: Helpers.hh:216
const Vector2 & operator*=(const Vector2 &_v)
Multiplication assignment operator.
Definition: Vector2.hh:296
void Set(T _x, T _y)
Set the contents of the vector.
Definition: Vector2.hh:103
T & X()
Return a mutable x value.
Definition: Vector2.hh:394
const Vector2< T > & operator+=(const T _s)
Addition assignment operator.
Definition: Vector2.hh:180
T operator[](const size_t _index) const
Array subscript operator.
Definition: Vector2.hh:373
T SquaredLength() const
Returns the square of the length (magnitude) of the vector.
Definition: Vector2.hh:82
static const size_t IGN_ZERO_SIZE_T
size_t type with a value of 0
Definition: Helpers.hh:213
friend Vector2< T > operator+(const T _s, const Vector2< T > &_v)
Addition operators.
Definition: Vector2.hh:171
const Vector2< T > & operator-=(T _s)
Subtraction assignment operator.
Definition: Vector2.hh:236
Two dimensional (x, y) vector.
Definition: Vector2.hh:29
T X() const
Return the x value.
Definition: Vector2.hh:380
bool Equal(const Vector2 &_v, const T &_tol) const
Equality test with tolerance.
Definition: Vector2.hh:338
Vector2< float > Vector2f
Definition: Vector2.hh:467
Vector2< T > operator-(const T _s) const
Subtraction operators.
Definition: Vector2.hh:217
bool operator!=(const Vector2 &_v) const
Not equal to operator.
Definition: Vector2.hh:355
friend std::istream & operator>>(std::istream &_in, Vector2< T > &_pt)
Stream extraction operator.
Definition: Vector2.hh:445
const Vector2 & operator/=(T _v)
Division operator.
Definition: Vector2.hh:276
bool operator==(const Vector2 &_v) const
Equal to operator.
Definition: Vector2.hh:348
const Vector2 operator*(const Vector2 &_v) const
Multiplication operators.
Definition: Vector2.hh:287
Vector2 & operator=(const Vector2 &_v)
Assignment operator.
Definition: Vector2.hh:120
const Vector2 & operator/=(const Vector2 &_v)
Division operator.
Definition: Vector2.hh:257
T & Y()
Return a mutable y value.
Definition: Vector2.hh:401
T Dot(const Vector2< T > &_v) const
Get the dot product of this vector and _v.
Definition: Vector2.hh:112
const Vector2 operator/(T _v) const
Division operator.
Definition: Vector2.hh:268
Vector2()
Default Constructor.
Definition: Vector2.hh:38
void Y(const T &_v)
Set the y value.
Definition: Vector2.hh:415
T Length() const
Returns the length (magnitude) of the vector.
Definition: Vector2.hh:75
Vector2 operator+(const Vector2 &_v) const
Addition operator.
Definition: Vector2.hh:142
static const Vector2< T > Zero
math::Vector2(0, 0)
Definition: Vector2.hh:32
const Vector2 & operator=(T _v)
Assignment operator.
Definition: Vector2.hh:131
Vector2< int > Vector2i
Definition: Vector2.hh:465
void X(const T &_v)
Set the x value.
Definition: Vector2.hh:408
double Distance(const Vector2 &_pt) const
Calc distance to the given point.
Definition: Vector2.hh:67
const Vector2 & operator-=(const Vector2 &_v)
Subtraction assignment operator.
Definition: Vector2.hh:206
Vector2 operator-() const
Negation operator.
Definition: Vector2.hh:190
void Normalize()
Normalize the vector length.
Definition: Vector2.hh:89
virtual ~Vector2()
Destructor.
Definition: Vector2.hh:62
Vector2(const Vector2< T > &_v)
Copy constructor.
Definition: Vector2.hh:55
Vector2< double > Vector2d
Definition: Vector2.hh:466
Vector2(const T &_x, const T &_y)
Constructor.
Definition: Vector2.hh:47
Vector2< T > operator+(const T _s) const
Addition operators.
Definition: Vector2.hh:161
static const Vector2< T > One
math::Vector2(1, 1)
Definition: Vector2.hh:35
Definition: Angle.hh:38
bool IsFinite() const
See if a point is finite (e.g., not nan)
Definition: Vector2.hh:362
T Y() const
Return the y value.
Definition: Vector2.hh:387
const Vector2 & operator*=(T _v)
Multiplication assignment operator.
Definition: Vector2.hh:325
const Vector2 operator*(T _v) const
Multiplication operators.
Definition: Vector2.hh:307
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:392
friend const Vector2 operator*(const T _s, const Vector2 &_v)
Scalar left multiplication operators.
Definition: Vector2.hh:316