2015-04-03 23:56:57 +00:00
|
|
|
#include "catch.hpp"
|
|
|
|
|
|
|
|
#include <mapnik/geometry.hpp>
|
2017-03-27 15:14:51 +00:00
|
|
|
#include <mapnik/geometry/envelope.hpp>
|
2015-04-03 23:56:57 +00:00
|
|
|
|
2016-05-03 16:26:46 +00:00
|
|
|
namespace {
|
2015-04-03 23:56:57 +00:00
|
|
|
|
2022-01-26 22:25:53 +00:00
|
|
|
template<typename T>
|
2016-05-03 16:26:46 +00:00
|
|
|
void envelope_test()
|
|
|
|
{
|
2015-04-03 23:56:57 +00:00
|
|
|
using namespace mapnik::geometry;
|
2016-05-03 16:26:46 +00:00
|
|
|
using coord_type = T;
|
|
|
|
|
2015-04-03 23:56:57 +00:00
|
|
|
{
|
2022-01-26 22:25:53 +00:00
|
|
|
geometry<coord_type> geom(point<coord_type>(1, 2));
|
2016-05-03 16:26:46 +00:00
|
|
|
mapnik::box2d<coord_type> bbox = mapnik::geometry::envelope(geom);
|
2022-01-26 22:25:53 +00:00
|
|
|
REQUIRE(bbox.minx() == 1);
|
|
|
|
REQUIRE(bbox.miny() == 2);
|
|
|
|
REQUIRE(bbox.maxx() == 1);
|
|
|
|
REQUIRE(bbox.maxy() == 2);
|
2015-04-03 23:56:57 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
// Test empty geom
|
2017-03-27 15:14:51 +00:00
|
|
|
geometry<coord_type> geom = mapnik::geometry::geometry_empty();
|
2016-05-03 16:26:46 +00:00
|
|
|
mapnik::box2d<coord_type> bbox = mapnik::geometry::envelope(geom);
|
2022-01-26 22:25:53 +00:00
|
|
|
REQUIRE_FALSE(bbox.valid());
|
2015-04-03 23:56:57 +00:00
|
|
|
}
|
|
|
|
{
|
2016-05-03 16:26:46 +00:00
|
|
|
line_string<coord_type> line;
|
2022-01-26 22:25:53 +00:00
|
|
|
line.emplace_back(0, 0);
|
|
|
|
line.emplace_back(1, 1);
|
|
|
|
line.emplace_back(2, 2);
|
2016-05-03 16:26:46 +00:00
|
|
|
geometry<coord_type> geom(line);
|
|
|
|
mapnik::box2d<coord_type> bbox = mapnik::geometry::envelope(geom);
|
2022-01-26 22:25:53 +00:00
|
|
|
REQUIRE(bbox.minx() == 0);
|
|
|
|
REQUIRE(bbox.miny() == 0);
|
|
|
|
REQUIRE(bbox.maxx() == 2);
|
|
|
|
REQUIRE(bbox.maxy() == 2);
|
2015-04-03 23:56:57 +00:00
|
|
|
}
|
|
|
|
{
|
2016-05-03 16:26:46 +00:00
|
|
|
line_string<coord_type> line;
|
2022-01-26 22:25:53 +00:00
|
|
|
line.emplace_back(0, 0);
|
|
|
|
line.emplace_back(1, 1);
|
|
|
|
line.emplace_back(2, 2);
|
2016-05-03 16:26:46 +00:00
|
|
|
line_string<coord_type> line2;
|
2022-01-26 22:25:53 +00:00
|
|
|
line2.emplace_back(0, 0);
|
|
|
|
line2.emplace_back(-1, -1);
|
|
|
|
line2.emplace_back(-2, -2);
|
2016-05-03 16:26:46 +00:00
|
|
|
multi_line_string<coord_type> multi_line;
|
2015-04-03 23:56:57 +00:00
|
|
|
multi_line.emplace_back(std::move(line));
|
|
|
|
multi_line.emplace_back(std::move(line2));
|
2016-05-03 16:26:46 +00:00
|
|
|
geometry<coord_type> geom(multi_line);
|
|
|
|
mapnik::box2d<coord_type> bbox = mapnik::geometry::envelope(geom);
|
2022-01-26 22:25:53 +00:00
|
|
|
REQUIRE(bbox.minx() == -2);
|
|
|
|
REQUIRE(bbox.miny() == -2);
|
|
|
|
REQUIRE(bbox.maxx() == 2);
|
|
|
|
REQUIRE(bbox.maxy() == 2);
|
2015-04-03 23:56:57 +00:00
|
|
|
}
|
|
|
|
{
|
2016-05-03 16:26:46 +00:00
|
|
|
polygon<coord_type> poly;
|
|
|
|
linear_ring<coord_type> ring;
|
2022-01-26 22:25:53 +00:00
|
|
|
ring.emplace_back(0, 0);
|
|
|
|
ring.emplace_back(-10, 0);
|
|
|
|
ring.emplace_back(-10, 10);
|
|
|
|
ring.emplace_back(0, 10);
|
|
|
|
ring.emplace_back(0, 0);
|
2016-06-29 11:37:32 +00:00
|
|
|
poly.push_back(std::move(ring));
|
2016-05-03 16:26:46 +00:00
|
|
|
geometry<coord_type> geom(poly);
|
|
|
|
mapnik::box2d<coord_type> bbox = mapnik::geometry::envelope(geom);
|
2022-01-26 22:25:53 +00:00
|
|
|
REQUIRE(bbox.minx() == -10);
|
|
|
|
REQUIRE(bbox.miny() == 0);
|
|
|
|
REQUIRE(bbox.maxx() == 0);
|
|
|
|
REQUIRE(bbox.maxy() == 10);
|
2015-04-03 23:56:57 +00:00
|
|
|
|
2016-05-03 16:26:46 +00:00
|
|
|
multi_polygon<coord_type> mp;
|
2015-04-03 23:56:57 +00:00
|
|
|
mp.push_back(poly);
|
2016-05-03 16:26:46 +00:00
|
|
|
geometry<coord_type> geom_mp(mp);
|
2015-04-03 23:56:57 +00:00
|
|
|
bbox = mapnik::geometry::envelope(geom_mp);
|
2022-01-26 22:25:53 +00:00
|
|
|
REQUIRE(bbox.minx() == -10);
|
|
|
|
REQUIRE(bbox.miny() == 0);
|
|
|
|
REQUIRE(bbox.maxx() == 0);
|
|
|
|
REQUIRE(bbox.maxy() == 10);
|
2015-04-03 23:56:57 +00:00
|
|
|
|
2016-05-03 16:26:46 +00:00
|
|
|
geometry_collection<coord_type> gc;
|
2015-04-03 23:56:57 +00:00
|
|
|
bbox = mapnik::geometry::envelope(gc);
|
2022-01-26 22:25:53 +00:00
|
|
|
REQUIRE_FALSE(bbox.valid());
|
2015-04-03 23:56:57 +00:00
|
|
|
gc.push_back(geom_mp);
|
|
|
|
bbox = mapnik::geometry::envelope(gc);
|
2022-01-26 22:25:53 +00:00
|
|
|
REQUIRE(bbox.minx() == -10);
|
|
|
|
REQUIRE(bbox.miny() == 0);
|
|
|
|
REQUIRE(bbox.maxx() == 0);
|
|
|
|
REQUIRE(bbox.maxy() == 10);
|
|
|
|
gc.emplace_back(point<coord_type>(-50, -50));
|
2015-04-03 23:56:57 +00:00
|
|
|
bbox = mapnik::geometry::envelope(gc);
|
2022-01-26 22:25:53 +00:00
|
|
|
REQUIRE(bbox.minx() == -50);
|
|
|
|
REQUIRE(bbox.miny() == -50);
|
|
|
|
REQUIRE(bbox.maxx() == 0);
|
|
|
|
REQUIRE(bbox.maxy() == 10);
|
2015-04-03 23:56:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// polygon with hole
|
2016-05-03 16:26:46 +00:00
|
|
|
polygon<coord_type> poly;
|
|
|
|
linear_ring<coord_type> ring;
|
2022-01-26 22:25:53 +00:00
|
|
|
ring.emplace_back(0, 0);
|
|
|
|
ring.emplace_back(-10, 0);
|
|
|
|
ring.emplace_back(-10, 10);
|
|
|
|
ring.emplace_back(0, 10);
|
|
|
|
ring.emplace_back(0, 0);
|
2016-06-29 11:37:32 +00:00
|
|
|
poly.push_back(std::move(ring));
|
2016-05-03 16:26:46 +00:00
|
|
|
linear_ring<coord_type> hole;
|
2022-01-26 22:25:53 +00:00
|
|
|
hole.emplace_back(-7, 7);
|
|
|
|
hole.emplace_back(-7, 3);
|
|
|
|
hole.emplace_back(-3, 3);
|
|
|
|
hole.emplace_back(-3, 7);
|
|
|
|
hole.emplace_back(-7, 7);
|
2016-06-29 11:37:32 +00:00
|
|
|
poly.push_back(std::move(hole));
|
2016-05-03 16:26:46 +00:00
|
|
|
geometry<coord_type> geom(poly);
|
|
|
|
mapnik::box2d<coord_type> bbox = mapnik::geometry::envelope(poly);
|
2022-01-26 22:25:53 +00:00
|
|
|
REQUIRE(bbox.minx() == -10);
|
|
|
|
REQUIRE(bbox.miny() == 0);
|
|
|
|
REQUIRE(bbox.maxx() == 0);
|
|
|
|
REQUIRE(bbox.maxy() == 10);
|
2015-04-03 23:56:57 +00:00
|
|
|
// add another hole inside the first hole
|
|
|
|
// which should be considered a hit
|
2016-05-03 16:26:46 +00:00
|
|
|
linear_ring<coord_type> fill;
|
2022-01-26 22:25:53 +00:00
|
|
|
fill.emplace_back(-6, 4);
|
|
|
|
fill.emplace_back(-6, 6);
|
|
|
|
fill.emplace_back(-4, 6);
|
|
|
|
fill.emplace_back(-4, 4);
|
|
|
|
fill.emplace_back(-6, 4);
|
2016-06-29 11:37:32 +00:00
|
|
|
poly.push_back(std::move(fill));
|
2015-04-03 23:56:57 +00:00
|
|
|
bbox = mapnik::geometry::envelope(poly);
|
2022-01-26 22:25:53 +00:00
|
|
|
REQUIRE(bbox.minx() == -10);
|
|
|
|
REQUIRE(bbox.miny() == 0);
|
|
|
|
REQUIRE(bbox.maxx() == 0);
|
|
|
|
REQUIRE(bbox.maxy() == 10);
|
2015-04-03 23:56:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 22:25:53 +00:00
|
|
|
} // namespace
|
2016-05-03 16:26:46 +00:00
|
|
|
|
2022-01-26 22:25:53 +00:00
|
|
|
TEST_CASE("geometry ops - envelope")
|
2016-05-03 16:26:46 +00:00
|
|
|
{
|
2022-01-26 22:25:53 +00:00
|
|
|
SECTION("envelope_test")
|
|
|
|
{
|
|
|
|
envelope_test<int>();
|
|
|
|
envelope_test<double>();
|
|
|
|
envelope_test<float>();
|
|
|
|
}
|
2016-05-03 16:26:46 +00:00
|
|
|
}
|