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/loader/marblegeometryassembler_p.h>
0008 
0009 #include <QDateTime>
0010 #include <QTest>
0011 
0012 using namespace KOSMIndoorMap;
0013 
0014 #define ADD_NODE(_id, lat, lon) { OSM::Node n; n.id = _id; n.coordinate = OSM::Coordinate(lat, lon); dataSet.addNode(std::move(n)); }
0015 
0016 class MarbleGeometryAssemblerTest : public QObject
0017 {
0018     Q_OBJECT
0019 private Q_SLOTS:
0020     void testLineMerge()
0021     {
0022         OSM::DataSet dataSet;
0023         OSM::DataSetMergeBuffer mergeBuffer;
0024         auto mxoidKey = dataSet.makeTagKey("mx:oid");
0025 
0026         // -1,-1 -> 1,1 split at 0,0
0027         ADD_NODE(1, -1.0, -1.0)
0028         ADD_NODE(2, 1.0, 1.0)
0029 
0030         ADD_NODE(-1, 0.0, 0.0)
0031         ADD_NODE(-2, 0.0, 0.0)
0032 
0033         {
0034             OSM::Way w;
0035             w.id = 42;
0036             w.nodes = {1, -1};
0037             dataSet.addWay(std::move(w));
0038         }
0039         {
0040             OSM::Way w;
0041             w.id = -23;
0042             w.nodes = {-2, 2};
0043             OSM::setTagValue(w, mxoidKey, QByteArray::number(42));
0044             mergeBuffer.ways.push_back(std::move(w));
0045         }
0046 
0047         MarbleGeometryAssembler assembler;
0048         assembler.setDataSet(&dataSet);
0049         assembler.merge(&mergeBuffer);
0050 
0051         QCOMPARE(dataSet.ways.size(), 1);
0052         auto &way = dataSet.ways.front();
0053         QCOMPARE(way.id, 42);
0054         QCOMPARE(way.nodes.size(), 2);
0055         QCOMPARE(way.isClosed(), false);
0056         QCOMPARE(way.nodes, std::vector<OSM::Id>({1, 2}));
0057     }
0058 
0059     void testAreaMerge1()
0060     {
0061         OSM::DataSet dataSet;
0062         OSM::DataSetMergeBuffer mergeBuffer;
0063         auto mxoidKey = dataSet.makeTagKey("mx:oid");
0064 
0065         // a 1x1 square around 0,0, cut along the 0 y-axis
0066         ADD_NODE(1, 1.0, -1.0)
0067         ADD_NODE(2, -1.0, -1.0)
0068         ADD_NODE(3, -1.0, 1.0)
0069         ADD_NODE(4, 1.0, 1.0)
0070 
0071         ADD_NODE(-1, 1.0, 0.0)
0072         ADD_NODE(-2, -1.0, 0.0)
0073         ADD_NODE(-3, 1.0, 0.0) // synthectic nodes are duplicated for each part
0074         ADD_NODE(-4, -1.0, 0.0)
0075 
0076         {
0077             OSM::Way w;
0078             w.id = 42;
0079             w.nodes = {-2, -1, 1, 2, -2};
0080             dataSet.addWay(std::move(w));
0081         }
0082         {
0083             OSM::Way w;
0084             w.id = -23;
0085             w.nodes = { 3, 4, -3, -4, 3};
0086             OSM::setTagValue(w, mxoidKey, QByteArray::number(42));
0087             mergeBuffer.ways.push_back(std::move(w));
0088         }
0089 
0090         MarbleGeometryAssembler assembler;
0091         assembler.setDataSet(&dataSet);
0092         assembler.merge(&mergeBuffer);
0093 
0094         QCOMPARE(dataSet.ways.size(), 1);
0095         auto &way = dataSet.ways.front();
0096         QCOMPARE(way.id, 42);
0097         QCOMPARE(way.nodes.size(), 5);
0098         QCOMPARE(way.isClosed(), true);
0099         QCOMPARE(way.nodes, std::vector<OSM::Id>({3, 4, 1, 2, 3}));
0100 
0101         // try the same with reversed roles
0102         way.nodes = { 3, 4, -3, -4, 3};
0103         {
0104             OSM::Way w;
0105             w.id = -23;
0106             w.nodes = {-2, -1, 1, 2, -2};
0107             OSM::setTagValue(w, mxoidKey, QByteArray::number(42));
0108             mergeBuffer.ways = {w};
0109         }
0110         assembler.merge(&mergeBuffer);
0111 
0112         QCOMPARE(dataSet.ways.size(), 1);
0113         way = dataSet.ways.front();
0114         QCOMPARE(way.id, 42);
0115         QCOMPARE(way.nodes.size(), 5);
0116         QCOMPARE(way.isClosed(), true);
0117         QCOMPARE(way.nodes, std::vector<OSM::Id>({3, 4, 1, 2, 3}));
0118     }
0119 
0120     void testAreaMerge2()
0121     {
0122         OSM::DataSet dataSet;
0123         OSM::DataSetMergeBuffer mergeBuffer;
0124         auto mxoidKey = dataSet.makeTagKey("mx:oid");
0125 
0126         // just one node on the "other" side, split along the 0 y-axis
0127         ADD_NODE(1, 0.0, -1.0)
0128         ADD_NODE(2, 2.0, 1.0)
0129         ADD_NODE(3, -2.0, 1.0)
0130         ADD_NODE(4, 0.0, 2.0)
0131 
0132         ADD_NODE(-1, 1.0, 0.0)
0133         ADD_NODE(-2, -1.0, 0.0)
0134         ADD_NODE(-3, 1.0, 0.0) // synthectic nodes are duplicated for each part
0135         ADD_NODE(-4, -1.0, 0.0)
0136 
0137         {
0138             OSM::Way w;
0139             w.id = 42;
0140             w.nodes = {-1, 1, -2, -1};
0141             dataSet.addWay(std::move(w));
0142         }
0143         {
0144             OSM::Way w;
0145             w.id = -23;
0146             w.nodes = { 3, 4, 2, -3, -4, 3 };
0147             OSM::setTagValue(w, mxoidKey, QByteArray::number(42));
0148             mergeBuffer.ways.push_back(std::move(w));
0149         }
0150 
0151         MarbleGeometryAssembler assembler;
0152         assembler.setDataSet(&dataSet);
0153         assembler.merge(&mergeBuffer);
0154 
0155         QCOMPARE(dataSet.ways.size(), 1);
0156         auto &way = dataSet.ways.front();
0157         QCOMPARE(way.id, 42);
0158         QCOMPARE(way.nodes.size(), 5);
0159         QCOMPARE(way.isClosed(), true);
0160         qDebug() << way.nodes;
0161         QCOMPARE(way.nodes, std::vector<OSM::Id>({1, 3, 4, 2, 1}));
0162     }
0163 
0164     void testAreaMergeQuadSplit()
0165     {
0166         OSM::DataSet dataSet;
0167         OSM::DataSetMergeBuffer mergeBuffer;
0168         auto mxoidKey = dataSet.makeTagKey("mx:oid");
0169 
0170         // a 1,1 -> -1,-1 quad split in four parts along the 0 axis
0171         // this results in a synthetic node in the center that isn't on any of the edges
0172         ADD_NODE(1, 1.0, 1.0)
0173         ADD_NODE(2, 1.0, -1.0)
0174         ADD_NODE(3, -1.0, -1.0)
0175         ADD_NODE(4, 1.0, -1.0)
0176 
0177         ADD_NODE(-12, 1.0, 0.0)
0178         ADD_NODE(-13, 0.0, 0.0)
0179         ADD_NODE(-14, 0.0, 1.0)
0180         OSM::Way w1;
0181         w1.id = 101;
0182         w1.nodes = {1, -12, -13, -14, 1};
0183 
0184         ADD_NODE(-21, 1.0, 0.0)
0185         ADD_NODE(-23, 0.0, -1.0)
0186         ADD_NODE(-24, 0.0, 0.0)
0187         OSM::Way w2;
0188         w2.id = -102;
0189         w2.nodes = { -21, 2, -23, -24, -21 };
0190         OSM::setTagValue(w2, mxoidKey, QByteArray::number(101));
0191 
0192         ADD_NODE(-31, 0.0, 0.0)
0193         ADD_NODE(-32, 0.0, -1.0)
0194         ADD_NODE(-34, -1.0, 0.0)
0195         OSM::Way w3;
0196         w3.id = -103;
0197         w3.nodes = { -31, -32, 3, -34, -31 };
0198         OSM::setTagValue(w3, mxoidKey, QByteArray::number(101));
0199 
0200         ADD_NODE(-41, 0.0, 1.0)
0201         ADD_NODE(-42, 0.0, 0.0)
0202         ADD_NODE(-43, -1.0, 0.0)
0203         OSM::Way w4;
0204         w4.id = -104;
0205         w4.nodes = { -41, -42, -43, 4, -41 };
0206         OSM::setTagValue(w4, mxoidKey, QByteArray::number(101));
0207 
0208         MarbleGeometryAssembler assembler;
0209         assembler.setDataSet(&dataSet);
0210 
0211         mergeBuffer.ways.push_back(std::move(w1));
0212         assembler.merge(&mergeBuffer);
0213         mergeBuffer.ways.push_back(std::move(w2));
0214         assembler.merge(&mergeBuffer);
0215         mergeBuffer.ways.push_back(std::move(w3));
0216         assembler.merge(&mergeBuffer);
0217         mergeBuffer.ways.push_back(std::move(w4));
0218         assembler.merge(&mergeBuffer);
0219         assembler.finalize();
0220 
0221         QCOMPARE(dataSet.ways.size(), 1);
0222         auto &way = dataSet.ways.front();
0223         QCOMPARE(way.id, 101);
0224         QCOMPARE(way.nodes.size(), 5);
0225         QCOMPARE(way.isClosed(), true);
0226         qDebug() << way.nodes;
0227         QCOMPARE(way.nodes, std::vector<OSM::Id>({1, 2, 3, 4, 1}));
0228     }
0229 };
0230 
0231 QTEST_GUILESS_MAIN(MarbleGeometryAssemblerTest)
0232 
0233 #include "marblegeometryassemblertest.moc"