From 305ff9a261fb24b24f8b011282b30f9fbae49882 Mon Sep 17 00:00:00 2001 From: Guanhua Hou Date: Mon, 23 Oct 2023 17:30:06 +0800 Subject: [PATCH] implemented function getAffineTransform --- include/Domain/Layout/Rect.hpp | 5 +++++ src/Domain/CMakeLists.txt | 1 + src/Domain/Layout/Rect.cpp | 17 +++++++++++++++ test/domain/layout/resizing_tests.cpp | 31 +++++++++++++++++++++++++++ 4 files changed, 54 insertions(+) diff --git a/include/Domain/Layout/Rect.hpp b/include/Domain/Layout/Rect.hpp index 32805a8dd..32c2f0779 100644 --- a/include/Domain/Layout/Rect.hpp +++ b/include/Domain/Layout/Rect.hpp @@ -15,6 +15,8 @@ */ #pragma once +#include + namespace VGG { @@ -167,6 +169,9 @@ struct Rect } }; +Matrix getAffineTransform(const std::array& oldPoints, + const std::array& newPoints); + } // namespace Layout } // namespace VGG diff --git a/src/Domain/CMakeLists.txt b/src/Domain/CMakeLists.txt index 4f56eec90..e5f11cdf7 100644 --- a/src/Domain/CMakeLists.txt +++ b/src/Domain/CMakeLists.txt @@ -32,6 +32,7 @@ target_include_directories(vgg_domain PRIVATE ${CMAKE_SOURCE_DIR}/include/Domain/Layout ${CMAKE_SOURCE_DIR}/include/Domain/Model ${VGG_CONTRIB_BOOST_INCLUDE} + ${VGG_CONTRIB_GLM_INCLUDE} ) target_link_libraries(vgg_domain flexbox diff --git a/src/Domain/Layout/Rect.cpp b/src/Domain/Layout/Rect.cpp index 561fa6651..3abaaf97e 100644 --- a/src/Domain/Layout/Rect.cpp +++ b/src/Domain/Layout/Rect.cpp @@ -17,6 +17,8 @@ #include "Utility/VggFloat.hpp" +#include + #include using namespace VGG; @@ -72,4 +74,19 @@ Rect Rect::makeTransform(const Matrix& matrix, ECoordinateType type) const auto maxY = std::max(std::max(p1.y, p2.y), std::max(p3.y, p4.y)); return { { minX, isYAxisDown ? minY : maxY }, { maxX - minX, maxY - minY } }; +} + +Matrix Layout::getAffineTransform(const std::array& oldPoints, + const std::array& newPoints) +{ + const glm::mat3 oldGlmPoints{ oldPoints[0].x, oldPoints[0].y, 1, + oldPoints[1].x, oldPoints[1].y, 1, + oldPoints[2].x, oldPoints[2].y, 1 }; + const glm::mat3x2 newGlmPoints{ newPoints[0].x, newPoints[0].y, newPoints[1].x, + newPoints[1].y, newPoints[2].x, newPoints[2].y }; + + const auto gmMatrix = newGlmPoints * glm::inverse(oldGlmPoints); + return { + gmMatrix[0].x, gmMatrix[0].y, gmMatrix[1].x, gmMatrix[1].y, gmMatrix[2].x, gmMatrix[2].y + }; } \ No newline at end of file diff --git a/test/domain/layout/resizing_tests.cpp b/test/domain/layout/resizing_tests.cpp index 5121d9d98..3942f1bfb 100644 --- a/test/domain/layout/resizing_tests.cpp +++ b/test/domain/layout/resizing_tests.cpp @@ -5,6 +5,9 @@ #include +#include +#include + using namespace VGG; class VggResizingTestSuite : public ::testing::Test @@ -245,4 +248,32 @@ TEST_F(VggResizingTestSuite, FigGroupWithRotatedChild) EXPECT_TRUE(grandsonFrame(0, 0) == expectedFrames[1]); EXPECT_TRUE(grandsonFrame(0, 1) == expectedFrames[2]); EXPECT_TRUE(grandsonFrame(0, 2) == expectedFrames[3]); +} + +TEST_F(VggResizingTestSuite, GetAffineTransform) +{ + // Given + const glm::mat3x2 glmMatrix{ 0.9396926164627075, 0.3420201241970062, -0.3420201241970062, + 0.9396926164627075, 177.10513305664062, -228.8557586669922 }; + const glm::mat3 oldGlmPoints{ 0, 0, 1, 0, 40, 1, 100, 0, 1 }; + const auto newGlmPoints = glmMatrix * oldGlmPoints; + + const std::array oldPoints{ + Layout::Point{ oldGlmPoints[0].x, oldGlmPoints[0].y }, + Layout::Point{ oldGlmPoints[1].x, oldGlmPoints[1].y }, + Layout::Point{ oldGlmPoints[2].x, oldGlmPoints[2].y } + }; + const std::array newPoints{ + Layout::Point{ newGlmPoints[0].x, newGlmPoints[0].y }, + Layout::Point{ newGlmPoints[1].x, newGlmPoints[1].y }, + Layout::Point{ newGlmPoints[2].x, newGlmPoints[2].y } + }; + + // When + const auto computedMatrix = Layout::getAffineTransform(oldPoints, newPoints); + + // Then + Layout::Matrix layoutMatrix{ glmMatrix[0].x, glmMatrix[0].y, glmMatrix[1].x, + glmMatrix[1].y, glmMatrix[2].x, glmMatrix[2].y }; + EXPECT_TRUE(layoutMatrix == computedMatrix); } \ No newline at end of file