/* Vector math for libostd. * * This file is part of libostd. See COPYING.md for futher information. */ #ifndef OSTD_VECMATH_HH #define OSTD_VECMATH_HH #include namespace ostd { template struct vec2 { union { struct { T x, y; }; T value[2]; }; vec2(): x(0), y(0) {} vec2(vec2 const &v): x(v.x), y(v.y) {} vec2(T v): x(v), y(v) {} vec2(T x, T y): x(x), y(y) {} T &operator[](std::size_t idx) { return value[idx]; } T operator[](std::size_t idx) const { return value[idx]; } vec2 &add(T v) { x += v; y += v; return *this; } vec2 &add(vec2 const &o) { x += o.x; y += o.y; return *this; } vec2 &sub(T v) { x -= v; y -= v; return *this; } vec2 &sub(vec2 const &o) { x -= o.x; y -= o.y; return *this; } vec2 &mul(T v) { x *= v; y *= v; return *this; } vec2 &mul(vec2 const &o) { x *= o.x; y *= o.y; return *this; } vec2 &div(T v) { x /= v; y /= v; return *this; } vec2 &div(vec2 const &o) { x /= o.x; y /= o.y; return *this; } vec2 &neg() { x = -x; y = -y; return *this; } bool is_zero() const { return (x == 0) && (y == 0); } T dot(vec2 const &o) const { return (x * o.x) + (y * o.y); } }; template inline bool operator==(vec2 const &a, vec2 const &b) { return (a.x == b.x) && (a.y == b.y); } template inline bool operator!=(vec2 const &a, vec2 const &b) { return (a.x != b.x) || (a.y != b.y); } template inline vec2 operator+(vec2 const &a, vec2 const &b) { return vec2(a).add(b); } template inline vec2 operator+(vec2 const &a, T b) { return vec2(a).add(b); } template inline vec2 operator-(vec2 const &a, vec2 const &b) { return vec2(a).sub(b); } template inline vec2 operator-(vec2 const &a, T b) { return vec2(a).sub(b); } template inline vec2 operator*(vec2 const &a, vec2 const &b) { return vec2(a).mul(b); } template inline vec2 operator*(vec2 const &a, T b) { return vec2(a).mul(b); } template inline vec2 operator/(vec2 const &a, vec2 const &b) { return vec2(a).div(b); } template inline vec2 operator/(vec2 const &a, T b) { return vec2(a).div(b); } template inline vec2 operator-(vec2 const &a) { return vec2(a).neg(); } using vec2f = vec2; using vec2d = vec2; using vec2b = vec2; using vec2i = vec2; template struct vec3 { union { struct { T x, y, z; }; struct { T r, g, b; }; T value[3]; }; vec3(): x(0), y(0), z(0) {} vec3(vec3 const &v): x(v.x), y(v.y), z(v.z) {} vec3(T v): x(v), y(v), z(v) {} vec3(T x, T y, T z): x(x), y(y), z(z) {} T &operator[](std::size_t idx) { return value[idx]; } T operator[](std::size_t idx) const { return value[idx]; } vec3 &add(T v) { x += v; y += v; z += v; return *this; } vec3 &add(vec3 const &o) { x += o.x; y += o.y; z += o.z; return *this; } vec3 &sub(T v) { x -= v; y -= v; z -= v; return *this; } vec3 &sub(vec3 const &o) { x -= o.x; y -= o.y; z -= o.z; return *this; } vec3 &mul(T v) { x *= v; y *= v; z *= v; return *this; } vec3 &mul(vec3 const &o) { x *= o.x; y *= o.y; z *= o.z; return *this; } vec3 &div(T v) { x /= v; y /= v; z /= v; return *this; } vec3 &div(vec3 const &o) { x /= o.x; y /= o.y; z /= o.z; return *this; } vec3 &neg() { x = -x; y = -y; z = -z; return *this; } bool is_zero() const { return (x == 0) && (y == 0) && (z == 0); } T dot(vec3 const &o) const { return (x * o.x) + (y * o.y) + (z * o.z); } }; template inline bool operator==(vec3 const &a, vec3 const &b) { return (a.x == b.x) && (a.y == b.y) && (a.z == b.z); } template inline bool operator!=(vec3 const &a, vec3 const &b) { return (a.x != b.x) || (a.y != b.y) || (a.z != b.z); } template inline vec3 operator+(vec3 const &a, vec3 const &b) { return vec3(a).add(b); } template inline vec3 operator+(vec3 const &a, T b) { return vec3(a).add(b); } template inline vec3 operator-(vec3 const &a, vec3 const &b) { return vec3(a).sub(b); } template inline vec3 operator-(vec3 const &a, T b) { return vec3(a).sub(b); } template inline vec3 operator*(vec3 const &a, vec3 const &b) { return vec3(a).mul(b); } template inline vec3 operator*(vec3 const &a, T b) { return vec3(a).mul(b); } template inline vec3 operator/(vec3 const &a, vec3 const &b) { return vec3(a).div(b); } template inline vec3 operator/(vec3 const &a, T b) { return vec3(a).div(b); } template inline vec3 operator-(vec3 const &a) { return vec3(a).neg(); } using vec3f = vec3; using vec3d = vec3; using vec3b = vec3; using vec3i = vec3; template struct vec4 { union { struct { T x, y, z, w; }; struct { T r, g, b, a; }; T value[4]; }; vec4(): x(0), y(0), z(0), w(0) {} vec4(vec4 const &v): x(v.x), y(v.y), z(v.z), w(v.w) {} vec4(T v): x(v), y(v), z(v), w(v) {} vec4(T x, T y, T z, T w): x(x), y(y), z(z), w(w) {} T &operator[](std::size_t idx) { return value[idx]; } T operator[](std::size_t idx) const { return value[idx]; } vec4 &add(T v) { x += v; y += v; z += v; w += v; return *this; } vec4 &add(vec4 const &o) { x += o.x; y += o.y; z += o.z; w += o.w; return *this; } vec4 &sub(T v) { x -= v; y -= v; z -= v; w -= v; return *this; } vec4 &sub(vec4 const &o) { x -= o.x; y -= o.y; z -= o.z; w -= o.w; return *this; } vec4 &mul(T v) { x *= v; y *= v; z *= v; w *= v; return *this; } vec4 &mul(vec4 const &o) { x *= o.x; y *= o.y; z *= o.z; w *= o.w; return *this; } vec4 &div(T v) { x /= v; y /= v; z /= v; w /= v; return *this; } vec4 &div(vec4 const &o) { x /= o.x; y /= o.y; z /= o.z; w /= o.w; return *this; } vec4 &neg() { x = -x; y = -y; z = -z; w = -w; return *this; } bool is_zero() const { return (x == 0) && (y == 0) && (z == 0) && (w == 0); } T dot(vec4 const &o) const { return (x * o.x) + (y * o.y) + (z * o.z) + (w * o.w); } }; template inline bool operator==(vec4 const &a, vec4 const &b) { return (a.x == b.x) && (a.y == b.y) && (a.z == b.z) && (a.w == b.w); } template inline bool operator!=(vec4 const &a, vec4 const &b) { return (a.x != b.x) || (a.y != b.y) || (a.z != b.z) || (a.w != b.w); } template inline vec4 operator+(vec4 const &a, vec4 const &b) { return vec4(a).add(b); } template inline vec4 operator+(vec4 const &a, T b) { return vec4(a).add(b); } template inline vec4 operator-(vec4 const &a, vec4 const &b) { return vec4(a).sub(b); } template inline vec4 operator-(vec4 const &a, T b) { return vec4(a).sub(b); } template inline vec4 operator*(vec4 const &a, vec4 const &b) { return vec4(a).mul(b); } template inline vec4 operator*(vec4 const &a, T b) { return vec4(a).mul(b); } template inline vec4 operator/(vec4 const &a, vec4 const &b) { return vec4(a).div(b); } template inline vec4 operator/(vec4 const &a, T b) { return vec4(a).div(b); } template inline vec4 operator-(vec4 const &a) { return vec4(a).neg(); } using vec4f = vec4; using vec4d = vec4; using vec4b = vec4; using vec4i = vec4; } /* namespace ostd */ #endif