philsupertramp/game-math
Loading...
Searching...
No Matches
vec4.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4#include <ostream>
5
10template<class T>
11struct vec4 {
12public:
13#if MATH_SILENCE_WARNING
14 #if COMPILER_GCC
15 #pragma GCC diagnostic push
16 #pragma GCC diagnostic ignored "-Wpedantic"
17 #elif COMPILER_CLANG
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
20 #pragma clang diagnostic ignored "-Wnested-anon-types"
21 #elif COMPILER_VC
22 #pragma warning(push)
23 #pragma warning(disable : 4201) // nonstandard extension used : nameless struct/union
24 #endif
25#endif
27 union {
28 struct {
29 T x, y, z, w;
30 };
31 };
32
33#if MATH_SILENCE_WARNING
34 #if COMPILER_CLANG
35 #pragma clang diagnostic pop
36 #elif COMPILER_GCC
37 #pragma GCC diagnostic pop
38 #elif COMPILER_VC
39 #pragma warning(pop)
40 #endif
41#endif
42
44 const size_t dim = 4;
45
49 vec4() {
50 x = static_cast<T>(0.0f);
51 y = static_cast<T>(0.0f);
52 z = static_cast<T>(0.0f);
53 w = static_cast<T>(0.0f);
54 }
55
63 vec4(const T& _x, const T& _y, const T& _z, const T& _w) {
64 x = _x;
65 y = _y;
66 z = _z;
67 w = _w;
68 }
69
70 explicit vec4(const T& v) {
71 x = v;
72 y = v;
73 z = v;
74 w = v;
75 }
76
81 explicit vec4(const T* v) {
82 x = v[0];
83 y = v[1];
84 z = v[2];
85 w = v[3];
86 }
87
92 vec4(const vec4<T>& v) {
93 x = v[0];
94 y = v[1];
95 z = v[2];
96 w = v[3];
97 }
98
99 template<typename V>
100 vec4(const V& v, const T& _w) {
101 x = v[0];
102 y = v[1];
103 z = v[2];
104 w = _w;
105 }
106
107 /* Misc functions */
112 inline float length() const { return sqrtf(x * x + y * y + z * z + w * w); }
113
118 inline vec4<T> normalize() const { return *this / length(); }
119
120 inline T sum() const { return x + y + z + w; }
121
122 /* Arithmetic operators */
129 friend vec4<T> operator+(vec4<T> lhs, const vec4<T>& rhs) { return lhs += rhs; }
136 friend vec4<T> operator-(vec4<T> lhs, const vec4<T>& rhs) { return lhs -= rhs; }
137
144 friend vec4<T> operator*(vec4<T> lhs, const T& rhs) { return lhs *= rhs; }
145
152 friend vec4<T> operator*(const T& lhs, vec4<T> rhs) { return rhs *= lhs; }
153
160 friend vec4<T> operator*(vec4<T> lhs, const vec4<T>& rhs) {
161 return vec4<T>(lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * rhs.z, lhs.w * lhs.w);
162 }
169 friend vec4<T> operator/(vec4<T> lhs, const T& rhs) { return lhs /= rhs; }
170
171 /* compound assignment */
178 x += rhs.x;
179 y += rhs.y;
180 z += rhs.z;
181 w += rhs.w;
182 return *this;
183 }
190 x -= rhs.x;
191 y -= rhs.y;
192 z -= rhs.z;
193 w -= rhs.w;
194 return *this;
195 }
196
202 vec4<T>& operator*=(const T& rhs) {
203 x *= rhs;
204 y *= rhs;
205 z *= rhs;
206 w *= rhs;
207 return *this;
208 }
209
215 vec4<T>& operator/=(const T& rhs) {
216 x /= rhs;
217 y /= rhs;
218 z /= rhs;
219 w /= rhs;
220 return *this;
221 }
222
223 /* comparison operators */
230 friend bool operator==(const vec4<T>& lhs, const vec4<T>& rhs) {
231 return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z && lhs.w == rhs.w;
232 }
233 /* explicit type casts */
234
240 constexpr vec4<T>& operator=(vec4<T> const& V) {
241 this->x = static_cast<T>(V.x);
242 this->y = static_cast<T>(V.y);
243 this->z = static_cast<T>(V.z);
244 this->w = static_cast<T>(V.w);
245 return *this;
246 }
247
248 /* Member access */
254 T& operator[](const int& index) {
255 switch(index) {
256 default:
257 case 0: return x;
258 case 1: return y;
259 case 2: return z;
260 case 3: return w;
261 }
262 }
268 const T& operator[](const int& index) const {
269 switch(index) {
270 default:
271 case 0: return x;
272 case 1: return y;
273 case 2: return z;
274 case 3: return w;
275 }
276 }
277
284 template<typename U>
285 explicit operator vec4<U>() {
286 return vec4<U>(x, y, z, w);
287 }
288
289 // LCOV_EXCL_START
290 /* stream operators */
296 template<class U>
297 friend std::ostream& operator<<(std::ostream&, const vec4<U>&);
298 // LCOV_EXCL_STOP
299};
300
301template<class T>
302std::ostream& operator<<(std::ostream& os, const vec4<T>& obj) {
303 os.precision(17);
304 os << obj.x << ", " << obj.y << ", " << obj.z << ", " << obj.w << std::endl;
305 return os;
306}
307
314template<class T>
316 T* values = (T*)in;
317 vec4<T> out(values[0], values[1], values[2], values[3]);
318 return out;
319}
320
Definition: vec4.h:11
T sum() const
Definition: vec4.h:120
constexpr vec4< T > & operator=(vec4< T > const &V)
Definition: vec4.h:240
friend vec4< T > operator-(vec4< T > lhs, const vec4< T > &rhs)
Definition: vec4.h:136
const T & operator[](const int &index) const
Definition: vec4.h:268
vec4(const V &v, const T &_w)
Definition: vec4.h:100
T & operator[](const int &index)
Definition: vec4.h:254
vec4(const vec4< T > &v)
Definition: vec4.h:92
T w
Definition: vec4.h:29
vec4< T > & operator/=(const T &rhs)
Definition: vec4.h:215
vec4(const T *v)
Definition: vec4.h:81
vec4< T > normalize() const
Definition: vec4.h:118
friend vec4< T > operator*(vec4< T > lhs, const vec4< T > &rhs)
Definition: vec4.h:160
friend vec4< T > operator+(vec4< T > lhs, const vec4< T > &rhs)
Definition: vec4.h:129
friend vec4< T > operator*(vec4< T > lhs, const T &rhs)
Definition: vec4.h:144
friend vec4< T > operator/(vec4< T > lhs, const T &rhs)
Definition: vec4.h:169
vec4()
Definition: vec4.h:49
vec4(const T &_x, const T &_y, const T &_z, const T &_w)
Definition: vec4.h:63
const size_t dim
dimension of object
Definition: vec4.h:44
T y
Definition: vec4.h:29
friend std::ostream & operator<<(std::ostream &, const vec4< U > &)
friend vec4< T > operator*(const T &lhs, vec4< T > rhs)
Definition: vec4.h:152
float length() const
Definition: vec4.h:112
vec4< T > & operator-=(const vec4< T > &rhs)
Definition: vec4.h:189
vec4< T > & operator+=(const vec4< T > &rhs)
Definition: vec4.h:177
T z
Definition: vec4.h:29
vec4(const T &v)
Definition: vec4.h:70
vec4< T > & operator*=(const T &rhs)
Definition: vec4.h:202
T x
Definition: vec4.h:29
friend bool operator==(const vec4< T > &lhs, const vec4< T > &rhs)
Definition: vec4.h:230
std::ostream & operator<<(std::ostream &os, const vec4< T > &obj)
Definition: vec4.h:302
vec4< T > build_vec4(void *in)
Definition: vec4.h:315