#pragma once #include "math.hpp" #include "vec.hpp" // // vec3 // template struct vec<3, T> { union { T val[3]; struct { T x, y, z; }; struct { T r, g, b; }; }; inline constexpr vec(); inline constexpr vec(T scalar); inline constexpr vec(T _x, T _y, T _z); constexpr inline vec<3, T> operator-() const; inline constexpr T & operator[](int i); inline constexpr T const& operator[](int i) const; inline constexpr vec<3, T>& operator=(vec<3, T> const& v); inline constexpr vec<3, T>& operator+=(vec<3, T> const& v); inline constexpr vec<3, T>& operator-=(vec<3, T> const& v); inline constexpr vec<3, T>& operator*=(T const& scalar); }; template inline constexpr vec<3, T>::vec() : x(0), y(0), z(0) {} template inline constexpr vec<3, T>::vec(T scalar) : x(scalar), y(scalar), z(scalar) {} template inline constexpr vec<3, T>::vec(T _x, T _y, T _z) : x(_x), y(_y), z(_z) {} template constexpr inline vec<3, T> vec<3, T>::operator-() const { return vec<3, T>(-x, -y, -z); } template inline constexpr T & vec<3, T>::operator[](int i) { return val[i]; } template inline constexpr T const& vec<3, T>::operator[](int i) const { return val[i]; } template inline constexpr vec<3, T>& vec<3, T>::operator=(vec<3, T> const& v) { this->x = static_cast(v.x); this->y = static_cast(v.y); this->z = static_cast(v.z); return *this; } template inline constexpr vec<3, T>& vec<3, T>::operator+=(vec<3, T> const& v) { *this = *this + vec<3, T>(v); return *this; } template inline constexpr vec<3, T>& vec<3, T>::operator-=(vec<3, T> const& v) { *this = *this - vec<3, T>(v); return *this; } template inline constexpr vec<3, T>& vec<3, T>::operator*=(T const& scalar) { *this = *this * scalar; return *this; } template inline constexpr vec<3, T> operator+(vec<3, T> const& v1, vec<3, T> const& v2) { return vec<3, T>(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z); } template inline constexpr vec<3, T> operator-(vec<3, T> const& v1, vec<3, T> const& v2) { return vec<3, T>(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z); } template inline constexpr vec<3, T> operator*(vec<3, T> const& v1, vec<3, T> const& v2) { return vec<3, T>(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z); } template inline constexpr vec<3, T> operator*(vec<3, T> const& v1, T const& scalar) { return v1 * vec<3, T>(scalar); } template inline constexpr vec<3, T> operator*(T const& scalar, vec<3, T> const& v1) { return vec<3, T>(scalar) * v1; } template inline constexpr vec<3, T> operator/(vec<3, T> const& v1, vec<3, T> const& v2) { return vec<3, T>(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z); } template inline constexpr vec<3, T> operator/(vec<3, T> const& v1, T const& scalar) { return v1 / vec<3, T>(scalar); } template inline constexpr T dot(vec<3, T> const& v1, vec<3, T> const& v2) { return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; } template inline constexpr vec<3, T> cross(vec<3, T> const& v1, vec<3, T> const& v2) { return vec<3, T>(v1.y * v2.z - v2.y * v1.z, v1.z * v2.x - v2.z * v1.x, v1.x * v2.y - v2.x * v1.y); } template inline constexpr vec<3, T> functor1(T (&func) (T const& x), vec<3, T> const& v) { return vec<3, T>(func(v.x), func(v.y), func(v.z)); } template inline constexpr vec<3, U> functor1(U (&func) (T const& x), vec<3, T> const& v) { return vec<3, U>(func(v.x), func(v.y), func(v.z)); }