linear_areal.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2014 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2014 Mateusz Loskot, London, UK.
  5. // Copyright (c) 2013-2014 Adam Wulkiewicz, Lodz, Poland.
  6. // This file was modified by Oracle on 2013-2022.
  7. // Modifications copyright (c) 2013-2022, Oracle and/or its affiliates.
  8. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  9. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  10. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  11. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  12. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  13. // Use, modification and distribution is subject to the Boost Software License,
  14. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt)
  16. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISJOINT_LINEAR_AREAL_HPP
  17. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISJOINT_LINEAR_AREAL_HPP
  18. #include <iterator>
  19. #include <boost/range/begin.hpp>
  20. #include <boost/range/end.hpp>
  21. #include <boost/range/value_type.hpp>
  22. #include <boost/geometry/core/closure.hpp>
  23. #include <boost/geometry/core/point_type.hpp>
  24. #include <boost/geometry/core/ring_type.hpp>
  25. #include <boost/geometry/core/exterior_ring.hpp>
  26. #include <boost/geometry/core/interior_rings.hpp>
  27. #include <boost/geometry/core/tag.hpp>
  28. #include <boost/geometry/core/tag_cast.hpp>
  29. #include <boost/geometry/core/tags.hpp>
  30. #include <boost/geometry/algorithms/detail/covered_by/implementation.hpp>
  31. #include <boost/geometry/algorithms/not_implemented.hpp>
  32. #include <boost/geometry/algorithms/detail/assign_indexed_point.hpp>
  33. #include <boost/geometry/algorithms/detail/point_on_border.hpp>
  34. #include <boost/geometry/algorithms/detail/disjoint/linear_linear.hpp>
  35. #include <boost/geometry/algorithms/detail/disjoint/linear_segment_or_box.hpp>
  36. #include <boost/geometry/algorithms/detail/disjoint/multirange_geometry.hpp>
  37. #include <boost/geometry/algorithms/detail/disjoint/point_box.hpp>
  38. #include <boost/geometry/algorithms/detail/disjoint/segment_box.hpp>
  39. #include <boost/geometry/algorithms/dispatch/disjoint.hpp>
  40. #include <boost/geometry/geometries/helper_geometry.hpp>
  41. namespace boost { namespace geometry
  42. {
  43. #ifndef DOXYGEN_NO_DETAIL
  44. namespace detail { namespace disjoint
  45. {
  46. template <typename Geometry1, typename Geometry2,
  47. typename Tag1 = typename tag<Geometry1>::type,
  48. typename Tag1OrMulti = typename tag_cast<Tag1, multi_tag>::type>
  49. struct disjoint_no_intersections_policy
  50. {
  51. /*!
  52. \tparam Strategy point_in_geometry strategy
  53. */
  54. template <typename Strategy>
  55. static inline bool apply(Geometry1 const& g1, Geometry2 const& g2, Strategy const& strategy)
  56. {
  57. using point_type = typename point_type<Geometry1>::type;
  58. typename helper_geometry<point_type>::type p;
  59. geometry::point_on_border(p, g1);
  60. return ! geometry::covered_by(p, g2, strategy);
  61. }
  62. };
  63. template <typename Geometry1, typename Geometry2, typename Tag1>
  64. struct disjoint_no_intersections_policy<Geometry1, Geometry2, Tag1, multi_tag>
  65. {
  66. /*!
  67. \tparam Strategy point_in_geometry strategy
  68. */
  69. template <typename Strategy>
  70. static inline bool apply(Geometry1 const& g1, Geometry2 const& g2, Strategy const& strategy)
  71. {
  72. // TODO: use partition or rtree on g2
  73. for (auto it = boost::begin(g1); it != boost::end(g1); ++it)
  74. {
  75. typedef typename boost::range_value<Geometry1 const>::type value_type;
  76. if (! disjoint_no_intersections_policy<value_type const, Geometry2>
  77. ::apply(*it, g2, strategy))
  78. {
  79. return false;
  80. }
  81. }
  82. return true;
  83. }
  84. };
  85. template<typename Geometry1, typename Geometry2,
  86. typename NoIntersectionsPolicy
  87. = disjoint_no_intersections_policy<Geometry1, Geometry2> >
  88. struct disjoint_linear_areal
  89. {
  90. /*!
  91. \tparam Strategy relate (segments intersection) strategy
  92. */
  93. template <typename Strategy>
  94. static inline bool apply(Geometry1 const& g1, Geometry2 const& g2, Strategy const& strategy)
  95. {
  96. // if there are intersections - return false
  97. if ( !disjoint_linear<Geometry1, Geometry2>::apply(g1, g2, strategy) )
  98. {
  99. return false;
  100. }
  101. return NoIntersectionsPolicy::apply(g1, g2, strategy);
  102. }
  103. };
  104. template
  105. <
  106. typename Segment,
  107. typename Areal,
  108. typename Tag = typename tag<Areal>::type
  109. >
  110. struct disjoint_segment_areal
  111. : not_implemented<Segment, Areal>
  112. {};
  113. template <typename Segment, typename Polygon>
  114. class disjoint_segment_areal<Segment, Polygon, polygon_tag>
  115. {
  116. template <typename InteriorRings, typename Strategy>
  117. static inline
  118. bool check_interior_rings(InteriorRings const& interior_rings,
  119. Segment const& segment,
  120. Strategy const& strategy)
  121. {
  122. using ring_type = typename boost::range_value<InteriorRings>::type;
  123. using unary_predicate_type = unary_disjoint_geometry_to_query_geometry
  124. <
  125. Segment,
  126. Strategy,
  127. disjoint_range_segment_or_box<ring_type, Segment>
  128. >;
  129. return std::all_of(boost::begin(interior_rings),
  130. boost::end(interior_rings),
  131. unary_predicate_type(segment, strategy));
  132. }
  133. public:
  134. template <typename IntersectionStrategy>
  135. static inline bool apply(Segment const& segment,
  136. Polygon const& polygon,
  137. IntersectionStrategy const& strategy)
  138. {
  139. if (! disjoint_range_segment_or_box
  140. <
  141. typename geometry::ring_type<Polygon>::type,
  142. Segment
  143. >::apply(geometry::exterior_ring(polygon), segment, strategy))
  144. {
  145. return false;
  146. }
  147. if (! check_interior_rings(geometry::interior_rings(polygon), segment, strategy))
  148. {
  149. return false;
  150. }
  151. typename point_type<Segment>::type p;
  152. detail::assign_point_from_index<0>(segment, p);
  153. return ! geometry::covered_by(p, polygon, strategy);
  154. }
  155. };
  156. template <typename Segment, typename MultiPolygon>
  157. struct disjoint_segment_areal<Segment, MultiPolygon, multi_polygon_tag>
  158. {
  159. template <typename IntersectionStrategy>
  160. static inline bool apply(Segment const& segment, MultiPolygon const& multipolygon,
  161. IntersectionStrategy const& strategy)
  162. {
  163. return multirange_constant_size_geometry
  164. <
  165. MultiPolygon, Segment
  166. >::apply(multipolygon, segment, strategy);
  167. }
  168. };
  169. template <typename Segment, typename Ring>
  170. struct disjoint_segment_areal<Segment, Ring, ring_tag>
  171. {
  172. template <typename IntersectionStrategy>
  173. static inline bool apply(Segment const& segment,
  174. Ring const& ring,
  175. IntersectionStrategy const& strategy)
  176. {
  177. if (! disjoint_range_segment_or_box<Ring, Segment>::apply(ring, segment, strategy))
  178. {
  179. return false;
  180. }
  181. typename point_type<Segment>::type p;
  182. detail::assign_point_from_index<0>(segment, p);
  183. return ! geometry::covered_by(p, ring, strategy);
  184. }
  185. };
  186. }} // namespace detail::disjoint
  187. #endif // DOXYGEN_NO_DETAIL
  188. #ifndef DOXYGEN_NO_DISPATCH
  189. namespace dispatch
  190. {
  191. template <typename Linear, typename Areal>
  192. struct disjoint<Linear, Areal, 2, linear_tag, areal_tag, false>
  193. : public detail::disjoint::disjoint_linear_areal<Linear, Areal>
  194. {};
  195. template <typename Areal, typename Linear>
  196. struct disjoint<Areal, Linear, 2, areal_tag, linear_tag, false>
  197. {
  198. template <typename Strategy>
  199. static inline bool apply(Areal const& areal, Linear const& linear,
  200. Strategy const& strategy)
  201. {
  202. return detail::disjoint::disjoint_linear_areal
  203. <
  204. Linear, Areal
  205. >::apply(linear, areal, strategy);
  206. }
  207. };
  208. template <typename Areal, typename Segment>
  209. struct disjoint<Areal, Segment, 2, areal_tag, segment_tag, false>
  210. {
  211. template <typename Strategy>
  212. static inline bool apply(Areal const& g1, Segment const& g2,
  213. Strategy const& strategy)
  214. {
  215. return detail::disjoint::disjoint_segment_areal
  216. <
  217. Segment, Areal
  218. >::apply(g2, g1, strategy);
  219. }
  220. };
  221. template <typename Segment, typename Areal>
  222. struct disjoint<Segment, Areal, 2, segment_tag, areal_tag, false>
  223. : detail::disjoint::disjoint_segment_areal<Segment, Areal>
  224. {};
  225. } // namespace dispatch
  226. #endif // DOXYGEN_NO_DISPATCH
  227. }} // namespace boost::geometry
  228. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISJOINT_LINEAR_AREAL_HPP