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 <ignition/math/Matrix4.hh>
21 #include <ignition/math/Helpers.hh>
22 #include <ignition/math/config.hh>
23 
24 namespace ignition
25 {
26  namespace math
27  {
28  // Inline bracket to help doxygen filtering.
29  inline namespace IGNITION_MATH_VERSION_NAMESPACE {
30  //
33  template<typename T>
34  class Vector4
35  {
37  public: static const Vector4<T> Zero;
38 
40  public: static const Vector4<T> One;
41 
43  public: Vector4()
44  {
45  this->data[0] = this->data[1] = this->data[2] = this->data[3] = 0;
46  }
47 
53  public: Vector4(const T &_x, const T &_y, const T &_z, const T &_w)
54  {
55  this->data[0] = _x;
56  this->data[1] = _y;
57  this->data[2] = _z;
58  this->data[3] = _w;
59  }
60 
63  public: Vector4(const Vector4<T> &_v)
64  {
65  this->data[0] = _v[0];
66  this->data[1] = _v[1];
67  this->data[2] = _v[2];
68  this->data[3] = _v[3];
69  }
70 
72  public: virtual ~Vector4() {}
73 
77  public: T Distance(const Vector4<T> &_pt) const
78  {
79  return sqrt((this->data[0]-_pt[0])*(this->data[0]-_pt[0]) +
80  (this->data[1]-_pt[1])*(this->data[1]-_pt[1]) +
81  (this->data[2]-_pt[2])*(this->data[2]-_pt[2]) +
82  (this->data[3]-_pt[3])*(this->data[3]-_pt[3]));
83  }
84 
87  public: T Length() const
88  {
89  return sqrt(this->SquaredLength());
90  }
91 
94  public: T SquaredLength() const
95  {
96  return std::pow(this->data[0], 2)
97  + std::pow(this->data[1], 2)
98  + std::pow(this->data[2], 2)
99  + std::pow(this->data[3], 2);
100  }
101 
103  public: void Normalize()
104  {
105  T d = this->Length();
106 
107  if (!equal<T>(d, static_cast<T>(0.0)))
108  {
109  this->data[0] /= d;
110  this->data[1] /= d;
111  this->data[2] /= d;
112  this->data[3] /= d;
113  }
114  }
115 
121  public: void Set(T _x = 0, T _y = 0, T _z = 0, T _w = 0)
122  {
123  this->data[0] = _x;
124  this->data[1] = _y;
125  this->data[2] = _z;
126  this->data[3] = _w;
127  }
128 
132  public: Vector4<T> &operator=(const Vector4<T> &_v)
133  {
134  this->data[0] = _v[0];
135  this->data[1] = _v[1];
136  this->data[2] = _v[2];
137  this->data[3] = _v[3];
138 
139  return *this;
140  }
141 
144  public: Vector4<T> &operator=(T _value)
145  {
146  this->data[0] = _value;
147  this->data[1] = _value;
148  this->data[2] = _value;
149  this->data[3] = _value;
150 
151  return *this;
152  }
153 
157  public: Vector4<T> operator+(const Vector4<T> &_v) const
158  {
159  return Vector4<T>(this->data[0] + _v[0],
160  this->data[1] + _v[1],
161  this->data[2] + _v[2],
162  this->data[3] + _v[3]);
163  }
164 
168  public: const Vector4<T> &operator+=(const Vector4<T> &_v)
169  {
170  this->data[0] += _v[0];
171  this->data[1] += _v[1];
172  this->data[2] += _v[2];
173  this->data[3] += _v[3];
174 
175  return *this;
176  }
177 
181  public: inline Vector4<T> operator+(const T _s) const
182  {
183  return Vector4<T>(this->data[0] + _s,
184  this->data[1] + _s,
185  this->data[2] + _s,
186  this->data[3] + _s);
187  }
188 
193  public: friend inline Vector4<T> operator+(const T _s,
194  const Vector4<T> &_v)
195  {
196  return _v + _s;
197  }
198 
202  public: const Vector4<T> &operator+=(const T _s)
203  {
204  this->data[0] += _s;
205  this->data[1] += _s;
206  this->data[2] += _s;
207  this->data[3] += _s;
208 
209  return *this;
210  }
211 
214  public: inline Vector4 operator-() const
215  {
216  return Vector4(-this->data[0], -this->data[1],
217  -this->data[2], -this->data[3]);
218  }
219 
223  public: Vector4<T> operator-(const Vector4<T> &_v) const
224  {
225  return Vector4<T>(this->data[0] - _v[0],
226  this->data[1] - _v[1],
227  this->data[2] - _v[2],
228  this->data[3] - _v[3]);
229  }
230 
234  public: const Vector4<T> &operator-=(const Vector4<T> &_v)
235  {
236  this->data[0] -= _v[0];
237  this->data[1] -= _v[1];
238  this->data[2] -= _v[2];
239  this->data[3] -= _v[3];
240 
241  return *this;
242  }
243 
247  public: inline Vector4<T> operator-(const T _s) const
248  {
249  return Vector4<T>(this->data[0] - _s,
250  this->data[1] - _s,
251  this->data[2] - _s,
252  this->data[3] - _s);
253  }
254 
259  public: friend inline Vector4<T> operator-(const T _s,
260  const Vector4<T> &_v)
261  {
262  return {_s - _v.X(), _s - _v.Y(), _s - _v.Z(), _s - _v.W()};
263  }
264 
268  public: const Vector4<T> &operator-=(const T _s)
269  {
270  this->data[0] -= _s;
271  this->data[1] -= _s;
272  this->data[2] -= _s;
273  this->data[3] -= _s;
274 
275  return *this;
276  }
277 
283  public: const Vector4<T> operator/(const Vector4<T> &_v) const
284  {
285  return Vector4<T>(this->data[0] / _v[0],
286  this->data[1] / _v[1],
287  this->data[2] / _v[2],
288  this->data[3] / _v[3]);
289  }
290 
296  public: const Vector4<T> &operator/=(const Vector4<T> &_v)
297  {
298  this->data[0] /= _v[0];
299  this->data[1] /= _v[1];
300  this->data[2] /= _v[2];
301  this->data[3] /= _v[3];
302 
303  return *this;
304  }
305 
311  public: const Vector4<T> operator/(T _v) const
312  {
313  return Vector4<T>(this->data[0] / _v, this->data[1] / _v,
314  this->data[2] / _v, this->data[3] / _v);
315  }
316 
320  public: const Vector4<T> &operator/=(T _v)
321  {
322  this->data[0] /= _v;
323  this->data[1] /= _v;
324  this->data[2] /= _v;
325  this->data[3] /= _v;
326 
327  return *this;
328  }
329 
335  public: const Vector4<T> operator*(const Vector4<T> &_pt) const
336  {
337  return Vector4<T>(this->data[0] * _pt[0],
338  this->data[1] * _pt[1],
339  this->data[2] * _pt[2],
340  this->data[3] * _pt[3]);
341  }
342 
346  public: const Vector4<T> operator*(const Matrix4<T> &_m) const
347  {
348  return Vector4<T>(
349  this->data[0]*_m(0, 0) + this->data[1]*_m(1, 0) +
350  this->data[2]*_m(2, 0) + this->data[3]*_m(3, 0),
351  this->data[0]*_m(0, 1) + this->data[1]*_m(1, 1) +
352  this->data[2]*_m(2, 1) + this->data[3]*_m(3, 1),
353  this->data[0]*_m(0, 2) + this->data[1]*_m(1, 2) +
354  this->data[2]*_m(2, 2) + this->data[3]*_m(3, 2),
355  this->data[0]*_m(0, 3) + this->data[1]*_m(1, 3) +
356  this->data[2]*_m(2, 3) + this->data[3]*_m(3, 3));
357  }
358 
364  public: const Vector4<T> &operator*=(const Vector4<T> &_pt)
365  {
366  this->data[0] *= _pt[0];
367  this->data[1] *= _pt[1];
368  this->data[2] *= _pt[2];
369  this->data[3] *= _pt[3];
370 
371  return *this;
372  }
373 
377  public: const Vector4<T> operator*(T _v) const
378  {
379  return Vector4<T>(this->data[0] * _v, this->data[1] * _v,
380  this->data[2] * _v, this->data[3] * _v);
381  }
382 
387  public: friend inline const Vector4 operator*(const T _s,
388  const Vector4 &_v)
389  {
390  return Vector4(_v * _s);
391  }
392 
396  public: const Vector4<T> &operator*=(T _v)
397  {
398  this->data[0] *= _v;
399  this->data[1] *= _v;
400  this->data[2] *= _v;
401  this->data[3] *= _v;
402 
403  return *this;
404  }
405 
411  public: bool Equal(const Vector4 &_v, const T &_tol) const
412  {
413  return equal<T>(this->data[0], _v[0], _tol)
414  && equal<T>(this->data[1], _v[1], _tol)
415  && equal<T>(this->data[2], _v[2], _tol)
416  && equal<T>(this->data[3], _v[3], _tol);
417  }
418 
423  public: bool operator==(const Vector4<T> &_v) const
424  {
425  return this->Equal(_v, static_cast<T>(1e-6));
426  }
427 
432  public: bool operator!=(const Vector4<T> &_pt) const
433  {
434  return !(*this == _pt);
435  }
436 
439  public: bool IsFinite() const
440  {
441  // std::isfinite works with floating point values,
442  // need to explicit cast to avoid ambiguity in vc++.
443  return std::isfinite(static_cast<double>(this->data[0])) &&
444  std::isfinite(static_cast<double>(this->data[1])) &&
445  std::isfinite(static_cast<double>(this->data[2])) &&
446  std::isfinite(static_cast<double>(this->data[3]));
447  }
448 
453  public: T &operator[](const std::size_t _index)
454  {
455  return this->data[clamp(_index, IGN_ZERO_SIZE_T, IGN_THREE_SIZE_T)];
456  }
457 
462  public: T operator[](const std::size_t _index) const
463  {
464  return this->data[clamp(_index, IGN_ZERO_SIZE_T, IGN_THREE_SIZE_T)];
465  }
466 
469  public: T &X()
470  {
471  return this->data[0];
472  }
473 
476  public: T &Y()
477  {
478  return this->data[1];
479  }
480 
483  public: T &Z()
484  {
485  return this->data[2];
486  }
487 
490  public: T &W()
491  {
492  return this->data[3];
493  }
494 
497  public: T X() const
498  {
499  return this->data[0];
500  }
501 
504  public: T Y() const
505  {
506  return this->data[1];
507  }
508 
511  public: T Z() const
512  {
513  return this->data[2];
514  }
515 
518  public: T W() const
519  {
520  return this->data[3];
521  }
522 
525  public: inline void X(const T &_v)
526  {
527  this->data[0] = _v;
528  }
529 
532  public: inline void Y(const T &_v)
533  {
534  this->data[1] = _v;
535  }
536 
539  public: inline void Z(const T &_v)
540  {
541  this->data[2] = _v;
542  }
543 
546  public: inline void W(const T &_v)
547  {
548  this->data[3] = _v;
549  }
550 
555  public: friend std::ostream &operator<<(
556  std::ostream &_out, const ignition::math::Vector4<T> &_pt)
557  {
558  _out << _pt[0] << " " << _pt[1] << " " << _pt[2] << " " << _pt[3];
559  return _out;
560  }
561 
566  public: friend std::istream &operator>>(
568  {
569  T x, y, z, w;
570 
571  // Skip white spaces
572  _in.setf(std::ios_base::skipws);
573  _in >> x >> y >> z >> w;
574  _pt.Set(x, y, z, w);
575  return _in;
576  }
577 
579  private: T data[4];
580  };
581 
582  template<typename T>
583  const Vector4<T> Vector4<T>::Zero(0, 0, 0, 0);
584 
585  template<typename T>
586  const Vector4<T> Vector4<T>::One(1, 1, 1, 1);
587 
591  }
592  }
593 }
594 #endif
friend Vector4< T > operator+(const T _s, const Vector4< T > &_v)
Addition operators.
Definition: Vector4.hh:193
const Vector4< T > operator*(T _v) const
Multiplication operators.
Definition: Vector4.hh:377
T setf(T... args)
static const Vector4< T > Zero
math::Vector4(0, 0, 0, 0)
Definition: Vector4.hh:37
const Vector4< T > operator/(T _v) const
Division assignment operator.
Definition: Vector4.hh:311
T & Z()
Return a mutable z value.
Definition: Vector4.hh:483
Vector4< T > operator-(const T _s) const
Subtraction operators.
Definition: Vector4.hh:247
T & W()
Return a mutable w value.
Definition: Vector4.hh:490
static const static double const static double const static double const static double const static float const static float const static float const static float const static uint16_t const static uint16_t const static uint16_t const static uint16_t const static int16_t const static int16_t const static int16_t const static int16_t const static uint32_t const static uint32_t const static uint32_t const static uint32_t const static int32_t const static int32_t const static int32_t const static int32_t const static uint64_t const static uint64_t const static uint64_t const static uint64_t const static int64_t const static int64_t const static int64_t const int64_t T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:403
bool operator!=(const Vector4< T > &_pt) const
Not equal to operator.
Definition: Vector4.hh:432
Vector4< T > operator+(const T _s) const
Addition operators.
Definition: Vector4.hh:181
bool operator==(const Vector4< T > &_v) const
Equal to operator.
Definition: Vector4.hh:423
friend std::istream & operator>>(std::istream &_in, Vector4< T > &_pt)
Stream extraction operator.
Definition: Vector4.hh:566
const Vector4< T > & operator/=(const Vector4< T > &_v)
Division assignment operator.
Definition: Vector4.hh:296
friend const Vector4 operator*(const T _s, const Vector4 &_v)
Scalar left multiplication operators.
Definition: Vector4.hh:387
void Y(const T &_v)
Set the y value.
Definition: Vector4.hh:532
friend std::ostream & operator<<(std::ostream &_out, const Vector4< T > &_pt)
Stream insertion operator.
Definition: Vector4.hh:555
A 4x4 matrix class.
Definition: Matrix4.hh:38
Vector4< T > & operator=(T _value)
Assignment operator.
Definition: Vector4.hh:144
T Z() const
Get the z value.
Definition: Vector4.hh:511
STL class.
void X(const T &_v)
Set the x value.
Definition: Vector4.hh:525
void W(const T &_v)
Set the w value.
Definition: Vector4.hh:546
const Vector4< T > & operator-=(const T _s)
Subtraction assignment operator.
Definition: Vector4.hh:268
T Y() const
Get the y value.
Definition: Vector4.hh:504
Vector4< float > Vector4f
Definition: Vector4.hh:590
static const Vector4< T > One
math::Vector4(1, 1, 1, 1)
Definition: Vector4.hh:40
T operator[](const std::size_t _index) const
Const-qualified array subscript operator.
Definition: Vector4.hh:462
Vector4< T > & operator=(const Vector4< T > &_v)
Assignment operator.
Definition: Vector4.hh:132
Vector4()
Constructor.
Definition: Vector4.hh:43
const Vector4< T > & operator/=(T _v)
Division operator.
Definition: Vector4.hh:320
T W() const
Get the w value.
Definition: Vector4.hh:518
static const size_t IGN_ZERO_SIZE_T
size_t type with a value of 0
Definition: Helpers.hh:224
T isfinite(T... args)
const Vector4< T > operator*(const Matrix4< T > &_m) const
Matrix multiplication operator.
Definition: Vector4.hh:346
virtual ~Vector4()
Destructor.
Definition: Vector4.hh:72
T Length() const
Returns the length (magnitude) of the vector.
Definition: Vector4.hh:87
Vector4(const Vector4< T > &_v)
Copy constructor.
Definition: Vector4.hh:63
void Normalize()
Normalize the vector length.
Definition: Vector4.hh:103
T & X()
Return a mutable x value.
Definition: Vector4.hh:469
const Vector4< T > & operator*=(const Vector4< T > &_pt)
Multiplication assignment operator.
Definition: Vector4.hh:364
const Vector4< T > operator/(const Vector4< T > &_v) const
Division assignment operator.
Definition: Vector4.hh:283
T & Y()
Return a mutable y value.
Definition: Vector4.hh:476
const Vector4< T > & operator-=(const Vector4< T > &_v)
Subtraction assigment operators.
Definition: Vector4.hh:234
T pow(T... args)
Vector4< int > Vector4i
Definition: Vector4.hh:588
const Vector4< T > & operator*=(T _v)
Multiplication assignment operator.
Definition: Vector4.hh:396
T & operator[](const std::size_t _index)
Array subscript operator.
Definition: Vector4.hh:453
T SquaredLength() const
Return the square of the length (magnitude) of the vector.
Definition: Vector4.hh:94
Vector4< T > operator-(const Vector4< T > &_v) const
Subtraction operator.
Definition: Vector4.hh:223
bool Equal(const Vector4 &_v, const T &_tol) const
Equality test with tolerance.
Definition: Vector4.hh:411
const Vector4< T > & operator+=(const T _s)
Addition assignment operator.
Definition: Vector4.hh:202
static const size_t IGN_THREE_SIZE_T
size_t type with a value of 3
Definition: Helpers.hh:233
bool IsFinite() const
See if a point is finite (e.g., not nan)
Definition: Vector4.hh:439
Definition: Angle.hh:42
friend Vector4< T > operator-(const T _s, const Vector4< T > &_v)
Subtraction operators.
Definition: Vector4.hh:259
Vector4< T > operator+(const Vector4< T > &_v) const
Addition operator.
Definition: Vector4.hh:157
STL class.
const Vector4< T > & operator+=(const Vector4< T > &_v)
Addition operator.
Definition: Vector4.hh:168
T Generic x, y, z, w vector.
Definition: Vector4.hh:34
Vector4< double > Vector4d
Definition: Vector4.hh:589
const Vector4< T > operator*(const Vector4< T > &_pt) const
Multiplication operator.
Definition: Vector4.hh:335
void Set(T _x=0, T _y=0, T _z=0, T _w=0)
Set the contents of the vector.
Definition: Vector4.hh:121
T X() const
Get the x value.
Definition: Vector4.hh:497
T Distance(const Vector4< T > &_pt) const
Calc distance to the given point.
Definition: Vector4.hh:77
Vector4(const T &_x, const T &_y, const T &_z, const T &_w)
Constructor with component values.
Definition: Vector4.hh:53
Vector4 operator-() const
Negation operator.
Definition: Vector4.hh:214
void Z(const T &_v)
Set the z value.
Definition: Vector4.hh:539