data/MapClusterer.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "MapClusterer.h" | ||
| 2 | |||
| 3 | #include "MapProjection.h" | ||
| 4 | |||
| 5 | #include <QHash> | ||
| 6 | #include <algorithm> | ||
| 7 | #include <cmath> | ||
| 8 | |||
| 9 | namespace MapClusterer { | ||
| 10 | |||
| 11 | 117 | QList<Cluster> cluster(const QList<Place> &places, double zoom, double cellPixels) { | |
| 12 | 117 | const double cell = cellPixels / (MapProjection::TileSize * std::exp2(zoom)); | |
| 13 | 117 | QHash<QPair<qint64, qint64>, QList<int>> cells; | |
| 14 | 117 | QList<QPair<qint64, qint64>> order; | |
| 15 |
2/2✓ Branch 14 → 3 taken 23281 times.
✓ Branch 14 → 15 taken 117 times.
|
23398 | for (int i = 0; i < places.size(); ++i) { |
| 16 | 23281 | const QPointF w = places.at(i).world; | |
| 17 | 23281 | const QPair<qint64, qint64> key{qint64(std::floor(w.x() / cell)), qint64(std::floor(w.y() / cell))}; | |
| 18 |
2/2✓ Branch 8 → 9 taken 3341 times.
✓ Branch 8 → 10 taken 19940 times.
|
23281 | if (!cells.contains(key)) |
| 19 |
1/1✓ Branch 9 → 10 taken 3341 times.
|
3341 | order.append(key); |
| 20 |
2/2✓ Branch 10 → 11 taken 23281 times.
✓ Branch 11 → 12 taken 23281 times.
|
23281 | cells[key].append(i); |
| 21 | } | ||
| 22 | 117 | QList<Cluster> clusters; | |
| 23 |
1/1✓ Branch 16 → 17 taken 117 times.
|
117 | clusters.reserve(order.size()); |
| 24 |
4/4✓ Branch 17 → 18 taken 117 times.
✓ Branch 18 → 19 taken 117 times.
✓ Branch 58 → 20 taken 3341 times.
✓ Branch 58 → 59 taken 117 times.
|
3458 | for (const auto &key : order) { |
| 25 | 3341 | const QList<int> indices = cells.value(key); | |
| 26 | 3341 | Cluster c; | |
| 27 | 3341 | double sx = 0; | |
| 28 | 3341 | double sy = 0; | |
| 29 | 3341 | double minX = 2, minY = 2, maxX = -1, maxY = -1; | |
| 30 |
2/2✓ Branch 39 → 24 taken 23281 times.
✓ Branch 39 → 40 taken 3341 times.
|
26622 | for (int index : indices) { |
| 31 | 23281 | const Place &p = places.at(index); | |
| 32 |
1/1✓ Branch 26 → 27 taken 23281 times.
|
23281 | c.members.append(p.id); |
| 33 | 23281 | sx += p.world.x(); | |
| 34 | 23281 | sy += p.world.y(); | |
| 35 | 23281 | minX = std::min(minX, p.world.x()); | |
| 36 | 23281 | maxX = std::max(maxX, p.world.x()); | |
| 37 | 23281 | minY = std::min(minY, p.world.y()); | |
| 38 | 23281 | maxY = std::max(maxY, p.world.y()); | |
| 39 | } | ||
| 40 |
1/1✓ Branch 40 → 41 taken 3341 times.
|
3341 | c.members.sort(); |
| 41 |
1/1✓ Branch 41 → 42 taken 3341 times.
|
3341 | c.id = c.members.first(); |
| 42 | 3341 | c.world = {sx / indices.size(), sy / indices.size()}; | |
| 43 | 3341 | c.bounds = QRectF(QPointF(minX, minY), QPointF(maxX, maxY)); | |
| 44 |
3/4✓ Branch 49 → 50 taken 2220 times.
✓ Branch 49 → 52 taken 1121 times.
✓ Branch 50 → 51 taken 2220 times.
✗ Branch 50 → 52 not taken.
|
3341 | c.sameSpot = (maxX - minX) < 1e-7 && (maxY - minY) < 1e-7; |
| 45 |
1/1✓ Branch 53 → 54 taken 3341 times.
|
3341 | clusters.append(c); |
| 46 | 3341 | } | |
| 47 |
3/3✓ Branch 59 → 60 taken 117 times.
✓ Branch 60 → 61 taken 117 times.
✓ Branch 61 → 62 taken 117 times.
|
117 | std::sort(clusters.begin(), clusters.end(), [](const Cluster &a, const Cluster &b) { |
| 48 |
1/2✓ Branch 4 → 5 taken 38257 times.
✗ Branch 4 → 8 not taken.
|
38257 | if (a.world.x() != b.world.x()) |
| 49 | 38257 | return a.world.x() < b.world.x(); | |
| 50 | ✗ | if (a.world.y() != b.world.y()) | |
| 51 | ✗ | return a.world.y() < b.world.y(); | |
| 52 | ✗ | return a.id < b.id; | |
| 53 | }); | ||
| 54 | 117 | return clusters; | |
| 55 | 117 | } | |
| 56 | |||
| 57 | } // namespace MapClusterer | ||
| 58 |