mirror of
https://github.com/boostorg/geometry.git
synced 2026-02-12 00:02:09 +00:00
Merge branch 'feature/variants' into develop
This commit is contained in:
@@ -30,12 +30,17 @@
|
||||
#include <boost/geometry/geometries/concepts/check.hpp>
|
||||
#include <boost/geometry/strategies/agnostic/simplify_douglas_peucker.hpp>
|
||||
#include <boost/geometry/strategies/concepts/simplify_concept.hpp>
|
||||
#include <boost/geometry/strategies/default_strategy.hpp>
|
||||
|
||||
#include <boost/geometry/algorithms/clear.hpp>
|
||||
#include <boost/geometry/algorithms/convert.hpp>
|
||||
#include <boost/geometry/algorithms/not_implemented.hpp>
|
||||
#include <boost/geometry/algorithms/num_interior_rings.hpp>
|
||||
|
||||
#include <boost/variant/static_visitor.hpp>
|
||||
#include <boost/variant/apply_visitor.hpp>
|
||||
#include <boost/variant/variant_fwd.hpp>
|
||||
|
||||
|
||||
namespace boost { namespace geometry
|
||||
{
|
||||
@@ -229,6 +234,146 @@ struct simplify_insert<Ring, ring_tag>
|
||||
#endif // DOXYGEN_NO_DISPATCH
|
||||
|
||||
|
||||
namespace resolve_strategy
|
||||
{
|
||||
|
||||
struct simplify
|
||||
{
|
||||
template <typename Geometry, typename Distance, typename Strategy>
|
||||
static inline void apply(Geometry const& geometry,
|
||||
Geometry& out,
|
||||
Distance const& max_distance,
|
||||
Strategy const& strategy)
|
||||
{
|
||||
dispatch::simplify<Geometry>::apply(geometry, out, max_distance, strategy);
|
||||
}
|
||||
|
||||
template <typename Geometry, typename Distance>
|
||||
static inline void apply(Geometry const& geometry,
|
||||
Geometry& out,
|
||||
Distance const& max_distance,
|
||||
default_strategy)
|
||||
{
|
||||
typedef typename point_type<Geometry>::type point_type;
|
||||
|
||||
typedef typename strategy::distance::services::default_strategy
|
||||
<
|
||||
segment_tag, point_type
|
||||
>::type ds_strategy_type;
|
||||
|
||||
typedef strategy::simplify::douglas_peucker
|
||||
<
|
||||
point_type, ds_strategy_type
|
||||
> strategy_type;
|
||||
|
||||
BOOST_CONCEPT_ASSERT(
|
||||
(concept::SimplifyStrategy<strategy_type, point_type>)
|
||||
);
|
||||
|
||||
apply(geometry, out, max_distance, strategy_type());
|
||||
}
|
||||
};
|
||||
|
||||
struct simplify_insert
|
||||
{
|
||||
template
|
||||
<
|
||||
typename Geometry,
|
||||
typename OutputIterator,
|
||||
typename Distance,
|
||||
typename Strategy
|
||||
>
|
||||
static inline void apply(Geometry const& geometry,
|
||||
OutputIterator& out,
|
||||
Distance const& max_distance,
|
||||
Strategy const& strategy)
|
||||
{
|
||||
dispatch::simplify_insert<Geometry>::apply(geometry, out, max_distance, strategy);
|
||||
}
|
||||
|
||||
template <typename Geometry, typename OutputIterator, typename Distance>
|
||||
static inline void apply(Geometry const& geometry,
|
||||
OutputIterator& out,
|
||||
Distance const& max_distance,
|
||||
default_strategy)
|
||||
{
|
||||
typedef typename point_type<Geometry>::type point_type;
|
||||
|
||||
typedef typename strategy::distance::services::default_strategy
|
||||
<
|
||||
segment_tag, point_type
|
||||
>::type ds_strategy_type;
|
||||
|
||||
typedef strategy::simplify::douglas_peucker
|
||||
<
|
||||
point_type, ds_strategy_type
|
||||
> strategy_type;
|
||||
|
||||
BOOST_CONCEPT_ASSERT(
|
||||
(concept::SimplifyStrategy<strategy_type, point_type>)
|
||||
);
|
||||
|
||||
apply(geometry, out, max_distance, strategy_type());
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace resolve_strategy
|
||||
|
||||
|
||||
namespace resolve_variant {
|
||||
|
||||
template <typename Geometry>
|
||||
struct simplify
|
||||
{
|
||||
template <typename Distance, typename Strategy>
|
||||
static inline void apply(Geometry const& geometry,
|
||||
Geometry& out,
|
||||
Distance const& max_distance,
|
||||
Strategy const& strategy)
|
||||
{
|
||||
resolve_strategy::simplify::apply(geometry, out, max_distance, strategy);
|
||||
}
|
||||
};
|
||||
|
||||
template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
|
||||
struct simplify<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
|
||||
{
|
||||
template <typename Distance, typename Strategy>
|
||||
struct visitor: boost::static_visitor<void>
|
||||
{
|
||||
Distance const& m_max_distance;
|
||||
Strategy const& m_strategy;
|
||||
|
||||
visitor(Distance const& max_distance, Strategy const& strategy)
|
||||
: m_max_distance(max_distance)
|
||||
, m_strategy(strategy)
|
||||
{}
|
||||
|
||||
template <typename Geometry>
|
||||
void operator()(Geometry const& geometry, Geometry& out) const
|
||||
{
|
||||
simplify<Geometry>::apply(geometry, out, m_max_distance, m_strategy);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Distance, typename Strategy>
|
||||
static inline void
|
||||
apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
|
||||
boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& out,
|
||||
Distance const& max_distance,
|
||||
Strategy const& strategy)
|
||||
{
|
||||
boost::apply_visitor(
|
||||
visitor<Distance, Strategy>(max_distance, strategy),
|
||||
geometry,
|
||||
out
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace resolve_variant
|
||||
|
||||
|
||||
/*!
|
||||
\brief Simplify a geometry using a specified strategy
|
||||
\ingroup simplify
|
||||
@@ -252,13 +397,9 @@ inline void simplify(Geometry const& geometry, Geometry& out,
|
||||
{
|
||||
concept::check<Geometry>();
|
||||
|
||||
BOOST_CONCEPT_ASSERT(
|
||||
(concept::SimplifyStrategy<Strategy, typename point_type<Geometry>::type>)
|
||||
);
|
||||
|
||||
geometry::clear(out);
|
||||
|
||||
dispatch::simplify<Geometry>::apply(geometry, out, max_distance, strategy);
|
||||
resolve_variant::simplify<Geometry>::apply(geometry, out, max_distance, strategy);
|
||||
}
|
||||
|
||||
|
||||
@@ -285,17 +426,8 @@ inline void simplify(Geometry const& geometry, Geometry& out,
|
||||
concept::check<Geometry>();
|
||||
|
||||
typedef typename point_type<Geometry>::type point_type;
|
||||
typedef typename strategy::distance::services::default_strategy
|
||||
<
|
||||
segment_tag, point_type
|
||||
>::type ds_strategy_type;
|
||||
|
||||
typedef strategy::simplify::douglas_peucker
|
||||
<
|
||||
point_type, ds_strategy_type
|
||||
> strategy_type;
|
||||
|
||||
simplify(geometry, out, max_distance, strategy_type());
|
||||
simplify(geometry, out, max_distance, default_strategy());
|
||||
}
|
||||
|
||||
|
||||
@@ -321,14 +453,11 @@ namespace detail { namespace simplify
|
||||
*/
|
||||
template<typename Geometry, typename OutputIterator, typename Distance, typename Strategy>
|
||||
inline void simplify_insert(Geometry const& geometry, OutputIterator out,
|
||||
Distance const& max_distance, Strategy const& strategy)
|
||||
Distance const& max_distance, Strategy const& strategy)
|
||||
{
|
||||
concept::check<Geometry const>();
|
||||
BOOST_CONCEPT_ASSERT(
|
||||
(concept::SimplifyStrategy<Strategy, typename point_type<Geometry>::type>)
|
||||
);
|
||||
|
||||
dispatch::simplify_insert<Geometry>::apply(geometry, out, max_distance, strategy);
|
||||
resolve_strategy::simplify_insert::apply(geometry, out, max_distance, strategy);
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -344,25 +473,13 @@ inline void simplify_insert(Geometry const& geometry, OutputIterator out,
|
||||
*/
|
||||
template<typename Geometry, typename OutputIterator, typename Distance>
|
||||
inline void simplify_insert(Geometry const& geometry, OutputIterator out,
|
||||
Distance const& max_distance)
|
||||
Distance const& max_distance)
|
||||
{
|
||||
typedef typename point_type<Geometry>::type point_type;
|
||||
|
||||
// Concept: output point type = point type of input geometry
|
||||
concept::check<Geometry const>();
|
||||
concept::check<point_type>();
|
||||
concept::check<typename point_type<Geometry>::type>();
|
||||
|
||||
typedef typename strategy::distance::services::default_strategy
|
||||
<
|
||||
segment_tag, point_type
|
||||
>::type ds_strategy_type;
|
||||
|
||||
typedef strategy::simplify::douglas_peucker
|
||||
<
|
||||
point_type, ds_strategy_type
|
||||
> strategy_type;
|
||||
|
||||
dispatch::simplify_insert<Geometry>::apply(geometry, out, max_distance, strategy_type());
|
||||
simplify_insert(geometry, out, max_distance, default_strategy());
|
||||
}
|
||||
|
||||
}} // namespace detail::simplify
|
||||
|
||||
@@ -17,8 +17,8 @@
|
||||
#include <boost/geometry/algorithms/simplify.hpp>
|
||||
#include <boost/geometry/algorithms/distance.hpp>
|
||||
#include <boost/geometry/strategies/strategies.hpp>
|
||||
|
||||
#include <boost/geometry/io/wkt/wkt.hpp>
|
||||
#include <boost/variant/variant.hpp>
|
||||
|
||||
template <typename Tag, typename Geometry>
|
||||
struct test_inserter
|
||||
@@ -46,24 +46,43 @@ struct test_inserter<bg::linestring_tag, Geometry>
|
||||
|
||||
|
||||
template <typename Geometry>
|
||||
void test_geometry(std::string const& wkt, std::string const& expected, double distance)
|
||||
void check_geometry(Geometry const& geometry,
|
||||
std::string const& expected,
|
||||
double distance)
|
||||
{
|
||||
Geometry geometry, simplified;
|
||||
|
||||
// Generate polygon using only integer coordinates and obvious results
|
||||
// Polygon is a hexagon, having one extra point (2,1) on a line which should be filtered out.
|
||||
bg::read_wkt(wkt, geometry);
|
||||
Geometry simplified;
|
||||
bg::simplify(geometry, simplified, distance);
|
||||
|
||||
{
|
||||
std::ostringstream out;
|
||||
out << std::setprecision(12) << bg::wkt(simplified);
|
||||
std::ostringstream out;
|
||||
out << std::setprecision(12) << bg::wkt(simplified);
|
||||
BOOST_CHECK_EQUAL(out.str(), expected);
|
||||
}
|
||||
|
||||
BOOST_CHECK_MESSAGE(out.str() == expected,
|
||||
"simplify: " << bg::wkt(geometry)
|
||||
<< " expected " << expected
|
||||
<< " got " << out.str());
|
||||
}
|
||||
template <typename Geometry, typename Strategy>
|
||||
void check_geometry(Geometry const& geometry,
|
||||
std::string const& expected,
|
||||
double distance,
|
||||
Strategy const& strategy)
|
||||
{
|
||||
Geometry simplified;
|
||||
bg::simplify(geometry, simplified, distance, strategy);
|
||||
|
||||
std::ostringstream out;
|
||||
out << std::setprecision(12) << bg::wkt(simplified);
|
||||
BOOST_CHECK_EQUAL(out.str(), expected);
|
||||
}
|
||||
|
||||
template <typename Geometry>
|
||||
void test_geometry(std::string const& wkt, std::string const& expected, double distance)
|
||||
{
|
||||
// Generate polygon using only integer coordinates and obvious results
|
||||
// Polygon is a hexagon, having one extra point (2,1) on a line which should be filtered out.
|
||||
Geometry geometry;
|
||||
bg::read_wkt(wkt, geometry);
|
||||
boost::variant<Geometry> v(geometry);
|
||||
|
||||
check_geometry(geometry, expected, distance);
|
||||
check_geometry(v, expected, distance);
|
||||
|
||||
// Check using user-specified strategy
|
||||
typedef typename bg::point_type<Geometry>::type point_type;
|
||||
@@ -76,13 +95,9 @@ void test_geometry(std::string const& wkt, std::string const& expected, double d
|
||||
> simplify_strategy_type;
|
||||
|
||||
BOOST_CONCEPT_ASSERT( (bg::concept::SimplifyStrategy<simplify_strategy_type, point_type>) );
|
||||
bg::simplify(geometry, simplified, distance, simplify_strategy_type());
|
||||
|
||||
{
|
||||
std::ostringstream out;
|
||||
out << std::setprecision(12) << bg::wkt(simplified);
|
||||
BOOST_CHECK_EQUAL(out.str(), expected);
|
||||
}
|
||||
check_geometry(geometry, expected, distance, simplify_strategy_type());
|
||||
check_geometry(v, expected, distance, simplify_strategy_type());
|
||||
|
||||
// Check inserter (if applicable)
|
||||
test_inserter
|
||||
|
||||
Reference in New Issue
Block a user