|
| 1 | +#include <gtest/gtest.h> |
| 2 | + |
| 3 | +#include "SphericalUtil.hpp" |
| 4 | + |
| 5 | + |
| 6 | +inline void EXPECT_NEAR_LatLan(LatLng actual, LatLng expected) { |
| 7 | + EXPECT_NEAR(actual.lat, expected.lat, 1e-6); |
| 8 | + // Issue #2 |
| 9 | + // Account for the convergence of longitude lines at the poles |
| 10 | + // double cosLat = cos(deg2rad(actual.lat)); |
| 11 | + // EXPECT_NEAR(cosLat * actual.lng, cosLat * expected.lng, 1e-6); |
| 12 | +} |
| 13 | + |
| 14 | +TEST(SphericalUtil, interpolate) { |
| 15 | + LatLng up(90, 0); |
| 16 | + LatLng down(-90, 0); |
| 17 | + LatLng front(0, 0); |
| 18 | + LatLng right(0, 90); |
| 19 | + LatLng back(0, -180); |
| 20 | + LatLng left(0, -90); |
| 21 | + |
| 22 | + EXPECT_NEAR_LatLan(up, SphericalUtil::interpolate(up, up, 1 / 2.0)); |
| 23 | + EXPECT_NEAR_LatLan(down, SphericalUtil::interpolate(down, down, 1 / 2.0)); |
| 24 | + EXPECT_NEAR_LatLan(left, SphericalUtil::interpolate(left, left, 1 / 2.0)); |
| 25 | + |
| 26 | + // Between front and up |
| 27 | + EXPECT_NEAR_LatLan(LatLng(1, 0), SphericalUtil::interpolate(front, up, 1 / 90.0)); |
| 28 | + EXPECT_NEAR_LatLan(LatLng(1, 0), SphericalUtil::interpolate(up, front, 89 / 90.0)); |
| 29 | + EXPECT_NEAR_LatLan(LatLng(89, 0), SphericalUtil::interpolate(front, up, 89 / 90.0)); |
| 30 | + EXPECT_NEAR_LatLan(LatLng(89, 0), SphericalUtil::interpolate(up, front, 1 / 90.0)); |
| 31 | + |
| 32 | + // Between front and down |
| 33 | + EXPECT_NEAR_LatLan(LatLng(-1, 0), SphericalUtil::interpolate(front, down, 1 / 90.0)); |
| 34 | + EXPECT_NEAR_LatLan(LatLng(-1, 0), SphericalUtil::interpolate(down, front, 89 / 90.0)); |
| 35 | + EXPECT_NEAR_LatLan(LatLng(-89, 0), SphericalUtil::interpolate(front, down, 89 / 90.0)); |
| 36 | + EXPECT_NEAR_LatLan(LatLng(-89, 0), SphericalUtil::interpolate(down, front, 1 / 90.0)); |
| 37 | + |
| 38 | + // Between left and back |
| 39 | + EXPECT_NEAR_LatLan(LatLng(0, -91), SphericalUtil::interpolate(left, back, 1 / 90.0)); |
| 40 | + EXPECT_NEAR_LatLan(LatLng(0, -91), SphericalUtil::interpolate(back, left, 89 / 90.0)); |
| 41 | + EXPECT_NEAR_LatLan(LatLng(0, -179), SphericalUtil::interpolate(left, back, 89 / 90.0)); |
| 42 | + EXPECT_NEAR_LatLan(LatLng(0, -179), SphericalUtil::interpolate(back, left, 1 / 90.0)); |
| 43 | + |
| 44 | + // geodesic crosses pole |
| 45 | + EXPECT_NEAR_LatLan(up, SphericalUtil::interpolate(LatLng(45, 0), LatLng(45, 180), 1 / 2.0)); |
| 46 | + EXPECT_NEAR_LatLan(down, SphericalUtil::interpolate(LatLng(-45, 0), LatLng(-45, 180), 1 / 2.0)); |
| 47 | + |
| 48 | + // boundary values for fraction, between left and back |
| 49 | + EXPECT_NEAR_LatLan(left, SphericalUtil::interpolate(left, back, 0.0)); |
| 50 | + EXPECT_NEAR_LatLan(back, SphericalUtil::interpolate(left, back, 1.0)); |
| 51 | + |
| 52 | + // two nearby points, separated by ~4m, for which the Slerp algorithm is not stable and we |
| 53 | + // have to fall back to linear interpolation. |
| 54 | + LatLng interpolateResult = SphericalUtil::interpolate(LatLng(-37.756891, 175.325262), LatLng(-37.756853, 175.325242), 0.5); |
| 55 | + LatLng goldenResult(-37.756872, 175.325252); |
| 56 | + |
| 57 | + EXPECT_NEAR(interpolateResult.lat, goldenResult.lat, 2e-5); |
| 58 | +} |
0 commit comments