implementation.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. // This file was modified by Oracle on 2014-2022.
  6. // Modifications copyright (c) 2014-2022 Oracle and/or its affiliates.
  7. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  8. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  9. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  10. // Use, modification and distribution is subject to the Boost Software License,
  11. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAPS_IMPLEMENTATION_HPP
  14. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAPS_IMPLEMENTATION_HPP
  15. #include <cstddef>
  16. #include <boost/geometry/core/access.hpp>
  17. #include <boost/geometry/algorithms/detail/gc_topological_dimension.hpp>
  18. #include <boost/geometry/algorithms/detail/overlaps/interface.hpp>
  19. #include <boost/geometry/algorithms/detail/relate/implementation.hpp>
  20. #include <boost/geometry/algorithms/detail/relate/implementation_gc.hpp>
  21. #include <boost/geometry/algorithms/not_implemented.hpp>
  22. #include <boost/geometry/geometries/concepts/check.hpp>
  23. #include <boost/geometry/strategies/relate/cartesian.hpp>
  24. #include <boost/geometry/strategies/relate/geographic.hpp>
  25. #include <boost/geometry/strategies/relate/spherical.hpp>
  26. #include <boost/geometry/views/detail/geometry_collection_view.hpp>
  27. namespace boost { namespace geometry
  28. {
  29. #ifndef DOXYGEN_NO_DETAIL
  30. namespace detail { namespace overlaps
  31. {
  32. template
  33. <
  34. std::size_t Dimension,
  35. std::size_t DimensionCount
  36. >
  37. struct box_box_loop
  38. {
  39. template <typename Box1, typename Box2>
  40. static inline void apply(Box1 const& b1, Box2 const& b2,
  41. bool& overlaps, bool& one_in_two, bool& two_in_one)
  42. {
  43. assert_dimension_equal<Box1, Box2>();
  44. typedef typename coordinate_type<Box1>::type coordinate_type1;
  45. typedef typename coordinate_type<Box2>::type coordinate_type2;
  46. coordinate_type1 const& min1 = get<min_corner, Dimension>(b1);
  47. coordinate_type1 const& max1 = get<max_corner, Dimension>(b1);
  48. coordinate_type2 const& min2 = get<min_corner, Dimension>(b2);
  49. coordinate_type2 const& max2 = get<max_corner, Dimension>(b2);
  50. // We might use the (not yet accepted) Boost.Interval
  51. // submission in the future
  52. // If:
  53. // B1: |-------|
  54. // B2: |------|
  55. // in any dimension -> no overlap
  56. if (max1 <= min2 || min1 >= max2)
  57. {
  58. overlaps = false;
  59. return;
  60. }
  61. // If:
  62. // B1: |--------------------|
  63. // B2: |-------------|
  64. // in all dimensions -> within, then no overlap
  65. // B1: |--------------------|
  66. // B2: |-------------|
  67. // this is "within-touch" -> then no overlap. So use < and >
  68. if (min1 < min2 || max1 > max2)
  69. {
  70. one_in_two = false;
  71. }
  72. // Same other way round
  73. if (min2 < min1 || max2 > max1)
  74. {
  75. two_in_one = false;
  76. }
  77. box_box_loop
  78. <
  79. Dimension + 1,
  80. DimensionCount
  81. >::apply(b1, b2, overlaps, one_in_two, two_in_one);
  82. }
  83. };
  84. template
  85. <
  86. std::size_t DimensionCount
  87. >
  88. struct box_box_loop<DimensionCount, DimensionCount>
  89. {
  90. template <typename Box1, typename Box2>
  91. static inline void apply(Box1 const& , Box2 const&, bool&, bool&, bool&)
  92. {
  93. }
  94. };
  95. struct box_box
  96. {
  97. template <typename Box1, typename Box2, typename Strategy>
  98. static inline bool apply(Box1 const& b1, Box2 const& b2, Strategy const& /*strategy*/)
  99. {
  100. bool overlaps = true;
  101. bool within1 = true;
  102. bool within2 = true;
  103. box_box_loop
  104. <
  105. 0,
  106. dimension<Box1>::type::value
  107. >::apply(b1, b2, overlaps, within1, within2);
  108. /*
  109. \see http://docs.codehaus.org/display/GEOTDOC/02+Geometry+Relationships#02GeometryRelationships-Overlaps
  110. where is stated that "inside" is not an "overlap",
  111. this is true and is implemented as such.
  112. */
  113. return overlaps && ! within1 && ! within2;
  114. }
  115. };
  116. }} // namespace detail::overlaps
  117. #endif // DOXYGEN_NO_DETAIL
  118. #ifndef DOXYGEN_NO_DISPATCH
  119. namespace dispatch
  120. {
  121. template <typename Box1, typename Box2>
  122. struct overlaps<Box1, Box2, box_tag, box_tag>
  123. : detail::overlaps::box_box
  124. {};
  125. template <typename Geometry1, typename Geometry2>
  126. struct overlaps<Geometry1, Geometry2, geometry_collection_tag, geometry_collection_tag>
  127. {
  128. template <typename Strategy>
  129. static inline bool apply(Geometry1 const& geometry1, Geometry2 const& geometry2,
  130. Strategy const& strategy)
  131. {
  132. int dimension1 = detail::gc_topological_dimension(geometry1);
  133. int dimension2 = detail::gc_topological_dimension(geometry2);
  134. if (dimension1 >= 0 && dimension2 >= 0)
  135. {
  136. if (dimension1 == 1 && dimension2 == 1)
  137. {
  138. return detail::relate::relate_impl
  139. <
  140. detail::de9im::static_mask_overlaps_d1_1_d2_1_type,
  141. Geometry1,
  142. Geometry2
  143. >::apply(geometry1, geometry2, strategy);
  144. }
  145. else if (dimension1 == dimension2)
  146. {
  147. return detail::relate::relate_impl
  148. <
  149. detail::de9im::static_mask_overlaps_d1_eq_d2_type,
  150. Geometry1,
  151. Geometry2
  152. >::apply(geometry1, geometry2, strategy);
  153. }
  154. }
  155. return false;
  156. }
  157. };
  158. template <typename Geometry1, typename Geometry2, typename Tag1>
  159. struct overlaps<Geometry1, Geometry2, Tag1, geometry_collection_tag>
  160. {
  161. template <typename Strategy>
  162. static inline bool apply(Geometry1 const& geometry1, Geometry2 const& geometry2,
  163. Strategy const& strategy)
  164. {
  165. using gc1_view_t = detail::geometry_collection_view<Geometry1>;
  166. return overlaps
  167. <
  168. gc1_view_t, Geometry2
  169. >::apply(gc1_view_t(geometry1), geometry2, strategy);
  170. }
  171. };
  172. template <typename Geometry1, typename Geometry2, typename Tag2>
  173. struct overlaps<Geometry1, Geometry2, geometry_collection_tag, Tag2>
  174. {
  175. template <typename Strategy>
  176. static inline bool apply(Geometry1 const& geometry1, Geometry2 const& geometry2,
  177. Strategy const& strategy)
  178. {
  179. using gc2_view_t = detail::geometry_collection_view<Geometry2>;
  180. return overlaps
  181. <
  182. Geometry1, gc2_view_t
  183. >::apply(geometry1, gc2_view_t(geometry2), strategy);
  184. }
  185. };
  186. } // namespace dispatch
  187. #endif // DOXYGEN_NO_DISPATCH
  188. }} // namespace boost::geometry
  189. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAPS_IMPLEMENTATION_HPP