libostd/ostd/vecmath.hh

393 lines
8.3 KiB
C++
Raw Normal View History

/* 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<typename T>
struct Vec2 {
union {
struct { T x, y; };
T value[2];
};
Vec2(): x(0), y(0) {}
2016-06-23 20:18:35 +02:00
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) {}
2017-01-30 19:11:39 +01:00
T &operator[](size_t idx) { return value[idx]; }
T operator[](size_t idx) const { return value[idx]; }
2015-07-26 16:05:12 +02:00
Vec2 &add(T v) {
x += v; y += v;
return *this;
}
2016-06-23 20:18:35 +02:00
Vec2 &add(Vec2 const &o) {
2015-07-26 16:05:12 +02:00
x += o.x; y += o.y;
return *this;
}
Vec2 &sub(T v) {
x -= v; y -= v;
return *this;
}
2016-06-23 20:18:35 +02:00
Vec2 &sub(Vec2 const &o) {
2015-07-26 16:05:12 +02:00
x -= o.x; y -= o.y;
return *this;
}
Vec2 &mul(T v) {
x *= v; y *= v;
return *this;
}
2016-06-23 20:18:35 +02:00
Vec2 &mul(Vec2 const &o) {
2015-07-26 16:05:12 +02:00
x *= o.x; y *= o.y;
return *this;
}
Vec2 &div(T v) {
x /= v; y /= v;
return *this;
}
2016-06-23 20:18:35 +02:00
Vec2 &div(Vec2 const &o) {
2015-07-26 16:05:12 +02:00
x /= o.x; y /= o.y;
return *this;
}
2015-07-26 16:18:41 +02:00
Vec2 &neg() {
x = -x; y = -y;
return *this;
}
bool is_zero() const {
return (x == 0) && (y == 0);
}
2016-06-23 20:18:35 +02:00
T dot(Vec2<T> const &o) const {
2015-07-26 16:18:41 +02:00
return (x * o.x) + (y * o.y);
}
};
template<typename T>
2016-06-23 20:18:35 +02:00
inline bool operator==(Vec2<T> const &a, Vec2<T> const &b) {
return (a.x == b.x) && (a.y == b.y);
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline bool operator!=(Vec2<T> const &a, Vec2<T> const &b) {
return (a.x != b.x) || (a.y != b.y);
}
2015-07-26 16:05:12 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec2<T> operator+(Vec2<T> const &a, Vec2<T> const &b) {
2015-07-26 16:05:12 +02:00
return Vec2<T>(a).add(b);
}
2015-07-26 16:26:52 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec2<T> operator+(Vec2<T> const &a, T b) {
2015-07-26 16:26:52 +02:00
return Vec2<T>(a).add(b);
}
2015-07-26 16:05:12 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec2<T> operator-(Vec2<T> const &a, Vec2<T> const &b) {
2015-07-26 16:05:12 +02:00
return Vec2<T>(a).sub(b);
}
2015-07-26 16:26:52 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec2<T> operator-(Vec2<T> const &a, T b) {
2015-07-26 16:26:52 +02:00
return Vec2<T>(a).sub(b);
}
2015-07-26 16:05:12 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec2<T> operator*(Vec2<T> const &a, Vec2<T> const &b) {
2015-07-26 16:05:12 +02:00
return Vec2<T>(a).mul(b);
}
2015-07-26 16:26:52 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec2<T> operator*(Vec2<T> const &a, T b) {
2015-07-26 16:26:52 +02:00
return Vec2<T>(a).mul(b);
}
2015-07-26 16:05:12 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec2<T> operator/(Vec2<T> const &a, Vec2<T> const &b) {
2015-07-26 16:05:12 +02:00
return Vec2<T>(a).div(b);
}
2015-07-26 16:26:52 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec2<T> operator/(Vec2<T> const &a, T b) {
2015-07-26 16:26:52 +02:00
return Vec2<T>(a).div(b);
}
2015-07-26 16:18:41 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec2<T> operator-(Vec2<T> const &a) {
2015-07-26 16:18:41 +02:00
return Vec2<T>(a).neg();
}
using Vec2f = Vec2<float>;
using Vec2d = Vec2<double>;
using Vec2b = Vec2<byte>;
using Vec2i = Vec2<int>;
template<typename T>
struct Vec3 {
union {
struct { T x, y, z; };
struct { T r, g, b; };
T value[3];
};
Vec3(): x(0), y(0), z(0) {}
2016-06-23 20:18:35 +02:00
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) {}
2015-07-26 16:05:12 +02:00
2017-01-30 19:11:39 +01:00
T &operator[](size_t idx) { return value[idx]; }
T operator[](size_t idx) const { return value[idx]; }
2015-07-26 16:05:12 +02:00
Vec3 &add(T v) {
x += v; y += v; z += v;
return *this;
}
2016-06-23 20:18:35 +02:00
Vec3 &add(Vec3 const &o) {
2015-07-26 16:05:12 +02:00
x += o.x; y += o.y; z += o.z;
return *this;
}
Vec3 &sub(T v) {
x -= v; y -= v; z -= v;
return *this;
}
2016-06-23 20:18:35 +02:00
Vec3 &sub(Vec3 const &o) {
2015-07-26 16:05:12 +02:00
x -= o.x; y -= o.y; z -= o.z;
return *this;
}
Vec3 &mul(T v) {
x *= v; y *= v; z *= v;
return *this;
}
2016-06-23 20:18:35 +02:00
Vec3 &mul(Vec3 const &o) {
2015-07-26 16:05:12 +02:00
x *= o.x; y *= o.y; z *= o.z;
return *this;
}
Vec3 &div(T v) {
x /= v; y /= v; z /= v;
return *this;
}
2016-06-23 20:18:35 +02:00
Vec3 &div(Vec3 const &o) {
2015-07-26 16:05:12 +02:00
x /= o.x; y /= o.y; z /= o.z;
return *this;
}
2015-07-26 16:18:41 +02:00
Vec3 &neg() {
x = -x; y = -y; z = -z;
return *this;
}
bool is_zero() const {
return (x == 0) && (y == 0) && (z == 0);
}
2016-06-23 20:18:35 +02:00
T dot(Vec3<T> const &o) const {
2015-07-26 16:18:41 +02:00
return (x * o.x) + (y * o.y) + (z * o.z);
}
};
template<typename T>
2016-06-23 20:18:35 +02:00
inline bool operator==(Vec3<T> const &a, Vec3<T> const &b) {
return (a.x == b.x) && (a.y == b.y) && (a.z == b.z);
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline bool operator!=(Vec3<T> const &a, Vec3<T> const &b) {
return (a.x != b.x) || (a.y != b.y) || (a.z != b.z);
}
2015-07-26 16:05:12 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec3<T> operator+(Vec3<T> const &a, Vec3<T> const &b) {
2015-07-26 16:05:12 +02:00
return Vec3<T>(a).add(b);
}
2015-07-26 16:26:52 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec3<T> operator+(Vec3<T> const &a, T b) {
2015-07-26 16:26:52 +02:00
return Vec3<T>(a).add(b);
}
2015-07-26 16:05:12 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec3<T> operator-(Vec3<T> const &a, Vec3<T> const &b) {
2015-07-26 16:05:12 +02:00
return Vec3<T>(a).sub(b);
}
2015-07-26 16:26:52 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec3<T> operator-(Vec3<T> const &a, T b) {
2015-07-26 16:26:52 +02:00
return Vec3<T>(a).sub(b);
}
2015-07-26 16:05:12 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec3<T> operator*(Vec3<T> const &a, Vec3<T> const &b) {
2015-07-26 16:05:12 +02:00
return Vec3<T>(a).mul(b);
}
2015-07-26 16:26:52 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec3<T> operator*(Vec3<T> const &a, T b) {
2015-07-26 16:26:52 +02:00
return Vec3<T>(a).mul(b);
}
2015-07-26 16:05:12 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec3<T> operator/(Vec3<T> const &a, Vec3<T> const &b) {
2015-07-26 16:05:12 +02:00
return Vec3<T>(a).div(b);
}
2015-07-26 16:26:52 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec3<T> operator/(Vec3<T> const &a, T b) {
2015-07-26 16:26:52 +02:00
return Vec3<T>(a).div(b);
}
2015-07-26 16:18:41 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec3<T> operator-(Vec3<T> const &a) {
2015-07-26 16:18:41 +02:00
return Vec3<T>(a).neg();
}
using Vec3f = Vec3<float>;
using Vec3d = Vec3<double>;
using Vec3b = Vec3<byte>;
using Vec3i = Vec3<int>;
template<typename T>
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) {}
2016-06-23 20:18:35 +02:00
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) {}
2015-07-26 16:05:12 +02:00
2017-01-30 19:11:39 +01:00
T &operator[](size_t idx) { return value[idx]; }
T operator[](size_t idx) const { return value[idx]; }
2015-07-26 16:05:12 +02:00
Vec4 &add(T v) {
x += v; y += v; z += v; w += v;
return *this;
}
2016-06-23 20:18:35 +02:00
Vec4 &add(Vec4 const &o) {
2015-07-26 16:05:12 +02:00
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;
}
2016-06-23 20:18:35 +02:00
Vec4 &sub(Vec4 const &o) {
2015-07-26 16:05:12 +02:00
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;
}
2016-06-23 20:18:35 +02:00
Vec4 &mul(Vec4 const &o) {
2015-07-26 16:05:12 +02:00
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;
}
2016-06-23 20:18:35 +02:00
Vec4 &div(Vec4 const &o) {
2015-07-26 16:05:12 +02:00
x /= o.x; y /= o.y; z /= o.z; w /= o.w;
return *this;
}
2015-07-26 16:18:41 +02:00
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);
}
2016-06-23 20:18:35 +02:00
T dot(Vec4<T> const &o) const {
2015-07-26 16:18:41 +02:00
return (x * o.x) + (y * o.y) + (z * o.z) + (w * o.w);
}
};
template<typename T>
2016-06-23 20:18:35 +02:00
inline bool operator==(Vec4<T> const &a, Vec4<T> const &b) {
return (a.x == b.x) && (a.y == b.y) && (a.z == b.z) && (a.w == b.w);
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline bool operator!=(Vec4<T> const &a, Vec4<T> const &b) {
return (a.x != b.x) || (a.y != b.y) || (a.z != b.z) || (a.w != b.w);
}
2015-07-26 16:05:12 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec4<T> operator+(Vec4<T> const &a, Vec4<T> const &b) {
2015-07-26 16:05:12 +02:00
return Vec4<T>(a).add(b);
}
2015-07-26 16:26:52 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec4<T> operator+(Vec4<T> const &a, T b) {
2015-07-26 16:26:52 +02:00
return Vec4<T>(a).add(b);
}
2015-07-26 16:05:12 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec4<T> operator-(Vec4<T> const &a, Vec4<T> const &b) {
2015-07-26 16:05:12 +02:00
return Vec4<T>(a).sub(b);
}
2015-07-26 16:26:52 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec4<T> operator-(Vec4<T> const &a, T b) {
2015-07-26 16:26:52 +02:00
return Vec4<T>(a).sub(b);
}
2015-07-26 16:05:12 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec4<T> operator*(Vec4<T> const &a, Vec4<T> const &b) {
2015-07-26 16:05:12 +02:00
return Vec4<T>(a).mul(b);
}
2015-07-26 16:26:52 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec4<T> operator*(Vec4<T> const &a, T b) {
2015-07-26 16:26:52 +02:00
return Vec4<T>(a).mul(b);
}
2015-07-26 16:05:12 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec4<T> operator/(Vec4<T> const &a, Vec4<T> const &b) {
2015-07-26 16:05:12 +02:00
return Vec4<T>(a).div(b);
}
2015-07-26 16:26:52 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec4<T> operator/(Vec4<T> const &a, T b) {
2015-07-26 16:26:52 +02:00
return Vec4<T>(a).div(b);
}
2015-07-26 16:18:41 +02:00
template<typename T>
2016-06-23 20:18:35 +02:00
inline Vec4<T> operator-(Vec4<T> const &a) {
2015-07-26 16:18:41 +02:00
return Vec4<T>(a).neg();
}
using Vec4f = Vec4<float>;
using Vec4d = Vec4<double>;
using Vec4b = Vec4<byte>;
using Vec4i = Vec4<int>;
} /* namespace ostd */
2016-02-07 22:17:15 +01:00
#endif