mapnik/test/unit/vertex_adapter/fake_path.hpp

87 lines
2.5 KiB
C++
Raw Normal View History

/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
2021-01-05 14:39:07 +00:00
* Copyright (C) 2021 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>
2022-01-26 22:25:53 +00:00
namespace detail {
2022-01-26 22:25:53 +00:00
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())
2022-01-26 22:25:53 +00:00
{}
2022-01-26 22:25:53 +00:00
fake_path(std::vector<T> const& v, bool make_invalid = false)
: fake_path(v.begin(), v.size(), make_invalid)
2022-01-26 22:25:53 +00:00
{}
2022-01-26 22:25:53 +00:00
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)
{
2022-01-26 22:25:53 +00:00
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();
}
2022-01-26 22:25:53 +00:00
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;
}
2022-01-26 22:25:53 +00:00
void rewind(unsigned) { itr_ = vertices_.begin(); }
};
2022-01-26 22:25:53 +00:00
} // namespace detail
using fake_path = detail::fake_path<double>;