intersection.hpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. // Boost.Geometry
  2. // Copyright (c) 2017 Adam Wulkiewicz, Lodz, Poland.
  3. // Copyright (c) 2016-2018, Oracle and/or its affiliates.
  4. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #ifndef BOOST_GEOMETRY_STRATEGIES_GEOGRAPHIC_INTERSECTION_HPP
  9. #define BOOST_GEOMETRY_STRATEGIES_GEOGRAPHIC_INTERSECTION_HPP
  10. #include <algorithm>
  11. #include <boost/geometry/core/cs.hpp>
  12. #include <boost/geometry/core/access.hpp>
  13. #include <boost/geometry/core/radian_access.hpp>
  14. #include <boost/geometry/core/tags.hpp>
  15. #include <boost/geometry/algorithms/detail/assign_values.hpp>
  16. #include <boost/geometry/algorithms/detail/assign_indexed_point.hpp>
  17. #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
  18. #include <boost/geometry/algorithms/detail/recalculate.hpp>
  19. #include <boost/geometry/formulas/andoyer_inverse.hpp>
  20. #include <boost/geometry/formulas/sjoberg_intersection.hpp>
  21. #include <boost/geometry/formulas/spherical.hpp>
  22. #include <boost/geometry/formulas/unit_spheroid.hpp>
  23. #include <boost/geometry/geometries/concepts/point_concept.hpp>
  24. #include <boost/geometry/geometries/concepts/segment_concept.hpp>
  25. #include <boost/geometry/policies/robustness/segment_ratio.hpp>
  26. #include <boost/geometry/srs/spheroid.hpp>
  27. #include <boost/geometry/strategies/geographic/area.hpp>
  28. #include <boost/geometry/strategies/geographic/distance.hpp>
  29. #include <boost/geometry/strategies/geographic/envelope_segment.hpp>
  30. #include <boost/geometry/strategies/geographic/parameters.hpp>
  31. #include <boost/geometry/strategies/geographic/point_in_poly_winding.hpp>
  32. #include <boost/geometry/strategies/geographic/side.hpp>
  33. #include <boost/geometry/strategies/intersection.hpp>
  34. #include <boost/geometry/strategies/intersection_result.hpp>
  35. #include <boost/geometry/strategies/side_info.hpp>
  36. #include <boost/geometry/util/math.hpp>
  37. #include <boost/geometry/util/select_calculation_type.hpp>
  38. namespace boost { namespace geometry
  39. {
  40. namespace strategy { namespace intersection
  41. {
  42. // CONSIDER: Improvement of the robustness/accuracy/repeatability by
  43. // moving all segments to 0 longitude
  44. // picking latitudes closer to 0
  45. // etc.
  46. template
  47. <
  48. typename FormulaPolicy = strategy::andoyer,
  49. unsigned int Order = strategy::default_order<FormulaPolicy>::value,
  50. typename Spheroid = srs::spheroid<double>,
  51. typename CalculationType = void
  52. >
  53. struct geographic_segments
  54. {
  55. typedef side::geographic
  56. <
  57. FormulaPolicy, Spheroid, CalculationType
  58. > side_strategy_type;
  59. inline side_strategy_type get_side_strategy() const
  60. {
  61. return side_strategy_type(m_spheroid);
  62. }
  63. template <typename Geometry1, typename Geometry2>
  64. struct point_in_geometry_strategy
  65. {
  66. typedef strategy::within::geographic_winding
  67. <
  68. typename point_type<Geometry1>::type,
  69. typename point_type<Geometry2>::type,
  70. FormulaPolicy,
  71. Spheroid,
  72. CalculationType
  73. > type;
  74. };
  75. template <typename Geometry1, typename Geometry2>
  76. inline typename point_in_geometry_strategy<Geometry1, Geometry2>::type
  77. get_point_in_geometry_strategy() const
  78. {
  79. typedef typename point_in_geometry_strategy
  80. <
  81. Geometry1, Geometry2
  82. >::type strategy_type;
  83. return strategy_type(m_spheroid);
  84. }
  85. template <typename Geometry>
  86. struct area_strategy
  87. {
  88. typedef area::geographic
  89. <
  90. FormulaPolicy,
  91. Order,
  92. Spheroid,
  93. CalculationType
  94. > type;
  95. };
  96. template <typename Geometry>
  97. inline typename area_strategy<Geometry>::type get_area_strategy() const
  98. {
  99. typedef typename area_strategy<Geometry>::type strategy_type;
  100. return strategy_type(m_spheroid);
  101. }
  102. template <typename Geometry>
  103. struct distance_strategy
  104. {
  105. typedef distance::geographic
  106. <
  107. FormulaPolicy,
  108. Spheroid,
  109. CalculationType
  110. > type;
  111. };
  112. template <typename Geometry>
  113. inline typename distance_strategy<Geometry>::type get_distance_strategy() const
  114. {
  115. typedef typename distance_strategy<Geometry>::type strategy_type;
  116. return strategy_type(m_spheroid);
  117. }
  118. typedef envelope::geographic_segment<FormulaPolicy, Spheroid, CalculationType>
  119. envelope_strategy_type;
  120. inline envelope_strategy_type get_envelope_strategy() const
  121. {
  122. return envelope_strategy_type(m_spheroid);
  123. }
  124. enum intersection_point_flag { ipi_inters = 0, ipi_at_a1, ipi_at_a2, ipi_at_b1, ipi_at_b2 };
  125. template <typename CoordinateType, typename SegmentRatio>
  126. struct segment_intersection_info
  127. {
  128. template <typename Point, typename Segment1, typename Segment2>
  129. void calculate(Point& point, Segment1 const& a, Segment2 const& b) const
  130. {
  131. if (ip_flag == ipi_inters)
  132. {
  133. // TODO: assign the rest of coordinates
  134. set_from_radian<0>(point, lon);
  135. set_from_radian<1>(point, lat);
  136. }
  137. else if (ip_flag == ipi_at_a1)
  138. {
  139. detail::assign_point_from_index<0>(a, point);
  140. }
  141. else if (ip_flag == ipi_at_a2)
  142. {
  143. detail::assign_point_from_index<1>(a, point);
  144. }
  145. else if (ip_flag == ipi_at_b1)
  146. {
  147. detail::assign_point_from_index<0>(b, point);
  148. }
  149. else // ip_flag == ipi_at_b2
  150. {
  151. detail::assign_point_from_index<1>(b, point);
  152. }
  153. }
  154. CoordinateType lon;
  155. CoordinateType lat;
  156. SegmentRatio robust_ra;
  157. SegmentRatio robust_rb;
  158. intersection_point_flag ip_flag;
  159. };
  160. explicit geographic_segments(Spheroid const& spheroid = Spheroid())
  161. : m_spheroid(spheroid)
  162. {}
  163. // Relate segments a and b
  164. template
  165. <
  166. typename Segment1,
  167. typename Segment2,
  168. typename Policy,
  169. typename RobustPolicy
  170. >
  171. inline typename Policy::return_type apply(Segment1 const& a, Segment2 const& b,
  172. Policy const& policy,
  173. RobustPolicy const& robust_policy) const
  174. {
  175. typedef typename point_type<Segment1>::type point1_t;
  176. typedef typename point_type<Segment2>::type point2_t;
  177. point1_t a1, a2;
  178. point2_t b1, b2;
  179. detail::assign_point_from_index<0>(a, a1);
  180. detail::assign_point_from_index<1>(a, a2);
  181. detail::assign_point_from_index<0>(b, b1);
  182. detail::assign_point_from_index<1>(b, b2);
  183. return apply(a, b, policy, robust_policy, a1, a2, b1, b2);
  184. }
  185. // Relate segments a and b
  186. template
  187. <
  188. typename Segment1,
  189. typename Segment2,
  190. typename Policy,
  191. typename RobustPolicy,
  192. typename Point1,
  193. typename Point2
  194. >
  195. inline typename Policy::return_type apply(Segment1 const& a, Segment2 const& b,
  196. Policy const&, RobustPolicy const&,
  197. Point1 a1, Point1 a2, Point2 b1, Point2 b2) const
  198. {
  199. bool is_a_reversed = get<1>(a1) > get<1>(a2);
  200. bool is_b_reversed = get<1>(b1) > get<1>(b2);
  201. if (is_a_reversed)
  202. {
  203. std::swap(a1, a2);
  204. }
  205. if (is_b_reversed)
  206. {
  207. std::swap(b1, b2);
  208. }
  209. return apply<Policy>(a, b, a1, a2, b1, b2, is_a_reversed, is_b_reversed);
  210. }
  211. private:
  212. // Relate segments a and b
  213. template
  214. <
  215. typename Policy,
  216. typename Segment1,
  217. typename Segment2,
  218. typename Point1,
  219. typename Point2
  220. >
  221. inline typename Policy::return_type apply(Segment1 const& a, Segment2 const& b,
  222. Point1 const& a1, Point1 const& a2,
  223. Point2 const& b1, Point2 const& b2,
  224. bool is_a_reversed, bool is_b_reversed) const
  225. {
  226. BOOST_CONCEPT_ASSERT( (concepts::ConstSegment<Segment1>) );
  227. BOOST_CONCEPT_ASSERT( (concepts::ConstSegment<Segment2>) );
  228. typedef typename select_calculation_type
  229. <Segment1, Segment2, CalculationType>::type calc_t;
  230. typedef srs::spheroid<calc_t> spheroid_type;
  231. static const calc_t c0 = 0;
  232. // normalized spheroid
  233. spheroid_type spheroid = formula::unit_spheroid<spheroid_type>(m_spheroid);
  234. // TODO: check only 2 first coordinates here?
  235. using geometry::detail::equals::equals_point_point;
  236. bool a_is_point = equals_point_point(a1, a2);
  237. bool b_is_point = equals_point_point(b1, b2);
  238. if(a_is_point && b_is_point)
  239. {
  240. return equals_point_point(a1, b2)
  241. ? Policy::degenerate(a, true)
  242. : Policy::disjoint()
  243. ;
  244. }
  245. calc_t const a1_lon = get_as_radian<0>(a1);
  246. calc_t const a1_lat = get_as_radian<1>(a1);
  247. calc_t const a2_lon = get_as_radian<0>(a2);
  248. calc_t const a2_lat = get_as_radian<1>(a2);
  249. calc_t const b1_lon = get_as_radian<0>(b1);
  250. calc_t const b1_lat = get_as_radian<1>(b1);
  251. calc_t const b2_lon = get_as_radian<0>(b2);
  252. calc_t const b2_lat = get_as_radian<1>(b2);
  253. side_info sides;
  254. // NOTE: potential optimization, don't calculate distance at this point
  255. // this would require to reimplement inverse strategy to allow
  256. // calculation of distance if needed, probably also storing intermediate
  257. // results somehow inside an object.
  258. typedef typename FormulaPolicy::template inverse<calc_t, true, true, false, false, false> inverse_dist_azi;
  259. typedef typename inverse_dist_azi::result_type inverse_result;
  260. // TODO: no need to call inverse formula if we know that the points are equal
  261. // distance can be set to 0 in this case and azimuth may be not calculated
  262. bool is_equal_a1_b1 = equals_point_point(a1, b1);
  263. bool is_equal_a2_b1 = equals_point_point(a2, b1);
  264. bool degen_neq_coords = false;
  265. inverse_result res_b1_b2, res_b1_a1, res_b1_a2;
  266. if (! b_is_point)
  267. {
  268. res_b1_b2 = inverse_dist_azi::apply(b1_lon, b1_lat, b2_lon, b2_lat, spheroid);
  269. if (math::equals(res_b1_b2.distance, c0))
  270. {
  271. b_is_point = true;
  272. degen_neq_coords = true;
  273. }
  274. else
  275. {
  276. res_b1_a1 = inverse_dist_azi::apply(b1_lon, b1_lat, a1_lon, a1_lat, spheroid);
  277. if (math::equals(res_b1_a1.distance, c0))
  278. {
  279. is_equal_a1_b1 = true;
  280. }
  281. res_b1_a2 = inverse_dist_azi::apply(b1_lon, b1_lat, a2_lon, a2_lat, spheroid);
  282. if (math::equals(res_b1_a2.distance, c0))
  283. {
  284. is_equal_a2_b1 = true;
  285. }
  286. sides.set<0>(is_equal_a1_b1 ? 0 : formula::azimuth_side_value(res_b1_a1.azimuth, res_b1_b2.azimuth),
  287. is_equal_a2_b1 ? 0 : formula::azimuth_side_value(res_b1_a2.azimuth, res_b1_b2.azimuth));
  288. if (sides.same<0>())
  289. {
  290. // Both points are at the same side of other segment, we can leave
  291. return Policy::disjoint();
  292. }
  293. }
  294. }
  295. bool is_equal_a1_b2 = equals_point_point(a1, b2);
  296. inverse_result res_a1_a2, res_a1_b1, res_a1_b2;
  297. if (! a_is_point)
  298. {
  299. res_a1_a2 = inverse_dist_azi::apply(a1_lon, a1_lat, a2_lon, a2_lat, spheroid);
  300. if (math::equals(res_a1_a2.distance, c0))
  301. {
  302. a_is_point = true;
  303. degen_neq_coords = true;
  304. }
  305. else
  306. {
  307. res_a1_b1 = inverse_dist_azi::apply(a1_lon, a1_lat, b1_lon, b1_lat, spheroid);
  308. if (math::equals(res_a1_b1.distance, c0))
  309. {
  310. is_equal_a1_b1 = true;
  311. }
  312. res_a1_b2 = inverse_dist_azi::apply(a1_lon, a1_lat, b2_lon, b2_lat, spheroid);
  313. if (math::equals(res_a1_b2.distance, c0))
  314. {
  315. is_equal_a1_b2 = true;
  316. }
  317. sides.set<1>(is_equal_a1_b1 ? 0 : formula::azimuth_side_value(res_a1_b1.azimuth, res_a1_a2.azimuth),
  318. is_equal_a1_b2 ? 0 : formula::azimuth_side_value(res_a1_b2.azimuth, res_a1_a2.azimuth));
  319. if (sides.same<1>())
  320. {
  321. // Both points are at the same side of other segment, we can leave
  322. return Policy::disjoint();
  323. }
  324. }
  325. }
  326. if(a_is_point && b_is_point)
  327. {
  328. return is_equal_a1_b2
  329. ? Policy::degenerate(a, true)
  330. : Policy::disjoint()
  331. ;
  332. }
  333. // NOTE: at this point the segments may still be disjoint
  334. // NOTE: at this point one of the segments may be degenerated
  335. bool collinear = sides.collinear();
  336. if (! collinear)
  337. {
  338. // WARNING: the side strategy doesn't have the info about the other
  339. // segment so it may return results inconsistent with this intersection
  340. // strategy, as it checks both segments for consistency
  341. if (sides.get<0, 0>() == 0 && sides.get<0, 1>() == 0)
  342. {
  343. collinear = true;
  344. sides.set<1>(0, 0);
  345. }
  346. else if (sides.get<1, 0>() == 0 && sides.get<1, 1>() == 0)
  347. {
  348. collinear = true;
  349. sides.set<0>(0, 0);
  350. }
  351. }
  352. if (collinear)
  353. {
  354. if (a_is_point)
  355. {
  356. return collinear_one_degenerated<Policy, calc_t>(a, true, b1, b2, a1, a2, res_b1_b2, res_b1_a1, res_b1_a2, is_b_reversed, degen_neq_coords);
  357. }
  358. else if (b_is_point)
  359. {
  360. return collinear_one_degenerated<Policy, calc_t>(b, false, a1, a2, b1, b2, res_a1_a2, res_a1_b1, res_a1_b2, is_a_reversed, degen_neq_coords);
  361. }
  362. else
  363. {
  364. calc_t dist_a1_a2, dist_a1_b1, dist_a1_b2;
  365. calc_t dist_b1_b2, dist_b1_a1, dist_b1_a2;
  366. // use shorter segment
  367. if (res_a1_a2.distance <= res_b1_b2.distance)
  368. {
  369. calculate_collinear_data(a1, a2, b1, b2, res_a1_a2, res_a1_b1, res_a1_b2, dist_a1_a2, dist_a1_b1);
  370. calculate_collinear_data(a1, a2, b2, b1, res_a1_a2, res_a1_b2, res_a1_b1, dist_a1_a2, dist_a1_b2);
  371. dist_b1_b2 = dist_a1_b2 - dist_a1_b1;
  372. dist_b1_a1 = -dist_a1_b1;
  373. dist_b1_a2 = dist_a1_a2 - dist_a1_b1;
  374. }
  375. else
  376. {
  377. calculate_collinear_data(b1, b2, a1, a2, res_b1_b2, res_b1_a1, res_b1_a2, dist_b1_b2, dist_b1_a1);
  378. calculate_collinear_data(b1, b2, a2, a1, res_b1_b2, res_b1_a2, res_b1_a1, dist_b1_b2, dist_b1_a2);
  379. dist_a1_a2 = dist_b1_a2 - dist_b1_a1;
  380. dist_a1_b1 = -dist_b1_a1;
  381. dist_a1_b2 = dist_b1_b2 - dist_b1_a1;
  382. }
  383. // NOTE: this is probably not needed
  384. calc_t const c0 = 0;
  385. int a1_on_b = position_value(c0, dist_a1_b1, dist_a1_b2);
  386. int a2_on_b = position_value(dist_a1_a2, dist_a1_b1, dist_a1_b2);
  387. int b1_on_a = position_value(c0, dist_b1_a1, dist_b1_a2);
  388. int b2_on_a = position_value(dist_b1_b2, dist_b1_a1, dist_b1_a2);
  389. if ((a1_on_b < 1 && a2_on_b < 1) || (a1_on_b > 3 && a2_on_b > 3))
  390. {
  391. return Policy::disjoint();
  392. }
  393. if (a1_on_b == 1)
  394. {
  395. dist_b1_a1 = 0;
  396. dist_a1_b1 = 0;
  397. }
  398. else if (a1_on_b == 3)
  399. {
  400. dist_b1_a1 = dist_b1_b2;
  401. dist_a1_b2 = 0;
  402. }
  403. if (a2_on_b == 1)
  404. {
  405. dist_b1_a2 = 0;
  406. dist_a1_b1 = dist_a1_a2;
  407. }
  408. else if (a2_on_b == 3)
  409. {
  410. dist_b1_a2 = dist_b1_b2;
  411. dist_a1_b2 = dist_a1_a2;
  412. }
  413. bool opposite = ! same_direction(res_a1_a2.azimuth, res_b1_b2.azimuth);
  414. // NOTE: If segment was reversed opposite, positions and segment ratios has to be altered
  415. if (is_a_reversed)
  416. {
  417. // opposite
  418. opposite = ! opposite;
  419. // positions
  420. std::swap(a1_on_b, a2_on_b);
  421. b1_on_a = 4 - b1_on_a;
  422. b2_on_a = 4 - b2_on_a;
  423. // distances for ratios
  424. std::swap(dist_b1_a1, dist_b1_a2);
  425. dist_a1_b1 = dist_a1_a2 - dist_a1_b1;
  426. dist_a1_b2 = dist_a1_a2 - dist_a1_b2;
  427. }
  428. if (is_b_reversed)
  429. {
  430. // opposite
  431. opposite = ! opposite;
  432. // positions
  433. a1_on_b = 4 - a1_on_b;
  434. a2_on_b = 4 - a2_on_b;
  435. std::swap(b1_on_a, b2_on_a);
  436. // distances for ratios
  437. dist_b1_a1 = dist_b1_b2 - dist_b1_a1;
  438. dist_b1_a2 = dist_b1_b2 - dist_b1_a2;
  439. std::swap(dist_a1_b1, dist_a1_b2);
  440. }
  441. segment_ratio<calc_t> ra_from(dist_b1_a1, dist_b1_b2);
  442. segment_ratio<calc_t> ra_to(dist_b1_a2, dist_b1_b2);
  443. segment_ratio<calc_t> rb_from(dist_a1_b1, dist_a1_a2);
  444. segment_ratio<calc_t> rb_to(dist_a1_b2, dist_a1_a2);
  445. return Policy::segments_collinear(a, b, opposite,
  446. a1_on_b, a2_on_b, b1_on_a, b2_on_a,
  447. ra_from, ra_to, rb_from, rb_to);
  448. }
  449. }
  450. else // crossing or touching
  451. {
  452. if (a_is_point || b_is_point)
  453. {
  454. return Policy::disjoint();
  455. }
  456. calc_t lon = 0, lat = 0;
  457. intersection_point_flag ip_flag;
  458. calc_t dist_a1_a2, dist_a1_i1, dist_b1_b2, dist_b1_i1;
  459. if (calculate_ip_data(a1, a2, b1, b2,
  460. a1_lon, a1_lat, a2_lon, a2_lat,
  461. b1_lon, b1_lat, b2_lon, b2_lat,
  462. res_a1_a2, res_a1_b1, res_a1_b2,
  463. res_b1_b2, res_b1_a1, res_b1_a2,
  464. sides, spheroid,
  465. lon, lat,
  466. dist_a1_a2, dist_a1_i1, dist_b1_b2, dist_b1_i1,
  467. ip_flag))
  468. {
  469. // NOTE: If segment was reversed sides and segment ratios has to be altered
  470. if (is_a_reversed)
  471. {
  472. // sides
  473. sides_reverse_segment<0>(sides);
  474. // distance for ratio
  475. dist_a1_i1 = dist_a1_a2 - dist_a1_i1;
  476. // ip flag
  477. ip_flag_reverse_segment(ip_flag, ipi_at_a1, ipi_at_a2);
  478. }
  479. if (is_b_reversed)
  480. {
  481. // sides
  482. sides_reverse_segment<1>(sides);
  483. // distance for ratio
  484. dist_b1_i1 = dist_b1_b2 - dist_b1_i1;
  485. // ip flag
  486. ip_flag_reverse_segment(ip_flag, ipi_at_b1, ipi_at_b2);
  487. }
  488. // intersects
  489. segment_intersection_info
  490. <
  491. calc_t,
  492. segment_ratio<calc_t>
  493. > sinfo;
  494. sinfo.lon = lon;
  495. sinfo.lat = lat;
  496. sinfo.robust_ra.assign(dist_a1_i1, dist_a1_a2);
  497. sinfo.robust_rb.assign(dist_b1_i1, dist_b1_b2);
  498. sinfo.ip_flag = ip_flag;
  499. return Policy::segments_crosses(sides, sinfo, a, b);
  500. }
  501. else
  502. {
  503. return Policy::disjoint();
  504. }
  505. }
  506. }
  507. template <typename Policy, typename CalcT, typename Segment, typename Point1, typename Point2, typename ResultInverse>
  508. static inline typename Policy::return_type
  509. collinear_one_degenerated(Segment const& segment, bool degenerated_a,
  510. Point1 const& a1, Point1 const& a2,
  511. Point2 const& b1, Point2 const& b2,
  512. ResultInverse const& res_a1_a2,
  513. ResultInverse const& res_a1_b1,
  514. ResultInverse const& res_a1_b2,
  515. bool is_other_reversed,
  516. bool degen_neq_coords)
  517. {
  518. CalcT dist_1_2, dist_1_o;
  519. if (! calculate_collinear_data(a1, a2, b1, b2, res_a1_a2, res_a1_b1, res_a1_b2, dist_1_2, dist_1_o, degen_neq_coords))
  520. {
  521. return Policy::disjoint();
  522. }
  523. // NOTE: If segment was reversed segment ratio has to be altered
  524. if (is_other_reversed)
  525. {
  526. // distance for ratio
  527. dist_1_o = dist_1_2 - dist_1_o;
  528. }
  529. return Policy::one_degenerate(segment, segment_ratio<CalcT>(dist_1_o, dist_1_2), degenerated_a);
  530. }
  531. // TODO: instead of checks below test bi against a1 and a2 here?
  532. // in order to make this independent from is_near()
  533. template <typename Point1, typename Point2, typename ResultInverse, typename CalcT>
  534. static inline bool calculate_collinear_data(Point1 const& a1, Point1 const& a2, // in
  535. Point2 const& b1, Point2 const& b2, // in
  536. ResultInverse const& res_a1_a2, // in
  537. ResultInverse const& res_a1_b1, // in
  538. ResultInverse const& res_a1_b2, // in
  539. CalcT& dist_a1_a2, // out
  540. CalcT& dist_a1_b1, // out
  541. bool degen_neq_coords = false) // in
  542. {
  543. dist_a1_a2 = res_a1_a2.distance;
  544. dist_a1_b1 = res_a1_b1.distance;
  545. if (! same_direction(res_a1_b1.azimuth, res_a1_a2.azimuth))
  546. {
  547. dist_a1_b1 = -dist_a1_b1;
  548. }
  549. // if b1 is close a1
  550. if (is_endpoint_equal(dist_a1_b1, a1, b1))
  551. {
  552. dist_a1_b1 = 0;
  553. return true;
  554. }
  555. // if b1 is close a2
  556. else if (is_endpoint_equal(dist_a1_a2 - dist_a1_b1, a2, b1))
  557. {
  558. dist_a1_b1 = dist_a1_a2;
  559. return true;
  560. }
  561. // check the other endpoint of degenerated segment near a pole
  562. if (degen_neq_coords)
  563. {
  564. static CalcT const c0 = 0;
  565. if (math::equals(res_a1_b2.distance, c0))
  566. {
  567. dist_a1_b1 = 0;
  568. return true;
  569. }
  570. else if (math::equals(dist_a1_a2 - res_a1_b2.distance, c0))
  571. {
  572. dist_a1_b1 = dist_a1_a2;
  573. return true;
  574. }
  575. }
  576. // or i1 is on b
  577. return segment_ratio<CalcT>(dist_a1_b1, dist_a1_a2).on_segment();
  578. }
  579. template <typename Point1, typename Point2, typename CalcT, typename ResultInverse, typename Spheroid_>
  580. static inline bool calculate_ip_data(Point1 const& a1, Point1 const& a2, // in
  581. Point2 const& b1, Point2 const& b2, // in
  582. CalcT const& a1_lon, CalcT const& a1_lat, // in
  583. CalcT const& a2_lon, CalcT const& a2_lat, // in
  584. CalcT const& b1_lon, CalcT const& b1_lat, // in
  585. CalcT const& b2_lon, CalcT const& b2_lat, // in
  586. ResultInverse const& res_a1_a2, // in
  587. ResultInverse const& res_a1_b1, // in
  588. ResultInverse const& res_a1_b2, // in
  589. ResultInverse const& res_b1_b2, // in
  590. ResultInverse const& res_b1_a1, // in
  591. ResultInverse const& res_b1_a2, // in
  592. side_info const& sides, // in
  593. Spheroid_ const& spheroid, // in
  594. CalcT & lon, CalcT & lat, // out
  595. CalcT& dist_a1_a2, CalcT& dist_a1_ip, // out
  596. CalcT& dist_b1_b2, CalcT& dist_b1_ip, // out
  597. intersection_point_flag& ip_flag) // out
  598. {
  599. dist_a1_a2 = res_a1_a2.distance;
  600. dist_b1_b2 = res_b1_b2.distance;
  601. // assign the IP if some endpoints overlap
  602. using geometry::detail::equals::equals_point_point;
  603. if (equals_point_point(a1, b1))
  604. {
  605. lon = a1_lon;
  606. lat = a1_lat;
  607. dist_a1_ip = 0;
  608. dist_b1_ip = 0;
  609. ip_flag = ipi_at_a1;
  610. return true;
  611. }
  612. else if (equals_point_point(a1, b2))
  613. {
  614. lon = a1_lon;
  615. lat = a1_lat;
  616. dist_a1_ip = 0;
  617. dist_b1_ip = dist_b1_b2;
  618. ip_flag = ipi_at_a1;
  619. return true;
  620. }
  621. else if (equals_point_point(a2, b1))
  622. {
  623. lon = a2_lon;
  624. lat = a2_lat;
  625. dist_a1_ip = dist_a1_a2;
  626. dist_b1_ip = 0;
  627. ip_flag = ipi_at_a2;
  628. return true;
  629. }
  630. else if (equals_point_point(a2, b2))
  631. {
  632. lon = a2_lon;
  633. lat = a2_lat;
  634. dist_a1_ip = dist_a1_a2;
  635. dist_b1_ip = dist_b1_b2;
  636. ip_flag = ipi_at_a2;
  637. return true;
  638. }
  639. // at this point we know that the endpoints doesn't overlap
  640. // check cases when an endpoint lies on the other geodesic
  641. if (sides.template get<0, 0>() == 0) // a1 wrt b
  642. {
  643. if (res_b1_a1.distance <= res_b1_b2.distance
  644. && same_direction(res_b1_a1.azimuth, res_b1_b2.azimuth))
  645. {
  646. lon = a1_lon;
  647. lat = a1_lat;
  648. dist_a1_ip = 0;
  649. dist_b1_ip = res_b1_a1.distance;
  650. ip_flag = ipi_at_a1;
  651. return true;
  652. }
  653. else
  654. {
  655. return false;
  656. }
  657. }
  658. else if (sides.template get<0, 1>() == 0) // a2 wrt b
  659. {
  660. if (res_b1_a2.distance <= res_b1_b2.distance
  661. && same_direction(res_b1_a2.azimuth, res_b1_b2.azimuth))
  662. {
  663. lon = a2_lon;
  664. lat = a2_lat;
  665. dist_a1_ip = res_a1_a2.distance;
  666. dist_b1_ip = res_b1_a2.distance;
  667. ip_flag = ipi_at_a2;
  668. return true;
  669. }
  670. else
  671. {
  672. return false;
  673. }
  674. }
  675. else if (sides.template get<1, 0>() == 0) // b1 wrt a
  676. {
  677. if (res_a1_b1.distance <= res_a1_a2.distance
  678. && same_direction(res_a1_b1.azimuth, res_a1_a2.azimuth))
  679. {
  680. lon = b1_lon;
  681. lat = b1_lat;
  682. dist_a1_ip = res_a1_b1.distance;
  683. dist_b1_ip = 0;
  684. ip_flag = ipi_at_b1;
  685. return true;
  686. }
  687. else
  688. {
  689. return false;
  690. }
  691. }
  692. else if (sides.template get<1, 1>() == 0) // b2 wrt a
  693. {
  694. if (res_a1_b2.distance <= res_a1_a2.distance
  695. && same_direction(res_a1_b2.azimuth, res_a1_a2.azimuth))
  696. {
  697. lon = b2_lon;
  698. lat = b2_lat;
  699. dist_a1_ip = res_a1_b2.distance;
  700. dist_b1_ip = res_b1_b2.distance;
  701. ip_flag = ipi_at_b2;
  702. return true;
  703. }
  704. else
  705. {
  706. return false;
  707. }
  708. }
  709. // At this point neither the endpoints overlaps
  710. // nor any andpoint lies on the other geodesic
  711. // So the endpoints should lie on the opposite sides of both geodesics
  712. bool const ok = formula::sjoberg_intersection<CalcT, FormulaPolicy::template inverse, Order>
  713. ::apply(a1_lon, a1_lat, a2_lon, a2_lat, res_a1_a2.azimuth,
  714. b1_lon, b1_lat, b2_lon, b2_lat, res_b1_b2.azimuth,
  715. lon, lat, spheroid);
  716. if (! ok)
  717. {
  718. return false;
  719. }
  720. typedef typename FormulaPolicy::template inverse<CalcT, true, true, false, false, false> inverse_dist_azi;
  721. typedef typename inverse_dist_azi::result_type inverse_result;
  722. inverse_result const res_a1_ip = inverse_dist_azi::apply(a1_lon, a1_lat, lon, lat, spheroid);
  723. dist_a1_ip = res_a1_ip.distance;
  724. if (! same_direction(res_a1_ip.azimuth, res_a1_a2.azimuth))
  725. {
  726. dist_a1_ip = -dist_a1_ip;
  727. }
  728. bool is_on_a = segment_ratio<CalcT>(dist_a1_ip, dist_a1_a2).on_segment();
  729. // NOTE: not fully consistent with equals_point_point() since radians are always used.
  730. bool is_on_a1 = math::equals(lon, a1_lon) && math::equals(lat, a1_lat);
  731. bool is_on_a2 = math::equals(lon, a2_lon) && math::equals(lat, a2_lat);
  732. if (! (is_on_a || is_on_a1 || is_on_a2))
  733. {
  734. return false;
  735. }
  736. inverse_result const res_b1_ip = inverse_dist_azi::apply(b1_lon, b1_lat, lon, lat, spheroid);
  737. dist_b1_ip = res_b1_ip.distance;
  738. if (! same_direction(res_b1_ip.azimuth, res_b1_b2.azimuth))
  739. {
  740. dist_b1_ip = -dist_b1_ip;
  741. }
  742. bool is_on_b = segment_ratio<CalcT>(dist_b1_ip, dist_b1_b2).on_segment();
  743. // NOTE: not fully consistent with equals_point_point() since radians are always used.
  744. bool is_on_b1 = math::equals(lon, b1_lon) && math::equals(lat, b1_lat);
  745. bool is_on_b2 = math::equals(lon, b2_lon) && math::equals(lat, b2_lat);
  746. if (! (is_on_b || is_on_b1 || is_on_b2))
  747. {
  748. return false;
  749. }
  750. ip_flag = ipi_inters;
  751. if (is_on_b1)
  752. {
  753. lon = b1_lon;
  754. lat = b1_lat;
  755. dist_b1_ip = 0;
  756. ip_flag = ipi_at_b1;
  757. }
  758. else if (is_on_b2)
  759. {
  760. lon = b2_lon;
  761. lat = b2_lat;
  762. dist_b1_ip = res_b1_b2.distance;
  763. ip_flag = ipi_at_b2;
  764. }
  765. if (is_on_a1)
  766. {
  767. lon = a1_lon;
  768. lat = a1_lat;
  769. dist_a1_ip = 0;
  770. ip_flag = ipi_at_a1;
  771. }
  772. else if (is_on_a2)
  773. {
  774. lon = a2_lon;
  775. lat = a2_lat;
  776. dist_a1_ip = res_a1_a2.distance;
  777. ip_flag = ipi_at_a2;
  778. }
  779. return true;
  780. }
  781. template <typename CalcT, typename P1, typename P2>
  782. static inline bool is_endpoint_equal(CalcT const& dist,
  783. P1 const& ai, P2 const& b1)
  784. {
  785. static CalcT const c0 = 0;
  786. using geometry::detail::equals::equals_point_point;
  787. return is_near(dist) && (math::equals(dist, c0) || equals_point_point(ai, b1));
  788. }
  789. template <typename CalcT>
  790. static inline bool is_near(CalcT const& dist)
  791. {
  792. // NOTE: This strongly depends on the Inverse method
  793. CalcT const small_number = CalcT(boost::is_same<CalcT, float>::value ? 0.0001 : 0.00000001);
  794. return math::abs(dist) <= small_number;
  795. }
  796. template <typename ProjCoord1, typename ProjCoord2>
  797. static inline int position_value(ProjCoord1 const& ca1,
  798. ProjCoord2 const& cb1,
  799. ProjCoord2 const& cb2)
  800. {
  801. // S1x 0 1 2 3 4
  802. // S2 |---------->
  803. return math::equals(ca1, cb1) ? 1
  804. : math::equals(ca1, cb2) ? 3
  805. : cb1 < cb2 ?
  806. ( ca1 < cb1 ? 0
  807. : ca1 > cb2 ? 4
  808. : 2 )
  809. : ( ca1 > cb1 ? 0
  810. : ca1 < cb2 ? 4
  811. : 2 );
  812. }
  813. template <typename CalcT>
  814. static inline bool same_direction(CalcT const& azimuth1, CalcT const& azimuth2)
  815. {
  816. // distance between two angles normalized to (-180, 180]
  817. CalcT const angle_diff = math::longitude_distance_signed<radian>(azimuth1, azimuth2);
  818. return math::abs(angle_diff) <= math::half_pi<CalcT>();
  819. }
  820. template <int Which>
  821. static inline void sides_reverse_segment(side_info & sides)
  822. {
  823. // names assuming segment A is reversed (Which == 0)
  824. int a1_wrt_b = sides.template get<Which, 0>();
  825. int a2_wrt_b = sides.template get<Which, 1>();
  826. std::swap(a1_wrt_b, a2_wrt_b);
  827. sides.template set<Which>(a1_wrt_b, a2_wrt_b);
  828. int b1_wrt_a = sides.template get<1 - Which, 0>();
  829. int b2_wrt_a = sides.template get<1 - Which, 1>();
  830. sides.template set<1 - Which>(-b1_wrt_a, -b2_wrt_a);
  831. }
  832. static inline void ip_flag_reverse_segment(intersection_point_flag & ip_flag,
  833. intersection_point_flag const& ipi_at_p1,
  834. intersection_point_flag const& ipi_at_p2)
  835. {
  836. ip_flag = ip_flag == ipi_at_p1 ? ipi_at_p2 :
  837. ip_flag == ipi_at_p2 ? ipi_at_p1 :
  838. ip_flag;
  839. }
  840. private:
  841. Spheroid m_spheroid;
  842. };
  843. }} // namespace strategy::intersection
  844. }} // namespace boost::geometry
  845. #endif // BOOST_GEOMETRY_STRATEGIES_GEOGRAPHIC_INTERSECTION_HPP