From f210699884e7d1b5cde321b943c6a3779ee857e2 Mon Sep 17 00:00:00 2001 From: Artem Pavlenko Date: Fri, 22 Sep 2017 13:20:23 +0200 Subject: [PATCH] add more tests --- test/unit/geometry/closest_point.cpp | 85 +++++++++++++++++++++------- 1 file changed, 63 insertions(+), 22 deletions(-) diff --git a/test/unit/geometry/closest_point.cpp b/test/unit/geometry/closest_point.cpp index 2d1fd8427..425bca271 100644 --- a/test/unit/geometry/closest_point.cpp +++ b/test/unit/geometry/closest_point.cpp @@ -4,43 +4,84 @@ TEST_CASE("geometry closest point") { +SECTION("geometry_empty") { + + mapnik::geometry::point pt(0, 0); + mapnik::geometry::geometry_empty empty; + auto result = mapnik::geometry::closest_point(empty, pt); + REQUIRE(result.x == 0.0); + REQUIRE(result.y == 0.0); + REQUIRE(result.distance == -1.0); +} + SECTION("point") { mapnik::geometry::point pt(0, 0); mapnik::geometry::point geom(3.0, 4.0); - auto result = mapnik::geometry::closest_point(pt, geom); - REQUIRE(result.closest_point == geom); + auto result = mapnik::geometry::closest_point(geom, pt); + REQUIRE(result.x == geom.x); + REQUIRE(result.y == geom.y); REQUIRE(result.distance == 5.0); } SECTION("linestring") { - //mapnik::geometry::line_string line; - //line.emplace_back(0, 0); - //line.emplace_back(25, 25); - //line.emplace_back(50, 50); - //mapnik::geometry::point centroid; - //REQUIRE(mapnik::geometry::centroid(line, centroid)); - //REQUIRE(centroid.x == 25); - //REQUIRE(centroid.y == 25); + mapnik::geometry::line_string line; + line.emplace_back(0, 0); + line.emplace_back(0, 100); + line.emplace_back(100, 100); + line.emplace_back(100, 0); + mapnik::geometry::point pt(50, 50); + auto result = mapnik::geometry::closest_point(line, pt); + REQUIRE(result.x == 0.0); + REQUIRE(result.y == 50.0); + REQUIRE(result.distance == 50.0); } SECTION("polygon") { - //mapnik::geometry::polygon poly; - //mapnik::geometry::linear_ring ring; - //ring.emplace_back(0, 0); - //ring.emplace_back(1, 0); - //ring.emplace_back(1, 1); - //ring.emplace_back(0, 1); - //ring.emplace_back(0, 0); - //poly.push_back(std::move(ring)); + mapnik::geometry::polygon poly; + mapnik::geometry::linear_ring ring; + ring.emplace_back(0, 0); + ring.emplace_back(1, 0); + ring.emplace_back(1, 1); + ring.emplace_back(0, 1); + ring.emplace_back(0, 0); + poly.push_back(std::move(ring)); - //mapnik::geometry::point centroid; - //REQUIRE(mapnik::geometry::centroid(poly, centroid)); - ///REQUIRE(centroid.x == 0.5); - //REQUIRE(centroid.y == 0.5); + { + // point inside polygon + mapnik::geometry::point pt {0.5, 0.25}; + auto result = mapnik::geometry::closest_point(poly, pt); + REQUIRE(result.x == 0.5); + REQUIRE(result.y == 0.25); + REQUIRE(result.distance == 0.0); + } + { + // point outside polygon + mapnik::geometry::point pt {1.25, 0.5}; + auto result = mapnik::geometry::closest_point(poly, pt); + REQUIRE(result.x == 1.0); + REQUIRE(result.y == 0.5); + REQUIRE(result.distance == 0.25); + } + { + // point outside polygon + mapnik::geometry::point pt {4.0, 5.0}; + auto result = mapnik::geometry::closest_point(poly, pt); + REQUIRE(result.x == 1.0); + REQUIRE(result.y == 1.0); + REQUIRE(result.distance == 5.0); + } + { + // point on polygon boundary + mapnik::geometry::point pt {0, 0.4}; + auto result = mapnik::geometry::closest_point(poly, pt); + REQUIRE(result.x == 0.0); + REQUIRE(result.y == 0.4); + REQUIRE(result.distance == 0.0); + } } }