range_of_boxes.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2015-2020, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  4. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_ENVELOPE_RANGE_OF_BOXES_HPP
  10. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_ENVELOPE_RANGE_OF_BOXES_HPP
  11. #include <algorithm>
  12. #include <cstddef>
  13. #include <type_traits>
  14. #include <vector>
  15. #include <boost/range/begin.hpp>
  16. #include <boost/range/empty.hpp>
  17. #include <boost/range/end.hpp>
  18. #include <boost/range/value_type.hpp>
  19. #include <boost/geometry/algorithms/detail/convert_point_to_point.hpp>
  20. #include <boost/geometry/algorithms/detail/max_interval_gap.hpp>
  21. #include <boost/geometry/algorithms/detail/expand/indexed.hpp>
  22. #include <boost/geometry/core/access.hpp>
  23. #include <boost/geometry/core/assert.hpp>
  24. #include <boost/geometry/core/coordinate_system.hpp>
  25. #include <boost/geometry/core/coordinate_type.hpp>
  26. #include <boost/geometry/util/is_inverse_spheroidal_coordinates.hpp>
  27. #include <boost/geometry/util/math.hpp>
  28. #include <boost/geometry/util/range.hpp>
  29. #include <boost/geometry/views/detail/indexed_point_view.hpp>
  30. namespace boost { namespace geometry
  31. {
  32. #ifndef DOXYGEN_NO_DETAIL
  33. namespace detail { namespace envelope
  34. {
  35. template <typename T>
  36. class longitude_interval
  37. {
  38. typedef T const& reference_type;
  39. public:
  40. typedef T value_type;
  41. typedef T difference_type;
  42. longitude_interval(T const& left, T const& right)
  43. {
  44. m_end[0] = left;
  45. m_end[1] = right;
  46. }
  47. template <std::size_t Index>
  48. reference_type get() const
  49. {
  50. return m_end[Index];
  51. }
  52. difference_type length() const
  53. {
  54. return get<1>() - get<0>();
  55. }
  56. private:
  57. T m_end[2];
  58. };
  59. template <typename Units>
  60. struct envelope_range_of_longitudes
  61. {
  62. template <std::size_t Index>
  63. struct longitude_less
  64. {
  65. template <typename Interval>
  66. inline bool operator()(Interval const& i1, Interval const& i2) const
  67. {
  68. return math::smaller(i1.template get<Index>(),
  69. i2.template get<Index>());
  70. }
  71. };
  72. template <typename RangeOfLongitudeIntervals, typename Longitude>
  73. static inline void apply(RangeOfLongitudeIntervals const& range,
  74. Longitude& lon_min, Longitude& lon_max)
  75. {
  76. typedef typename math::detail::constants_on_spheroid
  77. <
  78. Longitude, Units
  79. > constants;
  80. Longitude const zero = 0;
  81. Longitude const period = constants::period();
  82. lon_min = lon_max = zero;
  83. // the range of longitude intervals can be empty if all input boxes
  84. // degenerate to the north or south pole (or combination of the two)
  85. // in this case the initialization values for lon_min and
  86. // lon_max are valid choices
  87. if (! boost::empty(range))
  88. {
  89. lon_min = std::min_element(boost::begin(range),
  90. boost::end(range),
  91. longitude_less<0>())->template get<0>();
  92. lon_max = std::max_element(boost::begin(range),
  93. boost::end(range),
  94. longitude_less<1>())->template get<1>();
  95. if (math::larger(lon_max - lon_min, constants::half_period()))
  96. {
  97. Longitude max_gap_left, max_gap_right;
  98. Longitude max_gap = geometry::maximum_gap(range,
  99. max_gap_left,
  100. max_gap_right);
  101. BOOST_GEOMETRY_ASSERT(! math::larger(lon_min, lon_max));
  102. BOOST_GEOMETRY_ASSERT
  103. (! math::larger(lon_max, constants::max_longitude()));
  104. BOOST_GEOMETRY_ASSERT
  105. (! math::smaller(lon_min, constants::min_longitude()));
  106. BOOST_GEOMETRY_ASSERT
  107. (! math::larger(max_gap_left, max_gap_right));
  108. BOOST_GEOMETRY_ASSERT
  109. (! math::larger(max_gap_right, constants::max_longitude()));
  110. BOOST_GEOMETRY_ASSERT
  111. (! math::smaller(max_gap_left, constants::min_longitude()));
  112. if (math::larger(max_gap, zero))
  113. {
  114. Longitude wrapped_gap = period + lon_min - lon_max;
  115. if (math::larger(max_gap, wrapped_gap))
  116. {
  117. lon_min = max_gap_right;
  118. lon_max = max_gap_left + period;
  119. }
  120. }
  121. }
  122. }
  123. }
  124. };
  125. template <std::size_t Dimension, std::size_t DimensionCount>
  126. struct envelope_range_of_boxes_by_expansion
  127. {
  128. template <typename RangeOfBoxes, typename Box>
  129. static inline void apply(RangeOfBoxes const& range_of_boxes, Box& mbr)
  130. {
  131. typedef typename boost::range_value<RangeOfBoxes>::type box_type;
  132. // first initialize MBR
  133. detail::indexed_point_view<Box, min_corner> mbr_min(mbr);
  134. detail::indexed_point_view<Box, max_corner> mbr_max(mbr);
  135. detail::indexed_point_view<box_type const, min_corner>
  136. first_box_min(range::front(range_of_boxes));
  137. detail::indexed_point_view<box_type const, max_corner>
  138. first_box_max(range::front(range_of_boxes));
  139. detail::conversion::point_to_point
  140. <
  141. detail::indexed_point_view<box_type const, min_corner>,
  142. detail::indexed_point_view<Box, min_corner>,
  143. Dimension,
  144. DimensionCount
  145. >::apply(first_box_min, mbr_min);
  146. detail::conversion::point_to_point
  147. <
  148. detail::indexed_point_view<box_type const, max_corner>,
  149. detail::indexed_point_view<Box, max_corner>,
  150. Dimension,
  151. DimensionCount
  152. >::apply(first_box_max, mbr_max);
  153. // now expand using the remaining boxes
  154. auto it = boost::begin(range_of_boxes);
  155. for (++it; it != boost::end(range_of_boxes); ++it)
  156. {
  157. detail::expand::indexed_loop
  158. <
  159. min_corner,
  160. Dimension,
  161. DimensionCount
  162. >::apply(mbr, *it);
  163. detail::expand::indexed_loop
  164. <
  165. max_corner,
  166. Dimension,
  167. DimensionCount
  168. >::apply(mbr, *it);
  169. }
  170. }
  171. };
  172. struct envelope_range_of_boxes
  173. {
  174. template <std::size_t Index>
  175. struct latitude_less
  176. {
  177. template <typename Box>
  178. inline bool operator()(Box const& box1, Box const& box2) const
  179. {
  180. return math::smaller(geometry::get<Index, 1>(box1),
  181. geometry::get<Index, 1>(box2));
  182. }
  183. };
  184. template <typename RangeOfBoxes, typename Box>
  185. static inline void apply(RangeOfBoxes const& range_of_boxes, Box& mbr)
  186. {
  187. // boxes in the range are assumed to be normalized already
  188. typedef typename boost::range_value<RangeOfBoxes>::type box_type;
  189. typedef typename coordinate_type<box_type>::type coordinate_type;
  190. typedef typename detail::cs_angular_units<box_type>::type units_type;
  191. static const bool is_equatorial = ! std::is_same
  192. <
  193. typename cs_tag<box_type>::type,
  194. spherical_polar_tag
  195. >::value;
  196. typedef math::detail::constants_on_spheroid
  197. <
  198. coordinate_type, units_type, is_equatorial
  199. > constants;
  200. typedef longitude_interval<coordinate_type> interval_type;
  201. typedef std::vector<interval_type> interval_range_type;
  202. BOOST_GEOMETRY_ASSERT(! boost::empty(range_of_boxes));
  203. auto const it_min = std::min_element(boost::begin(range_of_boxes),
  204. boost::end(range_of_boxes),
  205. latitude_less<min_corner>());
  206. auto const it_max = std::max_element(boost::begin(range_of_boxes),
  207. boost::end(range_of_boxes),
  208. latitude_less<max_corner>());
  209. coordinate_type const min_longitude = constants::min_longitude();
  210. coordinate_type const max_longitude = constants::max_longitude();
  211. coordinate_type const period = constants::period();
  212. interval_range_type intervals;
  213. for (auto it = boost::begin(range_of_boxes);
  214. it != boost::end(range_of_boxes);
  215. ++it)
  216. {
  217. auto const& box = *it;
  218. if (is_inverse_spheroidal_coordinates(box))
  219. {
  220. continue;
  221. }
  222. coordinate_type lat_min = geometry::get<min_corner, 1>(box);
  223. coordinate_type lat_max = geometry::get<max_corner, 1>(box);
  224. if (math::equals(lat_min, constants::max_latitude())
  225. || math::equals(lat_max, constants::min_latitude()))
  226. {
  227. // if the box degenerates to the south or north pole
  228. // just ignore it
  229. continue;
  230. }
  231. coordinate_type lon_left = geometry::get<min_corner, 0>(box);
  232. coordinate_type lon_right = geometry::get<max_corner, 0>(box);
  233. if (math::larger(lon_right, max_longitude))
  234. {
  235. intervals.push_back(interval_type(lon_left, max_longitude));
  236. intervals.push_back
  237. (interval_type(min_longitude, lon_right - period));
  238. }
  239. else
  240. {
  241. intervals.push_back(interval_type(lon_left, lon_right));
  242. }
  243. }
  244. coordinate_type lon_min = 0;
  245. coordinate_type lon_max = 0;
  246. envelope_range_of_longitudes
  247. <
  248. units_type
  249. >::apply(intervals, lon_min, lon_max);
  250. // do not convert units; conversion will be performed at a
  251. // higher level
  252. // assign now the min/max longitude/latitude values
  253. detail::indexed_point_view<Box, min_corner> mbr_min(mbr);
  254. detail::indexed_point_view<Box, max_corner> mbr_max(mbr);
  255. geometry::set<0>(mbr_min, lon_min);
  256. geometry::set<1>(mbr_min, geometry::get<min_corner, 1>(*it_min));
  257. geometry::set<0>(mbr_max, lon_max);
  258. geometry::set<1>(mbr_max, geometry::get<max_corner, 1>(*it_max));
  259. // what remains to be done is to compute the envelope range
  260. // for the remaining dimensions (if any)
  261. envelope_range_of_boxes_by_expansion
  262. <
  263. 2, dimension<Box>::value
  264. >::apply(range_of_boxes, mbr);
  265. }
  266. };
  267. }} // namespace detail::envelope
  268. #endif // DOXYGEN_NO_DETAIL
  269. }} // namespace boost::geometry
  270. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_ENVELOPE_RANGE_OF_BOXES_HPP