Skip to content

Commit 4406a5b

Browse files
committed
Fix interpolate fallback for nearly-coincident points
1 parent 2ca4e46 commit 4406a5b

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

include/SphericalUtil.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ class SphericalUtil {
136136
double angle = SphericalUtil::computeAngleBetween(from, to);
137137
double sinAngle = sin(angle);
138138
if (sinAngle < 1e-6) {
139-
return from;
139+
return LatLng(
140+
from.lat + fraction * (to.lat - from.lat),
141+
from.lng + fraction * (to.lng - from.lng));
140142
}
141143
double a = sin((1 - fraction) * angle) / sinAngle;
142144
double b = sin(fraction * angle) / sinAngle;

tests/SphericalUtil/interpolate.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,16 @@ TEST(SphericalUtil, interpolate) {
4444

4545
// two nearby points, separated by ~4m, for which the Slerp algorithm is not stable and we
4646
// have to fall back to linear interpolation.
47-
LatLng interpolateResult = SphericalUtil::interpolate(LatLng(-37.756891, 175.325262), LatLng(-37.756853, 175.325242), 0.5);
47+
LatLng nearA(-37.756891, 175.325262);
48+
LatLng nearB(-37.756853, 175.325242);
49+
50+
LatLng interpolateResult = SphericalUtil::interpolate(nearA, nearB, 0.5);
4851
LatLng goldenResult(-37.756872, 175.325252);
52+
EXPECT_NEAR(interpolateResult.lat, goldenResult.lat, 1e-6);
53+
EXPECT_NEAR(interpolateResult.lng, goldenResult.lng, 1e-6);
4954

50-
EXPECT_NEAR(interpolateResult.lat, goldenResult.lat, 2e-5);
51-
EXPECT_NEAR(interpolateResult.lng, goldenResult.lng, 2e-5);
55+
// fraction=1.0 must return 'to', not 'from'
56+
LatLng endResult = SphericalUtil::interpolate(nearA, nearB, 1.0);
57+
EXPECT_NEAR(endResult.lat, nearB.lat, 1e-6);
58+
EXPECT_NEAR(endResult.lng, nearB.lng, 1e-6);
5259
}

0 commit comments

Comments
 (0)