Skip to content

armgit / krita

Public Imported from GitHub
0

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++

Merge rects together in GridIterationTools

Agata Cacko89cabfc633d1
diff --git a/benchmarks/KisLiquifyTransformWorkerBenchmark.cpp b/benchmarks/KisLiquifyTransformWorkerBenchmark.cpp
index ebb6e7f02e..e97aaa3021 100644
--- a/benchmarks/KisLiquifyTransformWorkerBenchmark.cpp
+++ b/benchmarks/KisLiquifyTransformWorkerBenchmark.cpp
@@ -16,6 +16,7 @@
#include <testutil.h>
#include <kis_liquify_transform_worker.h>
#include <kis_algebra_2d.h>
+#include <kis_grid_interpolation_tools.h>
#include <quazip.h>
@@ -467,4 +468,22 @@ void KisLiquifyTransformWorkerBenchmark::checkMath()
prepareRandData(randGen, sigmas, flows, scales, randValuesCount);
}
+void KisLiquifyTransformWorkerBenchmark::testCheckingIfPolygonsAreConvex()
+{
+
+ QScopedPointer<KisLiquifyTransformWorker> liquifyWorker;
+ liquifyWorker.reset(getWorkerFromXml("liquify_mask_typical_config.zip"));
+ QVERIFY(liquifyWorker);
+ GridIterationTools::RegularGridIndexesOp indexesOp = GridIterationTools::RegularGridIndexesOp(liquifyWorker->gridSize());
+
+ bool isConvex;
+ QBENCHMARK {
+ isConvex = GridIterationTools::canProcessRectsInRandomOrder(indexesOp, liquifyWorker->transformedPoints(), liquifyWorker->gridSize());
+ }
+
+ QVERIFY(isConvex);
+
+
+}
+
SIMPLE_TEST_MAIN(KisLiquifyTransformWorkerBenchmark)
diff --git a/benchmarks/KisLiquifyTransformWorkerBenchmark.h b/benchmarks/KisLiquifyTransformWorkerBenchmark.h
index 79c2ebd9f9..bc2cf18006 100644
--- a/benchmarks/KisLiquifyTransformWorkerBenchmark.h
+++ b/benchmarks/KisLiquifyTransformWorkerBenchmark.h
@@ -56,9 +56,10 @@ private Q_SLOTS:
void testCopyConstructor();
void testMovePointsOutsideOfTheGrid();
-
void checkMath();
+ void testCheckingIfPolygonsAreConvex();
+
};
#endif
diff --git a/libs/image/KisBezierGradientMesh.cpp b/libs/image/KisBezierGradientMesh.cpp
index a8e6162a66..b175b4b098 100644
--- a/libs/image/KisBezierGradientMesh.cpp
+++ b/libs/image/KisBezierGradientMesh.cpp
@@ -59,6 +59,8 @@ struct QImageGradientOp
}
}
+ void finalize() {}
+
const std::array<QColor, 4> &m_colors;
QImage &m_dstImage;
QPointF m_dstImageOffset;
diff --git a/libs/image/kis_grid_interpolation_tools.h b/libs/image/kis_grid_interpolation_tools.h
index 3cd8c91805..ec39c0ba20 100644
--- a/libs/image/kis_grid_interpolation_tools.h
+++ b/libs/image/kis_grid_interpolation_tools.h
@@ -20,6 +20,7 @@
#include "kis_iterator_ng.h"
#include "kis_random_sub_accessor.h"
#include "kis_painter.h"
+#include "KisRegion.h"
//#define DEBUG_PAINTING_POLYGONS
@@ -197,7 +198,15 @@ struct PaintDevicePolygonOp
}
void fastCopyArea(QRect areaToCopy) {
- KisPainter::copyAreaOptimized(areaToCopy.topLeft(), m_srcDev, m_dstDev, areaToCopy);
+ fastCopyArea(areaToCopy, m_canMergeRects);
+ }
+
+ void fastCopyArea(QRect areaToCopy, bool lazy) {
+ if (lazy) {
+ m_rectsToCopy.append(areaToCopy.adjusted(0, 0, -1, -1));
+ } else {
+ KisPainter::copyAreaOptimized(areaToCopy.topLeft(), m_srcDev, m_dstDev, areaToCopy);
+ }
}
void operator() (const QPolygonF &srcPolygon, const QPolygonF &dstPolygon) {
@@ -274,10 +283,29 @@ struct PaintDevicePolygonOp
}
+ void finalize() {
+
+ QVector<QRect>::iterator end = KisRegion::mergeSparseRects(m_rectsToCopy.begin(), m_rectsToCopy.end());
+
+ for (QVector<QRect>::iterator it = m_rectsToCopy.begin(); it < end; it++) {
+ QRect areaToCopy = *it;
+ fastCopyArea(areaToCopy.adjusted(0, 0, 1, 1), false);
+ }
+ m_rectsToCopy = QVector<QRect>();
+ }
+
+ inline void setCanMergeRects(bool newCanMergeRects) {
+ m_canMergeRects = newCanMergeRects;
+ }
+
KisPaintDeviceSP m_srcDev;
KisPaintDeviceSP m_dstDev;
const qreal m_epsilon {0.1};
+private:
+ bool m_canMergeRects {true};
+ QVector<QRect> m_rectsToCopy;
+
};
struct QImagePolygonOp
@@ -337,6 +365,14 @@ struct QImagePolygonOp
}
void fastCopyArea(QRect areaToCopy) {
+ fastCopyArea(areaToCopy, m_canMergeRects);
+ }
+
+ void fastCopyArea(QRect areaToCopy, bool lazy) {
+ if (lazy) {
+ m_rectsToCopy.append(areaToCopy.adjusted(0, 0, -1, -1));
+ return;
+ }
// only handling saved offsets
QRect srcArea = areaToCopy.translated(-m_srcImageOffset.toPoint());
@@ -430,6 +466,23 @@ struct QImagePolygonOp
}
+ void finalize() {
+
+ QVector<QRect>::iterator end = KisRegion::mergeSparseRects(m_rectsToCopy.begin(), m_rectsToCopy.end());
+
+ for (QVector<QRect>::iterator it = m_rectsToCopy.begin(); it < end; it++) {
+ QRect areaToCopy = *it;
+ fastCopyArea(areaToCopy.adjusted(0, 0, 1, 1), false);
+ }
+
+ m_rectsToCopy = QVector<QRect>();
+ }
+
+ inline void setCanMergeRects(bool canMergeRects) {
+ m_canMergeRects = canMergeRects;
+ }
+
+
const QImage &m_srcImage;
QImage &m_dstImage;
QPointF m_srcImageOffset;
@@ -439,6 +492,10 @@ struct QImagePolygonOp
QRect m_dstImageRect;
const qreal m_epsilon {0.1};
+
+private:
+ bool m_canMergeRects {true};
+ QVector<QRect> m_rectsToCopy;
};
/*************************************************************/
@@ -801,6 +858,48 @@ inline void adjustAlignedPolygon(QPolygonF &polygon)
polygon[3] += p3;
}
+template <class IndexesOp>
+bool canProcessRectsInRandomOrder(IndexesOp &indexesOp, const QVector<QPointF> &transformedPoints, QSize grid) {
+ return canProcessRectsInRandomOrder(indexesOp, transformedPoints, QRect(QPoint(0, 0), grid));
+}
+
+template <class IndexesOp>
+bool canProcessRectsInRandomOrder(IndexesOp &indexesOp, const QVector<QPointF> &transformedPoints, QRect subgrid) {
+ QVector<int> polygonPoints(4);
+ QPoint startPoint = subgrid.topLeft();
+ QPoint endPoint = subgrid.bottomRight();
+
+
+ int polygonsChecked = 0;
+
+ for (int row = startPoint.y(); row < endPoint.y(); row++) {
+ for (int col = startPoint.x(); col < endPoint.x(); col++) {
+ int numExistingPoints = 0;
+
+ polygonPoints = indexesOp.calculateMappedIndexes(col, row, &numExistingPoints);
+
+ QPolygonF dstPolygon;
+
+ for (int i = 0; i < polygonPoints.count(); i++) {
+ const int index = polygonPoints[i];
+ dstPolygon << transformedPoints[index];
+ }
+
+
+ adjustAlignedPolygon(dstPolygon);
+
+
+ if (!KisAlgebra2D::isPolygonTrulyConvex(dstPolygon)) {
+ return false;
+ }
+
+ }
+ }
+ return true;
+}
+
+
+
template <template <class PolygonOp, class IndexesOp> class IncompletePolygonPolicy,
class PolygonOp,
class IndexesOp>
@@ -865,6 +964,8 @@ void iterateThroughGrid(PolygonOp &polygonOp,
}
}
}
+
+ polygonOp.finalize();
}
}
diff --git a/libs/image/kis_liquify_transform_worker.cpp b/libs/image/kis_liquify_transform_worker.cpp
index ad84faf202..fb973458d3 100644
--- a/libs/image/kis_liquify_transform_worker.cpp
+++ b/libs/image/kis_liquify_transform_worker.cpp
@@ -420,6 +420,10 @@ void KisLiquifyTransformWorker::run(KisPaintDeviceSP srcDevice, KisPaintDeviceSP
PaintDevicePolygonOp polygonOp(srcDevice, dstDevice);
RegularGridIndexesOp indexesOp(m_d->gridSize);
+
+ bool canMergeRects = GridIterationTools::canProcessRectsInRandomOrder(indexesOp, m_d->transformedPoints, correctSubGrid);
+ polygonOp.setCanMergeRects(canMergeRects);
+
iterateThroughGrid<AlwaysCompletePolygonPolicy>(polygonOp, indexesOp,
m_d->gridSize,
m_d->originalPoints,
@@ -427,7 +431,7 @@ void KisLiquifyTransformWorker::run(KisPaintDeviceSP srcDevice, KisPaintDeviceSP
correctSubGrid);
QList<QRectF> areasToCopy = cutOutSubgridFromBounds(correctSubGrid, m_d->srcBounds, m_d->gridSize, m_d->originalPoints);
for (int i = 0; i < areasToCopy.length(); i++) {
- polygonOp.fastCopyArea(areasToCopy[i]);
+ polygonOp.fastCopyArea(areasToCopy[i].toRect(), false);
}
}
@@ -527,6 +531,10 @@ QImage KisLiquifyTransformWorker::runOnQImage(const QImage &srcImage,
QRect correctSubGrid = GridIterationTools::calculateCorrectSubGrid(m_d->srcBounds, m_d->pixelPrecision, m_d->accumulatedBrushStrokes, m_d->gridSize);
+ bool canMergeRects = GridIterationTools::canProcessRectsInRandomOrder(indexesOp, m_d->transformedPoints, correctSubGrid);
+ polygonOp.setCanMergeRects(canMergeRects);
+
+
GridIterationTools::iterateThroughGrid<GridIterationTools::AlwaysCompletePolygonPolicy>(polygonOp, indexesOp,
m_d->gridSize,
originalPointsLocal,
@@ -535,6 +543,7 @@ QImage KisLiquifyTransformWorker::runOnQImage(const QImage &srcImage,
QList<QRectF> areasToCopy = GridIterationTools::cutOutSubgridFromBounds(correctSubGrid, m_d->srcBounds, m_d->gridSize, m_d->originalPoints);
+ polygonOp.setCanMergeRects(false);
for (int i = 0; i < areasToCopy.length(); i++) {
polygonOp.fastCopyArea(imageToThumbTransform.map(QPolygonF(areasToCopy[i])));
}
diff --git a/libs/image/tests/KisGridInterpolationToolsTest.cpp b/libs/image/tests/KisGridInterpolationToolsTest.cpp
index 5b49206cf5..5eed3138c8 100644
--- a/libs/image/tests/KisGridInterpolationToolsTest.cpp
+++ b/libs/image/tests/KisGridInterpolationToolsTest.cpp
@@ -306,6 +306,25 @@ void KisGridInterpolationToolsTest::testCutOutSubgridFromBounds()
}
+void KisGridInterpolationToolsTest::testCanProcessPolygonsInRandomOrder()
+{
+ QRect srcBounds = QRect(0, 0, 16, 24);
+ int pixelPrecision = 8;
+ QSize gridSize = GridIterationTools::calcGridSize(srcBounds, pixelPrecision);
+ AllPointsFetcherOp pointsOp(srcBounds);
+ GridIterationTools::processGrid(pointsOp, srcBounds, pixelPrecision);
+ QVector<QPointF> transformedPoints = pointsOp.m_points;
+ QRectF acc = QRectF(10, 10, 20, 20);
+ QRect correctSubGrid = GridIterationTools::calculateCorrectSubGrid(srcBounds, pixelPrecision, acc, gridSize);
+
+
+ GridIterationTools::RegularGridIndexesOp indexesOp(gridSize);
+
+ bool canMergeRects = GridIterationTools::canProcessRectsInRandomOrder(indexesOp, transformedPoints, correctSubGrid);
+ QVERIFY(canMergeRects);
+
+}
+
void KisGridInterpolationToolsTest::testQImagePolygonOpStructFastAreaCopy()
{
QImage srcImage(TestUtil::fetchDataFileLazy("test_grid_iteration_tools_qimage_fast_area_copy.png"));
diff --git a/libs/image/tests/KisGridInterpolationToolsTest.h b/libs/image/tests/KisGridInterpolationToolsTest.h
index d2fcb4faa9..8ae3b5c6da 100644
--- a/libs/image/tests/KisGridInterpolationToolsTest.h
+++ b/libs/image/tests/KisGridInterpolationToolsTest.h
@@ -41,6 +41,7 @@ private Q_SLOTS:
void testRegularGridIndexesOp();
*/
+ void testCanProcessPolygonsInRandomOrder();
void testQImagePolygonOpStructFastAreaCopy();