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

Add tests and debug for the Remove Gutter functions

Agata Cackofa931fd489a2
diff --git a/libs/global/kis_algebra_2d.cpp b/libs/global/kis_algebra_2d.cpp
index 2d9c47b12d..f262fcd768 100644
--- a/libs/global/kis_algebra_2d.cpp
+++ b/libs/global/kis_algebra_2d.cpp
@@ -1952,6 +1952,10 @@ QPainterPath removeGutterOneEndSmart(const QPainterPath &shape1, int index1, con
}
+ //qreal distanceToMiddle = kisDistanceToLine(intersectionPoint, middleLine);
+ //if (distanceToMiddle > qMax(leftPointDistanceFromMiddle, rightPointDistanceFromMiddle)) {
+ // return simpleResult;
+ //}
QPainterPath betterResult = makeTrianglePath(intersectionPoint);
@@ -2028,6 +2032,29 @@ QPainterPath simplifyShape(const QPainterPath& path) {
return vector.trulySimplified().asPainterPath();
}
+VectorPath combineShapes(VectorPath shape1, int index1, VectorPath shape2, int index2, VectorPath end1, VectorPath end2)
+{
+ using VPoint = VectorPath::VectorPathPoint;
+ QList<VPoint> result;
+
+ // s1(0..index1), end1, s2(index2, ..., index2 wrapped)
+
+ for(int i = 0; i < index1; i++) {
+ result << shape1.pointAt(i);
+ }
+ for (int i = 0; i < end1.pointsCount(); i++) {
+ result << end1.pointAt(i);
+ }
+ for (int i = index2; i < index2 + shape2.pointsCount(); i++) {
+ result << shape2.pointAt(wrapValue(i, 0, shape2.pointsCount()));
+ }
+
+ return VectorPath(QList<VPoint>());
+
+
+}
+
+
QPainterPath removeGutterSmart(const QPainterPath &shape1, int index1, const QPainterPath &shape2, int index2)
{
@@ -2040,6 +2067,8 @@ QPainterPath removeGutterSmart(const QPainterPath &shape1, int index1, const QPa
QLineF line1 = getLineFromElements(shape1, index1);
QLineF line2 = getLineFromElements(shape2, index2);
+ //QPointF middleLineVector = QPointF((line1.dx() + line2.dx())/2, (line1.dy() + line2.dy())/2);
+
QPainterPath gutterShape = QPainterPath();
QVector<QPointF> poly1 = QVector<QPointF>() << line1.p1() << line1.p2() << line2.p1();
@@ -2067,34 +2096,132 @@ QPainterPath removeGutterSmart(const QPainterPath &shape1, int index1, const QPa
gutterShape.connectPath(otherEnd);
gutterShape.closeSubpath();
+ gutterShape = gutterShape.toReversed();
+
qCritical() << ppVar(gutterShape);
qCritical() << ppVar(oneEnd);
qCritical() << ppVar(otherEnd);
+ qreal eps = 1e-05;
+ for (int i = 0; i < gutterShape.elementCount(); i++) {
+ QPainterPath::Element gutterEl = gutterShape.elementAt(i);
+ qCritical() << "Gutter element: " << gutterEl.x << gutterEl.y << gutterEl.type;
+ for (int j = 0; j < shape1.elementCount(); j++) {
+ QPainterPath::Element shapeEl = shape1.elementAt(j);
+ if (qAbs(gutterEl.x - shapeEl.x) < eps && qAbs(gutterEl.y - shapeEl.y) < eps) {
+ gutterShape.setElementPositionAt(i, shapeEl.x, shapeEl.y);
+ break;
+ }
+ }
+ for (int j = 0; j < shape2.elementCount(); j++) {
+ QPainterPath::Element shapeEl = shape2.elementAt(j);
+ if (qAbs(gutterEl.x - shapeEl.x) < eps && qAbs(gutterEl.y - shapeEl.y) < eps) {
+ gutterShape.setElementPositionAt(i, shapeEl.x, shapeEl.y);
+ break;
+ }
+ }
+ }
+
+ for (int i = 0; i < shape1.elementCount(); i++) {
+ QPainterPath::Element shapeEl = shape1.elementAt(i);
+ qCritical() << "Shape1 element: " << shapeEl.x << shapeEl.y << shapeEl.type;
+ }
+
+ for (int i = 0; i < shape2.elementCount(); i++) {
+ QPainterPath::Element shapeEl = shape2.elementAt(i);
+ qCritical() << "Shape2 element: " << shapeEl.x << shapeEl.y << shapeEl.type;
+ }
+
+
+ QPainterPath windingShape1 = shape1;
+ QPainterPath windingShape2 = shape2;
+ gutterShape.setFillRule(Qt::WindingFill);
+ windingShape1.setFillRule(Qt::WindingFill);
+ windingShape2.setFillRule(Qt::WindingFill);
+
+
+ QPainterPath result = (windingShape1 | windingShape2 | gutterShape);
+
+
+
+ qCritical() << "#* Result of everything, after adding the gutter, is: " << result;
+ qCritical() << "#* Partial results: " << ppVar(shape1 | shape2) << ppVar(shape2 | gutterShape) << ppVar(shape1 | gutterShape) << ppVar(shape1 | shape2 | gutterShape) << ppVar((shape1 | shape2) | gutterShape) << ppVar((shape1 | gutterShape) | shape2);
+ qCritical() << "#* Shapes to unite were also: " << ppVar(shape1) << ppVar(shape2);
+ qCritical() << "#* Shapes to unite were also (2): " << ppVar(shape1.toFillPolygon()) << ppVar(shape2.toFillPolygon()) << ppVar(gutterShape.toFillPolygon());
+ qCritical() << "#* Fill rules:" << ppVar(shape1.fillRule()) << ppVar(shape2.fillRule()) << ppVar(gutterShape.fillRule());
+
+
+ qCritical() << "#* Result of everything, after adding the gutter, is: " << result;
+ qCritical() << "#* Partial results: " << ppVar(windingShape1 | windingShape2) << ppVar(windingShape2 | gutterShape) << ppVar(windingShape1 | gutterShape) << ppVar(windingShape1 | windingShape2 | gutterShape)
+ << ppVar((windingShape1 | windingShape2) | gutterShape) << ppVar((windingShape1 | gutterShape) | windingShape2);
+ qCritical() << "#* Shapes to unite were also: " << ppVar(windingShape1) << ppVar(windingShape2);
+ qCritical() << "#* Shapes to unite were also (2): " << ppVar(windingShape1.toFillPolygon()) << ppVar(windingShape2.toFillPolygon()) << ppVar(gutterShape.toFillPolygon());
+ qCritical() << "#* Fill rules:" << ppVar(windingShape1.fillRule()) << ppVar(windingShape2.fillRule()) << ppVar(gutterShape.fillRule());
+
+
+ return VectorPath(result).trulySimplified().asPainterPath();
+
+ QPainterPath closedShape1 = shape1;
+ QPainterPath closedShape2 = shape2;
- QPainterPath result = shape1 | shape2 | gutterShape;
+ closedShape1.closeSubpath();
+ closedShape2.closeSubpath();
+ gutterShape.closeSubpath();
+
+ closedShape1.setFillRule(Qt::WindingFill);
+ closedShape2.setFillRule(Qt::WindingFill);
+ gutterShape.setFillRule(Qt::WindingFill);
+
+
+ QPainterPath partialResult = (closedShape1 | gutterShape);
+ partialResult.closeSubpath();
+ partialResult.setFillRule(Qt::WindingFill);
+ result = (partialResult | closedShape2);
+ result.closeSubpath();
+ result = result.simplified();
+ //result = (shape1 | gutterShape) | shape2;
+
+
+ qCritical() << "# Result of everything, after adding the gutter, is: " << result;
+ qCritical() << "# Partial results: " << ppVar(shape1 | shape2) << ppVar(shape2 | gutterShape) << ppVar(shape1 | gutterShape) << ppVar(shape1 | shape2 | gutterShape) << ppVar((shape1 | shape2) | gutterShape) << ppVar((shape1 | gutterShape) | shape2);
+ qCritical() << "# Shapes to unite were also: " << ppVar(shape1) << ppVar(shape2);
+ qCritical() << "# Shapes to unite were also (2): " << ppVar(shape1.toFillPolygon()) << ppVar(shape2.toFillPolygon()) << ppVar(gutterShape.toFillPolygon());
- return result.simplified();
+ // TEMPORARY:
+ VectorPath shape1Vector(shape1);
+ VectorPath shape2Vector(shape2);
+ VectorPath gutterVector(gutterShape);
+ QPolygonF shape1Polygon = shape1Vector.trulySimplified().asPainterPath().toFillPolygon();
+ QPolygonF shape2Polygon = shape2Vector.trulySimplified().asPainterPath().toFillPolygon();
+ QPolygonF gutterPolygon = gutterVector.trulySimplified().asPainterPath().toFillPolygon();
+ QPolygonF resultPolygon = shape1Polygon.united(gutterPolygon);
+ resultPolygon = resultPolygon.united(shape2Polygon);
- // now, easy version:
- gutterShape.moveTo(line1.p1());
- gutterShape.lineTo(line1.p2());
- gutterShape.lineTo(line2.p1());
- gutterShape.lineTo(line2.p2());
+ QPainterPath newResult;
+ newResult.addPolygon(resultPolygon);
+ newResult.closeSubpath();
- //QPainterPath result = shape1 | shape2 | gutterShape;
- //result.closeSubpath();
- //return result.simplified();
+ newResult = VectorPath(newResult).trulySimplified().asPainterPath();
- //return result;
+ return newResult;
+ //return QPainterPath.addPolygon();
+
+
+ VectorPath vectorResult(result);
+ qCritical() << ppVar(vectorResult.asPainterPath()) << ppVar(vectorResult.trulySimplified().asPainterPath());
+ qCritical() << "^ that's what we're gonna return.";
+
+ return vectorResult.trulySimplified().asPainterPath();
+
+
}
@@ -2211,7 +2338,8 @@ VectorPath VectorPath::trulySimplified()
} else if (m_points[i].type == VPoint::LineTo) {
if (previousPoint.type == VPoint::LineTo) {
// check whether they're parallel
- qCritical() << "Check whether they're parallel";
+ qCritical() << "Check whether they're parallel: to " << ppVar(m_points[i].endPoint) << " from " << ppVar(lineBeginPoint.endPoint) << " and to " << ppVar(previousPoint.endPoint) << " from " << ppVar(lineBeginPoint.endPoint);
+
qreal eps = 1e-05;
if (qAbs(crossProduct(m_points[i].endPoint - lineBeginPoint.endPoint, previousPoint.endPoint - lineBeginPoint.endPoint)) >= eps) {
qCritical() << "They are not parallel: " << m_points[i].endPoint - lineBeginPoint.endPoint << previousPoint.endPoint - lineBeginPoint.endPoint
diff --git a/libs/image/tests/kis_algebra_2d_test.cpp b/libs/image/tests/kis_algebra_2d_test.cpp
index 56b8986974..270c7b87e6 100644
--- a/libs/image/tests/kis_algebra_2d_test.cpp
+++ b/libs/image/tests/kis_algebra_2d_test.cpp
@@ -1170,7 +1170,8 @@ void testRemoveGutterOneCase(const QPainterPath& shape1, const QPainterPath& sha
assert(indexes2.length() == 1);
QPainterPath result = KisAlgebra2D::removeGutterSmart(shape1, indexes1[0], shape2, indexes2[0]);
- qCritical() << result;
+ qCritical() << "Result path:" << result;
+ qCritical() << "Result polygon: " << result.toFillPolygon();
if (expectedSize >= 0) {
qCritical() << ppVar(result.elementCount()) << ppVar(expectedSize);
@@ -1278,13 +1279,89 @@ void KisAlgebra2DTest::testRemoveGutter()
convertShapeToDebugArray(otherPoly);
+ testRemoveGutterOneCase(onePoly, otherPoly, QLineF(QPointF(2010,662),QPointF(2006,938)));
- //testRemoveGutterOneCase(onePoly, otherPoly, QLineF(QPointF(2010,662),QPointF(2006,938)));
+ QPainterPath disappearingOne = fromPolygon(QVector({QPointF(2302.64, 816.154), QPointF(3270, 809.908), QPointF(3270, 1567.38), QPointF(2702.65, 1582.76), QPointF(2302.64, 816.154)}));
+ QPainterPath disappearingTwo = fromPolygon(QVector({QPointF(3077.96, 2307), QPointF(2427.5, 2307), QPointF(2148.5, 1650.95), QPointF(3073.03, 1625.89), QPointF(3077.96, 2307)}));
+ testRemoveGutterOneCase(disappearingOne, disappearingTwo, QLineF(QPointF(2892,1773),QPointF(3027,1245)));
+ qCritical() << "###############################";
+
+ //shape1.toFillPolygon() = QPolygonF(QPointF(3077.96,2307)QPointF(2427.5,2307)QPointF(2148.5,1650.95)QPointF(3073.03,1625.89)QPointF(3077.96,2307))
+ // shape2.toFillPolygon() = QPolygonF(QPointF(2302.64,816.154)QPointF(3270,809.908)QPointF(3270,1567.38)QPointF(2702.65,1582.76)QPointF(2302.64,816.154))
+ // gutterShape.toFillPolygon() = QPolygonF(QPointF(2148.5,1650.95)QPointF(2730.01,1635.19)QPointF(2702.65,1582.76)QPointF(3270,1567.38)QPointF(3072.64,1572.73)QPointF(3073.03,1625.89)QPointF(2148.5,1650.95))
+ QPainterPath toUnite1 = fromPolygon(QPolygonF(QVector({QPointF(3077.96,2307), QPointF(2427.5,2307), QPointF(2148.5,1650.95), QPointF(3073.03,1625.89), QPointF(3077.96,2307)})));
+ QPainterPath toUnite2 = fromPolygon(QPolygonF(QVector({QPointF(2302.64,816.154), QPointF(3270,809.908), QPointF(3270,1567.38), QPointF(2702.65,1582.76), QPointF(2302.64,816.154)})));
+ QPainterPath toUniteGutter = fromPolygon(QPolygonF(QVector({QPointF(2148.5,1650.95), QPointF(2730.01,1635.19), QPointF(2702.65,1582.76), QPointF(3270,1567.38), QPointF(3072.64,1572.73), QPointF(3073.03,1625.89), QPointF(2148.5,1650.95)})));
+
+ qCritical() << "Is it really so incorrect>? " << (toUnite1 | toUnite2 | toUniteGutter);
+
+}
+
+void KisAlgebra2DTest::testMergingPainterPaths()
+{
+ QPainterPath first; // counter-clockwise
+ first.moveTo(0, 0);
+ first.lineTo(100, 0);
+ first.lineTo(100, 100);
+ first.lineTo(0, 100);
+ first.lineTo(0, 0);
+
+
+ QPainterPath second;
+ second.moveTo(100, 0);
+ second.lineTo(200, 0);
+ second.lineTo(200, 100);
+ second.lineTo(100, 100);
+ second.lineTo(100, 0);
+
+
+ qCritical() << ppVar(first | second);
+ qCritical() << ppVar(first | second.toReversed());
+
+ qCritical() << ppVar((first | second).simplified());
+ qCritical() << ppVar((first | second.toReversed()).simplified());
+
+ first.setFillRule(Qt::OddEvenFill);
+ second.setFillRule(Qt::OddEvenFill);
+
+
+ qCritical() << ppVar(first | second);
+ qCritical() << ppVar(first | second.toReversed());
+
+ qCritical() << ppVar((first | second).simplified());
+ qCritical() << ppVar((first | second.toReversed()).simplified());
+
+ // this is from the real life example, how to make it work?
+
+ QPainterPath shape1;
+ shape1.moveTo(QPointF(2302.64, 816.154));
+ shape1.lineTo(QPointF(3270, 809.908));
+ shape1.lineTo(QPointF(3270, 1567.38));
+ shape1.lineTo(QPointF(2702.65, 1582.76));
+ shape1.lineTo(QPointF(2302.64, 816.154));
+
+ QPainterPath shape2;
+ shape2.moveTo(QPointF(3077.96, 2307));
+ shape2.lineTo(QPointF(2427.5, 2307));
+ shape2.lineTo(QPointF(2148.5, 1650.95));
+ shape2.lineTo(QPointF(3073.03, 1625.89));
+ shape2.lineTo(QPointF(3077.96, 2307));
+
+ QPainterPath gutter;
+ gutter.moveTo(QPointF(3270, 1567.38));
+ gutter.lineTo(QPointF(2702.65, 1582.76));
+ gutter.lineTo(QPointF(2730.01, 1635.19));
+ gutter.lineTo(QPointF(2148.5, 1650.95));
+ gutter.lineTo(QPointF(3073.03, 1625.89));
+ gutter.lineTo(QPointF(3072.64, 1572.73));
+ gutter.lineTo(QPointF(3270, 1567.38));
+
+ qCritical() << ppVar(shape1 | shape2 | gutter);
}
diff --git a/libs/image/tests/kis_algebra_2d_test.h b/libs/image/tests/kis_algebra_2d_test.h
index b4a805c55c..38eefe75eb 100644
--- a/libs/image/tests/kis_algebra_2d_test.h
+++ b/libs/image/tests/kis_algebra_2d_test.h
@@ -41,6 +41,7 @@ private Q_SLOTS:
void testGetParallelLines2();
void testRemoveGutter();
+ void testMergingPainterPaths();
diff --git a/plugins/tools/tool_knife/RemoveGutterStrategy.cpp b/plugins/tools/tool_knife/RemoveGutterStrategy.cpp
index ef88967241..9af651f287 100644
--- a/plugins/tools/tool_knife/RemoveGutterStrategy.cpp
+++ b/plugins/tools/tool_knife/RemoveGutterStrategy.cpp
@@ -252,13 +252,22 @@ void RemoveGutterStrategy::finishInteraction(Qt::KeyboardModifiers modifiers)
qCritical() << "Found two shapes.";
qCritical() << "Shape 1:";
convertShapeToDebugArray(srcOutlines[shapeNewIndexes[0]]);
+ qCritical() << ppVar(srcOutlines[shapeNewIndexes[0]].toFillPolygon());
+
qCritical() << "Shape 2:";
convertShapeToDebugArray(srcOutlines[shapeNewIndexes[1]]);
+ qCritical() << ppVar(srcOutlines[shapeNewIndexes[1]].toFillPolygon());
+
+
QPainterPath result = KisAlgebra2D::removeGutterSmart(srcOutlines[shapeNewIndexes[0]], lineSegmentIndexes[0], srcOutlines[shapeNewIndexes[1]], lineSegmentIndexes[1]);
+ qCritical() << "Finally got a result:";
+ convertShapeToDebugArray(result);
+ qCritical() << ppVar(result.toFillPolygon());
+
QList<KoShape*> resultShapes;
qCritical() << "Indexes that are supposed to be left:";