/* Vector math for OctaSTD. * * This file is part of OctaSTD. See COPYING.md for futher information. */ #ifndef OSTD_VECMATH_HH #define OSTD_VECMATH_HH #include "ostd/types.hh" 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[](size_t idx) { return value[idx]; } T operator[](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[](size_t idx) { return value[idx]; } T operator[](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[](size_t idx) { return value[idx]; } T operator[](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