Vector4D.hpp
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <etk/types.hpp>
9 
10 #include <cmath>
11 #ifdef ETK_BUILD_LINEARMATH
12  #include <LinearMath/btScalar.h>
13  #include <LinearMath/btMinMax.h>
14  #include <LinearMath/btVector3.h>
15  #include <LinearMath/btQuaternion.h>
16 #else
17  namespace etk {
18  template <typename T> class Vector4D;
19  };
20 #endif
21 namespace etk {
25  template <typename T> class Vector4D {
26  public:
27  T m_floats[4];
28  public:
33  #ifdef DEBUG
34  // in debug mode we set supid value to prevent forget of the inits ...
35  m_floats[0] = T(34673363);
36  m_floats[1] = T(34523535);
37  m_floats[2] = T(43523424);
38  m_floats[3] = T(23452345);
39  #endif
40  }
48  Vector4D(const T& _xxx, const T& _yyy, const T& _zzz, const T& _www) {
49  m_floats[0] = _xxx;
50  m_floats[1] = _yyy;
51  m_floats[2] = _zzz;
52  m_floats[3] = _www;
53  }
60  m_floats[0] += _obj.m_floats[0];
61  m_floats[1] += _obj.m_floats[1];
62  m_floats[2] += _obj.m_floats[2];
63  m_floats[3] += _obj.m_floats[3];
64  return *this;
65  }
72  return Vector4D<T>(m_floats[0] + _obj.m_floats[0],
73  m_floats[1] + _obj.m_floats[1],
74  m_floats[2] + _obj.m_floats[2],
75  m_floats[3] + _obj.m_floats[3]);
76  }
83  m_floats[0] -= _obj.m_floats[0];
84  m_floats[1] -= _obj.m_floats[1];
85  m_floats[2] -= _obj.m_floats[2];
86  m_floats[3] -= _obj.m_floats[3];
87  return *this;
88  }
95  return Vector4D<T>(m_floats[0] - _obj.m_floats[0],
96  m_floats[1] - _obj.m_floats[1],
97  m_floats[2] - _obj.m_floats[2],
98  m_floats[3] - _obj.m_floats[3]);
99  }
105  Vector4D<T>& operator*=(const T& _val) {
106  m_floats[0] *= _val;
107  m_floats[1] *= _val;
108  m_floats[2] *= _val;
109  m_floats[3] *= _val;
110  return *this;
111  }
117  Vector4D<T> operator*(const T& _val) {
118  return Vector4D<T>(m_floats[0] * _val,
119  m_floats[1] * _val,
120  m_floats[2] * _val,
121  m_floats[3] * _val);
122  }
128  Vector4D<T>& operator/=(const T& _val) {
129  if (_val != 0) {
130  m_floats[0] /= _val;
131  m_floats[1] /= _val;
132  m_floats[2] /= _val;
133  m_floats[3] /= _val;
134  return *this;
135  }
136  return *this;
137  }
143  Vector4D<T> operator/(const T& _val) {
144  if (_val != 0) {
145  return Vector4D<T>(m_floats[0] / _val,
146  m_floats[1] / _val,
147  m_floats[2] / _val,
148  m_floats[3] / _val);
149  }
150  return *this;
151  }
157  float dot(const Vector4D<T>& _obj) const {
158  return m_floats[0] * _obj.m_floats[0] +
159  m_floats[1] * _obj.m_floats[1] +
160  m_floats[2] * _obj.m_floats[2] +
161  m_floats[3] * _obj.m_floats[3];
162  }
167  float length2() const {
168  return dot(*this);
169  }
174  float length() const {
175  #ifdef ETK_BUILD_LINEARMATH
176  return btSqrt(length2());
177  #else
178  #if __CPP_VERSION__ >= 2011 && !defined(__TARGET_OS__MacOs) && !defined(__TARGET_OS__IOs)
179  return std::sqrt(length2());
180  #else
181  return sqrt(length2());
182  #endif
183  #endif
184  }
191  float distance2(const Vector4D<T>& _obj) const {
192  return (_obj - *this).length2();
193  }
200  float distance(const Vector4D<T>& _obj) const {
201  return (_obj - *this).length();
202  }
208  return *this /= length();
209  }
215  return *this / length();
216  }
222  return Vector4D<T>( abs(m_floats[0]),
223  abs(m_floats[1]),
224  abs(m_floats[2]),
225  abs(m_floats[3]));
226  }
233  m_floats[0] *= _obj.m_floats[0];
234  m_floats[1] *= _obj.m_floats[1];
235  m_floats[2] *= _obj.m_floats[2];
236  m_floats[3] *= _obj.m_floats[3];
237  return *this;
238  }
245  return Vector4D<T>(m_floats[0] * _obj.m_floats[0],
246  m_floats[1] * _obj.m_floats[1],
247  m_floats[2] * _obj.m_floats[2],
248  m_floats[3] * _obj.m_floats[3]);
249  }
254  const T& getX() const {
255  return m_floats[0];
256  }
261  const T& getY() const {
262  return m_floats[1];
263  }
268  const T& getZ() const {
269  return m_floats[2];
270  }
275  const T& getW() const {
276  return m_floats[3];
277  }
282  void setX(T _x) {
283  m_floats[0] = _x;
284  };
289  void setY(T _y) {
290  m_floats[1] = _y;
291  };
296  void setZ(T _z) {
297  m_floats[2] = _z;
298  };
303  void setW(T _w) {
304  m_floats[3] = _w;
305  };
310  const T& x() const {
311  return m_floats[0];
312  }
317  const T& y() const {
318  return m_floats[1];
319  }
324  const T& z() const {
325  return m_floats[2];
326  }
331  const T& w() const {
332  return m_floats[3];
333  }
338  operator T *() {
339  return &m_floats[0];
340  }
345  operator const T *() const {
346  return &m_floats[0];
347  }
354  bool operator==(const Vector4D<T>& _obj) const {
355  return ( (m_floats[3] == _obj.m_floats[3])
356  && (m_floats[2] == _obj.m_floats[2])
357  && (m_floats[1] == _obj.m_floats[1])
358  && (m_floats[0] == _obj.m_floats[0]));
359  }
366  bool operator!=(const Vector4D<T>& _obj) const {
367  return ( (m_floats[3] != _obj.m_floats[3])
368  || (m_floats[2] != _obj.m_floats[2])
369  || (m_floats[1] != _obj.m_floats[1])
370  || (m_floats[0] != _obj.m_floats[0]));
371  }
376  void setMax(const Vector4D<T>& _obj) {
377  btSetMax(m_floats[0], _obj.m_floats[0]);
378  btSetMax(m_floats[1], _obj.m_floats[1]);
379  btSetMax(m_floats[2], _obj.m_floats[2]);
380  btSetMax(m_floats[3], _obj.m_floats[3]);
381  }
386  void setMin(const Vector4D<T>& _obj) {
387  btSetMin(m_floats[0], _obj.m_floats[0]);
388  btSetMin(m_floats[1], _obj.m_floats[1]);
389  btSetMin(m_floats[2], _obj.m_floats[2]);
390  btSetMin(m_floats[3], _obj.m_floats[3]);
391  }
399  void setValue(const T& _xxx, const T& _yyy, const T& _zzz, const T& _www) {
400  m_floats[0]=_xxx;
401  m_floats[1]=_yyy;
402  m_floats[2]=_zzz;
403  m_floats[3]=_www;
404  }
408  void setZero() {
409  setValue(0,0,0,0);
410  }
416  bool isZero() const {
417  return m_floats[0] == 0
418  && m_floats[1] == 0
419  && m_floats[2] == 0
420  && m_floats[3] == 0;
421  }
422  };
424  std::ostream& operator <<(std::ostream& _os, const etk::Vector4D<int32_t>& _obj);
426  std::ostream& operator <<(std::ostream& _os, const etk::Vector4D<float>& _obj);
428  std::ostream& operator <<(std::ostream& _os, const etk::Vector4D<uint32_t>& _obj);
430  std::ostream& operator <<(std::ostream& _os, const etk::Vector4D<bool>& _obj);
431 }
432 
433 // To siplify the writing of the code ==> this permit to have the same name with the glsl language...
436 // not compatible with glsl ... but it is better to have a same writing
439 
440 
const T & z() const
Get Z value.
Definition: Vector4D.hpp:324
float length() const
Return the length of the vector.
Definition: Vector4D.hpp:174
void setW(T _w)
Set the w value.
Definition: Vector4D.hpp:303
Vector4D< T > operator/(const T &_val)
Inversely scale the vector.
Definition: Vector4D.hpp:143
void setValue(const T &_xxx, const T &_yyy, const T &_zzz, const T &_www)
Set Value on the vector.
Definition: Vector4D.hpp:399
bool operator==(const Vector4D< T > &_obj) const
Equality compare operator with an other object.
Definition: Vector4D.hpp:354
const T & w() const
Get W value.
Definition: Vector4D.hpp:331
float dot(const Vector4D< T > &_obj) const
Return the dot product.
Definition: Vector4D.hpp:157
T m_floats[4]
all internal values
Definition: Vector4D.hpp:27
Vector4D< T > & normalize()
Normalize this vector x^2 + y^2 + z^2 + w^2 = 1.
Definition: Vector4D.hpp:207
basic namespace of the etk library. (it might contain all the etk fuctions/class/structures without m...
Definition: Archive.hpp:16
bool operator!=(const Vector4D< T > &_obj) const
In-Equality compare operator with an other object.
Definition: Vector4D.hpp:366
Vector4D< T > operator+(const Vector4D< T > &_obj)
Add a vector to this one.
Definition: Vector4D.hpp:71
Vector4D< T > & operator+=(const Vector4D< T > &_obj)
Add a vector to this one.
Definition: Vector4D.hpp:59
void setMax(const Vector4D< T > &_obj)
Set each element to the max of the current values and the values of another Vector.
Definition: Vector4D.hpp:376
Vector4D< T > & operator*=(const Vector4D< T > &_obj)
Multiply this vector by the other.
Definition: Vector4D.hpp:232
bool isZero() const
Check if the vector is equal to (0,0,0,0)
Definition: Vector4D.hpp:416
const T & x() const
Get X value.
Definition: Vector4D.hpp:310
Vector4D()
No initialization constructor (faster ...)
Definition: Vector4D.hpp:32
void setZero()
Set 0 value on all the vector.
Definition: Vector4D.hpp:408
const T & y() const
Get Y value.
Definition: Vector4D.hpp:317
Vector4D< T > operator-(const Vector4D< T > &_obj)
Subtract a vector from this one.
Definition: Vector4D.hpp:94
Vector4D< T > & operator*=(const T &_val)
Scale the vector.
Definition: Vector4D.hpp:105
void setX(T _x)
Set the x value.
Definition: Vector4D.hpp:282
Vector4D< T > absolute() const
Return a vector will the absolute values of each element.
Definition: Vector4D.hpp:221
Vector4D< T > & operator-=(const Vector4D< T > &_obj)
Subtract a vector from this one.
Definition: Vector4D.hpp:82
void setZ(T _z)
Set the z value.
Definition: Vector4D.hpp:296
const T & getX() const
Get X value.
Definition: Vector4D.hpp:254
Vector4D< T > operator*(const T &_val)
Scale the vector.
Definition: Vector4D.hpp:117
float length2() const
Return the squared length of the vector.
Definition: Vector4D.hpp:167
Vector4D< T > normalized() const
Return a normalized version of this vector.
Definition: Vector4D.hpp:214
Vector4D(const T &_xxx, const T &_yyy, const T &_zzz, const T &_www)
Constructor from scalars.
Definition: Vector4D.hpp:48
Vectorial 4-dimention vector (x/y/z/w)
Definition: Vector4D.hpp:18
float distance(const Vector4D< T > &_obj) const
Return the distance between the ends of this and another vector This is symantically treating the vec...
Definition: Vector4D.hpp:200
void setMin(const Vector4D< T > &_obj)
Set each element to the min of the current values and the values of another Vector.
Definition: Vector4D.hpp:386
const T & getW() const
Get W value.
Definition: Vector4D.hpp:275
Vector4D< T > & operator/=(const T &_val)
Inversely scale the vector.
Definition: Vector4D.hpp:128
const T & getZ() const
Get Z value.
Definition: Vector4D.hpp:268
float distance2(const Vector4D< T > &_obj) const
Return the distance squared between the ends of this and another vector This is symantically treating...
Definition: Vector4D.hpp:191
Vector4D< T > operator*(const Vector4D< T > &_obj)
Multiply this vector by the other.
Definition: Vector4D.hpp:244
const T & getY() const
Get Y value.
Definition: Vector4D.hpp:261
void setY(T _y)
Set the y value.
Definition: Vector4D.hpp:289