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 template<typename U>
112 explicit vec3(const vec3<U>& v) {
113 x = static_cast<T>(v.x);
114 y = static_cast<T>(v.y);
115 z = static_cast<T>(v.z);
116 }
117 explicit vec3(const T* v) {
118 x = *(v++);
119 y = *(v++);
120 z = *(v++);
121 }
122
123
124 /* Misc functions */
130 inline vec3<T> cross(const vec3<T>& rhs) const {
131 return vec3<T>(
132 (float)y * rhs.z - (float)z * rhs.y, (float)z * rhs.x - (float)x * rhs.z, (float)x * rhs.y - y * rhs.x);
133 }
142 inline float length() const { return sqrtf(x * x + y * y + z * z); }
147 inline vec3<T> normalize() const {
148 float len = length();
149 return ((len != 0.0f) ? (*this / len) : *this);
150 }
151
152 /* Arithmetic operators */
159 friend vec3<T> operator+(vec3<T> lhs, const vec3<T>& rhs) { return lhs += rhs; }
166 friend vec3<T> operator-(vec3<T> lhs, const vec3<T>& rhs) { return lhs -= rhs; }
167
174 friend vec3<T> operator*(vec3<T> lhs, const T& rhs) { return lhs *= rhs; }
175
182 friend vec3<T> operator*(const T& lhs, vec3<T> rhs) { return rhs *= lhs; }
183
190 friend T operator*(vec3<T> lhs, const vec3<T>& rhs) { return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z; }
191
198 friend vec3<T> operator/(vec3<T> lhs, const T& rhs) { return lhs /= rhs; }
199
200 // element wise division
207 friend vec3<T> operator/(vec3<T> lhs, const vec3<T>& rhs) { return { lhs.x / rhs.x, lhs.y / rhs.y, lhs.z / rhs.z }; }
208
209 /* compound assignment */
216 this->x += rhs.x;
217 this->y += rhs.y;
218 this->z += rhs.z;
219 return *this;
220 }
227 this->x -= rhs.x;
228 this->y -= rhs.y;
229 this->z -= rhs.z;
230 return *this;
231 }
238 this->x *= rhs;
239 this->y *= rhs;
240 this->z *= rhs;
241 return *this;
242 }
249 this->x /= rhs;
250 this->y /= rhs;
251 this->z /= rhs;
252 return *this;
253 }
254
255 /* comparison operators */
262 friend bool operator==(const vec3<T>& lhs, const vec3<T>& rhs) {
263 return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z;
264 }
265
272 friend bool operator>(const vec3<T>& lhs, const vec3<T>& rhs) { return lhs.length() > rhs.length(); }
273
280 friend bool operator<(const vec3<T>& lhs, const vec3<T>& rhs) { return lhs.length() < rhs.length(); }
281
282 /* explicit type casts */
283
289 constexpr vec3<T>& operator=(vec3<T> const& V) {
290 this->x = static_cast<T>(V.x);
291 this->y = static_cast<T>(V.y);
292 this->z = static_cast<T>(V.z);
293 return *this;
294 }
295
296 /* Member access */
302 T& operator[](const int& index) {
303 switch(index) {
304 default:
305 case 0: return x;
306 case 1: return y;
307 case 2: return z;
308 }
309 }
315 const T& operator[](const int& index) const {
316 switch(index) {
317 default:
318 case 0: return x;
319 case 1: return y;
320 case 2: return z;
321 }
322 }
323
329 template<typename U>
330 explicit operator vec3<U>() {
331 return vec3<U>(x, y, z);
332 }
333
334 /* stream operators */
340 template<class U>
341 friend std::ostream& operator<<(std::ostream&, const vec3<U>&);
342};
343
344template<class T>
345std::ostream& operator<<(std::ostream& os, const vec3<T>& obj) {
346 os.precision(17);
347
348 os << obj.x << ", " << obj.y << ", " << obj.z << std::endl;
349 return os;
350}
351
352
359template<class T>
361 T* values = (T*)in;
362 vec3<T> out(values[0], values[1], values[2]);
363 return out;
364}
365
366namespace std {
371 template<typename T>
373 {
374 public:
379 static vec3<T> lowest() { return vec3<T>(std::numeric_limits<T>::lowest()); };
384 static vec3<T> max() { return vec3<T>(std::numeric_limits<T>::max()); };
389 static vec3<T> min() { return vec3<T>(std::numeric_limits<T>::min()); };
390 // One can implement other methods if needed
391 };
392} // namespace std
393
Definition Matrix.h:42
static vec3< T > lowest()
Definition vec3.h:379
static vec3< T > min()
Definition vec3.h:389
static vec3< T > max()
Definition vec3.h:384
Definition vec3.h:366
Definition vec3.h:14
friend bool operator>(const vec3< T > &lhs, const vec3< T > &rhs)
Definition vec3.h:272
friend bool operator==(const vec3< T > &lhs, const vec3< T > &rhs)
Definition vec3.h:262
vec3< T > & operator+=(const vec3< T > &rhs)
Definition vec3.h:215
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:166
const T & operator[](const int &index) const
Definition vec3.h:315
vec3(const T *v)
Definition vec3.h:117
friend T operator*(vec3< T > lhs, const vec3< T > &rhs)
Definition vec3.h:190
friend vec3< T > operator/(vec3< T > lhs, const vec3< T > &rhs)
Definition vec3.h:207
friend vec3< T > operator/(vec3< T > lhs, const T &rhs)
Definition vec3.h:198
friend vec3< T > operator+(vec3< T > lhs, const vec3< T > &rhs)
Definition vec3.h:159
T y
Definition vec3.h:33
T z
Definition vec3.h:33
vec3< T > normalize() const
Definition vec3.h:147
constexpr vec3< T > & operator=(vec3< T > const &V)
Definition vec3.h:289
T & operator[](const int &index)
Definition vec3.h:302
vec3< T > & operator*=(const T &rhs)
Definition vec3.h:237
vec3< T > & operator/=(const T &rhs)
Definition vec3.h:248
vec3(const T &_x, const T &_y, const T &_z)
Definition vec3.h:65
vec3(const vec3< U > &v)
Definition vec3.h:112
friend bool operator<(const vec3< T > &lhs, const vec3< T > &rhs)
Definition vec3.h:280
vec3(const vec3< T > &v)
Definition vec3.h:106
friend std::ostream & operator<<(std::ostream &, const vec3< U > &)
float length() const
Definition vec3.h:142
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:182
vec3< T > cross(const vec3< T > &rhs) const
Definition vec3.h:130
friend vec3< T > operator*(vec3< T > lhs, const T &rhs)
Definition vec3.h:174
vec3< T > & operator-=(const vec3< T > &rhs)
Definition vec3.h:226
vec3()
Definition vec3.h:53
vec3< T > build_vec3(void *in)
Definition vec3.h:360
std::ostream & operator<<(std::ostream &os, const vec3< T > &obj)
Definition vec3.h:345