intersection.hpp 33 KB

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