Vector3D.hpp
Go to the documentation of this file.
1 
7 #include <etk/types.hpp>
8 
9 #pragma once
10 
11 #include <cmath>
12 
13 #ifdef ETK_BUILD_LINEARMATH
14  #include <LinearMath/btScalar.h>
15  #include <LinearMath/btMinMax.h>
16  #include <LinearMath/btVector3.h>
17  #include <LinearMath/btQuaternion.h>
18 #else
19  namespace etk {
20  template <typename T> class Vector3D;
21  };
24 #endif
25 
26 namespace etk {
30  template <typename T> class Vector3D {
31  public:
32  T m_floats[4];
33  public:
38  #ifdef DEBUG
39  // in debug mode we set supid value to prevent forget of the inits ...
40  m_floats[0] = (T)34673363;
41  m_floats[1] = (T)34523535;
42  m_floats[2] = (T)43523424;
43  m_floats[3] = (T)23452345;
44  #endif
45  #if ( !defined(__TARGET_OS__MacOs) \
46  && !defined(__TARGET_OS__IOs) \
47  && defined(ETK_BUILD_LINEARMATH))
48  // hide a bullet warning
49  (void)btInfinityMask;
50  #endif
51  }
58  Vector3D(const T& _xxx, const T& _yyy, const T& _zzz) {
59  m_floats[0] = _xxx;
60  m_floats[1] = _yyy;
61  m_floats[2] = _zzz;
62  m_floats[3] = 0;
63  }
70  m_floats[0] += _obj.m_floats[0];
71  m_floats[1] += _obj.m_floats[1];
72  m_floats[2] += _obj.m_floats[2];
73  return *this;
74  }
81  return Vector3D<T>(m_floats[0] + _obj.m_floats[0],
82  m_floats[1] + _obj.m_floats[1],
83  m_floats[2] + _obj.m_floats[2]);
84  }
91  m_floats[0] -= _obj.m_floats[0];
92  m_floats[1] -= _obj.m_floats[1];
93  m_floats[2] -= _obj.m_floats[2];
94  return *this;
95  }
102  return Vector3D<T>(m_floats[0] - _obj.m_floats[0],
103  m_floats[1] - _obj.m_floats[1],
104  m_floats[2] - _obj.m_floats[2]);
105  }
111  Vector3D<T>& operator*=(const T& _val) {
112  m_floats[0] *= _val;
113  m_floats[1] *= _val;
114  m_floats[2] *= _val;
115  return *this;
116  }
122  Vector3D<T> operator*(const T& _val) {
123  return Vector3D<T>(m_floats[0] * _val,
124  m_floats[1] * _val,
125  m_floats[2] * _val);
126  }
133  if (_val != 0) {
134  return *this *= float(1.0) / _val;
135  }
136  return *this;
137  }
143  Vector3D<T>& operator/=(const T& _val) {
144  if (_val != 0) {
145  m_floats[0] /= _val;
146  m_floats[1] /= _val;
147  m_floats[2] /= _val;
148  return *this;
149  }
150  return *this;
151  }
157  float dot(const Vector3D<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  }
166  float length2() const {
167  return dot(*this);
168  }
173  float length() const {
174  #if __CPP_objERSION__ >= 2011 && !defined(__STDCPP_LLVM__)
175  return std::sqrt(length2());
176  #else
177  return sqrt(length2());
178  #endif
179  }
186  float distance2(const Vector3D<T>& _obj) const {
187  return (_obj - *this).length2();
188  }
195  float distance(const Vector3D<T>& _obj) const {
196  return (_obj - *this).length();
197  }
203  Vector3D<T> absVec = this->absolute();
204  int maxIndex = absVec.maxAxis();
205  if (absVec[maxIndex]>0) {
206  *this /= absVec[maxIndex];
207  return *this /= length();
208  }
209  setValue(1,0,0);
210  return *this;
211  }
217  return *this /= length();
218  }
224  Vector3D<T> out = *this;
225  out /= length();
226  return out;
227  }
234  Vector3D<T> rotate( const Vector3D<T>& _wAxis, const float _angle ) const {
235  Vector3D<T> o = _wAxis * _wAxis.dot( *this );
236  Vector3D<T> x = *this - o;
237  Vector3D<T> y;
238  y = _wAxis.cross( *this );
239  return ( o + x * cosf(_angle) + y * sinf(_angle) );
240  }
246  float angle(const Vector3D<T>& _obj) const {
247  float s = sqrtf(length2() * _obj.length2());
248  if (0!=s) {
249  return acosf(dot(_obj) / s);
250  }
251  return 0;
252  }
258  return Vector3D<T>( abs(m_floats[0]),
259  abs(m_floats[1]),
260  abs(m_floats[2]));
261  }
267  Vector3D<T> cross(const Vector3D<T>& _obj) const {
268  return Vector3D<T>(m_floats[1] * _obj.m_floats[2] - m_floats[2] * _obj.m_floats[1],
269  m_floats[2] * _obj.m_floats[0] - m_floats[0] * _obj.m_floats[2],
270  m_floats[0] * _obj.m_floats[1] - m_floats[1] * _obj.m_floats[0]);
271  }
278  T triple(const Vector3D<T>& _obj1, const Vector3D<T>& _obj2) const {
279  return m_floats[0] * (_obj1.m_floats[1] * _obj2.m_floats[2] - _obj1.m_floats[2] * _obj2.m_floats[1])
280  + m_floats[1] * (_obj1.m_floats[2] * _obj2.m_floats[0] - _obj1.m_floats[0] * _obj2.m_floats[2])
281  + m_floats[2] * (_obj1.m_floats[0] * _obj2.m_floats[1] - _obj1.m_floats[1] * _obj2.m_floats[0]);
282  }
287  int32_t minAxis() const {
288  if (m_floats[0] < m_floats[1]) {
289  return m_floats[0] < m_floats[2] ? 0 : 2;
290  }
291  return m_floats[1] < m_floats[2] ? 1 : 2;
292  }
297  int32_t maxAxis() const {
298  if (m_floats[0] < m_floats[1]) {
299  return m_floats[1] < m_floats[2] ? 2 : 1;
300  }
301  return m_floats[0] < m_floats[2] ? 2 : 0;
302  }
307  int32_t furthestAxis() const {
308  return absolute().minAxis();
309  }
314  int32_t closestAxis() const {
315  return absolute().maxAxis();
316  }
323  void setInterpolate3(const Vector3D<T>& _obj0, const Vector3D<T>& _obj1, T _ratio) {
324  float inverse = 1.0 - _ratio;
325  m_floats[0] = inverse * _obj0.m_floats[0] + _ratio * _obj1.m_floats[0];
326  m_floats[1] = inverse * _obj0.m_floats[1] + _ratio * _obj1.m_floats[1];
327  m_floats[2] = inverse * _obj0.m_floats[2] + _ratio * _obj1.m_floats[2];
328  // m_co[3] = s * v0[3] + rt * v1[3];
329  }
336  Vector3D<T> lerp(const Vector3D<T>& _obj, const float& _ratio) const {
337  return Vector3D<T>(m_floats[0] + (_obj.m_floats[0] - m_floats[0]) * _ratio,
338  m_floats[1] + (_obj.m_floats[1] - m_floats[1]) * _ratio,
339  m_floats[2] + (_obj.m_floats[2] - m_floats[2]) * _ratio);
340  }
347  m_floats[0] *= _obj.m_floats[0];
348  m_floats[1] *= _obj.m_floats[1];
349  m_floats[2] *= _obj.m_floats[2];
350  return *this;
351  }
358  return Vector3D<T>(m_floats[0] * _obj.m_floats[0],
359  m_floats[1] * _obj.m_floats[1],
360  m_floats[2] * _obj.m_floats[2]);
361  }
366  const T& getX() const {
367  return m_floats[0];
368  }
373  const T& getY() const {
374  return m_floats[1];
375  }
380  const T& getZ() const {
381  return m_floats[2];
382  }
387  void setX(T _x) {
388  m_floats[0] = _x;
389  }
394  void setY(T _y) {
395  m_floats[1] = _y;
396  }
401  void setZ(T _z) {
402  m_floats[2] = _z;
403  }
408  const T& x() const {
409  return m_floats[0];
410  }
415  const T& y() const {
416  return m_floats[1];
417  }
422  const T& z() const {
423  return m_floats[2];
424  }
429  operator T *() {
430  return &m_floats[0];
431  }
436  operator const T *() const {
437  return &m_floats[0];
438  }
445  bool operator==(const Vector3D<T>& _obj) const {
446  return ( (m_floats[3] == _obj.m_floats[3])
447  && (m_floats[2] == _obj.m_floats[2])
448  && (m_floats[1] == _obj.m_floats[1])
449  && (m_floats[0] == _obj.m_floats[0]));
450  }
457  bool operator!=(const Vector3D<T>& _obj) const {
458  return ( (m_floats[3] != _obj.m_floats[3])
459  || (m_floats[2] != _obj.m_floats[2])
460  || (m_floats[1] != _obj.m_floats[1])
461  || (m_floats[0] != _obj.m_floats[0]));
462  }
467  void setMax(const Vector3D<T>& _obj) {
468  btSetMax(m_floats[0], _obj.m_floats[0]);
469  btSetMax(m_floats[1], _obj.m_floats[1]);
470  btSetMax(m_floats[2], _obj.m_floats[2]);
471  btSetMax(m_floats[3], _obj.m_floats[3]);
472  }
477  void setMin(const Vector3D<T>& _obj) {
478  btSetMin(m_floats[0], _obj.m_floats[0]);
479  btSetMin(m_floats[1], _obj.m_floats[1]);
480  btSetMin(m_floats[2], _obj.m_floats[2]);
481  btSetMin(m_floats[3], _obj.m_floats[3]);
482  }
489  void setValue(const T& _xxx, const T& _yyy, const T& _zzz) {
490  m_floats[0]=_xxx;
491  m_floats[1]=_yyy;
492  m_floats[2]=_zzz;
493  m_floats[3] = 0;
494  }
501  void getSkewSymmetricMatrix(Vector3D<T>* _obj0,Vector3D<T>* _obj1,Vector3D<T>* _obj2) const {
502  _obj0->setValue(0. ,-z() ,y());
503  _obj1->setValue(z() ,0. ,-x());
504  _obj2->setValue(-y() ,x() ,0.);
505  }
509  void setZero() {
510  setValue(0,0,0);
511  }
517  bool isZero() const {
518  return m_floats[0] == 0
519  && m_floats[1] == 0
520  && m_floats[2] == 0;
521  }
522  };
524  std::ostream& operator <<(std::ostream& _os, const etk::Vector3D<int32_t>& _obj);
526  std::ostream& operator <<(std::ostream& _os, const etk::Vector3D<uint32_t>& _obj);
528  std::ostream& operator <<(std::ostream& _os, const etk::Vector3D<bool>& _obj);
529 };
530 
531 // To siplify the writing of the code ==> this permit to have the same name with the glsl language...
532 #ifdef ETK_BUILD_LINEARMATH
533  using vec3 = btVector3;
534 #else
536 #endif
539 // not compatible with glsl ... but it is better to have a same writing
542 
544 std::ostream& operator <<(std::ostream& _os, const vec3& _obj);
545 
546 #ifdef ETK_BUILD_LINEARMATH
547  vec3 quaternionToEulerXYZ(const btQuaternion& quat);
548 #endif
549 
555 inline vec3 vec3ClipInt32(const vec3& _val) {
556  return vec3(int32_t(_val.x()), int32_t(_val.y()), int32_t(_val.z()));
557 }
563 inline vec3 vec3ClipInt64(const vec3& _val) {
564  return vec3(int64_t(_val.x()), int64_t(_val.y()), int64_t(_val.z()));
565 }
566 
567 namespace etk {
569  std::ostream& operator <<(std::ostream& _os, const std::vector<vec3>& _obj);
571  std::ostream& operator <<(std::ostream& _os, const std::vector<ivec3>& _obj);
573  std::ostream& operator <<(std::ostream& _os, const std::vector<uivec3>& _obj);
575  std::ostream& operator <<(std::ostream& _os, const std::vector<bvec3>& _obj);
576 }
577 
578 
Vector3D< T > & operator-=(const Vector3D< T > &_obj)
Subtract a vector from this one.
Definition: Vector3D.hpp:90
float distance2(const Vector3D< T > &_obj) const
Return the distance squared between the ends of this and another vector This is symantically treating...
Definition: Vector3D.hpp:186
T m_floats[4]
all internal values
Definition: Vector3D.hpp:32
void setMin(const Vector3D< T > &_obj)
Set each element to the min of the current values and the values of another Vector3D<T> ...
Definition: Vector3D.hpp:477
void getSkewSymmetricMatrix(Vector3D< T > *_obj0, Vector3D< T > *_obj1, Vector3D< T > *_obj2) const
Create a skew matrix of the object.
Definition: Vector3D.hpp:501
int32_t maxAxis() const
Return the axis with the largest value.
Definition: Vector3D.hpp:297
int32_t furthestAxis() const
Return the axis with the smallest ABSOLUTE value.
Definition: Vector3D.hpp:307
basic namespace of the etk library. (it might contain all the etk fuctions/class/structures without m...
Definition: Archive.hpp:16
Vector3D< T > operator*(const T &_val)
Scale the vector.
Definition: Vector3D.hpp:122
const T & y() const
Get Y value.
Definition: Vector3D.hpp:415
vec3 vec3ClipInt32(const vec3 &_val)
Limit at integer value the input vector: vec3(1.2, 5.6, -2.9) ==> vec3(1.0, 5.0, -2.0)
Definition: Vector3D.hpp:555
const T & z() const
Get Z value.
Definition: Vector3D.hpp:422
Vector3D< T > & operator*=(const T &_val)
Scale the vector.
Definition: Vector3D.hpp:111
void setZ(T _z)
Set the z value.
Definition: Vector3D.hpp:401
Vector3D< T > & operator+=(const Vector3D< T > &_obj)
Add a vector to this one.
Definition: Vector3D.hpp:69
Vector3D< T > operator+(const Vector3D< T > &_obj)
Add a vector to this one.
Definition: Vector3D.hpp:80
vec3 vec3ClipInt64(const vec3 &_val)
Limit at integer value the input vector: vec3(1.2, 5.6, -2.9) ==> vec3(1.0, 5.0, -2.0)
Definition: Vector3D.hpp:563
Vector3D< T > rotate(const Vector3D< T > &_wAxis, const float _angle) const
Return a rotated version of this vector.
Definition: Vector3D.hpp:234
const T & getZ() const
Get Z value.
Definition: Vector3D.hpp:380
float distance(const Vector3D< T > &_obj) const
Return the distance between the ends of this and another vector This is symantically treating the vec...
Definition: Vector3D.hpp:195
void setValue(const T &_xxx, const T &_yyy, const T &_zzz)
Set Value on the vector.
Definition: Vector3D.hpp:489
Vector3D(const T &_xxx, const T &_yyy, const T &_zzz)
Constructor from scalars.
Definition: Vector3D.hpp:58
Vector3D< T > absolute() const
Return a vector will the absolute values of each element.
Definition: Vector3D.hpp:257
int32_t closestAxis() const
Return the axis with the largest ABSOLUTE value.
Definition: Vector3D.hpp:314
etk::Vector3D< float > btVector3
compatibility with bullet lib
Definition: Vector3D.hpp:21
int32_t minAxis() const
Return the axis with the smallest value.
Definition: Vector3D.hpp:287
T triple(const Vector3D< T > &_obj1, const Vector3D< T > &_obj2) const
Return the triple product between this and another vector and another.
Definition: Vector3D.hpp:278
void setZero()
Set 0 value on all the vector.
Definition: Vector3D.hpp:509
float dot(const Vector3D< T > &_obj) const
Return the dot product.
Definition: Vector3D.hpp:157
void setInterpolate3(const Vector3D< T > &_obj0, const Vector3D< T > &_obj1, T _ratio)
Interpolate the vector with a ration between 2 others.
Definition: Vector3D.hpp:323
bool operator!=(const Vector3D< T > &_obj) const
In-Equality compare operator with an other object.
Definition: Vector3D.hpp:457
Vector3D< T > & operator/=(const Vector3D< T > &_val)
Inversely scale the vector.
Definition: Vector3D.hpp:132
float length() const
Get the length of the vector.
Definition: Vector3D.hpp:173
Vector3D< T > operator-(const Vector3D< T > &_obj)
Subtract a vector from this one.
Definition: Vector3D.hpp:101
void setX(T _x)
Set the x value.
Definition: Vector3D.hpp:387
Vector3D< T > normalized() const
Return a normalized version of this vector.
Definition: Vector3D.hpp:223
void setMax(const Vector3D< T > &_obj)
Set each element to the max of the current values and the values of another Vector3D<T> ...
Definition: Vector3D.hpp:467
Vector3D< T > & operator*=(const Vector3D< T > &_obj)
Elementwise multiply this vector by the other.
Definition: Vector3D.hpp:346
Vectorial 3-dimention vector (x/y/z)
Definition: Vector3D.hpp:20
const T & getX() const
Get X value.
Definition: Vector3D.hpp:366
bool isZero() const
Check if the vector is equal to (0,0,0)
Definition: Vector3D.hpp:517
Vector3D< T > & safeNormalize()
Normalize this vector x^2 + y^2 + z^2 = 1 (check if not deviding by 0, if it is the case ==> return (...
Definition: Vector3D.hpp:202
Vector3D< T > & operator/=(const T &_val)
Inversely scale the vector.
Definition: Vector3D.hpp:143
const T & x() const
Get X value.
Definition: Vector3D.hpp:408
const T & getY() const
Get Y value.
Definition: Vector3D.hpp:373
Vector3D< T > lerp(const Vector3D< T > &_obj, const float &_ratio) const
Return the linear interpolation between this and another vector.
Definition: Vector3D.hpp:336
etk::Vector3D< float > vec3
wrapper on etk::Vector3D<float> to have the same naming has OpenGL shader
Definition: Vector3D.hpp:535
void setY(T _y)
Set the y value.
Definition: Vector3D.hpp:394
bool operator==(const Vector3D< T > &_obj) const
Equality compare operator with an other object.
Definition: Vector3D.hpp:445
Vector3D< T > cross(const Vector3D< T > &_obj) const
Return the cross product between this and another vector.
Definition: Vector3D.hpp:267
float angle(const Vector3D< T > &_obj) const
Calculate the angle between this and another vector.
Definition: Vector3D.hpp:246
float length2() const
Get the length of the vector squared.
Definition: Vector3D.hpp:166
Vector3D< T > operator*(const Vector3D< T > &_obj)
Elementwise multiply this vector by the other.
Definition: Vector3D.hpp:357
Vector3D< T > & normalize()
Normalize this vector x^2 + y^2 + z^2 = 1.
Definition: Vector3D.hpp:216
Vector3D()
No initialization constructor (faster ...)
Definition: Vector3D.hpp:37