2015-11-10 14:49:12 +01:00
|
|
|
#include "catch.hpp"
|
|
|
|
|
2017-03-27 17:14:51 +02:00
|
|
|
#include <mapnik/geometry/centroid.hpp>
|
2015-11-10 14:49:12 +01:00
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
TEST_CASE("geometry centroid")
|
|
|
|
{
|
|
|
|
SECTION("empty geometry")
|
|
|
|
{
|
|
|
|
mapnik::geometry::geometry_empty geom;
|
|
|
|
mapnik::geometry::point<double> centroid;
|
|
|
|
REQUIRE(!mapnik::geometry::centroid(geom, centroid));
|
|
|
|
}
|
2015-11-10 14:49:12 +01:00
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
SECTION("geometry collection")
|
|
|
|
{
|
|
|
|
mapnik::geometry::geometry_collection<double> geom;
|
|
|
|
mapnik::geometry::point<double> centroid;
|
|
|
|
REQUIRE(!mapnik::geometry::centroid(geom, centroid));
|
|
|
|
}
|
2015-11-10 14:49:12 +01:00
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
SECTION("point")
|
|
|
|
{
|
|
|
|
mapnik::geometry::point<double> pt(10, 10);
|
|
|
|
mapnik::geometry::point<double> centroid;
|
|
|
|
REQUIRE(mapnik::geometry::centroid(pt, centroid));
|
|
|
|
REQUIRE(pt.x == centroid.x);
|
|
|
|
REQUIRE(pt.y == centroid.y);
|
|
|
|
}
|
2015-11-10 14:49:12 +01:00
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
SECTION("linestring")
|
|
|
|
{
|
|
|
|
mapnik::geometry::line_string<double> line;
|
|
|
|
line.emplace_back(0, 0);
|
|
|
|
line.emplace_back(25, 25);
|
|
|
|
line.emplace_back(50, 50);
|
|
|
|
mapnik::geometry::point<double> centroid;
|
|
|
|
REQUIRE(mapnik::geometry::centroid(line, centroid));
|
|
|
|
REQUIRE(centroid.x == 25);
|
|
|
|
REQUIRE(centroid.y == 25);
|
|
|
|
}
|
2015-11-10 14:49:12 +01:00
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
SECTION("empty linestring")
|
|
|
|
{
|
|
|
|
mapnik::geometry::line_string<double> line;
|
|
|
|
mapnik::geometry::point<double> centroid;
|
|
|
|
REQUIRE(!mapnik::geometry::centroid(line, centroid));
|
|
|
|
}
|
2015-11-10 14:49:12 +01:00
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
SECTION("polygon")
|
|
|
|
{
|
|
|
|
mapnik::geometry::polygon<double> poly;
|
|
|
|
mapnik::geometry::linear_ring<double> 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));
|
2015-11-10 14:49:12 +01:00
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
mapnik::geometry::point<double> centroid;
|
|
|
|
REQUIRE(mapnik::geometry::centroid(poly, centroid));
|
|
|
|
REQUIRE(centroid.x == 0.5);
|
|
|
|
REQUIRE(centroid.y == 0.5);
|
|
|
|
}
|
2015-11-10 14:49:12 +01:00
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
SECTION("polygon with empty exterior ring")
|
|
|
|
{
|
|
|
|
mapnik::geometry::polygon<double> poly;
|
|
|
|
mapnik::geometry::linear_ring<double> ring;
|
|
|
|
poly.push_back(std::move(ring));
|
2015-11-10 14:49:12 +01:00
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
mapnik::geometry::point<double> centroid;
|
|
|
|
REQUIRE(!mapnik::geometry::centroid(poly, centroid));
|
|
|
|
}
|
2015-11-10 14:49:12 +01:00
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
SECTION("empty polygon")
|
|
|
|
{
|
|
|
|
mapnik::geometry::polygon<double> poly;
|
|
|
|
poly.emplace_back();
|
|
|
|
mapnik::geometry::point<double> centroid;
|
|
|
|
REQUIRE(!mapnik::geometry::centroid(poly, centroid));
|
|
|
|
}
|
2015-11-10 14:49:12 +01:00
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
SECTION("multi-point")
|
|
|
|
{
|
|
|
|
mapnik::geometry::multi_point<double> geom;
|
|
|
|
geom.emplace_back(0, 0);
|
|
|
|
geom.emplace_back(25, 25);
|
|
|
|
geom.emplace_back(50, 50);
|
|
|
|
mapnik::geometry::point<double> centroid;
|
|
|
|
REQUIRE(mapnik::geometry::centroid(geom, centroid));
|
|
|
|
REQUIRE(centroid.x == 25);
|
|
|
|
REQUIRE(centroid.y == 25);
|
|
|
|
}
|
2015-11-10 14:49:12 +01:00
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
SECTION("empty multi-point")
|
|
|
|
{
|
|
|
|
mapnik::geometry::multi_point<double> geom;
|
|
|
|
mapnik::geometry::point<double> centroid;
|
|
|
|
REQUIRE(!mapnik::geometry::centroid(geom, centroid));
|
|
|
|
}
|
2015-11-10 14:49:12 +01:00
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
SECTION("multi-linestring")
|
|
|
|
{
|
|
|
|
mapnik::geometry::multi_line_string<double> geom;
|
|
|
|
{
|
|
|
|
mapnik::geometry::line_string<double> line;
|
|
|
|
line.emplace_back(0, 0);
|
|
|
|
line.emplace_back(0, 25);
|
|
|
|
line.emplace_back(0, 50);
|
|
|
|
geom.emplace_back(std::move(line));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
mapnik::geometry::line_string<double> line;
|
|
|
|
line.emplace_back(0, 0);
|
|
|
|
line.emplace_back(25, 0);
|
|
|
|
line.emplace_back(50, 0);
|
|
|
|
geom.emplace_back(std::move(line));
|
|
|
|
}
|
|
|
|
mapnik::geometry::point<double> centroid;
|
|
|
|
REQUIRE(mapnik::geometry::centroid(geom, centroid));
|
|
|
|
REQUIRE(centroid.x == 12.5);
|
|
|
|
REQUIRE(centroid.y == 12.5);
|
|
|
|
}
|
2015-11-10 14:49:12 +01:00
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
SECTION("multi-linestring: one component empty")
|
2015-11-10 14:49:12 +01:00
|
|
|
{
|
2022-01-26 23:25:53 +01:00
|
|
|
mapnik::geometry::multi_line_string<double> geom;
|
2015-11-10 14:49:12 +01:00
|
|
|
mapnik::geometry::line_string<double> line;
|
2016-06-27 11:23:13 +02:00
|
|
|
line.emplace_back(0, 0);
|
|
|
|
line.emplace_back(0, 25);
|
|
|
|
line.emplace_back(0, 50);
|
2015-11-10 14:49:12 +01:00
|
|
|
geom.emplace_back(std::move(line));
|
2022-01-26 23:25:53 +01:00
|
|
|
geom.emplace_back();
|
|
|
|
mapnik::geometry::point<double> centroid;
|
|
|
|
REQUIRE(mapnik::geometry::centroid(geom, centroid));
|
|
|
|
REQUIRE(centroid.x == 0);
|
|
|
|
REQUIRE(centroid.y == 25);
|
2015-11-10 14:49:12 +01:00
|
|
|
}
|
2022-01-26 23:25:53 +01:00
|
|
|
|
|
|
|
SECTION("empty multi-linestring")
|
2015-11-10 14:49:12 +01:00
|
|
|
{
|
2022-01-26 23:25:53 +01:00
|
|
|
mapnik::geometry::multi_line_string<double> geom;
|
|
|
|
mapnik::geometry::point<double> centroid;
|
|
|
|
REQUIRE(!mapnik::geometry::centroid(geom, centroid));
|
2015-11-10 14:49:12 +01:00
|
|
|
}
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
SECTION("multi-polygon")
|
|
|
|
{
|
|
|
|
mapnik::geometry::multi_polygon<double> geom;
|
|
|
|
{
|
|
|
|
mapnik::geometry::polygon<double> poly;
|
|
|
|
mapnik::geometry::linear_ring<double> 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));
|
|
|
|
geom.emplace_back(std::move(poly));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
mapnik::geometry::polygon<double> poly;
|
|
|
|
mapnik::geometry::linear_ring<double> ring;
|
|
|
|
ring.emplace_back(1, 1);
|
|
|
|
ring.emplace_back(2, 1);
|
|
|
|
ring.emplace_back(2, 2);
|
|
|
|
ring.emplace_back(1, 2);
|
|
|
|
ring.emplace_back(1, 1);
|
|
|
|
poly.push_back(std::move(ring));
|
|
|
|
geom.emplace_back(std::move(poly));
|
|
|
|
}
|
|
|
|
|
|
|
|
mapnik::geometry::point<double> centroid;
|
|
|
|
REQUIRE(mapnik::geometry::centroid(geom, centroid));
|
|
|
|
REQUIRE(centroid.x == 1);
|
|
|
|
REQUIRE(centroid.y == 1);
|
|
|
|
}
|
2015-11-10 14:49:12 +01:00
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
SECTION("multi-polygon: one component empty")
|
2015-11-10 14:49:12 +01:00
|
|
|
{
|
2022-01-26 23:25:53 +01:00
|
|
|
mapnik::geometry::multi_polygon<double> geom;
|
2015-11-10 14:49:12 +01:00
|
|
|
mapnik::geometry::polygon<double> poly;
|
|
|
|
mapnik::geometry::linear_ring<double> ring;
|
2016-06-27 11:23:13 +02:00
|
|
|
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);
|
2016-06-29 13:37:32 +02:00
|
|
|
poly.push_back(std::move(ring));
|
2015-11-10 14:49:12 +01:00
|
|
|
geom.emplace_back(std::move(poly));
|
2022-01-26 23:25:53 +01:00
|
|
|
geom.emplace_back();
|
|
|
|
geom.back().emplace_back();
|
|
|
|
mapnik::geometry::point<double> centroid;
|
|
|
|
REQUIRE(mapnik::geometry::centroid(geom, centroid));
|
|
|
|
REQUIRE(centroid.x == 0.5);
|
|
|
|
REQUIRE(centroid.y == 0.5);
|
2015-11-10 14:49:12 +01:00
|
|
|
}
|
2022-01-26 23:25:53 +01:00
|
|
|
|
|
|
|
SECTION("empty multi-polygon")
|
2015-11-10 14:49:12 +01:00
|
|
|
{
|
2022-01-26 23:25:53 +01:00
|
|
|
mapnik::geometry::multi_polygon<double> geom;
|
|
|
|
mapnik::geometry::point<double> centroid;
|
|
|
|
REQUIRE(!mapnik::geometry::centroid(geom, centroid));
|
2015-11-10 14:49:12 +01:00
|
|
|
}
|
|
|
|
}
|