Files
Blitz/include/blitz/maths/Vector.h
Persson-dev 507946a0f4
All checks were successful
Linux arm64 / Build (push) Successful in 4m41s
fix operator- for Vector
2024-03-28 16:14:01 +01:00

323 lines
6.9 KiB
C++

#pragma once
#include <algorithm>
#include <cmath>
#include <cstddef>
namespace blitz {
//////////////////////////////////////////////////////////////////
// Vector //
//////////////////////////////////////////////////////////////////
template <typename T>
struct Vec2 {
union {
T x;
T r;
};
union {
T y;
T g;
};
constexpr Vec2(T X = 0, T Y = 0) : x(X), y(Y) {}
};
template <typename T>
struct Vec3 {
union {
T x;
T r;
};
union {
T y;
T g;
};
union {
T z;
T b;
};
constexpr Vec3(T X = 0, T Y = 0, T Z = 0) : x(X), y(Y), z(Z) {}
};
template <typename T>
struct Vec4 {
union {
T x;
T r;
};
union {
T y;
T g;
};
union {
T z;
T b;
};
union {
T w;
T a;
};
constexpr Vec4(Vec3<T> vec, T W = 1) : x(vec.x), y(vec.y), z(vec.z), w(W) {}
constexpr Vec4(T X = 0, T Y = 0, T Z = 0, T W = 0) : x(X), y(Y), z(Z), w(W) {}
};
using Vec2uc = Vec2<unsigned char>;
using Vec2i = Vec2<int>;
using Vec2u = Vec2<unsigned int>;
using Vec2f = Vec2<float>;
using Vec2d = Vec2<double>;
using Vec3uc = Vec3<unsigned char>;
using Vec3i = Vec3<int>;
using Vec3u = Vec3<unsigned int>;
using Vec3f = Vec3<float>;
using Vec3d = Vec3<double>;
using Vec4uc = Vec4<unsigned char>;
using Vec4i = Vec4<int>;
using Vec4u = Vec4<unsigned int>;
using Vec4f = Vec4<float>;
using Vec4d = Vec4<double>;
using Color = Vec3uc;
// Vec2
template <typename T>
constexpr bool operator==(const Vec2<T>& vec2, const Vec2<T>& other) {
return vec2.x == other.x && vec2.y == other.y;
}
template <typename T>
constexpr Vec2<T> operator+(const Vec2<T>& vect, const Vec2<T>& other) {
return {vect.x + other.x, vect.y + other.y};
}
template <typename T>
Vec2<T>& operator+=(Vec2<T>& vect, const Vec2<T>& other) {
vect = vect + other;
return vect;
}
template <typename T>
constexpr Vec2<T> operator-(const Vec2<T>& vect) {
return {-vect.x, -vect.y};
}
template <typename T>
constexpr Vec2<T> operator-(const Vec2<T>& vect, const Vec2<T>& other) {
return {vect.x - other.x, vect.y - other.y};
}
template <typename T>
Vec2<T>& operator-=(Vec2<T>& vect, const Vec2<T>& other) {
vect = vect - other;
return vect;
}
template <typename T>
constexpr Vec2<T> operator*(const Vec2<T>& vect, T mult) {
return {vect.x * mult, vect.y * mult};
}
template <typename T>
constexpr Vec2<T> operator*(T mult, const Vec2<T>& vect) {
return vect * mult;
}
// Vec3
template <typename T>
constexpr bool operator==(const Vec3<T>& vec3, const Vec3<T>& other) {
return vec3.x == other.x && vec3.y == other.y && vec3.z == other.z;
}
template <typename T>
constexpr Vec3<T> operator-(const Vec3<T>& vect) {
return {-vect.x, -vect.y, -vect.z};
}
template <typename T>
constexpr Vec3<T> operator+(const Vec3<T>& vect, const Vec3<T>& other) {
return {vect.x + other.x, vect.y + other.y, vect.z + other.z};
}
template <typename T>
Vec3<T>& operator+=(Vec3<T>& vect, const Vec3<T>& other) {
vect = vect + other;
return vect;
}
template <typename T>
constexpr Vec3<T> operator-(const Vec3<T>& vect, const Vec3<T>& other) {
return {vect.x - other.x, vect.y - other.y, vect.z - other.z};
}
template <typename T>
Vec3<T>& operator-=(Vec3<T>& vect, const Vec3<T>& other) {
vect = vect - other;
return vect;
}
template <typename T>
constexpr Vec3<T> operator*(const Vec3<T>& vect, T mult) {
return {vect.x * mult, vect.y * mult, vect.z * mult};
}
template <typename T>
constexpr Vec3<T> operator*(T mult, const Vec3<T>& vect) {
return vect * mult;
}
template <typename T>
constexpr Vec3<T> operator*=(const Vec3<T>& vect, T mult) {
vect = vect * mult;
return vect;
}
template <typename T>
constexpr Vec3<T> operator*=(T mult, const Vec3<T>& vect) {
vect = vect * mult;
return vect;
}
template <typename T>
constexpr Vec3<T> operator*(const Vec3<T>& vect, const Vec3<T>& other) {
return {vect.x * other.x, vect.y * other.y, vect.z * other.z};
}
template <typename T>
Vec3<T>& operator*=(Vec3<T>& vect, const Vec3<T>& other) {
vect = vect * other;
return vect;
}
template <typename T>
constexpr Vec3<T> operator/(const Vec3<T>& vect, const Vec3<T>& other) {
return {vect.x / other.x, vect.y / other.y, vect.z / other.z};
}
template <typename T>
Vec3<T>& operator/=(Vec3<T>& vect, const Vec3<T>& other) {
vect = vect / other;
return vect;
}
template <typename T>
constexpr bool operator<(const Vec3<T>& vec3, const Vec3<T>& other) {
return vec3.x < other.x && vec3.y < other.y && vec3.z < other.z;
}
// Vec4
template <typename T>
constexpr bool operator==(const Vec4<T>& vec4, const Vec4<T>& other) {
return vec4.x == other.x && vec4.y == other.y && vec4.z == other.z && vec4.w == other.w;
}
template <typename T>
constexpr Vec4<T> operator-(const Vec4<T>& vect) {
return {-vect.x, -vect.y, -vect.z, -vect.w};
}
template <typename T>
constexpr Vec4<T> operator+(const Vec4<T>& vect, const Vec4<T>& other) {
return {vect.x + other.x, vect.y + other.y, vect.z + other.z, vect.w + other.w};
}
template <typename T>
Vec4<T>& operator+=(Vec4<T>& vect, const Vec4<T>& other) {
vect = vect + other;
return vect;
}
template <typename T>
constexpr Vec4<T> operator-(const Vec4<T>& vect, const Vec4<T>& other) {
return {vect.x - other.x, vect.y - other.y, vect.z - other.z, vect.w - other.w};
}
template <typename T>
Vec4<T>& operator-=(Vec4<T>& vect, const Vec4<T>& other) {
vect = vect - other;
return vect;
}
template <typename T>
constexpr Vec4<T> operator*(const Vec4<T>& vect, T mult) {
return {vect.x * mult, vect.y * mult, vect.z * mult, vect.w * mult};
}
template <typename T>
constexpr Vec4<T> operator*(T mult, const Vec4<T>& vect) {
return vect * mult;
}
//////////////////////////////////////////////////////////////////
// Matrix //
//////////////////////////////////////////////////////////////////
template <typename T>
struct Mat4 {
static const std::size_t MATRIX_SIZE = 4;
T x0, x1, x2, x3;
T y0, y1, y2, y3;
T z0, z1, z2, z3;
T w0, w1, w2, w3;
T operator[](std::size_t offset) const {
return reinterpret_cast<const T*>(this)[offset];
}
T& operator[](std::size_t offset) {
return reinterpret_cast<T*>(this)[offset];
}
T* data() {
return reinterpret_cast<T*>(this);
}
const T* data() const {
return reinterpret_cast<const T*>(this);
}
T at(std::size_t row, std::size_t column) const {
return operator[](row * MATRIX_SIZE + column);
}
T& at(std::size_t row, std::size_t column) {
return operator[](row * MATRIX_SIZE + column);
}
};
typedef Mat4<float> Mat4f;
typedef Mat4<int> Mat4i;
typedef Mat4<double> Mat4d;
template <typename T>
bool operator==(const Mat4<T>& mat, const Mat4<T>& other) {
return mat.x0 == other.x0 && mat.y0 == other.y0 && mat.z0 == other.z0 && mat.w0 == other.w0 && mat.x1 == other.x1 &&
mat.y1 == other.y1 && mat.z1 == other.z1 && mat.w1 == other.w1 && mat.x2 == other.x2 && mat.y2 == other.y2 &&
mat.z2 == other.z2 && mat.w2 == other.w2 && mat.x3 == other.x3 && mat.y3 == other.y3 && mat.z3 == other.z3 &&
mat.w3 == other.w3;
}
} // namespace blitz