move implementatio into *.cpp + return 'closest_point_result' struct.

This commit is contained in:
Artem Pavlenko 2017-09-22 13:21:50 +02:00
parent afb2af84f7
commit 3d4963f40c
3 changed files with 168 additions and 100 deletions

View file

@ -29,113 +29,20 @@
#include <mapnik/geometry.hpp>
#include <mapnik/geometry/boost_adapters.hpp>
#include <boost/geometry/algorithms/within.hpp>
#include <boost/geometry/algorithms/distance.hpp>
#include <boost/geometry/algorithms/comparable_distance.hpp>
#include <boost/geometry/extensions/algorithms/closest_point.hpp>
namespace mapnik { namespace geometry {
using coordinate_type = double;
namespace detail {
template <typename T>
struct closest_point
struct closest_point_result
{
using coordinate_type = T;
using result_type = boost::geometry::closest_point_result<mapnik::geometry::point<coordinate_type>>;
closest_point(mapnik::geometry::point<coordinate_type> const& pt)
: pt_(pt) {}
result_type operator() (mapnik::geometry::geometry_empty const&) const
{
return result_type(); // FIXME: consider std::optional<result_type>
}
result_type operator() (mapnik::geometry::point<coordinate_type> const& pt) const
{
result_type info;
boost::geometry::closest_point(pt_ ,pt, info);
return info;
}
result_type operator() (mapnik::geometry::line_string<coordinate_type> const& line) const
{
result_type info;
boost::geometry::closest_point(pt_ ,line, info);
return info;
}
result_type operator() (mapnik::geometry::polygon<coordinate_type> const& poly) const
{
result_type info;
if (boost::geometry::within(pt_, poly))
{
info.closest_point = pt_;
info.distance = 0.0;
return info;
}
bool first = true;
for (auto const& ring : poly)
{
result_type ring_info;
boost::geometry::closest_point(pt_ ,ring, ring_info);
if (first)
{
first = false;
info = std::move(ring_info);
}
else if (ring_info.distance < info.distance)
{
info = std::move(ring_info);
}
}
return info;
}
// Multi* + GeometryCollection
result_type operator() (mapnik::geometry::geometry<coordinate_type> const& geom) const
{
return mapnik::util::apply_visitor(*this, geom);
}
template <typename MultiGeometry>
result_type operator() (MultiGeometry const& multi_geom) const
{
result_type info;
bool first = true;
for (auto const& geom : multi_geom)
{
if (first)
{
first = false;
info = std::move(operator()(geom));
}
else
{
auto sub_info = operator()(geom);
if (sub_info.distance < info.distance)
{
info = std::move(sub_info);
}
}
}
return info;
}
mapnik::geometry::point<coordinate_type> pt_;
double x = 0.0;
double y = 0.0;
double distance = -1.0;
};
}
using result_type = closest_point_result;
template <typename T1, typename T2>
inline typename detail::closest_point<T2>::result_type
closest_point(T1 const& geom, mapnik::geometry::point<T2> const& pt)
{
return detail::closest_point<T2>(pt)(geom);
}
MAPNIK_DECL result_type closest_point(T1 const& geom, mapnik::geometry::point<T2> const& pt);
}}

View file

@ -170,6 +170,7 @@ source = Split(
datasource_cache_static.cpp
debug.cpp
geometry/box2d.cpp
geometry/closest_point.cpp
geometry/reprojection.cpp
geometry/envelope.cpp
expression_node.cpp

View file

@ -0,0 +1,160 @@
/*****************************************************************************
*
* 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
*
*****************************************************************************/
#include <boost/version.hpp>
#if BOOST_VERSION >= 106200
#include <mapnik/geometry.hpp>
#include <mapnik/geometry/boost_adapters.hpp>
#include <mapnik/geometry/closest_point.hpp>
#include <boost/geometry/extensions/algorithms/closest_point.hpp>
#include <boost/geometry/algorithms/within.hpp>
namespace mapnik { namespace geometry {
namespace detail {
template <typename T>
struct closest_point
{
using coordinate_type = T;
using info_type = boost::geometry::closest_point_result<mapnik::geometry::point<coordinate_type>>;
closest_point(mapnik::geometry::point<coordinate_type> const& pt)
: pt_(pt) {}
result_type operator() (mapnik::geometry::geometry_empty const&) const
{
return result_type();
}
result_type operator() (mapnik::geometry::point<coordinate_type> const& pt) const
{
info_type info;
boost::geometry::closest_point(pt_, pt, info);
return result_type{info.closest_point.x, info.closest_point.y, info.distance};
}
result_type operator() (mapnik::geometry::line_string<coordinate_type> const& line) const
{
info_type info;
boost::geometry::closest_point(pt_ ,line, info);
return result_type{info.closest_point.x, info.closest_point.y, info.distance};
}
result_type operator() (mapnik::geometry::polygon<coordinate_type> const& poly) const
{
info_type info;
if (boost::geometry::within(pt_, poly))
{
return result_type { pt_.x, pt_.y, 0.0 };
}
bool first = true;
for (auto const& ring : poly)
{
info_type ring_info;
boost::geometry::closest_point(pt_ ,ring, ring_info);
if (first)
{
first = false;
info = std::move(ring_info);
}
else if (ring_info.distance < info.distance)
{
info = std::move(ring_info);
}
}
return result_type{info.closest_point.x, info.closest_point.y, info.distance};
}
// Multi* + GeometryCollection
result_type operator() (mapnik::geometry::geometry<coordinate_type> const& geom) const
{
return mapnik::util::apply_visitor(*this, geom);
}
template <typename MultiGeometry>
result_type operator() (MultiGeometry const& multi_geom) const
{
result_type result;
bool first = true;
for (auto const& geom : multi_geom)
{
if (first)
{
first = false;
result = std::move(operator()(geom));
}
else
{
auto sub_result = operator()(geom);
if (sub_result.distance < result.distance)
{
result = std::move(sub_result);
}
}
}
return result;
}
mapnik::geometry::point<coordinate_type> pt_;
};
}
template <typename T1, typename T2>
MAPNIK_DECL result_type
closest_point(T1 const& geom, mapnik::geometry::point<T2> const& pt)
{
return detail::closest_point<T2>(pt)(geom);
}
template MAPNIK_DECL result_type
closest_point(geometry<double> const&, point<double> const&);
template MAPNIK_DECL result_type
closest_point(point<double> const&, point<double> const&);
template MAPNIK_DECL result_type
closest_point(line_string<double> const&, point<double> const&);
template MAPNIK_DECL result_type
closest_point(polygon<double>const&, point<double> const&);
template MAPNIK_DECL result_type
closest_point(multi_point<double>const&, point<double> const&);
template MAPNIK_DECL result_type
closest_point(multi_line_string<double>const&, point<double> const&);
template MAPNIK_DECL result_type
closest_point(multi_polygon<double>const&, point<double> const&);
template MAPNIK_DECL result_type
closest_point(geometry_empty const&, point<double> const&);
template MAPNIK_DECL result_type
closest_point(geometry_collection<double> const&, point<double> const&);
}}
#endif //