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 segments and index conversion to VectorPath

Agata Cacko0847e542a847
diff --git a/libs/global/kis_algebra_2d.cpp b/libs/global/kis_algebra_2d.cpp
index 6ffbfbca81..2132e81030 100644
--- a/libs/global/kis_algebra_2d.cpp
+++ b/libs/global/kis_algebra_2d.cpp
@@ -2073,6 +2073,15 @@ QList<VectorPath::VectorPathPoint> VectorPath::segmentAt(int i) const
return response;
}
+std::optional<VectorPath::Segment> VectorPath::segmentAtAsSegment(int i) const
+{
+ QList<VectorPath::VectorPathPoint> segment = segmentAt(i);
+ if (segment.count() == 2) {
+ return VectorPath::Segment(segment[0], segment[1]);
+ }
+ return std::nullopt;
+}
+
QLineF VectorPath::segmentAtAsLine(int i) const
{
QList<VectorPath::VectorPathPoint> points = segmentAt(i);
@@ -2082,6 +2091,48 @@ QLineF VectorPath::segmentAtAsLine(int i) const
return QLineF(points[0].endPoint, points[1].endPoint);
}
+int VectorPath::pathIndexToSegmentIndex(int index)
+{
+ // first should be MoveTo
+ int pathIndex = 0;
+ for (int i = 1; i < m_points.length(); i++) {
+ if (index < pathIndex) {
+ // it was the previous segment, it's just a control point
+ return i - 1;
+ }
+ if (index == pathIndex) {
+ return i - 1;
+ }
+ if (m_points[i].type == VectorPathPoint::BezierTo) {
+ pathIndex += 2; // add space for control points
+ }
+ pathIndex++;
+ }
+ return m_points.length() - 1;
+}
+
+int VectorPath::segmentIndexToPathIndex(int index)
+{
+ // first should be MoveTo
+ int pathIndex = 0;
+ for (int i = 1; i < m_points.length(); i++) {
+ if (i - 1 == index) {
+ if (m_points[i - 1].type == VectorPathPoint::BezierTo) {
+ return pathIndex - 2;
+ }
+ return pathIndex;
+ }
+ if (m_points[i].type == VectorPathPoint::BezierTo) {
+ pathIndex += 2; // add space for control points
+ }
+ pathIndex++;
+ }
+ if (m_points.length() > 0 && m_points[m_points.length() - 1].type == VectorPathPoint::BezierTo) {
+ return pathIndex - 2;
+ }
+ return pathIndex;
+}
+
VectorPath VectorPath::trulySimplified(qreal epsDegrees) const
{
qreal eps = kisDegreesToRadians(epsDegrees);
diff --git a/libs/global/kis_algebra_2d.h b/libs/global/kis_algebra_2d.h
index 0090635af4..3a076390dd 100644
--- a/libs/global/kis_algebra_2d.h
+++ b/libs/global/kis_algebra_2d.h
@@ -18,6 +18,7 @@
#include <kritaglobal_export.h>
#include <functional>
#include <boost/optional.hpp>
+#include <optional>
class QPainterPath;
class QTransform;
@@ -600,7 +601,8 @@ public:
Type type {MoveTo};
-
+ VectorPathPoint() {
+ }
VectorPathPoint(Type _type, QPointF _endPoint, QPointF c1 = QPointF(), QPointF c2 = QPointF()) {
type = _type;
@@ -622,6 +624,32 @@ public:
}
};
+ struct Segment
+ {
+ VectorPathPoint start;
+ VectorPathPoint end;
+
+ QPointF startPoint {QPointF()};
+ QPointF endPoint {QPointF()};
+ QPointF controlPoint1 {QPointF()};
+ QPointF controlPoint2 {QPointF()};
+
+ VectorPathPoint::Type type {VectorPathPoint::MoveTo};
+
+ Segment(VectorPathPoint _start, VectorPathPoint _end)
+ : start(_start)
+ , end(_end)
+ , startPoint(_start.endPoint)
+ , endPoint(_end.endPoint)
+ , controlPoint1(_end.controlPoint1)
+ , controlPoint2(_end.controlPoint2)
+ , type(_end.type)
+ {
+
+ }
+
+ };
+
public:
VectorPath(const QPainterPath& path);
VectorPath(const QList<VectorPathPoint> path);
@@ -631,8 +659,15 @@ public:
VectorPathPoint pointAt(int i) const;
int segmentsCount() const;
QList<VectorPathPoint> segmentAt(int i) const;
+ std::optional<Segment> segmentAtAsSegment(int i) const;
+
QLineF segmentAtAsLine(int i) const;
+
+ int pathIndexToSegmentIndex(int index);
+ int segmentIndexToPathIndex(int index);
+
+
VectorPath trulySimplified(qreal epsDegrees = 0.5) const;
// not open-path friendly
VectorPath reversed() const;
diff --git a/libs/image/tests/kis_algebra_2d_test.cpp b/libs/image/tests/kis_algebra_2d_test.cpp
index d27040c96a..93d7e71eb7 100644
--- a/libs/image/tests/kis_algebra_2d_test.cpp
+++ b/libs/image/tests/kis_algebra_2d_test.cpp
@@ -13,6 +13,9 @@
#include "kis_algebra_2d.h"
#include "kis_debug.h"
+Q_DECLARE_METATYPE(KisAlgebra2D::VectorPath::VectorPathPoint);
+
+
namespace KisAlgebra2D {
}
@@ -1142,6 +1145,66 @@ void KisAlgebra2DTest::testVectorPathReversed()
}
+void KisAlgebra2DTest::testVectorPathIndexes_data()
+{
+
+ using VPoint = KisAlgebra2D::VectorPath::VectorPathPoint;
+ QTest::addColumn<QList<VPoint>>("path");
+
+ QList<VPoint> path;
+ path << VPoint::moveTo(QPointF(0, 0));
+ path << VPoint::lineTo(QPointF(-10, 20));
+ path << VPoint::bezierTo(QPointF(5, 20), QPointF(-5, 30), QPointF(5, 30));
+ path << VPoint::lineTo(QPointF(10, 20));
+ path << VPoint::bezierTo(QPointF(5, 20), QPointF(-5, 30), QPointF(5, 30));
+ path << VPoint::lineTo(QPointF(0, 0));
+
+
+ QTest::addRow("path1") << path;
+
+ QList<VPoint> path2;
+ path2 << VPoint::moveTo(QPointF(0, 0));
+ path2 << VPoint::bezierTo(QPointF(5, 20), QPointF(-5, 30), QPointF(5, 30));
+ path2 << VPoint::lineTo(QPointF(10, 20));
+ path2 << VPoint::bezierTo(QPointF(0, 0), QPointF(-5, 30), QPointF(5, 30));
+
+ QTest::addRow("path2") << path2;
+
+}
+
+
+
+void KisAlgebra2DTest::testVectorPathIndexes()
+{
+ using VPoint = KisAlgebra2D::VectorPath::VectorPathPoint;
+
+ QFETCH(QList<VPoint>, path);
+
+ KisAlgebra2D::VectorPath vpath(path);
+
+ QPainterPath ppath = vpath.asPainterPath();
+ //ENTER_FUNCTION() << ppath;
+
+ for (int i = 0; i < ppath.elementCount(); i++) {
+ int pathIndex = i;
+ int segmentIndex = vpath.pathIndexToSegmentIndex(pathIndex);
+ int pathIndexRev = vpath.segmentIndexToPathIndex(segmentIndex);
+ //ENTER_FUNCTION() << ppVar(pathIndex) << ppVar(segmentIndex) << ppVar(pathIndexRev) << ppVar(ppath.elementAt(i));
+ if (ppath.elementAt(i).type != QPainterPath::CurveToDataElement) {
+ // if it is, then the reversed path index won't match, because it will point to the actual curve point
+ QCOMPARE(pathIndex, pathIndexRev);
+ }
+ }
+
+ for (int i = 0; i < vpath.segmentsCount(); i++) {
+ int segmentIndex = i;
+ int pathIndex = vpath.segmentIndexToPathIndex(segmentIndex);
+ int segmentIndexRev = vpath.pathIndexToSegmentIndex(pathIndex);
+ //ENTER_FUNCTION() << ppVar(segmentIndex) << ppVar(pathIndex) << ppVar(segmentIndexRev);
+ QCOMPARE(segmentIndex, segmentIndexRev);
+ }
+
+}
QPainterPath makeSpecialShape(bool left)
{
// left has teeth on the left
diff --git a/libs/image/tests/kis_algebra_2d_test.h b/libs/image/tests/kis_algebra_2d_test.h
index 04cacc659a..6826b1330f 100644
--- a/libs/image/tests/kis_algebra_2d_test.h
+++ b/libs/image/tests/kis_algebra_2d_test.h
@@ -42,6 +42,9 @@ private Q_SLOTS:
void testVectorPathReversed();
+ void testVectorPathIndexes_data();
+ void testVectorPathIndexes();
+
void testRemoveGutter();
void testMergingPainterPaths();