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 isPolygonTrulyConvex in KisAlgebra2D
Agata Cackof6e6cd5fdf02
diff --git a/libs/global/kis_algebra_2d.h b/libs/global/kis_algebra_2d.hindex ab00aef1f5..5a6c45a6b5 100644--- a/libs/global/kis_algebra_2d.h+++ b/libs/global/kis_algebra_2d.h@@ -893,41 +893,51 @@ bool isPolygonPixelAlignedRect(const Polygon &poly, Difference tolerance) {}-template <class T>-bool isPolygonTrulyConvex(const QVector<T> &polygon) {- const int numPoints = polygon.size();+bool isPolygonTrulyConvex(const QVector<T> &polygon, bool ensureNoLoops = true) {+ int numPoints = polygon.size();if (numPoints < 3)return true;- qreal baseAngle = std::atan2(polygon[1].y() - polygon[0].y(), polygon[1].x() - polygon[0].x());- qreal secondAngle = std::atan2(polygon[2].y() - polygon[1].y(), polygon[2].x() - polygon[1].x());+ if (fuzzyPointCompare(polygon[0], polygon[numPoints - 1])) {+ // common in QPainterPaths to have the startPoint and endPoint the same+ numPoints--;+ }++ int sign = 0;+ qreal angleSum = 0;+ for (int i = 0; i < numPoints; i++) {+ int a = i;+ int b = wrapValue(i + 1, numPoints);+ int c = wrapValue(i + 2, numPoints);- qreal prevAngle = secondAngle;- secondAngle -= baseAngle;+ qreal angle = angleBetweenVectors(static_cast<QPointF>(polygon[b] - polygon[a]), static_cast<QPointF>(polygon[c] - polygon[b]));- qreal sign = signZZ(secondAngle);- for (int i = 2; i < numPoints; i++) {- int next = i == (numPoints - 1) ? 0 : i + 1;- if (next == 0 && fuzzyPointCompare(polygon[next], polygon[i])) {- break;+ if (angle < -M_PI) {+ angle += 2*M_PI;}- qreal nextAngle = std::atan2(polygon[next].y() - polygon[i].y(), polygon[next].x() - polygon[i].x());-- qreal newPrevAngle = nextAngle;- nextAngle -= baseAngle;- nextAngle -= prevAngle;- prevAngle = newPrevAngle;- if (nextAngle < -M_PI) {- nextAngle += 2*M_PI;- } else if (nextAngle > M_PI) {- nextAngle -= 2*M_PI;++ if (angle > M_PI) {+ angle -= 2*M_PI;}- if (int(signZZ(nextAngle)) != sign) {- return false;++ angleSum += angle;++ if (sign == 0) {+ sign = signZZ(angle);+ } else {+ int currentSign = signZZ(angle);+ if (currentSign != 0 && currentSign != sign) {+ return false;+ }}}++ if (ensureNoLoops && !qFuzzyCompare(qAbs(angleSum), 2*M_PI)) {+ return false;+ }+return true;}diff --git a/libs/image/tests/kis_algebra_2d_test.cpp b/libs/image/tests/kis_algebra_2d_test.cppindex d2ba0af7ce..1ff0403424 100644--- a/libs/image/tests/kis_algebra_2d_test.cpp+++ b/libs/image/tests/kis_algebra_2d_test.cpp@@ -2008,7 +2008,22 @@ void KisAlgebra2DTest::testIsPolygonTrulyConvex_data()QTest::addColumn<bool>("isConvex");QTest::addRow("nearly rectangle") << QPolygonF(QVector<QPointF> {QPointF(0,64), QPointF(8.00001,64), QPointF(8.00001,72), QPointF(0,72)}) << true;+ QTest::addRow("concave polygon") << QPolygonF(QVector<QPointF> {QPointF(0,0), QPointF(10,0), QPointF(10, 10), QPointF(-10, -5)}) << false;+ QTest::addRow("concave polygon 2") << QPolygonF(QVector<QPointF> {QPointF(10,0), QPointF(10, 10), QPointF(-10, -5), QPointF(0,0)}) << false;+ QTest::addRow("tangled polygon") << QPolygonF(QVector<QPointF> {QPointF(9,3), QPointF(8, 6), QPointF(4, 4), QPointF(7,3), QPointF(4,8), QPointF(1,5), QPointF(2,2), QPointF(4,1), QPointF(6,1)}) << false;+ QTest::addRow("untangled polygon") << QPolygonF(QVector<QPointF> {QPointF(9,3), QPointF(8, 6), QPointF(4, 8), QPointF(1,5), QPointF(2,2), QPointF(4,1), QPointF(6,1)}) << true;+ QTest::addRow("untangled polygon 2") << QPolygonF(QVector<QPointF> {QPointF(9,3), QPointF(8, 6), QPointF(4, 4), QPointF(6,1)}) << true;++++ QTest::addRow("tangled polygon ccw") << QPolygonF(QVector<QPointF> {QPointF(6,1), QPointF(4,1), QPointF(2,2), QPointF(1,5), QPointF(4,8), QPointF(7,3), QPointF(4, 4), QPointF(8, 6), QPointF(9,3)}) << false;+ QTest::addRow("untangled polygon ccw") << QPolygonF(QVector<QPointF> {QPointF(6,1), QPointF(4,1), QPointF(2,2), QPointF(1,5), QPointF(4, 8), QPointF(8, 6), QPointF(9,3)}) << true;+ QTest::addRow("untangled polygon 2 ccw") << QPolygonF(QVector<QPointF> {QPointF(6,1), QPointF(4, 4), QPointF(8, 6), QPointF(9,3)}) << true;+++ QTest::addRow("tangled polygon ccw, double start point") << QPolygonF(QVector<QPointF> {QPointF(6,1), QPointF(4,1), QPointF(2,2), QPointF(1,5), QPointF(4,8), QPointF(7,3), QPointF(4, 4), QPointF(8, 6), QPointF(9,3), QPointF(6,1)}) << false;+ QTest::addRow("untangled polygon ccw, double start point") << QPolygonF(QVector<QPointF> {QPointF(6,1), QPointF(4,1), QPointF(2,2), QPointF(1,5), QPointF(4, 8), QPointF(8, 6), QPointF(9,3), QPointF(6,1)}) << true;}