point_in_geometry.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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) 2014 Adam Wulkiewicz, Lodz, Poland.
  6. // This file was modified by Oracle on 2013, 2014, 2015, 2017, 2018, 2019.
  7. // Modifications copyright (c) 2013-2019, Oracle and/or its affiliates.
  8. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  9. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  10. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  11. // Use, modification and distribution is subject to the Boost Software License,
  12. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_WITHIN_POINT_IN_GEOMETRY_HPP
  15. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_WITHIN_POINT_IN_GEOMETRY_HPP
  16. #include <boost/core/ignore_unused.hpp>
  17. #include <boost/mpl/assert.hpp>
  18. #include <boost/range.hpp>
  19. #include <boost/type_traits/is_same.hpp>
  20. #include <boost/type_traits/remove_reference.hpp>
  21. #include <boost/geometry/core/assert.hpp>
  22. #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
  23. #include <boost/geometry/algorithms/detail/interior_iterator.hpp>
  24. #include <boost/geometry/geometries/concepts/check.hpp>
  25. #include <boost/geometry/strategies/concepts/within_concept.hpp>
  26. #include <boost/geometry/strategies/default_strategy.hpp>
  27. #include <boost/geometry/strategies/relate.hpp>
  28. #include <boost/geometry/util/range.hpp>
  29. #include <boost/geometry/views/detail/normalized_view.hpp>
  30. namespace boost { namespace geometry {
  31. #ifndef DOXYGEN_NO_DETAIL
  32. namespace detail { namespace within {
  33. template <typename Point1, typename Point2, typename Strategy>
  34. inline bool equals_point_point(Point1 const& p1, Point2 const& p2, Strategy const& strategy)
  35. {
  36. return equals::equals_point_point(p1, p2, strategy.get_equals_point_point_strategy());
  37. }
  38. // TODO: is this needed?
  39. inline int check_result_type(int result)
  40. {
  41. return result;
  42. }
  43. template <typename T>
  44. inline T check_result_type(T result)
  45. {
  46. BOOST_GEOMETRY_ASSERT(false);
  47. return result;
  48. }
  49. template <typename Point, typename Range, typename Strategy> inline
  50. int point_in_range(Point const& point, Range const& range, Strategy const& strategy)
  51. {
  52. boost::ignore_unused(strategy);
  53. typedef typename boost::range_iterator<Range const>::type iterator_type;
  54. typename Strategy::state_type state;
  55. iterator_type it = boost::begin(range);
  56. iterator_type end = boost::end(range);
  57. for ( iterator_type previous = it++ ; it != end ; ++previous, ++it )
  58. {
  59. if ( ! strategy.apply(point, *previous, *it, state) )
  60. {
  61. break;
  62. }
  63. }
  64. return check_result_type(strategy.result(state));
  65. }
  66. template <typename Geometry, typename Point, typename Range>
  67. inline int point_in_range(Point const& point, Range const& range)
  68. {
  69. typedef typename strategy::point_in_geometry::services::default_strategy
  70. <
  71. Point, Geometry
  72. >::type strategy_type;
  73. return point_in_range(point, range, strategy_type());
  74. }
  75. }} // namespace detail::within
  76. namespace detail_dispatch { namespace within {
  77. // checks the relation between a point P and geometry G
  78. // returns 1 if P is in the interior of G
  79. // returns 0 if P is on the boundry of G
  80. // returns -1 if P is in the exterior of G
  81. template <typename Geometry,
  82. typename Tag = typename geometry::tag<Geometry>::type>
  83. struct point_in_geometry
  84. : not_implemented<Tag>
  85. {};
  86. template <typename Point2>
  87. struct point_in_geometry<Point2, point_tag>
  88. {
  89. template <typename Point1, typename Strategy> static inline
  90. int apply(Point1 const& point1, Point2 const& point2, Strategy const& strategy)
  91. {
  92. boost::ignore_unused(strategy);
  93. return strategy.apply(point1, point2) ? 1 : -1;
  94. }
  95. };
  96. template <typename Segment>
  97. struct point_in_geometry<Segment, segment_tag>
  98. {
  99. template <typename Point, typename Strategy> static inline
  100. int apply(Point const& point, Segment const& segment, Strategy const& strategy)
  101. {
  102. boost::ignore_unused(strategy);
  103. typedef typename geometry::point_type<Segment>::type point_type;
  104. point_type p0, p1;
  105. // TODO: don't copy points
  106. detail::assign_point_from_index<0>(segment, p0);
  107. detail::assign_point_from_index<1>(segment, p1);
  108. typename Strategy::state_type state;
  109. strategy.apply(point, p0, p1, state);
  110. int r = detail::within::check_result_type(strategy.result(state));
  111. if ( r != 0 )
  112. return -1; // exterior
  113. // if the point is equal to the one of the terminal points
  114. if ( detail::within::equals_point_point(point, p0, strategy)
  115. || detail::within::equals_point_point(point, p1, strategy) )
  116. return 0; // boundary
  117. else
  118. return 1; // interior
  119. }
  120. };
  121. template <typename Linestring>
  122. struct point_in_geometry<Linestring, linestring_tag>
  123. {
  124. template <typename Point, typename Strategy> static inline
  125. int apply(Point const& point, Linestring const& linestring, Strategy const& strategy)
  126. {
  127. std::size_t count = boost::size(linestring);
  128. if ( count > 1 )
  129. {
  130. if ( detail::within::point_in_range(point, linestring, strategy) != 0 )
  131. return -1; // exterior
  132. // if the linestring doesn't have a boundary
  133. if (detail::within::equals_point_point(range::front(linestring), range::back(linestring), strategy))
  134. return 1; // interior
  135. // else if the point is equal to the one of the terminal points
  136. else if (detail::within::equals_point_point(point, range::front(linestring), strategy)
  137. || detail::within::equals_point_point(point, range::back(linestring), strategy))
  138. return 0; // boundary
  139. else
  140. return 1; // interior
  141. }
  142. // TODO: for now degenerated linestrings are ignored
  143. // throw an exception here?
  144. /*else if ( count == 1 )
  145. {
  146. if ( detail::equals::equals_point_point(point, range::front(linestring)) )
  147. return 1;
  148. }*/
  149. return -1; // exterior
  150. }
  151. };
  152. template <typename Ring>
  153. struct point_in_geometry<Ring, ring_tag>
  154. {
  155. template <typename Point, typename Strategy> static inline
  156. int apply(Point const& point, Ring const& ring, Strategy const& strategy)
  157. {
  158. if ( boost::size(ring) < core_detail::closure::minimum_ring_size
  159. <
  160. geometry::closure<Ring>::value
  161. >::value )
  162. {
  163. return -1;
  164. }
  165. detail::normalized_view<Ring const> view(ring);
  166. return detail::within::point_in_range(point, view, strategy);
  167. }
  168. };
  169. // Polygon: in exterior ring, and if so, not within interior ring(s)
  170. template <typename Polygon>
  171. struct point_in_geometry<Polygon, polygon_tag>
  172. {
  173. template <typename Point, typename Strategy>
  174. static inline int apply(Point const& point, Polygon const& polygon,
  175. Strategy const& strategy)
  176. {
  177. int const code = point_in_geometry
  178. <
  179. typename ring_type<Polygon>::type
  180. >::apply(point, exterior_ring(polygon), strategy);
  181. if (code == 1)
  182. {
  183. typename interior_return_type<Polygon const>::type
  184. rings = interior_rings(polygon);
  185. for (typename detail::interior_iterator<Polygon const>::type
  186. it = boost::begin(rings);
  187. it != boost::end(rings);
  188. ++it)
  189. {
  190. int const interior_code = point_in_geometry
  191. <
  192. typename ring_type<Polygon>::type
  193. >::apply(point, *it, strategy);
  194. if (interior_code != -1)
  195. {
  196. // If 0, return 0 (touch)
  197. // If 1 (inside hole) return -1 (outside polygon)
  198. // If -1 (outside hole) check other holes if any
  199. return -interior_code;
  200. }
  201. }
  202. }
  203. return code;
  204. }
  205. };
  206. template <typename Geometry>
  207. struct point_in_geometry<Geometry, multi_point_tag>
  208. {
  209. template <typename Point, typename Strategy> static inline
  210. int apply(Point const& point, Geometry const& geometry, Strategy const& strategy)
  211. {
  212. typedef typename boost::range_value<Geometry>::type point_type;
  213. typedef typename boost::range_const_iterator<Geometry>::type iterator;
  214. for ( iterator it = boost::begin(geometry) ; it != boost::end(geometry) ; ++it )
  215. {
  216. int pip = point_in_geometry<point_type>::apply(point, *it, strategy);
  217. //BOOST_GEOMETRY_ASSERT(pip != 0);
  218. if ( pip > 0 ) // inside
  219. return 1;
  220. }
  221. return -1; // outside
  222. }
  223. };
  224. template <typename Geometry>
  225. struct point_in_geometry<Geometry, multi_linestring_tag>
  226. {
  227. template <typename Point, typename Strategy> static inline
  228. int apply(Point const& point, Geometry const& geometry, Strategy const& strategy)
  229. {
  230. int pip = -1; // outside
  231. typedef typename boost::range_value<Geometry>::type linestring_type;
  232. typedef typename boost::range_value<linestring_type>::type point_type;
  233. typedef typename boost::range_iterator<Geometry const>::type iterator;
  234. iterator it = boost::begin(geometry);
  235. for ( ; it != boost::end(geometry) ; ++it )
  236. {
  237. pip = point_in_geometry<linestring_type>::apply(point, *it, strategy);
  238. // inside or on the boundary
  239. if ( pip >= 0 )
  240. {
  241. ++it;
  242. break;
  243. }
  244. }
  245. // outside
  246. if ( pip < 0 )
  247. return -1;
  248. // TODO: the following isn't needed for covered_by()
  249. unsigned boundaries = pip == 0 ? 1 : 0;
  250. for ( ; it != boost::end(geometry) ; ++it )
  251. {
  252. if ( boost::size(*it) < 2 )
  253. continue;
  254. point_type const& front = range::front(*it);
  255. point_type const& back = range::back(*it);
  256. // is closed_ring - no boundary
  257. if ( detail::within::equals_point_point(front, back, strategy) )
  258. continue;
  259. // is point on boundary
  260. if ( detail::within::equals_point_point(point, front, strategy)
  261. || detail::within::equals_point_point(point, back, strategy) )
  262. {
  263. ++boundaries;
  264. }
  265. }
  266. // if the number of boundaries is odd, the point is on the boundary
  267. return boundaries % 2 ? 0 : 1;
  268. }
  269. };
  270. template <typename Geometry>
  271. struct point_in_geometry<Geometry, multi_polygon_tag>
  272. {
  273. template <typename Point, typename Strategy> static inline
  274. int apply(Point const& point, Geometry const& geometry, Strategy const& strategy)
  275. {
  276. // For invalid multipolygons
  277. //int res = -1; // outside
  278. typedef typename boost::range_value<Geometry>::type polygon_type;
  279. typedef typename boost::range_const_iterator<Geometry>::type iterator;
  280. for ( iterator it = boost::begin(geometry) ; it != boost::end(geometry) ; ++it )
  281. {
  282. int pip = point_in_geometry<polygon_type>::apply(point, *it, strategy);
  283. // inside or on the boundary
  284. if ( pip >= 0 )
  285. return pip;
  286. // For invalid multi-polygons
  287. //if ( 1 == pip ) // inside polygon
  288. // return 1;
  289. //else if ( res < pip ) // point must be inside at least one polygon
  290. // res = pip;
  291. }
  292. return -1; // for valid multipolygons
  293. //return res; // for invalid multipolygons
  294. }
  295. };
  296. }} // namespace detail_dispatch::within
  297. namespace detail { namespace within {
  298. // 1 - in the interior
  299. // 0 - in the boundry
  300. // -1 - in the exterior
  301. template <typename Point, typename Geometry, typename Strategy>
  302. inline int point_in_geometry(Point const& point, Geometry const& geometry, Strategy const& strategy)
  303. {
  304. concepts::within::check<Point, Geometry, Strategy>();
  305. return detail_dispatch::within::point_in_geometry<Geometry>::apply(point, geometry, strategy);
  306. }
  307. template <typename Point, typename Geometry>
  308. inline int point_in_geometry(Point const& point, Geometry const& geometry)
  309. {
  310. typedef typename strategy::point_in_geometry::services::default_strategy
  311. <
  312. Point, Geometry
  313. >::type strategy_type;
  314. return point_in_geometry(point, geometry, strategy_type());
  315. }
  316. template <typename Point, typename Geometry, typename Strategy>
  317. inline bool within_point_geometry(Point const& point, Geometry const& geometry, Strategy const& strategy)
  318. {
  319. return point_in_geometry(point, geometry, strategy) > 0;
  320. }
  321. template <typename Point, typename Geometry>
  322. inline bool within_point_geometry(Point const& point, Geometry const& geometry)
  323. {
  324. return point_in_geometry(point, geometry) > 0;
  325. }
  326. template <typename Point, typename Geometry, typename Strategy>
  327. inline bool covered_by_point_geometry(Point const& point, Geometry const& geometry, Strategy const& strategy)
  328. {
  329. return point_in_geometry(point, geometry, strategy) >= 0;
  330. }
  331. template <typename Point, typename Geometry>
  332. inline bool covered_by_point_geometry(Point const& point, Geometry const& geometry)
  333. {
  334. return point_in_geometry(point, geometry) >= 0;
  335. }
  336. }} // namespace detail::within
  337. #endif // DOXYGEN_NO_DETAIL
  338. }} // namespace boost::geometry
  339. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_WITHIN_POINT_IN_GEOMETRY_HPP