summaryrefslogtreecommitdiff
path: root/test/util
diff options
context:
space:
mode:
authorMikko Pulkki <mikko.pulkki@mapbox.com>2020-03-23 18:21:13 +0200
committerMikko Pulkki <55925868+mpulkki-mapbox@users.noreply.github.com>2020-04-06 12:10:21 +0300
commitd0c103fa8f0bade2be56d50a745f16b4e9ed6b29 (patch)
treee41956f80cfd63d7ab4e154c9785b39e7e6c99d4 /test/util
parent69dc210598ff8319de53bb603438ba89d47d6229 (diff)
downloadqtlocation-mapboxgl-d0c103fa8f0bade2be56d50a745f16b4e9ed6b29.tar.gz
Refactor tileCover to support lod tiles
Diffstat (limited to 'test/util')
-rw-r--r--test/util/bounding_volumes.test.cpp152
-rw-r--r--test/util/tile_cover.test.cpp169
2 files changed, 298 insertions, 23 deletions
diff --git a/test/util/bounding_volumes.test.cpp b/test/util/bounding_volumes.test.cpp
new file mode 100644
index 0000000000..c788c9919a
--- /dev/null
+++ b/test/util/bounding_volumes.test.cpp
@@ -0,0 +1,152 @@
+#include <mbgl/util/bounding_volumes.hpp>
+
+#include <gtest/gtest.h>
+#include <math.h>
+
+using namespace mbgl;
+
+TEST(BoundingVolumes, CreateAabb) {
+ util::AABB aabb({0, 0, 0}, {2, 4, 6});
+
+ EXPECT_EQ(aabb.min, (vec3{0, 0, 0}));
+ EXPECT_EQ(aabb.max, (vec3{2, 4, 6}));
+}
+
+TEST(BoundingVolumes, AabbEquality) {
+ util::AABB aabb0({-4.23, 1.01, 2.0}, {-1.0, 1.02, 100.0});
+ util::AABB aabb1({-4.23, 1.01, 2.0}, {-1.0, 1.02, 100.0});
+ util::AABB aabb2({-4.23, 1.01, 2.0}, {-1.0, 1.03, 55.0});
+
+ EXPECT_EQ(aabb0, aabb1);
+ EXPECT_NE(aabb0, aabb2);
+}
+
+TEST(BoundingVolumes, CreateAabbQuadrants) {
+ util::AABB aabb({0, 0, 0}, {2, 4, 16});
+
+ util::AABB quadrant0({0, 0, 0}, {1, 2, 16});
+ util::AABB quadrant1({1, 0, 0}, {2, 2, 16});
+ util::AABB quadrant2({0, 2, 0}, {1, 4, 16});
+ util::AABB quadrant3({1, 2, 0}, {2, 4, 16});
+
+ EXPECT_EQ(aabb.quadrant(0), quadrant0);
+ EXPECT_EQ(aabb.quadrant(1), quadrant1);
+ EXPECT_EQ(aabb.quadrant(2), quadrant2);
+ EXPECT_EQ(aabb.quadrant(3), quadrant3);
+}
+
+TEST(BoundingVolumes, AabbDistanceToPoint) {
+ util::AABB aabb({-1.0, -1.0, -1.0}, {1.0, 1.0, 1.0});
+
+ EXPECT_EQ(aabb.distanceXYZ({0.5, -0.5, 0.5}), (vec3{0, 0, 0}));
+ EXPECT_EQ(aabb.distanceXYZ({1, 1, 1}), (vec3{0, 0, 0}));
+ EXPECT_EQ(aabb.distanceXYZ({0, 10, 0}), (vec3{0, 9, 0}));
+ EXPECT_EQ(aabb.distanceXYZ({-2, -2, -4}), (vec3{1, 1, 3}));
+}
+
+TEST(BoundingVolumes, AabbAabbIntersection) {
+ util::AABB aabb0({1, 0, 0}, {3, 2, 1});
+ util::AABB aabb1({2.5, 1.5, -1}, {6.0, 2.5, 1});
+ util::AABB aabb2({3.5, -2, -2}, {5.0, 1, -1.5});
+ util::AABB aabb3({0.5, -1, -1}, {6.5, 3, 4});
+
+ EXPECT_EQ(aabb0.intersects(aabb1), true);
+ EXPECT_EQ(aabb0.intersects(aabb2), false);
+ EXPECT_EQ(aabb1.intersects(aabb2), false);
+ EXPECT_EQ(aabb3.intersects(aabb0), true);
+ EXPECT_EQ(aabb3.intersects(aabb1), true);
+ EXPECT_EQ(aabb3.intersects(aabb2), false);
+}
+
+TEST(BoundingVolumes, CreateFrustumFromProjectionMatrix) {
+ mat4 projMatrix;
+ mat4 invProjMatrix;
+ matrix::perspective(projMatrix, M_PI_2, 1.0, 0.1, 100.0);
+ matrix::invert(invProjMatrix, projMatrix);
+
+ const util::Frustum frustum = util::Frustum::fromInvProjMatrix(invProjMatrix, 1.0, 0.0, false);
+
+ const std::array<vec3, 8> expectedPoints = {vec3{-0.1, 0.1, -0.1},
+ vec3{0.1, 0.1, -0.1},
+ vec3{0.1, -0.1, -0.1},
+ vec3{-0.1, -0.1, -0.1},
+ vec3{-100.0, 100.0, -100.0},
+ vec3{100.0, 100.0, -100.0},
+ vec3{100.0, -100.0, -100.0},
+ vec3{-100.0, -100.0, -100.0}};
+
+ const std::array<vec4, 6> expectedPlanes = {
+
+ vec4{0, -0.707, 0.707, 0},
+ vec4{-0.707, 0, 0.707, 0},
+ vec4{0.707, 0, 0.707, 0},
+ vec4{0, 0.707, 0.707, 0},
+ vec4{0, 0, 1.0, 0.1},
+ vec4{0, 0, -1.0, -100.0}};
+
+ const auto roundPoint = [](vec3& v) {
+ for (auto& p : v) {
+ p = round(p * 10.0) / 10.0;
+ }
+ };
+
+ const auto roundPlane = [](vec4& v) {
+ for (auto& p : v) {
+ p = round(p * 1000.0) / 1000.0;
+ }
+ };
+
+ std::array<vec3, 8> actualPoints = frustum.getPoints();
+ std::array<vec4, 6> actualPlanes = frustum.getPlanes();
+
+ std::for_each(actualPoints.begin(), actualPoints.end(), roundPoint);
+ std::for_each(actualPlanes.begin(), actualPlanes.end(), roundPlane);
+
+ EXPECT_EQ(actualPoints, expectedPoints);
+ EXPECT_EQ(actualPlanes, expectedPlanes);
+}
+
+static util::Frustum createTestFrustum(
+ double fovy, double aspectRatio, double zNear, double zFar, double elevation, double bearing) {
+ mat4 proj;
+ mat4 invProj;
+
+ // Create a complete projection matrix for transforming from world space to clip space
+ matrix::perspective(proj, fovy, aspectRatio, zNear, zFar);
+ matrix::scale(proj, proj, 1, -1, 1);
+ matrix::translate(proj, proj, 0, 0, elevation);
+ matrix::rotate_z(proj, proj, bearing);
+ matrix::invert(invProj, proj);
+
+ return util::Frustum::fromInvProjMatrix(invProj, 1.0, 0.0);
+}
+
+TEST(BoundingVolumes, AabbFullyInsideFrustum) {
+ const util::Frustum frustum = createTestFrustum(M_PI_2, 1.0, 0.1, 100.0, -5.0, 0.0);
+
+ const std::array<util::AABB, 3> aabbArray = {
+ util::AABB({-1, -1, 0}, {1, 1, 0}), util::AABB({-5, -5, 0}, {5, 5, 0}), util::AABB({-5, -5, 0}, {-4, -2, 0})};
+
+ EXPECT_EQ(frustum.intersects(aabbArray[0]), util::IntersectionResult::Contains);
+ EXPECT_EQ(frustum.intersects(aabbArray[1]), util::IntersectionResult::Contains);
+ EXPECT_EQ(frustum.intersects(aabbArray[2]), util::IntersectionResult::Contains);
+}
+
+TEST(BoundingVolumes, AabbIntersectsFrustum) {
+ const util::Frustum frustum = createTestFrustum(M_PI_2, 1.0, 0.1, 100.0, -5.0, 0.0);
+
+ const std::array<util::AABB, 2> aabbArray = {util::AABB({-6, -6, 0}, {6, 6, 0}),
+ util::AABB({-6, -6, 0}, {-5, -5, 0})};
+
+ EXPECT_EQ(frustum.intersects(aabbArray[0]), util::IntersectionResult::Intersects);
+ EXPECT_EQ(frustum.intersects(aabbArray[1]), util::IntersectionResult::Intersects);
+}
+
+TEST(BoundingVolumes, AabbIntersectsFrustumEdgeCase) {
+ const util::Frustum frustum = createTestFrustum(M_PI_2, 1.0, 0.1, 100.0, -5.0, M_PI_4);
+ const util::AABB aabb({-10, 10, 0}, {10, 12, 0});
+
+ // Intersection test should report intersection even though shapes are separate
+ EXPECT_EQ(frustum.intersects(aabb), util::IntersectionResult::Intersects);
+ EXPECT_EQ(frustum.intersectsPrecise(aabb), util::IntersectionResult::Separate);
+} \ No newline at end of file
diff --git a/test/util/tile_cover.test.cpp b/test/util/tile_cover.test.cpp
index ba39bfa61b..2401608d45 100644
--- a/test/util/tile_cover.test.cpp
+++ b/test/util/tile_cover.test.cpp
@@ -37,9 +37,7 @@ TEST(TileCover, Pitch) {
transform.jumpTo(CameraOptions().withCenter(LatLng { 0.1, -0.1, }).withZoom(2.0).withBearing(5.0).withPitch(40.0));
- EXPECT_EQ((std::vector<UnwrappedTileID>{
- { 2, 1, 1 }, { 2, 2, 1 }, { 2, 1, 2 }, { 2, 2, 2 }
- }),
+ EXPECT_EQ((std::vector<OverscaledTileID>{{2, 1, 1}, {2, 2, 1}, {2, 1, 2}, {2, 2, 2}}),
util::tileCover(transform.getState(), 2));
}
@@ -51,25 +49,23 @@ TEST(TileCover, PitchIssue15442) {
.withZoom(2.0551126748417214).withBearing(0.74963938256567264 * util::RAD2DEG)
.withPitch(1.0471975511965976 * util::RAD2DEG));
- EXPECT_EQ((std::vector<UnwrappedTileID>{
- { 2, 3, 1 }, { 2, 2, 1 }, { 2, 3, 0 }, { 2, 2, 0 }, { 1, { 2, 0, 0 } }, { 1, { 2, 1, 0 } }
- }),
+ EXPECT_EQ((std::vector<OverscaledTileID>{
+ {2, 3, 1}, {2, 2, 1}, {2, 3, 0}, {2, 2, 0}, {2, 1, {2, 0, 0}}, {2, 1, {2, 1, 0}}}),
util::tileCover(transform.getState(), 2));
}
TEST(TileCover, PitchOverAllowedByContentInsets) {
Transform transform;
- transform.resize({ 512, 512 });
+ transform.resize({512, 512});
transform.jumpTo(CameraOptions().withCenter(LatLng { 0.1, -0.1 }).withPadding(EdgeInsets { 376, 0, 0, 0 })
.withZoom(8.0).withBearing(45.0).withPitch(60.0));
// Top padding of 376 leads to capped pitch. See Transform::getMaxPitchForEdgeInsets.
EXPECT_LE(transform.getPitch() + 0.001, util::DEG2RAD * 60);
- EXPECT_EQ((std::vector<UnwrappedTileID>{
- { 3, 4, 3 }, { 3, 3, 3 }, { 3, 4, 4 }, { 3, 3, 4 }, { 3, 4, 2 }, { 3, 5, 3 }, { 3, 5, 2 }
- }),
- util::tileCover(transform.getState(), 3));
+ EXPECT_EQ(
+ (std::vector<OverscaledTileID>{{3, 4, 3}, {3, 3, 3}, {3, 4, 4}, {3, 3, 4}, {3, 4, 2}, {3, 5, 3}, {3, 5, 2}}),
+ util::tileCover(transform.getState(), 3));
}
TEST(TileCover, PitchWithLargerResultSet) {
@@ -84,29 +80,156 @@ TEST(TileCover, PitchWithLargerResultSet) {
auto cover = util::tileCover(transform.getState(), 5);
// Returned vector has above 100 elements, we check first 16 as there is a
// plan to return lower LOD for distant tiles.
- EXPECT_EQ((std::vector<UnwrappedTileID> {
- { 5, 15, 16 }, { 5, 15, 17 }, { 5, 14, 16 }, { 5, 14, 17 },
- { 5, 16, 16 }, { 5, 16, 17 }, { 5, 15, 15 }, { 5, 14, 15 },
- { 5, 15, 18 }, { 5, 14, 18 }, { 5, 16, 15 }, { 5, 13, 16 },
- { 5, 13, 17 }, { 5, 16, 18 }, { 5, 13, 18 }, { 5, 15, 19 }
- }), (std::vector<UnwrappedTileID> { cover.begin(), cover.begin() + 16}) );
+ EXPECT_EQ((std::vector<OverscaledTileID>{{5, 15, 16},
+ {5, 15, 17},
+ {5, 14, 16},
+ {5, 14, 17},
+ {5, 16, 16},
+ {5, 16, 17},
+ {5, 15, 15},
+ {5, 14, 15},
+ {5, 15, 18},
+ {5, 14, 18},
+ {5, 16, 15},
+ {5, 13, 16},
+ {5, 13, 17},
+ {5, 16, 18},
+ {5, 13, 18},
+ {5, 15, 19}}),
+ (std::vector<OverscaledTileID>{cover.begin(), cover.begin() + 16}));
}
TEST(TileCover, WorldZ1) {
EXPECT_EQ((std::vector<UnwrappedTileID>{
- { 1, 0, 0 }, { 1, 0, 1 }, { 1, 1, 0 }, { 1, 1, 1 },
- }),
+ {1, 0, 0},
+ {1, 0, 1},
+ {1, 1, 0},
+ {1, 1, 1},
+ }),
util::tileCover(LatLngBounds::world(), 1));
}
TEST(TileCover, SingletonZ0) {
- EXPECT_EQ((std::vector<UnwrappedTileID>{}),
- util::tileCover(LatLngBounds::singleton({ 0, 0 }), 0));
+ EXPECT_EQ((std::vector<UnwrappedTileID>{}), util::tileCover(LatLngBounds::singleton({0, 0}), 0));
}
TEST(TileCover, SingletonZ1) {
- EXPECT_EQ((std::vector<UnwrappedTileID>{}),
- util::tileCover(LatLngBounds::singleton({ 0, 0 }), 1));
+ EXPECT_EQ((std::vector<UnwrappedTileID>{}), util::tileCover(LatLngBounds::singleton({0, 0}), 1));
+}
+
+TEST(TileCover, CoordinatesAreUnwrapped) {
+ Transform transform;
+ transform.resize({512, 512});
+ transform.jumpTo(CameraOptions()
+ .withCenter(LatLng{
+ 0.1,
+ -180.1,
+ })
+ .withZoom(1.0));
+
+ EXPECT_EQ(
+ (std::vector<OverscaledTileID>{{1, 0, {1, 1, 0}}, {1, 1, {1, 0, 0}}, {1, 0, {1, 1, 1}}, {1, 1, {1, 0, 1}}}),
+ util::tileCover(transform.getState(), 1));
+}
+
+TEST(TileCover, DifferentOverscaledZ) {
+ Transform transform;
+ transform.resize({512, 512});
+ // slightly offset center so that tile order is better defined
+
+ transform.jumpTo(CameraOptions()
+ .withCenter(LatLng{
+ 0.1,
+ -0.1,
+ })
+ .withZoom(2.0));
+
+ EXPECT_EQ(
+ (std::vector<OverscaledTileID>{{3, 0, {2, 1, 1}}, {3, 0, {2, 2, 1}}, {3, 0, {2, 1, 2}}, {3, 0, {2, 2, 2}}}),
+ util::tileCover(transform.getState(), 2, 3));
+}
+
+TEST(TileCover, DifferentOverscaledZWithPitch) {
+ Transform transform;
+ transform.resize({512, 512});
+ transform.jumpTo(CameraOptions()
+ .withCenter(LatLng{
+ 30.0,
+ -10.0,
+ })
+ .withZoom(3.5)
+ .withPitch(60));
+
+ EXPECT_EQ((std::vector<OverscaledTileID>{{5, 0, {3, 3, 3}},
+ {5, 0, {3, 4, 3}},
+ {5, 0, {3, 3, 2}},
+ {5, 0, {3, 4, 2}},
+ {5, 0, {3, 3, 1}},
+ {5, 0, {3, 4, 1}},
+ {5, 0, {3, 2, 1}}}),
+ util::tileCover(transform.getState(), 3, 5));
+}
+
+TEST(TileCover, DifferentOverscaledZWrapped) {
+ Transform transform;
+ transform.resize({512, 512});
+ transform.jumpTo(CameraOptions()
+ .withCenter(LatLng{
+ 0.1,
+ -180.1,
+ })
+ .withZoom(1.0));
+
+ EXPECT_EQ(
+ (std::vector<OverscaledTileID>{{2, 0, {1, 1, 0}}, {2, 1, {1, 0, 0}}, {2, 0, {1, 1, 1}}, {2, 1, {1, 0, 1}}}),
+ util::tileCover(transform.getState(), 1, 2));
+}
+
+TEST(TileCover, FlippedY) {
+ Transform transform;
+ transform.resize({512, 512});
+ transform.setViewportMode(ViewportMode::FlippedY);
+ transform.jumpTo(CameraOptions()
+ .withCenter(LatLng{
+ 0.2,
+ -0.1,
+ })
+ .withZoom(1.0));
+
+ EXPECT_EQ((std::vector<OverscaledTileID>{{1, 0, 0}, {1, 1, 0}, {1, 0, 1}, {1, 1, 1}}),
+ util::tileCover(transform.getState(), 1));
+}
+
+TEST(TileCover, FlippedYPitch) {
+ Transform transform;
+ transform.resize({512, 512});
+ transform.setViewportMode(ViewportMode::FlippedY);
+ transform.jumpTo(CameraOptions()
+ .withCenter(LatLng{
+ 0.1,
+ -0.1,
+ })
+ .withZoom(2.0)
+ .withBearing(5.0)
+ .withPitch(40.0));
+
+ EXPECT_EQ((std::vector<OverscaledTileID>{{2, 1, 1}, {2, 2, 1}, {2, 1, 2}, {2, 2, 2}}),
+ util::tileCover(transform.getState(), 2));
+}
+
+TEST(TileCover, FlippedYHelsinki) {
+ Transform transform;
+ transform.resize({512, 512});
+ transform.setViewportMode(ViewportMode::FlippedY);
+ transform.jumpTo(CameraOptions()
+ .withCenter(LatLng{
+ 60.167231,
+ 24.942063,
+ })
+ .withZoom(11.447425));
+
+ EXPECT_EQ((std::vector<OverscaledTileID>{{11, 1165, 592}, {11, 1166, 592}, {11, 1165, 593}, {11, 1166, 593}}),
+ util::tileCover(transform.getState(), 11));
}
TEST(TileCoverStream, Arctic) {