File indexing completed on 2024-04-14 04:35:56

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/view.h>
0008 
0009 #include <QTest>
0010 
0011 using namespace KOSMIndoorMap;
0012 
0013 class MapViewTest : public QObject
0014 {
0015     Q_OBJECT
0016 private Q_SLOTS:
0017     void testProjection()
0018     {
0019         View v;
0020         QCOMPARE(v.mapGeoToScene(OSM::Coordinate{0.0, 0.0}), QPointF(128.0, 128.0));
0021         QCOMPARE(v.mapSceneToGeo(QPointF(128.0, 128.0)), OSM::Coordinate(0.0, 0.0));
0022         QCOMPARE(v.mapGeoToScene(OSM::Coordinate{0.0, -180.0}), QPointF(0.0, 128.0));
0023         QCOMPARE(v.mapSceneToGeo(QPointF(0.0, 128.0)), OSM::Coordinate(0.0, -180.0));
0024         QCOMPARE(v.mapGeoToScene(OSM::Coordinate{0.0, 180.0}), QPointF(256.0, 128.0));
0025         QCOMPARE(v.mapSceneToGeo(QPointF(256.0, 128.0)), OSM::Coordinate(0.0, 180.0));
0026 
0027         QCOMPARE(v.mapGeoToScene(OSM::Coordinate{90.0, 0.0}).toPoint().y(), 0);
0028         QCOMPARE(v.mapSceneToGeo(QPointF(0.0, 128.0)).latF(), 0.0);
0029         QCOMPARE(v.mapGeoToScene(OSM::Coordinate{85.0512, 0.0}).toPoint().y(), 0);
0030         QCOMPARE(v.mapSceneToGeo(QPointF(0.0, 0.0)).latF(), 85.0511287);
0031         QCOMPARE(v.mapGeoToScene(OSM::Coordinate{60.0, 0.0}).toPoint().y(), 74);
0032         QCOMPARE(v.mapGeoToScene(OSM::Coordinate{-60.0, 0.0}).toPoint().y(), 182);
0033         QCOMPARE(v.mapGeoToScene(OSM::Coordinate{-85.0512, 0.0}).toPoint().y(), 256.0);
0034         QCOMPARE(v.mapSceneToGeo(QPointF(0.0, 256.0)).latF(), -85.0511288);
0035         QCOMPARE(v.mapGeoToScene(OSM::Coordinate{-90.0, 0.0}).toPoint().y(), 256.0);
0036 
0037         QCOMPARE(v.mapGeoToScene(OSM::BoundingBox{OSM::Coordinate{-90.0, -180.0}, OSM::Coordinate{90.0, 180.0}}).toRect(), QRect(0, 0, 256, 256));
0038         QCOMPARE(v.mapGeoToScene(OSM::BoundingBox{OSM::Coordinate{0.0, 0.0}, OSM::Coordinate{90.0, 90.0}}).toRect(), QRect(128, 0, 64, 128));
0039     }
0040 
0041     void testViewport()
0042     {
0043         {
0044             View v;
0045             v.setScreenSize({100, 200});
0046             v.setSceneBoundingBox(QRectF{QPointF{13.0, 52.0}, QPointF{14.0, 54.0}});
0047             QCOMPARE(v.viewport(), QRectF(QPointF{13.0, 52.0}, QPointF{14.0, 54.0}));
0048         } {
0049             View v;
0050             v.setScreenSize({200, 100});
0051             v.setSceneBoundingBox(QRectF(QPointF{13.0, 52.0}, QPointF{14.0, 54.0}));
0052             QCOMPARE(v.viewport(), QRectF(QPointF{13.0, 52.0}, QPointF{14.0, 52.5}));
0053         } {
0054             View v;
0055             v.setScreenSize({100, 100});
0056             v.setSceneBoundingBox(QRectF(QPointF{13.0, 52.0}, QPointF{14.0, 54.0}));
0057             QCOMPARE(v.viewport(), QRectF(QPointF{13.0, 52.0}, QPointF{14.0, 53.0}));
0058         }
0059     }
0060 
0061     void testTransform()
0062     {
0063         {
0064             View v;
0065             v.setScreenSize({100, 100});
0066             v.setSceneBoundingBox(QRectF(QPointF{13.0, 53.0}, QPointF{14.0, 54.0}));
0067             QCOMPARE(v.sceneToScreenTransform().m11(), 100.0);
0068             QCOMPARE(v.sceneToScreenTransform().m22(), 100.0);
0069             QCOMPARE(v.sceneToScreenTransform().m31(), -1300.0);
0070             QCOMPARE(v.sceneToScreenTransform().m32(), -5300.0);
0071         } {
0072             View v;
0073             v.setScreenSize({100, 100});
0074             v.setSceneBoundingBox(QRectF(QPointF{13.0, 52.0}, QPointF{15.0, 53.0}));
0075             QCOMPARE(v.sceneToScreenTransform().m11(), 100.0);
0076             QCOMPARE(v.sceneToScreenTransform().m22(), 100.0);
0077             QCOMPARE(v.sceneToScreenTransform().m31(), -1300.0);
0078             QCOMPARE(v.sceneToScreenTransform().m32(), -5200.0);
0079         } {
0080             View v;
0081             v.setScreenSize({100, 200});
0082             v.setSceneBoundingBox(QRectF(QPointF{13.0, 52.0}, QPointF{13.1, 52.5}));
0083             QCOMPARE(v.sceneToScreenTransform().m11(), 1000.0);
0084             QCOMPARE(v.sceneToScreenTransform().m22(), 1000.0);
0085             QCOMPARE(v.sceneToScreenTransform().m31(), -13000.0);
0086             QCOMPARE(v.sceneToScreenTransform().m32(), -52000.0);
0087         }
0088     }
0089 
0090     void testZoomLevel()
0091     {
0092         View v;
0093         v.setScreenSize({512, 256});
0094         v.setSceneBoundingBox(QRectF(QPointF{-180.0, -90.0}, QPointF{180.0, 90.0}));
0095         QCOMPARE(v.zoomLevel(), 1.0);
0096         v.setScreenSize({1024, 512});
0097         v.setSceneBoundingBox(QRectF(QPointF{-180.0, -90.0}, QPointF{180.0, 90.0}));
0098         QCOMPARE(v.zoomLevel(), 2.0);
0099         v.zoomIn({512, 256});
0100         QCOMPARE(v.zoomLevel(), 3.0);
0101         v.zoomOut({512, 256});
0102         QCOMPARE(v.zoomLevel(), 2.0);
0103         v.zoomOut({512, 256});
0104         v.zoomOut({512, 256});
0105         v.zoomOut({512, 256});
0106         QCOMPARE(v.zoomLevel(), 2.0);
0107     }
0108 
0109     void testPan()
0110     {
0111         View v;
0112         v.setScreenSize({512, 256});
0113         v.setSceneBoundingBox(QRectF(QPointF{-180.0, -90.0}, QPointF{180.0, 90.0}));
0114         QCOMPARE(v.viewport(), QRectF(QPointF{-180.0, -90.0}, QPointF{180.0, 90.0}));
0115         v.zoomIn({256, 128});
0116         v.panScreenSpace(QPoint(-10000, -10000));
0117         QCOMPARE(v.viewport().left(), -180.0);
0118         QCOMPARE(v.viewport().top(), -90.0);
0119         v.panScreenSpace(QPoint(10000, 10000));
0120         QCOMPARE(v.viewport().left(), 0.0);
0121         QCOMPARE(v.viewport().top(), 0.0);
0122     }
0123 };
0124 
0125 QTEST_GUILESS_MAIN(MapViewTest)
0126 
0127 #include "mapviewtest.moc"