From 818ede5b93aefae9271392f82c655ba399d93e2e Mon Sep 17 00:00:00 2001 From: Matt Amos Date: Mon, 18 May 2015 15:10:55 +0100 Subject: [PATCH] Ported box2d_test.py --- test/unit/core/box2d_test.cpp | 163 ++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 test/unit/core/box2d_test.cpp diff --git a/test/unit/core/box2d_test.cpp b/test/unit/core/box2d_test.cpp new file mode 100644 index 000000000..c51980794 --- /dev/null +++ b/test/unit/core/box2d_test.cpp @@ -0,0 +1,163 @@ +#include "catch.hpp" + +#include +#include +#include + +TEST_CASE("box2d") { +SECTION("coord init") { + auto c = mapnik::coord2d(100, 100); + + REQUIRE(c.x == 100); + REQUIRE(c.y == 100); +} + +SECTION("coord multiplication") { + auto c = mapnik::coord2d(100, 100); + c *= 2; + + REQUIRE(c.x == 200); + REQUIRE(c.y == 200); +} + +SECTION("envelope init") { + auto e = mapnik::box2d(100, 100, 200, 200); + + REQUIRE(e.contains(100, 100)); + REQUIRE(e.contains(100, 200)); + REQUIRE(e.contains(200, 200)); + REQUIRE(e.contains(200, 100)); + + REQUIRE(e.contains(e.center())); + + REQUIRE(!e.contains(99.9, 99.9)); + REQUIRE(!e.contains(99.9, 200.1)); + REQUIRE(!e.contains(200.1, 200.1)); + REQUIRE(!e.contains(200.1, 99.9)); + + REQUIRE(e.width() == 100); + REQUIRE(e.height() == 100); + + REQUIRE(e.minx() == 100); + REQUIRE(e.miny() == 100); + + REQUIRE(e.maxx() == 200); + REQUIRE(e.maxy() == 200); + + REQUIRE(e[0] == 100); + REQUIRE(e[1] == 100); + REQUIRE(e[2] == 200); + REQUIRE(e[3] == 200); + REQUIRE(e[0] == e[-4]); + REQUIRE(e[1] == e[-3]); + REQUIRE(e[2] == e[-2]); + REQUIRE(e[3] == e[-1]); + + auto c = e.center(); + + REQUIRE(c.x == 150); + REQUIRE(c.y == 150); +} + +SECTION("envelope static init") { + auto e = mapnik::box2d(100, 100, 200, 200); + + mapnik::box2d e1, e2, e3; + REQUIRE(e1.from_string("100 100 200 200")); + REQUIRE(e2.from_string("100,100,200,200")); + REQUIRE(e3.from_string("100 , 100 , 200 , 200")); + + REQUIRE(e == e1); + REQUIRE(e == e2); + REQUIRE(e == e3); +} + +SECTION("envelope multiplication") { + // no width then no impact of multiplication + { + auto a = mapnik::box2d(100, 100, 100, 100); + a *= 5; + + REQUIRE(a.minx() == 100); + REQUIRE(a.miny() == 100); + REQUIRE(a.maxx() == 100); + REQUIRE(a.maxy() == 100); + } + + { + auto a = mapnik::box2d(100.0, 100.0, 100.0, 100.0); + a *= 5; + + REQUIRE(a.minx() == 100); + REQUIRE(a.miny() == 100); + REQUIRE(a.maxx() == 100); + REQUIRE(a.maxy() == 100); + } + + { + auto a = mapnik::box2d(100.0, 100.0, 100.001, 100.001); + a *= 5; + + REQUIRE(std::abs(a.minx() - 99.9980) < 0.001); + REQUIRE(std::abs(a.miny() - 99.9980) < 0.001); + REQUIRE(std::abs(a.maxx() - 100.0030) < 0.001); + REQUIRE(std::abs(a.maxy() - 100.0030) < 0.001); + } + + { + auto e = mapnik::box2d(100, 100, 200, 200); + e *= 2; + + REQUIRE(e.minx() == 50); + REQUIRE(e.miny() == 50); + REQUIRE(e.maxx() == 250); + REQUIRE(e.maxy() == 250); + + REQUIRE(e.contains(50, 50)); + REQUIRE(e.contains(50, 250)); + REQUIRE(e.contains(250, 250)); + REQUIRE(e.contains(250, 50)); + + REQUIRE(!e.contains(49.9, 49.9)); + REQUIRE(!e.contains(49.9, 250.1)); + REQUIRE(!e.contains(250.1, 250.1)); + REQUIRE(!e.contains(250.1, 49.9)); + + REQUIRE(e.contains(e.center())); + + REQUIRE(e.width() == 200); + REQUIRE(e.height() == 200); + + REQUIRE(e.minx() == 50); + REQUIRE(e.miny() == 50); + + REQUIRE(e.maxx() == 250); + REQUIRE(e.maxy() == 250); + + auto c = e.center(); + + REQUIRE(c.x == 150); + REQUIRE(c.y == 150); + } +} + +SECTION("envelope clipping") { + auto e1 = mapnik::box2d(-180,-90,180,90); + auto e2 = mapnik::box2d(-120,40,-110,48); + e1.clip(e2); + REQUIRE(e1 == e2); + + // madagascar in merc + e1 = mapnik::box2d(4772116.5490, -2744395.0631, 5765186.4203, -1609458.0673); + e2 = mapnik::box2d(5124338.3753, -2240522.1727, 5207501.8621, -2130452.8520); + e1.clip(e2); + REQUIRE(e1 == e2); + + // nz in lon/lat + e1 = mapnik::box2d(163.8062, -47.1897, 179.3628, -33.9069); + e2 = mapnik::box2d(173.7378, -39.6395, 174.4849, -38.9252); + e1.clip(e2); + REQUIRE(e1 == e2); +} + +} // TEST_CASE