Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit 70fbc97

Browse files
committed
[test] Distance expression: Update unit tests
1 parent 0fa4850 commit 70fbc97

File tree

5 files changed

+456
-46
lines changed

5 files changed

+456
-46
lines changed

src/mbgl/style/expression/distance.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ struct Comparator {
270270
// The priority queue will ensure the top element would always be the pair that has the biggest distance
271271
using DistQueue = std::priority_queue<DistPair, std::deque<DistPair>, Comparator>;
272272

273+
// Divide and conqure, the time complexity is O(n*lgn), faster than Brute force O(n*n)
274+
// Most of the time, use index for in-place processing.
275+
273276
double lineToPolygonDistance(const mapbox::geometry::line_string<double>& line,
274277
const mapbox::geometry::polygon<double>& polygon,
275278
mapbox::cheap_ruler::CheapRuler& ruler,
@@ -278,6 +281,7 @@ double lineToPolygonDistance(const mapbox::geometry::line_string<double>& line,
278281
DistQueue distQueue;
279282
distQueue.push(std::forward_as_tuple(0, IndexRange(0, line.size() - 1), IndexRange(0, 0)));
280283

284+
const auto polyBBox = getBBox(polygon);
281285
while (!distQueue.empty()) {
282286
auto distPair = distQueue.top();
283287
distQueue.pop();
@@ -293,9 +297,9 @@ double lineToPolygonDistance(const mapbox::geometry::line_string<double>& line,
293297
} else {
294298
auto newRangesA = splitRange(range, true /*isLine*/);
295299
const auto updateQueue =
296-
[&distQueue, &miniDist, &ruler, &line, &polygon](mbgl::optional<IndexRange>& rangeA) {
300+
[&distQueue, &miniDist, &ruler, &line, &polyBBox](mbgl::optional<IndexRange>& rangeA) {
297301
if (!rangeA) return;
298-
auto tempDist = bboxToBBoxDistance(getBBox(line, *rangeA), getBBox(polygon), ruler);
302+
auto tempDist = bboxToBBoxDistance(getBBox(line, *rangeA), polyBBox, ruler);
299303
// Insert new pair to the queue if the bbox distance is less or equal to miniDist,
300304
// The pair with biggest distance will be at the top
301305
if (tempDist <= miniDist)
@@ -316,6 +320,7 @@ double pointsToPolygonDistance(const mapbox::geometry::multi_point<double>& poin
316320
DistQueue distQueue;
317321
distQueue.push(std::forward_as_tuple(0, IndexRange(0, points.size() - 1), IndexRange(0, 0)));
318322

323+
const auto polyBBox = getBBox(polygon);
319324
while (!distQueue.empty()) {
320325
auto distPair = distQueue.top();
321326
distQueue.pop();
@@ -330,11 +335,11 @@ double pointsToPolygonDistance(const mapbox::geometry::multi_point<double>& poin
330335
if (miniDist == 0.0) return 0.0;
331336
}
332337
} else {
333-
auto newRangesA = splitRange(range, true /*isLine*/);
338+
auto newRangesA = splitRange(range, false /*isLine*/);
334339
const auto updateQueue =
335-
[&distQueue, &miniDist, &ruler, &points, &polygon](mbgl::optional<IndexRange>& rangeA) {
340+
[&distQueue, &miniDist, &ruler, &points, &polyBBox](mbgl::optional<IndexRange>& rangeA) {
336341
if (!rangeA) return;
337-
auto tempDist = bboxToBBoxDistance(getBBox(points, *rangeA), getBBox(polygon), ruler);
342+
auto tempDist = bboxToBBoxDistance(getBBox(points, *rangeA), polyBBox, ruler);
338343
// Insert new pair to the queue if the bbox distance is less or equal to miniDist,
339344
// The pair with biggest distance will be at the top
340345
if (tempDist <= miniDist)
@@ -347,8 +352,6 @@ double pointsToPolygonDistance(const mapbox::geometry::multi_point<double>& poin
347352
return miniDist;
348353
}
349354

350-
// Divide and conqure, the time complexity is O(n*lgn), faster than Brute force O(n*n)
351-
// Use index for in-place processing.
352355
double lineToLineDistance(const mapbox::geometry::line_string<double>& line1,
353356
const mapbox::geometry::multi_point<double>& line2,
354357
mapbox::cheap_ruler::CheapRuler& ruler,
@@ -391,8 +394,6 @@ double lineToLineDistance(const mapbox::geometry::line_string<double>& line1,
391394
return miniDist;
392395
}
393396

394-
// Divide and conqure, the time complexity is O(n*lgn), faster than Brute force O(n*n)
395-
// Use index for in-place processing.
396397
double pointsToPointsDistance(const mapbox::geometry::multi_point<double>& pointSet1,
397398
const mapbox::geometry::multi_point<double>& pointSet2,
398399
mapbox::cheap_ruler::CheapRuler& ruler) {
@@ -436,8 +437,6 @@ double pointsToPointsDistance(const mapbox::geometry::multi_point<double>& point
436437
return miniDist;
437438
}
438439

439-
// Divide and conqure, the time complexity is O(n*lgn), faster than Brute force O(n*n)
440-
// Most of the time, use index for in-place processing.
441440
double pointsToLineDistance(const mapbox::geometry::multi_point<double>& points,
442441
const mapbox::geometry::line_string<double>& line,
443442
mapbox::cheap_ruler::CheapRuler& ruler) {
@@ -653,7 +652,7 @@ double calculateDistance(const GeometryTileFeature& feature,
653652

654653
optional<GeoJSON> parseValue(const style::conversion::Convertible& value, style::expression::ParsingContext& ctx) {
655654
if (isArray(value)) {
656-
// object value, quoted with ["Distance", GeoJSONObj]
655+
// object value, quoted with ["distance", GeoJSONObj]
657656
auto length = arrayLength(value);
658657
if (length != 2) {
659658
ctx.error("'distance' expression requires one argument, but found " +
@@ -672,7 +671,7 @@ optional<GeoJSON> parseValue(const style::conversion::Convertible& value, style:
672671
ctx.error(error.message);
673672
}
674673
}
675-
ctx.error("'distance' expression needs to be an array with format [\"Distance\", GeoJSONObj].");
674+
ctx.error("'distance' expression needs to be an array with format [\"distance\", GeoJSONObj].");
676675
return nullopt;
677676
}
678677

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"type": "Feature",
3+
"properties": {
4+
"marker-color": "#e0d96a"
5+
},
6+
"geometry": {
7+
"type": "MultiPoint",
8+
"coordinates": [
9+
[
10+
24.941325187683105,
11+
60.169503396855
12+
],
13+
[
14+
24.943127632141113,
15+
60.16984495711831
16+
],
17+
[
18+
24.94493007659912,
19+
60.17101904344053
20+
],
21+
[
22+
24.947762489318848,
23+
60.16753935642882
24+
],
25+
[
26+
24.9479341506958,
27+
60.16907644153227
28+
],
29+
[
30+
24.946002960205078,
31+
60.16901239775532
32+
],
33+
[
34+
24.944028854370117,
35+
60.16824386269413
36+
],
37+
[
38+
24.9459171295166,
39+
60.16756070532545
40+
],
41+
[
42+
24.940338134765625,
43+
60.168286559558055
44+
],
45+
[
46+
24.94248390197754,
47+
60.1678168910033
48+
],
49+
[
50+
24.944286346435547,
51+
60.16696293097573
52+
],
53+
[
54+
24.946260452270508,
55+
60.16619434797435
56+
],
57+
[
58+
24.941411018371582,
59+
60.16619434797435
60+
],
61+
[
62+
24.943749904632565,
63+
60.16782756536319
64+
],
65+
[
66+
24.942612648010254,
67+
60.16908711548297
68+
],
69+
[
70+
24.944608211517334,
71+
60.16952474447549
72+
]
73+
]
74+
}
75+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
{
2+
"type": "FeatureCollection",
3+
"features": [
4+
{
5+
"type": "Feature",
6+
"properties": {
7+
"stroke": "#e0d96a",
8+
"stroke-width": 2,
9+
"stroke-opacity": 1,
10+
"fill": "#c6ec00",
11+
"fill-opacity": 0.5
12+
},
13+
"geometry": {
14+
"type": "MultiPolygon",
15+
"coordinates": [
16+
[
17+
[
18+
[
19+
24.93870735168457,
20+
60.16429416283512
21+
],
22+
[
23+
24.936389923095703,
24+
60.16205223008669
25+
],
26+
[
27+
24.940123558044434,
28+
60.16250062887229
29+
],
30+
[
31+
24.943857192993164,
32+
60.16333335324268
33+
],
34+
[
35+
24.943127632141113,
36+
60.16465712803696
37+
],
38+
[
39+
24.94123935699463,
40+
60.16595949958244
41+
],
42+
[
43+
24.936561584472656,
44+
60.16561789892518
45+
],
46+
[
47+
24.93450164794922,
48+
60.16435821580911
49+
],
50+
[
51+
24.935274124145508,
52+
60.163568220403924
53+
],
54+
[
55+
24.93870735168457,
56+
60.16429416283512
57+
]
58+
]
59+
],
60+
[
61+
[
62+
[
63+
24.95166778564453,
64+
60.174306261926034
65+
],
66+
[
67+
24.949607849121094,
68+
60.172918839697616
69+
],
70+
[
71+
24.952783584594727,
72+
60.17144597351834
73+
],
74+
[
75+
24.95595932006836,
76+
60.17217174191747
77+
],
78+
[
79+
24.956517219543457,
80+
60.1742635728851
81+
],
82+
[
83+
24.954586029052734,
84+
60.175736312737385
85+
],
86+
[
87+
24.950637817382812,
88+
60.175245406790246
89+
],
90+
[
91+
24.9514102935791,
92+
60.174348950911465
93+
],
94+
[
95+
24.95166778564453,
96+
60.174306261926034
97+
]
98+
]
99+
],
100+
[
101+
[
102+
[
103+
24.930424690246582,
104+
60.17413550542946
105+
],
106+
[
107+
24.92875099182129,
108+
60.171830205844614
109+
],
110+
[
111+
24.936904907226562,
112+
60.1703145966823
113+
],
114+
[
115+
24.937891960144043,
116+
60.172150396016946
117+
],
118+
[
119+
24.937891960144043,
120+
60.173922058560485
121+
],
122+
[
123+
24.930424690246582,
124+
60.17413550542946
125+
]
126+
]
127+
]
128+
]
129+
}
130+
}
131+
]
132+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"type": "Feature",
3+
"properties": {},
4+
"geometry": {
5+
"type": "MultiPolygon",
6+
"coordinates": [
7+
[
8+
[
9+
[
10+
24.94171142578125,
11+
60.16777419352904
12+
],
13+
[
14+
24.948577880859375,
15+
60.16777419352904
16+
],
17+
[
18+
24.948577880859375,
19+
60.170122472217564
20+
],
21+
[
22+
24.94171142578125,
23+
60.170122472217564
24+
],
25+
[
26+
24.94171142578125,
27+
60.16777419352904
28+
]
29+
]
30+
],
31+
[
32+
[
33+
[
34+
24.951581954956055,
35+
60.169503396855
36+
],
37+
[
38+
24.955186843872067,
39+
60.169503396855
40+
],
41+
[
42+
24.955186843872067,
43+
60.17052806699205
44+
],
45+
[
46+
24.951581954956055,
47+
60.17052806699205
48+
],
49+
[
50+
24.951581954956055,
51+
60.169503396855
52+
]
53+
]
54+
],
55+
[
56+
[
57+
[
58+
24.944243431091305,
59+
60.16301307713581
60+
],
61+
[
62+
24.94716167449951,
63+
60.16301307713581
64+
],
65+
[
66+
24.94716167449951,
67+
60.16412335429406
68+
],
69+
[
70+
24.944243431091305,
71+
60.16412335429406
72+
],
73+
[
74+
24.944243431091305,
75+
60.16301307713581
76+
]
77+
]
78+
]
79+
]
80+
}
81+
}

0 commit comments

Comments
 (0)