diff options
| author | Yuqian Yang <crupest@crupest.life> | 2025-09-21 23:40:30 +0800 |
|---|---|---|
| committer | Yuqian Yang <crupest@crupest.life> | 2025-12-18 17:29:36 +0800 |
| commit | b89a4783e033db1530322a35a1674630c413e847 (patch) | |
| tree | 20d297c70c0047e65a707f41bfbc96318e1bcb1f | |
| parent | 6bbadfc6b1eaf14f68a27cb8e378f5e09ab38de6 (diff) | |
| download | cru-b89a4783e033db1530322a35a1674630c413e847.tar.gz cru-b89a4783e033db1530322a35a1674630c413e847.tar.bz2 cru-b89a4783e033db1530322a35a1674630c413e847.zip | |
HALF WORK!
| -rw-r--r-- | include/cru/platform/GraphicsBase.h | 10 | ||||
| -rw-r--r-- | include/cru/platform/graphics/Geometry.h | 12 | ||||
| -rw-r--r-- | include/cru/platform/graphics/quartz/Geometry.h | 3 | ||||
| -rw-r--r-- | src/platform/graphics/Geometry.cpp | 42 | ||||
| -rw-r--r-- | src/platform/graphics/quartz/Geometry.cpp | 14 | ||||
| -rw-r--r-- | test/platform/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | test/platform/graphics/CMakeLists.txt | 6 | ||||
| -rw-r--r-- | test/platform/graphics/GeometryTest.cpp | 32 |
8 files changed, 118 insertions, 3 deletions
diff --git a/include/cru/platform/GraphicsBase.h b/include/cru/platform/GraphicsBase.h index 0c391b11..4e88ca26 100644 --- a/include/cru/platform/GraphicsBase.h +++ b/include/cru/platform/GraphicsBase.h @@ -2,6 +2,7 @@ #include <cru/base/Range.h> #include <cru/base/StringUtil.h> +#include <cmath> #include <format> #include <limits> @@ -21,6 +22,11 @@ struct Point final { constexpr Point Negate() const { return Point(-x, -y); } + constexpr float Distance(const Point& other) { + auto dx = x - other.x, dy = y - other.y; + return std::sqrt(dx * dx + dy * dy); + } + std::string ToString() const { return std::format("Point(x: {}, y: {})", x, y); } @@ -39,6 +45,10 @@ constexpr Point operator-(const Point& left, const Point& right) { return Point(left.x - right.x, left.y - right.y); } +constexpr Point operator/(const Point& point, float scale) { + return Point(point.x / scale, point.y / scale); +} + struct Size final { constexpr Size() = default; constexpr Size(const float width, const float height) diff --git a/include/cru/platform/graphics/Geometry.h b/include/cru/platform/graphics/Geometry.h index ef661f2a..7d314f0a 100644 --- a/include/cru/platform/graphics/Geometry.h +++ b/include/cru/platform/graphics/Geometry.h @@ -63,6 +63,18 @@ struct CRU_PLATFORM_GRAPHICS_API IGeometry : virtual IGraphicsResource { * virtual so it can override them. */ struct CRU_PLATFORM_GRAPHICS_API IGeometryBuilder : virtual IGraphicsResource { + struct ArcInfo { + Point center; + // In radian. + float start_angle; + // In radian. + float end_angle; + }; + + static ArcInfo CalculateArcInfo(const Point& start_point, const Point& radius, + float angle, bool is_large_arc, bool is_clockwise, + const Point& end_point); + virtual Point GetCurrentPosition() = 0; virtual void MoveTo(const Point& point) = 0; diff --git a/include/cru/platform/graphics/quartz/Geometry.h b/include/cru/platform/graphics/quartz/Geometry.h index e13d268e..abdff00e 100644 --- a/include/cru/platform/graphics/quartz/Geometry.h +++ b/include/cru/platform/graphics/quartz/Geometry.h @@ -43,6 +43,9 @@ class QuartzGeometryBuilder : public OsxQuartzResource, const Point& end_point) override; void QuadraticBezierTo(const Point& control_point, const Point& end_point) override; + void ArcTo(const Point& radius, float angle, bool is_large_arc, + bool is_clockwise, const Point& end_point) override; + void CloseFigure(bool close) override; std::unique_ptr<IGeometry> Build() override; diff --git a/src/platform/graphics/Geometry.cpp b/src/platform/graphics/Geometry.cpp index 859a7bd6..896b324f 100644 --- a/src/platform/graphics/Geometry.cpp +++ b/src/platform/graphics/Geometry.cpp @@ -1,9 +1,11 @@ #include "cru/platform/graphics/Geometry.h" - #include "cru/base/StringUtil.h" +#include "cru/platform/GraphicsBase.h" +#include "cru/platform/Matrix.h" #include "cru/platform/graphics/Factory.h" #include <cmath> +#include <numbers> #include <unordered_set> namespace cru::platform::graphics { @@ -19,6 +21,40 @@ std::unique_ptr<IGeometry> IGeometry::CreateStrokeGeometry( "not supported on this platform."); } +IGeometryBuilder::ArcInfo IGeometryBuilder::CalculateArcInfo( + const Point& start_point, const Point& radius, float angle, + bool is_large_arc, bool is_clockwise, const Point& end_point) { + auto matrix = + Matrix::Rotation(-angle) * Matrix::Scale(1 / radius.x, 1 / radius.y); + auto s1 = matrix.TransformPoint(start_point), + s2 = matrix.TransformPoint(end_point); + if (!is_clockwise) { + using std::swap; + swap(s1, s2); + } + + auto mid = (s1 + s2) / 2; + auto d = s1.Distance(s2) / 2; + auto dc = std::sqrt(1 - d * d); + auto a = std::atan2(s1.x - s2.x, s2.y - s1.y); + Point center(mid.x - dc * std::cos(a), mid.y - dc * std::sin(a)); + + if (std::abs(center.x) < 0.000001) { + center.x = 0.f; + } + if (std::abs(center.y) < 0.000001) { + center.y = 0.f; + } + + auto start_angle = std::atan2(s1.y - center.y, s1.x - center.x); + auto end_angle = std::atan2(s2.y - center.y, s2.x - center.x); + if (end_angle < 0) { + end_angle += std::numbers::pi_v<float> * 2; + } + + return {matrix.Inverted()->TransformPoint(center), start_angle, end_angle}; +} + void IGeometryBuilder::RelativeMoveTo(const Point& offset) { MoveTo(GetCurrentPosition() + offset); } @@ -233,9 +269,9 @@ void IGeometryBuilder::ParseAndApplySvgPathData(std::string_view path_d) { ++position; } - auto result = cru::string::ParseToNumber<float>( - path_d.substr(position), cru::string::ParseToNumberFlags::AllowTrailingJunk); + path_d.substr(position), + cru::string::ParseToNumberFlags::AllowTrailingJunk); if (!result.valid) throw Exception("Invalid svg path data number."); diff --git a/src/platform/graphics/quartz/Geometry.cpp b/src/platform/graphics/quartz/Geometry.cpp index 4c2f90a6..9b93ce15 100644 --- a/src/platform/graphics/quartz/Geometry.cpp +++ b/src/platform/graphics/quartz/Geometry.cpp @@ -67,6 +67,20 @@ void QuartzGeometryBuilder::QuadraticBezierTo(const Point &control_point, control_point.y, end_point.x, end_point.y); } +void QuartzGeometryBuilder::ArcTo(const Point &radius, float angle, + bool is_large_arc, bool is_clockwise, + const Point &end_point) { + auto info = CalculateArcInfo(GetCurrentPosition(), radius, angle, + is_large_arc, is_clockwise, end_point); + + auto matrix = + Matrix::Translation(info.center) * Matrix::Scale(radius.x, radius.y); + CGAffineTransform transform = Convert(matrix); + + CGPathAddArc(cg_mutable_path_, &transform, 0, 0, 1, info.start_angle, + info.end_angle, true); +} + void QuartzGeometryBuilder::CloseFigure(bool close) { if (close) CGPathCloseSubpath(cg_mutable_path_); } diff --git a/test/platform/CMakeLists.txt b/test/platform/CMakeLists.txt index 187068b8..6e8b8646 100644 --- a/test/platform/CMakeLists.txt +++ b/test/platform/CMakeLists.txt @@ -6,6 +6,8 @@ add_executable(CruPlatformBaseTest ) target_link_libraries(CruPlatformBaseTest PRIVATE CruPlatformBase CruTestBase) +add_subdirectory(graphics) + if (WIN32) add_subdirectory(graphics/direct2d) endif() diff --git a/test/platform/graphics/CMakeLists.txt b/test/platform/graphics/CMakeLists.txt new file mode 100644 index 00000000..22be21e6 --- /dev/null +++ b/test/platform/graphics/CMakeLists.txt @@ -0,0 +1,6 @@ +add_executable(CruPlatformGraphicsTest + GeometryTest.cpp +) +target_link_libraries(CruPlatformGraphicsTest PRIVATE CruPlatformGraphics CruTestBase) + +cru_catch_discover_tests(CruPlatformGraphicsTest) diff --git a/test/platform/graphics/GeometryTest.cpp b/test/platform/graphics/GeometryTest.cpp new file mode 100644 index 00000000..8a48ddde --- /dev/null +++ b/test/platform/graphics/GeometryTest.cpp @@ -0,0 +1,32 @@ +#include "cru/platform/graphics/Geometry.h" + +#include <catch2/catch_test_macros.hpp> +#include <numbers> +#include "catch2/catch_approx.hpp" + +using Catch::Approx; +using cru::platform::graphics::IGeometryBuilder; + +constexpr float margin = 0.000001; + +TEST_CASE("IGeometryBuilder CalculateArcInfo", "[graphics][geometry]") { + auto test = [](float start_x, float start_y, float radius_x, float radius_y, + float angle, bool is_large_arc, bool is_clockwise, float end_x, + float end_y, float expect_center_x, float expect_center_y, + float expect_start_angle, float expect_end_angle) { + auto info = IGeometryBuilder::CalculateArcInfo( + {start_x, start_y}, {radius_x, radius_y}, angle, is_large_arc, + is_clockwise, {end_x, end_y}); + REQUIRE(info.center.x == Approx(expect_center_x).margin(margin)); + REQUIRE(info.center.y == Approx(expect_center_y).margin(margin)); + REQUIRE(info.start_angle == Approx(expect_start_angle).margin(margin)); + REQUIRE(info.end_angle == Approx(expect_end_angle).margin(margin)); + }; + + test(1, 0, 1, 1, 0, false, true, 0, 1, 0, 0, 0, std::numbers::pi / 2); + test(0, 1, 1, 1, 0, false, true, -1, 0, 0, 0, std::numbers::pi / 2, + std::numbers::pi); + test(-1, 0, 1, 1, 0, false, true, 0, -1, 0, 0, std::numbers::pi, + -std::numbers::pi / 2); + test(0, -1, 1, 1, 0, false, true, 1, 0, 0, 0, -std::numbers::pi / 2, 0); +} |
