Skip to content

Commit

Permalink
Math: init Region, Vector2D and Box
Browse files Browse the repository at this point in the history
  • Loading branch information
vaxerski committed Jun 18, 2024
1 parent a8f9373 commit e9d4a99
Show file tree
Hide file tree
Showing 9 changed files with 852 additions and 3 deletions.
15 changes: 12 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ endif()
file(GLOB_RECURSE SRCFILES CONFIGURE_DEPENDS "src/*.cpp" "include/*.hpp")
file(GLOB_RECURSE PUBLIC_HEADERS CONFIGURE_DEPENDS "include/*.hpp")

find_package(PkgConfig REQUIRED)
pkg_check_modules(deps REQUIRED IMPORTED_TARGET pixman-1)

add_library(hyprutils SHARED ${SRCFILES})
target_include_directories( hyprutils
PUBLIC "./include"
Expand All @@ -39,25 +42,31 @@ set_target_properties(hyprutils PROPERTIES
VERSION ${hyprutils_VERSION}
SOVERSION 0
)
target_link_libraries(hyprutils PkgConfig::deps)

# tests
add_custom_target(tests)

add_executable(hyprutils_memory "tests/memory.cpp")
target_link_libraries(hyprutils_memory PRIVATE hyprutils)
target_link_libraries(hyprutils_memory PRIVATE hyprutils PkgConfig::deps)
add_test(NAME "Memory" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests COMMAND hyprutils_memory "memory")
add_dependencies(tests hyprutils_memory)

add_executable(hyprutils_string "tests/string.cpp")
target_link_libraries(hyprutils_string PRIVATE hyprutils)
target_link_libraries(hyprutils_string PRIVATE hyprutils PkgConfig::deps)
add_test(NAME "String" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests COMMAND hyprutils_string "string")
add_dependencies(tests hyprutils_string)

add_executable(hyprutils_signal "tests/signal.cpp")
target_link_libraries(hyprutils_signal PRIVATE hyprutils)
target_link_libraries(hyprutils_signal PRIVATE hyprutils PkgConfig::deps)
add_test(NAME "Signal" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests COMMAND hyprutils_signal "signal")
add_dependencies(tests hyprutils_signal)

add_executable(hyprutils_math "tests/math.cpp")
target_link_libraries(hyprutils_math PRIVATE hyprutils PkgConfig::deps)
add_test(NAME "Math" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests COMMAND hyprutils_math "math")
add_dependencies(tests hyprutils_math)

# Installation
install(TARGETS hyprutils)
install(DIRECTORY "include/hyprutils" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
Expand Down
106 changes: 106 additions & 0 deletions include/hyprutils/math/Box.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#pragma once

#include "./Vector2D.hpp"
#include "./Misc.hpp"

namespace Hyprutils {
namespace Math {
struct SBoxExtents {
Vector2D topLeft;
Vector2D bottomRight;

//
SBoxExtents operator*(const double& scale) const {
return SBoxExtents{topLeft * scale, bottomRight * scale};
}

SBoxExtents round() {
return {topLeft.round(), bottomRight.round()};
}

bool operator==(const SBoxExtents& other) const {
return topLeft == other.topLeft && bottomRight == other.bottomRight;
}

void addExtents(const SBoxExtents& other) {
topLeft = topLeft.getComponentMax(other.topLeft);
bottomRight = bottomRight.getComponentMax(other.bottomRight);
}
};

class CBox {
public:
CBox(double x_, double y_, double w_, double h_) {
x = x_;
y = y_;
w = w_;
h = h_;
}

CBox() {
w = 0;
h = 0;
}

CBox(const double d) {
x = d;
y = d;
w = d;
h = d;
}

CBox(const Vector2D& pos, const Vector2D& size) {
x = pos.x;
y = pos.y;
w = size.x;
h = size.y;
}

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;

SBoxExtents extentsFrom(const CBox&); // this is the big box

Vector2D middle() const;
Vector2D pos() const;
Vector2D size() const;
Vector2D closestPoint(const Vector2D& vec) const;

bool containsPoint(const Vector2D& vec) const;
bool empty() const;

double x = 0, y = 0;
union {
double w;
double width;
};
union {
double h;
double height;
};

double rot = 0; /* rad, ccw */

//
bool operator==(const CBox& rhs) const {
return x == rhs.x && y == rhs.y && w == rhs.w && h == rhs.h;
}

private:
CBox roundInternal();
};
}
}
16 changes: 16 additions & 0 deletions include/hyprutils/math/Misc.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

namespace Hyprutils {
namespace Math {
enum eTransform {
HYPRUTILS_TRANSFORM_NORMAL = 0,
HYPRUTILS_TRANSFORM_90 = 1,
HYPRUTILS_TRANSFORM_180 = 2,
HYPRUTILS_TRANSFORM_270 = 3,
HYPRUTILS_TRANSFORM_FLIPPED = 4,
HYPRUTILS_TRANSFORM_FLIPPED_90 = 5,
HYPRUTILS_TRANSFORM_FLIPPED_180 = 6,
HYPRUTILS_TRANSFORM_FLIPPED_270 = 7,
};
}
}
70 changes: 70 additions & 0 deletions include/hyprutils/math/Region.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#pragma once

#include <pixman.h>
#include <vector>
#include "Vector2D.hpp"
#include "Box.hpp"

namespace Hyprutils {
namespace Math {
class CRegion {
public:
/* Create an empty region */
CRegion();
/* Create from a reference. Copies, does not own. */
CRegion(const pixman_region32_t* const ref);
/* Create from a box */
CRegion(double x, double y, double w, double h);
/* Create from a CBox */
CRegion(const CBox& box);
/* Create from a pixman_box32_t */
CRegion(pixman_box32_t* box);

CRegion(const CRegion&);
CRegion(CRegion&&);

~CRegion();

CRegion& operator=(CRegion&& other) {
pixman_region32_copy(&m_rRegion, other.pixman());
return *this;
}

CRegion& operator=(CRegion& other) {
pixman_region32_copy(&m_rRegion, other.pixman());
return *this;
}

CRegion& clear();
CRegion& set(const CRegion& other);
CRegion& add(const CRegion& other);
CRegion& add(double x, double y, double w, double h);
CRegion& add(const CBox& other);
CRegion& subtract(const CRegion& other);
CRegion& intersect(const CRegion& other);
CRegion& intersect(double x, double y, double w, double h);
CRegion& translate(const Vector2D& vec);
CRegion& transform(const eTransform t, double w, double h);
CRegion& invert(pixman_box32_t* box);
CRegion& invert(const CBox& box);
CRegion& scale(float scale);
CRegion& scale(const Vector2D& scale);
CRegion& rationalize();
CBox getExtents();
bool containsPoint(const Vector2D& vec) const;
bool empty() const;
Vector2D closestPoint(const Vector2D& vec) const;
CRegion copy() const;

std::vector<pixman_box32_t> getRects() const;

//
pixman_region32_t* pixman() {
return &m_rRegion;
}

private:
pixman_region32_t m_rRegion;
};
}
}
Loading

0 comments on commit e9d4a99

Please sign in to comment.