Loading...
Searching...
No Matches
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
21#include <ignition/math/config.hh>
22
23namespace ignition
24{
25 namespace math
26 {
27 inline namespace IGNITION_MATH_VERSION_NAMESPACE
28 {
31 template<typename T>
32 class Vector2
33 {
35 public: static const Vector2<T> Zero;
36
38 public: static const Vector2<T> One;
39
41 public: Vector2()
42 {
43 this->data[0] = 0;
44 this->data[1] = 0;
45 }
46
50 public: Vector2(const T &_x, const T &_y)
51 {
52 this->data[0] = _x;
53 this->data[1] = _y;
54 }
55
58 public: Vector2(const Vector2<T> &_v)
59 {
60 this->data[0] = _v[0];
61 this->data[1] = _v[1];
62 }
63
65 public: virtual ~Vector2() {}
66
70 public: double Distance(const Vector2 &_pt) const
71 {
72 return sqrt((this->data[0]-_pt[0])*(this->data[0]-_pt[0]) +
73 (this->data[1]-_pt[1])*(this->data[1]-_pt[1]));
74 }
75
78 public: T Length() const
79 {
80 return sqrt(this->SquaredLength());
81 }
82
85 public: T SquaredLength() const
86 {
87 return std::pow(this->data[0], 2)
88 + std::pow(this->data[1], 2);
89 }
90
92 public: void Normalize()
93 {
94 double d = this->Length();
95
96 if (!equal<T>(d, static_cast<T>(0.0)))
97 {
98 this->data[0] /= d;
99 this->data[1] /= d;
100 }
101 }
102
106 public: void Set(T _x, T _y)
107 {
108 this->data[0] = _x;
109 this->data[1] = _y;
110 }
111
115 public: T Dot(const Vector2<T> &_v) const
116 {
117 return (this->data[0] * _v[0]) + (this->data[1] * _v[1]);
118 }
119
123 public: Vector2 &operator=(const Vector2 &_v)
124 {
125 this->data[0] = _v[0];
126 this->data[1] = _v[1];
127
128 return *this;
129 }
130
134 public: const Vector2 &operator=(T _v)
135 {
136 this->data[0] = _v;
137 this->data[1] = _v;
138
139 return *this;
140 }
141
145 public: Vector2 operator+(const Vector2 &_v) const
146 {
147 return Vector2(this->data[0] + _v[0], this->data[1] + _v[1]);
148 }
149
152 // \return this
153 public: const Vector2 &operator+=(const Vector2 &_v)
154 {
155 this->data[0] += _v[0];
156 this->data[1] += _v[1];
157
158 return *this;
159 }
160
164 public: inline Vector2<T> operator+(const T _s) const
165 {
166 return Vector2<T>(this->data[0] + _s,
167 this->data[1] + _s);
168 }
169
174 public: friend inline Vector2<T> operator+(const T _s,
175 const Vector2<T> &_v)
176 {
177 return _v + _s;
178 }
179
183 public: const Vector2<T> &operator+=(const T _s)
184 {
185 this->data[0] += _s;
186 this->data[1] += _s;
187
188 return *this;
189 }
190
193 public: inline Vector2 operator-() const
194 {
195 return Vector2(-this->data[0], -this->data[1]);
196 }
197
201 public: Vector2 operator-(const Vector2 &_v) const
202 {
203 return Vector2(this->data[0] - _v[0], this->data[1] - _v[1]);
204 }
205
209 public: const Vector2 &operator-=(const Vector2 &_v)
210 {
211 this->data[0] -= _v[0];
212 this->data[1] -= _v[1];
213
214 return *this;
215 }
216
220 public: inline Vector2<T> operator-(const T _s) const
221 {
222 return Vector2<T>(this->data[0] - _s,
223 this->data[1] - _s);
224 }
225
230 public: friend inline Vector2<T> operator-(const T _s,
231 const Vector2<T> &_v)
232 {
233 return {_s - _v.X(), _s - _v.Y()};
234 }
235
239 public: const Vector2<T> &operator-=(T _s)
240 {
241 this->data[0] -= _s;
242 this->data[1] -= _s;
243
244 return *this;
245 }
246
251 public: const Vector2 operator/(const Vector2 &_v) const
252 {
253 return Vector2(this->data[0] / _v[0], this->data[1] / _v[1]);
254 }
255
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: const Vector2 operator/(T _v) const
272 {
273 return Vector2(this->data[0] / _v, this->data[1] / _v);
274 }
275
279 public: const Vector2 &operator/=(T _v)
280 {
281 this->data[0] /= _v;
282 this->data[1] /= _v;
283
284 return *this;
285 }
286
290 public: const Vector2 operator*(const Vector2 &_v) const
291 {
292 return Vector2(this->data[0] * _v[0], this->data[1] * _v[1]);
293 }
294
299 public: const Vector2 &operator*=(const Vector2 &_v)
300 {
301 this->data[0] *= _v[0];
302 this->data[1] *= _v[1];
303
304 return *this;
305 }
306
310 public: const Vector2 operator*(T _v) const
311 {
312 return Vector2(this->data[0] * _v, this->data[1] * _v);
313 }
314
319 public: friend inline const Vector2 operator*(const T _s,
320 const Vector2 &_v)
321 {
322 return Vector2(_v * _s);
323 }
324
328 public: const Vector2 &operator*=(T _v)
329 {
330 this->data[0] *= _v;
331 this->data[1] *= _v;
332
333 return *this;
334 }
335
341 public: bool Equal(const Vector2 &_v, const T &_tol) const
342 {
343 return equal<T>(this->data[0], _v[0], _tol)
344 && equal<T>(this->data[1], _v[1], _tol);
345 }
346
351 public: bool operator==(const Vector2 &_v) const
352 {
353 return this->Equal(_v, static_cast<T>(1e-6));
354 }
355
358 public: bool operator!=(const Vector2 &_v) const
359 {
360 return !(*this == _v);
361 }
362
365 public: bool IsFinite() const
366 {
367 // std::isfinite works with floating point values,
368 // need to explicit cast to avoid ambiguity in vc++.
369 return std::isfinite(static_cast<double>(this->data[0])) &&
370 std::isfinite(static_cast<double>(this->data[1]));
371 }
372
376 public: T &operator[](const std::size_t _index)
377 {
378 return this->data[clamp(_index, IGN_ZERO_SIZE_T, IGN_ONE_SIZE_T)];
379 }
380
384 public: T operator[](const std::size_t _index) const
385 {
386 return this->data[clamp(_index, IGN_ZERO_SIZE_T, IGN_ONE_SIZE_T)];
387 }
388
391 public: inline T X() const
392 {
393 return this->data[0];
394 }
395
398 public: inline T Y() const
399 {
400 return this->data[1];
401 }
402
405 public: inline T &X()
406 {
407 return this->data[0];
408 }
409
412 public: inline T &Y()
413 {
414 return this->data[1];
415 }
416
419 public: inline void X(const T &_v)
420 {
421 this->data[0] = _v;
422 }
423
426 public: inline void Y(const T &_v)
427 {
428 this->data[1] = _v;
429 }
430
435 public: friend std::ostream
436 &operator<<(std::ostream &_out, const Vector2<T> &_pt)
437 {
438 _out << _pt[0] << " " << _pt[1];
439 return _out;
440 }
441
446 public: bool operator<(const Vector2<T> &_pt) const
447 {
448 return this->data[0] < _pt[0] || this->data[1] < _pt[1];
449 }
450
455 public: friend std::istream
456 &operator>>(std::istream &_in, Vector2<T> &_pt)
457 {
458 T x, y;
459 // Skip white spaces
460 _in.setf(std::ios_base::skipws);
461 _in >> x >> y;
462 _pt.Set(x, y);
463 return _in;
464 }
465
467 private: T data[2];
468 };
469
470 template<typename T>
471 const Vector2<T> Vector2<T>::Zero(0, 0);
472
473 template<typename T>
474 const Vector2<T> Vector2<T>::One(1, 1);
475
479 }
480 }
481}
482#endif
Two dimensional (x, y) vector.
Definition Vector2.hh:33
friend std::ostream & operator<<(std::ostream &_out, const Vector2< T > &_pt)
Stream extraction operator.
Definition Vector2.hh:436
T operator[](const std::size_t _index) const
Const-qualified array subscript operator.
Definition Vector2.hh:384
const Vector2< T > & operator+=(const T _s)
Addition assignment operator.
Definition Vector2.hh:183
Vector2< T > operator-(const T _s) const
Subtraction operators.
Definition Vector2.hh:220
friend const Vector2 operator*(const T _s, const Vector2 &_v)
Scalar left multiplication operators.
Definition Vector2.hh:319
const Vector2 operator*(T _v) const
Multiplication operators.
Definition Vector2.hh:310
T SquaredLength() const
Returns the square of the length (magnitude) of the vector.
Definition Vector2.hh:85
bool operator==(const Vector2 &_v) const
Equal to operator.
Definition Vector2.hh:351
const Vector2 & operator/=(const Vector2 &_v)
Division operator.
Definition Vector2.hh:260
double Distance(const Vector2 &_pt) const
Calc distance to the given point.
Definition Vector2.hh:70
const Vector2 & operator*=(T _v)
Multiplication assignment operator.
Definition Vector2.hh:328
bool operator<(const Vector2< T > &_pt) const
Less than operator.
Definition Vector2.hh:446
virtual ~Vector2()
Destructor.
Definition Vector2.hh:65
const Vector2 & operator-=(const Vector2 &_v)
Subtraction assignment operator.
Definition Vector2.hh:209
Vector2()
Default Constructor.
Definition Vector2.hh:41
T & operator[](const std::size_t _index)
Array subscript operator.
Definition Vector2.hh:376
bool Equal(const Vector2 &_v, const T &_tol) const
Equality test with tolerance.
Definition Vector2.hh:341
void Normalize()
Normalize the vector length.
Definition Vector2.hh:92
T & Y()
Return a mutable y value.
Definition Vector2.hh:412
static const Vector2< T > One
math::Vector2(1, 1)
Definition Vector2.hh:38
const Vector2< T > & operator-=(T _s)
Subtraction assignment operator.
Definition Vector2.hh:239
Vector2(const T &_x, const T &_y)
Constructor.
Definition Vector2.hh:50
const Vector2 & operator/=(T _v)
Division operator.
Definition Vector2.hh:279
void X(const T &_v)
Set the x value.
Definition Vector2.hh:419
friend std::istream & operator>>(std::istream &_in, Vector2< T > &_pt)
Stream extraction operator.
Definition Vector2.hh:456
friend Vector2< T > operator-(const T _s, const Vector2< T > &_v)
Subtraction operators.
Definition Vector2.hh:230
T Y() const
Return the y value.
Definition Vector2.hh:398
const Vector2 operator/(const Vector2 &_v) const
Division operator.
Definition Vector2.hh:251
friend Vector2< T > operator+(const T _s, const Vector2< T > &_v)
Addition operators.
Definition Vector2.hh:174
T & X()
Return a mutable x value.
Definition Vector2.hh:405
void Y(const T &_v)
Set the y value.
Definition Vector2.hh:426
Vector2 & operator=(const Vector2 &_v)
Assignment operator.
Definition Vector2.hh:123
Vector2(const Vector2< T > &_v)
Copy constructor.
Definition Vector2.hh:58
const Vector2 & operator+=(const Vector2 &_v)
Addition assignment operator.
Definition Vector2.hh:153
void Set(T _x, T _y)
Set the contents of the vector.
Definition Vector2.hh:106
Vector2< T > operator+(const T _s) const
Addition operators.
Definition Vector2.hh:164
T Length() const
Returns the length (magnitude) of the vector.
Definition Vector2.hh:78
Vector2 operator+(const Vector2 &_v) const
Addition operator.
Definition Vector2.hh:145
const Vector2 & operator*=(const Vector2 &_v)
Multiplication assignment operator.
Definition Vector2.hh:299
static const Vector2< T > Zero
math::Vector2(0, 0)
Definition Vector2.hh:35
const Vector2 operator*(const Vector2 &_v) const
Multiplication operators.
Definition Vector2.hh:290
bool IsFinite() const
See if a point is finite (e.g., not nan)
Definition Vector2.hh:365
T X() const
Return the x value.
Definition Vector2.hh:391
Vector2 operator-(const Vector2 &_v) const
Subtraction operator.
Definition Vector2.hh:201
const Vector2 & operator=(T _v)
Assignment operator.
Definition Vector2.hh:134
const Vector2 operator/(T _v) const
Division operator.
Definition Vector2.hh:271
T Dot(const Vector2< T > &_v) const
Get the dot product of this vector and _v.
Definition Vector2.hh:115
bool operator!=(const Vector2 &_v) const
Not equal to operator.
Definition Vector2.hh:358
Vector2 operator-() const
Negation operator.
Definition Vector2.hh:193
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition Helpers.hh:395
bool equal(const T &_a, const T &_b, const T &_epsilon=T(1e-6))
check if two values are equal, within a tolerance
Definition Helpers.hh:545
Vector2< double > Vector2d
Definition Vector2.hh:477
static const size_t IGN_ZERO_SIZE_T
size_t type with a value of 0
Definition Helpers.hh:216
Vector2< int > Vector2i
Definition Vector2.hh:476
Vector2< float > Vector2f
Definition Vector2.hh:478
static const size_t IGN_ONE_SIZE_T
size_t type with a value of 1
Definition Helpers.hh:219
Definition Angle.hh:40