@@ -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
271271using 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+
273276double 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.
352355double 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.
396397double 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.
441440double 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
654653optional<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
0 commit comments