mapnik/test/unit/geometry/centroid.cpp

207 lines
6.4 KiB
C++
Raw Normal View History

#include "catch.hpp"
2017-03-27 15:14:51 +00:00
#include <mapnik/geometry/centroid.hpp>
2022-01-26 22:25:53 +00:00
TEST_CASE("geometry centroid")
{
SECTION("empty geometry")
{
mapnik::geometry::geometry_empty geom;
mapnik::geometry::point<double> centroid;
REQUIRE(!mapnik::geometry::centroid(geom, centroid));
}
2022-01-26 22:25:53 +00:00
SECTION("geometry collection")
{
mapnik::geometry::geometry_collection<double> geom;
mapnik::geometry::point<double> centroid;
REQUIRE(!mapnik::geometry::centroid(geom, centroid));
}
2022-01-26 22:25:53 +00: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);
}
2022-01-26 22:25:53 +00: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);
}
2022-01-26 22:25:53 +00:00
SECTION("empty linestring")
{
mapnik::geometry::line_string<double> line;
mapnik::geometry::point<double> centroid;
REQUIRE(!mapnik::geometry::centroid(line, centroid));
}
2022-01-26 22:25:53 +00: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));
2022-01-26 22:25:53 +00:00
mapnik::geometry::point<double> centroid;
REQUIRE(mapnik::geometry::centroid(poly, centroid));
REQUIRE(centroid.x == 0.5);
REQUIRE(centroid.y == 0.5);
}
2022-01-26 22:25:53 +00:00
SECTION("polygon with empty exterior ring")
{
mapnik::geometry::polygon<double> poly;
mapnik::geometry::linear_ring<double> ring;
poly.push_back(std::move(ring));
2022-01-26 22:25:53 +00:00
mapnik::geometry::point<double> centroid;
REQUIRE(!mapnik::geometry::centroid(poly, centroid));
}
2022-01-26 22:25:53 +00:00
SECTION("empty polygon")
{
mapnik::geometry::polygon<double> poly;
poly.emplace_back();
mapnik::geometry::point<double> centroid;
REQUIRE(!mapnik::geometry::centroid(poly, centroid));
}
2022-01-26 22:25:53 +00: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);
}
2022-01-26 22:25:53 +00:00
SECTION("empty multi-point")
{
mapnik::geometry::multi_point<double> geom;
mapnik::geometry::point<double> centroid;
REQUIRE(!mapnik::geometry::centroid(geom, centroid));
}
2022-01-26 22:25:53 +00: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);
}
2022-01-26 22:25:53 +00:00
SECTION("multi-linestring: one component empty")
{
2022-01-26 22:25:53 +00:00
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));
2022-01-26 22:25:53 +00:00
geom.emplace_back();
mapnik::geometry::point<double> centroid;
REQUIRE(mapnik::geometry::centroid(geom, centroid));
REQUIRE(centroid.x == 0);
REQUIRE(centroid.y == 25);
}
2022-01-26 22:25:53 +00:00
SECTION("empty multi-linestring")
{
2022-01-26 22:25:53 +00:00
mapnik::geometry::multi_line_string<double> geom;
mapnik::geometry::point<double> centroid;
REQUIRE(!mapnik::geometry::centroid(geom, centroid));
}
2022-01-26 22:25:53 +00: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);
}
2022-01-26 22:25:53 +00:00
SECTION("multi-polygon: one component empty")
{
2022-01-26 22:25:53 +00:00
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));
2022-01-26 22:25:53 +00: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);
}
2022-01-26 22:25:53 +00:00
SECTION("empty multi-polygon")
{
2022-01-26 22:25:53 +00:00
mapnik::geometry::multi_polygon<double> geom;
mapnik::geometry::point<double> centroid;
REQUIRE(!mapnik::geometry::centroid(geom, centroid));
}
}