intersection.hpp 39 KB

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