Skip to content

Commit 396faa4

Browse files
committed
Added tests for SphericalUtil class
1 parent 33c6e17 commit 396faa4

12 files changed

+393
-24
lines changed

functional_tests/SphericalUtil/computeAngleBetween.cpp renamed to tests/SphericalUtil/computeAngleBetween.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
#include "gtest/gtest.h"
1+
#include <gtest/gtest.h>
22

3-
#include <PolyUtil.hpp>
3+
#include "SphericalUtil.hpp"
44

55

66
TEST(SphericalUtil, computeAngleBetween) {
7-
LatLng up(90, 0);
8-
LatLng down(-90, 0);
9-
LatLng front(0, 0);
10-
LatLng right(0, 90);
11-
LatLng back(0, -180);
12-
LatLng left(0, -90);
7+
LatLng up = { 90.0, 0.0 };
8+
LatLng down = {-90.0, 0.0 };
9+
LatLng front = { 0.0, 0.0 };
10+
LatLng right = { 0.0, 90.0 };
11+
LatLng back = { 0.0, -180.0 };
12+
LatLng left = { 0.0, -90.0 };
1313

1414
EXPECT_NEAR(SphericalUtil::computeAngleBetween(up, up), 0, 1e-6);
1515
EXPECT_NEAR(SphericalUtil::computeAngleBetween(down, down), 0, 1e-6);
@@ -38,4 +38,4 @@ TEST(SphericalUtil, computeAngleBetween) {
3838
EXPECT_NEAR(SphericalUtil::computeAngleBetween(up, down), M_PI, 1e-6);
3939
EXPECT_NEAR(SphericalUtil::computeAngleBetween(front, back), M_PI, 1e-6);
4040
EXPECT_NEAR(SphericalUtil::computeAngleBetween(left, right), M_PI, 1e-6);
41-
}
41+
}

tests/SphericalUtil/computeArea.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <gtest/gtest.h>
2+
3+
#include "SphericalUtil.hpp"
4+
5+
6+
TEST(SphericalUtil, computeArea) {
7+
LatLng up = { 90.0, 0.0 };
8+
LatLng down = {-90.0, 0.0 };
9+
LatLng front = { 0.0, 0.0 };
10+
LatLng right = { 0.0, 90.0 };
11+
LatLng back = { 0.0, -180.0 };
12+
LatLng left = { 0.0, -90.0 };
13+
14+
std::vector<LatLng> first = { right, up, front, down, right };
15+
EXPECT_NEAR(SphericalUtil::computeArea(first), M_PI * MathUtil::EARTH_RADIUS * MathUtil::EARTH_RADIUS, .4);
16+
17+
std::vector<LatLng> second = { right, down, front, up, right };
18+
EXPECT_NEAR(SphericalUtil::computeArea(second), M_PI * MathUtil::EARTH_RADIUS * MathUtil::EARTH_RADIUS, .4);
19+
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#include "gtest/gtest.h"
1+
#include <gtest/gtest.h>
22

3-
#include <PolyUtil.hpp>
3+
#include "SphericalUtil.hpp"
44

55

66
TEST(SphericalUtil, computeDistanceBetween) {
7-
LatLng up(90, 0);
8-
LatLng down(-90, 0);
7+
LatLng up = { 90.0, 0.0};
8+
LatLng down = {-90.0, 0.0};
99

1010
EXPECT_NEAR(SphericalUtil::computeDistanceBetween(up, down), M_PI * MathUtil::EARTH_RADIUS, 1e-6);
11-
}
11+
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
#include "gtest/gtest.h"
1+
#include <gtest/gtest.h>
22

3-
#include <PolyUtil.hpp>
3+
#include "SphericalUtil.hpp"
44

55

66
TEST(SphericalUtil, computeHeading) {
7-
LatLng up(90, 0);
8-
LatLng down(-90, 0);
9-
LatLng front(0, 0);
10-
LatLng right(0, 90);
11-
LatLng back(0, -180);
12-
LatLng left(0, -90);
7+
LatLng up = { 90.0, 0.0 };
8+
LatLng down = {-90.0, 0.0 };
9+
LatLng front = { 0.0, 0.0 };
10+
LatLng right = { 0.0, 90.0 };
11+
LatLng back = { 0.0, -180.0 };
12+
LatLng left = { 0.0, -90.0 };
1313

1414
// Opposing vertices for which there is a result
1515
EXPECT_NEAR(SphericalUtil::computeHeading(up, down), -180, 1e-6);
16-
EXPECT_NEAR(SphericalUtil::computeHeading(down, up), 0, 1e-6);
16+
EXPECT_NEAR(SphericalUtil::computeHeading(down, up), 0, 1e-6);
1717

1818
// Adjacent vertices for which there is a result
1919
EXPECT_NEAR(SphericalUtil::computeHeading(front, up), 0, 1e-6);
@@ -31,4 +31,4 @@ TEST(SphericalUtil, computeHeading) {
3131

3232
EXPECT_NEAR(SphericalUtil::computeHeading(front, right), 90, 1e-6);
3333
EXPECT_NEAR(SphericalUtil::computeHeading(back, right), -90, 1e-6);
34-
}
34+
}

tests/SphericalUtil/computeLength.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <gtest/gtest.h>
2+
3+
#include "SphericalUtil.hpp"
4+
5+
6+
TEST(SphericalUtil, computeLength) {
7+
// List without points
8+
std::vector<LatLng> latLngs;
9+
EXPECT_NEAR(SphericalUtil::computeLength(latLngs), 0, 1e-6);
10+
11+
// List with one point
12+
latLngs.push_back(LatLng(0, 0));
13+
EXPECT_NEAR(SphericalUtil::computeLength(latLngs), 0, 1e-6);
14+
15+
// List with two points
16+
latLngs.push_back(LatLng(0.1, 0.1));
17+
EXPECT_NEAR(SphericalUtil::computeLength(latLngs), deg2rad(0.1) * sqrt(2) * MathUtil::EARTH_RADIUS, 1);
18+
19+
// List with three points
20+
std::vector<LatLng> latLngs2 = { {0, 0}, {90, 0}, {0, 90} };
21+
EXPECT_NEAR(SphericalUtil::computeLength(latLngs2), M_PI * MathUtil::EARTH_RADIUS, 1e-6);
22+
}

tests/SphericalUtil/computeOffset.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <gtest/gtest.h>
2+
3+
#include "SphericalUtil.hpp"
4+
5+
inline void EXPECT_NEAR_LatLan(LatLng actual, LatLng expected) {
6+
EXPECT_NEAR(actual.lat, expected.lat, 1e-6);
7+
// Issue #2
8+
// Account for the convergence of longitude lines at the poles
9+
// double cosLat = cos(deg2rad(actual.lat));
10+
// EXPECT_NEAR(cosLat * actual.lng, cosLat * expected.lng, 1e-6);
11+
}
12+
13+
TEST(SphericalUtil, computeOffset) {
14+
LatLng up = { 90.0, 0.0 };
15+
LatLng down = {-90.0, 0.0 };
16+
LatLng front = { 0.0, 0.0 };
17+
LatLng right = { 0.0, 90.0 };
18+
LatLng back = { 0.0, -180.0 };
19+
LatLng left = { 0.0, -90.0 };
20+
21+
EXPECT_NEAR_LatLan(front, SphericalUtil::computeOffset(front, 0, 0));
22+
EXPECT_NEAR_LatLan(up, SphericalUtil::computeOffset(front, M_PI * MathUtil::EARTH_RADIUS / 2, 0));
23+
EXPECT_NEAR_LatLan(down, SphericalUtil::computeOffset(front, M_PI * MathUtil::EARTH_RADIUS / 2, 180));
24+
EXPECT_NEAR_LatLan(left, SphericalUtil::computeOffset(front, M_PI * MathUtil::EARTH_RADIUS / 2, -90));
25+
EXPECT_NEAR_LatLan(right, SphericalUtil::computeOffset(front, M_PI * MathUtil::EARTH_RADIUS / 2, 90));
26+
EXPECT_NEAR_LatLan(back, SphericalUtil::computeOffset(front, M_PI * MathUtil::EARTH_RADIUS, 0));
27+
EXPECT_NEAR_LatLan(back, SphericalUtil::computeOffset(front, M_PI * MathUtil::EARTH_RADIUS, 90));
28+
29+
// From left
30+
EXPECT_NEAR_LatLan(left, SphericalUtil::computeOffset(left, 0, 0));
31+
EXPECT_NEAR_LatLan(up, SphericalUtil::computeOffset(left, M_PI * MathUtil::EARTH_RADIUS / 2, 0));
32+
EXPECT_NEAR_LatLan(down, SphericalUtil::computeOffset(left, M_PI * MathUtil::EARTH_RADIUS / 2, 180));
33+
EXPECT_NEAR_LatLan(front, SphericalUtil::computeOffset(left, M_PI * MathUtil::EARTH_RADIUS / 2, 90));
34+
EXPECT_NEAR_LatLan(back, SphericalUtil::computeOffset(left, M_PI * MathUtil::EARTH_RADIUS / 2, -90));
35+
EXPECT_NEAR_LatLan(right, SphericalUtil::computeOffset(left, M_PI * MathUtil::EARTH_RADIUS, 0));
36+
EXPECT_NEAR_LatLan(right, SphericalUtil::computeOffset(left, M_PI * MathUtil::EARTH_RADIUS, 90));
37+
38+
// NOTE: Heading is undefined at the poles, so we do not test from up/down.
39+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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, computeOffsetOrigin) {
15+
LatLng front = { 0.0, 0.0 };
16+
17+
EXPECT_NEAR_LatLan(front, SphericalUtil::computeOffsetOrigin(front, 0, 0));
18+
19+
EXPECT_NEAR_LatLan(front, SphericalUtil::computeOffsetOrigin(LatLng( 0, 45), M_PI * MathUtil::EARTH_RADIUS / 4, 90));
20+
EXPECT_NEAR_LatLan(front, SphericalUtil::computeOffsetOrigin(LatLng( 0, -45), M_PI * MathUtil::EARTH_RADIUS / 4, -90));
21+
EXPECT_NEAR_LatLan(front, SphericalUtil::computeOffsetOrigin(LatLng( 45, 0), M_PI * MathUtil::EARTH_RADIUS / 4, 0));
22+
EXPECT_NEAR_LatLan(front, SphericalUtil::computeOffsetOrigin(LatLng(-45, 0), M_PI * MathUtil::EARTH_RADIUS / 4, 180));
23+
24+
// Issue #3
25+
// Situations with no solution, should return null.
26+
//
27+
// First 'over' the pole.
28+
// EXPECT_NULL(SphericalUtil::computeOffsetOrigin(LatLng(80, 0), M_PI * MathUtil::EARTH_RADIUS / 4, 180));
29+
30+
// Second a distance that doesn't fit on the earth.
31+
// EXPECT_NULL(SphericalUtil::computeOffsetOrigin(LatLng(80, 0), M_PI * MathUtil::EARTH_RADIUS / 4, 90));
32+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <gtest/gtest.h>
2+
3+
#include "SphericalUtil.hpp"
4+
5+
6+
TEST(SphericalUtil, computeSignedArea) {
7+
LatLng up = { 90.0, 0.0 };
8+
LatLng down = {-90.0, 0.0 };
9+
LatLng front = { 0.0, 0.0 };
10+
LatLng right = { 0.0, 90.0 };
11+
12+
std::vector<LatLng> path = { right, up, front, down, right };
13+
std::vector<LatLng> pathReversed = { right, down, front, up, right };
14+
15+
EXPECT_NEAR(-SphericalUtil::computeSignedArea(path), SphericalUtil::computeSignedArea(pathReversed), 0);
16+
}

tests/SphericalUtil/interpolate.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}

tests/packages.config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn" version="1.8.0" targetFramework="native" />
4+
</packages>

0 commit comments

Comments
 (0)