1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2015 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2015 Mateusz Loskot, London, UK.
6 // Copyright (c) 2014-2015 Adam Wulkiewicz, Lodz, Poland.
8 // This file was modified by Oracle on 2014, 2015.
9 // Modifications copyright (c) 2014-2015 Oracle and/or its affiliates.
11 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
12 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
14 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
15 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
17 // Use, modification and distribution is subject to the Boost Software License,
18 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
19 // http://www.boost.org/LICENSE_1_0.txt)
21 #ifndef BOOST_GEOMETRY_ALGORITHMS_CENTROID_HPP
22 #define BOOST_GEOMETRY_ALGORITHMS_CENTROID_HPP
27 #include <boost/core/ignore_unused.hpp>
28 #include <boost/range.hpp>
30 #include <boost/variant/apply_visitor.hpp>
31 #include <boost/variant/static_visitor.hpp>
32 #include <boost/variant/variant_fwd.hpp>
34 #include <boost/geometry/core/closure.hpp>
35 #include <boost/geometry/core/cs.hpp>
36 #include <boost/geometry/core/coordinate_dimension.hpp>
37 #include <boost/geometry/core/exception.hpp>
38 #include <boost/geometry/core/exterior_ring.hpp>
39 #include <boost/geometry/core/interior_rings.hpp>
40 #include <boost/geometry/core/tag_cast.hpp>
41 #include <boost/geometry/core/tags.hpp>
42 #include <boost/geometry/core/point_type.hpp>
44 #include <boost/geometry/geometries/concepts/check.hpp>
46 #include <boost/geometry/algorithms/assign.hpp>
47 #include <boost/geometry/algorithms/convert.hpp>
48 #include <boost/geometry/algorithms/detail/interior_iterator.hpp>
49 #include <boost/geometry/algorithms/detail/point_on_border.hpp>
50 #include <boost/geometry/algorithms/not_implemented.hpp>
51 #include <boost/geometry/strategies/centroid.hpp>
52 #include <boost/geometry/strategies/concepts/centroid_concept.hpp>
53 #include <boost/geometry/strategies/default_strategy.hpp>
54 #include <boost/geometry/views/closeable_view.hpp>
56 #include <boost/geometry/util/for_each_coordinate.hpp>
57 #include <boost/geometry/util/select_coordinate_type.hpp>
59 #include <boost/geometry/algorithms/is_empty.hpp>
61 #include <boost/geometry/algorithms/detail/centroid/translating_transformer.hpp>
64 namespace boost { namespace geometry
68 #if ! defined(BOOST_GEOMETRY_CENTROID_NO_THROW)
71 \brief Centroid Exception
73 \details The centroid_exception is thrown if the free centroid function is called with
74 geometries for which the centroid cannot be calculated. For example: a linestring
75 without points, a polygon without points, an empty multi-geometry.
78 \* [link geometry.reference.algorithms.centroid the centroid function]
82 class centroid_exception : public geometry::exception
87 \brief The default constructor
89 inline centroid_exception() {}
92 \brief Returns the explanatory string.
93 \return Pointer to a null-terminated string with explanatory information.
95 virtual char const* what() const throw()
97 return "Boost.Geometry Centroid calculation exception";
104 #ifndef DOXYGEN_NO_DETAIL
105 namespace detail { namespace centroid
108 struct centroid_point
110 template<typename Point, typename PointCentroid, typename Strategy>
111 static inline void apply(Point const& point, PointCentroid& centroid,
114 geometry::convert(point, centroid);
122 std::size_t Dimension = 0,
123 std::size_t DimensionCount = dimension<Indexed>::type::value
125 struct centroid_indexed_calculator
127 typedef typename select_coordinate_type
130 >::type coordinate_type;
131 static inline void apply(Indexed const& indexed, Point& centroid)
133 coordinate_type const c1 = get<min_corner, Dimension>(indexed);
134 coordinate_type const c2 = get<max_corner, Dimension>(indexed);
135 coordinate_type m = c1 + c2;
136 coordinate_type const two = 2;
139 set<Dimension>(centroid, m);
141 centroid_indexed_calculator
143 Indexed, Point, Dimension + 1
144 >::apply(indexed, centroid);
149 template<typename Indexed, typename Point, std::size_t DimensionCount>
150 struct centroid_indexed_calculator<Indexed, Point, DimensionCount, DimensionCount>
152 static inline void apply(Indexed const& , Point& )
158 struct centroid_indexed
160 template<typename Indexed, typename Point, typename Strategy>
161 static inline void apply(Indexed const& indexed, Point& centroid,
164 centroid_indexed_calculator
167 >::apply(indexed, centroid);
172 // There is one thing where centroid is different from e.g. within.
173 // If the ring has only one point, it might make sense that
174 // that point is the centroid.
175 template<typename Point, typename Range>
176 inline bool range_ok(Range const& range, Point& centroid)
178 std::size_t const n = boost::size(range);
185 #if ! defined(BOOST_GEOMETRY_CENTROID_NO_THROW)
186 throw centroid_exception();
193 // Take over the first point in a "coordinate neutral way"
194 geometry::convert(*boost::begin(range), centroid);
197 //return true; // unreachable
201 \brief Calculate the centroid of a Ring or a Linestring.
203 template <closure_selector Closure>
204 struct centroid_range_state
206 template<typename Ring, typename PointTransformer, typename Strategy>
207 static inline void apply(Ring const& ring,
208 PointTransformer const& transformer,
209 Strategy const& strategy,
210 typename Strategy::state_type& state)
212 boost::ignore_unused(strategy);
214 typedef typename geometry::point_type<Ring const>::type point_type;
215 typedef typename closeable_view<Ring const, Closure>::type view_type;
217 typedef typename boost::range_iterator<view_type const>::type iterator_type;
219 view_type view(ring);
220 iterator_type it = boost::begin(view);
221 iterator_type end = boost::end(view);
225 typename PointTransformer::result_type
226 previous_pt = transformer.apply(*it);
228 for ( ++it ; it != end ; ++it)
230 typename PointTransformer::result_type
231 pt = transformer.apply(*it);
233 strategy.apply(static_cast<point_type const&>(previous_pt),
234 static_cast<point_type const&>(pt),
243 template <closure_selector Closure>
244 struct centroid_range
246 template<typename Range, typename Point, typename Strategy>
247 static inline bool apply(Range const& range, Point& centroid,
248 Strategy const& strategy)
250 if (range_ok(range, centroid))
252 // prepare translation transformer
253 translating_transformer<Range> transformer(*boost::begin(range));
255 typename Strategy::state_type state;
256 centroid_range_state<Closure>::apply(range, transformer,
259 if ( strategy.result(state, centroid) )
261 // translate the result back
262 transformer.apply_reverse(centroid);
273 \brief Centroid of a polygon.
274 \note Because outer ring is clockwise, inners are counter clockwise,
275 triangle approach is OK and works for polygons with rings.
277 struct centroid_polygon_state
279 template<typename Polygon, typename PointTransformer, typename Strategy>
280 static inline void apply(Polygon const& poly,
281 PointTransformer const& transformer,
282 Strategy const& strategy,
283 typename Strategy::state_type& state)
285 typedef typename ring_type<Polygon>::type ring_type;
286 typedef centroid_range_state<geometry::closure<ring_type>::value> per_ring;
288 per_ring::apply(exterior_ring(poly), transformer, strategy, state);
290 typename interior_return_type<Polygon const>::type
291 rings = interior_rings(poly);
293 for (typename detail::interior_iterator<Polygon const>::type
294 it = boost::begin(rings); it != boost::end(rings); ++it)
296 per_ring::apply(*it, transformer, strategy, state);
301 struct centroid_polygon
303 template<typename Polygon, typename Point, typename Strategy>
304 static inline bool apply(Polygon const& poly, Point& centroid,
305 Strategy const& strategy)
307 if (range_ok(exterior_ring(poly), centroid))
309 // prepare translation transformer
310 translating_transformer<Polygon>
311 transformer(*boost::begin(exterior_ring(poly)));
313 typename Strategy::state_type state;
314 centroid_polygon_state::apply(poly, transformer, strategy, state);
316 if ( strategy.result(state, centroid) )
318 // translate the result back
319 transformer.apply_reverse(centroid);
330 \brief Building block of a multi-point, to be used as Policy in the
331 more generec centroid_multi
333 struct centroid_multi_point_state
335 template <typename Point, typename PointTransformer, typename Strategy>
336 static inline void apply(Point const& point,
337 PointTransformer const& transformer,
338 Strategy const& strategy,
339 typename Strategy::state_type& state)
341 boost::ignore_unused(strategy);
342 strategy.apply(static_cast<Point const&>(transformer.apply(point)),
349 \brief Generic implementation which calls a policy to calculate the
350 centroid of the total of its single-geometries
351 \details The Policy is, in general, the single-version, with state. So
352 detail::centroid::centroid_polygon_state is used as a policy for this
353 detail::centroid::centroid_multi
356 template <typename Policy>
357 struct centroid_multi
359 template <typename Multi, typename Point, typename Strategy>
360 static inline bool apply(Multi const& multi,
362 Strategy const& strategy)
364 #if ! defined(BOOST_GEOMETRY_CENTROID_NO_THROW)
365 // If there is nothing in any of the ranges, it is not possible
366 // to calculate the centroid
367 if (geometry::is_empty(multi))
369 throw centroid_exception();
373 // prepare translation transformer
374 translating_transformer<Multi> transformer(multi);
376 typename Strategy::state_type state;
378 for (typename boost::range_iterator<Multi const>::type
379 it = boost::begin(multi);
380 it != boost::end(multi);
383 Policy::apply(*it, transformer, strategy, state);
386 if ( strategy.result(state, centroid) )
388 // translate the result back
389 transformer.apply_reverse(centroid);
398 template <typename Algorithm>
399 struct centroid_linear_areal
401 template <typename Geometry, typename Point, typename Strategy>
402 static inline void apply(Geometry const& geom,
404 Strategy const& strategy)
406 if ( ! Algorithm::apply(geom, centroid, strategy) )
408 geometry::point_on_border(centroid, geom);
414 }} // namespace detail::centroid
415 #endif // DOXYGEN_NO_DETAIL
418 #ifndef DOXYGEN_NO_DISPATCH
425 typename Tag = typename tag<Geometry>::type
427 struct centroid: not_implemented<Tag>
430 template <typename Geometry>
431 struct centroid<Geometry, point_tag>
432 : detail::centroid::centroid_point
435 template <typename Box>
436 struct centroid<Box, box_tag>
437 : detail::centroid::centroid_indexed
440 template <typename Segment>
441 struct centroid<Segment, segment_tag>
442 : detail::centroid::centroid_indexed
445 template <typename Ring>
446 struct centroid<Ring, ring_tag>
447 : detail::centroid::centroid_linear_areal
449 detail::centroid::centroid_range<geometry::closure<Ring>::value>
453 template <typename Linestring>
454 struct centroid<Linestring, linestring_tag>
455 : detail::centroid::centroid_linear_areal
457 detail::centroid::centroid_range<closed>
461 template <typename Polygon>
462 struct centroid<Polygon, polygon_tag>
463 : detail::centroid::centroid_linear_areal
465 detail::centroid::centroid_polygon
469 template <typename MultiLinestring>
470 struct centroid<MultiLinestring, multi_linestring_tag>
471 : detail::centroid::centroid_linear_areal
473 detail::centroid::centroid_multi
475 detail::centroid::centroid_range_state<closed>
480 template <typename MultiPolygon>
481 struct centroid<MultiPolygon, multi_polygon_tag>
482 : detail::centroid::centroid_linear_areal
484 detail::centroid::centroid_multi
486 detail::centroid::centroid_polygon_state
491 template <typename MultiPoint>
492 struct centroid<MultiPoint, multi_point_tag>
493 : detail::centroid::centroid_multi
495 detail::centroid::centroid_multi_point_state
500 } // namespace dispatch
501 #endif // DOXYGEN_NO_DISPATCH
504 namespace resolve_strategy {
506 template <typename Geometry>
509 template <typename Point, typename Strategy>
510 static inline void apply(Geometry const& geometry, Point& out, Strategy const& strategy)
512 dispatch::centroid<Geometry>::apply(geometry, out, strategy);
515 template <typename Point>
516 static inline void apply(Geometry const& geometry, Point& out, default_strategy)
518 typedef typename strategy::centroid::services::default_strategy
520 typename cs_tag<Geometry>::type,
523 typename tag<Geometry>::type,
528 dimension<Geometry>::type::value,
531 >::type strategy_type;
533 dispatch::centroid<Geometry>::apply(geometry, out, strategy_type());
537 } // namespace resolve_strategy
540 namespace resolve_variant {
542 template <typename Geometry>
545 template <typename Point, typename Strategy>
546 static inline void apply(Geometry const& geometry, Point& out, Strategy const& strategy)
548 concepts::check_concepts_and_equal_dimensions<Point, Geometry const>();
549 resolve_strategy::centroid<Geometry>::apply(geometry, out, strategy);
553 template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
554 struct centroid<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
556 template <typename Point, typename Strategy>
557 struct visitor: boost::static_visitor<void>
560 Strategy const& m_strategy;
562 visitor(Point& out, Strategy const& strategy)
563 : m_out(out), m_strategy(strategy)
566 template <typename Geometry>
567 void operator()(Geometry const& geometry) const
569 centroid<Geometry>::apply(geometry, m_out, m_strategy);
573 template <typename Point, typename Strategy>
575 apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
577 Strategy const& strategy)
579 boost::apply_visitor(visitor<Point, Strategy>(out, strategy), geometry);
583 } // namespace resolve_variant
587 \brief \brief_calc{centroid} \brief_strategy
589 \details \details_calc{centroid,geometric center (or: center of mass)}. \details_strategy_reasons
590 \tparam Geometry \tparam_geometry
591 \tparam Point \tparam_point
592 \tparam Strategy \tparam_strategy{Centroid}
593 \param geometry \param_geometry
594 \param c \param_point \param_set{centroid}
595 \param strategy \param_strategy{centroid}
597 \qbk{distinguish,with strategy}
598 \qbk{[include reference/algorithms/centroid.qbk]}
599 \qbk{[include reference/algorithms/centroid_strategies.qbk]}
603 template<typename Geometry, typename Point, typename Strategy>
604 inline void centroid(Geometry const& geometry, Point& c,
605 Strategy const& strategy)
607 resolve_variant::centroid<Geometry>::apply(geometry, c, strategy);
612 \brief \brief_calc{centroid}
614 \details \details_calc{centroid,geometric center (or: center of mass)}. \details_default_strategy
615 \tparam Geometry \tparam_geometry
616 \tparam Point \tparam_point
617 \param geometry \param_geometry
618 \param c The calculated centroid will be assigned to this point reference
620 \qbk{[include reference/algorithms/centroid.qbk]}
627 template<typename Geometry, typename Point>
628 inline void centroid(Geometry const& geometry, Point& c)
630 geometry::centroid(geometry, c, default_strategy());
635 \brief \brief_calc{centroid}
637 \details \details_calc{centroid,geometric center (or: center of mass)}. \details_return{centroid}.
638 \tparam Point \tparam_point
639 \tparam Geometry \tparam_geometry
640 \param geometry \param_geometry
641 \return \return_calc{centroid}
643 \qbk{[include reference/algorithms/centroid.qbk]}
645 template<typename Point, typename Geometry>
646 inline Point return_centroid(Geometry const& geometry)
649 geometry::centroid(geometry, c);
654 \brief \brief_calc{centroid} \brief_strategy
656 \details \details_calc{centroid,geometric center (or: center of mass)}. \details_return{centroid}. \details_strategy_reasons
657 \tparam Point \tparam_point
658 \tparam Geometry \tparam_geometry
659 \tparam Strategy \tparam_strategy{centroid}
660 \param geometry \param_geometry
661 \param strategy \param_strategy{centroid}
662 \return \return_calc{centroid}
664 \qbk{distinguish,with strategy}
665 \qbk{[include reference/algorithms/centroid.qbk]}
666 \qbk{[include reference/algorithms/centroid_strategies.qbk]}
668 template<typename Point, typename Geometry, typename Strategy>
669 inline Point return_centroid(Geometry const& geometry, Strategy const& strategy)
672 geometry::centroid(geometry, c, strategy);
677 }} // namespace boost::geometry
680 #endif // BOOST_GEOMETRY_ALGORITHMS_CENTROID_HPP