intersection.hpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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_SPHERICAL_INTERSECTION_HPP
  9. #define BOOST_GEOMETRY_STRATEGIES_SPHERICAL_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/arithmetic/arithmetic.hpp>
  21. #include <boost/geometry/arithmetic/cross_product.hpp>
  22. #include <boost/geometry/arithmetic/dot_product.hpp>
  23. #include <boost/geometry/arithmetic/normalize.hpp>
  24. #include <boost/geometry/formulas/spherical.hpp>
  25. #include <boost/geometry/geometries/concepts/point_concept.hpp>
  26. #include <boost/geometry/geometries/concepts/segment_concept.hpp>
  27. #include <boost/geometry/geometries/segment.hpp>
  28. #include <boost/geometry/policies/robustness/segment_ratio.hpp>
  29. #include <boost/geometry/strategy/spherical/area.hpp>
  30. #include <boost/geometry/strategy/spherical/envelope.hpp>
  31. #include <boost/geometry/strategy/spherical/expand_box.hpp>
  32. #include <boost/geometry/strategy/spherical/expand_segment.hpp>
  33. #include <boost/geometry/strategies/covered_by.hpp>
  34. #include <boost/geometry/strategies/intersection.hpp>
  35. #include <boost/geometry/strategies/intersection_result.hpp>
  36. #include <boost/geometry/strategies/side.hpp>
  37. #include <boost/geometry/strategies/side_info.hpp>
  38. #include <boost/geometry/strategies/spherical/disjoint_box_box.hpp>
  39. #include <boost/geometry/strategies/spherical/disjoint_segment_box.hpp>
  40. #include <boost/geometry/strategies/spherical/distance_haversine.hpp>
  41. #include <boost/geometry/strategies/spherical/point_in_point.hpp>
  42. #include <boost/geometry/strategies/spherical/point_in_poly_winding.hpp>
  43. #include <boost/geometry/strategies/spherical/ssf.hpp>
  44. #include <boost/geometry/strategies/within.hpp>
  45. #include <boost/geometry/util/math.hpp>
  46. #include <boost/geometry/util/select_calculation_type.hpp>
  47. namespace boost { namespace geometry
  48. {
  49. namespace strategy { namespace intersection
  50. {
  51. // NOTE:
  52. // The coordinates of crossing IP may be calculated with small precision in some cases.
  53. // For double, near the equator noticed error ~1e-9 so far greater than
  54. // machine epsilon which is ~1e-16. This error is ~0.04m.
  55. // E.g. consider two cases, one near the origin and the second one rotated by 90 deg around Z or SN axis.
  56. // After the conversion from spherical degrees to cartesian 3d the following coordinates
  57. // are calculated:
  58. // for sph (-1 -1, 1 1) deg cart3d ys are -0.017449748351250485 and 0.017449748351250485
  59. // for sph (89 -1, 91 1) deg cart3d xs are 0.017449748351250571 and -0.017449748351250450
  60. // During the conversion degrees must first be converted to radians and then radians
  61. // are passed into trigonometric functions. The error may have several causes:
  62. // 1. Radians cannot represent exactly the same angles as degrees.
  63. // 2. Different longitudes are passed into sin() for x, corresponding to cos() for y,
  64. // and for different angle the error of the result may be different.
  65. // 3. These non-corresponding cartesian coordinates are used in calculation,
  66. // e.g. multiplied several times in cross and dot products.
  67. // If it was a problem this strategy could e.g. "normalize" longitudes before the conversion using the source units
  68. // by rotating the globe around Z axis, so moving longitudes always the same way towards the origin,
  69. // assuming this could help which is not clear.
  70. // For now, intersection points near the endpoints are checked explicitly if needed (if the IP is near the endpoint)
  71. // to generate precise result for them. Only the crossing (i) case may suffer from lower precision.
  72. template
  73. <
  74. typename CalcPolicy,
  75. typename CalculationType = void
  76. >
  77. struct ecef_segments
  78. {
  79. typedef spherical_tag cs_tag;
  80. enum intersection_point_flag { ipi_inters = 0, ipi_at_a1, ipi_at_a2, ipi_at_b1, ipi_at_b2 };
  81. // segment_intersection_info cannot outlive relate_ecef_segments
  82. template <typename CoordinateType, typename SegmentRatio, typename Vector3d>
  83. struct segment_intersection_info
  84. {
  85. segment_intersection_info(CalcPolicy const& calc)
  86. : calc_policy(calc)
  87. {}
  88. template <typename Point, typename Segment1, typename Segment2>
  89. void calculate(Point& point, Segment1 const& a, Segment2 const& b) const
  90. {
  91. if (ip_flag == ipi_inters)
  92. {
  93. // TODO: assign the rest of coordinates
  94. point = calc_policy.template from_cart3d<Point>(intersection_point);
  95. }
  96. else if (ip_flag == ipi_at_a1)
  97. {
  98. detail::assign_point_from_index<0>(a, point);
  99. }
  100. else if (ip_flag == ipi_at_a2)
  101. {
  102. detail::assign_point_from_index<1>(a, point);
  103. }
  104. else if (ip_flag == ipi_at_b1)
  105. {
  106. detail::assign_point_from_index<0>(b, point);
  107. }
  108. else // ip_flag == ipi_at_b2
  109. {
  110. detail::assign_point_from_index<1>(b, point);
  111. }
  112. }
  113. Vector3d intersection_point;
  114. SegmentRatio robust_ra;
  115. SegmentRatio robust_rb;
  116. intersection_point_flag ip_flag;
  117. CalcPolicy const& calc_policy;
  118. };
  119. // Relate segments a and b
  120. template
  121. <
  122. typename UniqueSubRange1,
  123. typename UniqueSubRange2,
  124. typename Policy
  125. >
  126. static inline typename Policy::return_type
  127. apply(UniqueSubRange1 const& range_p, UniqueSubRange2 const& range_q,
  128. Policy const&)
  129. {
  130. // For now create it using default constructor. In the future it could
  131. // be stored in strategy. However then apply() wouldn't be static and
  132. // all relops and setops would have to take the strategy or model.
  133. // Initialize explicitly to prevent compiler errors in case of PoD type
  134. CalcPolicy const calc_policy = CalcPolicy();
  135. typedef typename UniqueSubRange1::point_type point1_type;
  136. typedef typename UniqueSubRange2::point_type point2_type;
  137. BOOST_CONCEPT_ASSERT( (concepts::ConstPoint<point1_type>) );
  138. BOOST_CONCEPT_ASSERT( (concepts::ConstPoint<point2_type>) );
  139. point1_type const& a1 = range_p.at(0);
  140. point1_type const& a2 = range_p.at(1);
  141. point2_type const& b1 = range_q.at(0);
  142. point2_type const& b2 = range_q.at(1);
  143. typedef model::referring_segment<point1_type const> segment1_type;
  144. typedef model::referring_segment<point2_type const> segment2_type;
  145. segment1_type const a(a1, a2);
  146. segment2_type const b(b1, b2);
  147. // TODO: check only 2 first coordinates here?
  148. bool a_is_point = equals_point_point(a1, a2);
  149. bool b_is_point = equals_point_point(b1, b2);
  150. if(a_is_point && b_is_point)
  151. {
  152. return equals_point_point(a1, b2)
  153. ? Policy::degenerate(a, true)
  154. : Policy::disjoint()
  155. ;
  156. }
  157. typedef typename select_calculation_type
  158. <segment1_type, segment2_type, CalculationType>::type calc_t;
  159. calc_t const c0 = 0;
  160. calc_t const c1 = 1;
  161. typedef model::point<calc_t, 3, cs::cartesian> vec3d_t;
  162. vec3d_t const a1v = calc_policy.template to_cart3d<vec3d_t>(a1);
  163. vec3d_t const a2v = calc_policy.template to_cart3d<vec3d_t>(a2);
  164. vec3d_t const b1v = calc_policy.template to_cart3d<vec3d_t>(b1);
  165. vec3d_t const b2v = calc_policy.template to_cart3d<vec3d_t>(b2);
  166. bool degen_neq_coords = false;
  167. side_info sides;
  168. typename CalcPolicy::template plane<vec3d_t>
  169. plane2 = calc_policy.get_plane(b1v, b2v);
  170. calc_t dist_b1_b2 = 0;
  171. if (! b_is_point)
  172. {
  173. calculate_dist(b1v, b2v, plane2, dist_b1_b2);
  174. if (math::equals(dist_b1_b2, c0))
  175. {
  176. degen_neq_coords = true;
  177. b_is_point = true;
  178. dist_b1_b2 = 0;
  179. }
  180. else
  181. {
  182. // not normalized normals, the same as in side strategy
  183. sides.set<0>(plane2.side_value(a1v), plane2.side_value(a2v));
  184. if (sides.same<0>())
  185. {
  186. // Both points are at same side of other segment, we can leave
  187. return Policy::disjoint();
  188. }
  189. }
  190. }
  191. typename CalcPolicy::template plane<vec3d_t>
  192. plane1 = calc_policy.get_plane(a1v, a2v);
  193. calc_t dist_a1_a2 = 0;
  194. if (! a_is_point)
  195. {
  196. calculate_dist(a1v, a2v, plane1, dist_a1_a2);
  197. if (math::equals(dist_a1_a2, c0))
  198. {
  199. degen_neq_coords = true;
  200. a_is_point = true;
  201. dist_a1_a2 = 0;
  202. }
  203. else
  204. {
  205. // not normalized normals, the same as in side strategy
  206. sides.set<1>(plane1.side_value(b1v), plane1.side_value(b2v));
  207. if (sides.same<1>())
  208. {
  209. // Both points are at same side of other segment, we can leave
  210. return Policy::disjoint();
  211. }
  212. }
  213. }
  214. // NOTE: at this point the segments may still be disjoint
  215. calc_t len1 = 0;
  216. // point or opposite sides of a sphere/spheroid, assume point
  217. if (! a_is_point && ! detail::vec_normalize(plane1.normal, len1))
  218. {
  219. a_is_point = true;
  220. if (sides.get<0, 0>() == 0 || sides.get<0, 1>() == 0)
  221. {
  222. sides.set<0>(0, 0);
  223. }
  224. }
  225. calc_t len2 = 0;
  226. if (! b_is_point && ! detail::vec_normalize(plane2.normal, len2))
  227. {
  228. b_is_point = true;
  229. if (sides.get<1, 0>() == 0 || sides.get<1, 1>() == 0)
  230. {
  231. sides.set<1>(0, 0);
  232. }
  233. }
  234. // check both degenerated once more
  235. if (a_is_point && b_is_point)
  236. {
  237. return equals_point_point(a1, b2)
  238. ? Policy::degenerate(a, true)
  239. : Policy::disjoint()
  240. ;
  241. }
  242. // NOTE: at this point the segments may still be disjoint
  243. // NOTE: at this point one of the segments may be degenerated
  244. bool collinear = sides.collinear();
  245. if (! collinear)
  246. {
  247. // NOTE: for some approximations it's possible that both points may lie
  248. // on the same geodesic but still some of the sides may be != 0.
  249. // This is e.g. true for long segments represented as elliptic arcs
  250. // with origin different than the center of the coordinate system.
  251. // So make the sides consistent
  252. // WARNING: the side strategy doesn't have the info about the other
  253. // segment so it may return results inconsistent with this intersection
  254. // strategy, as it checks both segments for consistency
  255. if (sides.get<0, 0>() == 0 && sides.get<0, 1>() == 0)
  256. {
  257. collinear = true;
  258. sides.set<1>(0, 0);
  259. }
  260. else if (sides.get<1, 0>() == 0 && sides.get<1, 1>() == 0)
  261. {
  262. collinear = true;
  263. sides.set<0>(0, 0);
  264. }
  265. }
  266. calc_t dot_n1n2 = dot_product(plane1.normal, plane2.normal);
  267. // NOTE: this is technically not needed since theoretically above sides
  268. // are calculated, but just in case check the normals.
  269. // Have in mind that SSF side strategy doesn't check this.
  270. // collinear if normals are equal or opposite: cos(a) in {-1, 1}
  271. if (! collinear && math::equals(math::abs(dot_n1n2), c1))
  272. {
  273. collinear = true;
  274. sides.set<0>(0, 0);
  275. sides.set<1>(0, 0);
  276. }
  277. if (collinear)
  278. {
  279. if (a_is_point)
  280. {
  281. return collinear_one_degenerated<Policy, calc_t>(a, true, b1, b2, a1, a2, b1v, b2v,
  282. plane2, a1v, a2v, dist_b1_b2, degen_neq_coords);
  283. }
  284. else if (b_is_point)
  285. {
  286. // b2 used to be consistent with (degenerated) checks above (is it needed?)
  287. return collinear_one_degenerated<Policy, calc_t>(b, false, a1, a2, b1, b2, a1v, a2v,
  288. plane1, b1v, b2v, dist_a1_a2, degen_neq_coords);
  289. }
  290. else
  291. {
  292. calc_t dist_a1_b1, dist_a1_b2;
  293. calc_t dist_b1_a1, dist_b1_a2;
  294. calculate_collinear_data(a1, a2, b1, b2, a1v, a2v, plane1, b1v, b2v, dist_a1_a2, dist_a1_b1);
  295. calculate_collinear_data(a1, a2, b2, b1, a1v, a2v, plane1, b2v, b1v, dist_a1_a2, dist_a1_b2);
  296. calculate_collinear_data(b1, b2, a1, a2, b1v, b2v, plane2, a1v, a2v, dist_b1_b2, dist_b1_a1);
  297. calculate_collinear_data(b1, b2, a2, a1, b1v, b2v, plane2, a2v, a1v, dist_b1_b2, dist_b1_a2);
  298. // NOTE: The following optimization causes problems with consitency
  299. // It may either be caused by numerical issues or the way how distance is coded:
  300. // as cosine of angle scaled and translated, see: calculate_dist()
  301. /*dist_b1_b2 = dist_a1_b2 - dist_a1_b1;
  302. dist_b1_a1 = -dist_a1_b1;
  303. dist_b1_a2 = dist_a1_a2 - dist_a1_b1;
  304. dist_a1_a2 = dist_b1_a2 - dist_b1_a1;
  305. dist_a1_b1 = -dist_b1_a1;
  306. dist_a1_b2 = dist_b1_b2 - dist_b1_a1;*/
  307. segment_ratio<calc_t> ra_from(dist_b1_a1, dist_b1_b2);
  308. segment_ratio<calc_t> ra_to(dist_b1_a2, dist_b1_b2);
  309. segment_ratio<calc_t> rb_from(dist_a1_b1, dist_a1_a2);
  310. segment_ratio<calc_t> rb_to(dist_a1_b2, dist_a1_a2);
  311. // NOTE: this is probably not needed
  312. int const a1_wrt_b = position_value(c0, dist_a1_b1, dist_a1_b2);
  313. int const a2_wrt_b = position_value(dist_a1_a2, dist_a1_b1, dist_a1_b2);
  314. int const b1_wrt_a = position_value(c0, dist_b1_a1, dist_b1_a2);
  315. int const b2_wrt_a = position_value(dist_b1_b2, dist_b1_a1, dist_b1_a2);
  316. if (a1_wrt_b == 1)
  317. {
  318. ra_from.assign(0, dist_b1_b2);
  319. rb_from.assign(0, dist_a1_a2);
  320. }
  321. else if (a1_wrt_b == 3)
  322. {
  323. ra_from.assign(dist_b1_b2, dist_b1_b2);
  324. rb_to.assign(0, dist_a1_a2);
  325. }
  326. if (a2_wrt_b == 1)
  327. {
  328. ra_to.assign(0, dist_b1_b2);
  329. rb_from.assign(dist_a1_a2, dist_a1_a2);
  330. }
  331. else if (a2_wrt_b == 3)
  332. {
  333. ra_to.assign(dist_b1_b2, dist_b1_b2);
  334. rb_to.assign(dist_a1_a2, dist_a1_a2);
  335. }
  336. if ((a1_wrt_b < 1 && a2_wrt_b < 1) || (a1_wrt_b > 3 && a2_wrt_b > 3))
  337. {
  338. return Policy::disjoint();
  339. }
  340. bool const opposite = dot_n1n2 < c0;
  341. return Policy::segments_collinear(a, b, opposite,
  342. a1_wrt_b, a2_wrt_b, b1_wrt_a, b2_wrt_a,
  343. ra_from, ra_to, rb_from, rb_to);
  344. }
  345. }
  346. else // crossing
  347. {
  348. if (a_is_point || b_is_point)
  349. {
  350. return Policy::disjoint();
  351. }
  352. vec3d_t i1;
  353. intersection_point_flag ip_flag;
  354. calc_t dist_a1_i1, dist_b1_i1;
  355. if (calculate_ip_data(a1, a2, b1, b2, a1v, a2v, b1v, b2v,
  356. plane1, plane2, calc_policy,
  357. sides, dist_a1_a2, dist_b1_b2,
  358. i1, dist_a1_i1, dist_b1_i1, ip_flag))
  359. {
  360. // intersects
  361. segment_intersection_info
  362. <
  363. calc_t,
  364. segment_ratio<calc_t>,
  365. vec3d_t
  366. > sinfo(calc_policy);
  367. sinfo.robust_ra.assign(dist_a1_i1, dist_a1_a2);
  368. sinfo.robust_rb.assign(dist_b1_i1, dist_b1_b2);
  369. sinfo.intersection_point = i1;
  370. sinfo.ip_flag = ip_flag;
  371. return Policy::segments_crosses(sides, sinfo, a, b);
  372. }
  373. else
  374. {
  375. return Policy::disjoint();
  376. }
  377. }
  378. }
  379. private:
  380. template <typename Policy, typename CalcT, typename Segment, typename Point1, typename Point2, typename Vec3d, typename Plane>
  381. static inline typename Policy::return_type
  382. collinear_one_degenerated(Segment const& segment, bool degenerated_a,
  383. Point1 const& a1, Point1 const& a2,
  384. Point2 const& b1, Point2 const& b2,
  385. Vec3d const& a1v, Vec3d const& a2v,
  386. Plane const& plane,
  387. Vec3d const& b1v, Vec3d const& b2v,
  388. CalcT const& dist_1_2,
  389. bool degen_neq_coords)
  390. {
  391. CalcT dist_1_o;
  392. return ! calculate_collinear_data(a1, a2, b1, b2, a1v, a2v, plane, b1v, b2v, dist_1_2, dist_1_o, degen_neq_coords)
  393. ? Policy::disjoint()
  394. : Policy::one_degenerate(segment, segment_ratio<CalcT>(dist_1_o, dist_1_2), degenerated_a);
  395. }
  396. template <typename Point1, typename Point2, typename Vec3d, typename Plane, typename CalcT>
  397. static inline bool calculate_collinear_data(Point1 const& a1, Point1 const& a2, // in
  398. Point2 const& b1, Point2 const& /*b2*/, // in
  399. Vec3d const& a1v, // in
  400. Vec3d const& a2v, // in
  401. Plane const& plane1, // in
  402. Vec3d const& b1v, // in
  403. Vec3d const& b2v, // in
  404. CalcT const& dist_a1_a2, // in
  405. CalcT& dist_a1_b1, // out
  406. bool degen_neq_coords = false) // in
  407. {
  408. // calculate dist_a1_b1
  409. calculate_dist(a1v, a2v, plane1, b1v, dist_a1_b1);
  410. // if b1 is equal to a1
  411. if (is_endpoint_equal(dist_a1_b1, a1, b1))
  412. {
  413. dist_a1_b1 = 0;
  414. return true;
  415. }
  416. // or b1 is equal to a2
  417. else if (is_endpoint_equal(dist_a1_a2 - dist_a1_b1, a2, b1))
  418. {
  419. dist_a1_b1 = dist_a1_a2;
  420. return true;
  421. }
  422. // check the other endpoint of degenerated segment near a pole
  423. if (degen_neq_coords)
  424. {
  425. static CalcT const c0 = 0;
  426. CalcT dist_a1_b2 = 0;
  427. calculate_dist(a1v, a2v, plane1, b2v, dist_a1_b2);
  428. if (math::equals(dist_a1_b2, c0))
  429. {
  430. dist_a1_b1 = 0;
  431. return true;
  432. }
  433. else if (math::equals(dist_a1_a2 - dist_a1_b2, c0))
  434. {
  435. dist_a1_b1 = dist_a1_a2;
  436. return true;
  437. }
  438. }
  439. // or i1 is on b
  440. return segment_ratio<CalcT>(dist_a1_b1, dist_a1_a2).on_segment();
  441. }
  442. template <typename Point1, typename Point2, typename Vec3d, typename Plane, typename CalcT>
  443. static inline bool calculate_ip_data(Point1 const& a1, Point1 const& a2, // in
  444. Point2 const& b1, Point2 const& b2, // in
  445. Vec3d const& a1v, Vec3d const& a2v, // in
  446. Vec3d const& b1v, Vec3d const& b2v, // in
  447. Plane const& plane1, // in
  448. Plane const& plane2, // in
  449. CalcPolicy const& calc_policy, // in
  450. side_info const& sides, // in
  451. CalcT const& dist_a1_a2, // in
  452. CalcT const& dist_b1_b2, // in
  453. Vec3d & ip, // out
  454. CalcT& dist_a1_ip, // out
  455. CalcT& dist_b1_ip, // out
  456. intersection_point_flag& ip_flag) // out
  457. {
  458. Vec3d ip1, ip2;
  459. calc_policy.intersection_points(plane1, plane2, ip1, ip2);
  460. calculate_dist(a1v, a2v, plane1, ip1, dist_a1_ip);
  461. ip = ip1;
  462. // choose the opposite side of the globe if the distance is shorter
  463. {
  464. CalcT const d = abs_distance(dist_a1_a2, dist_a1_ip);
  465. if (d > CalcT(0))
  466. {
  467. // TODO: this should be ok not only for sphere
  468. // but requires more investigation
  469. CalcT const dist_a1_i2 = dist_of_i2(dist_a1_ip);
  470. CalcT const d2 = abs_distance(dist_a1_a2, dist_a1_i2);
  471. if (d2 < d)
  472. {
  473. dist_a1_ip = dist_a1_i2;
  474. ip = ip2;
  475. }
  476. }
  477. }
  478. bool is_on_a = false, is_near_a1 = false, is_near_a2 = false;
  479. if (! is_potentially_crossing(dist_a1_a2, dist_a1_ip, is_on_a, is_near_a1, is_near_a2))
  480. {
  481. return false;
  482. }
  483. calculate_dist(b1v, b2v, plane2, ip, dist_b1_ip);
  484. bool is_on_b = false, is_near_b1 = false, is_near_b2 = false;
  485. if (! is_potentially_crossing(dist_b1_b2, dist_b1_ip, is_on_b, is_near_b1, is_near_b2))
  486. {
  487. return false;
  488. }
  489. // reassign the IP if some endpoints overlap
  490. if (is_near_a1)
  491. {
  492. if (is_near_b1 && equals_point_point(a1, b1))
  493. {
  494. dist_a1_ip = 0;
  495. dist_b1_ip = 0;
  496. //i1 = a1v;
  497. ip_flag = ipi_at_a1;
  498. return true;
  499. }
  500. if (is_near_b2 && equals_point_point(a1, b2))
  501. {
  502. dist_a1_ip = 0;
  503. dist_b1_ip = dist_b1_b2;
  504. //i1 = a1v;
  505. ip_flag = ipi_at_a1;
  506. return true;
  507. }
  508. }
  509. if (is_near_a2)
  510. {
  511. if (is_near_b1 && equals_point_point(a2, b1))
  512. {
  513. dist_a1_ip = dist_a1_a2;
  514. dist_b1_ip = 0;
  515. //i1 = a2v;
  516. ip_flag = ipi_at_a2;
  517. return true;
  518. }
  519. if (is_near_b2 && equals_point_point(a2, b2))
  520. {
  521. dist_a1_ip = dist_a1_a2;
  522. dist_b1_ip = dist_b1_b2;
  523. //i1 = a2v;
  524. ip_flag = ipi_at_a2;
  525. return true;
  526. }
  527. }
  528. // at this point we know that the endpoints doesn't overlap
  529. // reassign IP and distance if the IP is on a segment and one of
  530. // the endpoints of the other segment lies on the former segment
  531. if (is_on_a)
  532. {
  533. if (is_near_b1 && sides.template get<1, 0>() == 0) // b1 wrt a
  534. {
  535. calculate_dist(a1v, a2v, plane1, b1v, dist_a1_ip); // for consistency
  536. dist_b1_ip = 0;
  537. //i1 = b1v;
  538. ip_flag = ipi_at_b1;
  539. return true;
  540. }
  541. if (is_near_b2 && sides.template get<1, 1>() == 0) // b2 wrt a
  542. {
  543. calculate_dist(a1v, a2v, plane1, b2v, dist_a1_ip); // for consistency
  544. dist_b1_ip = dist_b1_b2;
  545. //i1 = b2v;
  546. ip_flag = ipi_at_b2;
  547. return true;
  548. }
  549. }
  550. if (is_on_b)
  551. {
  552. if (is_near_a1 && sides.template get<0, 0>() == 0) // a1 wrt b
  553. {
  554. dist_a1_ip = 0;
  555. calculate_dist(b1v, b2v, plane2, a1v, dist_b1_ip); // for consistency
  556. //i1 = a1v;
  557. ip_flag = ipi_at_a1;
  558. return true;
  559. }
  560. if (is_near_a2 && sides.template get<0, 1>() == 0) // a2 wrt b
  561. {
  562. dist_a1_ip = dist_a1_a2;
  563. calculate_dist(b1v, b2v, plane2, a2v, dist_b1_ip); // for consistency
  564. //i1 = a2v;
  565. ip_flag = ipi_at_a2;
  566. return true;
  567. }
  568. }
  569. ip_flag = ipi_inters;
  570. return is_on_a && is_on_b;
  571. }
  572. template <typename Vec3d, typename Plane, typename CalcT>
  573. static inline void calculate_dist(Vec3d const& a1v, // in
  574. Vec3d const& a2v, // in
  575. Plane const& plane1, // in
  576. CalcT& dist_a1_a2) // out
  577. {
  578. static CalcT const c1 = 1;
  579. CalcT const cos_a1_a2 = plane1.cos_angle_between(a1v, a2v);
  580. dist_a1_a2 = -cos_a1_a2 + c1; // [1, -1] -> [0, 2] representing [0, pi]
  581. }
  582. template <typename Vec3d, typename Plane, typename CalcT>
  583. static inline void calculate_dist(Vec3d const& a1v, // in
  584. Vec3d const& /*a2v*/, // in
  585. Plane const& plane1, // in
  586. Vec3d const& i1, // in
  587. CalcT& dist_a1_i1) // out
  588. {
  589. static CalcT const c1 = 1;
  590. static CalcT const c2 = 2;
  591. static CalcT const c4 = 4;
  592. bool is_forward = true;
  593. CalcT cos_a1_i1 = plane1.cos_angle_between(a1v, i1, is_forward);
  594. dist_a1_i1 = -cos_a1_i1 + c1; // [0, 2] representing [0, pi]
  595. if (! is_forward) // left or right of a1 on a
  596. {
  597. dist_a1_i1 = -dist_a1_i1; // [0, 2] -> [0, -2] representing [0, -pi]
  598. }
  599. if (dist_a1_i1 <= -c2) // <= -pi
  600. {
  601. dist_a1_i1 += c4; // += 2pi
  602. }
  603. }
  604. /*
  605. template <typename Vec3d, typename Plane, typename CalcT>
  606. static inline void calculate_dists(Vec3d const& a1v, // in
  607. Vec3d const& a2v, // in
  608. Plane const& plane1, // in
  609. Vec3d const& i1, // in
  610. CalcT& dist_a1_a2, // out
  611. CalcT& dist_a1_i1) // out
  612. {
  613. calculate_dist(a1v, a2v, plane1, dist_a1_a2);
  614. calculate_dist(a1v, a2v, plane1, i1, dist_a1_i1);
  615. }
  616. */
  617. // the dist of the ip on the other side of the sphere
  618. template <typename CalcT>
  619. static inline CalcT dist_of_i2(CalcT const& dist_a1_i1)
  620. {
  621. CalcT const c2 = 2;
  622. CalcT const c4 = 4;
  623. CalcT dist_a1_i2 = dist_a1_i1 - c2; // dist_a1_i2 = dist_a1_i1 - pi;
  624. if (dist_a1_i2 <= -c2) // <= -pi
  625. {
  626. dist_a1_i2 += c4; // += 2pi;
  627. }
  628. return dist_a1_i2;
  629. }
  630. template <typename CalcT>
  631. static inline CalcT abs_distance(CalcT const& dist_a1_a2, CalcT const& dist_a1_i1)
  632. {
  633. if (dist_a1_i1 < CalcT(0))
  634. return -dist_a1_i1;
  635. else if (dist_a1_i1 > dist_a1_a2)
  636. return dist_a1_i1 - dist_a1_a2;
  637. else
  638. return CalcT(0);
  639. }
  640. template <typename CalcT>
  641. static inline bool is_potentially_crossing(CalcT const& dist_a1_a2, CalcT const& dist_a1_i1, // in
  642. bool& is_on_a, bool& is_near_a1, bool& is_near_a2) // out
  643. {
  644. is_on_a = segment_ratio<CalcT>(dist_a1_i1, dist_a1_a2).on_segment();
  645. is_near_a1 = is_near(dist_a1_i1);
  646. is_near_a2 = is_near(dist_a1_a2 - dist_a1_i1);
  647. return is_on_a || is_near_a1 || is_near_a2;
  648. }
  649. template <typename CalcT, typename P1, typename P2>
  650. static inline bool is_endpoint_equal(CalcT const& dist,
  651. P1 const& ai, P2 const& b1)
  652. {
  653. static CalcT const c0 = 0;
  654. return is_near(dist) && (math::equals(dist, c0) || equals_point_point(ai, b1));
  655. }
  656. template <typename CalcT>
  657. static inline bool is_near(CalcT const& dist)
  658. {
  659. CalcT const small_number = CalcT(std::is_same<CalcT, float>::value ? 0.0001 : 0.00000001);
  660. return math::abs(dist) <= small_number;
  661. }
  662. template <typename ProjCoord1, typename ProjCoord2>
  663. static inline int position_value(ProjCoord1 const& ca1,
  664. ProjCoord2 const& cb1,
  665. ProjCoord2 const& cb2)
  666. {
  667. // S1x 0 1 2 3 4
  668. // S2 |---------->
  669. return math::equals(ca1, cb1) ? 1
  670. : math::equals(ca1, cb2) ? 3
  671. : cb1 < cb2 ?
  672. ( ca1 < cb1 ? 0
  673. : ca1 > cb2 ? 4
  674. : 2 )
  675. : ( ca1 > cb1 ? 0
  676. : ca1 < cb2 ? 4
  677. : 2 );
  678. }
  679. template <typename Point1, typename Point2>
  680. static inline bool equals_point_point(Point1 const& point1, Point2 const& point2)
  681. {
  682. return strategy::within::spherical_point_point::apply(point1, point2);
  683. }
  684. };
  685. struct spherical_segments_calc_policy
  686. {
  687. template <typename Point, typename Point3d>
  688. static Point from_cart3d(Point3d const& point_3d)
  689. {
  690. return formula::cart3d_to_sph<Point>(point_3d);
  691. }
  692. template <typename Point3d, typename Point>
  693. static Point3d to_cart3d(Point const& point)
  694. {
  695. return formula::sph_to_cart3d<Point3d>(point);
  696. }
  697. template <typename Point3d>
  698. struct plane
  699. {
  700. typedef typename coordinate_type<Point3d>::type coord_t;
  701. // not normalized
  702. plane(Point3d const& p1, Point3d const& p2)
  703. : normal(cross_product(p1, p2))
  704. {}
  705. int side_value(Point3d const& pt) const
  706. {
  707. return formula::sph_side_value(normal, pt);
  708. }
  709. static coord_t cos_angle_between(Point3d const& p1, Point3d const& p2)
  710. {
  711. return dot_product(p1, p2);
  712. }
  713. coord_t cos_angle_between(Point3d const& p1, Point3d const& p2, bool & is_forward) const
  714. {
  715. coord_t const c0 = 0;
  716. is_forward = dot_product(normal, cross_product(p1, p2)) >= c0;
  717. return dot_product(p1, p2);
  718. }
  719. Point3d normal;
  720. };
  721. template <typename Point3d>
  722. static plane<Point3d> get_plane(Point3d const& p1, Point3d const& p2)
  723. {
  724. return plane<Point3d>(p1, p2);
  725. }
  726. template <typename Point3d>
  727. static bool intersection_points(plane<Point3d> const& plane1,
  728. plane<Point3d> const& plane2,
  729. Point3d & ip1, Point3d & ip2)
  730. {
  731. typedef typename coordinate_type<Point3d>::type coord_t;
  732. ip1 = cross_product(plane1.normal, plane2.normal);
  733. // NOTE: the length should be greater than 0 at this point
  734. // if the normals were not normalized and their dot product
  735. // not checked before this function is called the length
  736. // should be checked here (math::equals(len, c0))
  737. coord_t const len = math::sqrt(dot_product(ip1, ip1));
  738. geometry::detail::for_each_dimension<Point3d>([&](auto index)
  739. {
  740. coord_t const coord = get<index>(ip1) / len; // normalize
  741. set<index>(ip1, coord);
  742. set<index>(ip2, -coord);
  743. });
  744. return true;
  745. }
  746. };
  747. template
  748. <
  749. typename CalculationType = void
  750. >
  751. struct spherical_segments
  752. : ecef_segments
  753. <
  754. spherical_segments_calc_policy,
  755. CalculationType
  756. >
  757. {};
  758. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  759. namespace services
  760. {
  761. /*template <typename CalculationType>
  762. struct default_strategy<spherical_polar_tag, CalculationType>
  763. {
  764. typedef spherical_segments<CalculationType> type;
  765. };*/
  766. template <typename CalculationType>
  767. struct default_strategy<spherical_equatorial_tag, CalculationType>
  768. {
  769. typedef spherical_segments<CalculationType> type;
  770. };
  771. template <typename CalculationType>
  772. struct default_strategy<geographic_tag, CalculationType>
  773. {
  774. // NOTE: Spherical strategy returns the same result as the geographic one
  775. // representing segments as great elliptic arcs. If the elliptic arcs are
  776. // not great elliptic arcs (the origin not in the center of the coordinate
  777. // system) then there may be problems with consistency of the side and
  778. // intersection strategies.
  779. typedef spherical_segments<CalculationType> type;
  780. };
  781. } // namespace services
  782. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  783. }} // namespace strategy::intersection
  784. namespace strategy
  785. {
  786. namespace within { namespace services
  787. {
  788. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  789. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, linear_tag, linear_tag, spherical_tag, spherical_tag>
  790. {
  791. typedef strategy::intersection::spherical_segments<> type;
  792. };
  793. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  794. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, linear_tag, polygonal_tag, spherical_tag, spherical_tag>
  795. {
  796. typedef strategy::intersection::spherical_segments<> type;
  797. };
  798. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  799. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, polygonal_tag, linear_tag, spherical_tag, spherical_tag>
  800. {
  801. typedef strategy::intersection::spherical_segments<> type;
  802. };
  803. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  804. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, polygonal_tag, polygonal_tag, spherical_tag, spherical_tag>
  805. {
  806. typedef strategy::intersection::spherical_segments<> type;
  807. };
  808. }} // within::services
  809. namespace covered_by { namespace services
  810. {
  811. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  812. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, linear_tag, linear_tag, spherical_tag, spherical_tag>
  813. {
  814. typedef strategy::intersection::spherical_segments<> type;
  815. };
  816. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  817. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, linear_tag, polygonal_tag, spherical_tag, spherical_tag>
  818. {
  819. typedef strategy::intersection::spherical_segments<> type;
  820. };
  821. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  822. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, polygonal_tag, linear_tag, spherical_tag, spherical_tag>
  823. {
  824. typedef strategy::intersection::spherical_segments<> type;
  825. };
  826. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  827. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, polygonal_tag, polygonal_tag, spherical_tag, spherical_tag>
  828. {
  829. typedef strategy::intersection::spherical_segments<> type;
  830. };
  831. }} // within::services
  832. } // strategy
  833. }} // namespace boost::geometry
  834. #endif // BOOST_GEOMETRY_STRATEGIES_SPHERICAL_INTERSECTION_HPP