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 isInsideShape to KisAlgebra2D
Agata Cacko1313eab4d488
diff --git a/libs/global/kis_algebra_2d.cpp b/libs/global/kis_algebra_2d.cppindex 34550b53aa..179e4e6268 100644--- a/libs/global/kis_algebra_2d.cpp+++ b/libs/global/kis_algebra_2d.cpp@@ -28,6 +28,7 @@#endif /*HAVE_GSL*/#include <Eigen/Eigenvalues>+#include <KisBezierUtils.h>#define SANITY_CHECKS@@ -2091,6 +2092,40 @@ QLineF VectorPath::segmentAtAsLine(int i) constreturn QLineF(points[0].endPoint, points[1].endPoint);}+QList<QPointF> VectorPath::intersectSegmentWithLineBounded(const QLineF &line, const Segment &segment)+{+ qreal eps = 1e-5;+ if (segment.type != VectorPath::VectorPathPoint::BezierTo) {+ QLineF segLine = QLineF(segment.startPoint, segment.endPoint);+ QPointF intersection;+ if (segLine.intersects(line, &intersection) == QLineF::BoundedIntersection) {+ return QList<QPointF> {intersection};+ } else {+ return QList<QPointF>();+ }++ } else {+ QList<QPointF> result;+ // check for intersections+ QVector<qreal> intersections = KisBezierUtils::intersectWithLine(segment.startPoint, segment.controlPoint1, segment.controlPoint2, segment.endPoint, line, eps);+ for (int i = 0; i < intersections.count(); i++) {+ QPointF p = KisBezierUtils::bezierCurve(segment.startPoint, segment.controlPoint1, segment.controlPoint2, segment.endPoint, intersections[i]);+ bool onLine = isOnLine(line, p, eps, true, true, true);+ if (onLine) {+ result << p;+ }+ }++ return result;+ }++}++QList<QPointF> VectorPath::intersectSegmentWithLineBounded(const QLineF &line, const VectorPathPoint &p1, const VectorPathPoint &p2)+{+ return intersectSegmentWithLineBounded(line, VectorPath::Segment(p1, p2));+}+int VectorPath::pathIndexToSegmentIndex(int index){// first should be MoveTo@@ -2327,9 +2362,6 @@ QDebug operator<<(QDebug debug, const VectorPath &path)return debug;}---QDebug operator<<(QDebug debug, const VectorPath::VectorPathPoint &point){bool autoInsertSpaces = debug.autoInsertSpaces();@@ -2348,5 +2380,131 @@ QDebug operator<<(QDebug debug, const VectorPath::VectorPathPoint &point)return debug;}+bool isInsideShape(const VectorPath &path, const QPointF &point)+{+ if (path.pointsCount() == 0) {+ return false;+ }++ QRectF boundRect;+ accumulateBounds(path.pointAt(0).endPoint, &boundRect);++ bool isPolygon = true;++ for (int i = 0; i < path.segmentsCount(); i++) {+ QList<VectorPath::VectorPathPoint> points = path.segmentAt(i);+ accumulateBounds(points[0].endPoint, &boundRect);+ accumulateBounds(points[1].endPoint, &boundRect);++ if (points[1].type == VectorPath::VectorPathPoint::BezierTo) {+ accumulateBounds(points[1].controlPoint1, &boundRect);+ accumulateBounds(points[1].controlPoint2, &boundRect);+ isPolygon = false;+ }+ }++ if (!boundRect.contains(point)) {+ return false;+ }++ if (isPolygon) {+ return path.asPainterPath().toFillPolygon().containsPoint(point, Qt::WindingFill);+ }++ boundRect = kisGrowRect(boundRect, 5); // just safety margins++ // NOTE: it would be way faster to do it taking into account the fact that the lines are vertical and horizontal+ // except for the last one maybe+ // but let's wait until it proves to be a problem+ QList<QLineF> lines;+ lines << QLineF(point, QPointF(boundRect.left(), point.y()));+ lines << QLineF(point, QPointF(boundRect.right(), point.y()));+ lines << QLineF(point, QPointF(point.x(), boundRect.top()));+ lines << QLineF(point, QPointF(point.x(), boundRect.bottom()));+ lines << QLineF(point, boundRect.topLeft()); // last resort, diagonal++ int intersectionsCount = 0;++ for (int k = 0; k < lines.length(); k++) {+ int intersections = 0;+ QLineF line = lines[k];+ bool checkNext = false;++ for (int i = 0; i < path.segmentsCount(); i++) {+ std::optional<VectorPath::Segment> segmentOpt = path.segmentAtAsSegment(i);+ KIS_SAFE_ASSERT_RECOVER(segmentOpt.has_value()) {continue;}+ VectorPath::Segment segment = segmentOpt.value();++ QList<QPointF> intersectionsHere = VectorPath::intersectSegmentWithLineBounded(line, segment);+ qreal eps = 1e-4;++ for (int i = 0; i < intersectionsHere.count(); i++) {+ if (fuzzyPointCompare(intersectionsHere[i], segment.startPoint, eps) || fuzzyPointCompare(intersectionsHere[i], segment.endPoint, eps)) {+ checkNext = true;+ break;+ }+ }++ if (checkNext) {+ break;+ }++ intersections += intersectionsHere.count();+ }++ intersectionsCount = intersections;+ if (!checkNext) {+ break;+ }+ }++ return intersectionsCount%2 == 1;+}++bool isInsideShape(const QPainterPath &path, const QPointF &point)+{+ for (int i = 0; i < path.elementCount(); i++) {+ if (path.elementAt(i).type == QPainterPath::CurveToDataElement) {+ // contains control points+ return isInsideShape(VectorPath(path), point);+ }+ }++ return path.toFillPolygon().containsPoint(point, Qt::WindingFill);+}++bool isOnLine(const QLineF &line, const QPointF &point, const qreal eps, bool boundedStart, bool boundedEnd, bool includeEnds)+{+ if (fuzzyPointCompare(point, line.p1(), eps)) {+ return includeEnds || !boundedStart;+ }+ if (fuzzyPointCompare(point, line.p2(), eps)) {+ return includeEnds || !boundedEnd;+ }++ if (kisDistanceToLine(point, line) > eps) {+ return false;+ }++ QPointF lineVector = line.p2() - line.p1();+ QPointF pointVector = point - line.p1();+ qreal t = -1;+ if (qAbs(pointVector.x()) < eps) {+ // use y+ t = pointVector.y()/lineVector.y();+ } else {+ t = pointVector.x()/lineVector.x();+ }++ if (t < 0) {+ return !boundedStart;+ }+ if (t > 1.0) {+ return !boundedEnd;+ }++ return true;+}+} // namespacediff --git a/libs/global/kis_algebra_2d.h b/libs/global/kis_algebra_2d.hindex 9803d1529f..cb4839b0f8 100644--- a/libs/global/kis_algebra_2d.h+++ b/libs/global/kis_algebra_2d.h@@ -664,6 +664,11 @@ public:QLineF segmentAtAsLine(int i) const;+ static QList<QPointF> intersectSegmentWithLineBounded(const QLineF &line, const Segment &segment);+ static QList<QPointF> intersectSegmentWithLineBounded(const QLineF &line, const VectorPathPoint &p1, const VectorPathPoint &p2);+++int pathIndexToSegmentIndex(int index);int segmentIndexToPathIndex(int index);@@ -1024,6 +1029,9 @@ QVector<QPointF> KRITAGLOBAL_EXPORT findTrianglePoint(const QPointF &p1, const Q*/boost::optional<QPointF> KRITAGLOBAL_EXPORT findTrianglePointNearest(const QPointF &p1, const QPointF &p2, qreal a, qreal b, const QPointF &nearest);+bool isOnLine(const QLineF &line, const QPointF &point, const qreal eps, bool boundedStart, bool boundedEnd, bool includeEnds);++/*** @brief moveElasticPoint moves point \p pt based on the model of elasticity* @param pt point in question, tied to points \p base, \p wingA and \p wingB@@ -1210,7 +1218,13 @@ VectorPath mergeShapesWithGutter(const VectorPath& shape1, const VectorPath& sha*/QPainterPath KRITAGLOBAL_EXPORT trySimplifyPath(const QPainterPath &path, qreal lengthThreshold);-}+bool KRITAGLOBAL_EXPORT isInsideShape(const VectorPath &path, const QPointF &point);+bool KRITAGLOBAL_EXPORT isInsideShape(const QPainterPath &path, const QPointF &point);+++++}#endif /* __KIS_ALGEBRA_2D_H */diff --git a/libs/image/tests/kis_algebra_2d_test.cpp b/libs/image/tests/kis_algebra_2d_test.cppindex 93d7e71eb7..effba4d5aa 100644--- a/libs/image/tests/kis_algebra_2d_test.cpp+++ b/libs/image/tests/kis_algebra_2d_test.cpp@@ -1205,6 +1205,63 @@ void KisAlgebra2DTest::testVectorPathIndexes()}}++void KisAlgebra2DTest::testIsInsideShape_data()+{++ using VPoint = KisAlgebra2D::VectorPath::VectorPathPoint;+ QTest::addColumn<QList<VPoint>>("path");+ QTest::addColumn<QPointF>("point");+ QTest::addColumn<bool>("result");+++ qreal optimalDistanceMagicNumber = 4*(sqrt(2) - 1)/3;++ QList<VPoint> squareDiag = QList<VPoint> {+ VPoint(VPoint::MoveTo, QPointF(1, 0)),+ VPoint(VPoint::LineTo, QPointF(0, 1)),+ VPoint(VPoint::LineTo, QPointF(-1, 0)),+ VPoint(VPoint::LineTo, QPointF(0, -1)),+ VPoint(VPoint::LineTo, QPointF(1, 0)),+ };+++ QList<VPoint> circle = QList<VPoint> {+ VPoint(VPoint::MoveTo, QPointF(1, 0)),+ VPoint(VPoint::BezierTo, QPointF(0, 1), QPointF(1, optimalDistanceMagicNumber), QPointF(optimalDistanceMagicNumber, 1)),+ VPoint(VPoint::BezierTo, QPointF(-1, 0), QPointF(-optimalDistanceMagicNumber, 1), QPointF(-1, optimalDistanceMagicNumber)),+ VPoint(VPoint::BezierTo, QPointF(0, -1), QPointF(-1, -optimalDistanceMagicNumber), QPointF(-optimalDistanceMagicNumber, -1)),+ VPoint(VPoint::BezierTo, QPointF(1, 0), QPointF(optimalDistanceMagicNumber, -1), QPointF(1, -optimalDistanceMagicNumber)),+ };++ QTest::addRow("perfect circle and center") << circle << QPointF(0, 0) << true;+ QTest::addRow("perfect circle, not center") << circle << QPointF(0.5, 0.5) << true;+ QTest::addRow("perfect circle, outside") << circle << QPointF(1, 1) << false;++ QTest::addRow("square diag, perfect center") << squareDiag << QPointF(0, 0) << true;+ QTest::addRow("square diag, not center") << squareDiag << QPointF(0.2, 0.2) << true;+ QTest::addRow("square diag, outside") << squareDiag << QPointF(1, 1) << false;++++}+++void KisAlgebra2DTest::testIsInsideShape()+{++ using VPoint = KisAlgebra2D::VectorPath::VectorPathPoint;+ QFETCH(QList<VPoint>, path);+ QFETCH(QPointF, point);+ QFETCH(bool, result);++ bool res = KisAlgebra2D::isInsideShape(path, point);+ QCOMPARE(res, result);+++}++QPainterPath makeSpecialShape(bool left){// left has teeth on the leftdiff --git a/libs/image/tests/kis_algebra_2d_test.h b/libs/image/tests/kis_algebra_2d_test.hindex 6826b1330f..3685042242 100644--- a/libs/image/tests/kis_algebra_2d_test.h+++ b/libs/image/tests/kis_algebra_2d_test.h@@ -45,6 +45,10 @@ private Q_SLOTS:void testVectorPathIndexes_data();void testVectorPathIndexes();++ void testIsInsideShape_data();+ void testIsInsideShape();+void testRemoveGutter();void testMergingPainterPaths();