File: ViewportParamsTest.cpp

package info (click to toggle)
marble 4%3A25.08.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 159,996 kB
  • sloc: cpp: 191,890; xml: 39,908; ansic: 7,204; python: 2,190; sh: 1,187; makefile: 235; perl: 218; ruby: 97; java: 66
file content (488 lines) | stat: -rw-r--r-- 18,039 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// SPDX-FileCopyrightText: 2012 Thibaut Gridel <tgridel@free.fr>
// SPDX-FileCopyrightText: 2012, 2013 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
//

#include "TestUtils.h"

#include "ViewportParams.h"

#include "AbstractProjection.h"
#include "GeoDataCoordinates.h"
#include "GeoDataLineString.h"
#include "GeoDataLinearRing.h"

Q_DECLARE_METATYPE(Marble::GeoDataLinearRing)
Q_DECLARE_METATYPE(Marble::Projection)
Q_DECLARE_METATYPE(Marble::TessellationFlag)
Q_DECLARE_METATYPE(Marble::TessellationFlags)

namespace Marble
{

class ViewportParamsTest : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void constructorDefaultValues();

    void constructorValues_data();
    void constructorValues();

    void screenCoordinates_GeoDataLineString_data();
    void screenCoordinates_GeoDataLineString();

    void screenCoordinates_GeoDataLineString2();

    void screenCoordinates_GeoDataLinearRing();

    void geoDataLinearRing_data();
    void geoDataLinearRing();

    void setInvalidRadius();

    void setFocusPoint();
};

void ViewportParamsTest::constructorDefaultValues()
{
    const ViewportParams viewport;

    QCOMPARE(viewport.projection(), Spherical);
    QCOMPARE(viewport.size(), QSize(100, 100));
    QCOMPARE(viewport.width(), 100);
    QCOMPARE(viewport.height(), 100);
    QCOMPARE(viewport.centerLongitude(), 0.);
    QCOMPARE(viewport.centerLatitude(), 0.);
    QCOMPARE(viewport.polarity(), 1);
    QCOMPARE(viewport.radius(), 2000);
    QCOMPARE(viewport.mapCoversViewport(), true);
    QCOMPARE(viewport.focusPoint(), GeoDataCoordinates(0., 0., 0.));

    // invariants:
    QVERIFY(viewport.radius() > 0); // avoids divisions by zero
    QVERIFY(viewport.viewLatLonAltBox() == viewport.latLonAltBox(QRect(0, 0, 100, 100)));
    // FIXME QCOMPARE( viewport.viewLatLonAltBox().center().longitude(), viewport.centerLongitude() );
    // FIXME QCOMPARE( viewport.viewLatLonAltBox().center().latitude(), viewport.centerLatitude() );
}

void ViewportParamsTest::constructorValues_data()
{
    QTest::addColumn<Marble::Projection>("projection");
    QTest::addColumn<qreal>("lon");
    QTest::addColumn<qreal>("lat");
    QTest::addColumn<int>("radius");
    QTest::addColumn<QSize>("size");

    ViewportParams viewport;

    viewport.setProjection(Spherical);
    const AbstractProjection *const spherical = viewport.currentProjection();

    viewport.setProjection(Mercator);
    const AbstractProjection *const mercator = viewport.currentProjection();

    viewport.setProjection(Equirectangular);
    const AbstractProjection *const equirectangular = viewport.currentProjection();

    addRow() << Spherical << qreal(0) << qreal(0) << 2000 << QSize(100, 100);
    addRow() << Mercator << qreal(0) << qreal(0) << 2000 << QSize(100, 100);
    addRow() << Equirectangular << qreal(0) << qreal(0) << 2000 << QSize(100, 100);

    addRow() << Spherical << qreal(205 * DEG2RAD) << spherical->maxValidLat() + qreal(1.0) << 2000 << QSize(100, 100);
    addRow() << Mercator << qreal(205 * DEG2RAD) << mercator->maxValidLat() + qreal(1.0) << 2000 << QSize(100, 100);
    addRow() << Equirectangular << qreal(205 * DEG2RAD) << equirectangular->maxValidLat() + qreal(1.0) << 2000 << QSize(100, 100);
}

void ViewportParamsTest::constructorValues()
{
    QFETCH(Projection, projection);
    QFETCH(qreal, lon);
    QFETCH(qreal, lat);
    QFETCH(int, radius);
    QFETCH(QSize, size);

    const ViewportParams byConstructor(projection, lon, lat, radius, size);

    ViewportParams bySetters;
    bySetters.setProjection(projection);
    bySetters.centerOn(lon, lat);
    bySetters.setRadius(radius);
    bySetters.setSize(size);

    QCOMPARE(byConstructor.projection(), bySetters.projection());
    QCOMPARE(byConstructor.currentProjection(), bySetters.currentProjection());
    QCOMPARE(byConstructor.centerLongitude(), bySetters.centerLongitude());
    QCOMPARE(byConstructor.centerLatitude(), bySetters.centerLatitude());
    QCOMPARE(byConstructor.planetAxis(), bySetters.planetAxis());
    QCOMPARE(byConstructor.angularResolution(), bySetters.angularResolution());
    QCOMPARE(byConstructor.radius(), bySetters.radius());
    QCOMPARE(byConstructor.size(), bySetters.size());
}

void ViewportParamsTest::screenCoordinates_GeoDataLineString_data()
{
    QTest::addColumn<Marble::Projection>("projection");
    QTest::addColumn<Marble::TessellationFlags>("tessellation");
    QTest::addColumn<GeoDataLineString>("line");
    QTest::addColumn<int>("size");

    GeoDataCoordinates::Unit deg = GeoDataCoordinates::Degree;

    GeoDataLineString longitudeLine;
    longitudeLine << GeoDataCoordinates(185, 5, 0, deg) << GeoDataCoordinates(185, 15, 0, deg);

    GeoDataLineString diagonalLine;
    diagonalLine << GeoDataCoordinates(-185, 5, 0, deg) << GeoDataCoordinates(185, 15, 0, deg);

    GeoDataLineString latitudeLine;
    latitudeLine << GeoDataCoordinates(-185, 5, 0, deg) << GeoDataCoordinates(185, 5, 0, deg);

    Projection projection = Mercator;

    TessellationFlags flags = NoTessellation;
    QTest::newRow("Mercator NoTesselation Longitude") << projection << flags << longitudeLine << 2;

    QTest::newRow("Mercator NoTesselation Diagonal IDL") << projection << flags << diagonalLine << 2;

    QTest::newRow("Mercator NoTesselation Latitude IDL") << projection << flags << latitudeLine << 2;

    flags = Tessellate;
    QTest::newRow("Mercator Tesselate Longitude") << projection << flags << longitudeLine << 2;

    QTest::newRow("Mercator Tesselate Diagonal IDL") << projection << flags << diagonalLine << 2;

    QTest::newRow("Mercator Tesselate Latitude IDL") << projection << flags << latitudeLine << 2;

    flags = Tessellate | RespectLatitudeCircle;
    QTest::newRow("Mercator LatitudeCircle Longitude") << projection << flags << longitudeLine << 2;

    QTest::newRow("Mercator LatitudeCircle Diagonal IDL") << projection << flags << diagonalLine << 2;

    QTest::newRow("Mercator LatitudeCircle Latitude IDL") << projection << flags << latitudeLine << 2;

    projection = Equirectangular;

    flags = NoTessellation;
    QTest::newRow("Equirect NoTesselation Longitude") << projection << flags << longitudeLine << 2;

    QTest::newRow("Equirect NoTesselation Diagonal IDL") << projection << flags << diagonalLine << 2;

    QTest::newRow("Equirect NoTesselation Latitude IDL") << projection << flags << latitudeLine << 2;

    flags = Tessellate;
    QTest::newRow("Equirect Tesselate Longitude") << projection << flags << longitudeLine << 2;

    QTest::newRow("Equirect Tesselate Diagonal IDL") << projection << flags << diagonalLine << 2;

    QTest::newRow("Equirect Tesselate Latitude IDL") << projection << flags << latitudeLine << 2;

    flags = Tessellate | RespectLatitudeCircle;
    QTest::newRow("Equirect LatitudeCircle Longitude") << projection << flags << longitudeLine << 2;

    QTest::newRow("Equirect LatitudeCircle Diagonal IDL") << projection << flags << diagonalLine << 2;

    QTest::newRow("Equirect LatitudeCircle Latitude IDL") << projection << flags << latitudeLine << 2;

    projection = Spherical;

    flags = NoTessellation;
    QTest::newRow("Spherical NoTesselation Longitude") << projection << flags << longitudeLine << 1;

    QTest::newRow("Spherical NoTesselation Diagonal IDL") << projection << flags << diagonalLine << 1;

    QTest::newRow("Spherical NoTesselation Latitude IDL") << projection << flags << latitudeLine << 1;

    flags = Tessellate;
    QTest::newRow("Spherical Tesselate Longitude") << projection << flags << longitudeLine << 1;

    QTest::newRow("Spherical Tesselate Diagonal IDL") << projection << flags << diagonalLine << 1;

    QTest::newRow("Spherical Tesselate Latitude IDL") << projection << flags << latitudeLine << 1;

    flags = Tessellate | RespectLatitudeCircle;
    QTest::newRow("Spherical LatitudeCircle Longitude") << projection << flags << longitudeLine << 1;

    QTest::newRow("Spherical LatitudeCircle Diagonal IDL") << projection << flags << diagonalLine << 1;

    QTest::newRow("Spherical LatitudeCircle Latitude IDL") << projection << flags << latitudeLine << 1;
}

void ViewportParamsTest::screenCoordinates_GeoDataLineString()
{
    QFETCH(Marble::Projection, projection);
    QFETCH(Marble::TessellationFlags, tessellation);
    QFETCH(GeoDataLineString, line);
    QFETCH(int, size);

    ViewportParams viewport;
    viewport.setProjection(projection);
    viewport.setRadius(360 / 4); // for easy mapping of lon <-> x
    viewport.centerOn(185 * DEG2RAD, 0);

    line.setTessellationFlags(tessellation);
    QList<QPolygonF *> polys;
    viewport.screenCoordinates(line, polys);

    for (QPolygonF *poly : std::as_const(polys)) {
        // at least 2 points in one poly
        QVERIFY(poly->size() > 1);
        QPointF oldCoord = poly->first();
        poly->pop_front();

        for (const QPointF &coord : std::as_const(*poly)) {
            // no 2 same points
            QVERIFY((coord - oldCoord) != QPointF());

            // no 2 consecutive points should be more than 90° apart
            QVERIFY((coord - oldCoord).manhattanLength() < viewport.radius());
            oldCoord = coord;
        }
    }

    // check the provided number of polys
    QCOMPARE(polys.size(), size);
}

void ViewportParamsTest::screenCoordinates_GeoDataLineString2()
{
    const ViewportParams viewport(Spherical, 90 * DEG2RAD, 38 * DEG2RAD, 256, QSize(1165, 833));

    const GeoDataCoordinates coordinates(-90, 23.44, 0.0, GeoDataCoordinates::Degree);
    qreal x, y;
    bool globeHidesPoint;
    viewport.screenCoordinates(coordinates, x, y, globeHidesPoint);

    QCOMPARE(globeHidesPoint, true);

    GeoDataLineString line(Tessellate | RespectLatitudeCircle);
    line << GeoDataCoordinates(-180, 23.4400, 0.0, GeoDataCoordinates::Degree);
    line << GeoDataCoordinates(0, 23.4400, 0.0, GeoDataCoordinates::Degree);

    QList<QPolygonF *> polys;
    viewport.screenCoordinates(line, polys);

    QCOMPARE(polys.size(), 2);
}

void ViewportParamsTest::screenCoordinates_GeoDataLinearRing()
{
    // Creates a Rectangle on the eastern southern hemisphere
    // with the planet rotated so that only the western half
    // of the rectangle is visible. As a result only a single
    // screen polygon should be rendered.

    const ViewportParams viewport(Spherical, -15 * DEG2RAD, 0 * DEG2RAD, 350, QSize(1000, 750));

    GeoDataLinearRing line(Tessellate);
    GeoDataCoordinates coord1(30, -10, 0.0, GeoDataCoordinates::Degree);
    GeoDataCoordinates coord2(30, -45, 0.0, GeoDataCoordinates::Degree);
    GeoDataCoordinates coord3(100, -45, 0.0, GeoDataCoordinates::Degree);
    GeoDataCoordinates coord4(100, -10, 0.0, GeoDataCoordinates::Degree);

    qreal x, y;
    bool globeHidesPoint;
    viewport.screenCoordinates(coord1, x, y, globeHidesPoint);
    QCOMPARE(globeHidesPoint, false);
    viewport.screenCoordinates(coord2, x, y, globeHidesPoint);
    QCOMPARE(globeHidesPoint, false);
    viewport.screenCoordinates(coord3, x, y, globeHidesPoint);
    QCOMPARE(globeHidesPoint, true);
    viewport.screenCoordinates(coord4, x, y, globeHidesPoint);
    QCOMPARE(globeHidesPoint, true);

    line << coord1 << coord2 << coord3 << coord4;

    QList<QPolygonF *> polys;
    viewport.screenCoordinates(line, polys);

    QCOMPARE(polys.size(), 1);
}

void ViewportParamsTest::geoDataLinearRing_data()
{
    QTest::addColumn<Marble::Projection>("projection");
    QTest::addColumn<Marble::TessellationFlags>("tessellation");
    QTest::addColumn<GeoDataLinearRing>("ring");
    QTest::addColumn<int>("size");

    GeoDataCoordinates::Unit deg = GeoDataCoordinates::Degree;

    GeoDataLinearRing normalRing;
    normalRing << GeoDataCoordinates(175, 5, 0, deg) << GeoDataCoordinates(175, 15, 0, deg) << GeoDataCoordinates(170, 15, 0, deg);

    GeoDataLinearRing acrossIDLRing;
    acrossIDLRing << GeoDataCoordinates(-175, 5, 0, deg) << GeoDataCoordinates(175, 5, 0, deg) << GeoDataCoordinates(175, 15, 0, deg);

    GeoDataLinearRing aroundSPoleRing;
    aroundSPoleRing << GeoDataCoordinates(-175, -65, 0, deg) << GeoDataCoordinates(-55, -70, 0, deg) << GeoDataCoordinates(65, -75, 0, deg);

    Projection projection = Mercator;

    TessellationFlags flags = NoTessellation;
    QTest::newRow("Mercator NoTesselation normalRing") << projection << flags << normalRing << 2;

    QTest::newRow("Mercator NoTesselation acrossIDLRing") << projection << flags << acrossIDLRing << 2;

#ifdef BUG_357540_IS_FIXED
    QTest::newRow("Mercator NoTesselation aroundSPoleRing") << projection << flags << aroundSPoleRing << 2;
#endif

    flags = Tessellate;
    QTest::newRow("Mercator Tesselate normalRing") << projection << flags << normalRing << 2;

    QTest::newRow("Mercator Tesselate acrossIDLRing") << projection << flags << acrossIDLRing << 2;

#ifdef BUG_357540_IS_FIXED
    QTest::newRow("Mercator Tesselate aroundSPoleRing") << projection << flags << aroundSPoleRing << 2;
#endif

    flags = Tessellate | RespectLatitudeCircle;
    QTest::newRow("Mercator LatitudeCircle normalRing") << projection << flags << normalRing << 2;

    QTest::newRow("Mercator LatitudeCircle acrossIDLRing") << projection << flags << acrossIDLRing << 2;

#ifdef BUG_357540_IS_FIXED
    QTest::newRow("Mercator LatitudeCircle aroundSPoleRing") << projection << flags << aroundSPoleRing << 2;
#endif

    projection = Equirectangular;

    flags = NoTessellation;
    QTest::newRow("Equirect NoTesselation normalRing") << projection << flags << normalRing << 2;

    QTest::newRow("Equirect NoTesselation acrossIDLRing") << projection << flags << acrossIDLRing << 2;

#ifdef BUG_357540_IS_FIXED
    QTest::newRow("Equirect NoTesselation aroundSPoleRing") << projection << flags << aroundSPoleRing << 2;
#endif

    flags = Tessellate;
    QTest::newRow("Equirect Tesselate normalRing") << projection << flags << normalRing << 2;

    QTest::newRow("Equirect Tesselate acrossIDLRing") << projection << flags << acrossIDLRing << 2;

#ifdef BUG_357540_IS_FIXED
    QTest::newRow("Equirect Tesselate aroundSPoleRing") << projection << flags << aroundSPoleRing << 2;
#endif

    flags = Tessellate | RespectLatitudeCircle;
    QTest::newRow("Equirect LatitudeCircle normalRing") << projection << flags << normalRing << 2;

    QTest::newRow("Equirect LatitudeCircle acrossIDLRing") << projection << flags << acrossIDLRing << 2;

#ifdef BUG_357540_IS_FIXED
    QTest::newRow("Equirect LatitudeCircle aroundSPoleRing") << projection << flags << aroundSPoleRing << 2;
#endif

    projection = Spherical;

    flags = NoTessellation;
    QTest::newRow("Spherical NoTesselation normalRing") << projection << flags << normalRing << 1;

    QTest::newRow("Spherical NoTesselation acrossIDLRing") << projection << flags << acrossIDLRing << 1;

    QTest::newRow("Spherical NoTesselation aroundSPoleRing") << projection << flags << aroundSPoleRing << 1;

    flags = Tessellate;
    QTest::newRow("Spherical Tesselate normalRing") << projection << flags << normalRing << 1;

    QTest::newRow("Spherical Tesselate acrossIDLRing") << projection << flags << acrossIDLRing << 1;

    /*    QTest::newRow("Spherical Tesselate aroundSPoleRing")
                << projection << flags << aroundSPoleRing << 1;*/

    flags = Tessellate | RespectLatitudeCircle;
    QTest::newRow("Spherical LatitudeCircle normalRing") << projection << flags << normalRing << 1;

    QTest::newRow("Spherical LatitudeCircle acrossIDLRing") << projection << flags << acrossIDLRing << 1;

    /*    QTest::newRow("Spherical LatitudeCircle aroundSPoleRing")
                << projection << flags << aroundSPoleRing << 1;*/
}

void ViewportParamsTest::geoDataLinearRing()
{
    QFETCH(Marble::Projection, projection);
    QFETCH(Marble::TessellationFlags, tessellation);
    QFETCH(GeoDataLinearRing, ring);
    QFETCH(int, size);

    ViewportParams viewport;
    viewport.setProjection(projection);
    viewport.setRadius(360 / 4); // for easy mapping of lon <-> x
    viewport.centerOn(175 * DEG2RAD, 0);

    ring.setTessellationFlags(tessellation);
    QList<QPolygonF *> polys;
    viewport.screenCoordinates(ring, polys);

    for (QPolygonF *poly : std::as_const(polys)) {
        // at least 3 points in one poly
        QVERIFY(poly->size() > 2);
        QPointF oldCoord = poly->first();
        // polygon comes back to same point
        QVERIFY(poly->isClosed());
        poly->pop_front();

        for (const QPointF &coord : std::as_const(*poly)) {
            // no 2 same points
            QVERIFY((coord - oldCoord) != QPointF());

            // no 2 consecutive points should be more than 90° apart
            //            QVERIFY( (coord-oldCoord).manhattanLength() < viewport.radius() );
            oldCoord = coord;
        }
    }

    // check the provided number of polys
    QCOMPARE(polys.size(), size);
}

void ViewportParamsTest::setInvalidRadius()
{
    ViewportParams viewport;

    // QVERIFY( viewport.radius() > 0 ); already verified above

    const int radius = viewport.radius();
    viewport.setRadius(0);

    QCOMPARE(viewport.radius(), radius);
}

void ViewportParamsTest::setFocusPoint()
{
    const GeoDataCoordinates focusPoint1(10, 13, 0, GeoDataCoordinates::Degree);
    const GeoDataCoordinates focusPoint2(14.3, 20.5, 0, GeoDataCoordinates::Degree);

    ViewportParams viewport;

    const GeoDataCoordinates center = viewport.focusPoint();

    QVERIFY(center != focusPoint1);
    QVERIFY(center != focusPoint2);

    viewport.setFocusPoint(focusPoint1);
    QCOMPARE(viewport.focusPoint(), focusPoint1);

    viewport.resetFocusPoint();
    QCOMPARE(viewport.focusPoint(), center);

    viewport.setFocusPoint(focusPoint2);
    QCOMPARE(viewport.focusPoint(), focusPoint2);

    viewport.setFocusPoint(focusPoint1);
    QCOMPARE(viewport.focusPoint(), focusPoint1);

    viewport.resetFocusPoint();
    QCOMPARE(viewport.focusPoint(), center);
}

}

QTEST_MAIN(Marble::ViewportParamsTest)

#include "ViewportParamsTest.moc"