point.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2015 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2015 Mateusz Loskot, London, UK.
  5. // Copyright (c) 2014-2015 Samuel Debionne, Grenoble, France.
  6. // This file was modified by Oracle on 2015-2018.
  7. // Modifications copyright (c) 2015-2018, 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. // Distributed under the Boost Software License, Version 1.0.
  14. // (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_EXPAND_POINT_HPP
  17. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_EXPAND_POINT_HPP
  18. #include <cstddef>
  19. #include <algorithm>
  20. #include <functional>
  21. #include <boost/mpl/assert.hpp>
  22. #include <boost/type_traits/is_same.hpp>
  23. #include <boost/geometry/core/access.hpp>
  24. #include <boost/geometry/core/coordinate_dimension.hpp>
  25. #include <boost/geometry/core/coordinate_system.hpp>
  26. #include <boost/geometry/core/coordinate_type.hpp>
  27. #include <boost/geometry/core/tags.hpp>
  28. #include <boost/geometry/util/is_inverse_spheroidal_coordinates.hpp>
  29. #include <boost/geometry/util/math.hpp>
  30. #include <boost/geometry/util/select_coordinate_type.hpp>
  31. #include <boost/geometry/algorithms/detail/normalize.hpp>
  32. #include <boost/geometry/algorithms/detail/envelope/transform_units.hpp>
  33. #include <boost/geometry/algorithms/dispatch/expand.hpp>
  34. namespace boost { namespace geometry
  35. {
  36. #ifndef DOXYGEN_NO_DETAIL
  37. namespace detail { namespace expand
  38. {
  39. template <std::size_t Dimension, std::size_t DimensionCount>
  40. struct point_loop
  41. {
  42. template <typename Box, typename Point, typename Strategy>
  43. static inline void apply(Box& box, Point const& source, Strategy const& strategy)
  44. {
  45. typedef typename select_coordinate_type
  46. <
  47. Point, Box
  48. >::type coordinate_type;
  49. std::less<coordinate_type> less;
  50. std::greater<coordinate_type> greater;
  51. coordinate_type const coord = get<Dimension>(source);
  52. if (less(coord, get<min_corner, Dimension>(box)))
  53. {
  54. set<min_corner, Dimension>(box, coord);
  55. }
  56. if (greater(coord, get<max_corner, Dimension>(box)))
  57. {
  58. set<max_corner, Dimension>(box, coord);
  59. }
  60. point_loop<Dimension + 1, DimensionCount>::apply(box, source, strategy);
  61. }
  62. };
  63. template <std::size_t DimensionCount>
  64. struct point_loop<DimensionCount, DimensionCount>
  65. {
  66. template <typename Box, typename Point, typename Strategy>
  67. static inline void apply(Box&, Point const&, Strategy const&) {}
  68. };
  69. // implementation for the spherical and geographic coordinate systems
  70. template <std::size_t DimensionCount, bool IsEquatorial = true>
  71. struct point_loop_on_spheroid
  72. {
  73. template <typename Box, typename Point, typename Strategy>
  74. static inline void apply(Box& box,
  75. Point const& point,
  76. Strategy const& strategy)
  77. {
  78. typedef typename point_type<Box>::type box_point_type;
  79. typedef typename coordinate_type<Box>::type box_coordinate_type;
  80. typedef typename coordinate_system<Box>::type::units units_type;
  81. typedef math::detail::constants_on_spheroid
  82. <
  83. box_coordinate_type,
  84. units_type
  85. > constants;
  86. // normalize input point and input box
  87. Point p_normalized = detail::return_normalized<Point>(point);
  88. // transform input point to be of the same type as the box point
  89. box_point_type box_point;
  90. detail::envelope::transform_units(p_normalized, box_point);
  91. if (is_inverse_spheroidal_coordinates(box))
  92. {
  93. geometry::set_from_radian<min_corner, 0>(box, geometry::get_as_radian<0>(p_normalized));
  94. geometry::set_from_radian<min_corner, 1>(box, geometry::get_as_radian<1>(p_normalized));
  95. geometry::set_from_radian<max_corner, 0>(box, geometry::get_as_radian<0>(p_normalized));
  96. geometry::set_from_radian<max_corner, 1>(box, geometry::get_as_radian<1>(p_normalized));
  97. } else {
  98. detail::normalize(box, box);
  99. box_coordinate_type p_lon = geometry::get<0>(box_point);
  100. box_coordinate_type p_lat = geometry::get<1>(box_point);
  101. typename coordinate_type<Box>::type
  102. b_lon_min = geometry::get<min_corner, 0>(box),
  103. b_lat_min = geometry::get<min_corner, 1>(box),
  104. b_lon_max = geometry::get<max_corner, 0>(box),
  105. b_lat_max = geometry::get<max_corner, 1>(box);
  106. if (math::is_latitude_pole<units_type, IsEquatorial>(p_lat))
  107. {
  108. // the point of expansion is the either the north or the
  109. // south pole; the only important coordinate here is the
  110. // pole's latitude, as the longitude can be anything;
  111. // we, thus, take into account the point's latitude only and return
  112. geometry::set<min_corner, 1>(box, (std::min)(p_lat, b_lat_min));
  113. geometry::set<max_corner, 1>(box, (std::max)(p_lat, b_lat_max));
  114. return;
  115. }
  116. if (math::equals(b_lat_min, b_lat_max)
  117. && math::is_latitude_pole<units_type, IsEquatorial>(b_lat_min))
  118. {
  119. // the box degenerates to either the north or the south pole;
  120. // the only important coordinate here is the pole's latitude,
  121. // as the longitude can be anything;
  122. // we thus take into account the box's latitude only and return
  123. geometry::set<min_corner, 0>(box, p_lon);
  124. geometry::set<min_corner, 1>(box, (std::min)(p_lat, b_lat_min));
  125. geometry::set<max_corner, 0>(box, p_lon);
  126. geometry::set<max_corner, 1>(box, (std::max)(p_lat, b_lat_max));
  127. return;
  128. }
  129. // update latitudes
  130. b_lat_min = (std::min)(b_lat_min, p_lat);
  131. b_lat_max = (std::max)(b_lat_max, p_lat);
  132. // update longitudes
  133. if (math::smaller(p_lon, b_lon_min))
  134. {
  135. box_coordinate_type p_lon_shifted = p_lon + constants::period();
  136. if (math::larger(p_lon_shifted, b_lon_max))
  137. {
  138. // here we could check using: ! math::larger(.., ..)
  139. if (math::smaller(b_lon_min - p_lon, p_lon_shifted - b_lon_max))
  140. {
  141. b_lon_min = p_lon;
  142. }
  143. else
  144. {
  145. b_lon_max = p_lon_shifted;
  146. }
  147. }
  148. }
  149. else if (math::larger(p_lon, b_lon_max))
  150. {
  151. // in this case, and since p_lon is normalized in the range
  152. // (-180, 180], we must have that b_lon_max <= 180
  153. if (b_lon_min < 0
  154. && math::larger(p_lon - b_lon_max,
  155. constants::period() - p_lon + b_lon_min))
  156. {
  157. b_lon_min = p_lon;
  158. b_lon_max += constants::period();
  159. }
  160. else
  161. {
  162. b_lon_max = p_lon;
  163. }
  164. }
  165. geometry::set<min_corner, 0>(box, b_lon_min);
  166. geometry::set<min_corner, 1>(box, b_lat_min);
  167. geometry::set<max_corner, 0>(box, b_lon_max);
  168. geometry::set<max_corner, 1>(box, b_lat_max);
  169. }
  170. point_loop
  171. <
  172. 2, DimensionCount
  173. >::apply(box, point, strategy);
  174. }
  175. };
  176. }} // namespace detail::expand
  177. #endif // DOXYGEN_NO_DETAIL
  178. #ifndef DOXYGEN_NO_DISPATCH
  179. namespace dispatch
  180. {
  181. // Box + point -> new box containing also point
  182. template
  183. <
  184. typename BoxOut, typename Point,
  185. typename CSTagOut, typename CSTag
  186. >
  187. struct expand
  188. <
  189. BoxOut, Point,
  190. box_tag, point_tag,
  191. CSTagOut, CSTag
  192. >
  193. {
  194. BOOST_MPL_ASSERT_MSG((false),
  195. NOT_IMPLEMENTED_FOR_THESE_COORDINATE_SYSTEMS,
  196. (types<CSTagOut, CSTag>()));
  197. };
  198. template <typename BoxOut, typename Point>
  199. struct expand
  200. <
  201. BoxOut, Point,
  202. box_tag, point_tag,
  203. cartesian_tag, cartesian_tag
  204. > : detail::expand::point_loop
  205. <
  206. 0, dimension<Point>::value
  207. >
  208. {};
  209. template <typename BoxOut, typename Point>
  210. struct expand
  211. <
  212. BoxOut, Point,
  213. box_tag, point_tag,
  214. spherical_equatorial_tag, spherical_equatorial_tag
  215. > : detail::expand::point_loop_on_spheroid
  216. <
  217. dimension<Point>::value
  218. >
  219. {};
  220. template <typename BoxOut, typename Point>
  221. struct expand
  222. <
  223. BoxOut, Point,
  224. box_tag, point_tag,
  225. spherical_polar_tag, spherical_polar_tag
  226. > : detail::expand::point_loop_on_spheroid
  227. <
  228. dimension<Point>::value,
  229. false
  230. >
  231. {};
  232. template
  233. <
  234. typename BoxOut, typename Point
  235. >
  236. struct expand
  237. <
  238. BoxOut, Point,
  239. box_tag, point_tag,
  240. geographic_tag, geographic_tag
  241. > : detail::expand::point_loop_on_spheroid
  242. <
  243. dimension<Point>::value
  244. >
  245. {};
  246. } // namespace dispatch
  247. #endif // DOXYGEN_NO_DISPATCH
  248. }} // namespace boost::geometry
  249. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_EXPAND_POINT_HPP