Chris@16: // Boost.Geometry (aka GGL, Generic Geometry Library) Chris@16: Chris@16: // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands. Chris@16: // Copyright (c) 2008-2012 Bruno Lalande, Paris, France. Chris@16: // Copyright (c) 2009-2012 Mateusz Loskot, London, UK. Chris@16: Chris@16: // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library Chris@16: // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands. Chris@16: Chris@16: // Use, modification and distribution is subject to the Boost Software License, Chris@16: // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_GEOMETRY_ALGORITHMS_LENGTH_HPP Chris@16: #define BOOST_GEOMETRY_ALGORITHMS_LENGTH_HPP Chris@16: Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: // #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: Chris@16: namespace boost { namespace geometry Chris@16: { Chris@16: Chris@16: Chris@16: #ifndef DOXYGEN_NO_DETAIL Chris@16: namespace detail { namespace length Chris@16: { Chris@16: Chris@16: Chris@16: template Chris@16: struct segment_length Chris@16: { Chris@16: template Chris@16: static inline typename default_length_result::type apply( Chris@16: Segment const& segment, Strategy const& strategy) Chris@16: { Chris@16: typedef typename point_type::type point_type; Chris@16: point_type p1, p2; Chris@16: geometry::detail::assign_point_from_index<0>(segment, p1); Chris@16: geometry::detail::assign_point_from_index<1>(segment, p2); Chris@16: return strategy.apply(p1, p2); Chris@16: } Chris@16: }; Chris@16: Chris@16: /*! Chris@16: \brief Internal, calculates length of a linestring using iterator pairs and Chris@16: specified strategy Chris@16: \note for_each could be used here, now that point_type is changed by boost Chris@16: range iterator Chris@16: */ Chris@16: template Chris@16: struct range_length Chris@16: { Chris@16: typedef typename default_length_result::type return_type; Chris@16: Chris@16: template Chris@16: static inline return_type apply( Chris@16: Range const& range, Strategy const& strategy) Chris@16: { Chris@16: boost::ignore_unused_variable_warning(strategy); Chris@16: typedef typename closeable_view::type view_type; Chris@16: typedef typename boost::range_iterator Chris@16: < Chris@16: view_type const Chris@16: >::type iterator_type; Chris@16: Chris@16: return_type sum = return_type(); Chris@16: view_type view(range); Chris@16: iterator_type it = boost::begin(view), end = boost::end(view); Chris@16: if(it != end) Chris@16: { Chris@16: for(iterator_type previous = it++; Chris@16: it != end; Chris@16: ++previous, ++it) Chris@16: { Chris@16: // Add point-point distance using the return type belonging Chris@16: // to strategy Chris@16: sum += strategy.apply(*previous, *it); Chris@16: } Chris@16: } Chris@16: Chris@16: return sum; Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: }} // namespace detail::length Chris@16: #endif // DOXYGEN_NO_DETAIL Chris@16: Chris@16: Chris@16: #ifndef DOXYGEN_NO_DISPATCH Chris@16: namespace dispatch Chris@16: { Chris@16: Chris@16: Chris@16: template ::type> Chris@16: struct length : detail::calculate_null Chris@16: { Chris@16: typedef typename default_length_result::type return_type; Chris@16: Chris@16: template Chris@16: static inline return_type apply(Geometry const& geometry, Strategy const& strategy) Chris@16: { Chris@16: return calculate_null::apply(geometry, strategy); Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: template Chris@16: struct length Chris@16: : detail::length::range_length Chris@16: {}; Chris@16: Chris@16: Chris@16: // RING: length is currently 0; it might be argued that it is the "perimeter" Chris@16: Chris@16: Chris@16: template Chris@16: struct length Chris@16: : detail::length::segment_length Chris@16: {}; Chris@16: Chris@16: Chris@16: template Chris@16: struct devarianted_length Chris@16: { Chris@16: typedef typename default_length_result::type result_type; Chris@16: Chris@16: template Chris@16: static inline result_type apply(Geometry const& geometry, Chris@16: Strategy const& strategy) Chris@16: { Chris@16: return length::apply(geometry, strategy); Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: struct devarianted_length > Chris@16: { Chris@16: typedef typename mpl::fold< Chris@16: typename mpl::transform< Chris@16: typename variant::types, Chris@16: default_length_result Chris@16: >::type, Chris@16: mpl::set0<>, Chris@16: mpl::insert Chris@16: >::type possible_result_types; Chris@16: Chris@16: typedef typename mpl::if_< Chris@16: mpl::greater< Chris@16: mpl::size, Chris@16: mpl::int_<1> Chris@16: >, Chris@16: typename make_variant_over::type, Chris@16: typename mpl::front::type Chris@16: >::type result_type; Chris@16: Chris@16: template Chris@16: struct visitor Chris@16: : static_visitor Chris@16: { Chris@16: Strategy const& m_strategy; Chris@16: Chris@16: visitor(Strategy const& strategy) Chris@16: : m_strategy(strategy) Chris@16: {} Chris@16: Chris@16: template Chris@16: inline typename devarianted_length::result_type Chris@16: operator()(Geometry const& geometry) const Chris@16: { Chris@16: return devarianted_length::apply(geometry, m_strategy); Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: static inline result_type apply( Chris@16: variant const& geometry, Chris@16: Strategy const& strategy Chris@16: ) Chris@16: { Chris@16: return apply_visitor(visitor(strategy), geometry); Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: } // namespace dispatch Chris@16: #endif // DOXYGEN_NO_DISPATCH Chris@16: Chris@16: Chris@16: /*! Chris@16: \brief \brief_calc{length} Chris@16: \ingroup length Chris@16: \details \details_calc{length, length (the sum of distances between consecutive points)}. \details_default_strategy Chris@16: \tparam Geometry \tparam_geometry Chris@16: \param geometry \param_geometry Chris@16: \return \return_calc{length} Chris@16: Chris@16: \qbk{[include reference/algorithms/length.qbk]} Chris@16: \qbk{[length] [length_output]} Chris@16: */ Chris@16: template Chris@16: inline typename dispatch::devarianted_length::result_type Chris@16: length(Geometry const& geometry) Chris@16: { Chris@16: concept::check(); Chris@16: Chris@16: // detail::throw_on_empty_input(geometry); Chris@16: Chris@16: typedef typename strategy::distance::services::default_strategy Chris@16: < Chris@16: point_tag, typename point_type::type Chris@16: >::type strategy_type; Chris@16: Chris@16: return dispatch::devarianted_length::apply(geometry, strategy_type()); Chris@16: } Chris@16: Chris@16: Chris@16: /*! Chris@16: \brief \brief_calc{length} \brief_strategy Chris@16: \ingroup length Chris@16: \details \details_calc{length, length (the sum of distances between consecutive points)} \brief_strategy. \details_strategy_reasons Chris@16: \tparam Geometry \tparam_geometry Chris@16: \tparam Strategy \tparam_strategy{distance} Chris@16: \param geometry \param_geometry Chris@16: \param strategy \param_strategy{distance} Chris@16: \return \return_calc{length} Chris@16: Chris@16: \qbk{distinguish,with strategy} Chris@16: \qbk{[include reference/algorithms/length.qbk]} Chris@16: \qbk{[length_with_strategy] [length_with_strategy_output]} Chris@16: */ Chris@16: template Chris@16: inline typename dispatch::devarianted_length::result_type Chris@16: length(Geometry const& geometry, Strategy const& strategy) Chris@16: { Chris@16: concept::check(); Chris@16: Chris@16: // detail::throw_on_empty_input(geometry); Chris@16: Chris@16: return dispatch::devarianted_length::apply(geometry, strategy); Chris@16: } Chris@16: Chris@16: Chris@16: }} // namespace boost::geometry Chris@16: Chris@16: #endif // BOOST_GEOMETRY_ALGORITHMS_LENGTH_HPP