intersection.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 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_INTERSECTION_HPP
  7. #define BOOST_GEOMETRY_ALGORITHMS_INTERSECTION_HPP
  8. #include <boost/geometry/core/coordinate_dimension.hpp>
  9. #include <boost/geometry/algorithms/detail/overlay/intersection_insert.hpp>
  10. #include <boost/geometry/algorithms/intersects.hpp>
  11. namespace boost { namespace geometry
  12. {
  13. #ifndef DOXYGEN_NO_DETAIL
  14. namespace detail { namespace intersection
  15. {
  16. template <std::size_t Dimension, std::size_t DimensionCount>
  17. struct intersection_box_box
  18. {
  19. template
  20. <
  21. typename Box1, typename Box2, typename BoxOut,
  22. typename Strategy
  23. >
  24. static inline bool apply(Box1 const& box1,
  25. Box2 const& box2, BoxOut& box_out,
  26. Strategy const& strategy)
  27. {
  28. typedef typename coordinate_type<BoxOut>::type ct;
  29. ct min1 = get<min_corner, Dimension>(box1);
  30. ct min2 = get<min_corner, Dimension>(box2);
  31. ct max1 = get<max_corner, Dimension>(box1);
  32. ct max2 = get<max_corner, Dimension>(box2);
  33. if (max1 < min2 || max2 < min1)
  34. {
  35. return false;
  36. }
  37. // Set dimensions of output coordinate
  38. set<min_corner, Dimension>(box_out, min1 < min2 ? min2 : min1);
  39. set<max_corner, Dimension>(box_out, max1 > max2 ? max2 : max1);
  40. return intersection_box_box<Dimension + 1, DimensionCount>
  41. ::apply(box1, box2, box_out, strategy);
  42. }
  43. };
  44. template <std::size_t DimensionCount>
  45. struct intersection_box_box<DimensionCount, DimensionCount>
  46. {
  47. template
  48. <
  49. typename Box1, typename Box2, typename BoxOut,
  50. typename Strategy
  51. >
  52. static inline bool apply(Box1 const&, Box2 const&, BoxOut&, Strategy const&)
  53. {
  54. return true;
  55. }
  56. };
  57. }} // namespace detail::intersection
  58. #endif // DOXYGEN_NO_DETAIL
  59. #ifndef DOXYGEN_NO_DISPATCH
  60. namespace dispatch
  61. {
  62. // By default, all is forwarded to the intersection_insert-dispatcher
  63. template
  64. <
  65. typename Geometry1, typename Geometry2,
  66. typename Tag1 = typename geometry::tag<Geometry1>::type,
  67. typename Tag2 = typename geometry::tag<Geometry2>::type,
  68. bool Reverse = reverse_dispatch<Geometry1, Geometry2>::type::value
  69. >
  70. struct intersection
  71. {
  72. template <typename GeometryOut, typename Strategy>
  73. static inline bool apply(Geometry1 const& geometry1,
  74. Geometry2 const& geometry2,
  75. GeometryOut& geometry_out,
  76. Strategy const& strategy)
  77. {
  78. typedef typename boost::range_value<GeometryOut>::type OneOut;
  79. intersection_insert
  80. <
  81. Geometry1, Geometry2, OneOut,
  82. overlay_intersection
  83. >::apply(geometry1, geometry2, std::back_inserter(geometry_out), strategy);
  84. return true;
  85. }
  86. };
  87. // If reversal is needed, perform it
  88. template
  89. <
  90. typename Geometry1, typename Geometry2,
  91. typename Tag1, typename Tag2
  92. >
  93. struct intersection
  94. <
  95. Geometry1, Geometry2,
  96. Tag1, Tag2,
  97. true
  98. >
  99. : intersection<Geometry2, Geometry1, Tag2, Tag1, false>
  100. {
  101. template <typename GeometryOut, typename Strategy>
  102. static inline bool apply(
  103. Geometry1 const& g1,
  104. Geometry2 const& g2,
  105. GeometryOut& out,
  106. Strategy const& strategy)
  107. {
  108. return intersection<
  109. Geometry2, Geometry1,
  110. Tag2, Tag1,
  111. false
  112. >::apply(g2, g1, out, strategy);
  113. }
  114. };
  115. template
  116. <
  117. typename Box1, typename Box2, bool Reverse
  118. >
  119. struct intersection
  120. <
  121. Box1, Box2,
  122. box_tag, box_tag,
  123. Reverse
  124. > : public detail::intersection::intersection_box_box
  125. <
  126. 0, geometry::dimension<Box1>::value
  127. >
  128. {};
  129. } // namespace dispatch
  130. #endif // DOXYGEN_NO_DISPATCH
  131. /*!
  132. \brief \brief_calc2{intersection}
  133. \ingroup intersection
  134. \details \details_calc2{intersection, spatial set theoretic intersection}.
  135. \tparam Geometry1 \tparam_geometry
  136. \tparam Geometry2 \tparam_geometry
  137. \tparam GeometryOut Collection of geometries (e.g. std::vector, std::deque, boost::geometry::multi*) of which
  138. the value_type fulfills a \p_l_or_c concept, or it is the output geometry (e.g. for a box)
  139. \param geometry1 \param_geometry
  140. \param geometry2 \param_geometry
  141. \param geometry_out The output geometry, either a multi_point, multi_polygon,
  142. multi_linestring, or a box (for intersection of two boxes)
  143. \qbk{[include reference/algorithms/intersection.qbk]}
  144. */
  145. template
  146. <
  147. typename Geometry1,
  148. typename Geometry2,
  149. typename GeometryOut
  150. >
  151. inline bool intersection(Geometry1 const& geometry1,
  152. Geometry2 const& geometry2,
  153. GeometryOut& geometry_out)
  154. {
  155. concept::check<Geometry1 const>();
  156. concept::check<Geometry2 const>();
  157. typedef strategy_intersection
  158. <
  159. typename cs_tag<Geometry1>::type,
  160. Geometry1,
  161. Geometry2,
  162. typename geometry::point_type<Geometry1>::type
  163. > strategy;
  164. return dispatch::intersection<
  165. Geometry1,
  166. Geometry2
  167. >::apply(geometry1, geometry2, geometry_out, strategy());
  168. }
  169. }} // namespace boost::geometry
  170. #endif // BOOST_GEOMETRY_ALGORITHMS_INTERSECTION_HPP