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

Fix removing a gutter

Agata Cackoad0447f7e12e
diff --git a/libs/global/kis_algebra_2d.cpp b/libs/global/kis_algebra_2d.cpp
index f262fcd768..c552b8ee49 100644
--- a/libs/global/kis_algebra_2d.cpp
+++ b/libs/global/kis_algebra_2d.cpp
@@ -1774,7 +1774,7 @@ QPainterPath getOnePathFromRectangleCutThrough(const QList<QPointF> &points, con
return path;
}
-QList<QPainterPath> getPathsFromRectangleCutThrough(QRectF rect, QLineF leftLine, QLineF rightLine) {
+QList<QPainterPath> getPathsFromRectangleCutThrough(const QRectF &rect, const QLineF &leftLine, const QLineF &rightLine) {
qCritical() << "~~~ BOTH PATHS ~~~";
@@ -2032,6 +2032,8 @@ QPainterPath simplifyShape(const QPainterPath& path) {
return vector.trulySimplified().asPainterPath();
}
+
+// TODO: obsolete, to remove
VectorPath combineShapes(VectorPath shape1, int index1, VectorPath shape2, int index2, VectorPath end1, VectorPath end2)
{
using VPoint = VectorPath::VectorPathPoint;
@@ -2055,8 +2057,186 @@ VectorPath combineShapes(VectorPath shape1, int index1, VectorPath shape2, int i
}
+VectorPath mergeShapesWithGutter(const VectorPath& shape1, const VectorPath& shape2, const VectorPath& oneEnd, const VectorPath& otherEnd, int index1, int index2, bool reverseSecondPoly, bool isSameShape)
+{
+ using VPoint = VectorPath::VectorPathPoint;
+ QList<VPoint> result;
+
+
+ ENTER_FUNCTION();
+
+ result.append(shape1.pointAt(0));
+ qCritical() << "Added point from shape1: " << shape1.pointAt(0).endPoint;
+
+ auto appendFrom = [] (QList<VPoint>& res, int start, int end, const VectorPath& shape) {
+ for (int i = start; i < end; i++) {
+ QList<VPoint> segment = shape.segmentAt(i);
+ KIS_SAFE_ASSERT_RECOVER_RETURN(segment.length() == 2);
+ res.append(segment[1]);
+ }
+ };
+
+ VectorPath reversedShape2 = reverseSecondPoly ? shape2.reversed() : VectorPath(shape2);
+ int reversedIndex2 = reverseSecondPoly ? (shape2.segmentsCount() - index2 - 1) : index2;
+
+ if (isSameShape) {
+
+ qCritical() << "Same shape = true, is reversedIndex?" << reverseSecondPoly;
+ int minIndex = qMin(index1, index2);
+ int maxIndex = qMax(index1, index2);
+
+
+ qCritical() << "Indexes are: " << index1 << index2 << "therefore: " << minIndex << maxIndex;
+ /*
+ qCritical() << "Append from shape1, indexes: (" << 0 << ", " << minIndex << ")";
+ appendFrom(0, minIndex, shape1);
+ qCritical() << "Append from oneEnd, indexes: (" << 0 << ", " << oneEnd.segmentsCount() << ")";
+ appendFrom(0, oneEnd.segmentsCount(), oneEnd);
+ qCritical() << "Append from shape1, indexes: (" << minIndex + 1 << ", " << maxIndex << ")";
+ appendFrom(minIndex + 1, maxIndex, shape1);
+ qCritical() << "Append from otherEnd, indexes: (" << 0 << ", " << otherEnd.segmentsCount() << ")";
+ appendFrom(0, otherEnd.segmentsCount(), otherEnd);
+ qCritical() << "Append from shape1, indexes: (" << maxIndex + 1 << ", " << shape1.segmentsCount() << ")";
+ appendFrom(maxIndex + 1, shape1.segmentsCount(), shape1);
+ */
+
+ // the indexes define a way to cut the shape into two shapes
+ // and one of them is kind of more outer than the other
+ // (they could intersect but... that's the user problem at that point, they can fix it point by point)
+ // in normal situation, it's like an outer ring of edges and inner ring of edges (which might be just one edge, which is the most common case)
+ // and we can find that out by checking whether the ends of the selected edges are inside or outside of the shape
+ // and if they're inside a shape, we're assuming that's the resulting shape (and the other is discarded)
+ // no islands in comic panels on my watch!
+
+
+
+ QList<VPoint> result1;
+ // min index to max index
+ result1 << VPoint::moveTo(shape1.segmentAt(minIndex)[1].endPoint);
+ appendFrom(result1, minIndex + 1, maxIndex, shape1);
+
+ QList<VPoint> result2;
+ result2 << VPoint::moveTo(shape1.segmentAt(maxIndex)[1].endPoint);
+ appendFrom(result2, maxIndex + 1, shape1.segmentsCount(), shape1);
+ appendFrom(result2, 0, minIndex, shape1);
+
+ qCritical() << "result 1 initialized with: " << shape1.segmentAt(minIndex)[1].endPoint;
+ qCritical() << "result 2 initialized with: " << shape1.segmentAt(maxIndex)[1].endPoint;
+
+ qCritical() << "Result 1 got indexes: (" << ppVar(minIndex + 1) << ", " << ppVar(maxIndex) << ")";
+ qCritical() << "Result 2 got indexes: (" << ppVar(maxIndex + 1) << ", " << ppVar(shape1.segmentsCount()) << ")";
+ qCritical() << "And: (" << ppVar(0) << ", " << ppVar(minIndex) << ")";
+
+
+
+ // TODO: include gutter shapes!!!!!
+ // minEnd and maxEnd
+ // and combine with results!
+
+
+
+ qCritical() << "Result 1 before appending = " << ppVar(VectorPath(result1).asPainterPath()) << ppVar(VectorPath(result1).segmentsCount());
+ qCritical() << "Result 2 before appending = " << ppVar(VectorPath(result2).asPainterPath()) << ppVar(VectorPath(result2).segmentsCount());
+ qCritical() << "Original had: " << ppVar(shape1.segmentsCount());
+
+
+
+
+ VectorPath minEnd = index1 > index2 ? oneEnd : otherEnd;
+ VectorPath maxEnd = index1 > index2 ? otherEnd : oneEnd;
+
+
+ qCritical() << "Indexes were: " << index1 << index2 << "therefore NOT swapping: " << (index1 < index2);
+ qCritical() << ppVar(oneEnd.asPainterPath()) << ppVar(otherEnd.asPainterPath()) << ppVar(minEnd.asPainterPath()) << ppVar(maxEnd.asPainterPath());
+
+
+ appendFrom(result1, 0, minEnd.segmentsCount(), minEnd);
+ appendFrom(result2, 0, maxEnd.segmentsCount(), maxEnd);
+
+
+
+
+
+
+ VectorPath result1Vec(result1);
+ VectorPath result2Vec(result2);
+
+
+ qCritical() << "Result 1 = " << ppVar(VectorPath(result1).asPainterPath()) << ppVar(VectorPath(result1).segmentsCount());
+ qCritical() << "Result 2 = " << ppVar(VectorPath(result2).asPainterPath()) << ppVar(VectorPath(result2).segmentsCount());
+ qCritical() << "Original had: " << ppVar(shape1.segmentsCount());
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ //QPolygonF result1Poly = result1Vec.asPainterPath().toFillPolygon();
+ QPolygonF result2Poly = result2Vec.asPainterPath().toFillPolygon();
+
+ // TODO: assumes winding fill
+ if (result2Poly.containsPoint(shape1.segmentAt(minIndex)[1].endPoint, Qt::WindingFill)) {
+ // could check all
+ return result2Vec;
+ }
+ return result1Vec;
+
+
+
+
+ //return VectorPath(result1);
+
+
+
+
+
+
+ } else {
+ appendFrom(result, 0, index1, shape1);
+ appendFrom(result, 0, oneEnd.segmentsCount(), oneEnd);
+ appendFrom(result, reversedIndex2 + 1, reversedShape2.segmentsCount(), reversedShape2);
+ appendFrom(result, 0, reversedIndex2, reversedShape2);
+ appendFrom(result, 0, otherEnd.segmentsCount(), otherEnd);
+ appendFrom(result, index1 + 1, shape1.segmentsCount(), shape1);
+ }
+
+ return VectorPath(result);
+
-QPainterPath removeGutterSmart(const QPainterPath &shape1, int index1, const QPainterPath &shape2, int index2)
+
+ /*
+
+ for (int i = 0; i < index1; i++) {
+ QList<VPoint> segment = shape1.segmentAt(i);
+ KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(segment.length() > 1, shape1);
+ result.append(segment[1]);
+ }
+
+ for (int i = 0; i < oneEnd.segmentsCount(); i++) {
+ QList<VPoint> segment = oneEnd.segmentAt(i);
+ KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(segment.length() > 1, shape1);
+ result.append(segment[1]);
+ }
+
+
+
+ */
+
+
+
+}
+
+
+QPainterPath removeGutterSmart(const QPainterPath &shape1, int index1, const QPainterPath &shape2, int index2, bool isSameShape)
{
qCritical() << ppVar(index1) << ppVar(shape1.elementCount()) << ppVar(index2) << ppVar(shape2.elementCount());
if (index1 + 1 >= shape1.elementCount() || index2 + 1 >= shape2.elementCount()) {
@@ -2090,6 +2270,17 @@ QPainterPath removeGutterSmart(const QPainterPath &shape1, int index1, const QPa
QPainterPath otherEnd = removeGutterOneEndSmart(shape2, index2, shape1, index1, middleLine, reverseSecondPoly, false);
+ VectorPath vectorShape1 = VectorPath(shape1);
+ VectorPath vectorShape2 = VectorPath(shape2);
+ VectorPath vectorOneEnd = VectorPath(oneEnd);
+ VectorPath vectorOtherEnd = VectorPath(otherEnd);
+
+ VectorPath vectorResultHere = mergeShapesWithGutter(vectorShape1, vectorShape2, vectorOneEnd, vectorOtherEnd, index1, index2, reverseSecondPoly, isSameShape);
+ return vectorResultHere.trulySimplified(1e-02).asPainterPath();
+
+
+
+ /// -----
gutterShape.moveTo(oneEnd.elementAt(0));
gutterShape.connectPath(oneEnd);
@@ -2098,11 +2289,41 @@ QPainterPath removeGutterSmart(const QPainterPath &shape1, int index1, const QPa
gutterShape = gutterShape.toReversed();
- qCritical() << ppVar(gutterShape);
- qCritical() << ppVar(oneEnd);
- qCritical() << ppVar(otherEnd);
+
+ QPainterPath roundedShape1 = shape1;
+ QPainterPath roundedShape2 = shape2;
+
+
+ qCritical() << Qt::fixed << qSetRealNumberPrecision(10) << "GUTTER SHAPE BEFORE ROUNDING:" ppVar(gutterShape);
+
+
+ // NOW THE DANGEROUS PART:
+ for (int i = 0; i < gutterShape.elementCount(); i++) {
+ QPainterPath::Element el = gutterShape.elementAt(i);
+ gutterShape.setElementPositionAt(i, qRound(el.x), qRound(el.y));
+ }
+
+ for (int i = 0; i < roundedShape1.elementCount(); i++) {
+ QPainterPath::Element el = roundedShape1.elementAt(i);
+ roundedShape1.setElementPositionAt(i, qRound(el.x), qRound(el.y));
+ }
+
+ for (int i = 0; i < roundedShape2.elementCount(); i++) {
+ QPainterPath::Element el = roundedShape2.elementAt(i);
+ roundedShape2.setElementPositionAt(i, qRound(el.x), qRound(el.y));
+ }
+
+
+
+
+
+
+ qCritical() << Qt::fixed << qSetRealNumberPrecision(10) << ppVar(gutterShape);
+ qCritical() << Qt::fixed << qSetRealNumberPrecision(10) << ppVar(oneEnd);
+ qCritical() << Qt::fixed << qSetRealNumberPrecision(10) << 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;
@@ -2121,6 +2342,7 @@ QPainterPath removeGutterSmart(const QPainterPath &shape1, int index1, const QPa
}
}
}
+ */
for (int i = 0; i < shape1.elementCount(); i++) {
QPainterPath::Element shapeEl = shape1.elementAt(i);
@@ -2144,22 +2366,23 @@ QPainterPath removeGutterSmart(const QPainterPath &shape1, int index1, const QPa
- 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() << Qt::fixed << qSetRealNumberPrecision(10) << "#* Result of everything, after adding the gutter, is: " << result;
+ qCritical() << Qt::fixed << qSetRealNumberPrecision(10) << "#* Partial results: " << ppVar(shape1 | shape2) << ppVar(shape2 | gutterShape) << ppVar(shape1 | gutterShape) << ppVar(shape1 | shape2 | gutterShape) << ppVar((shape1 | shape2) | gutterShape) << ppVar((shape1 | gutterShape) | shape2);
+ qCritical() << Qt::fixed << qSetRealNumberPrecision(10) << "#* Shapes to unite were also: " << ppVar(shape1) << ppVar(shape2) << ppVar(gutterShape);
+ qCritical() << Qt::fixed << qSetRealNumberPrecision(10) << "#* Shapes to unite were also (2): " << ppVar(shape1.toFillPolygon()) << ppVar(shape2.toFillPolygon()) << ppVar(gutterShape.toFillPolygon());
+ qCritical() << Qt::fixed << qSetRealNumberPrecision(10) << "#* 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)
+ qCritical() << Qt::fixed << qSetRealNumberPrecision(10) << "#* Result of everything, after adding the gutter, is: " << result;
+ qCritical() << Qt::fixed << qSetRealNumberPrecision(10) << "#* 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());
+ qCritical() << Qt::fixed << qSetRealNumberPrecision(10) << "#* Shapes to unite were also: " << ppVar(windingShape1) << ppVar(windingShape2);
+ qCritical() << Qt::fixed << qSetRealNumberPrecision(10) << "#* Shapes to unite were also (2): " << ppVar(windingShape1.toFillPolygon()) << ppVar(windingShape2.toFillPolygon()) << ppVar(gutterShape.toFillPolygon());
+ qCritical() << Qt::fixed << qSetRealNumberPrecision(10) << "#* Fill rules:" << ppVar(windingShape1.fillRule()) << ppVar(windingShape2.fillRule()) << ppVar(gutterShape.fillRule());
- return VectorPath(result).trulySimplified().asPainterPath();
+ return VectorPath(result).trulySimplified(1e-02).asPainterPath();
+ /// -------
QPainterPath closedShape1 = shape1;
QPainterPath closedShape2 = shape2;
@@ -2278,22 +2501,22 @@ VectorPath::VectorPath(const QList<VectorPathPoint> path)
m_points = path;
}
-int VectorPath::pointsCount()
+int VectorPath::pointsCount() const
{
return m_points.length();
}
-VectorPath::VectorPathPoint VectorPath::pointAt(int i)
+VectorPath::VectorPathPoint VectorPath::pointAt(int i) const
{
return m_points[i];
}
-int VectorPath::segmentsCount()
+int VectorPath::segmentsCount() const
{
return m_points.length() - 1 >= 0 ? m_points.length() - 1 : 0;
}
-QList<VectorPath::VectorPathPoint> VectorPath::segmentAt(int i)
+QList<VectorPath::VectorPathPoint> VectorPath::segmentAt(int i) const
{
QList<VectorPath::VectorPathPoint> response;
if (i < 0 || i >= segmentsCount()) {
@@ -2303,7 +2526,7 @@ QList<VectorPath::VectorPathPoint> VectorPath::segmentAt(int i)
return response;
}
-QLineF VectorPath::segmentAtAsLine(int i)
+QLineF VectorPath::segmentAtAsLine(int i) const
{
QList<VectorPath::VectorPathPoint> points = segmentAt(i);
if (points.length() < 1) {
@@ -2312,8 +2535,11 @@ QLineF VectorPath::segmentAtAsLine(int i)
return QLineF(points[0].endPoint, points[1].endPoint);
}
-VectorPath VectorPath::trulySimplified()
+VectorPath VectorPath::trulySimplified(qreal epsDegrees) const
{
+ qreal eps = kisDegreesToRadians(epsDegrees);
+
+
using VPoint = VectorPath::VectorPathPoint;
qCritical() << "Start simplifying path that has segments: " << segmentsCount();
@@ -2324,6 +2550,22 @@ VectorPath VectorPath::trulySimplified()
QList<VPoint> response;
VPoint lineBeginPoint = VPoint(VPoint::MoveTo, QPointF(0, 0));
VPoint previousPoint = m_points[0];
+
+
+ //qreal eps = 1e-05;
+ auto onTheLine = [eps] (QPointF start, QPointF middle, QPointF end) {
+ //QPointF vector1 = end - start;
+ //QPointF vector2 = end - start;
+ QLineF line1(start, end);
+ QLineF line2(start, middle);
+
+ qCritical() << "abs after dividing:" << qAbs(crossProduct(end - start, middle - start)/line1.length()/line2.length());
+ qCritical() << "meaning degrees: " << kisRadiansToDegrees(qAbs(crossProduct(end - start, middle - start)/line1.length()/line2.length()));
+
+
+ return (qAbs(crossProduct(end - start, middle - start)/line1.length()/line2.length()) < eps);
+ };
+
for (int i = 1; i < m_points.length(); i++) {
qCritical() << "Point is: " << m_points[i].endPoint << m_points[i].type;
if (m_points[i].type == VPoint::MoveTo) {
@@ -2340,14 +2582,16 @@ VectorPath VectorPath::trulySimplified()
// 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
- << crossProduct(m_points[i].endPoint - lineBeginPoint.endPoint, previousPoint.endPoint - lineBeginPoint.endPoint)
- << qAbs(crossProduct(m_points[i].endPoint - lineBeginPoint.endPoint, previousPoint.endPoint - lineBeginPoint.endPoint))
- << eps;
+ qCritical() << "Are they parallel?: (two vectors) " << m_points[i].endPoint - lineBeginPoint.endPoint << previousPoint.endPoint - lineBeginPoint.endPoint
+ << "cross product = " << crossProduct(m_points[i].endPoint - lineBeginPoint.endPoint, previousPoint.endPoint - lineBeginPoint.endPoint)
+ << "abs(cross product) = " << qAbs(crossProduct(m_points[i].endPoint - lineBeginPoint.endPoint, previousPoint.endPoint - lineBeginPoint.endPoint))
+ << ppVar(eps);
+ if (!onTheLine(lineBeginPoint.endPoint, previousPoint.endPoint, m_points[i].endPoint)) {
+ qCritical() << "Not parallel";
response << previousPoint;
lineBeginPoint = previousPoint;
+ } else {
+ qCritical() << "Parallel!";
}
previousPoint = m_points[i]; // let's skip the previous point since they're parallel
continue;
@@ -2365,10 +2609,76 @@ VectorPath VectorPath::trulySimplified()
}
response << previousPoint;
qCritical() << "After simplification, we've got points: " << response.length();
+
+ // now eliminate additional point at the beginning of the shape if it's in the middle of a line
+ // and wasn't simplified only because it's at the beginning
+
+ if (response.length() < 3) {
+ return VectorPath(response);
+ }
+
+ VPoint start = response[0];
+ VPoint second = response[1];
+
+ qCritical() << "In the middle, the amount of points is: " << response.length();
+ for(int i = 0; i < response.length(); i++) {
+ qCritical() << ppVar(response[i].type) << response[i].endPoint;
+ }
+
+
+ if (start.type == VPoint::MoveTo) {
+ if (fuzzyPointCompare(previousPoint.endPoint, start.endPoint)) {
+ // they are the same point, now whether they're on the line
+ if (second.type == VPoint::LineTo && onTheLine(response[response.length()-2].endPoint, start.endPoint, second.endPoint)) {
+ qCritical() << "Checked: " << "fuzzy compare point: " << ppVar(response[response.length()-2].endPoint) << ppVar(start.endPoint) << ", on the line: " << ppVar(previousPoint.endPoint) << ppVar(start.endPoint) << ppVar(second.endPoint);
+ response.pop_front();
+ response[0].type = VPoint::MoveTo;
+ response[response.length() - 1].endPoint = second.endPoint;
+ qCritical() << "We're in this case: simplify first MoveTo";
+ }
+ }
+
+ } else if (response[0].type == VPoint::LineTo) { // first point is then QPointF(0, 0)
+ if (qFuzzyIsNull(previousPoint.endPoint.x()) && qFuzzyIsNull(previousPoint.endPoint.y())) {
+ // they are the same point, now whether they're on the line
+ if (second.type == VPoint::LineTo && onTheLine(previousPoint.endPoint, QPointF(0, 0), start.endPoint)) {
+ qCritical() << "We're in this case";
+ //response.pop_front();
+ //response[0].type = VPoint::MoveTo;
+ //response[response.length() - 1].endPoint = start.endPoint;
+ }
+ }
+ }
+
+
+
+
+ return VectorPath(response);
+}
+
+VectorPath VectorPath::reversed() const
+{
+ using VPoint = VectorPath::VectorPathPoint;
+ QList<VPoint> response;
+ response.append(VPoint(VPoint::MoveTo, m_points[m_points.length() - 1].endPoint));
+ for (int i = m_points.length() - 1; i > 0; i--) {
+ // to reverse a segment, we take the end VPoint and change the .endPoint to the start VPoint coords
+ VPoint point = m_points[i];
+ //qCritical() << "Copying point" << point;
+ point.endPoint = m_points[i - 1].endPoint;
+ //qCritical() << "Changed end point to: " << point;
+ response.append(point);
+ }
return VectorPath(response);
}
-QPainterPath VectorPath::asPainterPath()
+bool VectorPath::fuzzyCompareCyclic(const VectorPath &path, qreal eps) const
+{
+ //
+ return true;
+}
+
+QPainterPath VectorPath::asPainterPath() const
{
using VPoint = VectorPath::VectorPathPoint;
if (m_originalPath.elementCount() > 0) {
@@ -2388,6 +2698,7 @@ QPainterPath VectorPath::asPainterPath()
break;
}
}
+ response.closeSubpath();
return response;
}
diff --git a/libs/global/kis_algebra_2d.h b/libs/global/kis_algebra_2d.h
index d9d62e9a19..2b16b7d20d 100644
--- a/libs/global/kis_algebra_2d.h
+++ b/libs/global/kis_algebra_2d.h
@@ -591,23 +591,43 @@ public:
type = _type;
endPoint = _endPoint;
controlPoint1 = c1;
- controlPoint1 = c2;
+ controlPoint2 = c2;
+ }
+
+ static VectorPathPoint moveTo(QPointF _endPoint) {
+ return VectorPathPoint(MoveTo, _endPoint);
+ }
+
+ static VectorPathPoint lineTo(QPointF _endPoint) {
+ return VectorPathPoint(LineTo, _endPoint);
+ }
+
+ static VectorPathPoint bezierTo(QPointF _endPoint, QPointF _controlPoint1, QPointF _controlPoint2) {
+ return VectorPathPoint(BezierTo, _endPoint, _controlPoint1, _controlPoint2);
}
};
public:
VectorPath(const QPainterPath& path);
VectorPath(const QList<VectorPathPoint> path);
+ //VectorPath(const QList<VectorPathPoint> path);
+
+
+ int pointsCount() const;
+ VectorPathPoint pointAt(int i) const;
+ int segmentsCount() const;
+ QList<VectorPathPoint> segmentAt(int i) const;
+ QLineF segmentAtAsLine(int i) const;
+
+ VectorPath trulySimplified(qreal epsDegrees = 0.5) const;
+ // not open-path friendly
+ VectorPath reversed() const;
- int pointsCount();
- VectorPathPoint pointAt(int i);
- int segmentsCount();
- QList<VectorPathPoint> segmentAt(int i);
- QLineF segmentAtAsLine(int i);
- VectorPath trulySimplified();
+ bool fuzzyCompareCyclic(const VectorPath& path, qreal eps = 0.00001f) const;
- QPainterPath asPainterPath();
+
+ QPainterPath asPainterPath() const;
private:
QPainterPath m_originalPath;
@@ -1032,7 +1052,8 @@ QPainterPath KRITAGLOBAL_EXPORT getOnePathFromRectangleCutThrough(const QList<QP
/// \param rightLine right line of the rectangle (used for the right-(bottom) side of the rectangle
/// \return
///
-QList<QPainterPath> KRITAGLOBAL_EXPORT getPathsFromRectangleCutThrough(QRectF rect, QLineF leftLine, QLineF rightLine);
+QList<QPainterPath> KRITAGLOBAL_EXPORT getPathsFromRectangleCutThrough(const QRectF &rect, const QLineF &leftLine, const QLineF &rightLine);
+
// isAlgebra2D::getLineSegmentCrossingLineIndexes(QLineF const&, QPainterPath const&)
QList<int> KRITAGLOBAL_EXPORT getLineSegmentCrossingLineIndexes(const QLineF &line, const QPainterPath& shape);
@@ -1045,7 +1066,20 @@ QList<int> KRITAGLOBAL_EXPORT getLineSegmentCrossingLineIndexes(const QLineF &li
/// \param index2
/// \return
///
-QPainterPath KRITAGLOBAL_EXPORT removeGutterSmart(const QPainterPath& shape1, int index1, const QPainterPath& shape2, int index2);
+QPainterPath KRITAGLOBAL_EXPORT removeGutterSmart(const QPainterPath& shape1, int index1, const QPainterPath& shape2, int index2, bool isSameShape);
+
+
+///
+/// \brief mergeShapesWithGutter merges two shapes with a gutter shape (defined as two paths)
+/// \param shape1
+/// \param shape2
+/// \param oneEnd
+/// \param otherEnd
+/// \param index1 index of the segment in the first shape
+/// \param index2 index of the segment in the second shape
+/// \return
+///
+VectorPath mergeShapesWithGutter(const VectorPath& shape1, const VectorPath& shape2, const VectorPath& oneEnd, const VectorPath& otherEnd, int index1, int index2, bool reverseSecondPoly, bool isSameShape);
}
diff --git a/libs/image/tests/kis_algebra_2d_test.cpp b/libs/image/tests/kis_algebra_2d_test.cpp
index 270c7b87e6..e76ed2c96b 100644
--- a/libs/image/tests/kis_algebra_2d_test.cpp
+++ b/libs/image/tests/kis_algebra_2d_test.cpp
@@ -1117,6 +1117,29 @@ void KisAlgebra2DTest::testGetParallelLines2()
qCritical() << line1 << line2;
+}
+
+void KisAlgebra2DTest::testVectorPathReversed()
+{
+ using VPoint = KisAlgebra2D::VectorPath::VectorPathPoint;
+ QList<VPoint> path;
+ path << VPoint::moveTo(QPointF(0, 0));
+ path << VPoint::lineTo(QPointF(-10, 20));
+ path << VPoint::lineTo(QPointF(-5, 20));
+ path << VPoint::bezierTo(QPointF(5, 20), QPointF(-5, 30), QPointF(5, 30));
+ path << VPoint::lineTo(QPointF(10, 20));
+ path << VPoint::lineTo(QPointF(0, 0));
+
+ KisAlgebra2D::VectorPath result = KisAlgebra2D::VectorPath(path).reversed();
+
+ for(int i = 0; i < result.pointsCount(); i++) {
+ VPoint p = result.pointAt(i);
+ qCritical() << ppVar(p.type) << ppVar(p.endPoint) << ppVar(p.controlPoint1) << ppVar(p.controlPoint2);
+ }
+
+ qCritical() << result.asPainterPath();
+
+
}
QPainterPath makeSpecialShape(bool left)
@@ -1160,16 +1183,31 @@ QPolygonF toPolygon(const QPainterPath& path) {
}
-void testRemoveGutterOneCase(const QPainterPath& shape1, const QPainterPath& shape2, const QLineF& mouseLine, int expectedSize = -1, const QPainterPath &expectedPath = QPainterPath())
+void testRemoveGutterOneCase(const QPainterPath& shape1, const QPainterPath& shape2, const QLineF& mouseLine, bool isSameShape = false, int expectedSize = -1, const QPainterPath &expectedPath = QPainterPath())
{
QList<int> indexes1 = KisAlgebra2D::getLineSegmentCrossingLineIndexes(mouseLine, shape1);
QList<int> indexes2 = KisAlgebra2D::getLineSegmentCrossingLineIndexes(mouseLine, shape2);
- assert(indexes1.length() == 1);
- assert(indexes2.length() == 1);
+ if (isSameShape) {
+ assert(indexes1.length()%2 == 0);
+ assert(indexes2.length()%2 == 0);
+ assert(indexes1.length() > 0);
+ assert(indexes2.length() > 0);
- QPainterPath result = KisAlgebra2D::removeGutterSmart(shape1, indexes1[0], shape2, indexes2[0]);
+ } else {
+ assert(indexes1.length() == 1);
+ assert(indexes2.length() == 1);
+ }
+
+
+
+ QPainterPath result;
+ if (isSameShape) {
+ result = KisAlgebra2D::removeGutterSmart(shape1, indexes1[0], shape1, indexes1[1], isSameShape);
+ } else {
+ result = KisAlgebra2D::removeGutterSmart(shape1, indexes1[0], shape2, indexes2[0], isSameShape);
+ }
qCritical() << "Result path:" << result;
qCritical() << "Result polygon: " << result.toFillPolygon();
@@ -1239,8 +1277,16 @@ void KisAlgebra2DTest::testRemoveGutter()
QLineF mouseLine = QLineF(QPointF(50, 50), QPointF(250, 50));
- testRemoveGutterOneCase(polyPath, polyPath2, mouseLine, 5, fromPolygon(QRectF(0, 0, 300, 100)));
- testRemoveGutterOneCase(polyPath, polyPath2.toReversed(), mouseLine, 5, fromPolygon(QRectF(0, 0, 300, 100)));
+
+ qCritical() << "############################### CASE [1] ####################################";
+
+ testRemoveGutterOneCase(polyPath, polyPath2, mouseLine, false, 5, fromPolygon(QRectF(0, 0, 300, 100)));
+
+
+ qCritical() << "############################### CASE [2] ####################################";
+
+
+ testRemoveGutterOneCase(polyPath, polyPath2.toReversed(), mouseLine, false, 5, fromPolygon(QRectF(0, 0, 300, 100)));
//qCritical() << result2;
@@ -1250,8 +1296,19 @@ void KisAlgebra2DTest::testRemoveGutter()
QPainterPath specialResult = fromPolygon(QVector({QPointF(100, 100), QPointF(900, 100), QPointF(850, 125), QPointF(900, 150), QPointF(850, 175), QPointF(900, 200), QPointF(100, 200), QPointF(150, 175),
QPointF(100, 150), QPointF(150, 125), QPointF(100, 100)}));
- testRemoveGutterOneCase(special1, special2, QLineF(QPointF(300, 150), QPointF(700, 150)), 11, specialResult);
- testRemoveGutterOneCase(special1, special2.toReversed(), QLineF(QPointF(300, 150), QPointF(700, 150)), 11, specialResult);
+
+
+ qCritical() << "############################### CASE [3] ####################################";
+
+
+ testRemoveGutterOneCase(special1, special2, QLineF(QPointF(300, 150), QPointF(700, 150)), false, 11, specialResult);
+
+
+
+ qCritical() << "############################### CASE [4] ####################################";
+
+
+ testRemoveGutterOneCase(special1, special2.toReversed(), QLineF(QPointF(300, 150), QPointF(700, 150)), false, 11, specialResult);
//QPainterPath bigOnTop = fromPolygon(QPolygonF(QVector({QPointF(0, 0), QPointF(100, 0)})));
QPainterPath bigOnTop = fromPolygon(QRectF(0, 3, 10, 3));
@@ -1260,9 +1317,19 @@ void KisAlgebra2DTest::testRemoveGutter()
QPainterPath smallLeft = fromPolygon(QRectF(0, 0, 2, 2));
- testRemoveGutterOneCase(bigOnTop, smallRight, QLineF(QPointF(8, 1), QPointF(8, 5)), -1, fromPolygon(QVector({QPointF(0, 3), QPointF(7, 3), QPointF(7, 0), QPointF(10, 0), QPointF(10, 6), QPointF(0, 6), QPointF(0, 3)})));
- testRemoveGutterOneCase(bigOnTop, smallMiddle, QLineF(QPointF(5, 1), QPointF(5, 5)), -1, fromPolygon(QVector({QPointF(0,3), QPointF(3,3), QPointF(3,0), QPointF(6,0), QPointF(6,3), QPointF(10,3), QPointF(10,6), QPointF(0,6), QPointF(0,3)})));
- testRemoveGutterOneCase(bigOnTop, smallLeft, QLineF(QPointF(1, 1), QPointF(1, 5)), -1, fromPolygon(QVector({QPointF(2,3), QPointF(10,3), QPointF(10,6), QPointF(0,6), QPointF(0,0), QPointF(2,0), QPointF(2,3)})));
+ qCritical() << "############################### CASE [5] ####################################";
+
+ testRemoveGutterOneCase(bigOnTop, smallRight, QLineF(QPointF(8, 1), QPointF(8, 5)), false, -1, fromPolygon(QVector({QPointF(0, 3), QPointF(7, 3), QPointF(7, 0), QPointF(10, 0), QPointF(10, 6), QPointF(0, 6), QPointF(0, 3)})));
+
+
+ qCritical() << "############################### CASE [6] ####################################";
+
+ testRemoveGutterOneCase(bigOnTop, smallMiddle, QLineF(QPointF(5, 1), QPointF(5, 5)), false, -1, fromPolygon(QVector({QPointF(0,3), QPointF(3,3), QPointF(3,0), QPointF(6,0), QPointF(6,3), QPointF(10,3), QPointF(10,6), QPointF(0,6), QPointF(0,3)})));
+
+
+ qCritical() << "############################### CASE [7] ####################################";
+
+ testRemoveGutterOneCase(bigOnTop, smallLeft, QLineF(QPointF(1, 1), QPointF(1, 5)), false,-1, fromPolygon(QVector({QPointF(0,0), QPointF(2,0), QPointF(2,3), QPointF(2,3), QPointF(10,3), QPointF(10,6), QPointF(0,6)})));
//testRemoveGutterOneCase(fromPolygon({QPointF(1134.24,823.696), QPointF(1777.95,819.542), QPointF(2109.36,1598.84), QPointF(1143.4,1625.02)}));
@@ -1279,6 +1346,8 @@ void KisAlgebra2DTest::testRemoveGutter()
convertShapeToDebugArray(otherPoly);
+ qCritical() << "############################### CASE [8] ####################################";
+
testRemoveGutterOneCase(onePoly, otherPoly, QLineF(QPointF(2010,662),QPointF(2006,938)));
@@ -1286,22 +1355,47 @@ void KisAlgebra2DTest::testRemoveGutter()
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)}));
+
+ qCritical() << "############################### CASE [9] ####################################";
testRemoveGutterOneCase(disappearingOne, disappearingTwo, QLineF(QPointF(2892,1773),QPointF(3027,1245)));
- qCritical() << "###############################";
+ qCritical() << "############################### CASE [10] ####################################";
//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);
+
+
+ QPainterPath shape1 = fromPolygon(QPolygonF(QVector({QPointF(1794.87,819.433), QPointF(2264.27,816.404), QPointF(2664.69,1583.79), QPointF(2126.14,1598.38), QPointF(1794.87,819.433)})));
+ QPainterPath shape2 = 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 expected =
+
+ testRemoveGutterOneCase(shape1, shape2, QLineF(QPointF(2606,1028),QPointF(2164,1134)), false);
+
+ qCritical() << "############################### CASE [11] ####################################";
+
+ shape1 = fromPolygon(QPolygonF(QVector({QPointF(1134.24,823.696),QPointF(2302.64,816.155),QPointF(2275.28,763.723),QPointF(1133.64,771.092),QPointF(1127.23,210),QPointF(3270,210),QPointF(3270,1567.38),
+ QPointF(1143.4,1625.02),QPointF(1134.24,823.696)})));
+
+ testRemoveGutterOneCase(shape1, shape1, QLineF(QPointF(1512,526), QPointF(1666,1178)), true);
+
+
+ qCritical() << "############################### CASE [12] ####################################";
+
+ shape1 = fromPolygon(QPolygonF(QVector({QPointF(1, 0),QPointF(3, 0),QPointF(3, 3),QPointF(0, 3),QPointF(0, 2),QPointF(2, 2),QPointF(2, 1),
+ QPointF(1, 1),QPointF(1, 0)})));
+
+ testRemoveGutterOneCase(shape1, shape1, QLineF(QPointF(1.5, 0.5), QPointF(1.5, 2.5)), true);
+
+
+
+
}
+
void KisAlgebra2DTest::testMergingPainterPaths()
{
QPainterPath first; // counter-clockwise
@@ -1339,29 +1433,46 @@ void KisAlgebra2DTest::testMergingPainterPaths()
// 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));
+ shape1.moveTo(QPointF(2302.6416666667, 816.1541666667));
+ shape1.lineTo(QPointF(3270, 809.9083333333));
+ shape1.lineTo(QPointF(3270, 1567.3791666667));
+ shape1.lineTo(QPointF(2702.6541666667, 1582.7583333333));
+ shape1.lineTo(QPointF(2302.6416666667, 816.1541666667));
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));
+ shape2.moveTo(QPointF(3077.9625000000, 2307));
+ shape2.lineTo(QPointF(2427.4958333333, 2307));
+ shape2.lineTo(QPointF(2148.4958333333, 1650.95));
+ shape2.lineTo(QPointF(3073.0250000000, 1625.8916666667));
+ shape2.lineTo(QPointF(3077.9625000000, 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);
+ //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));
+
+ gutter.moveTo(QPointF(2148.4958333333, 1650.9500000000));
+ gutter.lineTo(QPointF(3073.0250000000, 1625.8916666667));
+ gutter.lineTo(QPointF(3072.6395905126, 1572.7290581307));
+ gutter.lineTo(QPointF(3270.0000000000, 1567.3791666667));
+ gutter.lineTo(QPointF(2702.6541666667, 1582.7583333333));
+ gutter.lineTo(QPointF(2730.0121775463, 1635.1886471758));
+ gutter.lineTo(QPointF(2148.4958333333, 1650.9500000000));
+
+
+
+ qCritical() << Qt::fixed << qSetRealNumberPrecision(10) << ppVar(shape1 | shape2 | gutter);
+
+ 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);
+
}
diff --git a/libs/image/tests/kis_algebra_2d_test.h b/libs/image/tests/kis_algebra_2d_test.h
index 38eefe75eb..4a85431677 100644
--- a/libs/image/tests/kis_algebra_2d_test.h
+++ b/libs/image/tests/kis_algebra_2d_test.h
@@ -40,6 +40,8 @@ private Q_SLOTS:
void testGetParallelLines();
void testGetParallelLines2();
+
+ void testVectorPathReversed();
void testRemoveGutter();
void testMergingPainterPaths();
diff --git a/plugins/tools/tool_knife/RemoveGutterStrategy.cpp b/plugins/tools/tool_knife/RemoveGutterStrategy.cpp
index 9af651f287..11b85270dd 100644
--- a/plugins/tools/tool_knife/RemoveGutterStrategy.cpp
+++ b/plugins/tools/tool_knife/RemoveGutterStrategy.cpp
@@ -262,7 +262,7 @@ void RemoveGutterStrategy::finishInteraction(Qt::KeyboardModifiers modifiers)
- QPainterPath result = KisAlgebra2D::removeGutterSmart(srcOutlines[shapeNewIndexes[0]], lineSegmentIndexes[0], srcOutlines[shapeNewIndexes[1]], lineSegmentIndexes[1]);
+ QPainterPath result = KisAlgebra2D::removeGutterSmart(srcOutlines[shapeNewIndexes[0]], lineSegmentIndexes[0], srcOutlines[shapeNewIndexes[1]], lineSegmentIndexes[1], shapeNewIndexes[0]==shapeNewIndexes[1]);
qCritical() << "Finally got a result:";
convertShapeToDebugArray(result);
@@ -335,7 +335,7 @@ void RemoveGutterStrategy::finishInteraction(Qt::KeyboardModifiers modifiers)
- qCritical() << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ END OF REMOVING A GUTTER, SUCCESSFUL, yay! :) ";
+ qCritical() << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ END OF REMOVING A GUTTER, it did happen, whether successful or not... ";