within.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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_WITHIN_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_WITHIN_HPP
  12. #include <cstddef>
  13. #include <boost/concept_check.hpp>
  14. #include <boost/range.hpp>
  15. #include <boost/typeof/typeof.hpp>
  16. #include <boost/geometry/algorithms/make.hpp>
  17. #include <boost/geometry/algorithms/not_implemented.hpp>
  18. #include <boost/geometry/core/access.hpp>
  19. #include <boost/geometry/core/closure.hpp>
  20. #include <boost/geometry/core/cs.hpp>
  21. #include <boost/geometry/core/exterior_ring.hpp>
  22. #include <boost/geometry/core/interior_rings.hpp>
  23. #include <boost/geometry/core/point_order.hpp>
  24. #include <boost/geometry/core/ring_type.hpp>
  25. #include <boost/geometry/core/interior_rings.hpp>
  26. #include <boost/geometry/core/tags.hpp>
  27. #include <boost/geometry/geometries/concepts/check.hpp>
  28. #include <boost/geometry/strategies/within.hpp>
  29. #include <boost/geometry/strategies/concepts/within_concept.hpp>
  30. #include <boost/geometry/util/math.hpp>
  31. #include <boost/geometry/util/order_as_direction.hpp>
  32. #include <boost/geometry/views/closeable_view.hpp>
  33. #include <boost/geometry/views/reversible_view.hpp>
  34. namespace boost { namespace geometry
  35. {
  36. #ifndef DOXYGEN_NO_DETAIL
  37. namespace detail { namespace within
  38. {
  39. template
  40. <
  41. typename Point,
  42. typename Ring,
  43. iterate_direction Direction,
  44. closure_selector Closure,
  45. typename Strategy
  46. >
  47. struct point_in_ring
  48. {
  49. BOOST_CONCEPT_ASSERT( (geometry::concept::WithinStrategyPolygonal<Strategy>) );
  50. static inline int apply(Point const& point, Ring const& ring,
  51. Strategy const& strategy)
  52. {
  53. boost::ignore_unused_variable_warning(strategy);
  54. if (int(boost::size(ring))
  55. < core_detail::closure::minimum_ring_size<Closure>::value)
  56. {
  57. return -1;
  58. }
  59. typedef typename reversible_view<Ring const, Direction>::type rev_view_type;
  60. typedef typename closeable_view
  61. <
  62. rev_view_type const, Closure
  63. >::type cl_view_type;
  64. typedef typename boost::range_iterator<cl_view_type const>::type iterator_type;
  65. rev_view_type rev_view(ring);
  66. cl_view_type view(rev_view);
  67. typename Strategy::state_type state;
  68. iterator_type it = boost::begin(view);
  69. iterator_type end = boost::end(view);
  70. bool stop = false;
  71. for (iterator_type previous = it++;
  72. it != end && ! stop;
  73. ++previous, ++it)
  74. {
  75. if (! strategy.apply(point, *previous, *it, state))
  76. {
  77. stop = true;
  78. }
  79. }
  80. return strategy.result(state);
  81. }
  82. };
  83. // Polygon: in exterior ring, and if so, not within interior ring(s)
  84. template
  85. <
  86. typename Point,
  87. typename Polygon,
  88. iterate_direction Direction,
  89. closure_selector Closure,
  90. typename Strategy
  91. >
  92. struct point_in_polygon
  93. {
  94. BOOST_CONCEPT_ASSERT( (geometry::concept::WithinStrategyPolygonal<Strategy>) );
  95. static inline int apply(Point const& point, Polygon const& poly,
  96. Strategy const& strategy)
  97. {
  98. int const code = point_in_ring
  99. <
  100. Point,
  101. typename ring_type<Polygon>::type,
  102. Direction,
  103. Closure,
  104. Strategy
  105. >::apply(point, exterior_ring(poly), strategy);
  106. if (code == 1)
  107. {
  108. typename interior_return_type<Polygon const>::type rings
  109. = interior_rings(poly);
  110. for (BOOST_AUTO_TPL(it, boost::begin(rings));
  111. it != boost::end(rings);
  112. ++it)
  113. {
  114. int const interior_code = point_in_ring
  115. <
  116. Point,
  117. typename ring_type<Polygon>::type,
  118. Direction,
  119. Closure,
  120. Strategy
  121. >::apply(point, *it, strategy);
  122. if (interior_code != -1)
  123. {
  124. // If 0, return 0 (touch)
  125. // If 1 (inside hole) return -1 (outside polygon)
  126. // If -1 (outside hole) check other holes if any
  127. return -interior_code;
  128. }
  129. }
  130. }
  131. return code;
  132. }
  133. };
  134. }} // namespace detail::within
  135. #endif // DOXYGEN_NO_DETAIL
  136. #ifndef DOXYGEN_NO_DISPATCH
  137. namespace dispatch
  138. {
  139. template
  140. <
  141. typename Geometry1,
  142. typename Geometry2,
  143. typename Tag1 = typename tag<Geometry1>::type,
  144. typename Tag2 = typename tag<Geometry2>::type
  145. >
  146. struct within: not_implemented<Tag1, Tag2>
  147. {};
  148. template <typename Point, typename Box>
  149. struct within<Point, Box, point_tag, box_tag>
  150. {
  151. template <typename Strategy>
  152. static inline bool apply(Point const& point, Box const& box, Strategy const& strategy)
  153. {
  154. boost::ignore_unused_variable_warning(strategy);
  155. return strategy.apply(point, box);
  156. }
  157. };
  158. template <typename Box1, typename Box2>
  159. struct within<Box1, Box2, box_tag, box_tag>
  160. {
  161. template <typename Strategy>
  162. static inline bool apply(Box1 const& box1, Box2 const& box2, Strategy const& strategy)
  163. {
  164. assert_dimension_equal<Box1, Box2>();
  165. boost::ignore_unused_variable_warning(strategy);
  166. return strategy.apply(box1, box2);
  167. }
  168. };
  169. template <typename Point, typename Ring>
  170. struct within<Point, Ring, point_tag, ring_tag>
  171. {
  172. template <typename Strategy>
  173. static inline bool apply(Point const& point, Ring const& ring, Strategy const& strategy)
  174. {
  175. return detail::within::point_in_ring
  176. <
  177. Point,
  178. Ring,
  179. order_as_direction<geometry::point_order<Ring>::value>::value,
  180. geometry::closure<Ring>::value,
  181. Strategy
  182. >::apply(point, ring, strategy) == 1;
  183. }
  184. };
  185. template <typename Point, typename Polygon>
  186. struct within<Point, Polygon, point_tag, polygon_tag>
  187. {
  188. template <typename Strategy>
  189. static inline bool apply(Point const& point, Polygon const& polygon, Strategy const& strategy)
  190. {
  191. return detail::within::point_in_polygon
  192. <
  193. Point,
  194. Polygon,
  195. order_as_direction<geometry::point_order<Polygon>::value>::value,
  196. geometry::closure<Polygon>::value,
  197. Strategy
  198. >::apply(point, polygon, strategy) == 1;
  199. }
  200. };
  201. } // namespace dispatch
  202. #endif // DOXYGEN_NO_DISPATCH
  203. /*!
  204. \brief \brief_check12{is completely inside}
  205. \ingroup within
  206. \details \details_check12{within, is completely inside}.
  207. \tparam Geometry1 \tparam_geometry
  208. \tparam Geometry2 \tparam_geometry
  209. \param geometry1 \param_geometry which might be within the second geometry
  210. \param geometry2 \param_geometry which might contain the first geometry
  211. \return true if geometry1 is completely contained within geometry2,
  212. else false
  213. \note The default strategy is used for within detection
  214. \qbk{[include reference/algorithms/within.qbk]}
  215. \qbk{
  216. [heading Example]
  217. [within]
  218. [within_output]
  219. }
  220. */
  221. template<typename Geometry1, typename Geometry2>
  222. inline bool within(Geometry1 const& geometry1, Geometry2 const& geometry2)
  223. {
  224. concept::check<Geometry1 const>();
  225. concept::check<Geometry2 const>();
  226. assert_dimension_equal<Geometry1, Geometry2>();
  227. typedef typename point_type<Geometry1>::type point_type1;
  228. typedef typename point_type<Geometry2>::type point_type2;
  229. typedef typename strategy::within::services::default_strategy
  230. <
  231. typename tag<Geometry1>::type,
  232. typename tag<Geometry2>::type,
  233. typename tag<Geometry1>::type,
  234. typename tag_cast<typename tag<Geometry2>::type, areal_tag>::type,
  235. typename tag_cast
  236. <
  237. typename cs_tag<point_type1>::type, spherical_tag
  238. >::type,
  239. typename tag_cast
  240. <
  241. typename cs_tag<point_type2>::type, spherical_tag
  242. >::type,
  243. Geometry1,
  244. Geometry2
  245. >::type strategy_type;
  246. return dispatch::within
  247. <
  248. Geometry1,
  249. Geometry2
  250. >::apply(geometry1, geometry2, strategy_type());
  251. }
  252. /*!
  253. \brief \brief_check12{is completely inside} \brief_strategy
  254. \ingroup within
  255. \details \details_check12{within, is completely inside}, \brief_strategy. \details_strategy_reasons
  256. \tparam Geometry1 \tparam_geometry
  257. \tparam Geometry2 \tparam_geometry
  258. \param geometry1 \param_geometry which might be within the second geometry
  259. \param geometry2 \param_geometry which might contain the first geometry
  260. \param strategy strategy to be used
  261. \return true if geometry1 is completely contained within geometry2,
  262. else false
  263. \qbk{distinguish,with strategy}
  264. \qbk{[include reference/algorithms/within.qbk]}
  265. \qbk{
  266. [heading Available Strategies]
  267. \* [link geometry.reference.strategies.strategy_within_winding Winding (coordinate system agnostic)]
  268. \* [link geometry.reference.strategies.strategy_within_franklin Franklin (cartesian)]
  269. \* [link geometry.reference.strategies.strategy_within_crossings_multiply Crossings Multiply (cartesian)]
  270. [heading Example]
  271. [within_strategy]
  272. [within_strategy_output]
  273. }
  274. */
  275. template<typename Geometry1, typename Geometry2, typename Strategy>
  276. inline bool within(Geometry1 const& geometry1, Geometry2 const& geometry2,
  277. Strategy const& strategy)
  278. {
  279. concept::within::check
  280. <
  281. typename tag<Geometry1>::type,
  282. typename tag<Geometry2>::type,
  283. typename tag_cast<typename tag<Geometry2>::type, areal_tag>::type,
  284. Strategy
  285. >();
  286. concept::check<Geometry1 const>();
  287. concept::check<Geometry2 const>();
  288. assert_dimension_equal<Geometry1, Geometry2>();
  289. return dispatch::within
  290. <
  291. Geometry1,
  292. Geometry2
  293. >::apply(geometry1, geometry2, strategy);
  294. }
  295. }} // namespace boost::geometry
  296. #endif // BOOST_GEOMETRY_ALGORITHMS_WITHIN_HPP