Fix crash in case of empty ring

This commit is contained in:
Jiri Drbalek 2018-01-17 09:53:45 +00:00
parent 64b874ecb1
commit fe3c2762c0
3 changed files with 162 additions and 1 deletions

View file

@ -47,7 +47,10 @@ struct polygon_vertex_processor
ring.emplace_back(p);
break;
case SEG_CLOSE:
if (!ring.empty())
{
ring.emplace_back(ring.front());
}
if (exterior)
{
polygon_.exterior_ring = std::move(ring);

View file

@ -0,0 +1,65 @@
#include "catch.hpp"
#include "unit/vertex_adapter/fake_path.hpp"
#include <mapnik/geometry/polygon_vertex_processor.hpp>
TEST_CASE("polygon_vertex_processor") {
SECTION("empty polygon") {
fake_path path = {};
mapnik::geometry::polygon_vertex_processor<double> proc;
proc.add_path(path);
CHECK(proc.polygon_.exterior_ring.size() == 0);
CHECK(proc.polygon_.interior_rings.size() == 0);
}
SECTION("empty exterior ring") {
fake_path path = {};
path.vertices_.emplace_back(0, 0, mapnik::SEG_CLOSE);
path.rewind(0);
mapnik::geometry::polygon_vertex_processor<double> proc;
proc.add_path(path);
CHECK(proc.polygon_.exterior_ring.size() == 0);
CHECK(proc.polygon_.interior_rings.size() == 0);
}
SECTION("empty interior ring") {
fake_path path = {};
path.vertices_.emplace_back(-1, -1, mapnik::SEG_MOVETO);
path.vertices_.emplace_back( 1, -1, mapnik::SEG_LINETO);
path.vertices_.emplace_back( 1, 1, mapnik::SEG_LINETO);
path.vertices_.emplace_back(-1, 1, mapnik::SEG_LINETO);
path.vertices_.emplace_back( 0, 0, mapnik::SEG_CLOSE);
path.vertices_.emplace_back( 0, 0, mapnik::SEG_CLOSE);
path.rewind(0);
mapnik::geometry::polygon_vertex_processor<double> proc;
proc.add_path(path);
REQUIRE(proc.polygon_.interior_rings.size() == 1);
REQUIRE(proc.polygon_.interior_rings.front().size() == 0);
auto const& outer_ring = proc.polygon_.exterior_ring;
REQUIRE(outer_ring.size() == 5);
CHECK(outer_ring[0].x == Approx(-1));
CHECK(outer_ring[0].y == Approx(-1));
CHECK(outer_ring[1].x == Approx( 1));
CHECK(outer_ring[1].y == Approx(-1));
CHECK(outer_ring[2].x == Approx( 1));
CHECK(outer_ring[2].y == Approx( 1));
CHECK(outer_ring[3].x == Approx(-1));
CHECK(outer_ring[3].y == Approx( 1));
CHECK(outer_ring[4].x == Approx(-1));
CHECK(outer_ring[4].y == Approx(-1));
}
}

View file

@ -0,0 +1,93 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2017 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
// mapnik
#include <mapnik/vertex.hpp>
// stl
#include <vector>
#include <tuple>
namespace detail
{
template <typename T>
struct fake_path
{
using coord_type = std::tuple<T, T, unsigned>;
using cont_type = std::vector<coord_type>;
cont_type vertices_;
typename cont_type::iterator itr_;
fake_path(std::initializer_list<T> l)
: fake_path(l.begin(), l.size())
{
}
fake_path(std::vector<T> const &v, bool make_invalid = false)
: fake_path(v.begin(), v.size(), make_invalid)
{
}
template <typename Itr>
fake_path(Itr itr, size_t sz, bool make_invalid = false)
{
size_t num_coords = sz >> 1;
vertices_.reserve(num_coords + (make_invalid ? 1 : 0));
if (make_invalid)
{
vertices_.push_back(std::make_tuple(0,0,mapnik::SEG_END));
}
for (size_t i = 0; i < num_coords; ++i)
{
T x = *itr++;
T y = *itr++;
unsigned cmd = (i == 0) ? mapnik::SEG_MOVETO : mapnik::SEG_LINETO;
vertices_.push_back(std::make_tuple(x, y, cmd));
}
itr_ = vertices_.begin();
}
unsigned vertex(T *x, T *y)
{
if (itr_ == vertices_.end())
{
return mapnik::SEG_END;
}
*x = std::get<0>(*itr_);
*y = std::get<1>(*itr_);
unsigned cmd = std::get<2>(*itr_);
++itr_;
return cmd;
}
void rewind(unsigned)
{
itr_ = vertices_.begin();
}
};
}
using fake_path = detail::fake_path<double>;