disjoint.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. // Copyright (c) 2013 Adam Wulkiewicz, Lodz, Poland.
  6. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  7. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  8. // Use, modification and distribution is subject to the Boost Software License,
  9. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. #ifndef BOOST_GEOMETRY_ALGORITHMS_DISJOINT_HPP
  12. #define BOOST_GEOMETRY_ALGORITHMS_DISJOINT_HPP
  13. #include <cstddef>
  14. #include <deque>
  15. #include <boost/mpl/if.hpp>
  16. #include <boost/range.hpp>
  17. #include <boost/static_assert.hpp>
  18. #include <boost/geometry/core/access.hpp>
  19. #include <boost/geometry/core/coordinate_dimension.hpp>
  20. #include <boost/geometry/core/reverse_dispatch.hpp>
  21. #include <boost/geometry/algorithms/detail/disjoint.hpp>
  22. #include <boost/geometry/algorithms/detail/for_each_range.hpp>
  23. #include <boost/geometry/algorithms/detail/point_on_border.hpp>
  24. #include <boost/geometry/algorithms/detail/overlay/get_turns.hpp>
  25. #include <boost/geometry/algorithms/within.hpp>
  26. #include <boost/geometry/geometries/concepts/check.hpp>
  27. #include <boost/geometry/util/math.hpp>
  28. namespace boost { namespace geometry
  29. {
  30. #ifndef DOXYGEN_NO_DETAIL
  31. namespace detail { namespace disjoint
  32. {
  33. template<typename Geometry>
  34. struct check_each_ring_for_within
  35. {
  36. bool has_within;
  37. Geometry const& m_geometry;
  38. inline check_each_ring_for_within(Geometry const& g)
  39. : has_within(false)
  40. , m_geometry(g)
  41. {}
  42. template <typename Range>
  43. inline void apply(Range const& range)
  44. {
  45. typename geometry::point_type<Range>::type p;
  46. geometry::point_on_border(p, range);
  47. if (geometry::within(p, m_geometry))
  48. {
  49. has_within = true;
  50. }
  51. }
  52. };
  53. template <typename FirstGeometry, typename SecondGeometry>
  54. inline bool rings_containing(FirstGeometry const& geometry1,
  55. SecondGeometry const& geometry2)
  56. {
  57. check_each_ring_for_within<FirstGeometry> checker(geometry1);
  58. geometry::detail::for_each_range(geometry2, checker);
  59. return checker.has_within;
  60. }
  61. struct assign_disjoint_policy
  62. {
  63. // We want to include all points:
  64. static bool const include_no_turn = true;
  65. static bool const include_degenerate = true;
  66. static bool const include_opposite = true;
  67. // We don't assign extra info:
  68. template
  69. <
  70. typename Info,
  71. typename Point1,
  72. typename Point2,
  73. typename IntersectionInfo,
  74. typename DirInfo
  75. >
  76. static inline void apply(Info& , Point1 const& , Point2 const&,
  77. IntersectionInfo const&, DirInfo const&)
  78. {}
  79. };
  80. template <typename Geometry1, typename Geometry2>
  81. struct disjoint_linear
  82. {
  83. static inline bool apply(Geometry1 const& geometry1, Geometry2 const& geometry2)
  84. {
  85. typedef typename geometry::point_type<Geometry1>::type point_type;
  86. typedef overlay::turn_info<point_type> turn_info;
  87. std::deque<turn_info> turns;
  88. // Specify two policies:
  89. // 1) Stop at any intersection
  90. // 2) In assignment, include also degenerate points (which are normally skipped)
  91. disjoint_interrupt_policy policy;
  92. geometry::get_turns
  93. <
  94. false, false,
  95. assign_disjoint_policy
  96. >(geometry1, geometry2, turns, policy);
  97. if (policy.has_intersections)
  98. {
  99. return false;
  100. }
  101. return true;
  102. }
  103. };
  104. template <typename Segment1, typename Segment2>
  105. struct disjoint_segment
  106. {
  107. static inline bool apply(Segment1 const& segment1, Segment2 const& segment2)
  108. {
  109. typedef typename point_type<Segment1>::type point_type;
  110. segment_intersection_points<point_type> is
  111. = strategy::intersection::relate_cartesian_segments
  112. <
  113. policies::relate::segments_intersection_points
  114. <
  115. Segment1,
  116. Segment2,
  117. segment_intersection_points<point_type>
  118. >
  119. >::apply(segment1, segment2);
  120. return is.count == 0;
  121. }
  122. };
  123. template <typename Geometry1, typename Geometry2>
  124. struct general_areal
  125. {
  126. static inline bool apply(Geometry1 const& geometry1, Geometry2 const& geometry2)
  127. {
  128. if (! disjoint_linear<Geometry1, Geometry2>::apply(geometry1, geometry2))
  129. {
  130. return false;
  131. }
  132. // If there is no intersection of segments, they might located
  133. // inside each other
  134. if (rings_containing(geometry1, geometry2)
  135. || rings_containing(geometry2, geometry1))
  136. {
  137. return false;
  138. }
  139. return true;
  140. }
  141. };
  142. template <typename Segment, typename Box>
  143. struct disjoint_segment_box
  144. {
  145. static inline bool apply(Segment const& segment, Box const& box)
  146. {
  147. typedef typename point_type<Segment>::type point_type;
  148. point_type p0, p1;
  149. geometry::detail::assign_point_from_index<0>(segment, p0);
  150. geometry::detail::assign_point_from_index<1>(segment, p1);
  151. return ! detail::disjoint::segment_box_intersection<point_type, Box>::apply(p0, p1, box);
  152. }
  153. };
  154. template <typename Linestring, typename Box>
  155. struct disjoint_linestring_box
  156. {
  157. static inline bool apply(Linestring const& linestring, Box const& box)
  158. {
  159. typedef typename ::boost::range_value<Linestring>::type point_type;
  160. typedef typename ::boost::range_const_iterator<Linestring>::type const_iterator;
  161. typedef typename ::boost::range_size<Linestring>::type size_type;
  162. const size_type count = ::boost::size(linestring);
  163. if ( count == 0 )
  164. return false;
  165. else if ( count == 1 )
  166. return detail::disjoint::point_box<point_type, Box, 0, dimension<point_type>::value>
  167. ::apply(*::boost::begin(linestring), box);
  168. else
  169. {
  170. const_iterator it0 = ::boost::begin(linestring);
  171. const_iterator it1 = ::boost::begin(linestring) + 1;
  172. const_iterator last = ::boost::end(linestring);
  173. for ( ; it1 != last ; ++it0, ++it1 )
  174. {
  175. if ( detail::disjoint::segment_box_intersection<point_type, Box>::apply(*it0, *it1, box) )
  176. return false;
  177. }
  178. return true;
  179. }
  180. }
  181. };
  182. }} // namespace detail::disjoint
  183. #endif // DOXYGEN_NO_DETAIL
  184. #ifndef DOXYGEN_NO_DISPATCH
  185. namespace dispatch
  186. {
  187. template
  188. <
  189. typename Geometry1, typename Geometry2,
  190. std::size_t DimensionCount = dimension<Geometry1>::type::value,
  191. typename Tag1 = typename tag<Geometry1>::type,
  192. typename Tag2 = typename tag<Geometry2>::type,
  193. bool Reverse = reverse_dispatch<Geometry1, Geometry2>::type::value
  194. >
  195. struct disjoint
  196. : detail::disjoint::general_areal<Geometry1, Geometry2>
  197. {};
  198. // If reversal is needed, perform it
  199. template
  200. <
  201. typename Geometry1, typename Geometry2,
  202. std::size_t DimensionCount,
  203. typename Tag1, typename Tag2
  204. >
  205. struct disjoint<Geometry1, Geometry2, DimensionCount, Tag1, Tag2, true>
  206. : disjoint<Geometry2, Geometry1, DimensionCount, Tag2, Tag1, false>
  207. {
  208. static inline bool apply(Geometry1 const& g1, Geometry2 const& g2)
  209. {
  210. return disjoint
  211. <
  212. Geometry2, Geometry1,
  213. DimensionCount,
  214. Tag2, Tag1
  215. >::apply(g2, g1);
  216. }
  217. };
  218. template <typename Point1, typename Point2, std::size_t DimensionCount, bool Reverse>
  219. struct disjoint<Point1, Point2, DimensionCount, point_tag, point_tag, Reverse>
  220. : detail::disjoint::point_point<Point1, Point2, 0, DimensionCount>
  221. {};
  222. template <typename Box1, typename Box2, std::size_t DimensionCount, bool Reverse>
  223. struct disjoint<Box1, Box2, DimensionCount, box_tag, box_tag, Reverse>
  224. : detail::disjoint::box_box<Box1, Box2, 0, DimensionCount>
  225. {};
  226. template <typename Point, typename Box, std::size_t DimensionCount, bool Reverse>
  227. struct disjoint<Point, Box, DimensionCount, point_tag, box_tag, Reverse>
  228. : detail::disjoint::point_box<Point, Box, 0, DimensionCount>
  229. {};
  230. template <typename Point, typename Ring, std::size_t DimensionCount, bool Reverse>
  231. struct disjoint<Point, Ring, DimensionCount, point_tag, ring_tag, Reverse>
  232. : detail::disjoint::reverse_covered_by<Point, Ring>
  233. {};
  234. template <typename Point, typename Polygon, std::size_t DimensionCount, bool Reverse>
  235. struct disjoint<Point, Polygon, DimensionCount, point_tag, polygon_tag, Reverse>
  236. : detail::disjoint::reverse_covered_by<Point, Polygon>
  237. {};
  238. template <typename Linestring1, typename Linestring2, bool Reverse>
  239. struct disjoint<Linestring1, Linestring2, 2, linestring_tag, linestring_tag, Reverse>
  240. : detail::disjoint::disjoint_linear<Linestring1, Linestring2>
  241. {};
  242. template <typename Segment1, typename Segment2, bool Reverse>
  243. struct disjoint<Segment1, Segment2, 2, segment_tag, segment_tag, Reverse>
  244. : detail::disjoint::disjoint_segment<Segment1, Segment2>
  245. {};
  246. template <typename Linestring, typename Segment, bool Reverse>
  247. struct disjoint<Linestring, Segment, 2, linestring_tag, segment_tag, Reverse>
  248. : detail::disjoint::disjoint_linear<Linestring, Segment>
  249. {};
  250. template <typename Segment, typename Box, std::size_t DimensionCount, bool Reverse>
  251. struct disjoint<Segment, Box, DimensionCount, segment_tag, box_tag, Reverse>
  252. : detail::disjoint::disjoint_segment_box<Segment, Box>
  253. {};
  254. template <typename Linestring, typename Box, std::size_t DimensionCount, bool Reverse>
  255. struct disjoint<Linestring, Box, DimensionCount, linestring_tag, box_tag, Reverse>
  256. : detail::disjoint::disjoint_linestring_box<Linestring, Box>
  257. {};
  258. } // namespace dispatch
  259. #endif // DOXYGEN_NO_DISPATCH
  260. /*!
  261. \brief \brief_check2{are disjoint}
  262. \ingroup disjoint
  263. \tparam Geometry1 \tparam_geometry
  264. \tparam Geometry2 \tparam_geometry
  265. \param geometry1 \param_geometry
  266. \param geometry2 \param_geometry
  267. \return \return_check2{are disjoint}
  268. \qbk{[include reference/algorithms/disjoint.qbk]}
  269. */
  270. template <typename Geometry1, typename Geometry2>
  271. inline bool disjoint(Geometry1 const& geometry1,
  272. Geometry2 const& geometry2)
  273. {
  274. concept::check_concepts_and_equal_dimensions
  275. <
  276. Geometry1 const,
  277. Geometry2 const
  278. >();
  279. return dispatch::disjoint<Geometry1, Geometry2>::apply(geometry1, geometry2);
  280. }
  281. }} // namespace boost::geometry
  282. #endif // BOOST_GEOMETRY_ALGORITHMS_DISJOINT_HPP