centroid.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2015 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2015 Mateusz Loskot, London, UK.
  5. // Copyright (c) 2014-2017 Adam Wulkiewicz, Lodz, Poland.
  6. // This file was modified by Oracle on 2014-2020.
  7. // Modifications copyright (c) 2014-2020 Oracle and/or its affiliates.
  8. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  9. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  10. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  11. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  12. // Use, modification and distribution is subject to the Boost Software License,
  13. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. #ifndef BOOST_GEOMETRY_ALGORITHMS_CENTROID_HPP
  16. #define BOOST_GEOMETRY_ALGORITHMS_CENTROID_HPP
  17. #include <cstddef>
  18. #include <boost/core/ignore_unused.hpp>
  19. #include <boost/range/begin.hpp>
  20. #include <boost/range/end.hpp>
  21. #include <boost/range/size.hpp>
  22. #include <boost/throw_exception.hpp>
  23. #include <boost/variant/apply_visitor.hpp>
  24. #include <boost/variant/static_visitor.hpp>
  25. #include <boost/variant/variant_fwd.hpp>
  26. #include <boost/geometry/core/closure.hpp>
  27. #include <boost/geometry/core/cs.hpp>
  28. #include <boost/geometry/core/coordinate_dimension.hpp>
  29. #include <boost/geometry/core/exception.hpp>
  30. #include <boost/geometry/core/exterior_ring.hpp>
  31. #include <boost/geometry/core/interior_rings.hpp>
  32. #include <boost/geometry/core/tag_cast.hpp>
  33. #include <boost/geometry/core/tags.hpp>
  34. #include <boost/geometry/core/point_type.hpp>
  35. #include <boost/geometry/geometries/concepts/check.hpp>
  36. #include <boost/geometry/algorithms/assign.hpp>
  37. #include <boost/geometry/algorithms/convert.hpp>
  38. #include <boost/geometry/algorithms/detail/interior_iterator.hpp>
  39. #include <boost/geometry/algorithms/detail/point_on_border.hpp>
  40. #include <boost/geometry/algorithms/not_implemented.hpp>
  41. #include <boost/geometry/strategies/centroid.hpp>
  42. #include <boost/geometry/strategies/concepts/centroid_concept.hpp>
  43. #include <boost/geometry/strategies/default_strategy.hpp>
  44. #include <boost/geometry/views/closeable_view.hpp>
  45. #include <boost/geometry/util/for_each_coordinate.hpp>
  46. #include <boost/geometry/util/select_coordinate_type.hpp>
  47. #include <boost/geometry/algorithms/is_empty.hpp>
  48. #include <boost/geometry/algorithms/detail/centroid/translating_transformer.hpp>
  49. namespace boost { namespace geometry
  50. {
  51. #if ! defined(BOOST_GEOMETRY_CENTROID_NO_THROW)
  52. /*!
  53. \brief Centroid Exception
  54. \ingroup centroid
  55. \details The centroid_exception is thrown if the free centroid function is called with
  56. geometries for which the centroid cannot be calculated. For example: a linestring
  57. without points, a polygon without points, an empty multi-geometry.
  58. \qbk{
  59. [heading See also]
  60. \* [link geometry.reference.algorithms.centroid the centroid function]
  61. }
  62. */
  63. class centroid_exception : public geometry::exception
  64. {
  65. public:
  66. /*!
  67. \brief The default constructor
  68. */
  69. inline centroid_exception() {}
  70. /*!
  71. \brief Returns the explanatory string.
  72. \return Pointer to a null-terminated string with explanatory information.
  73. */
  74. virtual char const* what() const throw()
  75. {
  76. return "Boost.Geometry Centroid calculation exception";
  77. }
  78. };
  79. #endif
  80. #ifndef DOXYGEN_NO_DETAIL
  81. namespace detail { namespace centroid
  82. {
  83. struct centroid_point
  84. {
  85. template<typename Point, typename PointCentroid, typename Strategy>
  86. static inline void apply(Point const& point, PointCentroid& centroid,
  87. Strategy const&)
  88. {
  89. geometry::convert(point, centroid);
  90. }
  91. };
  92. template
  93. <
  94. typename Indexed,
  95. typename Point,
  96. std::size_t Dimension = 0,
  97. std::size_t DimensionCount = dimension<Indexed>::type::value
  98. >
  99. struct centroid_indexed_calculator
  100. {
  101. typedef typename select_coordinate_type
  102. <
  103. Indexed, Point
  104. >::type coordinate_type;
  105. static inline void apply(Indexed const& indexed, Point& centroid)
  106. {
  107. coordinate_type const c1 = get<min_corner, Dimension>(indexed);
  108. coordinate_type const c2 = get<max_corner, Dimension>(indexed);
  109. coordinate_type m = c1 + c2;
  110. coordinate_type const two = 2;
  111. m /= two;
  112. set<Dimension>(centroid, m);
  113. centroid_indexed_calculator
  114. <
  115. Indexed, Point, Dimension + 1
  116. >::apply(indexed, centroid);
  117. }
  118. };
  119. template<typename Indexed, typename Point, std::size_t DimensionCount>
  120. struct centroid_indexed_calculator<Indexed, Point, DimensionCount, DimensionCount>
  121. {
  122. static inline void apply(Indexed const& , Point& )
  123. {
  124. }
  125. };
  126. struct centroid_indexed
  127. {
  128. template<typename Indexed, typename Point, typename Strategy>
  129. static inline void apply(Indexed const& indexed, Point& centroid,
  130. Strategy const&)
  131. {
  132. centroid_indexed_calculator
  133. <
  134. Indexed, Point
  135. >::apply(indexed, centroid);
  136. }
  137. };
  138. // There is one thing where centroid is different from e.g. within.
  139. // If the ring has only one point, it might make sense that
  140. // that point is the centroid.
  141. template<typename Point, typename Range>
  142. inline bool range_ok(Range const& range, Point& centroid)
  143. {
  144. std::size_t const n = boost::size(range);
  145. if (n > 1)
  146. {
  147. return true;
  148. }
  149. else if (n <= 0)
  150. {
  151. #if ! defined(BOOST_GEOMETRY_CENTROID_NO_THROW)
  152. BOOST_THROW_EXCEPTION(centroid_exception());
  153. #else
  154. return false;
  155. #endif
  156. }
  157. else // if (n == 1)
  158. {
  159. // Take over the first point in a "coordinate neutral way"
  160. geometry::convert(*boost::begin(range), centroid);
  161. return false;
  162. }
  163. //return true; // unreachable
  164. }
  165. /*!
  166. \brief Calculate the centroid of a Ring or a Linestring.
  167. */
  168. template <closure_selector Closure>
  169. struct centroid_range_state
  170. {
  171. template<typename Ring, typename PointTransformer, typename Strategy>
  172. static inline void apply(Ring const& ring,
  173. PointTransformer const& transformer,
  174. Strategy const& strategy,
  175. typename Strategy::state_type& state)
  176. {
  177. boost::ignore_unused(strategy);
  178. typedef typename geometry::point_type<Ring const>::type point_type;
  179. typedef typename closeable_view<Ring const, Closure>::type view_type;
  180. typedef typename boost::range_iterator<view_type const>::type iterator_type;
  181. view_type view(ring);
  182. iterator_type it = boost::begin(view);
  183. iterator_type end = boost::end(view);
  184. if (it != end)
  185. {
  186. typename PointTransformer::result_type
  187. previous_pt = transformer.apply(*it);
  188. for ( ++it ; it != end ; ++it)
  189. {
  190. typename PointTransformer::result_type
  191. pt = transformer.apply(*it);
  192. strategy.apply(static_cast<point_type const&>(previous_pt),
  193. static_cast<point_type const&>(pt),
  194. state);
  195. previous_pt = pt;
  196. }
  197. }
  198. }
  199. };
  200. template <closure_selector Closure>
  201. struct centroid_range
  202. {
  203. template<typename Range, typename Point, typename Strategy>
  204. static inline bool apply(Range const& range, Point& centroid,
  205. Strategy const& strategy)
  206. {
  207. if (range_ok(range, centroid))
  208. {
  209. // prepare translation transformer
  210. translating_transformer<Range> transformer(*boost::begin(range));
  211. typename Strategy::state_type state;
  212. centroid_range_state<Closure>::apply(range, transformer,
  213. strategy, state);
  214. if ( strategy.result(state, centroid) )
  215. {
  216. // translate the result back
  217. transformer.apply_reverse(centroid);
  218. return true;
  219. }
  220. }
  221. return false;
  222. }
  223. };
  224. /*!
  225. \brief Centroid of a polygon.
  226. \note Because outer ring is clockwise, inners are counter clockwise,
  227. triangle approach is OK and works for polygons with rings.
  228. */
  229. struct centroid_polygon_state
  230. {
  231. template<typename Polygon, typename PointTransformer, typename Strategy>
  232. static inline void apply(Polygon const& poly,
  233. PointTransformer const& transformer,
  234. Strategy const& strategy,
  235. typename Strategy::state_type& state)
  236. {
  237. typedef typename ring_type<Polygon>::type ring_type;
  238. typedef centroid_range_state<geometry::closure<ring_type>::value> per_ring;
  239. per_ring::apply(exterior_ring(poly), transformer, strategy, state);
  240. typename interior_return_type<Polygon const>::type
  241. rings = interior_rings(poly);
  242. for (typename detail::interior_iterator<Polygon const>::type
  243. it = boost::begin(rings); it != boost::end(rings); ++it)
  244. {
  245. per_ring::apply(*it, transformer, strategy, state);
  246. }
  247. }
  248. };
  249. struct centroid_polygon
  250. {
  251. template<typename Polygon, typename Point, typename Strategy>
  252. static inline bool apply(Polygon const& poly, Point& centroid,
  253. Strategy const& strategy)
  254. {
  255. if (range_ok(exterior_ring(poly), centroid))
  256. {
  257. // prepare translation transformer
  258. translating_transformer<Polygon>
  259. transformer(*boost::begin(exterior_ring(poly)));
  260. typename Strategy::state_type state;
  261. centroid_polygon_state::apply(poly, transformer, strategy, state);
  262. if ( strategy.result(state, centroid) )
  263. {
  264. // translate the result back
  265. transformer.apply_reverse(centroid);
  266. return true;
  267. }
  268. }
  269. return false;
  270. }
  271. };
  272. /*!
  273. \brief Building block of a multi-point, to be used as Policy in the
  274. more generec centroid_multi
  275. */
  276. struct centroid_multi_point_state
  277. {
  278. template <typename Point, typename PointTransformer, typename Strategy>
  279. static inline void apply(Point const& point,
  280. PointTransformer const& transformer,
  281. Strategy const& strategy,
  282. typename Strategy::state_type& state)
  283. {
  284. boost::ignore_unused(strategy);
  285. strategy.apply(static_cast<Point const&>(transformer.apply(point)),
  286. state);
  287. }
  288. };
  289. /*!
  290. \brief Generic implementation which calls a policy to calculate the
  291. centroid of the total of its single-geometries
  292. \details The Policy is, in general, the single-version, with state. So
  293. detail::centroid::centroid_polygon_state is used as a policy for this
  294. detail::centroid::centroid_multi
  295. */
  296. template <typename Policy>
  297. struct centroid_multi
  298. {
  299. template <typename Multi, typename Point, typename Strategy>
  300. static inline bool apply(Multi const& multi,
  301. Point& centroid,
  302. Strategy const& strategy)
  303. {
  304. #if ! defined(BOOST_GEOMETRY_CENTROID_NO_THROW)
  305. // If there is nothing in any of the ranges, it is not possible
  306. // to calculate the centroid
  307. if (geometry::is_empty(multi))
  308. {
  309. BOOST_THROW_EXCEPTION(centroid_exception());
  310. }
  311. #endif
  312. // prepare translation transformer
  313. translating_transformer<Multi> transformer(multi);
  314. typename Strategy::state_type state;
  315. for (typename boost::range_iterator<Multi const>::type
  316. it = boost::begin(multi);
  317. it != boost::end(multi);
  318. ++it)
  319. {
  320. Policy::apply(*it, transformer, strategy, state);
  321. }
  322. if ( strategy.result(state, centroid) )
  323. {
  324. // translate the result back
  325. transformer.apply_reverse(centroid);
  326. return true;
  327. }
  328. return false;
  329. }
  330. };
  331. template <typename Algorithm>
  332. struct centroid_linear_areal
  333. {
  334. template <typename Geometry, typename Point, typename Strategy>
  335. static inline void apply(Geometry const& geom,
  336. Point& centroid,
  337. Strategy const& strategy)
  338. {
  339. if ( ! Algorithm::apply(geom, centroid, strategy) )
  340. {
  341. geometry::point_on_border(centroid, geom);
  342. }
  343. }
  344. };
  345. }} // namespace detail::centroid
  346. #endif // DOXYGEN_NO_DETAIL
  347. #ifndef DOXYGEN_NO_DISPATCH
  348. namespace dispatch
  349. {
  350. template
  351. <
  352. typename Geometry,
  353. typename Tag = typename tag<Geometry>::type
  354. >
  355. struct centroid: not_implemented<Tag>
  356. {};
  357. template <typename Geometry>
  358. struct centroid<Geometry, point_tag>
  359. : detail::centroid::centroid_point
  360. {};
  361. template <typename Box>
  362. struct centroid<Box, box_tag>
  363. : detail::centroid::centroid_indexed
  364. {};
  365. template <typename Segment>
  366. struct centroid<Segment, segment_tag>
  367. : detail::centroid::centroid_indexed
  368. {};
  369. template <typename Ring>
  370. struct centroid<Ring, ring_tag>
  371. : detail::centroid::centroid_linear_areal
  372. <
  373. detail::centroid::centroid_range<geometry::closure<Ring>::value>
  374. >
  375. {};
  376. template <typename Linestring>
  377. struct centroid<Linestring, linestring_tag>
  378. : detail::centroid::centroid_linear_areal
  379. <
  380. detail::centroid::centroid_range<closed>
  381. >
  382. {};
  383. template <typename Polygon>
  384. struct centroid<Polygon, polygon_tag>
  385. : detail::centroid::centroid_linear_areal
  386. <
  387. detail::centroid::centroid_polygon
  388. >
  389. {};
  390. template <typename MultiLinestring>
  391. struct centroid<MultiLinestring, multi_linestring_tag>
  392. : detail::centroid::centroid_linear_areal
  393. <
  394. detail::centroid::centroid_multi
  395. <
  396. detail::centroid::centroid_range_state<closed>
  397. >
  398. >
  399. {};
  400. template <typename MultiPolygon>
  401. struct centroid<MultiPolygon, multi_polygon_tag>
  402. : detail::centroid::centroid_linear_areal
  403. <
  404. detail::centroid::centroid_multi
  405. <
  406. detail::centroid::centroid_polygon_state
  407. >
  408. >
  409. {};
  410. template <typename MultiPoint>
  411. struct centroid<MultiPoint, multi_point_tag>
  412. : detail::centroid::centroid_multi
  413. <
  414. detail::centroid::centroid_multi_point_state
  415. >
  416. {};
  417. } // namespace dispatch
  418. #endif // DOXYGEN_NO_DISPATCH
  419. namespace resolve_strategy {
  420. template <typename Geometry>
  421. struct centroid
  422. {
  423. template <typename Point, typename Strategy>
  424. static inline void apply(Geometry const& geometry, Point& out, Strategy const& strategy)
  425. {
  426. dispatch::centroid<Geometry>::apply(geometry, out, strategy);
  427. }
  428. template <typename Point>
  429. static inline void apply(Geometry const& geometry, Point& out, default_strategy)
  430. {
  431. typedef typename strategy::centroid::services::default_strategy
  432. <
  433. typename cs_tag<Geometry>::type,
  434. typename tag_cast
  435. <
  436. typename tag<Geometry>::type,
  437. pointlike_tag,
  438. linear_tag,
  439. areal_tag
  440. >::type,
  441. dimension<Geometry>::type::value,
  442. Point,
  443. Geometry
  444. >::type strategy_type;
  445. dispatch::centroid<Geometry>::apply(geometry, out, strategy_type());
  446. }
  447. };
  448. } // namespace resolve_strategy
  449. namespace resolve_variant {
  450. template <typename Geometry>
  451. struct centroid
  452. {
  453. template <typename Point, typename Strategy>
  454. static inline void apply(Geometry const& geometry, Point& out, Strategy const& strategy)
  455. {
  456. concepts::check_concepts_and_equal_dimensions<Point, Geometry const>();
  457. resolve_strategy::centroid<Geometry>::apply(geometry, out, strategy);
  458. }
  459. };
  460. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  461. struct centroid<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  462. {
  463. template <typename Point, typename Strategy>
  464. struct visitor: boost::static_visitor<void>
  465. {
  466. Point& m_out;
  467. Strategy const& m_strategy;
  468. visitor(Point& out, Strategy const& strategy)
  469. : m_out(out), m_strategy(strategy)
  470. {}
  471. template <typename Geometry>
  472. void operator()(Geometry const& geometry) const
  473. {
  474. centroid<Geometry>::apply(geometry, m_out, m_strategy);
  475. }
  476. };
  477. template <typename Point, typename Strategy>
  478. static inline void
  479. apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
  480. Point& out,
  481. Strategy const& strategy)
  482. {
  483. boost::apply_visitor(visitor<Point, Strategy>(out, strategy), geometry);
  484. }
  485. };
  486. } // namespace resolve_variant
  487. /*!
  488. \brief \brief_calc{centroid} \brief_strategy
  489. \ingroup centroid
  490. \details \details_calc{centroid,geometric center (or: center of mass)}. \details_strategy_reasons
  491. \tparam Geometry \tparam_geometry
  492. \tparam Point \tparam_point
  493. \tparam Strategy \tparam_strategy{Centroid}
  494. \param geometry \param_geometry
  495. \param c \param_point \param_set{centroid}
  496. \param strategy \param_strategy{centroid}
  497. \qbk{distinguish,with strategy}
  498. \qbk{[include reference/algorithms/centroid.qbk]}
  499. \qbk{[include reference/algorithms/centroid_strategies.qbk]}
  500. }
  501. */
  502. template<typename Geometry, typename Point, typename Strategy>
  503. inline void centroid(Geometry const& geometry, Point& c,
  504. Strategy const& strategy)
  505. {
  506. resolve_variant::centroid<Geometry>::apply(geometry, c, strategy);
  507. }
  508. /*!
  509. \brief \brief_calc{centroid}
  510. \ingroup centroid
  511. \details \details_calc{centroid,geometric center (or: center of mass)}. \details_default_strategy
  512. \tparam Geometry \tparam_geometry
  513. \tparam Point \tparam_point
  514. \param geometry \param_geometry
  515. \param c The calculated centroid will be assigned to this point reference
  516. \qbk{[include reference/algorithms/centroid.qbk]}
  517. \qbk{
  518. [heading Example]
  519. [centroid]
  520. [centroid_output]
  521. }
  522. */
  523. template<typename Geometry, typename Point>
  524. inline void centroid(Geometry const& geometry, Point& c)
  525. {
  526. geometry::centroid(geometry, c, default_strategy());
  527. }
  528. /*!
  529. \brief \brief_calc{centroid}
  530. \ingroup centroid
  531. \details \details_calc{centroid,geometric center (or: center of mass)}. \details_return{centroid}.
  532. \tparam Point \tparam_point
  533. \tparam Geometry \tparam_geometry
  534. \param geometry \param_geometry
  535. \return \return_calc{centroid}
  536. \qbk{[include reference/algorithms/centroid.qbk]}
  537. */
  538. template<typename Point, typename Geometry>
  539. inline Point return_centroid(Geometry const& geometry)
  540. {
  541. Point c;
  542. geometry::centroid(geometry, c);
  543. return c;
  544. }
  545. /*!
  546. \brief \brief_calc{centroid} \brief_strategy
  547. \ingroup centroid
  548. \details \details_calc{centroid,geometric center (or: center of mass)}. \details_return{centroid}. \details_strategy_reasons
  549. \tparam Point \tparam_point
  550. \tparam Geometry \tparam_geometry
  551. \tparam Strategy \tparam_strategy{centroid}
  552. \param geometry \param_geometry
  553. \param strategy \param_strategy{centroid}
  554. \return \return_calc{centroid}
  555. \qbk{distinguish,with strategy}
  556. \qbk{[include reference/algorithms/centroid.qbk]}
  557. \qbk{[include reference/algorithms/centroid_strategies.qbk]}
  558. */
  559. template<typename Point, typename Geometry, typename Strategy>
  560. inline Point return_centroid(Geometry const& geometry, Strategy const& strategy)
  561. {
  562. Point c;
  563. geometry::centroid(geometry, c, strategy);
  564. return c;
  565. }
  566. }} // namespace boost::geometry
  567. #endif // BOOST_GEOMETRY_ALGORITHMS_CENTROID_HPP