point_in_geometry.hpp 12 KB

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