segment_box.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2014 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2014 Mateusz Loskot, London, UK.
  5. // Copyright (c) 2013-2014 Adam Wulkiewicz, Lodz, Poland.
  6. // This file was modified by Oracle on 2013-2021.
  7. // Modifications copyright (c) 2013-2021, Oracle and/or its affiliates.
  8. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  9. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  10. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  11. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  12. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  13. // Use, modification and distribution is subject to the Boost Software License,
  14. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt)
  16. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISJOINT_SEGMENT_BOX_HPP
  17. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISJOINT_SEGMENT_BOX_HPP
  18. #include <cstddef>
  19. #include <boost/geometry/core/tags.hpp>
  20. #include <boost/geometry/core/radian_access.hpp>
  21. #include <boost/geometry/algorithms/detail/assign_indexed_point.hpp>
  22. #include <boost/geometry/algorithms/detail/disjoint/point_box.hpp>
  23. #include <boost/geometry/algorithms/detail/disjoint/box_box.hpp>
  24. #include <boost/geometry/algorithms/detail/envelope/segment.hpp>
  25. #include <boost/geometry/algorithms/detail/normalize.hpp>
  26. #include <boost/geometry/algorithms/dispatch/disjoint.hpp>
  27. #include <boost/geometry/formulas/vertex_longitude.hpp>
  28. #include <boost/geometry/geometries/box.hpp>
  29. // Temporary, for envelope_segment_impl
  30. #include <boost/geometry/strategy/spherical/envelope_segment.hpp>
  31. namespace boost { namespace geometry
  32. {
  33. #ifndef DOXYGEN_NO_DETAIL
  34. namespace detail { namespace disjoint
  35. {
  36. template <typename CS_Tag>
  37. struct disjoint_segment_box_sphere_or_spheroid
  38. {
  39. struct disjoint_info
  40. {
  41. enum type
  42. {
  43. intersect,
  44. disjoint_no_vertex,
  45. disjoint_vertex
  46. };
  47. disjoint_info(type t) : m_(t){}
  48. operator type () const {return m_;}
  49. type m_;
  50. private :
  51. //prevent automatic conversion for any other built-in types
  52. template <typename T>
  53. operator T () const;
  54. };
  55. template
  56. <
  57. typename Segment, typename Box,
  58. typename AzimuthStrategy,
  59. typename NormalizeStrategy,
  60. typename DisjointPointBoxStrategy,
  61. typename DisjointBoxBoxStrategy
  62. >
  63. static inline bool apply(Segment const& segment,
  64. Box const& box,
  65. AzimuthStrategy const& azimuth_strategy,
  66. NormalizeStrategy const& normalize_strategy,
  67. DisjointPointBoxStrategy const& disjoint_point_box_strategy,
  68. DisjointBoxBoxStrategy const& disjoint_box_box_strategy)
  69. {
  70. typedef typename point_type<Segment>::type segment_point;
  71. segment_point vertex;
  72. return apply(segment, box, vertex,
  73. azimuth_strategy,
  74. normalize_strategy,
  75. disjoint_point_box_strategy,
  76. disjoint_box_box_strategy) != disjoint_info::intersect;
  77. }
  78. template
  79. <
  80. typename Segment, typename Box,
  81. typename P,
  82. typename AzimuthStrategy,
  83. typename NormalizeStrategy,
  84. typename DisjointPointBoxStrategy,
  85. typename DisjointBoxBoxStrategy
  86. >
  87. static inline disjoint_info apply(Segment const& segment,
  88. Box const& box,
  89. P& vertex,
  90. AzimuthStrategy const& azimuth_strategy,
  91. NormalizeStrategy const& ,
  92. DisjointPointBoxStrategy const& disjoint_point_box_strategy,
  93. DisjointBoxBoxStrategy const& disjoint_box_box_strategy)
  94. {
  95. assert_dimension_equal<Segment, Box>();
  96. typedef typename point_type<Segment>::type segment_point_type;
  97. segment_point_type p0, p1;
  98. geometry::detail::assign_point_from_index<0>(segment, p0);
  99. geometry::detail::assign_point_from_index<1>(segment, p1);
  100. //vertex not computed here
  101. disjoint_info disjoint_return_value = disjoint_info::disjoint_no_vertex;
  102. // Simplest cases first
  103. // Case 1: if box contains one of segment's endpoints then they are not disjoint
  104. if ( ! disjoint_point_box(p0, box, disjoint_point_box_strategy)
  105. || ! disjoint_point_box(p1, box, disjoint_point_box_strategy) )
  106. {
  107. return disjoint_info::intersect;
  108. }
  109. // Case 2: disjoint if bounding boxes are disjoint
  110. typedef typename coordinate_type<segment_point_type>::type CT;
  111. segment_point_type p0_normalized;
  112. NormalizeStrategy::apply(p0, p0_normalized);
  113. segment_point_type p1_normalized;
  114. NormalizeStrategy::apply(p1, p1_normalized);
  115. CT lon1 = geometry::get_as_radian<0>(p0_normalized);
  116. CT lat1 = geometry::get_as_radian<1>(p0_normalized);
  117. CT lon2 = geometry::get_as_radian<0>(p1_normalized);
  118. CT lat2 = geometry::get_as_radian<1>(p1_normalized);
  119. if (lon1 > lon2)
  120. {
  121. std::swap(lon1, lon2);
  122. std::swap(lat1, lat2);
  123. }
  124. geometry::model::box<segment_point_type> box_seg;
  125. strategy::envelope::detail::envelope_segment_impl
  126. <
  127. CS_Tag
  128. >::template apply<geometry::radian>(lon1, lat1,
  129. lon2, lat2,
  130. box_seg,
  131. azimuth_strategy);
  132. if (disjoint_box_box(box, box_seg, disjoint_box_box_strategy))
  133. {
  134. return disjoint_return_value;
  135. }
  136. // Case 3: test intersection by comparing angles
  137. CT alp1, a_b0, a_b1, a_b2, a_b3;
  138. CT b_lon_min = geometry::get_as_radian<geometry::min_corner, 0>(box);
  139. CT b_lat_min = geometry::get_as_radian<geometry::min_corner, 1>(box);
  140. CT b_lon_max = geometry::get_as_radian<geometry::max_corner, 0>(box);
  141. CT b_lat_max = geometry::get_as_radian<geometry::max_corner, 1>(box);
  142. azimuth_strategy.apply(lon1, lat1, lon2, lat2, alp1);
  143. azimuth_strategy.apply(lon1, lat1, b_lon_min, b_lat_min, a_b0);
  144. azimuth_strategy.apply(lon1, lat1, b_lon_max, b_lat_min, a_b1);
  145. azimuth_strategy.apply(lon1, lat1, b_lon_min, b_lat_max, a_b2);
  146. azimuth_strategy.apply(lon1, lat1, b_lon_max, b_lat_max, a_b3);
  147. int s0 = formula::azimuth_side_value(alp1, a_b0);
  148. int s1 = formula::azimuth_side_value(alp1, a_b1);
  149. int s2 = formula::azimuth_side_value(alp1, a_b2);
  150. int s3 = formula::azimuth_side_value(alp1, a_b3);
  151. if (s0 == 0 || s1 == 0 || s2 == 0 || s3 == 0)
  152. {
  153. return disjoint_info::intersect;
  154. }
  155. bool s0_positive = s0 > 0;
  156. bool s1_positive = s1 > 0;
  157. bool s2_positive = s2 > 0;
  158. bool s3_positive = s3 > 0;
  159. bool all_positive = s0_positive && s1_positive && s2_positive && s3_positive;
  160. bool all_non_positive = !(s0_positive || s1_positive || s2_positive || s3_positive);
  161. bool vertex_north = lat1 + lat2 > 0;
  162. if ((all_positive && vertex_north) || (all_non_positive && !vertex_north))
  163. {
  164. return disjoint_info::disjoint_no_vertex;
  165. }
  166. if (!all_positive && !all_non_positive)
  167. {
  168. return disjoint_info::intersect;
  169. }
  170. // Case 4: The only intersection case not covered above is when all four
  171. // points of the box are above (below) the segment in northern (southern)
  172. // hemisphere. Then we have to compute the vertex of the segment
  173. CT vertex_lat;
  174. if ((lat1 < b_lat_min && vertex_north)
  175. || (lat1 > b_lat_max && !vertex_north))
  176. {
  177. CT b_lat_below; //latitude of box closest to equator
  178. if (vertex_north)
  179. {
  180. vertex_lat = geometry::get_as_radian<geometry::max_corner, 1>(box_seg);
  181. b_lat_below = b_lat_min;
  182. } else {
  183. vertex_lat = geometry::get_as_radian<geometry::min_corner, 1>(box_seg);
  184. b_lat_below = b_lat_max;
  185. }
  186. //optimization TODO: computing the spherical longitude should suffice for
  187. // the majority of cases
  188. CT vertex_lon = geometry::formula::vertex_longitude<CT, CS_Tag>
  189. ::apply(lon1, lat1,
  190. lon2, lat2,
  191. vertex_lat,
  192. alp1,
  193. azimuth_strategy);
  194. geometry::set_from_radian<0>(vertex, vertex_lon);
  195. geometry::set_from_radian<1>(vertex, vertex_lat);
  196. disjoint_return_value = disjoint_info::disjoint_vertex; //vertex_computed
  197. // Check if the vertex point is within the band defined by the
  198. // minimum and maximum longitude of the box; if yes, then return
  199. // false if the point is above the min latitude of the box; return
  200. // true in all other cases
  201. if (vertex_lon >= b_lon_min && vertex_lon <= b_lon_max
  202. && std::abs(vertex_lat) > std::abs(b_lat_below))
  203. {
  204. return disjoint_info::intersect;
  205. }
  206. }
  207. return disjoint_return_value;
  208. }
  209. };
  210. struct disjoint_segment_box
  211. {
  212. template <typename Segment, typename Box, typename Strategy>
  213. static inline bool apply(Segment const& segment,
  214. Box const& box,
  215. Strategy const& strategy)
  216. {
  217. return strategy.disjoint(segment, box).apply(segment, box);
  218. }
  219. };
  220. }} // namespace detail::disjoint
  221. #endif // DOXYGEN_NO_DETAIL
  222. #ifndef DOXYGEN_NO_DISPATCH
  223. namespace dispatch
  224. {
  225. template <typename Segment, typename Box, std::size_t DimensionCount>
  226. struct disjoint<Segment, Box, DimensionCount, segment_tag, box_tag, false>
  227. : detail::disjoint::disjoint_segment_box
  228. {};
  229. } // namespace dispatch
  230. #endif // DOXYGEN_NO_DISPATCH
  231. }} // namespace boost::geometry
  232. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISJOINT_SEGMENT_BOX_HPP