Ignition Math

API Reference

6.4.0
Vector3.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 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_MATH_VECTOR3_HH_
18 #define IGNITION_MATH_VECTOR3_HH_
19 
20 #include <iostream>
21 #include <fstream>
22 #include <cmath>
23 #include <algorithm>
24 
25 #include <ignition/math/Helpers.hh>
26 #include <ignition/math/config.hh>
27 
28 namespace ignition
29 {
30  namespace math
31  {
32  // Inline bracket to help doxygen filtering.
33  inline namespace IGNITION_MATH_VERSION_NAMESPACE {
34  //
39  template<typename T>
40  class Vector3
41  {
43  public: static const Vector3 Zero;
44 
46  public: static const Vector3 One;
47 
49  public: static const Vector3 UnitX;
50 
52  public: static const Vector3 UnitY;
53 
55  public: static const Vector3 UnitZ;
56 
58  public: Vector3()
59  {
60  this->data[0] = 0;
61  this->data[1] = 0;
62  this->data[2] = 0;
63  }
64 
69  public: Vector3(const T &_x, const T &_y, const T &_z)
70  {
71  this->data[0] = _x;
72  this->data[1] = _y;
73  this->data[2] = _z;
74  }
75 
78  public: Vector3(const Vector3<T> &_v)
79  {
80  this->data[0] = _v[0];
81  this->data[1] = _v[1];
82  this->data[2] = _v[2];
83  }
84 
86  public: virtual ~Vector3() {}
87 
90  public: T Sum() const
91  {
92  return this->data[0] + this->data[1] + this->data[2];
93  }
94 
98  public: T Distance(const Vector3<T> &_pt) const
99  {
100  return sqrt((this->data[0]-_pt[0])*(this->data[0]-_pt[0]) +
101  (this->data[1]-_pt[1])*(this->data[1]-_pt[1]) +
102  (this->data[2]-_pt[2])*(this->data[2]-_pt[2]));
103  }
104 
110  public: T Distance(T _x, T _y, T _z) const
111  {
112  return this->Distance(Vector3(_x, _y, _z));
113  }
114 
117  public: T Length() const
118  {
119  return sqrt(this->SquaredLength());
120  }
121 
124  public: T SquaredLength() const
125  {
126  return std::pow(this->data[0], 2)
127  + std::pow(this->data[1], 2)
128  + std::pow(this->data[2], 2);
129  }
130 
133  public: Vector3 Normalize()
134  {
135  T d = this->Length();
136 
137  if (!equal<T>(d, static_cast<T>(0.0)))
138  {
139  this->data[0] /= d;
140  this->data[1] /= d;
141  this->data[2] /= d;
142  }
143 
144  return *this;
145  }
146 
149  public: Vector3 Normalized() const
150  {
151  Vector3<T> result = *this;
152  result.Normalize();
153  return result;
154  }
155 
158  public: Vector3 Round()
159  {
160  this->data[0] = nearbyint(this->data[0]);
161  this->data[1] = nearbyint(this->data[1]);
162  this->data[2] = nearbyint(this->data[2]);
163  return *this;
164  }
165 
168  public: Vector3 Rounded() const
169  {
170  Vector3<T> result = *this;
171  result.Round();
172  return result;
173  }
174 
179  public: inline void Set(T _x = 0, T _y = 0, T _z = 0)
180  {
181  this->data[0] = _x;
182  this->data[1] = _y;
183  this->data[2] = _z;
184  }
185 
189  public: Vector3 Cross(const Vector3<T> &_v) const
190  {
191  return Vector3(this->data[1] * _v[2] - this->data[2] * _v[1],
192  this->data[2] * _v[0] - this->data[0] * _v[2],
193  this->data[0] * _v[1] - this->data[1] * _v[0]);
194  }
195 
199  public: T Dot(const Vector3<T> &_v) const
200  {
201  return this->data[0] * _v[0] +
202  this->data[1] * _v[1] +
203  this->data[2] * _v[2];
204  }
205 
214  public: T AbsDot(const Vector3<T> &_v) const
215  {
216  return std::abs(this->data[0] * _v[0]) +
217  std::abs(this->data[1] * _v[1]) +
218  std::abs(this->data[2] * _v[2]);
219  }
220 
223  public: Vector3 Abs() const
224  {
225  return Vector3(std::abs(this->data[0]),
226  std::abs(this->data[1]),
227  std::abs(this->data[2]));
228  }
229 
232  public: Vector3 Perpendicular() const
233  {
234  static const T sqrZero = 1e-06 * 1e-06;
235 
236  Vector3<T> perp = this->Cross(Vector3(1, 0, 0));
237 
238  // Check the length of the vector
239  if (perp.SquaredLength() < sqrZero)
240  {
241  perp = this->Cross(Vector3(0, 1, 0));
242  }
243 
244  return perp;
245  }
246 
252  public: static Vector3 Normal(const Vector3<T> &_v1,
253  const Vector3<T> &_v2, const Vector3<T> &_v3)
254  {
255  Vector3<T> a = _v2 - _v1;
256  Vector3<T> b = _v3 - _v1;
257  Vector3<T> n = a.Cross(b);
258  return n.Normalize();
259  }
260 
265  public: T DistToLine(const Vector3<T> &_pt1, const Vector3 &_pt2)
266  {
267  T d = ((*this) - _pt1).Cross((*this) - _pt2).Length();
268  d = d / (_pt2 - _pt1).Length();
269  return d;
270  }
271 
275  public: void Max(const Vector3<T> &_v)
276  {
277  if (_v[0] > this->data[0])
278  this->data[0] = _v[0];
279  if (_v[1] > this->data[1])
280  this->data[1] = _v[1];
281  if (_v[2] > this->data[2])
282  this->data[2] = _v[2];
283  }
284 
288  public: void Min(const Vector3<T> &_v)
289  {
290  if (_v[0] < this->data[0])
291  this->data[0] = _v[0];
292  if (_v[1] < this->data[1])
293  this->data[1] = _v[1];
294  if (_v[2] < this->data[2])
295  this->data[2] = _v[2];
296  }
297 
300  public: T Max() const
301  {
302  return std::max(std::max(this->data[0], this->data[1]), this->data[2]);
303  }
304 
307  public: T Min() const
308  {
309  return std::min(std::min(this->data[0], this->data[1]), this->data[2]);
310  }
311 
315  public: Vector3 &operator=(const Vector3<T> &_v)
316  {
317  this->data[0] = _v[0];
318  this->data[1] = _v[1];
319  this->data[2] = _v[2];
320 
321  return *this;
322  }
323 
327  public: Vector3 &operator=(T _v)
328  {
329  this->data[0] = _v;
330  this->data[1] = _v;
331  this->data[2] = _v;
332 
333  return *this;
334  }
335 
339  public: Vector3 operator+(const Vector3<T> &_v) const
340  {
341  return Vector3(this->data[0] + _v[0],
342  this->data[1] + _v[1],
343  this->data[2] + _v[2]);
344  }
345 
349  public: const Vector3 &operator+=(const Vector3<T> &_v)
350  {
351  this->data[0] += _v[0];
352  this->data[1] += _v[1];
353  this->data[2] += _v[2];
354 
355  return *this;
356  }
357 
361  public: inline Vector3<T> operator+(const T _s) const
362  {
363  return Vector3<T>(this->data[0] + _s,
364  this->data[1] + _s,
365  this->data[2] + _s);
366  }
367 
372  public: friend inline Vector3<T> operator+(const T _s,
373  const Vector3<T> &_v)
374  {
375  return {_v.X() + _s, _v.Y() + _s, _v.Z() + _s};
376  }
377 
381  public: const Vector3<T> &operator+=(const T _s)
382  {
383  this->data[0] += _s;
384  this->data[1] += _s;
385  this->data[2] += _s;
386 
387  return *this;
388  }
389 
392  public: inline Vector3 operator-() const
393  {
394  return Vector3(-this->data[0], -this->data[1], -this->data[2]);
395  }
396 
400  public: inline Vector3<T> operator-(const Vector3<T> &_pt) const
401  {
402  return Vector3(this->data[0] - _pt[0],
403  this->data[1] - _pt[1],
404  this->data[2] - _pt[2]);
405  }
406 
410  public: const Vector3<T> &operator-=(const Vector3<T> &_pt)
411  {
412  this->data[0] -= _pt[0];
413  this->data[1] -= _pt[1];
414  this->data[2] -= _pt[2];
415 
416  return *this;
417  }
418 
422  public: inline Vector3<T> operator-(const T _s) const
423  {
424  return Vector3<T>(this->data[0] - _s,
425  this->data[1] - _s,
426  this->data[2] - _s);
427  }
428 
433  public: friend inline Vector3<T> operator-(const T _s,
434  const Vector3<T> &_v)
435  {
436  return {_s - _v.X(), _s - _v.Y(), _s - _v.Z()};
437  }
438 
442  public: const Vector3<T> &operator-=(const T _s)
443  {
444  this->data[0] -= _s;
445  this->data[1] -= _s;
446  this->data[2] -= _s;
447 
448  return *this;
449  }
450 
455  public: const Vector3<T> operator/(const Vector3<T> &_pt) const
456  {
457  return Vector3(this->data[0] / _pt[0],
458  this->data[1] / _pt[1],
459  this->data[2] / _pt[2]);
460  }
461 
466  public: const Vector3<T> &operator/=(const Vector3<T> &_pt)
467  {
468  this->data[0] /= _pt[0];
469  this->data[1] /= _pt[1];
470  this->data[2] /= _pt[2];
471 
472  return *this;
473  }
474 
479  public: const Vector3<T> operator/(T _v) const
480  {
481  return Vector3(this->data[0] / _v,
482  this->data[1] / _v,
483  this->data[2] / _v);
484  }
485 
490  public: const Vector3<T> &operator/=(T _v)
491  {
492  this->data[0] /= _v;
493  this->data[1] /= _v;
494  this->data[2] /= _v;
495 
496  return *this;
497  }
498 
503  public: Vector3<T> operator*(const Vector3<T> &_p) const
504  {
505  return Vector3(this->data[0] * _p[0],
506  this->data[1] * _p[1],
507  this->data[2] * _p[2]);
508  }
509 
514  public: const Vector3<T> &operator*=(const Vector3<T> &_v)
515  {
516  this->data[0] *= _v[0];
517  this->data[1] *= _v[1];
518  this->data[2] *= _v[2];
519 
520  return *this;
521  }
522 
526  public: inline Vector3<T> operator*(T _s) const
527  {
528  return Vector3<T>(this->data[0] * _s,
529  this->data[1] * _s,
530  this->data[2] * _s);
531  }
532 
537  public: friend inline Vector3<T> operator*(T _s, const Vector3<T> &_v)
538  {
539  return {_v.X() * _s, _v.Y() * _s, _v.Z() * _s};
540  }
541 
545  public: const Vector3<T> &operator*=(T _v)
546  {
547  this->data[0] *= _v;
548  this->data[1] *= _v;
549  this->data[2] *= _v;
550 
551  return *this;
552  }
553 
559  public: bool Equal(const Vector3 &_v, const T &_tol) const
560  {
561  return equal<T>(this->data[0], _v[0], _tol)
562  && equal<T>(this->data[1], _v[1], _tol)
563  && equal<T>(this->data[2], _v[2], _tol);
564  }
565 
570  public: bool operator==(const Vector3<T> &_v) const
571  {
572  return this->Equal(_v, static_cast<T>(1e-3));
573  }
574 
579  public: bool operator!=(const Vector3<T> &_v) const
580  {
581  return !(*this == _v);
582  }
583 
586  public: bool IsFinite() const
587  {
588  // std::isfinite works with floating point values,
589  // need to explicit cast to avoid ambiguity in vc++.
590  return std::isfinite(static_cast<double>(this->data[0])) &&
591  std::isfinite(static_cast<double>(this->data[1])) &&
592  std::isfinite(static_cast<double>(this->data[2]));
593  }
594 
596  public: inline void Correct()
597  {
598  // std::isfinite works with floating point values,
599  // need to explicit cast to avoid ambiguity in vc++.
600  if (!std::isfinite(static_cast<double>(this->data[0])))
601  this->data[0] = 0;
602  if (!std::isfinite(static_cast<double>(this->data[1])))
603  this->data[1] = 0;
604  if (!std::isfinite(static_cast<double>(this->data[2])))
605  this->data[2] = 0;
606  }
607 
612  public: T &operator[](const std::size_t _index)
613  {
614  return this->data[clamp(_index, IGN_ZERO_SIZE_T, IGN_TWO_SIZE_T)];
615  }
616 
621  public: T operator[](const std::size_t _index) const
622  {
623  return this->data[clamp(_index, IGN_ZERO_SIZE_T, IGN_TWO_SIZE_T)];
624  }
625 
628  public: void Round(int _precision)
629  {
630  this->data[0] = precision(this->data[0], _precision);
631  this->data[1] = precision(this->data[1], _precision);
632  this->data[2] = precision(this->data[2], _precision);
633  }
634 
639  public: bool Equal(const Vector3<T> &_v) const
640  {
641  return equal<T>(this->data[0], _v[0]) &&
642  equal<T>(this->data[1], _v[1]) &&
643  equal<T>(this->data[2], _v[2]);
644  }
645 
648  public: inline T X() const
649  {
650  return this->data[0];
651  }
652 
655  public: inline T Y() const
656  {
657  return this->data[1];
658  }
659 
662  public: inline T Z() const
663  {
664  return this->data[2];
665  }
666 
669  public: inline T &X()
670  {
671  return this->data[0];
672  }
673 
676  public: inline T &Y()
677  {
678  return this->data[1];
679  }
680 
683  public: inline T &Z()
684  {
685  return this->data[2];
686  }
687 
690  public: inline void X(const T &_v)
691  {
692  this->data[0] = _v;
693  }
694 
697  public: inline void Y(const T &_v)
698  {
699  this->data[1] = _v;
700  }
701 
704  public: inline void Z(const T &_v)
705  {
706  this->data[2] = _v;
707  }
708 
713  public: bool operator<(const Vector3<T> &_pt) const
714  {
715  return this->data[0] < _pt[0] || this->data[1] < _pt[1] ||
716  this->data[2] < _pt[2];
717  }
718 
723  public: friend std::ostream &operator<<(
724  std::ostream &_out, const ignition::math::Vector3<T> &_pt)
725  {
726  _out << precision(_pt[0], 6) << " " << precision(_pt[1], 6) << " "
727  << precision(_pt[2], 6);
728  return _out;
729  }
730 
735  public: friend std::istream &operator>>(
737  {
738  // Skip white spaces
739  _in.setf(std::ios_base::skipws);
740  T x, y, z;
741  _in >> x >> y >> z;
742  _pt.Set(x, y, z);
743  return _in;
744  }
745 
747  private: T data[3];
748  };
749 
750  template<typename T> const Vector3<T> Vector3<T>::Zero(0, 0, 0);
751  template<typename T> const Vector3<T> Vector3<T>::One(1, 1, 1);
752  template<typename T> const Vector3<T> Vector3<T>::UnitX(1, 0, 0);
753  template<typename T> const Vector3<T> Vector3<T>::UnitY(0, 1, 0);
754  template<typename T> const Vector3<T> Vector3<T>::UnitZ(0, 0, 1);
755 
759  }
760  }
761 }
762 #endif
Vector3< T > operator*(const Vector3< T > &_p) const
Multiplication operator.
Definition: Vector3.hh:503
const Vector3 & operator+=(const Vector3< T > &_v)
Addition assignment operator.
Definition: Vector3.hh:349
void Y(const T &_v)
Set the y value.
Definition: Vector3.hh:697
T setf(T... args)
Vector3 operator+(const Vector3< T > &_v) const
Addition operator.
Definition: Vector3.hh:339
Vector3< float > Vector3f
Definition: Vector3.hh:758
Vector3 Round()
Round to near whole number, return the result.
Definition: Vector3.hh:158
void Set(T _x=0, T _y=0, T _z=0)
Set the contents of the vector.
Definition: Vector3.hh:179
friend Vector3< T > operator-(const T _s, const Vector3< T > &_v)
Subtraction operators.
Definition: Vector3.hh:433
Vector3< int > Vector3i
Definition: Vector3.hh:756
T precision(const T &_a, const unsigned int &_precision)
get value at a specified precision
Definition: Helpers.hh:589
static Vector3 Normal(const Vector3< T > &_v1, const Vector3< T > &_v2, const Vector3< T > &_v3)
Get a normal vector to a triangle.
Definition: Vector3.hh:252
static const Vector3 UnitZ
math::Vector3(0, 0, 1)
Definition: Vector3.hh:55
bool Equal(const Vector3 &_v, const T &_tol) const
Equality test with tolerance.
Definition: Vector3.hh:559
void X(const T &_v)
Set the x value.
Definition: Vector3.hh:690
Vector3< T > operator-(const Vector3< T > &_pt) const
Subtraction operators.
Definition: Vector3.hh:400
const Vector3< T > & operator-=(const T _s)
Subtraction assignment operator.
Definition: Vector3.hh:442
bool operator==(const Vector3< T > &_v) const
Equal to operator.
Definition: Vector3.hh:570
T Min() const
Get the minimum value in the vector.
Definition: Vector3.hh:307
T AbsDot(const Vector3< T > &_v) const
Return the absolute dot product of this vector and another vector. This is similar to the Dot functio...
Definition: Vector3.hh:214
T Distance(const Vector3< T > &_pt) const
Calc distance to the given point.
Definition: Vector3.hh:98
STL class.
static const Vector3 UnitY
math::Vector3(0, 1, 0)
Definition: Vector3.hh:52
T X() const
Get the x value.
Definition: Vector3.hh:648
Vector3 Perpendicular() const
Return a vector that is perpendicular to this one.
Definition: Vector3.hh:232
T Z() const
Get the z value.
Definition: Vector3.hh:662
T min(T... args)
void Z(const T &_v)
Set the z value.
Definition: Vector3.hh:704
T Y() const
Get the y value.
Definition: Vector3.hh:655
Vector3 operator-() const
Negation operator.
Definition: Vector3.hh:392
Vector3 Normalized() const
Return a normalized vector.
Definition: Vector3.hh:149
Vector3 Abs() const
Get the absolute value of the vector.
Definition: Vector3.hh:223
void Max(const Vector3< T > &_v)
Set this vector&#39;s components to the maximum of itself and the passed in vector.
Definition: Vector3.hh:275
bool operator!=(const Vector3< T > &_v) const
Not equal to operator.
Definition: Vector3.hh:579
void Correct()
Corrects any nan values.
Definition: Vector3.hh:596
T Dot(const Vector3< T > &_v) const
Return the dot product of this vector and another vector.
Definition: Vector3.hh:199
Vector3 Cross(const Vector3< T > &_v) const
Return the cross product of this vector with another vector.
Definition: Vector3.hh:189
const Vector3< T > operator/(T _v) const
Division operator.
Definition: Vector3.hh:479
Vector3 Rounded() const
Get a rounded version of this vector.
Definition: Vector3.hh:168
friend Vector3< T > operator+(const T _s, const Vector3< T > &_v)
Addition operators.
Definition: Vector3.hh:372
const Vector3< T > & operator+=(const T _s)
Addition assignment operator.
Definition: Vector3.hh:381
Vector3()
Constructor.
Definition: Vector3.hh:58
static const size_t IGN_ZERO_SIZE_T
size_t type with a value of 0
Definition: Helpers.hh:226
T isfinite(T... args)
friend std::istream & operator>>(std::istream &_in, Vector3< T > &_pt)
Stream extraction operator.
Definition: Vector3.hh:735
Vector3< T > operator-(const T _s) const
Subtraction operators.
Definition: Vector3.hh:422
T max(T... args)
const Vector3< T > operator/(const Vector3< T > &_pt) const
Division operator.
Definition: Vector3.hh:455
Vector3 Normalize()
Normalize the vector length.
Definition: Vector3.hh:133
T SquaredLength() const
Return the square of the length (magnitude) of the vector.
Definition: Vector3.hh:124
T Length() const
Returns the length (magnitude) of the vector.
Definition: Vector3.hh:117
T & X()
Get a mutable reference to the x value.
Definition: Vector3.hh:669
The Vector3 class represents the generic vector containing 3 elements. Since it&#39;s commonly used to ke...
Definition: Vector3.hh:40
Vector3< double > Vector3d
Definition: Vector3.hh:757
const Vector3< T > & operator*=(const Vector3< T > &_v)
Multiplication assignment operators.
Definition: Vector3.hh:514
static const Vector3 One
math::Vector3(1, 1, 1)
Definition: Vector3.hh:46
Vector3 & operator=(const Vector3< T > &_v)
Assignment operator.
Definition: Vector3.hh:315
T & Y()
Get a mutable reference to the y value.
Definition: Vector3.hh:676
T & operator[](const std::size_t _index)
Array subscript operator.
Definition: Vector3.hh:612
T pow(T... args)
const Vector3< T > & operator*=(T _v)
Multiplication operator.
Definition: Vector3.hh:545
Vector3(const Vector3< T > &_v)
Copy constructor.
Definition: Vector3.hh:78
static const size_t IGN_TWO_SIZE_T
size_t type with a value of 2
Definition: Helpers.hh:232
Vector3 & operator=(T _v)
Assignment operator.
Definition: Vector3.hh:327
virtual ~Vector3()
Destructor.
Definition: Vector3.hh:86
T Max() const
Get the maximum value in the vector.
Definition: Vector3.hh:300
static const Vector3 UnitX
math::Vector3(1, 0, 0)
Definition: Vector3.hh:49
Vector3(const T &_x, const T &_y, const T &_z)
Constructor.
Definition: Vector3.hh:69
void Min(const Vector3< T > &_v)
Set this vector&#39;s components to the minimum of itself and the passed in vector.
Definition: Vector3.hh:288
T operator[](const std::size_t _index) const
Const-qualified array subscript operator.
Definition: Vector3.hh:621
T Sum() const
Return the sum of the values.
Definition: Vector3.hh:90
static const Vector3 Zero
math::Vector3(0, 0, 0)
Definition: Vector3.hh:43
Definition: Angle.hh:42
const Vector3< T > & operator/=(T _v)
Division assignment operator.
Definition: Vector3.hh:490
const Vector3< T > & operator/=(const Vector3< T > &_pt)
Division assignment operator.
Definition: Vector3.hh:466
const Vector3< T > & operator-=(const Vector3< T > &_pt)
Subtraction assignment operators.
Definition: Vector3.hh:410
STL class.
T DistToLine(const Vector3< T > &_pt1, const Vector3 &_pt2)
Get distance to a line.
Definition: Vector3.hh:265
void Round(int _precision)
Round all values to _precision decimal places.
Definition: Vector3.hh:628
Vector3< T > operator+(const T _s) const
Addition operators.
Definition: Vector3.hh:361
bool Equal(const Vector3< T > &_v) const
Equality test.
Definition: Vector3.hh:639
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:405
friend std::ostream & operator<<(std::ostream &_out, const Vector3< T > &_pt)
Stream insertion operator.
Definition: Vector3.hh:723
T Distance(T _x, T _y, T _z) const
Calc distance to the given point.
Definition: Vector3.hh:110
Vector3< T > operator*(T _s) const
Multiplication operators.
Definition: Vector3.hh:526
T & Z()
Get a mutable reference to the z value.
Definition: Vector3.hh:683
bool IsFinite() const
See if a point is finite (e.g., not nan)
Definition: Vector3.hh:586
friend Vector3< T > operator*(T _s, const Vector3< T > &_v)
Multiplication operators.
Definition: Vector3.hh:537