File indexing completed on 2024-05-26 08:33:00

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <map/scene/scenegeometry_p.h>
0008 
0009 #include <QFile>
0010 #include <QTest>
0011 
0012 #include <cmath>
0013 
0014 using namespace KOSMIndoorMap;
0015 
0016 class SceneGeometryTest: public QObject
0017 {
0018     Q_OBJECT
0019 private Q_SLOTS:
0020     void testPolygonCenter()
0021     {
0022         QPolygonF p1{{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}};
0023         QCOMPARE(SceneGeometry::polygonCentroid(p1), QPointF(0, 0));
0024 
0025         QPolygonF p2{{{0, 1}, {1, 1}, {1, 0}, {0, 0}}};
0026         QCOMPARE(SceneGeometry::polygonCentroid(p2), QPointF(0.5, 0.5));
0027 
0028         QPolygonF p3{{{0, 1}, {1, 0}, {0, -1}, {-0.5, -0.5}, {-1, 0}}};
0029         QCOMPARE(SceneGeometry::polygonCentroid(p3), QPointF(0, 0));
0030 
0031         QPolygonF p4{{{0.273, 0.146}, {0.423, 0.496}, {0.415, 0.499}, {0.266, 0.149}}};
0032 //         QCOMPARE((SceneGeometry::polygonCentroid(p4) * 1000000).toPoint(), QPoint(345669, 325821));
0033         // make the test pass on 32bit platforms too
0034         QCOMPARE((SceneGeometry::polygonCentroid(p4) * 1000000).toPoint().x(), 345669);
0035         QVERIFY((SceneGeometry::polygonCentroid(p4) * 1000000).toPoint().y() >= 325821);
0036         QVERIFY((SceneGeometry::polygonCentroid(p4) * 1000000).toPoint().y() <= 325822);
0037 
0038         QPolygonF p5{{{273, 146}, {423, 496}, {415, 499}, {266, 149}}};
0039 //         QCOMPARE((SceneGeometry::polygonCentroid(p5) * 1000).toPoint(), QPoint(345669, 325821));
0040         // make the test pass on 32bit platforms too
0041         QCOMPARE((SceneGeometry::polygonCentroid(p5) * 1000).toPoint().x(), 345669);
0042         QVERIFY((SceneGeometry::polygonCentroid(p5) * 1000).toPoint().y() >= 325821);
0043         QVERIFY((SceneGeometry::polygonCentroid(p5) * 1000).toPoint().y() <= 325822);
0044     }
0045 
0046     void testLineMidPoint()
0047     {
0048         QPolygonF p1{{{1,1}, {2,2}, {2,2}}};
0049         QCOMPARE(SceneGeometry::polylineMidPoint(p1), QPointF(1.5, 1.5));
0050 
0051         QPolygonF p2{{{1,1}, {2,2}, {3,3}}};
0052         QCOMPARE(SceneGeometry::polylineMidPoint(p2), QPointF(2, 2));
0053 
0054         QPolygonF p3{{{1,1}, {2,2}, {21,21}}};
0055         QCOMPARE(SceneGeometry::polylineMidPoint(p3), QPointF(11, 11));
0056     }
0057 
0058     void testPathAngle()
0059     {
0060         QPolygonF p1{{{1,1}, {2,2}, {2,2}}};
0061         QCOMPARE(SceneGeometry::polylineMidPointAngle(p1), 45.0);
0062 
0063         QPolygonF p2{{{1,1}, {2,2}, {2,20}}};
0064         QCOMPARE(SceneGeometry::polylineMidPointAngle(p2), 90.0);
0065     }
0066 
0067     void testDistanceToLine()
0068     {
0069         QLineF line({1,1}, {3, 1});
0070         QCOMPARE(SceneGeometry::distanceToLine(line, {0, 1}), 1.0);
0071         QCOMPARE(SceneGeometry::distanceToLine(line, {1, 1}), 0.0);
0072         QCOMPARE(SceneGeometry::distanceToLine(line, {2, 1}), 0.0);
0073         QCOMPARE(SceneGeometry::distanceToLine(line, {3, 1}), 0.0);
0074         QCOMPARE(SceneGeometry::distanceToLine(line, {4, 1}), 1.0);
0075 
0076         QCOMPARE(SceneGeometry::distanceToLine(line, {0, 2}), std::sqrt(2.0));
0077         QCOMPARE(SceneGeometry::distanceToLine(line, {1, 2}), 1.0);
0078         QCOMPARE(SceneGeometry::distanceToLine(line, {2, 2}), 1.0);
0079         QCOMPARE(SceneGeometry::distanceToLine(line, {3, 2}), 1.0);
0080         QCOMPARE(SceneGeometry::distanceToLine(line, {4, 2}), std::sqrt(2.0));
0081 
0082         QCOMPARE(SceneGeometry::distanceToLine(line, {0, 0}), std::sqrt(2.0));
0083         QCOMPARE(SceneGeometry::distanceToLine(line, {1, 0}), 1.0);
0084         QCOMPARE(SceneGeometry::distanceToLine(line, {2, 0}), 1.0);
0085         QCOMPARE(SceneGeometry::distanceToLine(line, {3, 0}), 1.0);
0086         QCOMPARE(SceneGeometry::distanceToLine(line, {4, 0}), std::sqrt(2.0));
0087     }
0088 };
0089 
0090 QTEST_GUILESS_MAIN(SceneGeometryTest)
0091 
0092 #include "scenegeometrytest.moc"