add 'closest_point' geometry algorithm
This commit is contained in:
parent
e9f912bba9
commit
143d1e57cf
4 changed files with 575 additions and 0 deletions
221
deps/boost/geometry/extensions/algorithms/closest_point.hpp
vendored
Normal file
221
deps/boost/geometry/extensions/algorithms/closest_point.hpp
vendored
Normal file
|
@ -0,0 +1,221 @@
|
|||
// Boost.Geometry (aka GGL, Generic Geometry Library)
|
||||
|
||||
// Copyright (c) 2007-2013 Barend Gehrels, Amsterdam, the Netherlands.
|
||||
// Copyright (c) 2008-2013 Bruno Lalande, Paris, France.
|
||||
// Copyright (c) 2009-2013 Mateusz Loskot, London, UK.
|
||||
// Copyright (c) 2013 Adam Wulkiewicz, Lodz, Poland.
|
||||
// Copyright (c) 2017 Artem Pavlenko, UK
|
||||
|
||||
// Use, modification and distribution is subject to the Boost Software License,
|
||||
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_GEOMETRY_ALGORITHMS_CLOSEST_POINT_HPP
|
||||
#define BOOST_GEOMETRY_ALGORITHMS_CLOSEST_POINT_HPP
|
||||
|
||||
#include <boost/mpl/if.hpp>
|
||||
#include <boost/range/functions.hpp>
|
||||
#include <boost/range/metafunctions.hpp>
|
||||
#include <boost/static_assert.hpp>
|
||||
|
||||
#include <boost/geometry/core/access.hpp>
|
||||
#include <boost/geometry/core/coordinate_dimension.hpp>
|
||||
#include <boost/geometry/core/reverse_dispatch.hpp>
|
||||
|
||||
#include <boost/geometry/algorithms/not_implemented.hpp>
|
||||
#include <boost/geometry/algorithms/detail/throw_on_empty_input.hpp>
|
||||
|
||||
#include <boost/geometry/geometries/concepts/check.hpp>
|
||||
|
||||
#include <boost/geometry/strategies/distance.hpp>
|
||||
#include <boost/geometry/strategies/default_distance_result.hpp>
|
||||
|
||||
#include <boost/geometry/strategies/cartesian/distance_pythagoras.hpp>
|
||||
#include <boost/geometry/extensions/strategies/cartesian/closest_point.hpp>
|
||||
|
||||
#include <boost/geometry/util/math.hpp>
|
||||
|
||||
|
||||
namespace boost { namespace geometry
|
||||
{
|
||||
|
||||
#ifndef DOXYGEN_NO_DETAIL
|
||||
namespace detail { namespace closest_point {
|
||||
|
||||
|
||||
template <typename Point1, typename Point2>
|
||||
struct point_point
|
||||
{
|
||||
template <typename Strategy, typename Result>
|
||||
static inline void apply(Point1 const& point1, Point2 const& point2, Strategy const& strategy, Result& result)
|
||||
{
|
||||
result.distance
|
||||
= strategy.apply_point_point(point1, point2);
|
||||
// The projected point makes not really sense in point-point.
|
||||
// We just assign one on the other
|
||||
geometry::convert(point1, result.closest_point);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Point, typename Range>
|
||||
struct point_range
|
||||
{
|
||||
template <typename Strategy, typename Result>
|
||||
static inline void apply(Point const& point, Range const& range, Strategy const& strategy, Result& result)
|
||||
{
|
||||
// This should not occur (see exception on empty input below)
|
||||
if (boost::begin(range) == boost::end(range))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// line of one point: same case as point-point
|
||||
typedef typename boost::range_const_iterator<Range>::type iterator_type;
|
||||
iterator_type it = boost::begin(range);
|
||||
iterator_type prev = it++;
|
||||
if (it == boost::end(range))
|
||||
{
|
||||
point_point<Point, typename boost::range_value<Range const>::type>::apply(point, *prev, strategy, result);
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialize with first segment
|
||||
strategy.apply(point, *prev, *it, result);
|
||||
|
||||
// Check other segments
|
||||
for(prev = it++; it != boost::end(range); prev = it++)
|
||||
{
|
||||
Result other;
|
||||
strategy.apply(point, *prev, *it, other);
|
||||
if (other.distance < result.distance)
|
||||
{
|
||||
result = other;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
}} // namespace detail::closest_point
|
||||
#endif // DOXYGEN_NO_DETAIL
|
||||
|
||||
|
||||
#ifndef DOXYGEN_NO_DISPATCH
|
||||
namespace dispatch
|
||||
{
|
||||
|
||||
template
|
||||
<
|
||||
typename Geometry1, typename Geometry2,
|
||||
typename Tag1 = typename tag<Geometry1>::type,
|
||||
typename Tag2 = typename tag<Geometry2>::type,
|
||||
bool Reverse = reverse_dispatch<Geometry1, Geometry2>::type::value
|
||||
>
|
||||
struct closest_point : not_implemented<Tag1, Tag2>
|
||||
{
|
||||
};
|
||||
|
||||
|
||||
template
|
||||
<
|
||||
typename Geometry1, typename Geometry2,
|
||||
typename Tag1, typename Tag2
|
||||
>
|
||||
struct closest_point<Geometry1, Geometry2, Tag1, Tag2, true>
|
||||
{
|
||||
template <typename Strategy, typename Result>
|
||||
static inline void apply(Geometry1 const& geometry1, Geometry2 const& geometry2,
|
||||
Strategy const& strategy, Result& result)
|
||||
{
|
||||
// Reversed version just calls dispatch with reversed arguments
|
||||
closest_point
|
||||
<
|
||||
Geometry2, Geometry1, Tag2, Tag1, false
|
||||
>::apply(geometry2, geometry1, strategy, result);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
template<typename Point1, typename Point2>
|
||||
struct closest_point
|
||||
<
|
||||
Point1, Point2,
|
||||
point_tag, point_tag, false
|
||||
> : public detail::closest_point::point_point<Point1, Point2>
|
||||
{};
|
||||
|
||||
|
||||
template<typename Point, typename Segment>
|
||||
struct closest_point
|
||||
<
|
||||
Point, Segment,
|
||||
point_tag, segment_tag,
|
||||
false
|
||||
>
|
||||
{
|
||||
template <typename Strategy, typename Result>
|
||||
static inline void apply(Point const& point, Segment const& segment,
|
||||
Strategy const& strategy, Result& result)
|
||||
{
|
||||
|
||||
typename point_type<Segment>::type p[2];
|
||||
geometry::detail::assign_point_from_index<0>(segment, p[0]);
|
||||
geometry::detail::assign_point_from_index<1>(segment, p[1]);
|
||||
|
||||
strategy.apply(point, p[0], p[1], result);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template <typename Point, typename Ring>
|
||||
struct closest_point<Point, Ring, point_tag, ring_tag, false>
|
||||
: detail::closest_point::point_range<Point, Ring>
|
||||
{};
|
||||
|
||||
//
|
||||
template<typename Point, typename Linestring>
|
||||
struct closest_point
|
||||
<
|
||||
Point, Linestring,
|
||||
point_tag, linestring_tag,
|
||||
false
|
||||
>
|
||||
: detail::closest_point::point_range<Point, Linestring>
|
||||
{};
|
||||
|
||||
|
||||
} // namespace dispatch
|
||||
#endif // DOXYGEN_NO_DISPATCH
|
||||
|
||||
|
||||
|
||||
template <typename Geometry1, typename Geometry2, typename Result>
|
||||
inline void closest_point(Geometry1 const& geometry1, Geometry2 const& geometry2, Result& result)
|
||||
{
|
||||
concepts::check<Geometry1 const>();
|
||||
concepts::check<Geometry2 const>();
|
||||
concepts::check<typename Result::point_type>();
|
||||
|
||||
assert_dimension_equal<Geometry1, Geometry2>();
|
||||
assert_dimension_equal<Geometry1, typename Result::point_type>();
|
||||
|
||||
detail::throw_on_empty_input(geometry1);
|
||||
detail::throw_on_empty_input(geometry2);
|
||||
|
||||
strategy::distance::calculate_closest_point<> info_strategy;
|
||||
|
||||
|
||||
dispatch::closest_point
|
||||
<
|
||||
Geometry1,
|
||||
Geometry2
|
||||
>::apply(geometry1, geometry2, info_strategy, result);
|
||||
}
|
||||
|
||||
|
||||
}} // namespace boost::geometry
|
||||
|
||||
#endif // BOOST_GEOMETRY_ALGORITHMS_CLOSEST_POINT_HPP
|
164
deps/boost/geometry/extensions/strategies/cartesian/closest_point.hpp
vendored
Normal file
164
deps/boost/geometry/extensions/strategies/cartesian/closest_point.hpp
vendored
Normal file
|
@ -0,0 +1,164 @@
|
|||
// Boost.Geometry (aka GGL, Generic Geometry Library)
|
||||
|
||||
// Copyright (c) 2007-2013 Barend Gehrels, Amsterdam, the Netherlands.
|
||||
// Copyright (c) 2008-2013 Bruno Lalande, Paris, France.
|
||||
// Copyright (c) 2009-2013 Mateusz Loskot, London, UK.
|
||||
// Copyright (c) 2013 Adam Wulkiewicz, Lodz, Poland.
|
||||
// Copyright (c) 2017 Artem Pavlenko, UK
|
||||
|
||||
// Use, modification and distribution is subject to the Boost Software License,
|
||||
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_GEOMETRY_STRATEGY_CARTESIAN_CLOSEST_POINT_HPP
|
||||
#define BOOST_GEOMETRY_STRATEGY_CARTESIAN_CLOSEST_POINT_HPP
|
||||
|
||||
#include <boost/type_traits/remove_const.hpp>
|
||||
|
||||
#include <boost/geometry/core/access.hpp>
|
||||
#include <boost/geometry/core/point_type.hpp>
|
||||
|
||||
#include <boost/geometry/algorithms/convert.hpp>
|
||||
#include <boost/geometry/arithmetic/arithmetic.hpp>
|
||||
#include <boost/geometry/arithmetic/dot_product.hpp>
|
||||
|
||||
#include <boost/geometry/strategies/tags.hpp>
|
||||
#include <boost/geometry/strategies/distance.hpp>
|
||||
#include <boost/geometry/strategies/default_distance_result.hpp>
|
||||
#include <boost/geometry/strategies/cartesian/distance_pythagoras.hpp>
|
||||
|
||||
#include <boost/geometry/util/select_coordinate_type.hpp>
|
||||
|
||||
#include <boost/geometry/algorithms/detail/overlay/segment_identifier.hpp>
|
||||
|
||||
|
||||
namespace boost { namespace geometry
|
||||
{
|
||||
|
||||
template <typename Point>
|
||||
struct closest_point_result
|
||||
{
|
||||
typedef Point point_type;
|
||||
typedef typename default_distance_result<Point>::type distance_type;
|
||||
distance_type distance;
|
||||
Point closest_point;
|
||||
|
||||
inline closest_point_result()
|
||||
: distance(distance_type())
|
||||
{}
|
||||
};
|
||||
|
||||
|
||||
namespace strategy { namespace distance
|
||||
{
|
||||
|
||||
template
|
||||
<
|
||||
typename CalculationType = void,
|
||||
typename Strategy = pythagoras<CalculationType>
|
||||
>
|
||||
struct calculate_closest_point
|
||||
{
|
||||
public :
|
||||
// The three typedefs below are necessary to calculate distances
|
||||
// from segments defined in integer coordinates.
|
||||
|
||||
// Integer coordinates can still result in FP distances.
|
||||
// There is a division, which must be represented in FP.
|
||||
// So promote.
|
||||
template <typename Point, typename PointOfSegment>
|
||||
struct calculation_type
|
||||
: promote_floating_point
|
||||
<
|
||||
typename strategy::distance::services::return_type
|
||||
<
|
||||
Strategy,
|
||||
Point,
|
||||
PointOfSegment
|
||||
>::type
|
||||
>
|
||||
{};
|
||||
|
||||
|
||||
public :
|
||||
|
||||
// Helper function
|
||||
template <typename Point1, typename Point2>
|
||||
inline typename calculation_type<Point1, Point2>::type
|
||||
apply_point_point(Point1 const& p1, Point2 const& p2) const
|
||||
{
|
||||
Strategy point_point_strategy;
|
||||
boost::ignore_unused_variable_warning(point_point_strategy);
|
||||
return point_point_strategy.apply(p1, p2);
|
||||
}
|
||||
|
||||
template <typename Point, typename PointOfSegment, typename Result>
|
||||
inline void apply(Point const& p,
|
||||
PointOfSegment const& p1, PointOfSegment const& p2,
|
||||
Result& result) const
|
||||
{
|
||||
assert_dimension_equal<Point, PointOfSegment>();
|
||||
|
||||
typedef typename calculation_type<Point, PointOfSegment>::type calculation_type;
|
||||
|
||||
//// A projected point of points in Integer coordinates must be able to be
|
||||
//// represented in FP.
|
||||
typedef model::point
|
||||
<
|
||||
calculation_type,
|
||||
dimension<PointOfSegment>::value,
|
||||
typename coordinate_system<PointOfSegment>::type
|
||||
> fp_point_type;
|
||||
|
||||
// For convenience
|
||||
typedef fp_point_type fp_vector_type;
|
||||
fp_vector_type v, w;
|
||||
|
||||
geometry::convert(p2, v);
|
||||
geometry::convert(p, w);
|
||||
subtract_point(v, p1);
|
||||
subtract_point(w, p1);
|
||||
|
||||
calculation_type const zero = calculation_type();
|
||||
|
||||
calculation_type const c1 = dot_product(w, v);
|
||||
calculation_type const c2 = dot_product(v, v);
|
||||
|
||||
if (c1 < zero)
|
||||
{
|
||||
result.distance = apply_point_point(p, p1);
|
||||
result.closest_point = p1;
|
||||
}
|
||||
else if(c1 > c2)
|
||||
{
|
||||
result.distance = apply_point_point(p, p2);
|
||||
result.closest_point = p2;
|
||||
}
|
||||
else
|
||||
{
|
||||
Strategy point_point_strategy;
|
||||
boost::ignore_unused_variable_warning(point_point_strategy);
|
||||
|
||||
if (geometry::math::equals(c2, zero))
|
||||
{
|
||||
geometry::convert(p1, result.closest_point);
|
||||
result.distance = apply_point_point(p, p1);
|
||||
return;
|
||||
}
|
||||
|
||||
calculation_type const b = c1 / c2;
|
||||
geometry::convert(p1, result.closest_point);
|
||||
multiply_value(v, b);
|
||||
add_point(result.closest_point, v);
|
||||
result.distance = apply_point_point(p, result.closest_point);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}} // namespace strategy::distance
|
||||
|
||||
|
||||
}} // namespace boost::geometry
|
||||
|
||||
|
||||
#endif // BOOST_GEOMETRY_STRATEGY_CARTESIAN_CLOSEST_POINT_HPP
|
144
include/mapnik/geometry/closest_point.hpp
Normal file
144
include/mapnik/geometry/closest_point.hpp
Normal file
|
@ -0,0 +1,144 @@
|
|||
/*****************************************************************************
|
||||
*
|
||||
* 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
|
||||
*
|
||||
*****************************************************************************/
|
||||
|
||||
#ifndef MAPNIK_GEOMETRY_CLOSEST_POINT_HPP
|
||||
#define MAPNIK_GEOMETRY_CLOSEST_POINT_HPP
|
||||
|
||||
#include <boost/version.hpp>
|
||||
|
||||
#if BOOST_VERSION >= 106200
|
||||
|
||||
#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 {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename T>
|
||||
struct closest_point
|
||||
{
|
||||
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_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
|
||||
#endif //
|
||||
#endif // MAPNIK_GEOMETRY_CLOSEST_POINT_HPP
|
46
test/unit/geometry/closest_point.cpp
Normal file
46
test/unit/geometry/closest_point.cpp
Normal file
|
@ -0,0 +1,46 @@
|
|||
#include "catch.hpp"
|
||||
|
||||
#include <mapnik/geometry/closest_point.hpp>
|
||||
|
||||
TEST_CASE("geometry closest point") {
|
||||
|
||||
SECTION("point") {
|
||||
|
||||
mapnik::geometry::point<double> pt(0, 0);
|
||||
mapnik::geometry::point<double> geom(3.0, 4.0);
|
||||
auto result = mapnik::geometry::closest_point(pt, geom);
|
||||
REQUIRE(result.closest_point == geom);
|
||||
REQUIRE(result.distance == 5.0);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
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));
|
||||
|
||||
//mapnik::geometry::point<double> centroid;
|
||||
//REQUIRE(mapnik::geometry::centroid(poly, centroid));
|
||||
///REQUIRE(centroid.x == 0.5);
|
||||
//REQUIRE(centroid.y == 0.5);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue