[length] Add support for DynamicGeometry and GeometryCollection in length().

This commit is contained in:
Adam Wulkiewicz 2021-06-02 01:30:18 +02:00
parent 7adb72e81a
commit e966bdd370
2 changed files with 59 additions and 40 deletions

View File

@ -34,10 +34,13 @@
#include <boost/geometry/algorithms/detail/dummy_geometries.hpp>
#include <boost/geometry/algorithms/detail/multi_sum.hpp>
// #include <boost/geometry/algorithms/detail/throw_on_empty_input.hpp>
#include <boost/geometry/algorithms/detail/visit.hpp>
#include <boost/geometry/core/cs.hpp>
#include <boost/geometry/core/closure.hpp>
#include <boost/geometry/core/tag.hpp>
#include <boost/geometry/core/tags.hpp>
#include <boost/geometry/core/visit.hpp>
#include <boost/geometry/geometries/adapted/boost_variant.hpp> // For backward compatibility
#include <boost/geometry/geometries/concepts/check.hpp>
@ -230,9 +233,9 @@ struct length<default_strategy, false>
} // namespace resolve_strategy
namespace resolve_variant {
namespace resolve_dynamic {
template <typename Geometry>
template <typename Geometry, typename Tag = typename geometry::tag<Geometry>::type>
struct length
{
template <typename Strategy>
@ -243,43 +246,40 @@ struct length
}
};
template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
struct length<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
template <typename Geometry>
struct length<Geometry, dynamic_geometry_tag>
{
typedef typename default_length_result
<
boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>
>::type result_type;
template <typename Strategy>
struct visitor
: static_visitor<result_type>
static inline typename default_length_result<Geometry>::type
apply(Geometry const& geometry, Strategy const& strategy)
{
Strategy const& m_strategy;
visitor(Strategy const& strategy)
: m_strategy(strategy)
{}
template <typename Geometry>
inline typename default_length_result<Geometry>::type
operator()(Geometry const& geometry) const
typename default_length_result<Geometry>::type result = 0;
traits::visit<Geometry>::apply([&](auto const& g)
{
return length<Geometry>::apply(geometry, m_strategy);
}
};
template <typename Strategy>
static inline result_type apply(
variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
Strategy const& strategy
)
{
return boost::apply_visitor(visitor<Strategy>(strategy), geometry);
result = length<util::remove_cref_t<decltype(g)>>::apply(g, strategy);
}, geometry);
return result;
}
};
} // namespace resolve_variant
template <typename Geometry>
struct length<Geometry, geometry_collection_tag>
{
template <typename Strategy>
static inline typename default_length_result<Geometry>::type
apply(Geometry const& geometry, Strategy const& strategy)
{
typename default_length_result<Geometry>::type result = 0;
detail::visit_breadth_first([&](auto const& g)
{
result += length<util::remove_cref_t<decltype(g)>>::apply(g, strategy);
return true;
}, geometry);
return result;
}
};
} // namespace resolve_dynamic
/*!
@ -301,7 +301,7 @@ length(Geometry const& geometry)
// detail::throw_on_empty_input(geometry);
return resolve_variant::length<Geometry>::apply(geometry, default_strategy());
return resolve_dynamic::length<Geometry>::apply(geometry, default_strategy());
}
@ -327,7 +327,7 @@ length(Geometry const& geometry, Strategy const& strategy)
// detail::throw_on_empty_input(geometry);
return resolve_variant::length<Geometry>::apply(geometry, strategy);
return resolve_dynamic::length<Geometry>::apply(geometry, strategy);
}

View File

@ -3,8 +3,9 @@
// Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
// Copyright (c) 2016 Oracle and/or its affiliates.
// Copyright (c) 2016-2021 Oracle and/or its affiliates.
// Contributed and/or modified by Vissarion Fisikopoulos, on behalf of Oracle
// Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
// Use, modification and distribution is subject to the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
@ -16,9 +17,15 @@
#include <geometry_test_common.hpp>
#include <boost/geometry/algorithms/length.hpp>
#ifndef BOOST_GEOMETRY_TEST_DEBUG
#include <boost/geometry/geometries/adapted/boost_variant.hpp>
#include <boost/geometry/geometries/adapted/boost_variant2.hpp>
#include <boost/geometry/geometries/geometry_collection.hpp>
#endif
#include <boost/geometry/io/wkt/wkt.hpp>
#include <boost/geometry/strategies/strategies.hpp>
#include <boost/variant/variant.hpp>
template <typename Geometry>
void test_length(Geometry const& geometry, long double expected_length)
@ -66,8 +73,14 @@ void test_geometry(std::string const& wkt, double expected_length)
Geometry geometry;
bg::read_wkt(wkt, geometry);
test_length(geometry, expected_length);
#if !defined(BOOST_GEOMETRY_TEST_DEBUG)
test_length(boost::variant<Geometry>(geometry), expected_length);
#ifndef BOOST_GEOMETRY_TEST_DEBUG
using variant_t = boost::variant<Geometry>;
using variant2_t = boost::variant2::variant<Geometry>;
using gc_t = bg::model::geometry_collection<variant2_t>;
test_length(variant_t(geometry), expected_length);
test_length(variant2_t(geometry), expected_length);
test_length(gc_t{variant2_t(geometry), variant2_t(geometry)}, expected_length * 2);
#endif
}
@ -77,8 +90,14 @@ void test_geometry(std::string const& wkt, double expected_length, Strategy stra
Geometry geometry;
bg::read_wkt(wkt, geometry);
test_length(geometry, expected_length, strategy);
#if !defined(BOOST_GEOMETRY_TEST_DEBUG)
test_length(boost::variant<Geometry>(geometry), expected_length, strategy);
#ifndef BOOST_GEOMETRY_TEST_DEBUG
using variant_t = boost::variant<Geometry>;
using variant2_t = boost::variant2::variant<Geometry>;
using gc_t = bg::model::geometry_collection<variant2_t>;
test_length(variant_t(geometry), expected_length, strategy);
test_length(variant2_t(geometry), expected_length, strategy);
test_length(gc_t{variant2_t(geometry), variant2_t(geometry)}, expected_length * 2, strategy);
#endif
}