intersection.hpp 39 KB

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