Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Floating-point precision fix in meili::Project() fixes map-match issue #2946

Merged
merged 15 commits into from
Mar 18, 2021
Prev Previous commit
Next Next commit
Scoped the encoded shape to the unit test. Moved the unit test out of…
… gurka and into

valhalla/tests as geometry.cc. Changed meili::Project() to use doubles as input/output
and within. Consequently, from using doubles, an existing map-match unit test started
failing - on my newly added "throw std::logic_error()" in cut_segments(). A quick
examination revealed that two projection percentage_along's were within 1e-10 of one
another but in the unexpected order. I added some tolerance into cut_segments() to
address the issue. I also modified the unit test to use decode7() and length()
instead of my homegrown stuff.
  • Loading branch information
ktatterso committed Mar 18, 2021
commit d08f5bbb3fa44901b363661199fabb77f7215d59
2 changes: 1 addition & 1 deletion src/meili/geometry_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Project(const projector_t& p, Shape7Decoder<midgard::PointLL>& shape, double sna
PointLL first_point(shape.pop());
auto closest_point = first_point;
auto closest_segment_point = first_point;
double closest_distance = p.approx.DistanceSquared(closest_segment_point);
double closest_distance = 1e100;
ktatterso marked this conversation as resolved.
Show resolved Hide resolved
size_t closest_segment = 0;
double closest_partial_length = 0.0;
double total_length = 0.0;
Expand Down
42 changes: 22 additions & 20 deletions test/geometry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,13 @@
#include "midgard/polyline2.h"
#include <gtest/gtest.h>

// Foundational geometric unit tests.

using namespace valhalla;
using namespace valhalla::midgard;

namespace {

// Some real world encoded shape data.
const std::string fwd_enc_shape =
{-24, -109, -78, 36, -90, -4, -46, 12, 33, 118, 71, 16, 117, 47, 41, 91, 113,
44, 45, 112, -125, 1, 80, -67, 1, 54, -1, 1, 117, -31, 2, -43, 1, -33,
1, 21, -93, 2, 15, -25, 1, 47, -97, 1, 86, -123, 1, -64, 1, 51, -110,
2, 48, -90, 1, 29, -84, 1, 67, -94, 2, 84, -10, 1, -86, 1, 82, -106,
2, 70, -32, 1, 64, -96, 3, -20, 1, -126, 2, 112, -56, 3, 26, -104, 3,
15, -30, 3, 101, -48, 3, -87, 1, -28, 3, -107, 1, -62, 1, 113, 118, -81,
1, 110, 27, 56, -106, 1, 71, -78, 1, -67, 1, -122, 2, -7, 1, -40, 1,
-81, 1, -86, 1, 17, 108, 26, -84, 1, -100, 1, 54, -62, 1, -111, 2, -120,
3, -127, 2, -16, 1, 127, -62, 1, -43, 1, -32, 1, -67, 2, 38, -49, 1,
-100, 1, 64, 14, -74, 1, 105, -100, 2, -125, 1, -44, 3, -81, 1, -126, 3,
-93, 1, -64, 1, -67, 2, -82, 3, -85, 1, -36, 1, -75, 1, 48, -67, 1,
-128, 1, -111, 1, -72, 2, -59, 2, -8, 3, -113, 3, -40, 3, -85, 2, -126,
2, -43, 2, -68, 2, -17, 1, -46, 1, -35, 2, -4, 2, 42, -106, 1, -96,
1, 48, -82, 7, -38, 4, -102, 2, -90, 1, -74, 3, -36, 1, -16, 1, 47,
-38, 2, -105, 2, -70, 1, -37, 1, -34, 2, -85, 2, -44, 1, 117, -106, 2,
91, -42, 2, -68, 1, -18, 2, -126, 1, -62, 7, -62, 2, -4, 2, -106, 1,
-2, 3, -118, 1, -78, 1, -118, 1};

//===========================================================================
// Take a given real-world road shape. Create a second shape that is the
// reverse shape-point order of the first.
Expand All @@ -41,6 +23,26 @@ const std::string fwd_enc_shape =
// 2) that the fwd_percentage_along + rev_percentage_along == 1.0 (within tol)
//===========================================================================
TEST(geometry, fwd_rev_projection_tols) {
// Some real world encoded shape data.
const std::string fwd_enc_shape =
{-24, -109, -78, 36, -90, -4, -46, 12, 33, 118, 71, 16, 117, 47, 41, 91, 113,
44, 45, 112, -125, 1, 80, -67, 1, 54, -1, 1, 117, -31, 2, -43, 1, -33,
1, 21, -93, 2, 15, -25, 1, 47, -97, 1, 86, -123, 1, -64, 1, 51, -110,
2, 48, -90, 1, 29, -84, 1, 67, -94, 2, 84, -10, 1, -86, 1, 82, -106,
2, 70, -32, 1, 64, -96, 3, -20, 1, -126, 2, 112, -56, 3, 26, -104, 3,
15, -30, 3, 101, -48, 3, -87, 1, -28, 3, -107, 1, -62, 1, 113, 118, -81,
1, 110, 27, 56, -106, 1, 71, -78, 1, -67, 1, -122, 2, -7, 1, -40, 1,
-81, 1, -86, 1, 17, 108, 26, -84, 1, -100, 1, 54, -62, 1, -111, 2, -120,
3, -127, 2, -16, 1, 127, -62, 1, -43, 1, -32, 1, -67, 2, 38, -49, 1,
-100, 1, 64, 14, -74, 1, 105, -100, 2, -125, 1, -44, 3, -81, 1, -126, 3,
-93, 1, -64, 1, -67, 2, -82, 3, -85, 1, -36, 1, -75, 1, 48, -67, 1,
-128, 1, -111, 1, -72, 2, -59, 2, -8, 3, -113, 3, -40, 3, -85, 2, -126,
2, -43, 2, -68, 2, -17, 1, -46, 1, -35, 2, -4, 2, 42, -106, 1, -96,
1, 48, -82, 7, -38, 4, -102, 2, -90, 1, -74, 3, -36, 1, -16, 1, 47,
-38, 2, -105, 2, -70, 1, -37, 1, -34, 2, -85, 2, -44, 1, 117, -106, 2,
91, -42, 2, -68, 1, -18, 2, -126, 1, -62, 7, -62, 2, -4, 2, -106, 1,
-2, 3, -118, 1, -78, 1, -118, 1};

std::vector<PointLL> fwd_shape_points = decode7<std::vector<PointLL>>(fwd_enc_shape);
std::vector<PointLL> rev_shape_points(fwd_shape_points);
std::reverse(rev_shape_points.begin(), rev_shape_points.end());
Expand Down