// Licensed under GPLv2 or any later version // Refer to the license.txt file included. // Copyright 2014 Tony Wasserka // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in the // documentation and/or other materials provided with the distribution. // * Neither the name of the owner nor the names of its contributors may // be used to endorse or promote products derived from this software // without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #pragma once #include #include #include namespace Common { template class Vec2; template class Vec3; template class Vec4; template class Vec2 { friend class boost::serialization::access; template void serialize(Archive & ar, const unsigned int file_version) { ar & x; ar & y; } public: T x; T y; T* AsArray() { return &x; } constexpr Vec2() = default; constexpr Vec2(const T& x_, const T& y_) : x(x_), y(y_) {} template constexpr Vec2 Cast() const { return Vec2(static_cast(x), static_cast(y)); } static constexpr Vec2 AssignToAll(const T& f) { return Vec2{f, f}; } constexpr Vec2 operator+(const Vec2& other) const { return {x + other.x, y + other.y}; } constexpr Vec2& operator+=(const Vec2& other) { x += other.x; y += other.y; return *this; } constexpr Vec2 operator-(const Vec2& other) const { return {x - other.x, y - other.y}; } constexpr Vec2& operator-=(const Vec2& other) { x -= other.x; y -= other.y; return *this; } template constexpr Vec2, U>> operator-() const { return {-x, -y}; } constexpr Vec2 operator*(const Vec2& other) const { return {x * other.x, y * other.y}; } template constexpr Vec2 operator*(const V& f) const { return {x * f, y * f}; } template constexpr Vec2& operator*=(const V& f) { *this = *this * f; return *this; } template constexpr Vec2 operator/(const V& f) const { return {x / f, y / f}; } template constexpr Vec2& operator/=(const V& f) { *this = *this / f; return *this; } constexpr T Length2() const { return x * x + y * y; } // Only implemented for T=float float Length() const; float Normalize(); // returns the previous length, which is often useful constexpr T& operator[](std::size_t i) { return *((&x) + i); } constexpr const T& operator[](std::size_t i) const { return *((&x) + i); } constexpr void SetZero() { x = 0; y = 0; } // Common aliases: UV (texel coordinates), ST (texture coordinates) constexpr T& u() { return x; } constexpr T& v() { return y; } constexpr T& s() { return x; } constexpr T& t() { return y; } constexpr const T& u() const { return x; } constexpr const T& v() const { return y; } constexpr const T& s() const { return x; } constexpr const T& t() const { return y; } // swizzlers - create a subvector of specific components constexpr Vec2 yx() const { return Vec2(y, x); } constexpr Vec2 vu() const { return Vec2(y, x); } constexpr Vec2 ts() const { return Vec2(y, x); } }; template constexpr Vec2 operator*(const V& f, const Vec2& vec) { return Vec2(f * vec.x, f * vec.y); } using Vec2f = Vec2; template <> inline float Vec2::Length() const { return std::sqrt(x * x + y * y); } template <> inline float Vec2::Normalize() { float length = Length(); *this /= length; return length; } template class Vec3 { friend class boost::serialization::access; template void serialize(Archive & ar, const unsigned int file_version) { ar & x; ar & y; ar & z; } public: T x; T y; T z; T* AsArray() { return &x; } constexpr Vec3() = default; constexpr Vec3(const T& x_, const T& y_, const T& z_) : x(x_), y(y_), z(z_) {} template constexpr Vec3 Cast() const { return Vec3(static_cast(x), static_cast(y), static_cast(z)); } static constexpr Vec3 AssignToAll(const T& f) { return Vec3(f, f, f); } constexpr Vec3 operator+(const Vec3& other) const { return {x + other.x, y + other.y, z + other.z}; } constexpr Vec3& operator+=(const Vec3& other) { x += other.x; y += other.y; z += other.z; return *this; } constexpr Vec3 operator-(const Vec3& other) const { return {x - other.x, y - other.y, z - other.z}; } constexpr Vec3& operator-=(const Vec3& other) { x -= other.x; y -= other.y; z -= other.z; return *this; } template constexpr Vec3, U>> operator-() const { return {-x, -y, -z}; } constexpr Vec3 operator*(const Vec3& other) const { return {x * other.x, y * other.y, z * other.z}; } template constexpr Vec3 operator*(const V& f) const { return {x * f, y * f, z * f}; } template constexpr Vec3& operator*=(const V& f) { *this = *this * f; return *this; } template constexpr Vec3 operator/(const V& f) const { return {x / f, y / f, z / f}; } template constexpr Vec3& operator/=(const V& f) { *this = *this / f; return *this; } constexpr T Length2() const { return x * x + y * y + z * z; } // Only implemented for T=float float Length() const; Vec3 Normalized() const; float Normalize(); // returns the previous length, which is often useful constexpr T& operator[](std::size_t i) { return *((&x) + i); } constexpr const T& operator[](std::size_t i) const { return *((&x) + i); } constexpr void SetZero() { x = 0; y = 0; z = 0; } // Common aliases: UVW (texel coordinates), RGB (colors), STQ (texture coordinates) constexpr T& u() { return x; } constexpr T& v() { return y; } constexpr T& w() { return z; } constexpr T& r() { return x; } constexpr T& g() { return y; } constexpr T& b() { return z; } constexpr T& s() { return x; } constexpr T& t() { return y; } constexpr T& q() { return z; } constexpr const T& u() const { return x; } constexpr const T& v() const { return y; } constexpr const T& w() const { return z; } constexpr const T& r() const { return x; } constexpr const T& g() const { return y; } constexpr const T& b() const { return z; } constexpr const T& s() const { return x; } constexpr const T& t() const { return y; } constexpr const T& q() const { return z; } // swizzlers - create a subvector of specific components // e.g. Vec2 uv() { return Vec2(x,y); } // _DEFINE_SWIZZLER2 defines a single such function, DEFINE_SWIZZLER2 defines all of them for all // component names (x<->r) and permutations (xy<->yx) #define _DEFINE_SWIZZLER2(a, b, name) \ constexpr Vec2 name() const { \ return Vec2(a, b); \ } #define DEFINE_SWIZZLER2(a, b, a2, b2, a3, b3, a4, b4) \ _DEFINE_SWIZZLER2(a, b, a##b); \ _DEFINE_SWIZZLER2(a, b, a2##b2); \ _DEFINE_SWIZZLER2(a, b, a3##b3); \ _DEFINE_SWIZZLER2(a, b, a4##b4); \ _DEFINE_SWIZZLER2(b, a, b##a); \ _DEFINE_SWIZZLER2(b, a, b2##a2); \ _DEFINE_SWIZZLER2(b, a, b3##a3); \ _DEFINE_SWIZZLER2(b, a, b4##a4) DEFINE_SWIZZLER2(x, y, r, g, u, v, s, t); DEFINE_SWIZZLER2(x, z, r, b, u, w, s, q); DEFINE_SWIZZLER2(y, z, g, b, v, w, t, q); #undef DEFINE_SWIZZLER2 #undef _DEFINE_SWIZZLER2 }; template constexpr Vec3 operator*(const V& f, const Vec3& vec) { return Vec3(f * vec.x, f * vec.y, f * vec.z); } template <> inline float Vec3::Length() const { return std::sqrt(x * x + y * y + z * z); } template <> inline Vec3 Vec3::Normalized() const { return *this / Length(); } template <> inline float Vec3::Normalize() { float length = Length(); *this /= length; return length; } using Vec3f = Vec3; template class Vec4 { friend class boost::serialization::access; template void serialize(Archive & ar, const unsigned int file_version) { ar & x; ar & y; ar & z; ar & w; } public: T x; T y; T z; T w; T* AsArray() { return &x; } constexpr Vec4() = default; constexpr Vec4(const T& x_, const T& y_, const T& z_, const T& w_) : x(x_), y(y_), z(z_), w(w_) {} template constexpr Vec4 Cast() const { return Vec4(static_cast(x), static_cast(y), static_cast(z), static_cast(w)); } static constexpr Vec4 AssignToAll(const T& f) { return Vec4(f, f, f, f); } constexpr Vec4 operator+(const Vec4& other) const { return {x + other.x, y + other.y, z + other.z, w + other.w}; } constexpr Vec4& operator+=(const Vec4& other) { x += other.x; y += other.y; z += other.z; w += other.w; return *this; } constexpr Vec4 operator-(const Vec4& other) const { return {x - other.x, y - other.y, z - other.z, w - other.w}; } constexpr Vec4& operator-=(const Vec4& other) { x -= other.x; y -= other.y; z -= other.z; w -= other.w; return *this; } template constexpr Vec4, U>> operator-() const { return {-x, -y, -z, -w}; } constexpr Vec4 operator*(const Vec4& other) const { return {x * other.x, y * other.y, z * other.z, w * other.w}; } template constexpr Vec4 operator*(const V& f) const { return {x * f, y * f, z * f, w * f}; } template constexpr Vec4& operator*=(const V& f) { *this = *this * f; return *this; } template constexpr Vec4 operator/(const V& f) const { return {x / f, y / f, z / f, w / f}; } template constexpr Vec4& operator/=(const V& f) { *this = *this / f; return *this; } constexpr T Length2() const { return x * x + y * y + z * z + w * w; } constexpr T& operator[](std::size_t i) { return *((&x) + i); } constexpr const T& operator[](std::size_t i) const { return *((&x) + i); } constexpr void SetZero() { x = 0; y = 0; z = 0; w = 0; } // Common alias: RGBA (colors) constexpr T& r() { return x; } constexpr T& g() { return y; } constexpr T& b() { return z; } constexpr T& a() { return w; } constexpr const T& r() const { return x; } constexpr const T& g() const { return y; } constexpr const T& b() const { return z; } constexpr const T& a() const { return w; } // Swizzlers - Create a subvector of specific components // e.g. Vec2 uv() { return Vec2(x,y); } // _DEFINE_SWIZZLER2 defines a single such function // DEFINE_SWIZZLER2_COMP1 defines one-component functions for all component names (x<->r) // DEFINE_SWIZZLER2_COMP2 defines two component functions for all component names (x<->r) and // permutations (xy<->yx) #define _DEFINE_SWIZZLER2(a, b, name) \ constexpr Vec2 name() const { \ return Vec2(a, b); \ } #define DEFINE_SWIZZLER2_COMP1(a, a2) \ _DEFINE_SWIZZLER2(a, a, a##a); \ _DEFINE_SWIZZLER2(a, a, a2##a2) #define DEFINE_SWIZZLER2_COMP2(a, b, a2, b2) \ _DEFINE_SWIZZLER2(a, b, a##b); \ _DEFINE_SWIZZLER2(a, b, a2##b2); \ _DEFINE_SWIZZLER2(b, a, b##a); \ _DEFINE_SWIZZLER2(b, a, b2##a2) DEFINE_SWIZZLER2_COMP2(x, y, r, g); DEFINE_SWIZZLER2_COMP2(x, z, r, b); DEFINE_SWIZZLER2_COMP2(x, w, r, a); DEFINE_SWIZZLER2_COMP2(y, z, g, b); DEFINE_SWIZZLER2_COMP2(y, w, g, a); DEFINE_SWIZZLER2_COMP2(z, w, b, a); DEFINE_SWIZZLER2_COMP1(x, r); DEFINE_SWIZZLER2_COMP1(y, g); DEFINE_SWIZZLER2_COMP1(z, b); DEFINE_SWIZZLER2_COMP1(w, a); #undef DEFINE_SWIZZLER2_COMP1 #undef DEFINE_SWIZZLER2_COMP2 #undef _DEFINE_SWIZZLER2 #define _DEFINE_SWIZZLER3(a, b, c, name) \ constexpr Vec3 name() const { \ return Vec3(a, b, c); \ } #define DEFINE_SWIZZLER3_COMP1(a, a2) \ _DEFINE_SWIZZLER3(a, a, a, a##a##a); \ _DEFINE_SWIZZLER3(a, a, a, a2##a2##a2) #define DEFINE_SWIZZLER3_COMP3(a, b, c, a2, b2, c2) \ _DEFINE_SWIZZLER3(a, b, c, a##b##c); \ _DEFINE_SWIZZLER3(a, c, b, a##c##b); \ _DEFINE_SWIZZLER3(b, a, c, b##a##c); \ _DEFINE_SWIZZLER3(b, c, a, b##c##a); \ _DEFINE_SWIZZLER3(c, a, b, c##a##b); \ _DEFINE_SWIZZLER3(c, b, a, c##b##a); \ _DEFINE_SWIZZLER3(a, b, c, a2##b2##c2); \ _DEFINE_SWIZZLER3(a, c, b, a2##c2##b2); \ _DEFINE_SWIZZLER3(b, a, c, b2##a2##c2); \ _DEFINE_SWIZZLER3(b, c, a, b2##c2##a2); \ _DEFINE_SWIZZLER3(c, a, b, c2##a2##b2); \ _DEFINE_SWIZZLER3(c, b, a, c2##b2##a2) DEFINE_SWIZZLER3_COMP3(x, y, z, r, g, b); DEFINE_SWIZZLER3_COMP3(x, y, w, r, g, a); DEFINE_SWIZZLER3_COMP3(x, z, w, r, b, a); DEFINE_SWIZZLER3_COMP3(y, z, w, g, b, a); DEFINE_SWIZZLER3_COMP1(x, r); DEFINE_SWIZZLER3_COMP1(y, g); DEFINE_SWIZZLER3_COMP1(z, b); DEFINE_SWIZZLER3_COMP1(w, a); #undef DEFINE_SWIZZLER3_COMP1 #undef DEFINE_SWIZZLER3_COMP3 #undef _DEFINE_SWIZZLER3 }; template constexpr Vec4 operator*(const V& f, const Vec4& vec) { return {f * vec.x, f * vec.y, f * vec.z, f * vec.w}; } using Vec4f = Vec4; template constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec2& a, const Vec2& b) { return a.x * b.x + a.y * b.y; } template constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec3& a, const Vec3& b) { return a.x * b.x + a.y * b.y + a.z * b.z; } template constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec4& a, const Vec4& b) { return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; } template constexpr Vec3 Cross(const Vec3& a, const Vec3& b) { return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}; } // linear interpolation via float: 0.0=begin, 1.0=end template constexpr decltype(X{} * float{} + X{} * float{}) Lerp(const X& begin, const X& end, const float t) { return begin * (1.f - t) + end * t; } // linear interpolation via int: 0=begin, base=end template constexpr decltype((X{} * int{} + X{} * int{}) / base) LerpInt(const X& begin, const X& end, const int t) { return (begin * (base - t) + end * t) / base; } // bilinear interpolation. s is for interpolating x00-x01 and x10-x11, and t is for the second // interpolation. template constexpr auto BilinearInterp(const X& x00, const X& x01, const X& x10, const X& x11, const float s, const float t) { auto y0 = Lerp(x00, x01, s); auto y1 = Lerp(x10, x11, s); return Lerp(y0, y1, t); } // Utility vector factories template constexpr Vec2 MakeVec(const T& x, const T& y) { return Vec2{x, y}; } template constexpr Vec3 MakeVec(const T& x, const T& y, const T& z) { return Vec3{x, y, z}; } template constexpr Vec4 MakeVec(const T& x, const T& y, const Vec2& zw) { return MakeVec(x, y, zw[0], zw[1]); } template constexpr Vec3 MakeVec(const Vec2& xy, const T& z) { return MakeVec(xy[0], xy[1], z); } template constexpr Vec3 MakeVec(const T& x, const Vec2& yz) { return MakeVec(x, yz[0], yz[1]); } template constexpr Vec4 MakeVec(const T& x, const T& y, const T& z, const T& w) { return Vec4{x, y, z, w}; } template constexpr Vec4 MakeVec(const Vec2& xy, const T& z, const T& w) { return MakeVec(xy[0], xy[1], z, w); } template constexpr Vec4 MakeVec(const T& x, const Vec2& yz, const T& w) { return MakeVec(x, yz[0], yz[1], w); } // NOTE: This has priority over "Vec2> MakeVec(const Vec2& x, const Vec2& y)". // Even if someone wanted to use an odd object like Vec2>, the compiler would error // out soon enough due to misuse of the returned structure. template constexpr Vec4 MakeVec(const Vec2& xy, const Vec2& zw) { return MakeVec(xy[0], xy[1], zw[0], zw[1]); } template constexpr Vec4 MakeVec(const Vec3& xyz, const T& w) { return MakeVec(xyz[0], xyz[1], xyz[2], w); } template constexpr Vec4 MakeVec(const T& x, const Vec3& yzw) { return MakeVec(x, yzw[0], yzw[1], yzw[2]); } } // namespace Common