distance_segment_box.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2018 Oracle and/or its affiliates.
  3. // Contributed and/or modified by Vissarion Fisikopoulos, on behalf of Oracle
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DISTANCE_SEGMENT_BOX_HPP
  8. #define BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DISTANCE_SEGMENT_BOX_HPP
  9. #include <boost/geometry/algorithms/detail/distance/segment_to_box.hpp>
  10. namespace boost { namespace geometry
  11. {
  12. namespace strategy { namespace distance
  13. {
  14. struct generic_segment_box
  15. {
  16. template
  17. <
  18. typename LessEqual,
  19. typename ReturnType,
  20. typename SegmentPoint,
  21. typename BoxPoint,
  22. typename SegmentBoxStrategy,
  23. typename AzimuthStrategy,
  24. typename EnvelopeSegmentStrategy
  25. >
  26. static inline ReturnType segment_below_of_box(
  27. SegmentPoint const& p0,
  28. SegmentPoint const& p1,
  29. BoxPoint const&,
  30. BoxPoint const& top_right,
  31. BoxPoint const& bottom_left,
  32. BoxPoint const& bottom_right,
  33. SegmentBoxStrategy const& sb_strategy,
  34. AzimuthStrategy & az_strategy,
  35. EnvelopeSegmentStrategy & es_strategy)
  36. {
  37. ReturnType result;
  38. typename LessEqual::other less_equal;
  39. typedef geometry::model::segment<SegmentPoint> Segment;
  40. typedef typename cs_tag<Segment>::type segment_cs_type;
  41. typedef geometry::detail::disjoint::
  42. disjoint_segment_box_sphere_or_spheroid<segment_cs_type>
  43. disjoint_sb;
  44. typedef typename disjoint_sb::disjoint_info disjoint_info_type;
  45. Segment seg(p0, p1);
  46. geometry::model::box<BoxPoint> input_box;
  47. geometry::set_from_radian<geometry::min_corner, 0>
  48. (input_box, geometry::get_as_radian<0>(bottom_left));
  49. geometry::set_from_radian<geometry::min_corner, 1>
  50. (input_box, geometry::get_as_radian<1>(bottom_left));
  51. geometry::set_from_radian<geometry::max_corner, 0>
  52. (input_box, geometry::get_as_radian<0>(top_right));
  53. geometry::set_from_radian<geometry::max_corner, 1>
  54. (input_box, geometry::get_as_radian<1>(top_right));
  55. SegmentPoint p_max;
  56. disjoint_info_type disjoint_result = disjoint_sb::
  57. apply(seg, input_box, az_strategy, p_max);
  58. if (disjoint_result == disjoint_info_type::intersect) //intersect
  59. {
  60. return 0;
  61. }
  62. // disjoint but vertex not computed
  63. if (disjoint_result == disjoint_info_type::disjoint_no_vertex)
  64. {
  65. typedef typename coordinate_type<SegmentPoint>::type CT;
  66. geometry::model::box<SegmentPoint> mbr;
  67. geometry::envelope(seg, mbr, es_strategy);
  68. CT lon1 = geometry::get_as_radian<0>(p0);
  69. CT lat1 = geometry::get_as_radian<1>(p0);
  70. CT lon2 = geometry::get_as_radian<0>(p1);
  71. CT lat2 = geometry::get_as_radian<1>(p1);
  72. CT vertex_lat;
  73. CT lat_sum = lat1 + lat2;
  74. if (lat_sum > CT(0))
  75. {
  76. vertex_lat = geometry::get_as_radian<geometry::max_corner, 1>(mbr);
  77. } else {
  78. vertex_lat = geometry::get_as_radian<geometry::min_corner, 1>(mbr);
  79. }
  80. CT alp1;
  81. az_strategy.apply(lon1, lat1, lon2, lat2, alp1);
  82. CT vertex_lon = geometry::formula::vertex_longitude
  83. <
  84. CT,
  85. segment_cs_type
  86. >::apply(lon1, lat1, lon2, lat2,
  87. vertex_lat, alp1, az_strategy);
  88. geometry::set_from_radian<0>(p_max, vertex_lon);
  89. geometry::set_from_radian<1>(p_max, vertex_lat);
  90. }
  91. //otherwise disjoint and vertex computed inside disjoint
  92. if (less_equal(geometry::get_as_radian<0>(bottom_left),
  93. geometry::get_as_radian<0>(p_max)))
  94. {
  95. result = boost::numeric_cast<ReturnType>(typename
  96. SegmentBoxStrategy::distance_ps_strategy::type().apply(bottom_left, p0, p1));
  97. }
  98. else
  99. {
  100. result = geometry::detail::distance::segment_to_box_2D
  101. <
  102. ReturnType,
  103. SegmentPoint,
  104. BoxPoint,
  105. SegmentBoxStrategy
  106. >::template call_above_of_box
  107. <
  108. typename LessEqual::other
  109. >(p1, p0, p_max, bottom_right, sb_strategy);
  110. }
  111. return result;
  112. }
  113. template <typename SPoint, typename BPoint>
  114. static void mirror(SPoint& p0,
  115. SPoint& p1,
  116. BPoint& bottom_left,
  117. BPoint& bottom_right,
  118. BPoint& top_left,
  119. BPoint& top_right)
  120. {
  121. //if segment's vertex is the southest point then mirror geometries
  122. if (geometry::get<1>(p0) + geometry::get<1>(p1) < 0)
  123. {
  124. BPoint bl = bottom_left;
  125. BPoint br = bottom_right;
  126. geometry::set<1>(p0, geometry::get<1>(p0) * -1);
  127. geometry::set<1>(p1, geometry::get<1>(p1) * -1);
  128. geometry::set<1>(bottom_left, geometry::get<1>(top_left) * -1);
  129. geometry::set<1>(top_left, geometry::get<1>(bl) * -1);
  130. geometry::set<1>(bottom_right, geometry::get<1>(top_right) * -1);
  131. geometry::set<1>(top_right, geometry::get<1>(br) * -1);
  132. }
  133. }
  134. };
  135. //===========================================================================
  136. template
  137. <
  138. typename CalculationType = void,
  139. typename Strategy = haversine<double, CalculationType>
  140. >
  141. struct spherical_segment_box
  142. {
  143. template <typename PointOfSegment, typename PointOfBox>
  144. struct calculation_type
  145. : promote_floating_point
  146. <
  147. typename strategy::distance::services::return_type
  148. <
  149. Strategy,
  150. PointOfSegment,
  151. PointOfBox
  152. >::type
  153. >
  154. {};
  155. // strategy getters
  156. // point-point strategy getters
  157. struct distance_pp_strategy
  158. {
  159. typedef Strategy type;
  160. };
  161. inline typename distance_pp_strategy::type get_distance_pp_strategy() const
  162. {
  163. return typename distance_pp_strategy::type();
  164. }
  165. // point-segment strategy getters
  166. struct distance_ps_strategy
  167. {
  168. typedef cross_track<CalculationType, Strategy> type;
  169. };
  170. inline typename distance_ps_strategy::type get_distance_ps_strategy() const
  171. {
  172. return typename distance_ps_strategy::type();
  173. }
  174. // methods
  175. template <typename LessEqual, typename ReturnType,
  176. typename SegmentPoint, typename BoxPoint>
  177. inline ReturnType segment_below_of_box(SegmentPoint const& p0,
  178. SegmentPoint const& p1,
  179. BoxPoint const& top_left,
  180. BoxPoint const& top_right,
  181. BoxPoint const& bottom_left,
  182. BoxPoint const& bottom_right) const
  183. {
  184. typedef typename azimuth::spherical<CalculationType> azimuth_strategy_type;
  185. azimuth_strategy_type az_strategy;
  186. typedef typename envelope::spherical_segment<CalculationType>
  187. envelope_segment_strategy_type;
  188. envelope_segment_strategy_type es_strategy;
  189. return generic_segment_box::segment_below_of_box
  190. <
  191. LessEqual,
  192. ReturnType
  193. >(p0,p1,top_left,top_right,bottom_left,bottom_right,
  194. spherical_segment_box<CalculationType>(),
  195. az_strategy, es_strategy);
  196. }
  197. template <typename SPoint, typename BPoint>
  198. static void mirror(SPoint& p0,
  199. SPoint& p1,
  200. BPoint& bottom_left,
  201. BPoint& bottom_right,
  202. BPoint& top_left,
  203. BPoint& top_right)
  204. {
  205. generic_segment_box::mirror(p0, p1,
  206. bottom_left, bottom_right,
  207. top_left, top_right);
  208. }
  209. };
  210. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  211. namespace services
  212. {
  213. template <typename CalculationType, typename Strategy>
  214. struct tag<spherical_segment_box<CalculationType, Strategy> >
  215. {
  216. typedef strategy_tag_distance_segment_box type;
  217. };
  218. template <typename CalculationType, typename Strategy, typename PS, typename PB>
  219. struct return_type<spherical_segment_box<CalculationType, Strategy>, PS, PB>
  220. : spherical_segment_box<CalculationType, Strategy>::template calculation_type<PS, PB>
  221. {};
  222. template <typename CalculationType, typename Strategy>
  223. struct comparable_type<spherical_segment_box<CalculationType, Strategy> >
  224. {
  225. // Define a cartesian_segment_box strategy with its underlying point-segment
  226. // strategy being comparable
  227. typedef spherical_segment_box
  228. <
  229. CalculationType,
  230. typename comparable_type<Strategy>::type
  231. > type;
  232. };
  233. template <typename CalculationType, typename Strategy>
  234. struct get_comparable<spherical_segment_box<CalculationType, Strategy> >
  235. {
  236. typedef typename comparable_type
  237. <
  238. spherical_segment_box<CalculationType, Strategy>
  239. >::type comparable_type;
  240. public :
  241. static inline comparable_type apply(spherical_segment_box<CalculationType, Strategy> const& )
  242. {
  243. return comparable_type();
  244. }
  245. };
  246. template <typename CalculationType, typename Strategy, typename PS, typename PB>
  247. struct result_from_distance<spherical_segment_box<CalculationType, Strategy>, PS, PB>
  248. {
  249. private :
  250. typedef typename return_type<
  251. spherical_segment_box
  252. <
  253. CalculationType,
  254. Strategy
  255. >,
  256. PS,
  257. PB
  258. >::type return_type;
  259. public :
  260. template <typename T>
  261. static inline return_type apply(spherical_segment_box<CalculationType,
  262. Strategy> const& ,
  263. T const& value)
  264. {
  265. Strategy s;
  266. return result_from_distance<Strategy, PS, PB>::apply(s, value);
  267. }
  268. };
  269. template <typename Segment, typename Box>
  270. struct default_strategy
  271. <
  272. segment_tag, box_tag, Segment, Box,
  273. spherical_equatorial_tag, spherical_equatorial_tag
  274. >
  275. {
  276. typedef spherical_segment_box<> type;
  277. };
  278. template <typename Box, typename Segment>
  279. struct default_strategy
  280. <
  281. box_tag, segment_tag, Box, Segment,
  282. spherical_equatorial_tag, spherical_equatorial_tag
  283. >
  284. {
  285. typedef typename default_strategy
  286. <
  287. segment_tag, box_tag, Segment, Box,
  288. spherical_equatorial_tag, spherical_equatorial_tag
  289. >::type type;
  290. };
  291. }
  292. #endif
  293. }} // namespace strategy::distance
  294. }} // namespace boost::geometry
  295. #endif // BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DISTANCE_SEGMENT_BOX_HPP