touches.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  6. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_ALGORITHMS_TOUCHES_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_TOUCHES_HPP
  12. #include <deque>
  13. #include <boost/geometry/geometries/concepts/check.hpp>
  14. #include <boost/geometry/algorithms/detail/overlay/self_turn_points.hpp>
  15. #include <boost/geometry/algorithms/detail/overlay/get_turns.hpp>
  16. #include <boost/geometry/algorithms/disjoint.hpp>
  17. #include <boost/geometry/algorithms/intersects.hpp>
  18. #include <boost/geometry/algorithms/num_geometries.hpp>
  19. namespace boost { namespace geometry
  20. {
  21. namespace detail { namespace touches
  22. {
  23. template <typename Turn>
  24. inline bool ok_for_touch(Turn const& turn)
  25. {
  26. return turn.both(detail::overlay::operation_union)
  27. || turn.both(detail::overlay::operation_blocked)
  28. || turn.combination(detail::overlay::operation_union, detail::overlay::operation_blocked)
  29. ;
  30. }
  31. template <typename Turns>
  32. inline bool has_only_turns(Turns const& turns)
  33. {
  34. bool has_touch = false;
  35. typedef typename boost::range_iterator<Turns const>::type iterator_type;
  36. for (iterator_type it = boost::begin(turns); it != boost::end(turns); ++it)
  37. {
  38. if (it->has(detail::overlay::operation_intersection))
  39. {
  40. return false;
  41. }
  42. switch(it->method)
  43. {
  44. case detail::overlay::method_crosses:
  45. return false;
  46. case detail::overlay::method_equal:
  47. // Segment spatially equal means: at the right side
  48. // the polygon internally overlaps. So return false.
  49. return false;
  50. case detail::overlay::method_touch:
  51. case detail::overlay::method_touch_interior:
  52. case detail::overlay::method_collinear:
  53. if (ok_for_touch(*it))
  54. {
  55. has_touch = true;
  56. }
  57. else
  58. {
  59. return false;
  60. }
  61. break;
  62. case detail::overlay::method_none :
  63. case detail::overlay::method_disjoint :
  64. case detail::overlay::method_error :
  65. break;
  66. }
  67. }
  68. return has_touch;
  69. }
  70. }}
  71. /*!
  72. \brief \brief_check{has at least one touching point (self-tangency)}
  73. \note This function can be called for one geometry (self-tangency) and
  74. also for two geometries (touch)
  75. \ingroup touches
  76. \tparam Geometry \tparam_geometry
  77. \param geometry \param_geometry
  78. \return \return_check{is self-touching}
  79. \qbk{distinguish,one geometry}
  80. \qbk{[def __one_parameter__]}
  81. \qbk{[include reference/algorithms/touches.qbk]}
  82. */
  83. template <typename Geometry>
  84. inline bool touches(Geometry const& geometry)
  85. {
  86. concept::check<Geometry const>();
  87. typedef detail::overlay::turn_info
  88. <
  89. typename geometry::point_type<Geometry>::type
  90. > turn_info;
  91. typedef detail::overlay::get_turn_info
  92. <
  93. typename point_type<Geometry>::type,
  94. typename point_type<Geometry>::type,
  95. turn_info,
  96. detail::overlay::assign_null_policy
  97. > policy_type;
  98. std::deque<turn_info> turns;
  99. detail::self_get_turn_points::no_interrupt_policy policy;
  100. detail::self_get_turn_points::get_turns
  101. <
  102. Geometry,
  103. std::deque<turn_info>,
  104. policy_type,
  105. detail::self_get_turn_points::no_interrupt_policy
  106. >::apply(geometry, turns, policy);
  107. return detail::touches::has_only_turns(turns);
  108. }
  109. /*!
  110. \brief \brief_check2{have at least one touching point (tangent - non overlapping)}
  111. \ingroup touches
  112. \tparam Geometry1 \tparam_geometry
  113. \tparam Geometry2 \tparam_geometry
  114. \param geometry1 \param_geometry
  115. \param geometry2 \param_geometry
  116. \return \return_check2{touch each other}
  117. \qbk{distinguish,two geometries}
  118. \qbk{[include reference/algorithms/touches.qbk]}
  119. */
  120. template <typename Geometry1, typename Geometry2>
  121. inline bool touches(Geometry1 const& geometry1, Geometry2 const& geometry2)
  122. {
  123. concept::check<Geometry1 const>();
  124. concept::check<Geometry2 const>();
  125. typedef detail::overlay::turn_info
  126. <
  127. typename geometry::point_type<Geometry1>::type
  128. > turn_info;
  129. typedef detail::overlay::get_turn_info
  130. <
  131. typename point_type<Geometry1>::type,
  132. typename point_type<Geometry2>::type,
  133. turn_info,
  134. detail::overlay::assign_null_policy
  135. > policy_type;
  136. std::deque<turn_info> turns;
  137. detail::get_turns::no_interrupt_policy policy;
  138. boost::geometry::get_turns
  139. <
  140. false, false,
  141. detail::overlay::assign_null_policy
  142. >(geometry1, geometry2, turns, policy);
  143. return detail::touches::has_only_turns(turns)
  144. && ! geometry::detail::disjoint::rings_containing(geometry1, geometry2)
  145. && ! geometry::detail::disjoint::rings_containing(geometry2, geometry1)
  146. ;
  147. }
  148. }} // namespace boost::geometry
  149. #endif // BOOST_GEOMETRY_ALGORITHMS_TOUCHES_HPP