From 32269aad5448c3cbf87d3cd5b48eb7a5bffa0865 Mon Sep 17 00:00:00 2001 From: Jasson Date: Sun, 23 Jun 2024 09:50:32 -0400 Subject: [PATCH 1/6] Added some constants to handle floating point presicion comparisons and other calculations plus some refactoring --- src/math/Box.cpp | 70 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 22 deletions(-) diff --git a/src/math/Box.cpp b/src/math/Box.cpp index 2fa77a6..4f93c25 100644 --- a/src/math/Box.cpp +++ b/src/math/Box.cpp @@ -7,6 +7,10 @@ using namespace Hyprutils::Math; +constexpr double HALF = 0.5; +constexpr double DOUBLE = 2.0; +constexpr double EPSILON = 1e-9; + CBox& Hyprutils::Math::CBox::scale(double scale) { x *= scale; y *= scale; @@ -33,7 +37,7 @@ CBox& Hyprutils::Math::CBox::translate(const Vector2D& vec) { } Vector2D Hyprutils::Math::CBox::middle() const { - return Vector2D{x + w / 2.0, y + h / 2.0}; + return Vector2D{x + w * HALF, y + h * HALF}; } bool Hyprutils::Math::CBox::containsPoint(const Vector2D& vec) const { @@ -41,14 +45,17 @@ bool Hyprutils::Math::CBox::containsPoint(const Vector2D& vec) const { } bool Hyprutils::Math::CBox::empty() const { - return w == 0 || h == 0; + return std::fabs(w) < EPSILON || std::fabs(h) < EPSILON; } CBox& Hyprutils::Math::CBox::round() { - float newW = x + w - std::round(x); - float newH = y + h - std::round(y); - x = std::round(x); - y = std::round(y); + double roundedX = std::round(x); + double roundedY = std::round(y); + double newW = x + w - roundedX; + double newH = y + h - roundedY; + + x = roundedX; + y = roundedY; w = std::round(newW); h = std::round(newH); @@ -56,6 +63,12 @@ CBox& Hyprutils::Math::CBox::round() { } CBox& Hyprutils::Math::CBox::transform(const eTransform t, double w, double h) { + + //Validate transforamtion + if (t < HYPRUTILS_TRANSFORM_NORMAL || t > HYPRUTILS_TRANSFORM_FLIPPED_270) { + throw std::invalid_argument("Invalid transformation type"); + } + CBox temp = *this; if (t % 2 == 0) { @@ -119,8 +132,8 @@ CBox& Hyprutils::Math::CBox::scaleFromCenter(double scale) { w *= scale; h *= scale; - x -= (w - oldW) / 2.0; - y -= (h - oldH) / 2.0; + x -= (w - oldW) * HALF; + y -= (h - oldH) * HALF; return *this; } @@ -128,10 +141,10 @@ CBox& Hyprutils::Math::CBox::scaleFromCenter(double scale) { CBox& Hyprutils::Math::CBox::expand(const double& value) { x -= value; y -= value; - w += value * 2.0; - h += value * 2.0; + w += value * DOUBLE; + h += value * DOUBLE; - if (w <= 0 || h <= 0) { + if (w <= EPSILON || h <= EPSILON) { w = 0; h = 0; } @@ -147,14 +160,14 @@ CBox& Hyprutils::Math::CBox::noNegativeSize() { } CBox Hyprutils::Math::CBox::intersection(const CBox& other) const { - const float newX = std::max(x, other.x); - const float newY = std::max(y, other.y); - const float newBottom = std::min(y + h, other.y + other.h); - const float newRight = std::min(x + w, other.x + other.w); - float newW = newRight - newX; - float newH = newBottom - newY; - - if (newW <= 0 || newH <= 0) { + const double newX = std::max(x, other.x); + const double newY = std::max(y, other.y); + const double newBottom = std::min(y + h, other.y + other.h); + const double newRight = std::min(x + w, other.x + other.w); + double newW = newRight - newX; + double newH = newBottom - newY; + + if (newW <= EPSILON || newH <= EPSILON) { newW = 0; newH = 0; } @@ -171,10 +184,12 @@ bool Hyprutils::Math::CBox::inside(const CBox& bound) const { } CBox Hyprutils::Math::CBox::roundInternal() { - float newW = x + w - std::floor(x); - float newH = y + h - std::floor(y); + double flooredX = std::floor(x); + double flooredY = std::floor(y); + double newW = x + w - flooredX; + double newH = y + h - flooredY; - return CBox{std::floor(x), std::floor(y), std::floor(newW), std::floor(newH)}; + return CBox{flooredX, flooredY, std::floor(newW), std::floor(newH)}; } CBox Hyprutils::Math::CBox::copy() const { @@ -196,6 +211,17 @@ Vector2D Hyprutils::Math::CBox::closestPoint(const Vector2D& vec) const { Vector2D nv = vec; nv.x = std::clamp(nv.x, x, x + w); nv.y = std::clamp(nv.y, y, y + h); + + if (std::fabs(nv.x - x) < EPSILON) + nv.x = x; + else if (std::fabs(nv.x - (x + w)) < EPSILON) + nv.x = x + w; + + if (std::fabs(nv.y - y) < EPSILON) + nv.y = y; + else if (std::fabs(nv.y - (y + h)) < EPSILON) + nv.y = y + h; + return nv; } From 8edd0c4ba402ad509b5c9a8c2a9a22baad60e4ef Mon Sep 17 00:00:00 2001 From: Jasson Date: Sun, 23 Jun 2024 10:08:11 -0400 Subject: [PATCH 2/6] Removed validation --- src/math/Box.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/math/Box.cpp b/src/math/Box.cpp index 4f93c25..21b5df8 100644 --- a/src/math/Box.cpp +++ b/src/math/Box.cpp @@ -64,11 +64,6 @@ CBox& Hyprutils::Math::CBox::round() { CBox& Hyprutils::Math::CBox::transform(const eTransform t, double w, double h) { - //Validate transforamtion - if (t < HYPRUTILS_TRANSFORM_NORMAL || t > HYPRUTILS_TRANSFORM_FLIPPED_270) { - throw std::invalid_argument("Invalid transformation type"); - } - CBox temp = *this; if (t % 2 == 0) { From 008947fc93b6c6af0845a291ba173684978c26a2 Mon Sep 17 00:00:00 2001 From: Jasson Date: Sun, 23 Jun 2024 23:48:24 -0400 Subject: [PATCH 3/6] Added comments to understand how box header works --- include/hyprutils/math/Box.hpp | 105 ++++++++++++++++++++++++++++----- 1 file changed, 91 insertions(+), 14 deletions(-) diff --git a/include/hyprutils/math/Box.hpp b/include/hyprutils/math/Box.hpp index ea974c5..bbf85c3 100644 --- a/include/hyprutils/math/Box.hpp +++ b/include/hyprutils/math/Box.hpp @@ -3,52 +3,89 @@ #include "./Vector2D.hpp" #include "./Misc.hpp" -namespace Hyprutils { - namespace Math { +namespace Hyprutils::Math { + + /** + * @brief Represents the extents of a bounding box. + */ struct SBoxExtents { Vector2D topLeft; Vector2D bottomRight; - // + /** + * @brief Scales the extents by a given factor. + * @param scale The scaling factor. + * @return Scaled SBoxExtents. + */ SBoxExtents operator*(const double& scale) const { return SBoxExtents{topLeft * scale, bottomRight * scale}; } - + /** + * @brief Rounds the coordinates of the extents. + * @return Rounded SBoxExtents. + */ SBoxExtents round() { return {topLeft.round(), bottomRight.round()}; } - + /** + * @brief Checks equality between two SBoxExtents objects. + * @param other Another SBoxExtents object to compare. + * @return True if both SBoxExtents are equal, false otherwise. + */ bool operator==(const SBoxExtents& other) const { return topLeft == other.topLeft && bottomRight == other.bottomRight; } + /** + * @brief Adjusts the extents to encompass another SBoxExtents. + * @param other Another SBoxExtents to add to this one. + */ void addExtents(const SBoxExtents& other) { topLeft = topLeft.getComponentMax(other.topLeft); bottomRight = bottomRight.getComponentMax(other.bottomRight); } }; + /** + * @brief Represents a 2D bounding box. + */ class CBox { public: + /** + * @brief Constructs a CBox with specified position and dimensions. + * @param x_ X-coordinate of the top-left corner. + * @param y_ Y-coordinate of the top-left corner. + * @param w_ Width of the box. + * @param h_ Height of the box. + */ CBox(double x_, double y_, double w_, double h_) { x = x_; y = y_; w = w_; h = h_; } - + /** + * @brief Default constructor. Initializes an empty box (0 width, 0 height). + */ CBox() { w = 0; h = 0; } - + /** + * @brief Constructs a CBox with uniform dimensions. + * @param d Dimensions to apply uniformly (x, y, width, height). + */ CBox(const double d) { x = d; y = d; w = d; h = d; } - + /** + * @brief Constructs a CBox from a position and size vector. + * @param pos Position vector representing the top-left corner. + * @param size Size vector representing width and height. + */ CBox(const Vector2D& pos, const Vector2D& size) { x = pos.x; y = pos.y; @@ -56,6 +93,7 @@ namespace Hyprutils { h = size.y; } + // Geometric operations CBox& applyFromWlr(); CBox& scale(double scale); CBox& scaleFromCenter(double scale); @@ -72,17 +110,52 @@ namespace Hyprutils { bool overlaps(const CBox& other) const; bool inside(const CBox& bound) const; + /** + * @brief Computes the extents of the box relative to another box. + * @param small Another CBox to compare against. + * @return SBoxExtents representing the extents of the box relative to 'small'. + */ SBoxExtents extentsFrom(const CBox&); // this is the big box - + + /** + * @brief Calculates the middle point of the box. + * @return Vector2D representing the middle point. + */ Vector2D middle() const; + + /** + * @brief Retrieves the position of the top-left corner of the box. + * @return Vector2D representing the position. + */ Vector2D pos() const; + + /** + * @brief Retrieves the size (width and height) of the box. + * @return Vector2D representing the size. + */ Vector2D size() const; + + /** + * @brief Finds the closest point within the box to a given vector. + * @param vec Vector from which to find the closest point. + * @return Vector2D representing the closest point within the box. + */ Vector2D closestPoint(const Vector2D& vec) const; + /** + * @brief Checks if a given point is inside the box. + * @param vec Vector representing the point to check. + * @return True if the point is inside the box, false otherwise. + */ bool containsPoint(const Vector2D& vec) const; + + /** + * @brief Checks if the box is empty (zero width or height). + * @return True if the box is empty, false otherwise. + */ bool empty() const; - double x = 0, y = 0; + double x = 0, y = 0; // Position of the top-left corner of the box. union { double w; double width; @@ -92,9 +165,14 @@ namespace Hyprutils { double height; }; - double rot = 0; /* rad, ccw */ + double rot = 0; //< Rotation angle of the box in radians (counterclockwise). + - // + /** + * @brief Checks equality between two CBox objects. + * @param rhs Another CBox object to compare. + * @return True if both CBox objects are equal, false otherwise. + */ bool operator==(const CBox& rhs) const { return x == rhs.x && y == rhs.y && w == rhs.w && h == rhs.h; } @@ -102,5 +180,4 @@ namespace Hyprutils { private: CBox roundInternal(); }; - } -} \ No newline at end of file + } \ No newline at end of file From 9804c00d5eb4cf69cbedbc0f735cf21a0b03eb9d Mon Sep 17 00:00:00 2001 From: Jasson Date: Sun, 23 Jun 2024 23:49:41 -0400 Subject: [PATCH 4/6] Extended the EXPECT macro to evaluate Vector2D test cases --- tests/shared.hpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/shared.hpp b/tests/shared.hpp index ce3d771..2e8c375 100644 --- a/tests/shared.hpp +++ b/tests/shared.hpp @@ -18,3 +18,14 @@ namespace Colors { } else { \ std::cout << Colors::GREEN << "Passed " << Colors::RESET << #expr << ". Got " << val << "\n"; \ } +#define EXPECT_VECTOR2D(expr, val) \ + do { \ + const auto& RESULT = expr; \ + const auto& EXPECTED = val; \ + if (!(std::abs(RESULT.x - EXPECTED.x) < 1e-6 && std::abs(RESULT.y - EXPECTED.y) < 1e-6)) { \ + std::cout << Colors::RED << "Failed: " << Colors::RESET << #expr << ", expected (" << EXPECTED.x << ", " << EXPECTED.y << ") but got (" << RESULT.x << ", " << RESULT.y << ")\n"; \ + ret = 1; \ + } else { \ + std::cout << Colors::GREEN << "Passed " << Colors::RESET << #expr << ". Got (" << RESULT.x << ", " << RESULT.y << ")\n"; \ + } \ + } while(0) From cf268dc0ca89b9ca51ae962f80d2ae8e41e9ec9d Mon Sep 17 00:00:00 2001 From: Jasson Date: Sun, 23 Jun 2024 23:50:29 -0400 Subject: [PATCH 5/6] Added box.cpp test cases --- tests/math.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/tests/math.cpp b/tests/math.cpp index cb47189..d96f745 100644 --- a/tests/math.cpp +++ b/tests/math.cpp @@ -17,5 +17,73 @@ int main(int argc, char** argv, char** envp) { EXPECT(rg.getExtents().width, 90); EXPECT(rg.getExtents().height, 190); + /*Box.cpp test cases*/ + // Test default constructor and accessors + { + CBox box1; + EXPECT(box1.x, 0); + EXPECT(box1.y, 0); + EXPECT(box1.width, 0); + EXPECT(box1.height, 0); + + // Test parameterized constructor and accessors + CBox box2(10, 20, 30, 40); + EXPECT(box2.x, 10); + EXPECT(box2.y, 20); + EXPECT(box2.width, 30); + EXPECT(box2.height, 40); + + // Test setters and getters + box2.translate(Vector2D(5, -5)); + EXPECT_VECTOR2D(box2.pos(), Vector2D(15, 15)); + } + + //Test Scaling and Transformation + { + CBox box(10, 10, 20, 30); + + // Test scaling + box.scale(2.0); + EXPECT_VECTOR2D(box.size(), Vector2D(40, 60)); + EXPECT_VECTOR2D(box.pos(), Vector2D(20, 20)); + + // Test scaling from center + box.scaleFromCenter(0.5); + EXPECT_VECTOR2D(box.size(), Vector2D(20, 30)); + EXPECT_VECTOR2D(box.pos(), Vector2D(30, 35)); + + // Test transformation + box.transform(HYPRUTILS_TRANSFORM_90, 100, 200); + EXPECT_VECTOR2D(box.pos(), Vector2D(135, 30)); + EXPECT_VECTOR2D(box.size(), Vector2D(30, 20)); + + // Test Intersection and Extents + } + + { + CBox box1(0, 0, 100, 100); + CBox box2(50, 50, 100, 100); + + CBox intersection = box1.intersection(box2); + EXPECT_VECTOR2D(intersection.pos(), Vector2D(50, 50)); + EXPECT_VECTOR2D(intersection.size(), Vector2D(50, 50)); + + SBoxExtents extents = box1.extentsFrom(box2); + EXPECT_VECTOR2D(extents.topLeft, Vector2D(50, 50)); + EXPECT_VECTOR2D(extents.bottomRight, Vector2D(-50, -50)); + } + + // Test Boundary Conditions and Special Cases + { + CBox box(0, 0, 50, 50); + + EXPECT(box.empty(), false); + + EXPECT(box.containsPoint(Vector2D(25, 25)), true); + EXPECT(box.containsPoint(Vector2D(60, 60)), false); + EXPECT(box.overlaps(CBox(25, 25, 50, 50)), true); + EXPECT(box.inside(CBox(0, 0, 100, 100)), false); + } + return ret; } \ No newline at end of file From 118a96a96f17d9e5da232b8372bf5365744e67c2 Mon Sep 17 00:00:00 2001 From: Jasson Date: Mon, 24 Jun 2024 17:32:16 -0400 Subject: [PATCH 6/6] Applied clang-format --- include/hyprutils/math/Box.hpp | 211 ++++++++++++++++----------------- src/math/Box.cpp | 26 ++-- tests/math.cpp | 66 +++++------ tests/shared.hpp | 11 +- 4 files changed, 157 insertions(+), 157 deletions(-) diff --git a/include/hyprutils/math/Box.hpp b/include/hyprutils/math/Box.hpp index bbf85c3..db10428 100644 --- a/include/hyprutils/math/Box.hpp +++ b/include/hyprutils/math/Box.hpp @@ -4,180 +4,179 @@ #include "./Misc.hpp" namespace Hyprutils::Math { - - /** + + /** * @brief Represents the extents of a bounding box. */ - struct SBoxExtents { - Vector2D topLeft; - Vector2D bottomRight; + struct SBoxExtents { + Vector2D topLeft; + Vector2D bottomRight; - /** + /** * @brief Scales the extents by a given factor. * @param scale The scaling factor. * @return Scaled SBoxExtents. */ - SBoxExtents operator*(const double& scale) const { - return SBoxExtents{topLeft * scale, bottomRight * scale}; - } - /** + SBoxExtents operator*(const double& scale) const { + return SBoxExtents{topLeft * scale, bottomRight * scale}; + } + /** * @brief Rounds the coordinates of the extents. * @return Rounded SBoxExtents. */ - SBoxExtents round() { - return {topLeft.round(), bottomRight.round()}; - } - /** + SBoxExtents round() { + return {topLeft.round(), bottomRight.round()}; + } + /** * @brief Checks equality between two SBoxExtents objects. * @param other Another SBoxExtents object to compare. * @return True if both SBoxExtents are equal, false otherwise. */ - bool operator==(const SBoxExtents& other) const { - return topLeft == other.topLeft && bottomRight == other.bottomRight; - } + bool operator==(const SBoxExtents& other) const { + return topLeft == other.topLeft && bottomRight == other.bottomRight; + } - /** + /** * @brief Adjusts the extents to encompass another SBoxExtents. * @param other Another SBoxExtents to add to this one. */ - void addExtents(const SBoxExtents& other) { - topLeft = topLeft.getComponentMax(other.topLeft); - bottomRight = bottomRight.getComponentMax(other.bottomRight); - } - }; + void addExtents(const SBoxExtents& other) { + topLeft = topLeft.getComponentMax(other.topLeft); + bottomRight = bottomRight.getComponentMax(other.bottomRight); + } + }; - /** + /** * @brief Represents a 2D bounding box. */ - class CBox { - public: - /** + class CBox { + public: + /** * @brief Constructs a CBox with specified position and dimensions. * @param x_ X-coordinate of the top-left corner. * @param y_ Y-coordinate of the top-left corner. * @param w_ Width of the box. * @param h_ Height of the box. */ - CBox(double x_, double y_, double w_, double h_) { - x = x_; - y = y_; - w = w_; - h = h_; - } - /** + CBox(double x_, double y_, double w_, double h_) { + x = x_; + y = y_; + w = w_; + h = h_; + } + /** * @brief Default constructor. Initializes an empty box (0 width, 0 height). */ - CBox() { - w = 0; - h = 0; - } - /** + CBox() { + w = 0; + h = 0; + } + /** * @brief Constructs a CBox with uniform dimensions. * @param d Dimensions to apply uniformly (x, y, width, height). */ - CBox(const double d) { - x = d; - y = d; - w = d; - h = d; - } - /** + CBox(const double d) { + x = d; + y = d; + w = d; + h = d; + } + /** * @brief Constructs a CBox from a position and size vector. * @param pos Position vector representing the top-left corner. * @param size Size vector representing width and height. */ - CBox(const Vector2D& pos, const Vector2D& size) { - x = pos.x; - y = pos.y; - w = size.x; - h = size.y; - } - - // Geometric operations - CBox& applyFromWlr(); - CBox& scale(double scale); - CBox& scaleFromCenter(double scale); - CBox& scale(const Vector2D& scale); - CBox& translate(const Vector2D& vec); - CBox& round(); - CBox& transform(const eTransform t, double w, double h); - CBox& addExtents(const SBoxExtents& e); - CBox& expand(const double& value); - CBox& noNegativeSize(); - - CBox copy() const; - CBox intersection(const CBox& other) const; - bool overlaps(const CBox& other) const; - bool inside(const CBox& bound) const; - - /** + CBox(const Vector2D& pos, const Vector2D& size) { + x = pos.x; + y = pos.y; + w = size.x; + h = size.y; + } + + // Geometric operations + CBox& applyFromWlr(); + CBox& scale(double scale); + CBox& scaleFromCenter(double scale); + CBox& scale(const Vector2D& scale); + CBox& translate(const Vector2D& vec); + CBox& round(); + CBox& transform(const eTransform t, double w, double h); + CBox& addExtents(const SBoxExtents& e); + CBox& expand(const double& value); + CBox& noNegativeSize(); + + CBox copy() const; + CBox intersection(const CBox& other) const; + bool overlaps(const CBox& other) const; + bool inside(const CBox& bound) const; + + /** * @brief Computes the extents of the box relative to another box. * @param small Another CBox to compare against. * @return SBoxExtents representing the extents of the box relative to 'small'. */ - SBoxExtents extentsFrom(const CBox&); // this is the big box - - /** + SBoxExtents extentsFrom(const CBox&); // this is the big box + + /** * @brief Calculates the middle point of the box. * @return Vector2D representing the middle point. */ - Vector2D middle() const; + Vector2D middle() const; - /** + /** * @brief Retrieves the position of the top-left corner of the box. * @return Vector2D representing the position. */ - Vector2D pos() const; + Vector2D pos() const; - /** + /** * @brief Retrieves the size (width and height) of the box. * @return Vector2D representing the size. */ - Vector2D size() const; - - /** + Vector2D size() const; + + /** * @brief Finds the closest point within the box to a given vector. * @param vec Vector from which to find the closest point. * @return Vector2D representing the closest point within the box. */ - Vector2D closestPoint(const Vector2D& vec) const; + Vector2D closestPoint(const Vector2D& vec) const; - /** + /** * @brief Checks if a given point is inside the box. * @param vec Vector representing the point to check. * @return True if the point is inside the box, false otherwise. */ - bool containsPoint(const Vector2D& vec) const; - - /** + bool containsPoint(const Vector2D& vec) const; + + /** * @brief Checks if the box is empty (zero width or height). * @return True if the box is empty, false otherwise. */ - bool empty() const; + bool empty() const; - double x = 0, y = 0; // Position of the top-left corner of the box. - union { - double w; - double width; - }; - union { - double h; - double height; - }; - - double rot = 0; //< Rotation angle of the box in radians (counterclockwise). + double x = 0, y = 0; // Position of the top-left corner of the box. + union { + double w; + double width; + }; + union { + double h; + double height; + }; + double rot = 0; //< Rotation angle of the box in radians (counterclockwise). - /** + /** * @brief Checks equality between two CBox objects. * @param rhs Another CBox object to compare. * @return True if both CBox objects are equal, false otherwise. */ - bool operator==(const CBox& rhs) const { - return x == rhs.x && y == rhs.y && w == rhs.w && h == rhs.h; - } + bool operator==(const CBox& rhs) const { + return x == rhs.x && y == rhs.y && w == rhs.w && h == rhs.h; + } - private: - CBox roundInternal(); - }; - } \ No newline at end of file + private: + CBox roundInternal(); + }; +} \ No newline at end of file diff --git a/src/math/Box.cpp b/src/math/Box.cpp index 21b5df8..21e8fe5 100644 --- a/src/math/Box.cpp +++ b/src/math/Box.cpp @@ -7,11 +7,11 @@ using namespace Hyprutils::Math; -constexpr double HALF = 0.5; -constexpr double DOUBLE = 2.0; +constexpr double HALF = 0.5; +constexpr double DOUBLE = 2.0; constexpr double EPSILON = 1e-9; -CBox& Hyprutils::Math::CBox::scale(double scale) { +CBox& Hyprutils::Math::CBox::scale(double scale) { x *= scale; y *= scale; w *= scale; @@ -51,13 +51,13 @@ bool Hyprutils::Math::CBox::empty() const { CBox& Hyprutils::Math::CBox::round() { double roundedX = std::round(x); double roundedY = std::round(y); - double newW = x + w - roundedX; - double newH = y + h - roundedY; - - x = roundedX; - y = roundedY; - w = std::round(newW); - h = std::round(newH); + double newW = x + w - roundedX; + double newH = y + h - roundedY; + + x = roundedX; + y = roundedY; + w = std::round(newW); + h = std::round(newH); return *this; } @@ -179,10 +179,10 @@ bool Hyprutils::Math::CBox::inside(const CBox& bound) const { } CBox Hyprutils::Math::CBox::roundInternal() { - double flooredX = std::floor(x); + double flooredX = std::floor(x); double flooredY = std::floor(y); - double newW = x + w - flooredX; - double newH = y + h - flooredY; + double newW = x + w - flooredX; + double newH = y + h - flooredY; return CBox{flooredX, flooredY, std::floor(newW), std::floor(newH)}; } diff --git a/tests/math.cpp b/tests/math.cpp index d96f745..8a29774 100644 --- a/tests/math.cpp +++ b/tests/math.cpp @@ -11,7 +11,7 @@ int main(int argc, char** argv, char** envp) { EXPECT(rg.getExtents().height, 200); EXPECT(rg.getExtents().width, 100); - + rg.intersect(CBox{10, 10, 300, 300}); EXPECT(rg.getExtents().width, 90); @@ -20,47 +20,47 @@ int main(int argc, char** argv, char** envp) { /*Box.cpp test cases*/ // Test default constructor and accessors { - CBox box1; - EXPECT(box1.x, 0); - EXPECT(box1.y, 0); - EXPECT(box1.width, 0); - EXPECT(box1.height, 0); - - // Test parameterized constructor and accessors - CBox box2(10, 20, 30, 40); - EXPECT(box2.x, 10); - EXPECT(box2.y, 20); - EXPECT(box2.width, 30); - EXPECT(box2.height, 40); - - // Test setters and getters - box2.translate(Vector2D(5, -5)); - EXPECT_VECTOR2D(box2.pos(), Vector2D(15, 15)); + CBox box1; + EXPECT(box1.x, 0); + EXPECT(box1.y, 0); + EXPECT(box1.width, 0); + EXPECT(box1.height, 0); + + // Test parameterized constructor and accessors + CBox box2(10, 20, 30, 40); + EXPECT(box2.x, 10); + EXPECT(box2.y, 20); + EXPECT(box2.width, 30); + EXPECT(box2.height, 40); + + // Test setters and getters + box2.translate(Vector2D(5, -5)); + EXPECT_VECTOR2D(box2.pos(), Vector2D(15, 15)); } //Test Scaling and Transformation { - CBox box(10, 10, 20, 30); + CBox box(10, 10, 20, 30); - // Test scaling - box.scale(2.0); - EXPECT_VECTOR2D(box.size(), Vector2D(40, 60)); - EXPECT_VECTOR2D(box.pos(), Vector2D(20, 20)); + // Test scaling + box.scale(2.0); + EXPECT_VECTOR2D(box.size(), Vector2D(40, 60)); + EXPECT_VECTOR2D(box.pos(), Vector2D(20, 20)); - // Test scaling from center - box.scaleFromCenter(0.5); - EXPECT_VECTOR2D(box.size(), Vector2D(20, 30)); - EXPECT_VECTOR2D(box.pos(), Vector2D(30, 35)); + // Test scaling from center + box.scaleFromCenter(0.5); + EXPECT_VECTOR2D(box.size(), Vector2D(20, 30)); + EXPECT_VECTOR2D(box.pos(), Vector2D(30, 35)); - // Test transformation - box.transform(HYPRUTILS_TRANSFORM_90, 100, 200); - EXPECT_VECTOR2D(box.pos(), Vector2D(135, 30)); - EXPECT_VECTOR2D(box.size(), Vector2D(30, 20)); + // Test transformation + box.transform(HYPRUTILS_TRANSFORM_90, 100, 200); + EXPECT_VECTOR2D(box.pos(), Vector2D(135, 30)); + EXPECT_VECTOR2D(box.size(), Vector2D(30, 20)); - // Test Intersection and Extents + // Test Intersection and Extents } - { + { CBox box1(0, 0, 100, 100); CBox box2(50, 50, 100, 100); @@ -73,7 +73,7 @@ int main(int argc, char** argv, char** envp) { EXPECT_VECTOR2D(extents.bottomRight, Vector2D(-50, -50)); } - // Test Boundary Conditions and Special Cases + // Test Boundary Conditions and Special Cases { CBox box(0, 0, 50, 50); diff --git a/tests/shared.hpp b/tests/shared.hpp index 2e8c375..e738e5a 100644 --- a/tests/shared.hpp +++ b/tests/shared.hpp @@ -18,14 +18,15 @@ namespace Colors { } else { \ std::cout << Colors::GREEN << "Passed " << Colors::RESET << #expr << ". Got " << val << "\n"; \ } -#define EXPECT_VECTOR2D(expr, val) \ +#define EXPECT_VECTOR2D(expr, val) \ do { \ - const auto& RESULT = expr; \ - const auto& EXPECTED = val; \ + const auto& RESULT = expr; \ + const auto& EXPECTED = val; \ if (!(std::abs(RESULT.x - EXPECTED.x) < 1e-6 && std::abs(RESULT.y - EXPECTED.y) < 1e-6)) { \ - std::cout << Colors::RED << "Failed: " << Colors::RESET << #expr << ", expected (" << EXPECTED.x << ", " << EXPECTED.y << ") but got (" << RESULT.x << ", " << RESULT.y << ")\n"; \ + std::cout << Colors::RED << "Failed: " << Colors::RESET << #expr << ", expected (" << EXPECTED.x << ", " << EXPECTED.y << ") but got (" << RESULT.x << ", " \ + << RESULT.y << ")\n"; \ ret = 1; \ } else { \ std::cout << Colors::GREEN << "Passed " << Colors::RESET << #expr << ". Got (" << RESULT.x << ", " << RESULT.y << ")\n"; \ } \ - } while(0) + } while (0)