correct.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // Copyright (c) 2014-2017 Adam Wulkiewicz, Lodz, Poland.
  6. // This file was modified by Oracle on 2017.
  7. // Modifications copyright (c) 2017 Oracle and/or its affiliates.
  8. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  9. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  10. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  11. // Use, modification and distribution is subject to the Boost Software License,
  12. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. #ifndef BOOST_GEOMETRY_ALGORITHMS_CORRECT_HPP
  15. #define BOOST_GEOMETRY_ALGORITHMS_CORRECT_HPP
  16. #include <algorithm>
  17. #include <cstddef>
  18. #include <functional>
  19. #include <boost/mpl/assert.hpp>
  20. #include <boost/range.hpp>
  21. #include <boost/type_traits/remove_reference.hpp>
  22. #include <boost/variant/apply_visitor.hpp>
  23. #include <boost/variant/static_visitor.hpp>
  24. #include <boost/variant/variant_fwd.hpp>
  25. #include <boost/geometry/algorithms/correct_closure.hpp>
  26. #include <boost/geometry/algorithms/detail/interior_iterator.hpp>
  27. #include <boost/geometry/core/closure.hpp>
  28. #include <boost/geometry/core/cs.hpp>
  29. #include <boost/geometry/core/exterior_ring.hpp>
  30. #include <boost/geometry/core/interior_rings.hpp>
  31. #include <boost/geometry/core/mutable_range.hpp>
  32. #include <boost/geometry/core/ring_type.hpp>
  33. #include <boost/geometry/core/tags.hpp>
  34. #include <boost/geometry/geometries/concepts/check.hpp>
  35. #include <boost/geometry/algorithms/area.hpp>
  36. #include <boost/geometry/algorithms/detail/multi_modify.hpp>
  37. #include <boost/geometry/util/order_as_direction.hpp>
  38. namespace boost { namespace geometry
  39. {
  40. // Silence warning C4127: conditional expression is constant
  41. #if defined(_MSC_VER)
  42. #pragma warning(push)
  43. #pragma warning(disable : 4127)
  44. #endif
  45. #ifndef DOXYGEN_NO_DETAIL
  46. namespace detail { namespace correct
  47. {
  48. template <typename Geometry>
  49. struct correct_nop
  50. {
  51. template <typename Strategy>
  52. static inline void apply(Geometry& , Strategy const& )
  53. {}
  54. };
  55. template <typename Box, std::size_t Dimension, std::size_t DimensionCount>
  56. struct correct_box_loop
  57. {
  58. typedef typename coordinate_type<Box>::type coordinate_type;
  59. static inline void apply(Box& box)
  60. {
  61. if (get<min_corner, Dimension>(box) > get<max_corner, Dimension>(box))
  62. {
  63. // Swap the coordinates
  64. coordinate_type max_value = get<min_corner, Dimension>(box);
  65. coordinate_type min_value = get<max_corner, Dimension>(box);
  66. set<min_corner, Dimension>(box, min_value);
  67. set<max_corner, Dimension>(box, max_value);
  68. }
  69. correct_box_loop
  70. <
  71. Box, Dimension + 1, DimensionCount
  72. >::apply(box);
  73. }
  74. };
  75. template <typename Box, std::size_t DimensionCount>
  76. struct correct_box_loop<Box, DimensionCount, DimensionCount>
  77. {
  78. static inline void apply(Box& )
  79. {}
  80. };
  81. // Correct a box: make min/max correct
  82. template <typename Box>
  83. struct correct_box
  84. {
  85. template <typename Strategy>
  86. static inline void apply(Box& box, Strategy const& )
  87. {
  88. // Currently only for Cartesian coordinates
  89. // (or spherical without crossing dateline)
  90. // Future version: adapt using strategies
  91. correct_box_loop
  92. <
  93. Box, 0, dimension<Box>::type::value
  94. >::apply(box);
  95. }
  96. };
  97. // Close a ring, if not closed
  98. template <typename Ring, template <typename> class Predicate>
  99. struct correct_ring
  100. {
  101. typedef typename point_type<Ring>::type point_type;
  102. typedef typename coordinate_type<Ring>::type coordinate_type;
  103. typedef detail::area::ring_area
  104. <
  105. order_as_direction<geometry::point_order<Ring>::value>::value,
  106. geometry::closure<Ring>::value
  107. > ring_area_type;
  108. template <typename Strategy>
  109. static inline void apply(Ring& r, Strategy const& strategy)
  110. {
  111. // Correct closure if necessary
  112. detail::correct_closure::close_or_open_ring<Ring>::apply(r);
  113. // Check area
  114. typedef typename area_result<Ring, Strategy>::type area_result_type;
  115. Predicate<area_result_type> predicate;
  116. area_result_type const zero = 0;
  117. if (predicate(ring_area_type::apply(r, strategy), zero))
  118. {
  119. std::reverse(boost::begin(r), boost::end(r));
  120. }
  121. }
  122. };
  123. // Correct a polygon: normalizes all rings, sets outer ring clockwise, sets all
  124. // inner rings counter clockwise (or vice versa depending on orientation)
  125. template <typename Polygon>
  126. struct correct_polygon
  127. {
  128. typedef typename ring_type<Polygon>::type ring_type;
  129. template <typename Strategy>
  130. static inline void apply(Polygon& poly, Strategy const& strategy)
  131. {
  132. correct_ring
  133. <
  134. ring_type,
  135. std::less
  136. >::apply(exterior_ring(poly), strategy);
  137. typename interior_return_type<Polygon>::type
  138. rings = interior_rings(poly);
  139. for (typename detail::interior_iterator<Polygon>::type
  140. it = boost::begin(rings); it != boost::end(rings); ++it)
  141. {
  142. correct_ring
  143. <
  144. ring_type,
  145. std::greater
  146. >::apply(*it, strategy);
  147. }
  148. }
  149. };
  150. }} // namespace detail::correct
  151. #endif // DOXYGEN_NO_DETAIL
  152. #ifndef DOXYGEN_NO_DISPATCH
  153. namespace dispatch
  154. {
  155. template <typename Geometry, typename Tag = typename tag<Geometry>::type>
  156. struct correct: not_implemented<Tag>
  157. {};
  158. template <typename Point>
  159. struct correct<Point, point_tag>
  160. : detail::correct::correct_nop<Point>
  161. {};
  162. template <typename LineString>
  163. struct correct<LineString, linestring_tag>
  164. : detail::correct::correct_nop<LineString>
  165. {};
  166. template <typename Segment>
  167. struct correct<Segment, segment_tag>
  168. : detail::correct::correct_nop<Segment>
  169. {};
  170. template <typename Box>
  171. struct correct<Box, box_tag>
  172. : detail::correct::correct_box<Box>
  173. {};
  174. template <typename Ring>
  175. struct correct<Ring, ring_tag>
  176. : detail::correct::correct_ring
  177. <
  178. Ring,
  179. std::less
  180. >
  181. {};
  182. template <typename Polygon>
  183. struct correct<Polygon, polygon_tag>
  184. : detail::correct::correct_polygon<Polygon>
  185. {};
  186. template <typename MultiPoint>
  187. struct correct<MultiPoint, multi_point_tag>
  188. : detail::correct::correct_nop<MultiPoint>
  189. {};
  190. template <typename MultiLineString>
  191. struct correct<MultiLineString, multi_linestring_tag>
  192. : detail::correct::correct_nop<MultiLineString>
  193. {};
  194. template <typename Geometry>
  195. struct correct<Geometry, multi_polygon_tag>
  196. : detail::multi_modify
  197. <
  198. Geometry,
  199. detail::correct::correct_polygon
  200. <
  201. typename boost::range_value<Geometry>::type
  202. >
  203. >
  204. {};
  205. } // namespace dispatch
  206. #endif // DOXYGEN_NO_DISPATCH
  207. namespace resolve_variant {
  208. template <typename Geometry>
  209. struct correct
  210. {
  211. template <typename Strategy>
  212. static inline void apply(Geometry& geometry, Strategy const& strategy)
  213. {
  214. concepts::check<Geometry const>();
  215. dispatch::correct<Geometry>::apply(geometry, strategy);
  216. }
  217. };
  218. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  219. struct correct<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  220. {
  221. template <typename Strategy>
  222. struct visitor: boost::static_visitor<void>
  223. {
  224. Strategy const& m_strategy;
  225. visitor(Strategy const& strategy): m_strategy(strategy) {}
  226. template <typename Geometry>
  227. void operator()(Geometry& geometry) const
  228. {
  229. correct<Geometry>::apply(geometry, m_strategy);
  230. }
  231. };
  232. template <typename Strategy>
  233. static inline void
  234. apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& geometry, Strategy const& strategy)
  235. {
  236. boost::apply_visitor(visitor<Strategy>(strategy), geometry);
  237. }
  238. };
  239. } // namespace resolve_variant
  240. /*!
  241. \brief Corrects a geometry
  242. \details Corrects a geometry: all rings which are wrongly oriented with respect
  243. to their expected orientation are reversed. To all rings which do not have a
  244. closing point and are typed as they should have one, the first point is
  245. appended. Also boxes can be corrected.
  246. \ingroup correct
  247. \tparam Geometry \tparam_geometry
  248. \param geometry \param_geometry which will be corrected if necessary
  249. \qbk{[include reference/algorithms/correct.qbk]}
  250. */
  251. template <typename Geometry>
  252. inline void correct(Geometry& geometry)
  253. {
  254. typedef typename point_type<Geometry>::type point_type;
  255. typedef typename strategy::area::services::default_strategy
  256. <
  257. typename cs_tag<point_type>::type
  258. >::type strategy_type;
  259. resolve_variant::correct<Geometry>::apply(geometry, strategy_type());
  260. }
  261. /*!
  262. \brief Corrects a geometry
  263. \details Corrects a geometry: all rings which are wrongly oriented with respect
  264. to their expected orientation are reversed. To all rings which do not have a
  265. closing point and are typed as they should have one, the first point is
  266. appended. Also boxes can be corrected.
  267. \ingroup correct
  268. \tparam Geometry \tparam_geometry
  269. \tparam Strategy \tparam_strategy{Area}
  270. \param geometry \param_geometry which will be corrected if necessary
  271. \param strategy \param_strategy{area}
  272. \qbk{distinguish,with strategy}
  273. \qbk{[include reference/algorithms/correct.qbk]}
  274. */
  275. template <typename Geometry, typename Strategy>
  276. inline void correct(Geometry& geometry, Strategy const& strategy)
  277. {
  278. resolve_variant::correct<Geometry>::apply(geometry, strategy);
  279. }
  280. #if defined(_MSC_VER)
  281. #pragma warning(pop)
  282. #endif
  283. }} // namespace boost::geometry
  284. #endif // BOOST_GEOMETRY_ALGORITHMS_CORRECT_HPP