implementation.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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, 2015, 2017.
  6. // Modifications copyright (c) 2014-2017 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/not_implemented.hpp>
  18. #include <boost/geometry/geometries/concepts/check.hpp>
  19. #include <boost/geometry/algorithms/relate.hpp>
  20. #include <boost/geometry/algorithms/detail/overlaps/interface.hpp>
  21. namespace boost { namespace geometry
  22. {
  23. #ifndef DOXYGEN_NO_DETAIL
  24. namespace detail { namespace overlaps
  25. {
  26. template
  27. <
  28. std::size_t Dimension,
  29. std::size_t DimensionCount
  30. >
  31. struct box_box_loop
  32. {
  33. template <typename Box1, typename Box2>
  34. static inline void apply(Box1 const& b1, Box2 const& b2,
  35. bool& overlaps, bool& one_in_two, bool& two_in_one)
  36. {
  37. assert_dimension_equal<Box1, Box2>();
  38. typedef typename coordinate_type<Box1>::type coordinate_type1;
  39. typedef typename coordinate_type<Box2>::type coordinate_type2;
  40. coordinate_type1 const& min1 = get<min_corner, Dimension>(b1);
  41. coordinate_type1 const& max1 = get<max_corner, Dimension>(b1);
  42. coordinate_type2 const& min2 = get<min_corner, Dimension>(b2);
  43. coordinate_type2 const& max2 = get<max_corner, Dimension>(b2);
  44. // We might use the (not yet accepted) Boost.Interval
  45. // submission in the future
  46. // If:
  47. // B1: |-------|
  48. // B2: |------|
  49. // in any dimension -> no overlap
  50. if (max1 <= min2 || min1 >= max2)
  51. {
  52. overlaps = false;
  53. return;
  54. }
  55. // If:
  56. // B1: |--------------------|
  57. // B2: |-------------|
  58. // in all dimensions -> within, then no overlap
  59. // B1: |--------------------|
  60. // B2: |-------------|
  61. // this is "within-touch" -> then no overlap. So use < and >
  62. if (min1 < min2 || max1 > max2)
  63. {
  64. one_in_two = false;
  65. }
  66. // Same other way round
  67. if (min2 < min1 || max2 > max1)
  68. {
  69. two_in_one = false;
  70. }
  71. box_box_loop
  72. <
  73. Dimension + 1,
  74. DimensionCount
  75. >::apply(b1, b2, overlaps, one_in_two, two_in_one);
  76. }
  77. };
  78. template
  79. <
  80. std::size_t DimensionCount
  81. >
  82. struct box_box_loop<DimensionCount, DimensionCount>
  83. {
  84. template <typename Box1, typename Box2>
  85. static inline void apply(Box1 const& , Box2 const&, bool&, bool&, bool&)
  86. {
  87. }
  88. };
  89. struct box_box
  90. {
  91. template <typename Box1, typename Box2, typename Strategy>
  92. static inline bool apply(Box1 const& b1, Box2 const& b2, Strategy const& /*strategy*/)
  93. {
  94. bool overlaps = true;
  95. bool within1 = true;
  96. bool within2 = true;
  97. box_box_loop
  98. <
  99. 0,
  100. dimension<Box1>::type::value
  101. >::apply(b1, b2, overlaps, within1, within2);
  102. /*
  103. \see http://docs.codehaus.org/display/GEOTDOC/02+Geometry+Relationships#02GeometryRelationships-Overlaps
  104. where is stated that "inside" is not an "overlap",
  105. this is true and is implemented as such.
  106. */
  107. return overlaps && ! within1 && ! within2;
  108. }
  109. };
  110. }} // namespace detail::overlaps
  111. #endif // DOXYGEN_NO_DETAIL
  112. #ifndef DOXYGEN_NO_DISPATCH
  113. namespace dispatch
  114. {
  115. template <typename Box1, typename Box2>
  116. struct overlaps<Box1, Box2, box_tag, box_tag>
  117. : detail::overlaps::box_box
  118. {};
  119. } // namespace dispatch
  120. #endif // DOXYGEN_NO_DISPATCH
  121. }} // namespace boost::geometry
  122. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAPS_IMPLEMENTATION_HPP