Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Add cutOutSubgridFromBounds to GridIterationTools
Agata Cackodfd6c576e5eb
diff --git a/libs/image/kis_grid_interpolation_tools.h b/libs/image/kis_grid_interpolation_tools.hindex d9cb78d68a..3cd8c91805 100644--- a/libs/image/kis_grid_interpolation_tools.h+++ b/libs/image/kis_grid_interpolation_tools.h@@ -516,6 +516,53 @@ inline QRect calculateCorrectSubGrid(QRect originalBoundsForGrid, int pixelPrecireturn QRect(startPointG, QSize(size.x(), size.y()));}+inline QList<QRectF> cutOutSubgridFromBounds(QRect subGrid, QRect srcBounds, const QSize &gridSize, const QVector<QPointF> &originalPoints) {+ if (subGrid.width() == 0 || subGrid.height() == 0) {+ return QList<QRectF> {srcBounds};+ }+ QPoint topLeft = subGrid.topLeft();+ QPoint bottomRight = subGrid.topLeft() + QPoint(subGrid.width() - 1, subGrid.height() - 1);++ int topLeftIndex = pointToIndex(topLeft, gridSize);+ int bottomRightIndex = pointToIndex(bottomRight, gridSize);++ topLeftIndex = qMax(0, qMin(topLeftIndex, originalPoints.length() - 1));+ bottomRightIndex = qMax(0, qMin(bottomRightIndex, originalPoints.length() - 1));++ QPointF topLeftReal = originalPoints[topLeftIndex];+ QPointF bottomRightReal = originalPoints[bottomRightIndex];+ QRectF cutOut = QRectF(topLeftReal, bottomRightReal);++ QList<QRectF> response;+ // *-----------*+ // | top |+ // |-----------|+ // | l |xxx| r |+ // | e |xxx| i |+ // | f |xxx| g |+ // | t |xxx| h |+ // | |xxx| t |+ // |-----------|+ // | bottom |+ // *-----------*+++ QRectF top = QRectF(srcBounds.topLeft(), QPointF(srcBounds.right(), topLeftReal.y()));+ QRectF bottom = QRectF(QPointF(srcBounds.left(), bottomRightReal.y()), srcBounds.bottomRight());+ QRectF left = QRectF(QPointF(srcBounds.left(), cutOut.top()), QPointF(cutOut.left(), cutOut.bottom()));+ QRectF right = QRectF(QPointF(cutOut.right(), cutOut.top()), QPointF(srcBounds.right(), cutOut.bottom()));+ QList<QRectF> rects = {top, left, right, bottom};+ for (int i = 0; i < rects.length(); i++) {+ if (!rects[i].isEmpty()) {+ response << rects[i];+ }+ }+ return response;++}+++template <class IndexesOp>bool getOrthogonalPointApproximation(const QPoint &cellPt,diff --git a/libs/image/tests/KisGridInterpolationToolsTest.cpp b/libs/image/tests/KisGridInterpolationToolsTest.cppindex 753dd15735..5b49206cf5 100644--- a/libs/image/tests/KisGridInterpolationToolsTest.cpp+++ b/libs/image/tests/KisGridInterpolationToolsTest.cpp@@ -260,7 +260,51 @@ QVector<QPointF> getPoints(QRect srcBounds, int pixelPrecision) {return pointsOp.m_points;}+void KisGridInterpolationToolsTest::testCutOutSubgridFromBounds_data()+{+ QTest::addColumn<QRect>("correctSubgrid");+ QTest::addColumn<QRect>("srcBounds");+ QTest::addColumn<QSize>("gridSize");+ QTest::addColumn<QVector<QPointF>>("originalPoints");+ QTest::addColumn<QList<QRectF>>("expected");++ int pixelPrecision = 8;+ QRect originalBounds = QRect(20, 20, 100, 200);++ QRect srcBounds = QRect(0, 0, 1240, 1754);+ QRect srcBoundsSmall = QRect(0, 0, 100, 100);++ QTest::addRow("out-of-bounds") << QRect(122, 18, 34, 31) << srcBounds << GridIterationTools::calcGridSize(srcBounds, pixelPrecision)+ << getPoints(srcBounds, pixelPrecision) << QList<QRectF> {QRectF(0,0, 1239,144), QRectF(0,144, 976,240), QRectF(0,384, 1239,1369)};+ QTest::addRow("out-of-bounds") << QRect(10, 5, 4, 7) << srcBoundsSmall << GridIterationTools::calcGridSize(srcBoundsSmall, pixelPrecision)+ << getPoints(srcBoundsSmall, pixelPrecision) << QList<QRectF> {QRectF(0,0, 99,40), QRectF(0,40, 80,48), QRectF(0,88, 99,11)};++ // this happened after an issue in calculateCorrectSubgrid, but it's still weird how it calculates it+ // GridIterationTools::calcGridSize(QRect(QPoint(824,30), QSize(393,330)), 4)+ QRect srcBounds2 = QRect(QPoint(824,30), QSize(393,330));++ QTest::addRow("out-of-bounds-0-width-case") << QRect(QPoint(100,0),QSize(0,84)) << srcBounds2 << QSize(100, 84)+ << getPoints(srcBounds2, 4) << QList<QRectF> {srcBounds2};++}+void KisGridInterpolationToolsTest::testCutOutSubgridFromBounds()+{+ QFETCH(QRect, correctSubgrid);+ QFETCH(QRect, srcBounds);+ QFETCH(QSize, gridSize);+ QFETCH(QVector<QPointF>, originalPoints);+ QFETCH(QList<QRectF>, expected);++ ENTER_FUNCTION() << ppVar(correctSubgrid) << ppVar(srcBounds) << ppVar(gridSize);++ QList<QRectF> result = GridIterationTools::cutOutSubgridFromBounds(correctSubgrid, srcBounds, gridSize, originalPoints);+ if (result != expected) {+ qCritical() << result << expected;// << "for data: " << ppVar(column) << ppVar(row) << ppVar(gridSize);+ }+ QCOMPARE(result, expected);++}void KisGridInterpolationToolsTest::testQImagePolygonOpStructFastAreaCopy(){diff --git a/libs/image/tests/KisGridInterpolationToolsTest.h b/libs/image/tests/KisGridInterpolationToolsTest.hindex 853c3e7e7a..d2fcb4faa9 100644--- a/libs/image/tests/KisGridInterpolationToolsTest.h+++ b/libs/image/tests/KisGridInterpolationToolsTest.h@@ -26,6 +26,8 @@ private Q_SLOTS:void testGetOrthogonalPointApproximation();void testCalculateCorrectSubGrid_data();void testCalculateCorrectSubGrid();+ void testCutOutSubgridFromBounds_data();+ void testCutOutSubgridFromBounds();/*void testAdjustAlignedPolygon();void testRestoreOriginalPolygonFromAligned();@@ -42,7 +44,6 @@ private Q_SLOTS:void testQImagePolygonOpStructFastAreaCopy();-};#endif