Skip to content

Commit

Permalink
implemented function getAffineTransform
Browse files Browse the repository at this point in the history
  • Loading branch information
gaimanjing authored and harry75369 committed Oct 25, 2023
1 parent 24666e9 commit 305ff9a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/Domain/Layout/Rect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
#pragma once

#include <array>

namespace VGG
{

Expand Down Expand Up @@ -167,6 +169,9 @@ struct Rect
}
};

Matrix getAffineTransform(const std::array<Point, 3>& oldPoints,
const std::array<Point, 3>& newPoints);

} // namespace Layout

} // namespace VGG
1 change: 1 addition & 0 deletions src/Domain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions src/Domain/Layout/Rect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#include "Utility/VggFloat.hpp"

#include <glm/glm.hpp>

#include <algorithm>

using namespace VGG;
Expand Down Expand Up @@ -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<Point, 3>& oldPoints,
const std::array<Point, 3>& 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
};
}
31 changes: 31 additions & 0 deletions test/domain/layout/resizing_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

#include <gtest/gtest.h>

#include <glm/ext.hpp>
#include <glm/glm.hpp>

using namespace VGG;

class VggResizingTestSuite : public ::testing::Test
Expand Down Expand Up @@ -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<Layout::Point, 3> 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<Layout::Point, 3> 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);
}

0 comments on commit 305ff9a

Please sign in to comment.