All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
Vector2.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2014 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_VECTOR2_HH_
18 #define _IGNITION_VECTOR2_HH_
19 
21 
22 namespace ignition
23 {
24  namespace math
25  {
28  template<typename T>
29  class Vector2
30  {
32  public: Vector2()
33  {
34  this->data[0] = 0;
35  this->data[1] = 0;
36  }
37 
41  public: Vector2(const T &_x, const T &_y)
42  {
43  this->data[0] = _x;
44  this->data[1] = _y;
45  }
46 
49  public: Vector2(const Vector2<T> &_v)
50  {
51  this->data[0] = _v[0];
52  this->data[1] = _v[1];
53  }
54 
56  public: virtual ~Vector2() {}
57 
61  public: double Distance(const Vector2 &_pt) const
62  {
63  return sqrt((this->data[0]-_pt[0])*(this->data[0]-_pt[0]) +
64  (this->data[1]-_pt[1])*(this->data[1]-_pt[1]));
65  }
66 
68  public: void Normalize()
69  {
70  double d = sqrt(this->data[0] * this->data[0] +
71  this->data[1] * this->data[1]);
72 
73  this->data[0] /= d;
74  this->data[1] /= d;
75  }
76 
80  public: void Set(T _x, T _y)
81  {
82  this->data[0] = _x;
83  this->data[1] = _y;
84  }
85 
89  public: T Dot(const Vector2<T> &_v) const
90  {
91  return (this->data[0] * _v[0]) + (this->data[1] * _v[1]);
92  }
93 
97  public: Vector2 &operator=(const Vector2 &_v)
98  {
99  this->data[0] = _v[0];
100  this->data[1] = _v[1];
101 
102  return *this;
103  }
104 
108  public: const Vector2 &operator=(T _v)
109  {
110  this->data[0] = _v;
111  this->data[1] = _v;
112 
113  return *this;
114  }
115 
119  public: Vector2 operator+(const Vector2 &_v) const
120  {
121  return Vector2(this->data[0] + _v[0], this->data[1] + _v[1]);
122  }
123 
126  // \return this
127  public: const Vector2 &operator+=(const Vector2 &_v)
128  {
129  this->data[0] += _v[0];
130  this->data[1] += _v[1];
131 
132  return *this;
133  }
134 
138  public: Vector2 operator-(const Vector2 &_v) const
139  {
140  return Vector2(this->data[0] - _v[0], this->data[1] - _v[1]);
141  }
142 
146  public: const Vector2 &operator-=(const Vector2 &_v)
147  {
148  this->data[0] -= _v[0];
149  this->data[1] -= _v[1];
150 
151  return *this;
152  }
153 
158  public: const Vector2 operator/(const Vector2 &_v) const
159  {
160  return Vector2(this->data[0] / _v[0], this->data[1] / _v[1]);
161  }
162 
167  public: const Vector2 &operator/=(const Vector2 &_v)
168  {
169  this->data[0] /= _v[0];
170  this->data[1] /= _v[1];
171 
172  return *this;
173  }
174 
178  public: const Vector2 operator/(T _v) const
179  {
180  return Vector2(this->data[0] / _v, this->data[1] / _v);
181  }
182 
186  public: const Vector2 &operator/=(T _v)
187  {
188  this->data[0] /= _v;
189  this->data[1] /= _v;
190 
191  return *this;
192  }
193 
197  public: const Vector2 operator*(const Vector2 &_v) const
198  {
199  return Vector2(this->data[0] * _v[0], this->data[1] * _v[1]);
200  }
201 
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: const Vector2 operator*(T _v) const
218  {
219  return Vector2(this->data[0] * _v, this->data[1] * _v);
220  }
221 
225  public: const Vector2 &operator*=(T _v)
226  {
227  this->data[0] *= _v;
228  this->data[1] *= _v;
229 
230  return *this;
231  }
232 
237  public: bool operator==(const Vector2 &_v) const
238  {
239  return equal(this->data[0], _v[0]) && equal(this->data[1], _v[1]);
240  }
241 
244  public: bool operator!=(const Vector2 &_v) const
245  {
246  return !(*this == _v);
247  }
248 
251  public: bool IsFinite() const
252  {
253  // std::isfinite works with floating point values,
254  // need to explicit cast to avoid ambiguity in vc++.
255  return std::isfinite(static_cast<double>(this->data[0])) &&
256  std::isfinite(static_cast<double>(this->data[1]));
257  }
258 
264  public: inline T operator[](size_t _index) const
265  {
266  if (_index > 1)
267  throw IndexException();
268  return this->data[_index];
269  }
270 
274  public: inline T X() const
275  {
276  return this->data[0];
277  }
278 
282  public: inline T Y() const
283  {
284  return this->data[1];
285  }
286 
290  public: inline T &X()
291  {
292  return this->data[0];
293  }
294 
298  public: inline T &Y()
299  {
300  return this->data[1];
301  }
302 
305  public: inline void X(const T &_v)
306  {
307  this->data[0] = _v;
308  }
309 
312  public: inline void Y(const T &_v)
313  {
314  this->data[1] = _v;
315  }
316 
322  public: friend std::ostream
323  &operator<<(std::ostream &_out, const Vector2<T> &_pt)
324  {
325  _out << _pt[0] << " " << _pt[1];
326  return _out;
327  }
328 
333  public: bool operator<(const Vector2<T> &_pt) const
334  {
335  return this->data[0] < _pt[0] || this->data[1] < _pt[1];
336  }
337 
343  public: friend std::istream
344  &operator>>(std::istream &_in, Vector2<T> &_pt)
345  {
346  T x, y;
347  // Skip white spaces
348  _in.setf(std::ios_base::skipws);
349  _in >> x >> y;
350  _pt.Set(x, y);
351  return _in;
352  }
353 
355  private: T data[2];
356  };
357 
361  }
362 }
363 #endif
const Vector2 & operator+=(const Vector2 &_v)
Addition assignment operator.
Definition: Vector2.hh:127
Vector2 operator-(const Vector2 &_v) const
Subtraction operator.
Definition: Vector2.hh:138
const Vector2 & operator*=(const Vector2 &_v)
Multiplication assignment operator.
Definition: Vector2.hh:206
void Set(T _x, T _y)
Set the contents of the vector.
Definition: Vector2.hh:80
T & X()
Return a mutable x value.
Definition: Vector2.hh:290
T Y() const
Return the y value.
Definition: Vector2.hh:282
T X() const
Return the x value.
Definition: Vector2.hh:274
Two dimensional (x, y) vector.
Definition: Vector2.hh:29
const Vector2 operator/(const Vector2 &_v) const
Division operator.
Definition: Vector2.hh:158
const Vector2 operator*(T _v) const
Multiplication operators.
Definition: Vector2.hh:217
Vector2< float > Vector2f
Definition: Vector2.hh:360
friend std::istream & operator>>(std::istream &_in, Vector2< T > &_pt)
Stream extraction operator.
Definition: Vector2.hh:344
T operator[](size_t _index) const
Array subscript operator.
Definition: Vector2.hh:264
const Vector2 & operator/=(T _v)
Division operator.
Definition: Vector2.hh:186
double Distance(const Vector2 &_pt) const
Calc distance to the given point.
Definition: Vector2.hh:61
Vector2 & operator=(const Vector2 &_v)
Assignment operator.
Definition: Vector2.hh:97
const Vector2 & operator/=(const Vector2 &_v)
Division operator.
Definition: Vector2.hh:167
T & Y()
Return a mutable y value.
Definition: Vector2.hh:298
const Vector2 operator/(T _v) const
Division operator.
Definition: Vector2.hh:178
Vector2()
Default Constructor.
Definition: Vector2.hh:32
void Y(const T &_v)
Set the y value.
Definition: Vector2.hh:312
Vector2 operator+(const Vector2 &_v) const
Addition operator.
Definition: Vector2.hh:119
T Dot(const Vector2< T > &_v) const
Get the dot product of this vector and _v.
Definition: Vector2.hh:89
bool operator!=(const Vector2 &_v) const
Not equal to operator.
Definition: Vector2.hh:244
const Vector2 & operator=(T _v)
Assignment operator.
Definition: Vector2.hh:108
Exception that is thrown when an out-of-bounds index is encountered.
Definition: IndexException.hh:30
Vector2< int > Vector2i
Definition: Vector2.hh:358
bool IsFinite() const
See if a point is finite (e.g., not nan)
Definition: Vector2.hh:251
void X(const T &_v)
Set the x value.
Definition: Vector2.hh:305
bool operator==(const Vector2 &_v) const
Equal to operator.
Definition: Vector2.hh:237
const Vector2 & operator-=(const Vector2 &_v)
Subtraction assignment operator.
Definition: Vector2.hh:146
void Normalize()
Normalize the vector length.
Definition: Vector2.hh:68
virtual ~Vector2()
Destructor.
Definition: Vector2.hh:56
Vector2(const Vector2< T > &_v)
Copy constructor.
Definition: Vector2.hh:49
Vector2< double > Vector2d
Definition: Vector2.hh:359
Vector2(const T &_x, const T &_y)
Constructor.
Definition: Vector2.hh:41
bool equal(const T &_a, const T &_b, const T &_epsilon=1e-6)
check if two values are equal, within a tolerance
Definition: Helpers.hh:177
const Vector2 operator*(const Vector2 &_v) const
Multiplication operators.
Definition: Vector2.hh:197
const Vector2 & operator*=(T _v)
Multiplication assignment operator.
Definition: Vector2.hh:225