correct_closure.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Boost.Geometry
  2. // Copyright (c) 2017 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Use, modification and distribution is subject to the Boost Software License,
  4. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GEOMETRY_ALGORITHMS_CORRECT_CLOSURE_HPP
  7. #define BOOST_GEOMETRY_ALGORITHMS_CORRECT_CLOSURE_HPP
  8. #include <cstddef>
  9. #include <boost/range.hpp>
  10. #include <boost/variant/apply_visitor.hpp>
  11. #include <boost/variant/static_visitor.hpp>
  12. #include <boost/variant/variant_fwd.hpp>
  13. #include <boost/geometry/algorithms/detail/interior_iterator.hpp>
  14. #include <boost/geometry/core/closure.hpp>
  15. #include <boost/geometry/core/exterior_ring.hpp>
  16. #include <boost/geometry/core/interior_rings.hpp>
  17. #include <boost/geometry/core/ring_type.hpp>
  18. #include <boost/geometry/core/tags.hpp>
  19. #include <boost/geometry/geometries/concepts/check.hpp>
  20. #include <boost/geometry/algorithms/disjoint.hpp>
  21. #include <boost/geometry/algorithms/detail/multi_modify.hpp>
  22. namespace boost { namespace geometry
  23. {
  24. // Silence warning C4127: conditional expression is constant
  25. #if defined(_MSC_VER)
  26. #pragma warning(push)
  27. #pragma warning(disable : 4127)
  28. #endif
  29. #ifndef DOXYGEN_NO_DETAIL
  30. namespace detail { namespace correct_closure
  31. {
  32. template <typename Geometry>
  33. struct nop
  34. {
  35. static inline void apply(Geometry& )
  36. {}
  37. };
  38. // Close a ring, if not closed, or open it
  39. template <typename Ring>
  40. struct close_or_open_ring
  41. {
  42. static inline void apply(Ring& r)
  43. {
  44. if (boost::size(r) <= 2)
  45. {
  46. return;
  47. }
  48. bool const disjoint = geometry::disjoint(*boost::begin(r),
  49. *(boost::end(r) - 1));
  50. closure_selector const s = geometry::closure<Ring>::value;
  51. if (disjoint && s == closed)
  52. {
  53. // Close it by adding first point
  54. geometry::append(r, *boost::begin(r));
  55. }
  56. else if (! disjoint && s != closed)
  57. {
  58. // Open it by removing last point
  59. geometry::traits::resize<Ring>::apply(r, boost::size(r) - 1);
  60. }
  61. }
  62. };
  63. // Close/open exterior ring and all its interior rings
  64. template <typename Polygon>
  65. struct close_or_open_polygon
  66. {
  67. typedef typename ring_type<Polygon>::type ring_type;
  68. static inline void apply(Polygon& poly)
  69. {
  70. close_or_open_ring<ring_type>::apply(exterior_ring(poly));
  71. typename interior_return_type<Polygon>::type
  72. rings = interior_rings(poly);
  73. for (typename detail::interior_iterator<Polygon>::type
  74. it = boost::begin(rings); it != boost::end(rings); ++it)
  75. {
  76. close_or_open_ring<ring_type>::apply(*it);
  77. }
  78. }
  79. };
  80. }} // namespace detail::correct_closure
  81. #endif // DOXYGEN_NO_DETAIL
  82. #ifndef DOXYGEN_NO_DISPATCH
  83. namespace dispatch
  84. {
  85. template <typename Geometry, typename Tag = typename tag<Geometry>::type>
  86. struct correct_closure: not_implemented<Tag>
  87. {};
  88. template <typename Point>
  89. struct correct_closure<Point, point_tag>
  90. : detail::correct_closure::nop<Point>
  91. {};
  92. template <typename LineString>
  93. struct correct_closure<LineString, linestring_tag>
  94. : detail::correct_closure::nop<LineString>
  95. {};
  96. template <typename Segment>
  97. struct correct_closure<Segment, segment_tag>
  98. : detail::correct_closure::nop<Segment>
  99. {};
  100. template <typename Box>
  101. struct correct_closure<Box, box_tag>
  102. : detail::correct_closure::nop<Box>
  103. {};
  104. template <typename Ring>
  105. struct correct_closure<Ring, ring_tag>
  106. : detail::correct_closure::close_or_open_ring<Ring>
  107. {};
  108. template <typename Polygon>
  109. struct correct_closure<Polygon, polygon_tag>
  110. : detail::correct_closure::close_or_open_polygon<Polygon>
  111. {};
  112. template <typename MultiPoint>
  113. struct correct_closure<MultiPoint, multi_point_tag>
  114. : detail::correct_closure::nop<MultiPoint>
  115. {};
  116. template <typename MultiLineString>
  117. struct correct_closure<MultiLineString, multi_linestring_tag>
  118. : detail::correct_closure::nop<MultiLineString>
  119. {};
  120. template <typename Geometry>
  121. struct correct_closure<Geometry, multi_polygon_tag>
  122. : detail::multi_modify
  123. <
  124. Geometry,
  125. detail::correct_closure::close_or_open_polygon
  126. <
  127. typename boost::range_value<Geometry>::type
  128. >
  129. >
  130. {};
  131. } // namespace dispatch
  132. #endif // DOXYGEN_NO_DISPATCH
  133. namespace resolve_variant
  134. {
  135. template <typename Geometry>
  136. struct correct_closure
  137. {
  138. static inline void apply(Geometry& geometry)
  139. {
  140. concepts::check<Geometry const>();
  141. dispatch::correct_closure<Geometry>::apply(geometry);
  142. }
  143. };
  144. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  145. struct correct_closure<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  146. {
  147. struct visitor: boost::static_visitor<void>
  148. {
  149. template <typename Geometry>
  150. void operator()(Geometry& geometry) const
  151. {
  152. correct_closure<Geometry>::apply(geometry);
  153. }
  154. };
  155. static inline void
  156. apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& geometry)
  157. {
  158. visitor vis;
  159. boost::apply_visitor(vis, geometry);
  160. }
  161. };
  162. } // namespace resolve_variant
  163. /*!
  164. \brief Closes or opens a geometry, according to its type
  165. \details Corrects a geometry w.r.t. closure points to all rings which do not
  166. have a closing point and are typed as they should have one, the first point
  167. is appended.
  168. \ingroup correct_closure
  169. \tparam Geometry \tparam_geometry
  170. \param geometry \param_geometry which will be corrected if necessary
  171. */
  172. template <typename Geometry>
  173. inline void correct_closure(Geometry& geometry)
  174. {
  175. resolve_variant::correct_closure<Geometry>::apply(geometry);
  176. }
  177. #if defined(_MSC_VER)
  178. #pragma warning(pop)
  179. #endif
  180. }} // namespace boost::geometry
  181. #endif // BOOST_GEOMETRY_ALGORITHMS_CORRECT_CLOSURE_HPP