intersection.hpp 39 KB

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