philsupertramp/game-math
Loading...
Searching...
No Matches
vec3.h
Go to the documentation of this file.
1#pragma once
2
3#include "vec2.h"
4#include "vec4.h"
5#include <cmath>
6#include <limits>
7#include <ostream>
8
13template<class T>
14struct vec3 {
15public:
16#if MATH_SILENCE_WARNING
17 #if COMPILER_GCC
18 #pragma GCC diagnostic push
19 #pragma GCC diagnostic ignored "-Wpedantic"
20 #elif COMPILER_CLANG
21 #pragma clang diagnostic push
22 #pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
23 #pragma clang diagnostic ignored "-Wnested-anon-types"
24 #elif COMPILER_VC
25 #pragma warning(push)
26 #pragma warning(disable : 4201) // nonstandard extension used : nameless struct/union
27 #endif
28#endif
29
31 union {
32 struct {
33 T x, y, z;
34 };
35 };
36
38 const size_t dim = 3;
39
40#if MATH_SILENCE_WARNING
41 #if COMPILER_CLANG
42 #pragma clang diagnostic pop
43 #elif COMPILER_GCC
44 #pragma GCC diagnostic pop
45 #elif COMPILER_VC
46 #pragma warning(pop)
47 #endif
48#endif
49
53 vec3() {
54 x = static_cast<T>(0.0f);
55 y = static_cast<T>(0.0f);
56 z = static_cast<T>(0.0f);
57 }
58
65 vec3(const T& _x, const T& _y, const T& _z) {
66 this->x = _x;
67 this->y = _y;
68 this->z = _z;
69 }
70
75 explicit vec3(const vec4<T>& a) {
76 this->x = a.x;
77 this->y = a.y;
78 this->z = a.z;
79 }
80
86 vec3(const vec2<T>& a, const T& z) {
87 this->x = a.x;
88 this->y = a.y;
89 this->z = z;
90 }
91
96 template<typename U>
97 explicit vec3(const U& _v) {
98 this->x = static_cast<T>(_v);
99 this->y = static_cast<T>(_v);
100 this->z = static_cast<T>(_v);
101 }
106 vec3(const vec3<T>& v) {
107 x = v.x;
108 y = v.y;
109 z = v.z;
110 }
111 explicit vec3(const T* v) {
112 x = *(v++);
113 y = *(v++);
114 z = *(v++);
115 }
116
117
118 /* Misc functions */
124 inline vec3<T> cross(const vec3<T>& rhs) const {
125 return vec3<T>(
126 (float)y * rhs.z - (float)z * rhs.y, (float)z * rhs.x - (float)x * rhs.z, (float)x * rhs.y - y * rhs.x);
127 }
136 inline float length() const { return sqrtf(x * x + y * y + z * z); }
141 inline vec3<T> normalize() const {
142 float len = length();
143 return ((len != 0.0f) ? (*this / len) : *this);
144 }
145
146 /* Arithmetic operators */
153 friend vec3<T> operator+(vec3<T> lhs, const vec3<T>& rhs) { return lhs += rhs; }
160 friend vec3<T> operator-(vec3<T> lhs, const vec3<T>& rhs) { return lhs -= rhs; }
161
168 friend vec3<T> operator*(vec3<T> lhs, const T& rhs) { return lhs *= rhs; }
169
176 friend vec3<T> operator*(const T& lhs, vec3<T> rhs) { return rhs *= lhs; }
177
184 friend T operator*(vec3<T> lhs, const vec3<T>& rhs) { return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z; }
185
192 friend vec3<T> operator/(vec3<T> lhs, const T& rhs) { return lhs /= rhs; }
193
194 // element wise division
201 friend vec3<T> operator/(vec3<T> lhs, const vec3<T>& rhs) { return { lhs.x / rhs.x, lhs.y / rhs.y, lhs.z / rhs.z }; }
202
203 /* compound assignment */
210 this->x += rhs.x;
211 this->y += rhs.y;
212 this->z += rhs.z;
213 return *this;
214 }
221 this->x -= rhs.x;
222 this->y -= rhs.y;
223 this->z -= rhs.z;
224 return *this;
225 }
231 vec3<T>& operator*=(const T& rhs) {
232 this->x *= rhs;
233 this->y *= rhs;
234 this->z *= rhs;
235 return *this;
236 }
242 vec3<T>& operator/=(const T& rhs) {
243 this->x /= rhs;
244 this->y /= rhs;
245 this->z /= rhs;
246 return *this;
247 }
248
249 /* comparison operators */
256 friend bool operator==(const vec3<T>& lhs, const vec3<T>& rhs) {
257 return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z;
258 }
259
266 friend bool operator>(const vec3<T>& lhs, const vec3<T>& rhs) { return lhs.length() > rhs.length(); }
267
274 friend bool operator<(const vec3<T>& lhs, const vec3<T>& rhs) { return lhs.length() < rhs.length(); }
275
276 /* explicit type casts */
277
283 constexpr vec3<T>& operator=(vec3<T> const& V) {
284 this->x = static_cast<T>(V.x);
285 this->y = static_cast<T>(V.y);
286 this->z = static_cast<T>(V.z);
287 return *this;
288 }
289
290 /* Member access */
296 T& operator[](const int& index) {
297 switch(index) {
298 default:
299 case 0: return x;
300 case 1: return y;
301 case 2: return z;
302 }
303 }
309 const T& operator[](const int& index) const {
310 switch(index) {
311 default:
312 case 0: return x;
313 case 1: return y;
314 case 2: return z;
315 }
316 }
317
323 template<typename U>
324 explicit operator vec3<U>() {
325 return vec3<U>(x, y, z);
326 }
327
328 /* stream operators */
334 template<class U>
335 friend std::ostream& operator<<(std::ostream&, const vec3<U>&);
336};
337
338template<class T>
339std::ostream& operator<<(std::ostream& os, const vec3<T>& obj) {
340 os.precision(17);
341
342 os << obj.x << ", " << obj.y << ", " << obj.z << std::endl;
343 return os;
344}
345
346
353template<class T>
355 T* values = (T*)in;
356 vec3<T> out(values[0], values[1], values[2]);
357 return out;
358}
359
360namespace std {
365 template<typename T>
366 class numeric_limits<vec3<T>>
367 {
368 public:
373 static vec3<T> lowest() { return vec3<T>(std::numeric_limits<T>::lowest()); };
378 static vec3<T> max() { return vec3<T>(std::numeric_limits<T>::max()); };
383 static vec3<T> min() { return vec3<T>(std::numeric_limits<T>::min()); };
384 // One can implement other methods if needed
385 };
386} // namespace std
387
static vec3< T > lowest()
Definition: vec3.h:373
static vec3< T > min()
Definition: vec3.h:383
static vec3< T > max()
Definition: vec3.h:378
Definition: vec3.h:360
Definition: vec2.h:11
T y
Definition: vec2.h:29
T x
Definition: vec2.h:29
Definition: vec3.h:14
friend bool operator>(const vec3< T > &lhs, const vec3< T > &rhs)
Definition: vec3.h:266
friend bool operator==(const vec3< T > &lhs, const vec3< T > &rhs)
Definition: vec3.h:256
vec3< T > & operator+=(const vec3< T > &rhs)
Definition: vec3.h:209
const size_t dim
dimension of object
Definition: vec3.h:38
vec3(const U &_v)
Definition: vec3.h:97
T x
Definition: vec3.h:33
friend vec3< T > operator-(vec3< T > lhs, const vec3< T > &rhs)
Definition: vec3.h:160
const T & operator[](const int &index) const
Definition: vec3.h:309
vec3(const T *v)
Definition: vec3.h:111
friend T operator*(vec3< T > lhs, const vec3< T > &rhs)
Definition: vec3.h:184
friend vec3< T > operator/(vec3< T > lhs, const vec3< T > &rhs)
Definition: vec3.h:201
friend vec3< T > operator/(vec3< T > lhs, const T &rhs)
Definition: vec3.h:192
friend vec3< T > operator+(vec3< T > lhs, const vec3< T > &rhs)
Definition: vec3.h:153
T y
Definition: vec3.h:33
T z
Definition: vec3.h:33
vec3< T > normalize() const
Definition: vec3.h:141
constexpr vec3< T > & operator=(vec3< T > const &V)
Definition: vec3.h:283
T & operator[](const int &index)
Definition: vec3.h:296
vec3< T > & operator*=(const T &rhs)
Definition: vec3.h:231
vec3< T > & operator/=(const T &rhs)
Definition: vec3.h:242
vec3(const T &_x, const T &_y, const T &_z)
Definition: vec3.h:65
friend bool operator<(const vec3< T > &lhs, const vec3< T > &rhs)
Definition: vec3.h:274
vec3(const vec3< T > &v)
Definition: vec3.h:106
friend std::ostream & operator<<(std::ostream &, const vec3< U > &)
float length() const
Definition: vec3.h:136
vec3(const vec2< T > &a, const T &z)
Definition: vec3.h:86
vec3(const vec4< T > &a)
Definition: vec3.h:75
friend vec3< T > operator*(const T &lhs, vec3< T > rhs)
Definition: vec3.h:176
vec3< T > cross(const vec3< T > &rhs) const
Definition: vec3.h:124
friend vec3< T > operator*(vec3< T > lhs, const T &rhs)
Definition: vec3.h:168
vec3< T > & operator-=(const vec3< T > &rhs)
Definition: vec3.h:220
vec3()
Definition: vec3.h:53
Definition: vec4.h:11
T y
Definition: vec4.h:29
T z
Definition: vec4.h:29
T x
Definition: vec4.h:29
vec3< T > build_vec3(void *in)
Definition: vec3.h:354
std::ostream & operator<<(std::ostream &os, const vec3< T > &obj)
Definition: vec3.h:339