presentation/MapTileRenderer.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "MapTileRenderer.h" | ||
| 2 | |||
| 3 | #include "data/GeoData.h" | ||
| 4 | #include "data/MapProjection.h" | ||
| 5 | |||
| 6 | #include <QElapsedTimer> | ||
| 7 | #include <QLoggingCategory> | ||
| 8 | #include <QPainter> | ||
| 9 | #include <QPainterPath> | ||
| 10 | #include <cmath> | ||
| 11 | #include <mutex> | ||
| 12 | |||
| 13 |
4/7✓ Branch 2 → 3 taken 5 times.
✓ Branch 2 → 8 taken 425 times.
✓ Branch 4 → 5 taken 5 times.
✗ Branch 4 → 8 not taken.
✓ Branch 5 → 6 taken 5 times.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
|
430 | Q_LOGGING_CATEGORY(lcMapTiles, "mailjd.contacts.map") |
| 14 | |||
| 15 | namespace MapTileRenderer { | ||
| 16 | |||
| 17 | 2 | std::shared_ptr<const World> World::from(const GeoData &data) { | |
| 18 |
1/1✓ Branch 2 → 3 taken 2 times.
|
2 | auto world = std::make_shared<World>(); |
| 19 | 2 | const QList<GeoData::Ring> &rings = data.rings(); | |
| 20 |
2/2✓ Branch 48 → 7 taken 484 times.
✓ Branch 48 → 49 taken 2 times.
|
486 | for (const GeoData::Country &country : data.countries()) { |
| 21 | 484 | Shape shape; | |
| 22 |
5/6✓ Branch 35 → 36 taken 3202 times.
✓ Branch 35 → 39 taken 484 times.
✓ Branch 37 → 38 taken 3202 times.
✗ Branch 37 → 39 not taken.
✓ Branch 40 → 9 taken 3202 times.
✓ Branch 40 → 41 taken 484 times.
|
3686 | for (quint32 r = country.firstRing; r < country.firstRing + country.ringCount && r < quint32(rings.size()); ++r) { |
| 23 | 3202 | const GeoData::Ring &ring = rings.at(int(r)); | |
| 24 | 3202 | QPolygonF polygon; | |
| 25 |
1/1✓ Branch 10 → 11 taken 3202 times.
|
3202 | polygon.reserve(int(ring.pointCount)); |
| 26 |
2/2✓ Branch 16 → 12 taken 104186 times.
✓ Branch 16 → 17 taken 3202 times.
|
107388 | for (quint32 p = ring.firstPoint; p < ring.firstPoint + ring.pointCount; ++p) { |
| 27 |
1/1✓ Branch 12 → 13 taken 104186 times.
|
104186 | const GeoData::Point point = data.point(int(p)); |
| 28 |
2/2✓ Branch 13 → 14 taken 104186 times.
✓ Branch 14 → 15 taken 104186 times.
|
104186 | polygon.append(MapProjection::project(double(point.lat) / GeoData::Scale, double(point.lon) / GeoData::Scale)); |
| 29 | } | ||
| 30 |
1/2✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 3202 times.
|
3202 | if (polygon.size() < 3) |
| 31 | ✗ | continue; | |
| 32 |
4/4✓ Branch 21 → 22 taken 478 times.
✓ Branch 21 → 23 taken 2724 times.
✓ Branch 22 → 25 taken 478 times.
✓ Branch 23 → 24 taken 2724 times.
|
3202 | shape.bounds = shape.bounds.isNull() ? polygon.boundingRect() : shape.bounds.united(polygon.boundingRect()); |
| 33 | 3202 | world->pointCount += int(polygon.size()); | |
| 34 |
1/1✓ Branch 27 → 28 taken 3202 times.
|
3202 | shape.rings.append(polygon); |
| 35 | 3202 | } | |
| 36 |
2/2✓ Branch 42 → 43 taken 478 times.
✓ Branch 42 → 45 taken 6 times.
|
484 | if (!shape.rings.isEmpty()) |
| 37 |
1/1✓ Branch 44 → 45 taken 478 times.
|
478 | world->shapes.append(shape); |
| 38 | 484 | } | |
| 39 | 4 | return world; | |
| 40 | 2 | } | |
| 41 | |||
| 42 | 375 | std::shared_ptr<const World> World::builtIn() { | |
| 43 | static std::once_flag once; | ||
| 44 |
3/4✓ Branch 2 → 3 taken 3 times.
✓ Branch 2 → 7 taken 372 times.
✓ Branch 4 → 5 taken 3 times.
✗ Branch 4 → 7 not taken.
|
375 | static std::shared_ptr<const World> world; |
| 45 |
1/1✓ Branch 7 → 8 taken 375 times.
|
375 | std::call_once(once, [] { |
| 46 | 3 | QElapsedTimer timer; | |
| 47 | 3 | timer.start(); | |
| 48 |
3/3✓ Branch 3 → 4 taken 3 times.
✓ Branch 5 → 6 taken 2 times.
✓ Branch 5 → 11 taken 1 time.
|
3 | if (const std::shared_ptr<const GeoData> data = GeoData::builtIn()) |
| 49 |
1/1✓ Branch 7 → 8 taken 2 times.
|
3 | world = from(*data); |
| 50 |
9/9✓ Branch 12 → 13 taken 3 times.
✓ Branch 16 → 17 taken 3 times.
✓ Branch 17 → 18 taken 3 times.
✓ Branch 19 → 20 taken 2 times.
✓ Branch 19 → 22 taken 1 time.
✓ Branch 23 → 24 taken 3 times.
✓ Branch 24 → 25 taken 3 times.
✓ Branch 31 → 14 taken 3 times.
✓ Branch 31 → 32 taken 3 times.
|
6 | qCInfo(lcMapTiles) << "Map outlines projected:" << (world ? world->pointCount : 0) << "points in" |
| 51 |
2/2✓ Branch 26 → 27 taken 3 times.
✓ Branch 27 → 28 taken 3 times.
|
3 | << timer.elapsed() << "ms"; |
| 52 | 3 | }); | |
| 53 | 375 | return world; | |
| 54 | } | ||
| 55 | |||
| 56 | 293 | QImage render(const World &world, int zoom, int x, int y, const Style &style, int pixels) { | |
| 57 |
1/1✓ Branch 2 → 3 taken 293 times.
|
293 | QImage image(pixels, pixels, QImage::Format_ARGB32_Premultiplied); |
| 58 |
1/1✓ Branch 3 → 4 taken 293 times.
|
293 | image.fill(style.water); |
| 59 |
1/1✓ Branch 4 → 5 taken 293 times.
|
293 | const int count = 1 << qBound(0, zoom, 30); |
| 60 | 293 | x = ((x % count) + count) % count; | |
| 61 |
3/4✓ Branch 5 → 6 taken 293 times.
✗ Branch 5 → 7 not taken.
✓ Branch 6 → 7 taken 1 time.
✓ Branch 6 → 8 taken 292 times.
|
293 | if (y < 0 || y >= count) |
| 62 | 1 | return image; | |
| 63 | 292 | const double scale = double(pixels) * count; // world → tile pixels | |
| 64 | 292 | const QRectF tile(double(x) / count, double(y) / count, 1.0 / count, 1.0 / count); | |
| 65 | // Half a pixel in world units: closer points add nothing visible. | ||
| 66 | 292 | const double minStep = 0.5 / scale; | |
| 67 | 292 | QPainterPath land; | |
| 68 |
1/1✓ Branch 10 → 11 taken 292 times.
|
292 | land.setFillRule(Qt::WindingFill); |
| 69 |
2/2✓ Branch 56 → 13 taken 69788 times.
✓ Branch 56 → 57 taken 292 times.
|
70080 | for (const World::Shape &shape : world.shapes) { |
| 70 |
2/2✓ Branch 15 → 16 taken 62588 times.
✓ Branch 15 → 17 taken 7200 times.
|
69788 | if (!shape.bounds.intersects(tile)) |
| 71 | 62588 | continue; | |
| 72 |
2/2✓ Branch 52 → 19 taken 91908 times.
✓ Branch 52 → 53 taken 7200 times.
|
99108 | for (const QPolygonF &ring : shape.rings) { |
| 73 | 91908 | QPointF last = ring.first(); | |
| 74 |
1/1✓ Branch 24 → 25 taken 91908 times.
|
91908 | land.moveTo((last - tile.topLeft()) * scale); |
| 75 |
2/2✓ Branch 48 → 26 taken 2861193 times.
✓ Branch 48 → 49 taken 91908 times.
|
2953101 | for (int i = 1; i < ring.size(); ++i) { |
| 76 | 2861193 | const QPointF &point = ring.at(i); | |
| 77 |
8/8✓ Branch 30 → 31 taken 1508125 times.
✓ Branch 30 → 38 taken 1353068 times.
✓ Branch 34 → 35 taken 979830 times.
✓ Branch 34 → 38 taken 528295 times.
✓ Branch 36 → 37 taken 940386 times.
✓ Branch 36 → 38 taken 39444 times.
✓ Branch 39 → 40 taken 940386 times.
✓ Branch 39 → 41 taken 1920807 times.
|
2861193 | if (std::abs(point.x() - last.x()) < minStep && std::abs(point.y() - last.y()) < minStep && i + 1 < ring.size()) |
| 78 | 940386 | continue; | |
| 79 |
1/1✓ Branch 44 → 45 taken 1920807 times.
|
1920807 | land.lineTo((point - tile.topLeft()) * scale); |
| 80 | 1920807 | last = point; | |
| 81 | } | ||
| 82 |
1/1✓ Branch 49 → 50 taken 91908 times.
|
91908 | land.closeSubpath(); |
| 83 | } | ||
| 84 | } | ||
| 85 |
1/1✓ Branch 57 → 58 taken 292 times.
|
292 | QPainter painter(&image); |
| 86 |
1/1✓ Branch 58 → 59 taken 292 times.
|
292 | painter.setRenderHint(QPainter::Antialiasing); |
| 87 |
5/5✓ Branch 60 → 61 taken 95 times.
✓ Branch 60 → 62 taken 197 times.
✓ Branch 63 → 64 taken 292 times.
✓ Branch 64 → 65 taken 292 times.
✓ Branch 65 → 66 taken 292 times.
|
292 | painter.setPen(QPen(style.border, qMax(1.0, pixels / 256.0) * (zoom >= 4 ? 0.8 : 0.5))); |
| 88 |
2/2✓ Branch 68 → 69 taken 292 times.
✓ Branch 69 → 70 taken 292 times.
|
292 | painter.setBrush(style.land); |
| 89 |
1/1✓ Branch 71 → 72 taken 292 times.
|
292 | painter.drawPath(land); |
| 90 | 292 | return image; | |
| 91 | 292 | } | |
| 92 | |||
| 93 | } // namespace MapTileRenderer | ||
| 94 |