From 880813eef7211397f5f2c70145e445b24702d5d2 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Tue, 24 Sep 2024 17:15:51 +0100 Subject: [PATCH 1/5] mat3x3 --- include/hyprutils/math/Mat3x3.hpp | 58 ++++++++++++ src/math/Mat3x3.cpp | 141 ++++++++++++++++++++++++++++++ tests/math.cpp | 10 +++ tests/shared.hpp | 2 +- 4 files changed, 210 insertions(+), 1 deletion(-) create mode 100644 include/hyprutils/math/Mat3x3.hpp create mode 100644 src/math/Mat3x3.cpp diff --git a/include/hyprutils/math/Mat3x3.hpp b/include/hyprutils/math/Mat3x3.hpp new file mode 100644 index 0000000..c58a4c9 --- /dev/null +++ b/include/hyprutils/math/Mat3x3.hpp @@ -0,0 +1,58 @@ +#pragma once + +#include +#include +#include +#include + +#include "./Misc.hpp" + +namespace Hyprutils { + namespace Math { + class CBox; + class Vector2D; + + class Mat3x3 { + public: + Mat3x3(); + Mat3x3(std::array); + Mat3x3(std::vector); + + /* create an identity 3x3 matrix */ + static Mat3x3 identity(); + + /* create an output projection matrix */ + static Mat3x3 outputProjection(const Vector2D& size, eTransform transform); + + /* get the matrix as an array, in a RTL TTB order. */ + std::array getMatrix() const; + + /* create a box projection matrix */ + Mat3x3 projectBox(const CBox& box, eTransform transform, float rot = 0.F /* rad, CCW */) const; + + /* in-place functions */ + Mat3x3& transform(eTransform transform); + Mat3x3& rotate(float rot /* rad, CCW */); + Mat3x3& scale(const Vector2D& scale); + Mat3x3& scale(const float scale); + Mat3x3& translate(const Vector2D& offset); + Mat3x3& transpose(); + Mat3x3& multiply(const Mat3x3& other); + + /* misc utils */ + std::string toString() const; + + bool operator==(const Mat3x3& other) const { + return other.matrix == matrix; + } + + friend std::ostream& operator<<(std::ostream& os, const Mat3x3& mat) { + os << mat.toString(); + return os; + } + + private: + std::array matrix; + }; + } +} \ No newline at end of file diff --git a/src/math/Mat3x3.cpp b/src/math/Mat3x3.cpp new file mode 100644 index 0000000..8b2d24e --- /dev/null +++ b/src/math/Mat3x3.cpp @@ -0,0 +1,141 @@ +#include +#include +#include +#include +#include +#include + +using namespace Hyprutils::Math; + +static std::unordered_map transforms = { + {HYPRUTILS_TRANSFORM_NORMAL, std::array{1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f}}, + {HYPRUTILS_TRANSFORM_90, std::array{0.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}}, + {HYPRUTILS_TRANSFORM_180, std::array{-1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f}}, + {HYPRUTILS_TRANSFORM_270, std::array{0.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}}, + {HYPRUTILS_TRANSFORM_FLIPPED, std::array{-1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f}}, + {HYPRUTILS_TRANSFORM_FLIPPED_90, std::array{0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}}, + {HYPRUTILS_TRANSFORM_FLIPPED_180, std::array{1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f}}, + {HYPRUTILS_TRANSFORM_FLIPPED_270, std::array{0.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}}, +}; + +Mat3x3::Mat3x3() { + matrix = {0}; +} + +Mat3x3::Mat3x3(std::array mat) : matrix(mat) { + ; +} + +Mat3x3::Mat3x3(std::vector mat) { + for (size_t i = 0; i < 9; ++i) { + matrix.at(i) = mat.size() < i ? mat.at(i) : 0.F; + } +} + +Mat3x3 Mat3x3::identity() { + return Mat3x3(std::array{1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f}); +} + +Mat3x3 Mat3x3::outputProjection(const Vector2D& size, eTransform transform) { + Mat3x3 mat; + + const auto& t = transforms.at(transform); + float x = 2.0f / size.x; + float y = 2.0f / size.y; + + // Rotation + reflection + mat.matrix[0] = x * t.matrix[0]; + mat.matrix[1] = x * t.matrix[1]; + mat.matrix[3] = y * -t.matrix[3]; + mat.matrix[4] = y * -t.matrix[4]; + + // Translation + mat.matrix[2] = -copysign(1.0f, mat.matrix[0] + mat.matrix[1]); + mat.matrix[5] = -copysign(1.0f, mat.matrix[3] + mat.matrix[4]); + + // Identity + mat.matrix[8] = 1.0f; + + return mat; +} + +std::array Mat3x3::getMatrix() const { + return matrix; +} + +Mat3x3 Mat3x3::projectBox(const CBox& box, eTransform transform, float rot /* rad, CCW */) const { + Mat3x3 mat = Mat3x3::identity(); + + const auto boxSize = box.size(); + + mat.translate(box.pos()); + + if (rot != 0) { + mat.translate(boxSize / 2); + mat.rotate(rot); + mat.translate(-boxSize / 2); + } + + mat.scale(boxSize); + + if (transform != HYPRUTILS_TRANSFORM_NORMAL) { + mat.translate({0.5, 0.5}); + mat.transform(transform); + mat.translate({-0.5, -0.5}); + } + + return mat.multiply(*this); +} + +Mat3x3& Mat3x3::transform(eTransform transform) { + multiply(transforms.at(transform)); + return *this; +} + +Mat3x3& Mat3x3::rotate(float rot) { + multiply(std::array{(float)cos(rot), (float)-sin(rot), 0.0f, (float)sin(rot), (float)cos(rot), 0.0f, 0.0f, 0.0f, 1.0f}); + return *this; +} + +Mat3x3& Mat3x3::scale(const Vector2D& scale_) { + multiply(std::array{(float)scale_.x, 0.0f, 0.0f, 0.0f, (float)scale_.y, 0.0f, 0.0f, 0.0f, 1.0f}); + return *this; +} + +Mat3x3& Mat3x3::scale(const float scale_) { + return scale({scale_, scale_}); +} + +Mat3x3& Mat3x3::translate(const Vector2D& offset) { + multiply(std::array{1.0f, 0.0f, (float)offset.x, 0.0f, 1.0f, (float)offset.y, 0.0f, 0.0f, 1.0f}); + return *this; +} + +Mat3x3& Mat3x3::transpose() { + matrix = std::array{matrix[0], matrix[3], matrix[6], matrix[1], matrix[4], matrix[7], matrix[2], matrix[5], matrix[8]}; + return *this; +} + +Mat3x3& Mat3x3::multiply(const Mat3x3& other) { + std::array product; + + product[0] = matrix[0] * other.matrix[0] + matrix[1] * other.matrix[3] + matrix[2] * other.matrix[6]; + product[1] = matrix[0] * other.matrix[1] + matrix[1] * other.matrix[4] + matrix[2] * other.matrix[7]; + product[2] = matrix[0] * other.matrix[2] + matrix[1] * other.matrix[5] + matrix[2] * other.matrix[8]; + + product[3] = matrix[3] * other.matrix[0] + matrix[4] * other.matrix[3] + matrix[5] * other.matrix[6]; + product[4] = matrix[3] * other.matrix[1] + matrix[4] * other.matrix[4] + matrix[5] * other.matrix[7]; + product[5] = matrix[3] * other.matrix[2] + matrix[4] * other.matrix[5] + matrix[5] * other.matrix[8]; + + product[6] = matrix[6] * other.matrix[0] + matrix[7] * other.matrix[3] + matrix[8] * other.matrix[6]; + product[7] = matrix[6] * other.matrix[1] + matrix[7] * other.matrix[4] + matrix[8] * other.matrix[7]; + product[8] = matrix[6] * other.matrix[2] + matrix[7] * other.matrix[5] + matrix[8] * other.matrix[8]; + + matrix = product; + return *this; +} + +std::string Mat3x3::toString() const { + return std::format("[mat3x3: {}, {}, {}, {}, {}, {}, {}, {}, {}]", matrix.at(0), matrix.at(1), matrix.at(2), matrix.at(3), matrix.at(4), matrix.at(5), matrix.at(6), + matrix.at(7), matrix.at(8)); +} diff --git a/tests/math.cpp b/tests/math.cpp index 8a29774..10f0bd3 100644 --- a/tests/math.cpp +++ b/tests/math.cpp @@ -1,4 +1,5 @@ #include +#include #include "shared.hpp" using namespace Hyprutils::Math; @@ -85,5 +86,14 @@ int main(int argc, char** argv, char** envp) { EXPECT(box.inside(CBox(0, 0, 100, 100)), false); } + // Test matrices + { + Mat3x3 jeremy = Mat3x3::outputProjection({1920, 1080}, HYPRUTILS_TRANSFORM_FLIPPED_90); + Mat3x3 matrixBox = jeremy.projectBox(CBox{10, 10, 200, 200}, HYPRUTILS_TRANSFORM_NORMAL).translate({100, 100}).scale({1.25F, 1.5F}).transpose(); + + Mat3x3 expected = std::array{0, -0.46296296, 0, 0.3125, 0, 0, -169.16667, 172.96297, 1}; + EXPECT(matrixBox, expected); + } + return ret; } \ No newline at end of file diff --git a/tests/shared.hpp b/tests/shared.hpp index e738e5a..131764c 100644 --- a/tests/shared.hpp +++ b/tests/shared.hpp @@ -13,7 +13,7 @@ namespace Colors { #define EXPECT(expr, val) \ if (const auto RESULT = expr; RESULT != (val)) { \ - std::cout << Colors::RED << "Failed: " << Colors::RESET << #expr << ", expected " << #val << " but got " << RESULT << "\n"; \ + std::cout << Colors::RED << "Failed: " << Colors::RESET << #expr << ", expected " << val << " but got " << RESULT << "\n"; \ ret = 1; \ } else { \ std::cout << Colors::GREEN << "Passed " << Colors::RESET << #expr << ". Got " << val << "\n"; \ From 6cdbb7597457be93f7c682cc178759f25b51fcf6 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Tue, 24 Sep 2024 18:13:57 +0100 Subject: [PATCH 2/5] oopes --- src/math/Mat3x3.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/math/Mat3x3.cpp b/src/math/Mat3x3.cpp index 8b2d24e..f1b2bc8 100644 --- a/src/math/Mat3x3.cpp +++ b/src/math/Mat3x3.cpp @@ -84,7 +84,7 @@ Mat3x3 Mat3x3::projectBox(const CBox& box, eTransform transform, float rot /* ra mat.translate({-0.5, -0.5}); } - return mat.multiply(*this); + return this->copy().multiply(mat); } Mat3x3& Mat3x3::transform(eTransform transform) { From 720b6562f0f09610543b5713281b708e1167c6ba Mon Sep 17 00:00:00 2001 From: Vaxry Date: Tue, 24 Sep 2024 18:14:53 +0100 Subject: [PATCH 3/5] oopes --- src/math/Mat3x3.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/math/Mat3x3.cpp b/src/math/Mat3x3.cpp index f1b2bc8..7806c41 100644 --- a/src/math/Mat3x3.cpp +++ b/src/math/Mat3x3.cpp @@ -46,8 +46,8 @@ Mat3x3 Mat3x3::outputProjection(const Vector2D& size, eTransform transform) { // Rotation + reflection mat.matrix[0] = x * t.matrix[0]; mat.matrix[1] = x * t.matrix[1]; - mat.matrix[3] = y * -t.matrix[3]; - mat.matrix[4] = y * -t.matrix[4]; + mat.matrix[3] = y * t.matrix[3]; + mat.matrix[4] = y * t.matrix[4]; // Translation mat.matrix[2] = -copysign(1.0f, mat.matrix[0] + mat.matrix[1]); From adc2233e17fdd49025251c9dfd5090cb859ada2e Mon Sep 17 00:00:00 2001 From: Vaxry Date: Tue, 24 Sep 2024 18:15:28 +0100 Subject: [PATCH 4/5] XD --- include/hyprutils/math/Mat3x3.hpp | 3 ++- src/math/Mat3x3.cpp | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/include/hyprutils/math/Mat3x3.hpp b/include/hyprutils/math/Mat3x3.hpp index c58a4c9..72f8e87 100644 --- a/include/hyprutils/math/Mat3x3.hpp +++ b/include/hyprutils/math/Mat3x3.hpp @@ -40,9 +40,10 @@ namespace Hyprutils { Mat3x3& multiply(const Mat3x3& other); /* misc utils */ + Mat3x3 copy() const; std::string toString() const; - bool operator==(const Mat3x3& other) const { + bool operator==(const Mat3x3& other) const { return other.matrix == matrix; } diff --git a/src/math/Mat3x3.cpp b/src/math/Mat3x3.cpp index 7806c41..58e2167 100644 --- a/src/math/Mat3x3.cpp +++ b/src/math/Mat3x3.cpp @@ -135,6 +135,10 @@ Mat3x3& Mat3x3::multiply(const Mat3x3& other) { return *this; } +Mat3x3 Mat3x3::copy() const { + return *this; +} + std::string Mat3x3::toString() const { return std::format("[mat3x3: {}, {}, {}, {}, {}, {}, {}, {}, {}]", matrix.at(0), matrix.at(1), matrix.at(2), matrix.at(3), matrix.at(4), matrix.at(5), matrix.at(6), matrix.at(7), matrix.at(8)); From 6e300332bf3c73f475776607145c09bc5b334233 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Tue, 24 Sep 2024 18:16:10 +0100 Subject: [PATCH 5/5] jfk --- tests/math.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/math.cpp b/tests/math.cpp index 10f0bd3..6c80151 100644 --- a/tests/math.cpp +++ b/tests/math.cpp @@ -91,7 +91,7 @@ int main(int argc, char** argv, char** envp) { Mat3x3 jeremy = Mat3x3::outputProjection({1920, 1080}, HYPRUTILS_TRANSFORM_FLIPPED_90); Mat3x3 matrixBox = jeremy.projectBox(CBox{10, 10, 200, 200}, HYPRUTILS_TRANSFORM_NORMAL).translate({100, 100}).scale({1.25F, 1.5F}).transpose(); - Mat3x3 expected = std::array{0, -0.46296296, 0, 0.3125, 0, 0, -169.16667, 172.96297, 1}; + Mat3x3 expected = std::array{0, 0.46296296, 0, 0.3125, 0, 0, 19.84375, 36.055557, 1}; EXPECT(matrixBox, expected); }