Ignition Math

API Reference

6.4.0
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 
22 #include <ignition/math/Matrix4.hh>
23 #include <ignition/math/Helpers.hh>
24 #include <ignition/math/config.hh>
25 
26 namespace ignition
27 {
28  namespace math
29  {
30  // Inline bracket to help doxygen filtering.
31  inline namespace IGNITION_MATH_VERSION_NAMESPACE {
32  //
35  template<typename T>
36  class Vector4
37  {
39  public: static const Vector4<T> Zero;
40 
42  public: static const Vector4<T> One;
43 
45  public: Vector4()
46  {
47  this->data[0] = this->data[1] = this->data[2] = this->data[3] = 0;
48  }
49 
55  public: Vector4(const T &_x, const T &_y, const T &_z, const T &_w)
56  {
57  this->data[0] = _x;
58  this->data[1] = _y;
59  this->data[2] = _z;
60  this->data[3] = _w;
61  }
62 
65  public: Vector4(const Vector4<T> &_v)
66  {
67  this->data[0] = _v[0];
68  this->data[1] = _v[1];
69  this->data[2] = _v[2];
70  this->data[3] = _v[3];
71  }
72 
74  public: virtual ~Vector4() {}
75 
79  public: T Distance(const Vector4<T> &_pt) const
80  {
81  return sqrt((this->data[0]-_pt[0])*(this->data[0]-_pt[0]) +
82  (this->data[1]-_pt[1])*(this->data[1]-_pt[1]) +
83  (this->data[2]-_pt[2])*(this->data[2]-_pt[2]) +
84  (this->data[3]-_pt[3])*(this->data[3]-_pt[3]));
85  }
86 
93  public: T Distance(T _x, T _y, T _z, T _w) const
94  {
95  return this->Distance(Vector4(_x, _y, _z, _w));
96  }
97 
100  public: T Length() const
101  {
102  return sqrt(this->SquaredLength());
103  }
104 
107  public: T SquaredLength() const
108  {
109  return std::pow(this->data[0], 2)
110  + std::pow(this->data[1], 2)
111  + std::pow(this->data[2], 2)
112  + std::pow(this->data[3], 2);
113  }
114 
116  public: void Round()
117  {
118  this->data[0] = nearbyint(this->data[0]);
119  this->data[1] = nearbyint(this->data[1]);
120  this->data[2] = nearbyint(this->data[2]);
121  this->data[3] = nearbyint(this->data[3]);
122  }
123 
126  public: Vector4 Rounded() const
127  {
128  Vector4<T> result = *this;
129  result.Round();
130  return result;
131  }
132 
134  public: inline void Correct()
135  {
136  // std::isfinite works with floating point values,
137  // need to explicit cast to avoid ambiguity in vc++.
138  if (!std::isfinite(static_cast<double>(this->data[0])))
139  this->data[0] = 0;
140  if (!std::isfinite(static_cast<double>(this->data[1])))
141  this->data[1] = 0;
142  if (!std::isfinite(static_cast<double>(this->data[2])))
143  this->data[2] = 0;
144  if (!std::isfinite(static_cast<double>(this->data[3])))
145  this->data[3] = 0;
146  }
147 
149  public: void Normalize()
150  {
151  T d = this->Length();
152 
153  if (!equal<T>(d, static_cast<T>(0.0)))
154  {
155  this->data[0] /= d;
156  this->data[1] /= d;
157  this->data[2] /= d;
158  this->data[3] /= d;
159  }
160  }
161 
164  public: Vector4 Normalized() const
165  {
166  Vector4<T> result = *this;
167  result.Normalize();
168  return result;
169  }
170 
174  public: T Dot(const Vector4<T> &_v) const
175  {
176  return this->data[0] * _v[0] +
177  this->data[1] * _v[1] +
178  this->data[2] * _v[2] +
179  this->data[3] * _v[3];
180  }
181 
190  public: T AbsDot(const Vector4<T> &_v) const
191  {
192  return std::abs(this->data[0] * _v[0]) +
193  std::abs(this->data[1] * _v[1]) +
194  std::abs(this->data[2] * _v[2]) +
195  std::abs(this->data[3] * _v[3]);
196  }
197 
200  public: Vector4 Abs() const
201  {
202  return Vector4(std::abs(this->data[0]),
203  std::abs(this->data[1]),
204  std::abs(this->data[2]),
205  std::abs(this->data[3]));
206  }
207 
213  public: void Set(T _x = 0, T _y = 0, T _z = 0, T _w = 0)
214  {
215  this->data[0] = _x;
216  this->data[1] = _y;
217  this->data[2] = _z;
218  this->data[3] = _w;
219  }
220 
224  public: void Max(const Vector4<T> &_v)
225  {
226  this->data[0] = std::max(_v[0], this->data[0]);
227  this->data[1] = std::max(_v[1], this->data[1]);
228  this->data[2] = std::max(_v[2], this->data[2]);
229  this->data[3] = std::max(_v[3], this->data[3]);
230  }
231 
235  public: void Min(const Vector4<T> &_v)
236  {
237  this->data[0] = std::min(_v[0], this->data[0]);
238  this->data[1] = std::min(_v[1], this->data[1]);
239  this->data[2] = std::min(_v[2], this->data[2]);
240  this->data[3] = std::min(_v[3], this->data[3]);
241  }
242 
245  public: T Max() const
246  {
247  return *std::max_element(this->data, this->data+4);
248  }
249 
252  public: T Min() const
253  {
254  return *std::min_element(this->data, this->data+4);
255  }
256 
259  public: T Sum() const
260  {
261  return this->data[0] + this->data[1] + this->data[2] + this->data[3];
262  }
263 
267  public: Vector4<T> &operator=(const Vector4<T> &_v)
268  {
269  this->data[0] = _v[0];
270  this->data[1] = _v[1];
271  this->data[2] = _v[2];
272  this->data[3] = _v[3];
273 
274  return *this;
275  }
276 
279  public: Vector4<T> &operator=(T _value)
280  {
281  this->data[0] = _value;
282  this->data[1] = _value;
283  this->data[2] = _value;
284  this->data[3] = _value;
285 
286  return *this;
287  }
288 
292  public: Vector4<T> operator+(const Vector4<T> &_v) const
293  {
294  return Vector4<T>(this->data[0] + _v[0],
295  this->data[1] + _v[1],
296  this->data[2] + _v[2],
297  this->data[3] + _v[3]);
298  }
299 
303  public: const Vector4<T> &operator+=(const Vector4<T> &_v)
304  {
305  this->data[0] += _v[0];
306  this->data[1] += _v[1];
307  this->data[2] += _v[2];
308  this->data[3] += _v[3];
309 
310  return *this;
311  }
312 
316  public: inline Vector4<T> operator+(const T _s) const
317  {
318  return Vector4<T>(this->data[0] + _s,
319  this->data[1] + _s,
320  this->data[2] + _s,
321  this->data[3] + _s);
322  }
323 
328  public: friend inline Vector4<T> operator+(const T _s,
329  const Vector4<T> &_v)
330  {
331  return _v + _s;
332  }
333 
337  public: const Vector4<T> &operator+=(const T _s)
338  {
339  this->data[0] += _s;
340  this->data[1] += _s;
341  this->data[2] += _s;
342  this->data[3] += _s;
343 
344  return *this;
345  }
346 
349  public: inline Vector4 operator-() const
350  {
351  return Vector4(-this->data[0], -this->data[1],
352  -this->data[2], -this->data[3]);
353  }
354 
358  public: Vector4<T> operator-(const Vector4<T> &_v) const
359  {
360  return Vector4<T>(this->data[0] - _v[0],
361  this->data[1] - _v[1],
362  this->data[2] - _v[2],
363  this->data[3] - _v[3]);
364  }
365 
369  public: const Vector4<T> &operator-=(const Vector4<T> &_v)
370  {
371  this->data[0] -= _v[0];
372  this->data[1] -= _v[1];
373  this->data[2] -= _v[2];
374  this->data[3] -= _v[3];
375 
376  return *this;
377  }
378 
382  public: inline Vector4<T> operator-(const T _s) const
383  {
384  return Vector4<T>(this->data[0] - _s,
385  this->data[1] - _s,
386  this->data[2] - _s,
387  this->data[3] - _s);
388  }
389 
394  public: friend inline Vector4<T> operator-(const T _s,
395  const Vector4<T> &_v)
396  {
397  return {_s - _v.X(), _s - _v.Y(), _s - _v.Z(), _s - _v.W()};
398  }
399 
403  public: const Vector4<T> &operator-=(const T _s)
404  {
405  this->data[0] -= _s;
406  this->data[1] -= _s;
407  this->data[2] -= _s;
408  this->data[3] -= _s;
409 
410  return *this;
411  }
412 
418  public: const Vector4<T> operator/(const Vector4<T> &_v) const
419  {
420  return Vector4<T>(this->data[0] / _v[0],
421  this->data[1] / _v[1],
422  this->data[2] / _v[2],
423  this->data[3] / _v[3]);
424  }
425 
431  public: const Vector4<T> &operator/=(const Vector4<T> &_v)
432  {
433  this->data[0] /= _v[0];
434  this->data[1] /= _v[1];
435  this->data[2] /= _v[2];
436  this->data[3] /= _v[3];
437 
438  return *this;
439  }
440 
446  public: const Vector4<T> operator/(T _v) const
447  {
448  return Vector4<T>(this->data[0] / _v, this->data[1] / _v,
449  this->data[2] / _v, this->data[3] / _v);
450  }
451 
455  public: const Vector4<T> &operator/=(T _v)
456  {
457  this->data[0] /= _v;
458  this->data[1] /= _v;
459  this->data[2] /= _v;
460  this->data[3] /= _v;
461 
462  return *this;
463  }
464 
470  public: const Vector4<T> operator*(const Vector4<T> &_pt) const
471  {
472  return Vector4<T>(this->data[0] * _pt[0],
473  this->data[1] * _pt[1],
474  this->data[2] * _pt[2],
475  this->data[3] * _pt[3]);
476  }
477 
481  public: const Vector4<T> operator*(const Matrix4<T> &_m) const
482  {
483  return Vector4<T>(
484  this->data[0]*_m(0, 0) + this->data[1]*_m(1, 0) +
485  this->data[2]*_m(2, 0) + this->data[3]*_m(3, 0),
486  this->data[0]*_m(0, 1) + this->data[1]*_m(1, 1) +
487  this->data[2]*_m(2, 1) + this->data[3]*_m(3, 1),
488  this->data[0]*_m(0, 2) + this->data[1]*_m(1, 2) +
489  this->data[2]*_m(2, 2) + this->data[3]*_m(3, 2),
490  this->data[0]*_m(0, 3) + this->data[1]*_m(1, 3) +
491  this->data[2]*_m(2, 3) + this->data[3]*_m(3, 3));
492  }
493 
499  public: const Vector4<T> &operator*=(const Vector4<T> &_pt)
500  {
501  this->data[0] *= _pt[0];
502  this->data[1] *= _pt[1];
503  this->data[2] *= _pt[2];
504  this->data[3] *= _pt[3];
505 
506  return *this;
507  }
508 
512  public: const Vector4<T> operator*(T _v) const
513  {
514  return Vector4<T>(this->data[0] * _v, this->data[1] * _v,
515  this->data[2] * _v, this->data[3] * _v);
516  }
517 
522  public: friend inline const Vector4 operator*(const T _s,
523  const Vector4 &_v)
524  {
525  return Vector4(_v * _s);
526  }
527 
531  public: const Vector4<T> &operator*=(T _v)
532  {
533  this->data[0] *= _v;
534  this->data[1] *= _v;
535  this->data[2] *= _v;
536  this->data[3] *= _v;
537 
538  return *this;
539  }
540 
546  public: bool Equal(const Vector4 &_v, const T &_tol) const
547  {
548  return equal<T>(this->data[0], _v[0], _tol)
549  && equal<T>(this->data[1], _v[1], _tol)
550  && equal<T>(this->data[2], _v[2], _tol)
551  && equal<T>(this->data[3], _v[3], _tol);
552  }
553 
558  public: bool operator==(const Vector4<T> &_v) const
559  {
560  return this->Equal(_v, static_cast<T>(1e-6));
561  }
562 
567  public: bool operator!=(const Vector4<T> &_pt) const
568  {
569  return !(*this == _pt);
570  }
571 
574  public: bool IsFinite() const
575  {
576  // std::isfinite works with floating point values,
577  // need to explicit cast to avoid ambiguity in vc++.
578  return std::isfinite(static_cast<double>(this->data[0])) &&
579  std::isfinite(static_cast<double>(this->data[1])) &&
580  std::isfinite(static_cast<double>(this->data[2])) &&
581  std::isfinite(static_cast<double>(this->data[3]));
582  }
583 
588  public: T &operator[](const std::size_t _index)
589  {
590  return this->data[clamp(_index, IGN_ZERO_SIZE_T, IGN_THREE_SIZE_T)];
591  }
592 
597  public: T operator[](const std::size_t _index) const
598  {
599  return this->data[clamp(_index, IGN_ZERO_SIZE_T, IGN_THREE_SIZE_T)];
600  }
601 
604  public: T &X()
605  {
606  return this->data[0];
607  }
608 
611  public: T &Y()
612  {
613  return this->data[1];
614  }
615 
618  public: T &Z()
619  {
620  return this->data[2];
621  }
622 
625  public: T &W()
626  {
627  return this->data[3];
628  }
629 
632  public: T X() const
633  {
634  return this->data[0];
635  }
636 
639  public: T Y() const
640  {
641  return this->data[1];
642  }
643 
646  public: T Z() const
647  {
648  return this->data[2];
649  }
650 
653  public: T W() const
654  {
655  return this->data[3];
656  }
657 
660  public: inline void X(const T &_v)
661  {
662  this->data[0] = _v;
663  }
664 
667  public: inline void Y(const T &_v)
668  {
669  this->data[1] = _v;
670  }
671 
674  public: inline void Z(const T &_v)
675  {
676  this->data[2] = _v;
677  }
678 
681  public: inline void W(const T &_v)
682  {
683  this->data[3] = _v;
684  }
685 
690  public: bool operator<(const Vector4<T> &_pt) const
691  {
692  return this->data[0] < _pt[0] || this->data[1] < _pt[1] ||
693  this->data[2] < _pt[2] || this->data[3] < _pt[3];
694  }
695 
700  public: friend std::ostream &operator<<(
701  std::ostream &_out, const ignition::math::Vector4<T> &_pt)
702  {
703  _out << _pt[0] << " " << _pt[1] << " " << _pt[2] << " " << _pt[3];
704  return _out;
705  }
706 
711  public: friend std::istream &operator>>(
713  {
714  T x, y, z, w;
715 
716  // Skip white spaces
717  _in.setf(std::ios_base::skipws);
718  _in >> x >> y >> z >> w;
719  _pt.Set(x, y, z, w);
720  return _in;
721  }
722 
724  private: T data[4];
725  };
726 
727  template<typename T>
728  const Vector4<T> Vector4<T>::Zero(0, 0, 0, 0);
729 
730  template<typename T>
731  const Vector4<T> Vector4<T>::One(1, 1, 1, 1);
732 
736  }
737  }
738 }
739 #endif
friend Vector4< T > operator+(const T _s, const Vector4< T > &_v)
Addition operators.
Definition: Vector4.hh:328
const Vector4< T > operator*(T _v) const
Multiplication operators.
Definition: Vector4.hh:512
T setf(T... args)
static const Vector4< T > Zero
math::Vector4(0, 0, 0, 0)
Definition: Vector4.hh:39
const Vector4< T > operator/(T _v) const
Division assignment operator.
Definition: Vector4.hh:446
T Dot(const Vector4< T > &_v) const
Return the dot product of this vector and another vector.
Definition: Vector4.hh:174
T & Z()
Return a mutable z value.
Definition: Vector4.hh:618
Vector4< T > operator-(const T _s) const
Subtraction operators.
Definition: Vector4.hh:382
T & W()
Return a mutable w value.
Definition: Vector4.hh:625
void Round()
Round to near whole number.
Definition: Vector4.hh:116
bool operator!=(const Vector4< T > &_pt) const
Not equal to operator.
Definition: Vector4.hh:567
Vector4< T > operator+(const T _s) const
Addition operators.
Definition: Vector4.hh:316
bool operator==(const Vector4< T > &_v) const
Equal to operator.
Definition: Vector4.hh:558
friend std::istream & operator>>(std::istream &_in, Vector4< T > &_pt)
Stream extraction operator.
Definition: Vector4.hh:711
T max_element(T... args)
const Vector4< T > & operator/=(const Vector4< T > &_v)
Division assignment operator.
Definition: Vector4.hh:431
friend const Vector4 operator*(const T _s, const Vector4 &_v)
Scalar left multiplication operators.
Definition: Vector4.hh:522
void Y(const T &_v)
Set the y value.
Definition: Vector4.hh:667
friend std::ostream & operator<<(std::ostream &_out, const Vector4< T > &_pt)
Stream insertion operator.
Definition: Vector4.hh:700
A 4x4 matrix class.
Definition: Matrix4.hh:38
Vector4< T > & operator=(T _value)
Assignment operator.
Definition: Vector4.hh:279
T Distance(T _x, T _y, T _z, T _w) const
Calc distance to the given point.
Definition: Vector4.hh:93
T Z() const
Get the z value.
Definition: Vector4.hh:646
STL class.
void X(const T &_v)
Set the x value.
Definition: Vector4.hh:660
void W(const T &_v)
Set the w value.
Definition: Vector4.hh:681
const Vector4< T > & operator-=(const T _s)
Subtraction assignment operator.
Definition: Vector4.hh:403
T Y() const
Get the y value.
Definition: Vector4.hh:639
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:235
Vector4< float > Vector4f
Definition: Vector4.hh:735
static const Vector4< T > One
math::Vector4(1, 1, 1, 1)
Definition: Vector4.hh:42
T operator[](const std::size_t _index) const
Const-qualified array subscript operator.
Definition: Vector4.hh:597
Vector4< T > & operator=(const Vector4< T > &_v)
Assignment operator.
Definition: Vector4.hh:267
Vector4()
Constructor.
Definition: Vector4.hh:45
const Vector4< T > & operator/=(T _v)
Division operator.
Definition: Vector4.hh:455
T W() const
Get the w value.
Definition: Vector4.hh:653
static const size_t IGN_ZERO_SIZE_T
size_t type with a value of 0
Definition: Helpers.hh:226
T isfinite(T... args)
const Vector4< T > operator*(const Matrix4< T > &_m) const
Matrix multiplication operator.
Definition: Vector4.hh:481
Vector4 Rounded() const
Get a rounded version of this vector.
Definition: Vector4.hh:126
T max(T... args)
virtual ~Vector4()
Destructor.
Definition: Vector4.hh:74
T Length() const
Returns the length (magnitude) of the vector.
Definition: Vector4.hh:100
Vector4(const Vector4< T > &_v)
Copy constructor.
Definition: Vector4.hh:65
void Normalize()
Normalize the vector length.
Definition: Vector4.hh:149
Vector4 Abs() const
Get the absolute value of the vector.
Definition: Vector4.hh:200
void Correct()
Corrects any nan values.
Definition: Vector4.hh:134
T & X()
Return a mutable x value.
Definition: Vector4.hh:604
const Vector4< T > & operator*=(const Vector4< T > &_pt)
Multiplication assignment operator.
Definition: Vector4.hh:499
const Vector4< T > operator/(const Vector4< T > &_v) const
Division assignment operator.
Definition: Vector4.hh:418
T & Y()
Return a mutable y value.
Definition: Vector4.hh:611
const Vector4< T > & operator-=(const Vector4< T > &_v)
Subtraction assigment operators.
Definition: Vector4.hh:369
T pow(T... args)
Vector4< int > Vector4i
Definition: Vector4.hh:733
const Vector4< T > & operator*=(T _v)
Multiplication assignment operator.
Definition: Vector4.hh:531
Vector4 Normalized() const
Return a normalized vector.
Definition: Vector4.hh:164
T & operator[](const std::size_t _index)
Array subscript operator.
Definition: Vector4.hh:588
T SquaredLength() const
Return the square of the length (magnitude) of the vector.
Definition: Vector4.hh:107
Vector4< T > operator-(const Vector4< T > &_v) const
Subtraction operator.
Definition: Vector4.hh:358
bool Equal(const Vector4 &_v, const T &_tol) const
Equality test with tolerance.
Definition: Vector4.hh:546
T Sum() const
Return the sum of the values.
Definition: Vector4.hh:259
const Vector4< T > & operator+=(const T _s)
Addition assignment operator.
Definition: Vector4.hh:337
T Max() const
Get the maximum value in the vector.
Definition: Vector4.hh:245
static const size_t IGN_THREE_SIZE_T
size_t type with a value of 3
Definition: Helpers.hh:235
bool IsFinite() const
See if a point is finite (e.g., not nan)
Definition: Vector4.hh:574
Definition: Angle.hh:42
T min_element(T... args)
T Min() const
Get the minimum value in the vector.
Definition: Vector4.hh:252
friend Vector4< T > operator-(const T _s, const Vector4< T > &_v)
Subtraction operators.
Definition: Vector4.hh:394
Vector4< T > operator+(const Vector4< T > &_v) const
Addition operator.
Definition: Vector4.hh:292
STL class.
const Vector4< T > & operator+=(const Vector4< T > &_v)
Addition operator.
Definition: Vector4.hh:303
T Generic x, y, z, w vector.
Definition: Vector4.hh:36
Vector4< double > Vector4d
Definition: Vector4.hh:734
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:224
const Vector4< T > operator*(const Vector4< T > &_pt) const
Multiplication operator.
Definition: Vector4.hh:470
void Set(T _x=0, T _y=0, T _z=0, T _w=0)
Set the contents of the vector.
Definition: Vector4.hh:213
T X() const
Get the x value.
Definition: Vector4.hh:632
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:405
T Distance(const Vector4< T > &_pt) const
Calc distance to the given point.
Definition: Vector4.hh:79
Vector4(const T &_x, const T &_y, const T &_z, const T &_w)
Constructor with component values.
Definition: Vector4.hh:55
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:190
Vector4 operator-() const
Negation operator.
Definition: Vector4.hh:349
void Z(const T &_v)
Set the z value.
Definition: Vector4.hh:674