1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2014 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2014 Mateusz Loskot, London, UK.
7 // This file was modified by Oracle on 2014.
8 // Modifications copyright (c) 2014, Oracle and/or its affiliates.
10 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
12 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
13 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
15 // Use, modification and distribution is subject to the Boost Software License,
16 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
17 // http://www.boost.org/LICENSE_1_0.txt)
19 #ifndef BOOST_GEOMETRY_ALGORITHMS_PERIMETER_HPP
20 #define BOOST_GEOMETRY_ALGORITHMS_PERIMETER_HPP
22 #include <boost/range/metafunctions.hpp>
23 #include <boost/variant/static_visitor.hpp>
24 #include <boost/variant/apply_visitor.hpp>
25 #include <boost/variant/variant_fwd.hpp>
27 #include <boost/geometry/algorithms/length.hpp>
28 #include <boost/geometry/algorithms/detail/calculate_null.hpp>
29 #include <boost/geometry/algorithms/detail/calculate_sum.hpp>
30 #include <boost/geometry/algorithms/detail/multi_sum.hpp>
31 // #include <boost/geometry/algorithms/detail/throw_on_empty_input.hpp>
32 #include <boost/geometry/core/cs.hpp>
33 #include <boost/geometry/core/closure.hpp>
34 #include <boost/geometry/core/tags.hpp>
35 #include <boost/geometry/geometries/concepts/check.hpp>
36 #include <boost/geometry/strategies/default_length_result.hpp>
37 #include <boost/geometry/strategies/default_strategy.hpp>
39 namespace boost { namespace geometry
42 #ifndef DOXYGEN_NO_DISPATCH
46 // Default perimeter is 0.0, specializations implement calculated values
47 template <typename Geometry, typename Tag = typename tag<Geometry>::type>
48 struct perimeter : detail::calculate_null
50 typedef typename default_length_result<Geometry>::type return_type;
52 template <typename Strategy>
53 static inline return_type apply(Geometry const& geometry, Strategy const& strategy)
55 return calculate_null::apply<return_type>(geometry, strategy);
59 template <typename Geometry>
60 struct perimeter<Geometry, ring_tag>
61 : detail::length::range_length
64 closure<Geometry>::value
68 template <typename Polygon>
69 struct perimeter<Polygon, polygon_tag> : detail::calculate_polygon_sum
71 typedef typename default_length_result<Polygon>::type return_type;
72 typedef detail::length::range_length
74 typename ring_type<Polygon>::type,
75 closure<Polygon>::value
78 template <typename Strategy>
79 static inline return_type apply(Polygon const& polygon, Strategy const& strategy)
81 return calculate_polygon_sum::apply<return_type, policy>(polygon, strategy);
85 template <typename MultiPolygon>
86 struct perimeter<MultiPolygon, multi_polygon_tag> : detail::multi_sum
88 typedef typename default_length_result<MultiPolygon>::type return_type;
90 template <typename Strategy>
91 static inline return_type apply(MultiPolygon const& multi, Strategy const& strategy)
93 return multi_sum::apply
96 perimeter<typename boost::range_value<MultiPolygon>::type>
102 // box,n-sphere: to be implemented
104 } // namespace dispatch
105 #endif // DOXYGEN_NO_DISPATCH
108 namespace resolve_strategy {
112 template <typename Geometry, typename Strategy>
113 static inline typename default_length_result<Geometry>::type
114 apply(Geometry const& geometry, Strategy const& strategy)
116 return dispatch::perimeter<Geometry>::apply(geometry, strategy);
119 template <typename Geometry>
120 static inline typename default_length_result<Geometry>::type
121 apply(Geometry const& geometry, default_strategy)
123 typedef typename strategy::distance::services::default_strategy
125 point_tag, point_tag, typename point_type<Geometry>::type
126 >::type strategy_type;
128 return dispatch::perimeter<Geometry>::apply(geometry, strategy_type());
132 } // namespace resolve_strategy
135 namespace resolve_variant {
137 template <typename Geometry>
140 template <typename Strategy>
141 static inline typename default_length_result<Geometry>::type
142 apply(Geometry const& geometry, Strategy const& strategy)
144 concept::check<Geometry const>();
145 return resolve_strategy::perimeter::apply(geometry, strategy);
149 template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
150 struct perimeter<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
152 typedef typename default_length_result
154 boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>
157 template <typename Strategy>
158 struct visitor: boost::static_visitor<result_type>
160 Strategy const& m_strategy;
162 visitor(Strategy const& strategy): m_strategy(strategy) {}
164 template <typename Geometry>
165 typename default_length_result<Geometry>::type
166 operator()(Geometry const& geometry) const
168 return perimeter<Geometry>::apply(geometry, m_strategy);
172 template <typename Strategy>
173 static inline result_type
174 apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
175 Strategy const& strategy)
177 return boost::apply_visitor(visitor<Strategy>(strategy), geometry);
181 } // namespace resolve_variant
185 \brief \brief_calc{perimeter}
187 \details The function perimeter returns the perimeter of a geometry,
188 using the default distance-calculation-strategy
189 \tparam Geometry \tparam_geometry
190 \param geometry \param_geometry
191 \return \return_calc{perimeter}
193 \qbk{[include reference/algorithms/perimeter.qbk]}
195 template<typename Geometry>
196 inline typename default_length_result<Geometry>::type perimeter(
197 Geometry const& geometry)
199 // detail::throw_on_empty_input(geometry);
200 return resolve_variant::perimeter<Geometry>::apply(geometry, default_strategy());
204 \brief \brief_calc{perimeter} \brief_strategy
206 \details The function perimeter returns the perimeter of a geometry,
207 using specified strategy
208 \tparam Geometry \tparam_geometry
209 \tparam Strategy \tparam_strategy{distance}
210 \param geometry \param_geometry
211 \param strategy strategy to be used for distance calculations.
212 \return \return_calc{perimeter}
214 \qbk{distinguish,with strategy}
215 \qbk{[include reference/algorithms/perimeter.qbk]}
217 template<typename Geometry, typename Strategy>
218 inline typename default_length_result<Geometry>::type perimeter(
219 Geometry const& geometry, Strategy const& strategy)
221 // detail::throw_on_empty_input(geometry);
222 return resolve_variant::perimeter<Geometry>::apply(geometry, strategy);
225 }} // namespace boost::geometry
227 #endif // BOOST_GEOMETRY_ALGORITHMS_PERIMETER_HPP