mirror of
https://github.com/boostorg/geometry.git
synced 2025-05-11 05:24:02 +00:00
Made simplify variant-aware
This commit is contained in:
parent
2ddcf5dccf
commit
4919e00e2d
@ -37,6 +37,10 @@
|
|||||||
#include <boost/geometry/algorithms/not_implemented.hpp>
|
#include <boost/geometry/algorithms/not_implemented.hpp>
|
||||||
#include <boost/geometry/algorithms/num_interior_rings.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
|
namespace boost { namespace geometry
|
||||||
{
|
{
|
||||||
@ -316,6 +320,60 @@ struct simplify_insert
|
|||||||
} // namespace resolve_strategy
|
} // 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
|
\brief Simplify a geometry using a specified strategy
|
||||||
\ingroup simplify
|
\ingroup simplify
|
||||||
@ -341,7 +399,7 @@ inline void simplify(Geometry const& geometry, Geometry& out,
|
|||||||
|
|
||||||
geometry::clear(out);
|
geometry::clear(out);
|
||||||
|
|
||||||
resolve_strategy::simplify::apply(geometry, out, max_distance, strategy);
|
resolve_variant::simplify<Geometry>::apply(geometry, out, max_distance, strategy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -17,8 +17,8 @@
|
|||||||
#include <boost/geometry/algorithms/simplify.hpp>
|
#include <boost/geometry/algorithms/simplify.hpp>
|
||||||
#include <boost/geometry/algorithms/distance.hpp>
|
#include <boost/geometry/algorithms/distance.hpp>
|
||||||
#include <boost/geometry/strategies/strategies.hpp>
|
#include <boost/geometry/strategies/strategies.hpp>
|
||||||
|
|
||||||
#include <boost/geometry/io/wkt/wkt.hpp>
|
#include <boost/geometry/io/wkt/wkt.hpp>
|
||||||
|
#include <boost/variant/variant.hpp>
|
||||||
|
|
||||||
template <typename Tag, typename Geometry>
|
template <typename Tag, typename Geometry>
|
||||||
struct test_inserter
|
struct test_inserter
|
||||||
@ -46,24 +46,43 @@ struct test_inserter<bg::linestring_tag, Geometry>
|
|||||||
|
|
||||||
|
|
||||||
template <typename 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;
|
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);
|
|
||||||
bg::simplify(geometry, simplified, distance);
|
bg::simplify(geometry, simplified, distance);
|
||||||
|
|
||||||
{
|
std::ostringstream out;
|
||||||
std::ostringstream out;
|
out << std::setprecision(12) << bg::wkt(simplified);
|
||||||
out << std::setprecision(12) << bg::wkt(simplified);
|
BOOST_CHECK_EQUAL(out.str(), expected);
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_CHECK_MESSAGE(out.str() == expected,
|
template <typename Geometry, typename Strategy>
|
||||||
"simplify: " << bg::wkt(geometry)
|
void check_geometry(Geometry const& geometry,
|
||||||
<< " expected " << expected
|
std::string const& expected,
|
||||||
<< " got " << out.str());
|
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
|
// Check using user-specified strategy
|
||||||
typedef typename bg::point_type<Geometry>::type point_type;
|
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;
|
> simplify_strategy_type;
|
||||||
|
|
||||||
BOOST_CONCEPT_ASSERT( (bg::concept::SimplifyStrategy<simplify_strategy_type, point_type>) );
|
BOOST_CONCEPT_ASSERT( (bg::concept::SimplifyStrategy<simplify_strategy_type, point_type>) );
|
||||||
bg::simplify(geometry, simplified, distance, simplify_strategy_type());
|
|
||||||
|
|
||||||
{
|
check_geometry(geometry, expected, distance, simplify_strategy_type());
|
||||||
std::ostringstream out;
|
check_geometry(v, expected, distance, simplify_strategy_type());
|
||||||
out << std::setprecision(12) << bg::wkt(simplified);
|
|
||||||
BOOST_CHECK_EQUAL(out.str(), expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check inserter (if applicable)
|
// Check inserter (if applicable)
|
||||||
test_inserter
|
test_inserter
|
||||||
|
Loading…
x
Reference in New Issue
Block a user