range_of_boxes.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2015-2018, 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 <cstddef>
  12. #include <algorithm>
  13. #include <vector>
  14. #include <boost/range.hpp>
  15. #include <boost/geometry/algorithms/detail/convert_point_to_point.hpp>
  16. #include <boost/geometry/algorithms/detail/max_interval_gap.hpp>
  17. #include <boost/geometry/algorithms/detail/expand/indexed.hpp>
  18. #include <boost/geometry/core/access.hpp>
  19. #include <boost/geometry/core/assert.hpp>
  20. #include <boost/geometry/core/coordinate_system.hpp>
  21. #include <boost/geometry/core/coordinate_type.hpp>
  22. #include <boost/geometry/util/math.hpp>
  23. #include <boost/geometry/util/normalize_spheroidal_coordinates.hpp>
  24. #include <boost/geometry/util/range.hpp>
  25. #include <boost/geometry/views/detail/indexed_point_view.hpp>
  26. namespace boost { namespace geometry
  27. {
  28. #ifndef DOXYGEN_NO_DETAIL
  29. namespace detail { namespace envelope
  30. {
  31. template <typename T>
  32. class longitude_interval
  33. {
  34. typedef T const& reference_type;
  35. public:
  36. typedef T value_type;
  37. typedef T difference_type;
  38. longitude_interval(T const& left, T const& right)
  39. {
  40. m_end[0] = left;
  41. m_end[1] = right;
  42. }
  43. template <std::size_t Index>
  44. reference_type get() const
  45. {
  46. return m_end[Index];
  47. }
  48. difference_type length() const
  49. {
  50. return get<1>() - get<0>();
  51. }
  52. private:
  53. T m_end[2];
  54. };
  55. template <typename Units>
  56. struct envelope_range_of_longitudes
  57. {
  58. template <std::size_t Index>
  59. struct longitude_less
  60. {
  61. template <typename Interval>
  62. inline bool operator()(Interval const& i1, Interval const& i2) const
  63. {
  64. return math::smaller(i1.template get<Index>(),
  65. i2.template get<Index>());
  66. }
  67. };
  68. template <typename RangeOfLongitudeIntervals, typename Longitude>
  69. static inline void apply(RangeOfLongitudeIntervals const& range,
  70. Longitude& lon_min, Longitude& lon_max)
  71. {
  72. typedef typename math::detail::constants_on_spheroid
  73. <
  74. Longitude, Units
  75. > constants;
  76. Longitude const zero = 0;
  77. Longitude const period = constants::period();
  78. lon_min = lon_max = zero;
  79. // the range of longitude intervals can be empty if all input boxes
  80. // degenerate to the north or south pole (or combination of the two)
  81. // in this case the initialization values for lon_min and
  82. // lon_max are valid choices
  83. if (! boost::empty(range))
  84. {
  85. lon_min = std::min_element(boost::begin(range),
  86. boost::end(range),
  87. longitude_less<0>())->template get<0>();
  88. lon_max = std::max_element(boost::begin(range),
  89. boost::end(range),
  90. longitude_less<1>())->template get<1>();
  91. if (math::larger(lon_max - lon_min, constants::half_period()))
  92. {
  93. Longitude max_gap_left, max_gap_right;
  94. Longitude max_gap = geometry::maximum_gap(range,
  95. max_gap_left,
  96. max_gap_right);
  97. BOOST_GEOMETRY_ASSERT(! math::larger(lon_min, lon_max));
  98. BOOST_GEOMETRY_ASSERT
  99. (! math::larger(lon_max, constants::max_longitude()));
  100. BOOST_GEOMETRY_ASSERT
  101. (! math::smaller(lon_min, constants::min_longitude()));
  102. BOOST_GEOMETRY_ASSERT
  103. (! math::larger(max_gap_left, max_gap_right));
  104. BOOST_GEOMETRY_ASSERT
  105. (! math::larger(max_gap_right, constants::max_longitude()));
  106. BOOST_GEOMETRY_ASSERT
  107. (! math::smaller(max_gap_left, constants::min_longitude()));
  108. if (math::larger(max_gap, zero))
  109. {
  110. Longitude wrapped_gap = period + lon_min - lon_max;
  111. if (math::larger(max_gap, wrapped_gap))
  112. {
  113. lon_min = max_gap_right;
  114. lon_max = max_gap_left + period;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. };
  121. template <std::size_t Dimension, std::size_t DimensionCount>
  122. struct envelope_range_of_boxes_by_expansion
  123. {
  124. template <typename RangeOfBoxes, typename Box>
  125. static inline void apply(RangeOfBoxes const& range_of_boxes, Box& mbr)
  126. {
  127. typedef typename boost::range_value<RangeOfBoxes>::type box_type;
  128. typedef typename boost::range_iterator
  129. <
  130. RangeOfBoxes const
  131. >::type iterator_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. iterator_type 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. typedef typename boost::range_iterator
  192. <
  193. RangeOfBoxes const
  194. >::type iterator_type;
  195. static const bool is_equatorial = ! boost::is_same
  196. <
  197. typename cs_tag<box_type>::type,
  198. spherical_polar_tag
  199. >::value;
  200. typedef math::detail::constants_on_spheroid
  201. <
  202. coordinate_type, units_type, is_equatorial
  203. > constants;
  204. typedef longitude_interval<coordinate_type> interval_type;
  205. typedef std::vector<interval_type> interval_range_type;
  206. BOOST_GEOMETRY_ASSERT(! boost::empty(range_of_boxes));
  207. iterator_type it_min = std::min_element(boost::begin(range_of_boxes),
  208. boost::end(range_of_boxes),
  209. latitude_less<min_corner>());
  210. iterator_type it_max = std::max_element(boost::begin(range_of_boxes),
  211. boost::end(range_of_boxes),
  212. latitude_less<max_corner>());
  213. coordinate_type const min_longitude = constants::min_longitude();
  214. coordinate_type const max_longitude = constants::max_longitude();
  215. coordinate_type const period = constants::period();
  216. interval_range_type intervals;
  217. for (iterator_type it = boost::begin(range_of_boxes);
  218. it != boost::end(range_of_boxes);
  219. ++it)
  220. {
  221. if (is_inverse_spheroidal_coordinates(*it))
  222. {
  223. continue;
  224. }
  225. coordinate_type lat_min = geometry::get<min_corner, 1>(*it);
  226. coordinate_type lat_max = geometry::get<max_corner, 1>(*it);
  227. if (math::equals(lat_min, constants::max_latitude())
  228. || math::equals(lat_max, constants::min_latitude()))
  229. {
  230. // if the box degenerates to the south or north pole
  231. // just ignore it
  232. continue;
  233. }
  234. coordinate_type lon_left = geometry::get<min_corner, 0>(*it);
  235. coordinate_type lon_right = geometry::get<max_corner, 0>(*it);
  236. if (math::larger(lon_right, max_longitude))
  237. {
  238. intervals.push_back(interval_type(lon_left, max_longitude));
  239. intervals.push_back
  240. (interval_type(min_longitude, lon_right - period));
  241. }
  242. else
  243. {
  244. intervals.push_back(interval_type(lon_left, lon_right));
  245. }
  246. }
  247. coordinate_type lon_min = 0;
  248. coordinate_type lon_max = 0;
  249. envelope_range_of_longitudes
  250. <
  251. units_type
  252. >::apply(intervals, lon_min, lon_max);
  253. // do not convert units; conversion will be performed at a
  254. // higher level
  255. // assign now the min/max longitude/latitude values
  256. detail::indexed_point_view<Box, min_corner> mbr_min(mbr);
  257. detail::indexed_point_view<Box, max_corner> mbr_max(mbr);
  258. geometry::set<0>(mbr_min, lon_min);
  259. geometry::set<1>(mbr_min, geometry::get<min_corner, 1>(*it_min));
  260. geometry::set<0>(mbr_max, lon_max);
  261. geometry::set<1>(mbr_max, geometry::get<max_corner, 1>(*it_max));
  262. // what remains to be done is to compute the envelope range
  263. // for the remaining dimensions (if any)
  264. envelope_range_of_boxes_by_expansion
  265. <
  266. 2, dimension<Box>::value
  267. >::apply(range_of_boxes, mbr);
  268. }
  269. };
  270. }} // namespace detail::envelope
  271. #endif // DOXYGEN_NO_DETAIL
  272. }} // namespace boost::geometry
  273. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_ENVELOPE_RANGE_OF_BOXES_HPP