point_in_poly_winding.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2013-2017 Adam Wulkiewicz, Lodz, Poland.
  4. // This file was modified by Oracle on 2013, 2014, 2016, 2017, 2018, 2019.
  5. // Modifications copyright (c) 2013-2019 Oracle and/or its affiliates.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  8. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  9. // Use, modification and distribution is subject to the Boost Software License,
  10. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #ifndef BOOST_GEOMETRY_STRATEGY_SPHERICAL_POINT_IN_POLY_WINDING_HPP
  13. #define BOOST_GEOMETRY_STRATEGY_SPHERICAL_POINT_IN_POLY_WINDING_HPP
  14. #include <boost/geometry/core/access.hpp>
  15. #include <boost/geometry/core/coordinate_system.hpp>
  16. #include <boost/geometry/core/cs.hpp>
  17. #include <boost/geometry/core/tags.hpp>
  18. #include <boost/geometry/util/math.hpp>
  19. #include <boost/geometry/util/select_calculation_type.hpp>
  20. #include <boost/geometry/util/normalize_spheroidal_coordinates.hpp>
  21. #include <boost/geometry/strategies/cartesian/point_in_box.hpp>
  22. #include <boost/geometry/strategies/covered_by.hpp>
  23. #include <boost/geometry/strategies/side.hpp>
  24. #include <boost/geometry/strategies/spherical/disjoint_box_box.hpp>
  25. #include <boost/geometry/strategies/spherical/ssf.hpp>
  26. #include <boost/geometry/strategies/within.hpp>
  27. namespace boost { namespace geometry
  28. {
  29. namespace strategy { namespace within
  30. {
  31. #ifndef DOXYGEN_NO_DETAIL
  32. namespace detail
  33. {
  34. template <typename SideStrategy, typename CalculationType>
  35. class spherical_winding_base
  36. {
  37. template <typename Point, typename PointOfSegment>
  38. struct calculation_type
  39. : select_calculation_type
  40. <
  41. Point,
  42. PointOfSegment,
  43. CalculationType
  44. >
  45. {};
  46. /*! subclass to keep state */
  47. class counter
  48. {
  49. int m_count;
  50. //int m_count_n;
  51. int m_count_s;
  52. int m_raw_count;
  53. int m_raw_count_anti;
  54. bool m_touches;
  55. inline int code() const
  56. {
  57. if (m_touches)
  58. {
  59. return 0;
  60. }
  61. if (m_raw_count != 0 && m_raw_count_anti != 0)
  62. {
  63. if (m_raw_count > 0) // right, wrap around south pole
  64. {
  65. return (m_count + m_count_s) == 0 ? -1 : 1;
  66. }
  67. else // left, wrap around north pole
  68. {
  69. //return (m_count + m_count_n) == 0 ? -1 : 1;
  70. // m_count_n is 0
  71. return m_count == 0 ? -1 : 1;
  72. }
  73. }
  74. return m_count == 0 ? -1 : 1;
  75. }
  76. public :
  77. friend class spherical_winding_base;
  78. inline counter()
  79. : m_count(0)
  80. //, m_count_n(0)
  81. , m_count_s(0)
  82. , m_raw_count(0)
  83. , m_raw_count_anti(0)
  84. , m_touches(false)
  85. {}
  86. };
  87. struct count_info
  88. {
  89. explicit count_info(int c = 0, bool ia = false)
  90. : count(c)
  91. , is_anti(ia)
  92. {}
  93. int count;
  94. bool is_anti;
  95. };
  96. public:
  97. typedef typename SideStrategy::cs_tag cs_tag;
  98. typedef SideStrategy side_strategy_type;
  99. inline side_strategy_type get_side_strategy() const
  100. {
  101. return m_side_strategy;
  102. }
  103. typedef expand::spherical_point expand_point_strategy_type;
  104. typedef typename SideStrategy::envelope_strategy_type envelope_strategy_type;
  105. inline envelope_strategy_type get_envelope_strategy() const
  106. {
  107. return m_side_strategy.get_envelope_strategy();
  108. }
  109. typedef typename SideStrategy::disjoint_strategy_type disjoint_strategy_type;
  110. inline disjoint_strategy_type get_disjoint_strategy() const
  111. {
  112. return m_side_strategy.get_disjoint_strategy();
  113. }
  114. typedef typename SideStrategy::equals_point_point_strategy_type equals_point_point_strategy_type;
  115. inline equals_point_point_strategy_type get_equals_point_point_strategy() const
  116. {
  117. return m_side_strategy.get_equals_point_point_strategy();
  118. }
  119. typedef disjoint::spherical_box_box disjoint_box_box_strategy_type;
  120. static inline disjoint_box_box_strategy_type get_disjoint_box_box_strategy()
  121. {
  122. return disjoint_box_box_strategy_type();
  123. }
  124. typedef covered_by::spherical_point_box disjoint_point_box_strategy_type;
  125. spherical_winding_base()
  126. {}
  127. template <typename Model>
  128. explicit spherical_winding_base(Model const& model)
  129. : m_side_strategy(model)
  130. {}
  131. // Typedefs and static methods to fulfill the concept
  132. typedef counter state_type;
  133. template <typename Point, typename PointOfSegment>
  134. inline bool apply(Point const& point,
  135. PointOfSegment const& s1, PointOfSegment const& s2,
  136. counter& state) const
  137. {
  138. typedef typename calculation_type<Point, PointOfSegment>::type calc_t;
  139. typedef typename geometry::detail::cs_angular_units<Point>::type units_t;
  140. typedef math::detail::constants_on_spheroid<calc_t, units_t> constants;
  141. bool eq1 = false;
  142. bool eq2 = false;
  143. bool s_antipodal = false;
  144. count_info ci = check_segment(point, s1, s2, state, eq1, eq2, s_antipodal);
  145. if (ci.count != 0)
  146. {
  147. if (! ci.is_anti)
  148. {
  149. int side = 0;
  150. if (ci.count == 1 || ci.count == -1)
  151. {
  152. side = side_equal(point, eq1 ? s1 : s2, ci);
  153. }
  154. else // count == 2 || count == -2
  155. {
  156. if (! s_antipodal)
  157. {
  158. // 1 left, -1 right
  159. side = m_side_strategy.apply(s1, s2, point);
  160. }
  161. else
  162. {
  163. calc_t const pi = constants::half_period();
  164. calc_t const s1_lat = get<1>(s1);
  165. calc_t const s2_lat = get<1>(s2);
  166. side = math::sign(ci.count)
  167. * (pi - s1_lat - s2_lat <= pi // segment goes through north pole
  168. ? -1 // going right all points will be on right side
  169. : 1); // going right all points will be on left side
  170. }
  171. }
  172. if (side == 0)
  173. {
  174. // Point is lying on segment
  175. state.m_touches = true;
  176. state.m_count = 0;
  177. return false;
  178. }
  179. // Side is NEG for right, POS for left.
  180. // The count is -2 for left, 2 for right (or -1/1)
  181. // Side positive thus means RIGHT and LEFTSIDE or LEFT and RIGHTSIDE
  182. // See accompagnying figure (TODO)
  183. if (side * ci.count > 0)
  184. {
  185. state.m_count += ci.count;
  186. }
  187. state.m_raw_count += ci.count;
  188. }
  189. else
  190. {
  191. // Count negated because the segment is on the other side of the globe
  192. // so it is reversed to match this side of the globe
  193. // Assuming geometry wraps around north pole, for segments on the other side of the globe
  194. // the point will always be RIGHT+RIGHTSIDE or LEFT+LEFTSIDE, so side*-count always < 0
  195. //state.m_count_n -= 0;
  196. // Assuming geometry wraps around south pole, for segments on the other side of the globe
  197. // the point will always be RIGHT+LEFTSIDE or LEFT+RIGHTSIDE, so side*-count always > 0
  198. state.m_count_s -= ci.count;
  199. state.m_raw_count_anti -= ci.count;
  200. }
  201. }
  202. return ! state.m_touches;
  203. }
  204. static inline int result(counter const& state)
  205. {
  206. return state.code();
  207. }
  208. protected:
  209. template <typename Point, typename PointOfSegment>
  210. static inline count_info check_segment(Point const& point,
  211. PointOfSegment const& seg1,
  212. PointOfSegment const& seg2,
  213. counter& state,
  214. bool& eq1, bool& eq2, bool& s_antipodal)
  215. {
  216. if (check_touch(point, seg1, seg2, state, eq1, eq2, s_antipodal))
  217. {
  218. return count_info(0, false);
  219. }
  220. return calculate_count(point, seg1, seg2, eq1, eq2, s_antipodal);
  221. }
  222. template <typename Point, typename PointOfSegment>
  223. static inline int check_touch(Point const& point,
  224. PointOfSegment const& seg1,
  225. PointOfSegment const& seg2,
  226. counter& state,
  227. bool& eq1,
  228. bool& eq2,
  229. bool& s_antipodal)
  230. {
  231. typedef typename calculation_type<Point, PointOfSegment>::type calc_t;
  232. typedef typename geometry::detail::cs_angular_units<Point>::type units_t;
  233. typedef math::detail::constants_on_spheroid<calc_t, units_t> constants;
  234. calc_t const c0 = 0;
  235. calc_t const c2 = 2;
  236. calc_t const pi = constants::half_period();
  237. calc_t const half_pi = pi / c2;
  238. calc_t const p_lon = get<0>(point);
  239. calc_t const s1_lon = get<0>(seg1);
  240. calc_t const s2_lon = get<0>(seg2);
  241. calc_t const p_lat = get<1>(point);
  242. calc_t const s1_lat = get<1>(seg1);
  243. calc_t const s2_lat = get<1>(seg2);
  244. // NOTE: lat in {-90, 90} and arbitrary lon
  245. // it doesn't matter what lon it is if it's a pole
  246. // so e.g. if one of the segment endpoints is a pole
  247. // then only the other lon matters
  248. bool eq1_strict = longitudes_equal<units_t>(s1_lon, p_lon);
  249. bool eq2_strict = longitudes_equal<units_t>(s2_lon, p_lon);
  250. bool eq1_anti = false;
  251. bool eq2_anti = false;
  252. calc_t const anti_p_lon = p_lon + (p_lon <= c0 ? pi : -pi);
  253. eq1 = eq1_strict // lon strictly equal to s1
  254. || (eq1_anti = longitudes_equal<units_t>(s1_lon, anti_p_lon)) // anti-lon strictly equal to s1
  255. || math::equals(math::abs(s1_lat), half_pi); // s1 is pole
  256. eq2 = eq2_strict // lon strictly equal to s2
  257. || (eq2_anti = longitudes_equal<units_t>(s2_lon, anti_p_lon)) // anti-lon strictly equal to s2
  258. || math::equals(math::abs(s2_lat), half_pi); // s2 is pole
  259. // segment overlapping pole
  260. calc_t const s_lon_diff = math::longitude_distance_signed<units_t>(s1_lon, s2_lon);
  261. s_antipodal = math::equals(s_lon_diff, pi);
  262. if (s_antipodal)
  263. {
  264. eq1 = eq2 = eq1 || eq2;
  265. // segment overlapping pole and point is pole
  266. if (math::equals(math::abs(p_lat), half_pi))
  267. {
  268. eq1 = eq2 = true;
  269. }
  270. }
  271. // Both equal p -> segment vertical
  272. // The only thing which has to be done is check if point is ON segment
  273. if (eq1 && eq2)
  274. {
  275. // segment endpoints on the same sides of the globe
  276. if (! s_antipodal)
  277. {
  278. // p's lat between segment endpoints' lats
  279. if ( (s1_lat <= p_lat && s2_lat >= p_lat) || (s2_lat <= p_lat && s1_lat >= p_lat) )
  280. {
  281. if (!eq1_anti || !eq2_anti)
  282. {
  283. state.m_touches = true;
  284. }
  285. }
  286. }
  287. else
  288. {
  289. // going through north or south pole?
  290. if (pi - s1_lat - s2_lat <= pi)
  291. {
  292. if ( (eq1_strict && s1_lat <= p_lat) || (eq2_strict && s2_lat <= p_lat) // north
  293. || math::equals(p_lat, half_pi) ) // point on north pole
  294. {
  295. state.m_touches = true;
  296. }
  297. else if (! eq1_strict && ! eq2_strict && math::equals(p_lat, -half_pi) ) // point on south pole
  298. {
  299. return false;
  300. }
  301. }
  302. else // south pole
  303. {
  304. if ( (eq1_strict && s1_lat >= p_lat) || (eq2_strict && s2_lat >= p_lat) // south
  305. || math::equals(p_lat, -half_pi) ) // point on south pole
  306. {
  307. state.m_touches = true;
  308. }
  309. else if (! eq1_strict && ! eq2_strict && math::equals(p_lat, half_pi) ) // point on north pole
  310. {
  311. return false;
  312. }
  313. }
  314. }
  315. return true;
  316. }
  317. return false;
  318. }
  319. // Called if point is not aligned with a vertical segment
  320. template <typename Point, typename PointOfSegment>
  321. static inline count_info calculate_count(Point const& point,
  322. PointOfSegment const& seg1,
  323. PointOfSegment const& seg2,
  324. bool eq1, bool eq2, bool s_antipodal)
  325. {
  326. typedef typename calculation_type<Point, PointOfSegment>::type calc_t;
  327. typedef typename geometry::detail::cs_angular_units<Point>::type units_t;
  328. typedef math::detail::constants_on_spheroid<calc_t, units_t> constants;
  329. // If both segment endpoints were poles below checks wouldn't be enough
  330. // but this means that either both are the same or that they are N/S poles
  331. // and therefore the segment is not valid.
  332. // If needed (eq1 && eq2 ? 0) could be returned
  333. calc_t const c0 = 0;
  334. calc_t const pi = constants::half_period();
  335. calc_t const p = get<0>(point);
  336. calc_t const s1 = get<0>(seg1);
  337. calc_t const s2 = get<0>(seg2);
  338. calc_t const s1_p = math::longitude_distance_signed<units_t>(s1, p);
  339. if (s_antipodal)
  340. {
  341. return count_info(s1_p < c0 ? -2 : 2, false); // choose W/E
  342. }
  343. calc_t const s1_s2 = math::longitude_distance_signed<units_t>(s1, s2);
  344. if (eq1 || eq2) // Point on level s1 or s2
  345. {
  346. return count_info(s1_s2 < c0 ? -1 : 1, // choose W/E
  347. longitudes_equal<units_t>(p + pi, (eq1 ? s1 : s2)));
  348. }
  349. // Point between s1 and s2
  350. if ( math::sign(s1_p) == math::sign(s1_s2)
  351. && math::abs(s1_p) < math::abs(s1_s2) )
  352. {
  353. return count_info(s1_s2 < c0 ? -2 : 2, false); // choose W/E
  354. }
  355. calc_t const s1_p_anti = math::longitude_distance_signed<units_t>(s1, p + pi);
  356. // Anti-Point between s1 and s2
  357. if ( math::sign(s1_p_anti) == math::sign(s1_s2)
  358. && math::abs(s1_p_anti) < math::abs(s1_s2) )
  359. {
  360. return count_info(s1_s2 < c0 ? -2 : 2, true); // choose W/E
  361. }
  362. return count_info(0, false);
  363. }
  364. // Fix for https://svn.boost.org/trac/boost/ticket/9628
  365. // For floating point coordinates, the <D> coordinate of a point is compared
  366. // with the segment's points using some EPS. If the coordinates are "equal"
  367. // the sides are calculated. Therefore we can treat a segment as a long areal
  368. // geometry having some width. There is a small ~triangular area somewhere
  369. // between the segment's effective area and a segment's line used in sides
  370. // calculation where the segment is on the one side of the line but on the
  371. // other side of a segment (due to the width).
  372. // Below picture assuming D = 1, if D = 0 horiz<->vert, E<->N, RIGHT<->UP.
  373. // For the s1 of a segment going NE the real side is RIGHT but the point may
  374. // be detected as LEFT, like this:
  375. // RIGHT
  376. // ___----->
  377. // ^ O Pt __ __
  378. // EPS __ __
  379. // v__ __ BUT DETECTED AS LEFT OF THIS LINE
  380. // _____7
  381. // _____/
  382. // _____/
  383. // In the code below actually D = 0, so segments are nearly-vertical
  384. // Called when the point is on the same level as one of the segment's points
  385. // but the point is not aligned with a vertical segment
  386. template <typename Point, typename PointOfSegment>
  387. inline int side_equal(Point const& point,
  388. PointOfSegment const& se,
  389. count_info const& ci) const
  390. {
  391. typedef typename coordinate_type<PointOfSegment>::type scoord_t;
  392. typedef typename geometry::detail::cs_angular_units<Point>::type units_t;
  393. if (math::equals(get<1>(point), get<1>(se)))
  394. {
  395. return 0;
  396. }
  397. // Create a horizontal segment intersecting the original segment's endpoint
  398. // equal to the point, with the derived direction (E/W).
  399. PointOfSegment ss1, ss2;
  400. set<1>(ss1, get<1>(se));
  401. set<0>(ss1, get<0>(se));
  402. set<1>(ss2, get<1>(se));
  403. scoord_t ss20 = get<0>(se);
  404. if (ci.count > 0)
  405. {
  406. ss20 += small_angle<scoord_t, units_t>();
  407. }
  408. else
  409. {
  410. ss20 -= small_angle<scoord_t, units_t>();
  411. }
  412. math::normalize_longitude<units_t>(ss20);
  413. set<0>(ss2, ss20);
  414. // Check the side using this vertical segment
  415. return m_side_strategy.apply(ss1, ss2, point);
  416. }
  417. // 1 deg or pi/180 rad
  418. template <typename CalcT, typename Units>
  419. static inline CalcT small_angle()
  420. {
  421. typedef math::detail::constants_on_spheroid<CalcT, Units> constants;
  422. return constants::half_period() / CalcT(180);
  423. }
  424. template <typename Units, typename CalcT>
  425. static inline bool longitudes_equal(CalcT const& lon1, CalcT const& lon2)
  426. {
  427. return math::equals(
  428. math::longitude_distance_signed<Units>(lon1, lon2),
  429. CalcT(0));
  430. }
  431. SideStrategy m_side_strategy;
  432. };
  433. } // namespace detail
  434. #endif // DOXYGEN_NO_DETAIL
  435. /*!
  436. \brief Within detection using winding rule in spherical coordinate system.
  437. \ingroup strategies
  438. \tparam Point \tparam_point
  439. \tparam PointOfSegment \tparam_segment_point
  440. \tparam CalculationType \tparam_calculation
  441. \qbk{
  442. [heading See also]
  443. [link geometry.reference.algorithms.within.within_3_with_strategy within (with strategy)]
  444. }
  445. */
  446. template
  447. <
  448. typename Point = void, // for backward compatibility
  449. typename PointOfSegment = Point, // for backward compatibility
  450. typename CalculationType = void
  451. >
  452. class spherical_winding
  453. : public within::detail::spherical_winding_base
  454. <
  455. side::spherical_side_formula<CalculationType>,
  456. CalculationType
  457. >
  458. {};
  459. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  460. namespace services
  461. {
  462. template <typename PointLike, typename Geometry, typename AnyTag1, typename AnyTag2>
  463. struct default_strategy<PointLike, Geometry, AnyTag1, AnyTag2, pointlike_tag, polygonal_tag, spherical_tag, spherical_tag>
  464. {
  465. typedef within::detail::spherical_winding_base
  466. <
  467. typename strategy::side::services::default_strategy
  468. <
  469. typename cs_tag<PointLike>::type
  470. >::type,
  471. void
  472. > type;
  473. };
  474. template <typename PointLike, typename Geometry, typename AnyTag1, typename AnyTag2>
  475. struct default_strategy<PointLike, Geometry, AnyTag1, AnyTag2, pointlike_tag, linear_tag, spherical_tag, spherical_tag>
  476. {
  477. typedef within::detail::spherical_winding_base
  478. <
  479. typename strategy::side::services::default_strategy
  480. <
  481. typename cs_tag<PointLike>::type
  482. >::type,
  483. void
  484. > type;
  485. };
  486. } // namespace services
  487. #endif
  488. }} // namespace strategy::within
  489. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  490. namespace strategy { namespace covered_by { namespace services
  491. {
  492. template <typename PointLike, typename Geometry, typename AnyTag1, typename AnyTag2>
  493. struct default_strategy<PointLike, Geometry, AnyTag1, AnyTag2, pointlike_tag, polygonal_tag, spherical_tag, spherical_tag>
  494. {
  495. typedef within::detail::spherical_winding_base
  496. <
  497. typename strategy::side::services::default_strategy
  498. <
  499. typename cs_tag<PointLike>::type
  500. >::type,
  501. void
  502. > type;
  503. };
  504. template <typename PointLike, typename Geometry, typename AnyTag1, typename AnyTag2>
  505. struct default_strategy<PointLike, Geometry, AnyTag1, AnyTag2, pointlike_tag, linear_tag, spherical_tag, spherical_tag>
  506. {
  507. typedef within::detail::spherical_winding_base
  508. <
  509. typename strategy::side::services::default_strategy
  510. <
  511. typename cs_tag<PointLike>::type
  512. >::type,
  513. void
  514. > type;
  515. };
  516. }}} // namespace strategy::covered_by::services
  517. #endif
  518. }} // namespace boost::geometry
  519. #endif // BOOST_GEOMETRY_STRATEGY_SPHERICAL_POINT_IN_POLY_WINDING_HPP