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 isPolygonTrulyConvex to KisAlgebra2D

Agata Cackoc68c4a368816
diff --git a/libs/global/kis_algebra_2d.h b/libs/global/kis_algebra_2d.h
index 55cd8317d9..0090635af4 100644
--- a/libs/global/kis_algebra_2d.h
+++ b/libs/global/kis_algebra_2d.h
@@ -835,6 +835,44 @@ bool isPolygonRect(const Polygon &poly, Difference tolerance) {
}
+
+template <class T>
+bool isPolygonTrulyConvex(const QVector<T> &polygon) {
+ const 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());
+
+
+ qreal prevAngle = secondAngle;
+ secondAngle -= baseAngle;
+
+ 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;
+ }
+ 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 (int(signZZ(nextAngle)) != sign) {
+ return false;
+ }
+ }
+ return true;
+}
+
struct KRITAGLOBAL_EXPORT DecomposedMatrix {
DecomposedMatrix();
diff --git a/libs/image/tests/kis_algebra_2d_test.cpp b/libs/image/tests/kis_algebra_2d_test.cpp
index 653bafa78f..d27040c96a 100644
--- a/libs/image/tests/kis_algebra_2d_test.cpp
+++ b/libs/image/tests/kis_algebra_2d_test.cpp
@@ -1885,4 +1885,24 @@ void KisAlgebra2DTest::testMultiplyWrappedRectVertical()
#endif
}
+void KisAlgebra2DTest::testIsPolygonTrulyConvex_data()
+{
+ QTest::addColumn<QPolygonF>("polygon");
+ 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;
+
+}
+
+
+void KisAlgebra2DTest::testIsPolygonTrulyConvex()
+{
+
+ QFETCH(QPolygonF, polygon);
+ QFETCH(bool, isConvex);
+
+ QCOMPARE(isConvex, KisAlgebra2D::isPolygonTrulyConvex(polygon));
+
+}
+
SIMPLE_TEST_MAIN(KisAlgebra2DTest)
diff --git a/libs/image/tests/kis_algebra_2d_test.h b/libs/image/tests/kis_algebra_2d_test.h
index 4a85431677..04cacc659a 100644
--- a/libs/image/tests/kis_algebra_2d_test.h
+++ b/libs/image/tests/kis_algebra_2d_test.h
@@ -62,6 +62,10 @@ private Q_SLOTS:
void testMultiplyWrappedRectVertical_data();
void testMultiplyWrappedRectVertical();
+
+ void testIsPolygonTrulyConvex_data();
+ void testIsPolygonTrulyConvex();
+
};
#endif /* __KIS_ALGEBRA_2D_TEST_H */