From 761604f9e4815e8f4355637ebb46052314cbed86 Mon Sep 17 00:00:00 2001 From: vimene Date: Wed, 10 Dec 2025 20:40:42 +0100 Subject: renamed .h to .hpp --- src/math/mat4.h | 170 ------------------------------------- src/math/mat4.hpp | 170 +++++++++++++++++++++++++++++++++++++ src/math/quat.h | 78 ----------------- src/math/quat.hpp | 78 +++++++++++++++++ src/math/tform.h | 49 ----------- src/math/tform.hpp | 49 +++++++++++ src/math/vector.h | 236 ---------------------------------------------------- src/math/vector.hpp | 236 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 533 insertions(+), 533 deletions(-) delete mode 100644 src/math/mat4.h create mode 100644 src/math/mat4.hpp delete mode 100644 src/math/quat.h create mode 100644 src/math/quat.hpp delete mode 100644 src/math/tform.h create mode 100644 src/math/tform.hpp delete mode 100644 src/math/vector.h create mode 100644 src/math/vector.hpp (limited to 'src/math') diff --git a/src/math/mat4.h b/src/math/mat4.h deleted file mode 100644 index df8f533..0000000 --- a/src/math/mat4.h +++ /dev/null @@ -1,170 +0,0 @@ -#ifndef MATH_MAT4_H -#define MATH_MAT4_H - -#include -#include -#include "math/vector.h" - -namespace engine::math { - -struct Matrix4 { - static constexpr Matrix4 idty() { - return { - 1.f, 0.f, 0.f, 0.f, - 0.f, 1.f, 0.f, 0.f, - 0.f, 0.f, 1.f, 0.f, - 0.f, 0.f, 0.f, 1.f, - }; - } - - static constexpr Matrix4 translate(const Vector3& v) { - return { - 1.f, 0.f, 0.f, v.x, - 0.f, 1.f, 0.f, v.y, - 0.f, 0.f, 1.f, v.z, - 0.f, 0.f, 0.f, 1.f, - }; - } - - static constexpr Matrix4 scale(float fac) { - return { - fac, 0.f, 0.f, 0.f, - 0.f, fac, 0.f, 0.f, - 0.f, 0.f, fac, 0.f, - 0.f, 0.f, 0.f, 1.f, - }; - } - - static constexpr Matrix4 scale(const Vector3& facs) { - return { - facs.x, 0.f, 0.f, 0.f, - 0.f, facs.y, 0.f, 0.f, - 0.f, 0.f, facs.z, 0.f, - 0.f, 0.f, 0.f, 1.f, - }; - } - - static constexpr Matrix4 rot_x(float a) { - float c = std::cos(a); - float s = std::sin(a); - return { - 1.f, 0.f, 0.f, 0.f, - 0.f, c, -s, 0.f, - 0.f, s, c, 0.f, - 0.f, 0.f, 0.f, 1.f, - }; - } - - static constexpr Matrix4 rot_y(float a) { - float c = std::cos(a); - float s = std::sin(a); - return { - c, 0.f, s, 0.f, - 0.f, 1.f, 0.f, 0.f, - -s, 0.f, c, 0.f, - 0.f, 0.f, 0.f, 1.f, - }; - } - - static constexpr Matrix4 rot_z(float a) { - float c = std::cos(a); - float s = std::sin(a); - return { - c, -s, 0.f, 0.f, - s, c, 0.f, 0.f, - 0.f, 0.f, 1.f, 0.f, - 0.f, 0.f, 0.f, 1.f, - }; - } - - static constexpr Matrix4 projection(float fov, float aspect_ratio, float min_z, float max_z) { - float inv_tan = 1.f / std::tan(fov / 2.f); - return {{ - aspect_ratio * inv_tan, 0.f, 0.f, 0.f, - 0.f, -inv_tan, 0.f, 0.f, - 0.f, 0.f, -2.f / (max_z - min_z), -(max_z + min_z) / (max_z - min_z), - 0.f, 0.f, -1.f, 0.f, - }}; - } - - // TODO: should be Quaternion::to_mat4 - static constexpr Matrix4 from_quaternion(const Quaternion& q) { - return { - 2.f * (q.w * q.w + q.x * q.x) - 1.f, 2.f * (q.x * q.y - q.w * q.z) , 2.f * (q.x * q.z + q.w * q.y) , 0.f, - 2.f * (q.x * q.y + q.w * q.z) , 2.f * (q.w * q.w + q.y * q.y) - 1.f, 2.f * (q.y * q.z - q.w * q.x) , 0.f, - 2.f * (q.x * q.z - q.w * q.y) , 2.f * (q.y * q.z + q.w * q.x) , 2.f * (q.w * q.w + q.z * q.z) - 1.f, 0.f, - 0.f, 0.f, 0.f, 1.f, - }; - } - - std::array values; - - constexpr Matrix4 operator+() const & { - return *this; - } - - constexpr Matrix4 operator-() const & { - return { - -values[ 0], -values[ 1], -values[ 2], -values[ 3], - -values[ 4], -values[ 5], -values[ 6], -values[ 7], - -values[ 8], -values[ 9], -values[10], -values[11], - -values[12], -values[13], -values[14], -values[15], - }; - } - - constexpr Matrix4 operator+(const Matrix4& m) const & { - return { - values[ 0] + m.values[ 0], values[ 1] + m.values[ 1], values[ 2] + m.values[ 2], values[ 3] + m.values[ 3], - values[ 4] + m.values[ 4], values[ 5] + m.values[ 5], values[ 6] + m.values[ 6], values[ 7] + m.values[ 7], - values[ 8] + m.values[ 8], values[ 9] + m.values[ 9], values[10] + m.values[10], values[11] + m.values[11], - values[12] + m.values[12], values[13] + m.values[13], values[14] + m.values[14], values[15] + m.values[15], - }; - } - - constexpr Matrix4 operator-(const Matrix4& m) const & { - return *this + (-m); - } - - constexpr Matrix4 operator*(const Matrix4& m) const & { - Matrix4 ret; - for (int i = 0; i < 4; i++) { - for (int j = 0; j < 4; j++) { - ret.values[i * 4 + j] = 0.f; - for (int k = 0; k < 4; k++) - ret.values[i * 4 + j] += values[i * 4 + k] * m.values[k * 4 + j]; - } - } - return ret; - } - - constexpr Vector4 operator*(const Vector4& v) const & { - return { - values[ 0] * v.x + values[ 1] * v.y + values[ 2] * v.z + values[ 3] * v.w, - values[ 4] * v.x + values[ 5] * v.y + values[ 6] * v.z + values[ 7] * v.w, - values[ 8] * v.x + values[ 9] * v.y + values[10] * v.z + values[11] * v.w, - values[12] * v.x + values[13] * v.y + values[14] * v.z + values[15] * v.w, - }; - } - - constexpr std::array to_vecs() const & { - return {{ - { values[ 0], values[ 4], values[ 8], values[12] }, - { values[ 1], values[ 5], values[ 9], values[13] }, - { values[ 2], values[ 6], values[10], values[14] }, - { values[ 3], values[ 7], values[11], values[15] }, - }}; - } -}; - -constexpr Matrix4 operator*(float fac, const Matrix4& m) { - return { - fac * m.values[ 0], fac * m.values[ 1], fac * m.values[ 2], fac * m.values[ 3], - fac * m.values[ 4], fac * m.values[ 5], fac * m.values[ 6], fac * m.values[ 7], - fac * m.values[ 8], fac * m.values[ 9], fac * m.values[10], fac * m.values[11], - fac * m.values[12], fac * m.values[13], fac * m.values[14], fac * m.values[15], - }; -} - -} - -#endif // MATH_MAT4_H diff --git a/src/math/mat4.hpp b/src/math/mat4.hpp new file mode 100644 index 0000000..dfc5a75 --- /dev/null +++ b/src/math/mat4.hpp @@ -0,0 +1,170 @@ +#ifndef MATH_MAT4_H +#define MATH_MAT4_H + +#include +#include +#include "math/vector.hpp" + +namespace engine::math { + +struct Matrix4 { + static constexpr Matrix4 idty() { + return { + 1.f, 0.f, 0.f, 0.f, + 0.f, 1.f, 0.f, 0.f, + 0.f, 0.f, 1.f, 0.f, + 0.f, 0.f, 0.f, 1.f, + }; + } + + static constexpr Matrix4 translate(const Vector3& v) { + return { + 1.f, 0.f, 0.f, v.x, + 0.f, 1.f, 0.f, v.y, + 0.f, 0.f, 1.f, v.z, + 0.f, 0.f, 0.f, 1.f, + }; + } + + static constexpr Matrix4 scale(float fac) { + return { + fac, 0.f, 0.f, 0.f, + 0.f, fac, 0.f, 0.f, + 0.f, 0.f, fac, 0.f, + 0.f, 0.f, 0.f, 1.f, + }; + } + + static constexpr Matrix4 scale(const Vector3& facs) { + return { + facs.x, 0.f, 0.f, 0.f, + 0.f, facs.y, 0.f, 0.f, + 0.f, 0.f, facs.z, 0.f, + 0.f, 0.f, 0.f, 1.f, + }; + } + + static constexpr Matrix4 rot_x(float a) { + float c = std::cos(a); + float s = std::sin(a); + return { + 1.f, 0.f, 0.f, 0.f, + 0.f, c, -s, 0.f, + 0.f, s, c, 0.f, + 0.f, 0.f, 0.f, 1.f, + }; + } + + static constexpr Matrix4 rot_y(float a) { + float c = std::cos(a); + float s = std::sin(a); + return { + c, 0.f, s, 0.f, + 0.f, 1.f, 0.f, 0.f, + -s, 0.f, c, 0.f, + 0.f, 0.f, 0.f, 1.f, + }; + } + + static constexpr Matrix4 rot_z(float a) { + float c = std::cos(a); + float s = std::sin(a); + return { + c, -s, 0.f, 0.f, + s, c, 0.f, 0.f, + 0.f, 0.f, 1.f, 0.f, + 0.f, 0.f, 0.f, 1.f, + }; + } + + static constexpr Matrix4 projection(float fov, float aspect_ratio, float min_z, float max_z) { + float inv_tan = 1.f / std::tan(fov / 2.f); + return {{ + aspect_ratio * inv_tan, 0.f, 0.f, 0.f, + 0.f, -inv_tan, 0.f, 0.f, + 0.f, 0.f, -2.f / (max_z - min_z), -(max_z + min_z) / (max_z - min_z), + 0.f, 0.f, -1.f, 0.f, + }}; + } + + // TODO: should be Quaternion::to_mat4 + static constexpr Matrix4 from_quaternion(const Quaternion& q) { + return { + 2.f * (q.w * q.w + q.x * q.x) - 1.f, 2.f * (q.x * q.y - q.w * q.z) , 2.f * (q.x * q.z + q.w * q.y) , 0.f, + 2.f * (q.x * q.y + q.w * q.z) , 2.f * (q.w * q.w + q.y * q.y) - 1.f, 2.f * (q.y * q.z - q.w * q.x) , 0.f, + 2.f * (q.x * q.z - q.w * q.y) , 2.f * (q.y * q.z + q.w * q.x) , 2.f * (q.w * q.w + q.z * q.z) - 1.f, 0.f, + 0.f, 0.f, 0.f, 1.f, + }; + } + + std::array values; + + constexpr Matrix4 operator+() const & { + return *this; + } + + constexpr Matrix4 operator-() const & { + return { + -values[ 0], -values[ 1], -values[ 2], -values[ 3], + -values[ 4], -values[ 5], -values[ 6], -values[ 7], + -values[ 8], -values[ 9], -values[10], -values[11], + -values[12], -values[13], -values[14], -values[15], + }; + } + + constexpr Matrix4 operator+(const Matrix4& m) const & { + return { + values[ 0] + m.values[ 0], values[ 1] + m.values[ 1], values[ 2] + m.values[ 2], values[ 3] + m.values[ 3], + values[ 4] + m.values[ 4], values[ 5] + m.values[ 5], values[ 6] + m.values[ 6], values[ 7] + m.values[ 7], + values[ 8] + m.values[ 8], values[ 9] + m.values[ 9], values[10] + m.values[10], values[11] + m.values[11], + values[12] + m.values[12], values[13] + m.values[13], values[14] + m.values[14], values[15] + m.values[15], + }; + } + + constexpr Matrix4 operator-(const Matrix4& m) const & { + return *this + (-m); + } + + constexpr Matrix4 operator*(const Matrix4& m) const & { + Matrix4 ret; + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + ret.values[i * 4 + j] = 0.f; + for (int k = 0; k < 4; k++) + ret.values[i * 4 + j] += values[i * 4 + k] * m.values[k * 4 + j]; + } + } + return ret; + } + + constexpr Vector4 operator*(const Vector4& v) const & { + return { + values[ 0] * v.x + values[ 1] * v.y + values[ 2] * v.z + values[ 3] * v.w, + values[ 4] * v.x + values[ 5] * v.y + values[ 6] * v.z + values[ 7] * v.w, + values[ 8] * v.x + values[ 9] * v.y + values[10] * v.z + values[11] * v.w, + values[12] * v.x + values[13] * v.y + values[14] * v.z + values[15] * v.w, + }; + } + + constexpr std::array to_vecs() const & { + return {{ + { values[ 0], values[ 4], values[ 8], values[12] }, + { values[ 1], values[ 5], values[ 9], values[13] }, + { values[ 2], values[ 6], values[10], values[14] }, + { values[ 3], values[ 7], values[11], values[15] }, + }}; + } +}; + +constexpr Matrix4 operator*(float fac, const Matrix4& m) { + return { + fac * m.values[ 0], fac * m.values[ 1], fac * m.values[ 2], fac * m.values[ 3], + fac * m.values[ 4], fac * m.values[ 5], fac * m.values[ 6], fac * m.values[ 7], + fac * m.values[ 8], fac * m.values[ 9], fac * m.values[10], fac * m.values[11], + fac * m.values[12], fac * m.values[13], fac * m.values[14], fac * m.values[15], + }; +} + +} + +#endif // MATH_MAT4_H diff --git a/src/math/quat.h b/src/math/quat.h deleted file mode 100644 index 271070a..0000000 --- a/src/math/quat.h +++ /dev/null @@ -1,78 +0,0 @@ -#ifndef MATH_QUAT_H -#define MATH_QUAT_H - -#include - -namespace engine::math { - -struct Quaternion { - static constexpr Quaternion zero() { - return {0.f, 0.f, 0.f, 0.f}; - } - - static constexpr Quaternion one() { - return {1.f, 0.f, 0.f, 0.f}; - } - - static constexpr Quaternion euler_zxy(float rx, float ry, float rz) { - float ca = std::cos(rx / 2.f), sa = std::sin(rx / 2.f), - cb = std::cos(ry / 2.f), sb = std::sin(ry / 2.f), - cc = std::cos(rz / 2.f), sc = std::sin(rz / 2.f); - return { - ca * cb * cc + sa * sb * sc, - sa * cb * cc + ca * sb * sc, - ca * sb * cc - sa * cb * sc, - ca * cb * sc - sa * sb * cc, - }; - } - - static constexpr Quaternion rot_y(float a) { - return {std::cos(a / 2.f), 0.f, std::sin(a / 2.f), 0.f}; - } - - float w, x, y, z; - - constexpr Quaternion() {} - constexpr Quaternion(float w, float x, float y, float z) : w{w}, x{x}, y{y}, z{z} {} - - constexpr bool operator==(const Quaternion& other) const & { - return w == other.w && x == other.x && y == other.y && z == other.z; - } - - constexpr bool operator!=(const Quaternion& other) const & { - return !(*this == other); - } - - constexpr Quaternion operator+() const & { - return *this; - } - - constexpr Quaternion operator-() const & { - return { -w, -x, -y, -z }; - } - - constexpr Quaternion operator+(const Quaternion& other) const & { - return { w + other.w, x + other.x, y + other.y, z + other.z }; - } - - constexpr Quaternion operator-(const Quaternion& other) const & { - return *this + (-other); - } - - constexpr Quaternion operator*(const Quaternion& other) const & { - return { - w * other.w - x * other.x - y * other.y - z * other.z, - w * other.x + x * other.w + y * other.z - z * other.y, - w * other.y + y * other.w + z * other.x - x * other.z, - w * other.z + z * other.w + x * other.y - y * other.x, - }; - } - - constexpr Quaternion conjugate() const & { - return {w, -x, -y, -z}; - } -}; - -} - -#endif // MATH_QUAT_H diff --git a/src/math/quat.hpp b/src/math/quat.hpp new file mode 100644 index 0000000..271070a --- /dev/null +++ b/src/math/quat.hpp @@ -0,0 +1,78 @@ +#ifndef MATH_QUAT_H +#define MATH_QUAT_H + +#include + +namespace engine::math { + +struct Quaternion { + static constexpr Quaternion zero() { + return {0.f, 0.f, 0.f, 0.f}; + } + + static constexpr Quaternion one() { + return {1.f, 0.f, 0.f, 0.f}; + } + + static constexpr Quaternion euler_zxy(float rx, float ry, float rz) { + float ca = std::cos(rx / 2.f), sa = std::sin(rx / 2.f), + cb = std::cos(ry / 2.f), sb = std::sin(ry / 2.f), + cc = std::cos(rz / 2.f), sc = std::sin(rz / 2.f); + return { + ca * cb * cc + sa * sb * sc, + sa * cb * cc + ca * sb * sc, + ca * sb * cc - sa * cb * sc, + ca * cb * sc - sa * sb * cc, + }; + } + + static constexpr Quaternion rot_y(float a) { + return {std::cos(a / 2.f), 0.f, std::sin(a / 2.f), 0.f}; + } + + float w, x, y, z; + + constexpr Quaternion() {} + constexpr Quaternion(float w, float x, float y, float z) : w{w}, x{x}, y{y}, z{z} {} + + constexpr bool operator==(const Quaternion& other) const & { + return w == other.w && x == other.x && y == other.y && z == other.z; + } + + constexpr bool operator!=(const Quaternion& other) const & { + return !(*this == other); + } + + constexpr Quaternion operator+() const & { + return *this; + } + + constexpr Quaternion operator-() const & { + return { -w, -x, -y, -z }; + } + + constexpr Quaternion operator+(const Quaternion& other) const & { + return { w + other.w, x + other.x, y + other.y, z + other.z }; + } + + constexpr Quaternion operator-(const Quaternion& other) const & { + return *this + (-other); + } + + constexpr Quaternion operator*(const Quaternion& other) const & { + return { + w * other.w - x * other.x - y * other.y - z * other.z, + w * other.x + x * other.w + y * other.z - z * other.y, + w * other.y + y * other.w + z * other.x - x * other.z, + w * other.z + z * other.w + x * other.y - y * other.x, + }; + } + + constexpr Quaternion conjugate() const & { + return {w, -x, -y, -z}; + } +}; + +} + +#endif // MATH_QUAT_H diff --git a/src/math/tform.h b/src/math/tform.h deleted file mode 100644 index 74186f8..0000000 --- a/src/math/tform.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef MATH_TFORM_H -#define MATH_TFORM_H - -#include -#include "math/vector.h" -#include "math/mat4.h" -#include "math/quat.h" - -namespace engine::math { - -class Transform { - public: - Vector3 loc; - Quaternion rot; - Vector3 scale; - - constexpr Transform(Vector3 loc, Quaternion rot, Vector3 scale) : loc{loc}, rot{rot}, scale{scale} {} - - constexpr Transform opposite() const & { - return {-loc, rot.conjugate(), 1.f / scale}; - } - - constexpr Matrix4 to_mat4() const & { - return { - scale.x * (2.f * (rot.w * rot.w + rot.x * rot.x) - 1.f), scale.y * (2.f * (rot.x * rot.y - rot.w * rot.z) ), scale.z * (2.f * (rot.x * rot.z + rot.w * rot.y) ), loc.x, - scale.x * (2.f * (rot.x * rot.y + rot.w * rot.z) ), scale.y * (2.f * (rot.w * rot.w + rot.y * rot.y) - 1.f), scale.z * (2.f * (rot.y * rot.z - rot.w * rot.x) ), loc.y, - scale.x * (2.f * (rot.x * rot.z - rot.w * rot.y) ), scale.y * (2.f * (rot.y * rot.z + rot.w * rot.x) ), scale.z * (2.f * (rot.w * rot.w + rot.z * rot.z) - 1.f), loc.z, - 0.f, 0.f, 0.f, 1.f, - }; - } - - constexpr Matrix4 to_inverse_mat4() const & { - std::array m{{ - (2.f * (rot.w * rot.w + rot.x * rot.x) - 1.f) / scale.x, (2.f * (rot.x * rot.y + rot.w * rot.z) ) / scale.x, (2.f * (rot.x * rot.z - rot.w * rot.y) ) / scale.x, - (2.f * (rot.x * rot.y - rot.w * rot.z) ) / scale.y, (2.f * (rot.w * rot.w + rot.y * rot.y) - 1.f) / scale.y, (2.f * (rot.y * rot.z + rot.w * rot.x) ) / scale.y, - (2.f * (rot.x * rot.z + rot.w * rot.y) ) / scale.z, (2.f * (rot.y * rot.z - rot.w * rot.x) ) / scale.z, (2.f * (rot.w * rot.w + rot.z * rot.z) - 1.f) / scale.z, - }}; - return { - m[0], m[1], m[2], - (m[0] * loc.x + m[1] * loc.y + m[2] * loc.z), - m[3], m[4], m[5], - (m[3] * loc.x + m[4] * loc.y + m[5] * loc.z), - m[6], m[7], m[8], - (m[6] * loc.x + m[7] * loc.y + m[8] * loc.z), - 0.f, 0.f, 0.f, 1.0f, - }; - } -}; - -} - -#endif // MATH_TFORM_H diff --git a/src/math/tform.hpp b/src/math/tform.hpp new file mode 100644 index 0000000..cd9301f --- /dev/null +++ b/src/math/tform.hpp @@ -0,0 +1,49 @@ +#ifndef MATH_TFORM_H +#define MATH_TFORM_H + +#include +#include "math/vector.hpp" +#include "math/mat4.hpp" +#include "math/quat.hpp" + +namespace engine::math { + +class Transform { + public: + Vector3 loc; + Quaternion rot; + Vector3 scale; + + constexpr Transform(Vector3 loc, Quaternion rot, Vector3 scale) : loc{loc}, rot{rot}, scale{scale} {} + + constexpr Transform opposite() const & { + return {-loc, rot.conjugate(), 1.f / scale}; + } + + constexpr Matrix4 to_mat4() const & { + return { + scale.x * (2.f * (rot.w * rot.w + rot.x * rot.x) - 1.f), scale.y * (2.f * (rot.x * rot.y - rot.w * rot.z) ), scale.z * (2.f * (rot.x * rot.z + rot.w * rot.y) ), loc.x, + scale.x * (2.f * (rot.x * rot.y + rot.w * rot.z) ), scale.y * (2.f * (rot.w * rot.w + rot.y * rot.y) - 1.f), scale.z * (2.f * (rot.y * rot.z - rot.w * rot.x) ), loc.y, + scale.x * (2.f * (rot.x * rot.z - rot.w * rot.y) ), scale.y * (2.f * (rot.y * rot.z + rot.w * rot.x) ), scale.z * (2.f * (rot.w * rot.w + rot.z * rot.z) - 1.f), loc.z, + 0.f, 0.f, 0.f, 1.f, + }; + } + + constexpr Matrix4 to_inverse_mat4() const & { + std::array m{{ + (2.f * (rot.w * rot.w + rot.x * rot.x) - 1.f) / scale.x, (2.f * (rot.x * rot.y + rot.w * rot.z) ) / scale.x, (2.f * (rot.x * rot.z - rot.w * rot.y) ) / scale.x, + (2.f * (rot.x * rot.y - rot.w * rot.z) ) / scale.y, (2.f * (rot.w * rot.w + rot.y * rot.y) - 1.f) / scale.y, (2.f * (rot.y * rot.z + rot.w * rot.x) ) / scale.y, + (2.f * (rot.x * rot.z + rot.w * rot.y) ) / scale.z, (2.f * (rot.y * rot.z - rot.w * rot.x) ) / scale.z, (2.f * (rot.w * rot.w + rot.z * rot.z) - 1.f) / scale.z, + }}; + return { + m[0], m[1], m[2], - (m[0] * loc.x + m[1] * loc.y + m[2] * loc.z), + m[3], m[4], m[5], - (m[3] * loc.x + m[4] * loc.y + m[5] * loc.z), + m[6], m[7], m[8], - (m[6] * loc.x + m[7] * loc.y + m[8] * loc.z), + 0.f, 0.f, 0.f, 1.0f, + }; + } +}; + +} + +#endif // MATH_TFORM_H diff --git a/src/math/vector.h b/src/math/vector.h deleted file mode 100644 index fcfc5bd..0000000 --- a/src/math/vector.h +++ /dev/null @@ -1,236 +0,0 @@ -#ifndef MATH_VECTOR_H -#define MATH_VECTOR_H - -#include -#include "math/quat.h" - -namespace engine::math { - -struct Vector2 { - float x, y; - - constexpr bool operator==(const Vector2& other) const & { - return x == other.x && y == other.y; - } - - constexpr bool operator!=(const Vector2& other) const & { - return !(*this == other); - } - - constexpr Vector2 operator+() const & { - return *this; - } - - constexpr Vector2 operator-() const & { - return { -x, -y }; - } - - constexpr Vector2 operator+(const Vector2& other) const & { - return { x + other.x, y + other.y }; - } - - constexpr Vector2 operator-(const Vector2& other) const & { - return *this + (-other); - } - - constexpr Vector2 operator+=(const Vector2& other) & { - x += other.x; - y += other.y; - return *this; - } - - constexpr float det(const Vector2& other) const & { - return this->x * other.y - other.x * this->y; - } - - constexpr Vector2 round() const & { - return { std::round(x), std::round(y) }; - } - - constexpr Vector2 mul_term(const Vector2& other) const & { - return { x * other.x, y * other.y }; - } -}; - -constexpr Vector2 operator*(float n, const Vector2& other) { - return { n * other.x, n * other.y }; -} - -constexpr Vector2 operator*(const Vector2& other, float n) { - return n * other; -} - -constexpr Vector2 operator/(const Vector2& other, float n) { - return { other.x / n, other.y / n }; -} - -constexpr Vector2 operator+(const Vector2& other, float n) { - return { other.x + n, other.y + n }; -} - -constexpr Vector2 operator-(const Vector2& other, float n) { - return { other.x - n, other.y - n }; -} - -struct Vector3; -constexpr Vector3 operator*(float n, const Vector3& other); -constexpr Vector3 operator/(const Vector3& other, float n); - -struct Vector3 { - static constexpr Vector3 bilerp(const Vector3& v1, const Vector3& v2, const Vector3& v3, float b0, float b1) { - return b0 * v1 + b1 * v2 + (1.f - b0 - b1) * v3; - } - - float x, y, z; - - constexpr bool operator==(const Vector3& other) const & { - return x == other.x && y == other.y && z == other.z; - } - - constexpr bool operator!=(const Vector3& other) const & { - return !(*this == other); - } - - constexpr Vector3 operator+() const & { - return *this; - } - - constexpr Vector3 operator-() const & { - return { -x, -y, -z }; - } - - constexpr Vector3 operator+(const Vector3& other) const & { - return { x + other.x, y + other.y, z + other.z }; - } - - constexpr Vector3 operator-(const Vector3& other) const & { - return *this + (-other); - } - - constexpr Vector3 operator+=(const Vector3& other) & { - x += other.x; - y += other.y; - z += other.z; - return *this; - } - - constexpr Vector3 round() { - return { std::round(x), std::round(y), std::round(z) }; - } - - constexpr Vector3 cross(const Vector3& other) const & { - return { - y * other.z - z * other.y, - z * other.x - x * other.z, - x * other.y - y * other.x - }; - } - - constexpr Vector3 rot(const Quaternion& q) const & { - return { - (2.f * (q.w * q.w + q.x * q.x) - 1.f) * x + (2.f * (q.x * q.y - q.w * q.z) ) * y + (2.f * (q.x * q.z + q.w * q.y) ) * z, - (2.f * (q.x * q.y + q.w * q.z) ) * x + (2.f * (q.w * q.w + q.y * q.y) - 1.f) * y + (2.f * (q.y * q.z - q.w * q.x) ) * z, - (2.f * (q.x * q.z - q.w * q.y) ) * x + (2.f * (q.y * q.z + q.w * q.x) ) * y + (2.f * (q.w * q.w + q.z * q.z) - 1.f) * z, - }; - } - - constexpr float dot(const Vector3& other) const & { - return x * other.x + y * other.y + z * other.z; - } - - constexpr float length_squared() const & { - return dot(*this); - } - - constexpr float length() const & { - return std::sqrt(length_squared()); - } - - constexpr Vector3 normalize() const & { - return *this / length(); - } -}; - -constexpr Vector3 operator*(float n, const Vector3& other) { - return { n * other.x, n * other.y, n * other.z }; -} - -constexpr Vector3 operator*(const Vector3& other, float n) { - return n * other; -} - -constexpr Vector3 operator/(float n, const Vector3& other) { - return { n / other.x, n / other.y, n / other.z }; -} - -constexpr Vector3 operator/(const Vector3& other, float n) { - return { other.x / n, other.y / n, other.z / n }; -} - -struct Vector4 { - float x, y, z, w; - - constexpr Vector4() {} - constexpr Vector4(float x, float y, float z, float w) : x{x}, y{y}, z{z}, w{w} {} - constexpr Vector4(float x, float y, float z) : x{x}, y{y}, z{z}, w{1.f} {} - constexpr Vector4(const Vector2& v, float z, float w) : x{v.x}, y{v.y}, z{z}, w{w} {} - constexpr Vector4(const Vector2& v, float z) : x{v.x}, y{v.y}, z{z}, w{1.f} {} - constexpr Vector4(const Vector3& v, float w) : x{v.x}, y{v.y}, z{v.z}, w{w} {} - constexpr Vector4(const Vector3& v) : x{v.x}, y{v.y}, z{v.z}, w{1.f} {} - - constexpr bool operator==(const Vector4& other) const & { - return x == other.x && y == other.y && z == other.z && w == other.w; - } - - constexpr bool operator!=(const Vector4& other) const & { - return !(*this == other); - } - - constexpr Vector4 operator+() const & { - return *this; - } - - constexpr Vector4 operator-() const & { - return { -x, -y, -z, -w }; - } - - constexpr Vector4 operator+(const Vector4& other) const & { - return { x + other.x, y + other.y, z + other.z, w + other.w }; - } - - constexpr Vector4 operator-(const Vector4& other) const & { - return *this + (-other); - } - - constexpr Vector4 round() const & { - return { std::round(x), std::round(y), std::round(z), std::round(w) }; - } - - constexpr Vector2 xy() const & { - return { x, y }; - } - - constexpr Vector3 xyz() const & { - return { x, y, z }; - } - - constexpr Vector4 div_by_w() const & { - return {x / w, y / w, z / w, w}; - } -}; - -constexpr Vector4 operator*(float n, const Vector4& other) { - return { n * other.x, n * other.y, n * other.z, n * other.w }; -} - -constexpr Vector4 operator*(const Vector4& other, float n) { - return n * other; -} - -constexpr Vector4 operator/(const Vector4& other, float n) { - return { other.x / n, other.y / n, other.z / n, other.w / n }; -} - -} - -#endif // MATH_VECTOR_H diff --git a/src/math/vector.hpp b/src/math/vector.hpp new file mode 100644 index 0000000..a0e58e1 --- /dev/null +++ b/src/math/vector.hpp @@ -0,0 +1,236 @@ +#ifndef MATH_VECTOR_H +#define MATH_VECTOR_H + +#include +#include "math/quat.hpp" + +namespace engine::math { + +struct Vector2 { + float x, y; + + constexpr bool operator==(const Vector2& other) const & { + return x == other.x && y == other.y; + } + + constexpr bool operator!=(const Vector2& other) const & { + return !(*this == other); + } + + constexpr Vector2 operator+() const & { + return *this; + } + + constexpr Vector2 operator-() const & { + return { -x, -y }; + } + + constexpr Vector2 operator+(const Vector2& other) const & { + return { x + other.x, y + other.y }; + } + + constexpr Vector2 operator-(const Vector2& other) const & { + return *this + (-other); + } + + constexpr Vector2 operator+=(const Vector2& other) & { + x += other.x; + y += other.y; + return *this; + } + + constexpr float det(const Vector2& other) const & { + return this->x * other.y - other.x * this->y; + } + + constexpr Vector2 round() const & { + return { std::round(x), std::round(y) }; + } + + constexpr Vector2 mul_term(const Vector2& other) const & { + return { x * other.x, y * other.y }; + } +}; + +constexpr Vector2 operator*(float n, const Vector2& other) { + return { n * other.x, n * other.y }; +} + +constexpr Vector2 operator*(const Vector2& other, float n) { + return n * other; +} + +constexpr Vector2 operator/(const Vector2& other, float n) { + return { other.x / n, other.y / n }; +} + +constexpr Vector2 operator+(const Vector2& other, float n) { + return { other.x + n, other.y + n }; +} + +constexpr Vector2 operator-(const Vector2& other, float n) { + return { other.x - n, other.y - n }; +} + +struct Vector3; +constexpr Vector3 operator*(float n, const Vector3& other); +constexpr Vector3 operator/(const Vector3& other, float n); + +struct Vector3 { + static constexpr Vector3 bilerp(const Vector3& v1, const Vector3& v2, const Vector3& v3, float b0, float b1) { + return b0 * v1 + b1 * v2 + (1.f - b0 - b1) * v3; + } + + float x, y, z; + + constexpr bool operator==(const Vector3& other) const & { + return x == other.x && y == other.y && z == other.z; + } + + constexpr bool operator!=(const Vector3& other) const & { + return !(*this == other); + } + + constexpr Vector3 operator+() const & { + return *this; + } + + constexpr Vector3 operator-() const & { + return { -x, -y, -z }; + } + + constexpr Vector3 operator+(const Vector3& other) const & { + return { x + other.x, y + other.y, z + other.z }; + } + + constexpr Vector3 operator-(const Vector3& other) const & { + return *this + (-other); + } + + constexpr Vector3 operator+=(const Vector3& other) & { + x += other.x; + y += other.y; + z += other.z; + return *this; + } + + constexpr Vector3 round() { + return { std::round(x), std::round(y), std::round(z) }; + } + + constexpr Vector3 cross(const Vector3& other) const & { + return { + y * other.z - z * other.y, + z * other.x - x * other.z, + x * other.y - y * other.x + }; + } + + constexpr Vector3 rot(const Quaternion& q) const & { + return { + (2.f * (q.w * q.w + q.x * q.x) - 1.f) * x + (2.f * (q.x * q.y - q.w * q.z) ) * y + (2.f * (q.x * q.z + q.w * q.y) ) * z, + (2.f * (q.x * q.y + q.w * q.z) ) * x + (2.f * (q.w * q.w + q.y * q.y) - 1.f) * y + (2.f * (q.y * q.z - q.w * q.x) ) * z, + (2.f * (q.x * q.z - q.w * q.y) ) * x + (2.f * (q.y * q.z + q.w * q.x) ) * y + (2.f * (q.w * q.w + q.z * q.z) - 1.f) * z, + }; + } + + constexpr float dot(const Vector3& other) const & { + return x * other.x + y * other.y + z * other.z; + } + + constexpr float length_squared() const & { + return dot(*this); + } + + constexpr float length() const & { + return std::sqrt(length_squared()); + } + + constexpr Vector3 normalize() const & { + return *this / length(); + } +}; + +constexpr Vector3 operator*(float n, const Vector3& other) { + return { n * other.x, n * other.y, n * other.z }; +} + +constexpr Vector3 operator*(const Vector3& other, float n) { + return n * other; +} + +constexpr Vector3 operator/(float n, const Vector3& other) { + return { n / other.x, n / other.y, n / other.z }; +} + +constexpr Vector3 operator/(const Vector3& other, float n) { + return { other.x / n, other.y / n, other.z / n }; +} + +struct Vector4 { + float x, y, z, w; + + constexpr Vector4() {} + constexpr Vector4(float x, float y, float z, float w) : x{x}, y{y}, z{z}, w{w} {} + constexpr Vector4(float x, float y, float z) : x{x}, y{y}, z{z}, w{1.f} {} + constexpr Vector4(const Vector2& v, float z, float w) : x{v.x}, y{v.y}, z{z}, w{w} {} + constexpr Vector4(const Vector2& v, float z) : x{v.x}, y{v.y}, z{z}, w{1.f} {} + constexpr Vector4(const Vector3& v, float w) : x{v.x}, y{v.y}, z{v.z}, w{w} {} + constexpr Vector4(const Vector3& v) : x{v.x}, y{v.y}, z{v.z}, w{1.f} {} + + constexpr bool operator==(const Vector4& other) const & { + return x == other.x && y == other.y && z == other.z && w == other.w; + } + + constexpr bool operator!=(const Vector4& other) const & { + return !(*this == other); + } + + constexpr Vector4 operator+() const & { + return *this; + } + + constexpr Vector4 operator-() const & { + return { -x, -y, -z, -w }; + } + + constexpr Vector4 operator+(const Vector4& other) const & { + return { x + other.x, y + other.y, z + other.z, w + other.w }; + } + + constexpr Vector4 operator-(const Vector4& other) const & { + return *this + (-other); + } + + constexpr Vector4 round() const & { + return { std::round(x), std::round(y), std::round(z), std::round(w) }; + } + + constexpr Vector2 xy() const & { + return { x, y }; + } + + constexpr Vector3 xyz() const & { + return { x, y, z }; + } + + constexpr Vector4 div_by_w() const & { + return {x / w, y / w, z / w, w}; + } +}; + +constexpr Vector4 operator*(float n, const Vector4& other) { + return { n * other.x, n * other.y, n * other.z, n * other.w }; +} + +constexpr Vector4 operator*(const Vector4& other, float n) { + return n * other; +} + +constexpr Vector4 operator/(const Vector4& other, float n) { + return { other.x / n, other.y / n, other.z / n, other.w / n }; +} + +} + +#endif // MATH_VECTOR_H -- cgit v1.2.3