Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / geometry / algorithms / perimeter.hpp
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2
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.
6
7 // This file was modified by Oracle on 2014.
8 // Modifications copyright (c) 2014, Oracle and/or its affiliates.
9
10 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
11
12 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
13 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
14
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)
18
19 #ifndef BOOST_GEOMETRY_ALGORITHMS_PERIMETER_HPP
20 #define BOOST_GEOMETRY_ALGORITHMS_PERIMETER_HPP
21
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>
26
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>
38
39 namespace boost { namespace geometry
40 {
41
42 #ifndef DOXYGEN_NO_DISPATCH
43 namespace dispatch
44 {
45
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
49 {
50     typedef typename default_length_result<Geometry>::type return_type;
51
52     template <typename Strategy>
53     static inline return_type apply(Geometry const& geometry, Strategy const& strategy)
54     {
55         return calculate_null::apply<return_type>(geometry, strategy);
56     }
57 };
58
59 template <typename Geometry>
60 struct perimeter<Geometry, ring_tag>
61     : detail::length::range_length
62         <
63             Geometry,
64             closure<Geometry>::value
65         >
66 {};
67
68 template <typename Polygon>
69 struct perimeter<Polygon, polygon_tag> : detail::calculate_polygon_sum
70 {
71     typedef typename default_length_result<Polygon>::type return_type;
72     typedef detail::length::range_length
73                 <
74                     typename ring_type<Polygon>::type,
75                     closure<Polygon>::value
76                 > policy;
77
78     template <typename Strategy>
79     static inline return_type apply(Polygon const& polygon, Strategy const& strategy)
80     {
81         return calculate_polygon_sum::apply<return_type, policy>(polygon, strategy);
82     }
83 };
84
85 template <typename MultiPolygon>
86 struct perimeter<MultiPolygon, multi_polygon_tag> : detail::multi_sum
87 {
88     typedef typename default_length_result<MultiPolygon>::type return_type;
89
90     template <typename Strategy>
91     static inline return_type apply(MultiPolygon const& multi, Strategy const& strategy)
92     {
93         return multi_sum::apply
94                <
95                    return_type,
96                    perimeter<typename boost::range_value<MultiPolygon>::type>
97                >(multi, strategy);
98     }
99 };
100
101
102 // box,n-sphere: to be implemented
103
104 } // namespace dispatch
105 #endif // DOXYGEN_NO_DISPATCH
106
107
108 namespace resolve_strategy {
109
110 struct perimeter
111 {
112     template <typename Geometry, typename Strategy>
113     static inline typename default_length_result<Geometry>::type
114     apply(Geometry const& geometry, Strategy const& strategy)
115     {
116         return dispatch::perimeter<Geometry>::apply(geometry, strategy);
117     }
118
119     template <typename Geometry>
120     static inline typename default_length_result<Geometry>::type
121     apply(Geometry const& geometry, default_strategy)
122     {
123         typedef typename strategy::distance::services::default_strategy
124             <
125                 point_tag, point_tag, typename point_type<Geometry>::type
126             >::type strategy_type;
127
128         return dispatch::perimeter<Geometry>::apply(geometry, strategy_type());
129     }
130 };
131
132 } // namespace resolve_strategy
133
134
135 namespace resolve_variant {
136
137 template <typename Geometry>
138 struct perimeter
139 {
140     template <typename Strategy>
141     static inline typename default_length_result<Geometry>::type
142     apply(Geometry const& geometry, Strategy const& strategy)
143     {
144         concept::check<Geometry const>();
145         return resolve_strategy::perimeter::apply(geometry, strategy);
146     }
147 };
148
149 template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
150 struct perimeter<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
151 {
152     typedef typename default_length_result
153         <
154             boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>
155         >::type result_type;
156
157     template <typename Strategy>
158     struct visitor: boost::static_visitor<result_type>
159     {
160         Strategy const& m_strategy;
161
162         visitor(Strategy const& strategy): m_strategy(strategy) {}
163
164         template <typename Geometry>
165         typename default_length_result<Geometry>::type
166         operator()(Geometry const& geometry) const
167         {
168             return perimeter<Geometry>::apply(geometry, m_strategy);
169         }
170     };
171
172     template <typename Strategy>
173     static inline result_type
174     apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
175           Strategy const& strategy)
176     {
177         return boost::apply_visitor(visitor<Strategy>(strategy), geometry);
178     }
179 };
180
181 } // namespace resolve_variant
182
183
184 /*!
185 \brief \brief_calc{perimeter}
186 \ingroup 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}
192
193 \qbk{[include reference/algorithms/perimeter.qbk]}
194  */
195 template<typename Geometry>
196 inline typename default_length_result<Geometry>::type perimeter(
197         Geometry const& geometry)
198 {
199     // detail::throw_on_empty_input(geometry);
200     return resolve_variant::perimeter<Geometry>::apply(geometry, default_strategy());
201 }
202
203 /*!
204 \brief \brief_calc{perimeter} \brief_strategy
205 \ingroup perimeter
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}
213
214 \qbk{distinguish,with strategy}
215 \qbk{[include reference/algorithms/perimeter.qbk]}
216  */
217 template<typename Geometry, typename Strategy>
218 inline typename default_length_result<Geometry>::type perimeter(
219         Geometry const& geometry, Strategy const& strategy)
220 {
221     // detail::throw_on_empty_input(geometry);
222     return resolve_variant::perimeter<Geometry>::apply(geometry, strategy);
223 }
224
225 }} // namespace boost::geometry
226
227 #endif // BOOST_GEOMETRY_ALGORITHMS_PERIMETER_HPP
228