SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLVec4.h
Go to the documentation of this file.
1 /**
2  * \file math/Math/SLVec4.h
3  * \date July 2014
4  * \authors Marcus Hudritsch
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8 */
9 
10 #ifndef SLVEC4_H
11 #define SLVEC4_H
12 
13 #include <SL.h>
14 #include <SLVec2.h>
15 #include <SLVec3.h>
16 #include <Utils.h>
17 
18 //-----------------------------------------------------------------------------
19 //! 4D vector template class for standard 4D vector algebra.
20 /*!
21 4D vector template class with type definitions for 4D vectors and colors:
22 \n Use SLVec4f for a specific float type vector
23 \n Use SLVec4d for a specific double type vector
24 \n Use SLVec4r for a precision independent real type.
25 */
26 // clang-format off
27 template <class T>
28 class SLVec4
29 {
30  public:
31  union
32  { struct {T x, y, z, w;};
33  struct {T r, g, b, a;};
34  struct {T comp[4];};
35  };
36 
37  SLVec4 () {x=0;y=0;z=0;w=0;}
38  explicit SLVec4 (const T V) {x=V; y=V; z=V; w=1;}
39  SLVec4 (const T X,
40  const T Y,
41  const T Z=0,
42  const T W=1) {x=X; y=Y; z=Z; w=W;}
43  explicit SLVec4 (const T v[4]) {x=v[0]; y=v[1]; z=v[2]; w=v[3];}
44  explicit SLVec4 (const SLVec2<T>& v) {x=v.x; y=v.y; z=0; w=1;}
45  explicit SLVec4 (const SLVec3<T>& v) {x=v.x; y=v.y; z=v.z; w=1;}
46  SLVec4 (const SLVec4<T>& v) {x=v.x; y=v.y; z=v.z; w=v.w;}
47  explicit SLVec4 (const SLstring& fourFloatsWithDelimiter) {fromString(fourFloatsWithDelimiter);}
48 
49  void set (const T X,
50  const T Y,
51  const T Z,
52  const T W=1) {x=X; y=Y; z=Z; w=W;}
53  void set (const T xyz) {x=xyz; y=xyz; z=xyz; w=1;}
54  void set (const T v[4]) {x=v[0]; y=v[1]; z=v[2]; w=v[3];}
55  void set (const SLVec2<T>& v) {x=v.x; y=v.y; z=0; w=1;}
56  void set (const SLVec3<T>& v) {x=v.x; y=v.y; z=v.z; w=1;}
57  void set (const SLVec4<T>& v) {x=v.x; y=v.y; z=v.z; w=v.w;}
58 
59  inline SLbool operator == (const SLVec4& v) const {return (x==v.x && y==v.y && z==v.z && w==v.w);}
60  inline SLbool operator != (const SLVec4& v) const {return (x!=v.x || y!=v.y || z!=v.z || w!=v.w);}
61  inline SLbool operator <= (const SLVec4& v) const {return (x<=v.x && y<=v.y && z<=v.z && w<=v.w);}
62  inline SLbool operator >= (const SLVec4& v) const {return (x>=v.x && y>=v.y && z>=v.z && w>=v.w);}
63  inline SLbool operator < (const SLVec4& v) const {return (x<v.x && y<v.y && z<v.z && w<v.w);}
64  inline SLbool operator > (const SLVec4& v) const {return (x>v.x && y>v.y && z>v.z && w>v.w);}
65  inline SLbool operator <= (const T v) const {return (x<=v && y<=v && z<=v && w<=v);}
66  inline SLbool operator >= (const T v) const {return (x>=v && y>=v && z>=v && w>=v);}
67  inline SLbool operator < (const T v) const {return (x<v && y<v && z<v && w<v);}
68  inline SLbool operator > (const T v) const {return (x>v && y>v && z>v && w>v);}
69 
70 
71  // Operators with temp. allocation
72  inline SLVec4 operator + (const SLVec4& v) const {return SLVec4(x+v.x, y+v.y, z+v.z, w+v.w);}
73  inline SLVec4 operator - (const SLVec4& v) const {return SLVec4(x-v.x, y-v.y, z-v.z, w-v.w);}
74  inline SLVec4 operator - () const {return SLVec4(-x, -y, -z, -w);}
75  inline T operator * (const SLVec4& v) const {return x*v.x+y*v.y+z*v.z+w*v.w;}
76  inline SLVec4 operator * (const T s) const {return SLVec4(x*s, y*s, z*s);}
77  inline SLVec4 operator / (const T s) const {return SLVec4(x/s, y/s, z/s, w/s);}
78  inline SLVec4 operator & (const SLVec4& v) const {return SLVec4(x*v.x, y*v.y, z*v.z, w*v.w);}
79  friend inline
80  SLVec4 operator * (T s, const SLVec4& v) {return SLVec4(v.x*s, v.y*s, v.z*s);}
81 
82  // Stream output operator
83  friend std::ostream& operator << (std::ostream& output,
84  const SLVec4& v){output<<"["<<v.x<<","<<v.y<<","<<v.z<<","<<v.w<<"]"; return output;}
85 
86  // Assign operators
87  SLVec4& operator = (const SLVec2<T>& v) {x=v.x; y=v.y; z=0; w=1; return *this;}
88  SLVec4& operator = (const SLVec3<T>& v) {x=v.x; y=v.y; z=v.z; w=1; return *this;}
89  SLVec4& operator = (const SLVec4& v) {x=v.x; y=v.y; z=v.z; w=v.w; return *this;}
90  SLVec4& operator += (const SLVec4& v) {x+=v.x; y+=v.y; z+=v.z; w+=v.w; return *this;}
91  SLVec4& operator += (const SLVec3<T>& v) {x+=v.x; y+=v.y; z+=v.z; w+=0; return *this;}
92  SLVec4& operator -= (const SLVec4& v) {x-=v.x; y-=v.y; z-=v.z; w-=v.w; return *this;}
93  SLVec4& operator -= (const SLVec3<T>& v) {x-=v.x; y-=v.y; z-=v.z; w-=0; return *this;}
94  SLVec4& operator *= (const T s) {x*=s; y*=s; z*=s; w*=s; return *this;}
95  SLVec4& operator /= (const T s) {x/=s; y/=s; z/=s; w/=s; return *this;}
96  SLVec4& operator &= (const SLVec4& v) {x*=v.x; y*=v.y; z*=v.z; w*=v.w; return *this;}
97  SLVec4& operator &= (const SLVec3<T>& v) {x*=v.x; y*=v.y; z*=v.z; w*=1; return *this;}
98 
99  // Operations without temp. allocation
100  inline void add (const SLVec4& a,
101  const SLVec4& b) {x=a.x+b.x; y=a.y+b.y, z=a.z+b.z; w=a.w+b.w;}
102  inline void sub (const SLVec4& a,
103  const SLVec4& b) {x=a.x-b.x; y=a.y-b.y, z=a.z-b.z; w=a.w-b.w;}
104  inline void scale (const T s) {x*=s; y*=s; z*=s; w*=s;}
105  inline T dot (const SLVec4& v) {return x*v.x+y*v.y+z*v.z+w*v.w;}
106  inline void cross (const SLVec4& a,
107  const SLVec4& b) {x = a.y*b.z - a.z*b.y;
108  y = a.z*b.x - a.x*b.z;
109  z = a.x*b.y - a.y*b.x;
110  w = 1;}
111  inline SLVec3<T> vec3 () const {return SLVec3<T>(x,y,z);}
112  inline SLVec2<T> vec2 () const {return SLVec2<T>(x,y);}
113  inline T length () const {return (T)sqrt(x*x+y*y+z*z+w*w);}
114  inline T lengthSqr () const {return (x*x+y*y+z*z+w*w);}
115  inline SLVec4& normalize () {T L = length();
116  if (L>0){x/=L; y/=L; z/=L; w/=L;}
117  return *this;}
118  inline void wdiv () {x/=w; y/=w; z/=w; w=1;}
119  inline void clampMinMax (const T min,
120  const T max) {x = (x>max)?max : (x<min)?min : x;
121  y = (y>max)?max : (y<min)?min : y;
122  z = (z>max)?max : (z<min)?min : z;
123  w = 1;}
124  inline T diff (const SLVec4& v) {return Utils::abs(x-v.x) +
125  Utils::abs(y-v.y) +
126  Utils::abs(z-v.z) +
127  Utils::abs(w-v.w);}
128  inline T diffRGB (const SLVec4& v) {return Utils::abs(x-v.x) +
129  Utils::abs(y-v.y) +
130  Utils::abs(z-v.z);}
131  inline void mix (const SLVec4& a,
132  const SLVec4& b,
133  const T factor_b) {T factor_a = 1-factor_b;
134  x = a.x*factor_a + b.x*factor_b;
135  y = a.y*factor_a + b.y*factor_b;
136  z = a.z*factor_a + b.z*factor_b;
137  w = a.w*factor_a + b.w*factor_b;}
138  inline T minXYZ () {if (x<=y && x<=z) return x;
139  else if (y<=z) return y;
140  else return z;}
141  inline T maxXYZ () {if (x>=y && x>=z) return x;
142  else if (y>=z) return y;
143  else return z;}
144  inline T minXYZW () {if (x<=y && x<=z && x<=w) return x;
145  else if (y<=z && y<=w) return y;
146  else if (z<=w) return z;
147  else return w;}
148  inline T maxXYZW () {if (x>=y && x>=z && x>=w) return x;
149  else if (y>=z && y>=w) return y;
150  else if (z>=w) return z;
151  else return w;}
152  inline SLint maxComp () {if (x>=y && x>=z && x>=w) return 0;
153  else if (y>=z && y>=w) return 1;
154  else if (z>=w) return 2;
155  else return 3;}
156  inline SLint minComp () {if (x<=y && x<=z && x<=w) return 0;
157  else if (y<=z && y<=w) return 1;
158  else if (z<=w) return 2;
159  else return 3;}
160  inline SLbool isZero () {return (x==0 && y==0 && z==0 && w==0);}
161 
162  //! Gamma correction
163  void gammaCorrect(T oneOverGamma) {x= pow(x,oneOverGamma);
164  y= pow(y,oneOverGamma);
165  z= pow(z,oneOverGamma);}
166 
167  void print (const SLchar* str=nullptr)
168  { if (str) SL_LOG("%s",str);
169  SL_LOG("% 3.3f, % 3.3f, % 3.3f, % 3.3f",x, y, z, w);
170  }
171 
172  //! Conversion to string
173  SLstring toString (SLstring delimiter=", ")
174  { return Utils::toString(x) + delimiter +
175  Utils::toString(y) + delimiter +
176  Utils::toString(z) + delimiter +
178  }
179 
180  //! Conversion from string
181  void fromString (const SLstring& fourFloatsWithDelimiter, SLchar delimiter=',')
182  { SLVstring components;
183  Utils::splitString(fourFloatsWithDelimiter, delimiter, components);
184  float f[4] = {0.0, 0.0f, 0.0f, 1.0f};
185  for (SLuint i=0; i<components.size(); ++i)
186  f[i] = (SLfloat)atof(components[i].c_str());
187  x = f[0]; y = f[1]; z = f[2]; w = f[3];
188  }
189 
190  //! HSVA to RGBA color conversion (http://www.rapidtables.com/convert/color/hsv-to-rgb.htm)
191  void hsva2rgba (const SLVec4 &hsva)
192  {
193  T h = fmod(fmod(hsva.x, Utils::TWOPI) + Utils::TWOPI, Utils::TWOPI); // 0 deg <= H <= 360 deg
194  T s = Utils::clamp(hsva.y, 0.0f, 1.0f);
195  T v = Utils::clamp(hsva.z, 0.0f, 1.0f);
196  T a = Utils::clamp(hsva.w, 0.0f, 1.0f);
197 
198  T c = v * s;
199  T x = c * (1.0f - (T)fabs((T)fmod(h*3.0f / Utils::PI, 2.0f) - 1.0f));
200  T m = v - c;
201 
202  switch (SLint(floor(h*3.0f * Utils::ONEOVERPI)))
203  { case 0: return set(m + c, m + x, m , a); // [ 0 deg, 60 deg]
204  case 1: return set(m + x, m + c, m , a); // [ 60 deg,120 deg]
205  case 2: return set(m , m + c, m + x, a); // [120 deg,180 deg]
206  case 3: return set(m , m + x, m + c, a); // [180 deg,240 deg]
207  case 4: return set(m + x, m , m + c, a); // [240 deg,300 deg]
208  case 5: return set(m + c, m , m + x, a); // [300 deg,360 deg]
209  }
210  }
211 
212  static SLVec4 ZERO;
213  static SLVec4 BLACK;
214  static SLVec4 GRAY;
215  static SLVec4 WHITE;
216  static SLVec4 RED;
217  static SLVec4 GREEN;
218  static SLVec4 BLUE;
219  static SLVec4 YELLOW;
220  static SLVec4 CYAN;
221  static SLVec4 MAGENTA;
222 };
223 //-----------------------------------------------------------------------------
224 template<class T> SLVec4<T> SLVec4<T>::ZERO = SLVec4<T>(0.0f, 0.0f, 0.0f, 1.0f);
225 template<class T> SLVec4<T> SLVec4<T>::BLACK = SLVec4<T>(0.0f, 0.0f, 0.0f, 1.0f);
226 template<class T> SLVec4<T> SLVec4<T>::GRAY = SLVec4<T>(0.5f, 0.5f, 0.5f, 1.0f);
227 template<class T> SLVec4<T> SLVec4<T>::WHITE = SLVec4<T>(1.0f, 1.0f, 1.0f, 1.0f);
228 template<class T> SLVec4<T> SLVec4<T>::RED = SLVec4<T>(1.0f, 0.0f, 0.0f, 1.0f);
229 template<class T> SLVec4<T> SLVec4<T>::GREEN = SLVec4<T>(0.0f, 1.0f, 0.0f, 1.0f);
230 template<class T> SLVec4<T> SLVec4<T>::BLUE = SLVec4<T>(0.0f, 0.0f, 1.0f, 1.0f);
231 template<class T> SLVec4<T> SLVec4<T>::YELLOW = SLVec4<T>(1.0f, 1.0f, 0.0f, 1.0f);
232 template<class T> SLVec4<T> SLVec4<T>::CYAN = SLVec4<T>(0.0f, 1.0f, 1.0f, 1.0f);
233 template<class T> SLVec4<T> SLVec4<T>::MAGENTA= SLVec4<T>(1.0f, 0.0f, 1.0f, 1.0f);
234 //-----------------------------------------------------------------------------
238 
239 typedef vector<SLVec4f> SLVVec4f;
240 typedef vector<SLVec4i> SLVVec4i;
241 typedef vector<SLCol4f> SLVCol4f;
242 
243 #ifdef SL_HAS_DOUBLE
244 typedef SLVec4<SLdouble> SLVec4d;
245 typedef vector<SLVec4d> SLVVec4d;
246 #endif
247 //-----------------------------------------------------------------------------
248 #endif
249 
250 
float SLfloat
Definition: SL.h:173
#define SL_LOG(...)
Definition: SL.h:233
unsigned int SLuint
Definition: SL.h:171
char SLchar
Definition: SL.h:162
bool SLbool
Definition: SL.h:175
vector< SLstring > SLVstring
Definition: SL.h:201
string SLstring
Definition: SL.h:158
int SLint
Definition: SL.h:170
SLScene * s
Definition: SLScene.h:31
vector< SLVec4f > SLVVec4f
Definition: SLVec4.h:239
vector< SLVec4i > SLVVec4i
Definition: SLVec4.h:240
SLVec4< SLint > SLVec4i
Definition: SLVec4.h:236
vector< SLCol4f > SLVCol4f
Definition: SLVec4.h:241
SLVec4< SLfloat > SLVec4f
Definition: SLVec4.h:235
SLVec4< SLfloat > SLCol4f
Definition: SLVec4.h:237
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47
2D vector template class for standard 2D vector algebra.
Definition: SLVec2.h:27
T y
Definition: SLVec2.h:30
T x
Definition: SLVec2.h:30
3D vector template class for standard 3D vector algebra.
Definition: SLVec3.h:40
T y
Definition: SLVec3.h:43
T x
Definition: SLVec3.h:43
T z
Definition: SLVec3.h:43
4D vector template class for standard 4D vector algebra.
Definition: SLVec4.h:29
SLVec4 & operator*=(const T s)
Definition: SLVec4.h:94
T length() const
Definition: SLVec4.h:113
static SLVec4 BLACK
Definition: SLVec4.h:213
SLbool operator>=(const SLVec4 &v) const
Definition: SLVec4.h:62
T w
Definition: SLVec4.h:32
SLVec4 & operator+=(const SLVec4 &v)
Definition: SLVec4.h:90
void set(const SLVec3< T > &v)
Definition: SLVec4.h:56
void scale(const T s)
Definition: SLVec4.h:104
SLVec2< T > vec2() const
Definition: SLVec4.h:112
SLVec4 & operator/=(const T s)
Definition: SLVec4.h:95
void sub(const SLVec4 &a, const SLVec4 &b)
Definition: SLVec4.h:102
T z
Definition: SLVec4.h:32
SLVec4 & operator&=(const SLVec4 &v)
Definition: SLVec4.h:96
T diff(const SLVec4 &v)
Definition: SLVec4.h:124
void set(const T xyz)
Definition: SLVec4.h:53
T minXYZ()
Definition: SLVec4.h:138
static SLVec4 GREEN
Definition: SLVec4.h:217
SLVec4(const T v[4])
Definition: SLVec4.h:43
void add(const SLVec4 &a, const SLVec4 &b)
Definition: SLVec4.h:100
void set(const SLVec4< T > &v)
Definition: SLVec4.h:57
SLVec3< T > vec3() const
Definition: SLVec4.h:111
SLVec4 operator&(const SLVec4 &v) const
Definition: SLVec4.h:78
SLbool operator<(const SLVec4 &v) const
Definition: SLVec4.h:63
T dot(const SLVec4 &v)
Definition: SLVec4.h:105
T comp[4]
Definition: SLVec4.h:34
SLVec4()
Definition: SLVec4.h:37
void wdiv()
Definition: SLVec4.h:118
SLVec4(const SLVec2< T > &v)
Definition: SLVec4.h:44
SLVec4 operator-() const
Definition: SLVec4.h:74
static SLVec4 WHITE
Definition: SLVec4.h:215
T maxXYZW()
Definition: SLVec4.h:148
SLVec4(const T X, const T Y, const T Z=0, const T W=1)
Definition: SLVec4.h:39
T g
Definition: SLVec4.h:33
SLVec4 operator/(const T s) const
Definition: SLVec4.h:77
void set(const T X, const T Y, const T Z, const T W=1)
Definition: SLVec4.h:49
T b
Definition: SLVec4.h:33
void hsva2rgba(const SLVec4 &hsva)
HSVA to RGBA color conversion (http://www.rapidtables.com/convert/color/hsv-to-rgb....
Definition: SLVec4.h:191
SLVec4 operator+(const SLVec4 &v) const
Definition: SLVec4.h:72
SLVec4 & operator=(const SLVec2< T > &v)
Definition: SLVec4.h:87
T diffRGB(const SLVec4 &v)
Definition: SLVec4.h:128
T minXYZW()
Definition: SLVec4.h:144
void cross(const SLVec4 &a, const SLVec4 &b)
Definition: SLVec4.h:106
static SLVec4 ZERO
Definition: SLVec4.h:212
SLVec4 & normalize()
Definition: SLVec4.h:115
friend std::ostream & operator<<(std::ostream &output, const SLVec4 &v)
Definition: SLVec4.h:83
SLbool operator<=(const SLVec4 &v) const
Definition: SLVec4.h:61
static SLVec4 GRAY
Definition: SLVec4.h:214
T maxXYZ()
Definition: SLVec4.h:141
SLstring toString(SLstring delimiter=", ")
Conversion to string.
Definition: SLVec4.h:173
SLVec4 & operator-=(const SLVec4 &v)
Definition: SLVec4.h:92
T y
Definition: SLVec4.h:32
void gammaCorrect(T oneOverGamma)
Gamma correction.
Definition: SLVec4.h:163
SLbool operator==(const SLVec4 &v) const
Definition: SLVec4.h:59
SLint minComp()
Definition: SLVec4.h:156
SLVec4(const SLstring &fourFloatsWithDelimiter)
Definition: SLVec4.h:47
T operator*(const SLVec4 &v) const
Definition: SLVec4.h:75
void mix(const SLVec4 &a, const SLVec4 &b, const T factor_b)
Definition: SLVec4.h:131
SLVec4(const T V)
Definition: SLVec4.h:38
SLbool operator>(const SLVec4 &v) const
Definition: SLVec4.h:64
SLVec4(const SLVec3< T > &v)
Definition: SLVec4.h:45
SLbool isZero()
Definition: SLVec4.h:160
T a
Definition: SLVec4.h:33
T r
Definition: SLVec4.h:33
static SLVec4 YELLOW
Definition: SLVec4.h:219
static SLVec4 MAGENTA
Definition: SLVec4.h:221
static SLVec4 CYAN
Definition: SLVec4.h:220
SLVec4(const SLVec4< T > &v)
Definition: SLVec4.h:46
void set(const T v[4])
Definition: SLVec4.h:54
void clampMinMax(const T min, const T max)
Definition: SLVec4.h:119
SLint maxComp()
Definition: SLVec4.h:152
T lengthSqr() const
Definition: SLVec4.h:114
T x
Definition: SLVec4.h:32
void set(const SLVec2< T > &v)
Definition: SLVec4.h:55
static SLVec4 RED
Definition: SLVec4.h:216
void fromString(const SLstring &fourFloatsWithDelimiter, SLchar delimiter=',')
Conversion from string.
Definition: SLVec4.h:181
SLbool operator!=(const SLVec4 &v) const
Definition: SLVec4.h:60
void print(const SLchar *str=nullptr)
Definition: SLVec4.h:167
static SLVec4 BLUE
Definition: SLVec4.h:218
T abs(T a)
Definition: Utils.h:249
T clamp(T a, T min, T max)
Definition: Utils.h:253
void splitString(const string &s, char delimiter, vector< string > &splits)
Splits an input string at a delimiter character into a string vector.
Definition: Utils.cpp:152
static const float ONEOVERPI
Definition: Utils.h:241
T floor(T a)
Definition: Utils.h:246
static const float PI
Definition: Utils.h:237
static const float TWOPI
Definition: Utils.h:240
string toString(float f, int roundedDecimals)
Returns a string from a float with max. one trailing zero.
Definition: Utils.cpp:92