Ignition Math

API Reference

6.9.3~pre2
Vector4.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_VECTOR4_HH_
18 #define IGNITION_MATH_VECTOR4_HH_
19 
20 #include <algorithm>
21 #include <cmath>
22 #include <limits>
23 
24 #include <ignition/math/Matrix4.hh>
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  //
37  template<typename T>
38  class Vector4
39  {
41  public: static const Vector4<T> Zero;
42 
44  public: static const Vector4<T> One;
45 
47  public: static const Vector4 NaN;
48 
50  public: Vector4()
51  {
52  this->data[0] = this->data[1] = this->data[2] = this->data[3] = 0;
53  }
54 
60  public: Vector4(const T &_x, const T &_y, const T &_z, const T &_w)
61  {
62  this->data[0] = _x;
63  this->data[1] = _y;
64  this->data[2] = _z;
65  this->data[3] = _w;
66  }
67 
70  public: Vector4(const Vector4<T> &_v)
71  {
72  this->data[0] = _v[0];
73  this->data[1] = _v[1];
74  this->data[2] = _v[2];
75  this->data[3] = _v[3];
76  }
77 
79  public: virtual ~Vector4() {}
80 
84  public: T Distance(const Vector4<T> &_pt) const
85  {
86  return static_cast<T>(sqrt(
87  (this->data[0]-_pt[0])*(this->data[0]-_pt[0]) +
88  (this->data[1]-_pt[1])*(this->data[1]-_pt[1]) +
89  (this->data[2]-_pt[2])*(this->data[2]-_pt[2]) +
90  (this->data[3]-_pt[3])*(this->data[3]-_pt[3])));
91  }
92 
99  public: T Distance(T _x, T _y, T _z, T _w) const
100  {
101  return this->Distance(Vector4(_x, _y, _z, _w));
102  }
103 
106  public: T Length() const
107  {
108  return static_cast<T>(sqrt(this->SquaredLength()));
109  }
110 
113  public: T SquaredLength() const
114  {
115  return
116  this->data[0] * this->data[0] +
117  this->data[1] * this->data[1] +
118  this->data[2] * this->data[2] +
119  this->data[3] * this->data[3];
120  }
121 
123  public: void Round()
124  {
125  this->data[0] = static_cast<T>(std::nearbyint(this->data[0]));
126  this->data[1] = static_cast<T>(std::nearbyint(this->data[1]));
127  this->data[2] = static_cast<T>(std::nearbyint(this->data[2]));
128  this->data[3] = static_cast<T>(std::nearbyint(this->data[3]));
129  }
130 
133  public: Vector4 Rounded() const
134  {
135  Vector4<T> result = *this;
136  result.Round();
137  return result;
138  }
139 
141  public: inline void Correct()
142  {
143  // std::isfinite works with floating point values,
144  // need to explicit cast to avoid ambiguity in vc++.
145  if (!std::isfinite(static_cast<double>(this->data[0])))
146  this->data[0] = 0;
147  if (!std::isfinite(static_cast<double>(this->data[1])))
148  this->data[1] = 0;
149  if (!std::isfinite(static_cast<double>(this->data[2])))
150  this->data[2] = 0;
151  if (!std::isfinite(static_cast<double>(this->data[3])))
152  this->data[3] = 0;
153  }
154 
156  public: void Normalize()
157  {
158  T d = this->Length();
159 
160  if (!equal<T>(d, static_cast<T>(0.0)))
161  {
162  this->data[0] /= d;
163  this->data[1] /= d;
164  this->data[2] /= d;
165  this->data[3] /= d;
166  }
167  }
168 
171  public: Vector4 Normalized() const
172  {
173  Vector4<T> result = *this;
174  result.Normalize();
175  return result;
176  }
177 
181  public: T Dot(const Vector4<T> &_v) const
182  {
183  return this->data[0] * _v[0] +
184  this->data[1] * _v[1] +
185  this->data[2] * _v[2] +
186  this->data[3] * _v[3];
187  }
188 
197  public: T AbsDot(const Vector4<T> &_v) const
198  {
199  return std::abs(this->data[0] * _v[0]) +
200  std::abs(this->data[1] * _v[1]) +
201  std::abs(this->data[2] * _v[2]) +
202  std::abs(this->data[3] * _v[3]);
203  }
204 
207  public: Vector4 Abs() const
208  {
209  return Vector4(std::abs(this->data[0]),
210  std::abs(this->data[1]),
211  std::abs(this->data[2]),
212  std::abs(this->data[3]));
213  }
214 
220  public: void Set(T _x = 0, T _y = 0, T _z = 0, T _w = 0)
221  {
222  this->data[0] = _x;
223  this->data[1] = _y;
224  this->data[2] = _z;
225  this->data[3] = _w;
226  }
227 
231  public: void Max(const Vector4<T> &_v)
232  {
233  this->data[0] = std::max(_v[0], this->data[0]);
234  this->data[1] = std::max(_v[1], this->data[1]);
235  this->data[2] = std::max(_v[2], this->data[2]);
236  this->data[3] = std::max(_v[3], this->data[3]);
237  }
238 
242  public: void Min(const Vector4<T> &_v)
243  {
244  this->data[0] = std::min(_v[0], this->data[0]);
245  this->data[1] = std::min(_v[1], this->data[1]);
246  this->data[2] = std::min(_v[2], this->data[2]);
247  this->data[3] = std::min(_v[3], this->data[3]);
248  }
249 
252  public: T Max() const
253  {
254  return *std::max_element(this->data, this->data+4);
255  }
256 
259  public: T Min() const
260  {
261  return *std::min_element(this->data, this->data+4);
262  }
263 
266  public: T Sum() const
267  {
268  return this->data[0] + this->data[1] + this->data[2] + this->data[3];
269  }
270 
274  public: Vector4<T> &operator=(const Vector4<T> &_v)
275  {
276  this->data[0] = _v[0];
277  this->data[1] = _v[1];
278  this->data[2] = _v[2];
279  this->data[3] = _v[3];
280 
281  return *this;
282  }
283 
286  public: Vector4<T> &operator=(T _value)
287  {
288  this->data[0] = _value;
289  this->data[1] = _value;
290  this->data[2] = _value;
291  this->data[3] = _value;
292 
293  return *this;
294  }
295 
299  public: Vector4<T> operator+(const Vector4<T> &_v) const
300  {
301  return Vector4<T>(this->data[0] + _v[0],
302  this->data[1] + _v[1],
303  this->data[2] + _v[2],
304  this->data[3] + _v[3]);
305  }
306 
310  public: const Vector4<T> &operator+=(const Vector4<T> &_v)
311  {
312  this->data[0] += _v[0];
313  this->data[1] += _v[1];
314  this->data[2] += _v[2];
315  this->data[3] += _v[3];
316 
317  return *this;
318  }
319 
323  public: inline Vector4<T> operator+(const T _s) const
324  {
325  return Vector4<T>(this->data[0] + _s,
326  this->data[1] + _s,
327  this->data[2] + _s,
328  this->data[3] + _s);
329  }
330 
335  public: friend inline Vector4<T> operator+(const T _s,
336  const Vector4<T> &_v)
337  {
338  return _v + _s;
339  }
340 
344  public: const Vector4<T> &operator+=(const T _s)
345  {
346  this->data[0] += _s;
347  this->data[1] += _s;
348  this->data[2] += _s;
349  this->data[3] += _s;
350 
351  return *this;
352  }
353 
356  public: inline Vector4 operator-() const
357  {
358  return Vector4(-this->data[0], -this->data[1],
359  -this->data[2], -this->data[3]);
360  }
361 
365  public: Vector4<T> operator-(const Vector4<T> &_v) const
366  {
367  return Vector4<T>(this->data[0] - _v[0],
368  this->data[1] - _v[1],
369  this->data[2] - _v[2],
370  this->data[3] - _v[3]);
371  }
372 
376  public: const Vector4<T> &operator-=(const Vector4<T> &_v)
377  {
378  this->data[0] -= _v[0];
379  this->data[1] -= _v[1];
380  this->data[2] -= _v[2];
381  this->data[3] -= _v[3];
382 
383  return *this;
384  }
385 
389  public: inline Vector4<T> operator-(const T _s) const
390  {
391  return Vector4<T>(this->data[0] - _s,
392  this->data[1] - _s,
393  this->data[2] - _s,
394  this->data[3] - _s);
395  }
396 
401  public: friend inline Vector4<T> operator-(const T _s,
402  const Vector4<T> &_v)
403  {
404  return {_s - _v.X(), _s - _v.Y(), _s - _v.Z(), _s - _v.W()};
405  }
406 
410  public: const Vector4<T> &operator-=(const T _s)
411  {
412  this->data[0] -= _s;
413  this->data[1] -= _s;
414  this->data[2] -= _s;
415  this->data[3] -= _s;
416 
417  return *this;
418  }
419 
425  public: const Vector4<T> operator/(const Vector4<T> &_v) const
426  {
427  return Vector4<T>(this->data[0] / _v[0],
428  this->data[1] / _v[1],
429  this->data[2] / _v[2],
430  this->data[3] / _v[3]);
431  }
432 
438  public: const Vector4<T> &operator/=(const Vector4<T> &_v)
439  {
440  this->data[0] /= _v[0];
441  this->data[1] /= _v[1];
442  this->data[2] /= _v[2];
443  this->data[3] /= _v[3];
444 
445  return *this;
446  }
447 
453  public: const Vector4<T> operator/(T _v) const
454  {
455  return Vector4<T>(this->data[0] / _v, this->data[1] / _v,
456  this->data[2] / _v, this->data[3] / _v);
457  }
458 
462  public: const Vector4<T> &operator/=(T _v)
463  {
464  this->data[0] /= _v;
465  this->data[1] /= _v;
466  this->data[2] /= _v;
467  this->data[3] /= _v;
468 
469  return *this;
470  }
471 
477  public: const Vector4<T> operator*(const Vector4<T> &_pt) const
478  {
479  return Vector4<T>(this->data[0] * _pt[0],
480  this->data[1] * _pt[1],
481  this->data[2] * _pt[2],
482  this->data[3] * _pt[3]);
483  }
484 
488  public: const Vector4<T> operator*(const Matrix4<T> &_m) const
489  {
490  return Vector4<T>(
491  this->data[0]*_m(0, 0) + this->data[1]*_m(1, 0) +
492  this->data[2]*_m(2, 0) + this->data[3]*_m(3, 0),
493  this->data[0]*_m(0, 1) + this->data[1]*_m(1, 1) +
494  this->data[2]*_m(2, 1) + this->data[3]*_m(3, 1),
495  this->data[0]*_m(0, 2) + this->data[1]*_m(1, 2) +
496  this->data[2]*_m(2, 2) + this->data[3]*_m(3, 2),
497  this->data[0]*_m(0, 3) + this->data[1]*_m(1, 3) +
498  this->data[2]*_m(2, 3) + this->data[3]*_m(3, 3));
499  }
500 
506  public: const Vector4<T> &operator*=(const Vector4<T> &_pt)
507  {
508  this->data[0] *= _pt[0];
509  this->data[1] *= _pt[1];
510  this->data[2] *= _pt[2];
511  this->data[3] *= _pt[3];
512 
513  return *this;
514  }
515 
519  public: const Vector4<T> operator*(T _v) const
520  {
521  return Vector4<T>(this->data[0] * _v, this->data[1] * _v,
522  this->data[2] * _v, this->data[3] * _v);
523  }
524 
529  public: friend inline const Vector4 operator*(const T _s,
530  const Vector4 &_v)
531  {
532  return Vector4(_v * _s);
533  }
534 
538  public: const Vector4<T> &operator*=(T _v)
539  {
540  this->data[0] *= _v;
541  this->data[1] *= _v;
542  this->data[2] *= _v;
543  this->data[3] *= _v;
544 
545  return *this;
546  }
547 
553  public: bool Equal(const Vector4 &_v, const T &_tol) const
554  {
555  return equal<T>(this->data[0], _v[0], _tol)
556  && equal<T>(this->data[1], _v[1], _tol)
557  && equal<T>(this->data[2], _v[2], _tol)
558  && equal<T>(this->data[3], _v[3], _tol);
559  }
560 
565  public: bool operator==(const Vector4<T> &_v) const
566  {
567  return this->Equal(_v, static_cast<T>(1e-6));
568  }
569 
574  public: bool operator!=(const Vector4<T> &_pt) const
575  {
576  return !(*this == _pt);
577  }
578 
581  public: bool IsFinite() const
582  {
583  // std::isfinite works with floating point values,
584  // need to explicit cast to avoid ambiguity in vc++.
585  return std::isfinite(static_cast<double>(this->data[0])) &&
586  std::isfinite(static_cast<double>(this->data[1])) &&
587  std::isfinite(static_cast<double>(this->data[2])) &&
588  std::isfinite(static_cast<double>(this->data[3]));
589  }
590 
595  public: T &operator[](const std::size_t _index)
596  {
597  return this->data[clamp(_index, IGN_ZERO_SIZE_T, IGN_THREE_SIZE_T)];
598  }
599 
604  public: T operator[](const std::size_t _index) const
605  {
606  return this->data[clamp(_index, IGN_ZERO_SIZE_T, IGN_THREE_SIZE_T)];
607  }
608 
611  public: T &X()
612  {
613  return this->data[0];
614  }
615 
618  public: T &Y()
619  {
620  return this->data[1];
621  }
622 
625  public: T &Z()
626  {
627  return this->data[2];
628  }
629 
632  public: T &W()
633  {
634  return this->data[3];
635  }
636 
639  public: T X() const
640  {
641  return this->data[0];
642  }
643 
646  public: T Y() const
647  {
648  return this->data[1];
649  }
650 
653  public: T Z() const
654  {
655  return this->data[2];
656  }
657 
660  public: T W() const
661  {
662  return this->data[3];
663  }
664 
667  public: inline void X(const T &_v)
668  {
669  this->data[0] = _v;
670  }
671 
674  public: inline void Y(const T &_v)
675  {
676  this->data[1] = _v;
677  }
678 
681  public: inline void Z(const T &_v)
682  {
683  this->data[2] = _v;
684  }
685 
688  public: inline void W(const T &_v)
689  {
690  this->data[3] = _v;
691  }
692 
697  public: bool operator<(const Vector4<T> &_pt) const
698  {
699  return this->data[0] < _pt[0] || this->data[1] < _pt[1] ||
700  this->data[2] < _pt[2] || this->data[3] < _pt[3];
701  }
702 
707  public: friend std::ostream &operator<<(
708  std::ostream &_out, const ignition::math::Vector4<T> &_pt)
709  {
710  _out << _pt[0] << " " << _pt[1] << " " << _pt[2] << " " << _pt[3];
711  return _out;
712  }
713 
718  public: friend std::istream &operator>>(
720  {
721  T x, y, z, w;
722 
723  // Skip white spaces
724  _in.setf(std::ios_base::skipws);
725  _in >> x >> y >> z >> w;
726  if (!_in.fail())
727  {
728  _pt.Set(x, y, z, w);
729  }
730  return _in;
731  }
732 
734  private: T data[4];
735  };
736 
737  template<typename T>
738  const Vector4<T> Vector4<T>::Zero(0, 0, 0, 0);
739 
740  template<typename T>
741  const Vector4<T> Vector4<T>::One(1, 1, 1, 1);
742 
743  template<typename T> const Vector4<T> Vector4<T>::NaN(
748 
752  }
753  }
754 }
755 #endif
friend Vector4< T > operator+(const T _s, const Vector4< T > &_v)
Addition operators.
Definition: Vector4.hh:335
const Vector4< T > operator*(T _v) const
Multiplication operators.
Definition: Vector4.hh:519
T setf(T... args)
static const Vector4< T > Zero
math::Vector4(0, 0, 0, 0)
Definition: Vector4.hh:41
const Vector4< T > operator/(T _v) const
Division assignment operator.
Definition: Vector4.hh:453
T Dot(const Vector4< T > &_v) const
Return the dot product of this vector and another vector.
Definition: Vector4.hh:181
T & Z()
Return a mutable z value.
Definition: Vector4.hh:625
Vector4< T > operator-(const T _s) const
Subtraction operators.
Definition: Vector4.hh:389
T & W()
Return a mutable w value.
Definition: Vector4.hh:632
void Round()
Round to near whole number.
Definition: Vector4.hh:123
bool operator!=(const Vector4< T > &_pt) const
Not equal to operator.
Definition: Vector4.hh:574
Vector4< T > operator+(const T _s) const
Addition operators.
Definition: Vector4.hh:323
T fail(T... args)
bool operator==(const Vector4< T > &_v) const
Equal to operator.
Definition: Vector4.hh:565
friend std::istream & operator>>(std::istream &_in, Vector4< T > &_pt)
Stream extraction operator.
Definition: Vector4.hh:718
T max_element(T... args)
const Vector4< T > & operator/=(const Vector4< T > &_v)
Division assignment operator.
Definition: Vector4.hh:438
friend const Vector4 operator*(const T _s, const Vector4 &_v)
Scalar left multiplication operators.
Definition: Vector4.hh:529
void Y(const T &_v)
Set the y value.
Definition: Vector4.hh:674
friend std::ostream & operator<<(std::ostream &_out, const Vector4< T > &_pt)
Stream insertion operator.
Definition: Vector4.hh:707
A 4x4 matrix class.
Definition: Matrix4.hh:38
Vector4< T > & operator=(T _value)
Assignment operator.
Definition: Vector4.hh:286
T Distance(T _x, T _y, T _z, T _w) const
Calc distance to the given point.
Definition: Vector4.hh:99
T Z() const
Get the z value.
Definition: Vector4.hh:653
STL class.
void X(const T &_v)
Set the x value.
Definition: Vector4.hh:667
void W(const T &_v)
Set the w value.
Definition: Vector4.hh:688
const Vector4< T > & operator-=(const T _s)
Subtraction assignment operator.
Definition: Vector4.hh:410
T Y() const
Get the y value.
Definition: Vector4.hh:646
T min(T... args)
void Min(const Vector4< T > &_v)
Set this vector&#39;s components to the minimum of itself and the passed in vector.
Definition: Vector4.hh:242
Vector4< float > Vector4f
Definition: Vector4.hh:751
static const Vector4< T > One
math::Vector4(1, 1, 1, 1)
Definition: Vector4.hh:44
T operator[](const std::size_t _index) const
Const-qualified array subscript operator.
Definition: Vector4.hh:604
Vector4< T > & operator=(const Vector4< T > &_v)
Assignment operator.
Definition: Vector4.hh:274
Vector4()
Constructor.
Definition: Vector4.hh:50
const Vector4< T > & operator/=(T _v)
Division operator.
Definition: Vector4.hh:462
T W() const
Get the w value.
Definition: Vector4.hh:660
static const size_t IGN_ZERO_SIZE_T
size_t type with a value of 0
Definition: Helpers.hh:227
T isfinite(T... args)
const Vector4< T > operator*(const Matrix4< T > &_m) const
Matrix multiplication operator.
Definition: Vector4.hh:488
Vector4 Rounded() const
Get a rounded version of this vector.
Definition: Vector4.hh:133
T max(T... args)
virtual ~Vector4()
Destructor.
Definition: Vector4.hh:79
T Length() const
Returns the length (magnitude) of the vector.
Definition: Vector4.hh:106
Vector4(const Vector4< T > &_v)
Copy constructor.
Definition: Vector4.hh:70
void Normalize()
Normalize the vector length.
Definition: Vector4.hh:156
Vector4 Abs() const
Get the absolute value of the vector.
Definition: Vector4.hh:207
void Correct()
Corrects any nan values.
Definition: Vector4.hh:141
T & X()
Return a mutable x value.
Definition: Vector4.hh:611
const Vector4< T > & operator*=(const Vector4< T > &_pt)
Multiplication assignment operator.
Definition: Vector4.hh:506
const Vector4< T > operator/(const Vector4< T > &_v) const
Division assignment operator.
Definition: Vector4.hh:425
T & Y()
Return a mutable y value.
Definition: Vector4.hh:618
const Vector4< T > & operator-=(const Vector4< T > &_v)
Subtraction assigment operators.
Definition: Vector4.hh:376
Vector4< int > Vector4i
Definition: Vector4.hh:749
const Vector4< T > & operator*=(T _v)
Multiplication assignment operator.
Definition: Vector4.hh:538
Vector4 Normalized() const
Return a normalized vector.
Definition: Vector4.hh:171
T & operator[](const std::size_t _index)
Array subscript operator.
Definition: Vector4.hh:595
T SquaredLength() const
Return the square of the length (magnitude) of the vector.
Definition: Vector4.hh:113
Vector4< T > operator-(const Vector4< T > &_v) const
Subtraction operator.
Definition: Vector4.hh:365
bool Equal(const Vector4 &_v, const T &_tol) const
Equality test with tolerance.
Definition: Vector4.hh:553
T Sum() const
Return the sum of the values.
Definition: Vector4.hh:266
const Vector4< T > & operator+=(const T _s)
Addition assignment operator.
Definition: Vector4.hh:344
T Max() const
Get the maximum value in the vector.
Definition: Vector4.hh:252
static const size_t IGN_THREE_SIZE_T
size_t type with a value of 3
Definition: Helpers.hh:236
bool IsFinite() const
See if a point is finite (e.g., not nan)
Definition: Vector4.hh:581
static const Vector4 NaN
math::Vector4(NaN, NaN, NaN, NaN)
Definition: Vector4.hh:47
Definition: Angle.hh:42
T min_element(T... args)
T Min() const
Get the minimum value in the vector.
Definition: Vector4.hh:259
friend Vector4< T > operator-(const T _s, const Vector4< T > &_v)
Subtraction operators.
Definition: Vector4.hh:401
Vector4< T > operator+(const Vector4< T > &_v) const
Addition operator.
Definition: Vector4.hh:299
STL class.
const Vector4< T > & operator+=(const Vector4< T > &_v)
Addition operator.
Definition: Vector4.hh:310
T Generic x, y, z, w vector.
Definition: Vector4.hh:38
Vector4< double > Vector4d
Definition: Vector4.hh:750
T nearbyint(T... args)
void Max(const Vector4< T > &_v)
Set this vector&#39;s components to the maximum of itself and the passed in vector.
Definition: Vector4.hh:231
const Vector4< T > operator*(const Vector4< T > &_pt) const
Multiplication operator.
Definition: Vector4.hh:477
void Set(T _x=0, T _y=0, T _z=0, T _w=0)
Set the contents of the vector.
Definition: Vector4.hh:220
T X() const
Get the x value.
Definition: Vector4.hh:639
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:406
T Distance(const Vector4< T > &_pt) const
Calc distance to the given point.
Definition: Vector4.hh:84
Vector4(const T &_x, const T &_y, const T &_z, const T &_w)
Constructor with component values.
Definition: Vector4.hh:60
T AbsDot(const Vector4< T > &_v) const
Return the absolute dot product of this vector and another vector. This is similar to the Dot functio...
Definition: Vector4.hh:197
Vector4 operator-() const
Negation operator.
Definition: Vector4.hh:356
void Z(const T &_v)
Set the z value.
Definition: Vector4.hh:681