normalize.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2015-2017, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  4. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  5. // Licensed under the Boost Software License version 1.0.
  6. // http://www.boost.org/users/license.html
  7. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_NORMALIZE_HPP
  8. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_NORMALIZE_HPP
  9. #include <cstddef>
  10. #include <boost/numeric/conversion/cast.hpp>
  11. #include <boost/geometry/core/access.hpp>
  12. #include <boost/geometry/core/coordinate_system.hpp>
  13. #include <boost/geometry/core/coordinate_type.hpp>
  14. #include <boost/geometry/core/cs.hpp>
  15. #include <boost/geometry/core/tag.hpp>
  16. #include <boost/geometry/core/tags.hpp>
  17. #include <boost/geometry/util/normalize_spheroidal_coordinates.hpp>
  18. #include <boost/geometry/util/normalize_spheroidal_box_coordinates.hpp>
  19. #include <boost/geometry/views/detail/indexed_point_view.hpp>
  20. namespace boost { namespace geometry
  21. {
  22. #ifndef DOXYGEN_NO_DETAIL
  23. namespace detail { namespace normalization
  24. {
  25. struct do_nothing
  26. {
  27. template <typename GeometryIn, typename GeometryOut>
  28. static inline void apply(GeometryIn const&, GeometryOut&)
  29. {
  30. }
  31. };
  32. template <std::size_t Dimension, std::size_t DimensionCount>
  33. struct assign_loop
  34. {
  35. template <typename CoordinateType, typename PointIn, typename PointOut>
  36. static inline void apply(CoordinateType const& longitude,
  37. CoordinateType const& latitude,
  38. PointIn const& point_in,
  39. PointOut& point_out)
  40. {
  41. geometry::set<Dimension>(point_out, boost::numeric_cast
  42. <
  43. typename coordinate_type<PointOut>::type
  44. >(geometry::get<Dimension>(point_in)));
  45. assign_loop
  46. <
  47. Dimension + 1, DimensionCount
  48. >::apply(longitude, latitude, point_in, point_out);
  49. }
  50. };
  51. template <std::size_t DimensionCount>
  52. struct assign_loop<DimensionCount, DimensionCount>
  53. {
  54. template <typename CoordinateType, typename PointIn, typename PointOut>
  55. static inline void apply(CoordinateType const&,
  56. CoordinateType const&,
  57. PointIn const&,
  58. PointOut&)
  59. {
  60. }
  61. };
  62. template <std::size_t DimensionCount>
  63. struct assign_loop<0, DimensionCount>
  64. {
  65. template <typename CoordinateType, typename PointIn, typename PointOut>
  66. static inline void apply(CoordinateType const& longitude,
  67. CoordinateType const& latitude,
  68. PointIn const& point_in,
  69. PointOut& point_out)
  70. {
  71. geometry::set<0>(point_out, boost::numeric_cast
  72. <
  73. typename coordinate_type<PointOut>::type
  74. >(longitude));
  75. assign_loop
  76. <
  77. 1, DimensionCount
  78. >::apply(longitude, latitude, point_in, point_out);
  79. }
  80. };
  81. template <std::size_t DimensionCount>
  82. struct assign_loop<1, DimensionCount>
  83. {
  84. template <typename CoordinateType, typename PointIn, typename PointOut>
  85. static inline void apply(CoordinateType const& longitude,
  86. CoordinateType const& latitude,
  87. PointIn const& point_in,
  88. PointOut& point_out)
  89. {
  90. geometry::set<1>(point_out, boost::numeric_cast
  91. <
  92. typename coordinate_type<PointOut>::type
  93. >(latitude));
  94. assign_loop
  95. <
  96. 2, DimensionCount
  97. >::apply(longitude, latitude, point_in, point_out);
  98. }
  99. };
  100. template <typename PointIn, typename PointOut, bool IsEquatorial = true>
  101. struct normalize_point
  102. {
  103. static inline void apply(PointIn const& point_in, PointOut& point_out)
  104. {
  105. typedef typename coordinate_type<PointIn>::type in_coordinate_type;
  106. in_coordinate_type longitude = geometry::get<0>(point_in);
  107. in_coordinate_type latitude = geometry::get<1>(point_in);
  108. math::normalize_spheroidal_coordinates
  109. <
  110. typename coordinate_system<PointIn>::type::units,
  111. IsEquatorial,
  112. in_coordinate_type
  113. >(longitude, latitude);
  114. assign_loop
  115. <
  116. 0, dimension<PointIn>::value
  117. >::apply(longitude, latitude, point_in, point_out);
  118. }
  119. };
  120. template <typename BoxIn, typename BoxOut, bool IsEquatorial = true>
  121. class normalize_box
  122. {
  123. template <typename UnitsIn, typename UnitsOut, typename CoordinateInType>
  124. static inline void apply_to_coordinates(CoordinateInType& lon_min,
  125. CoordinateInType& lat_min,
  126. CoordinateInType& lon_max,
  127. CoordinateInType& lat_max,
  128. BoxIn const& box_in,
  129. BoxOut& box_out)
  130. {
  131. detail::indexed_point_view<BoxOut, min_corner> p_min_out(box_out);
  132. assign_loop
  133. <
  134. 0, dimension<BoxIn>::value
  135. >::apply(lon_min,
  136. lat_min,
  137. detail::indexed_point_view
  138. <
  139. BoxIn const, min_corner
  140. >(box_in),
  141. p_min_out);
  142. detail::indexed_point_view<BoxOut, max_corner> p_max_out(box_out);
  143. assign_loop
  144. <
  145. 0, dimension<BoxIn>::value
  146. >::apply(lon_max,
  147. lat_max,
  148. detail::indexed_point_view
  149. <
  150. BoxIn const, max_corner
  151. >(box_in),
  152. p_max_out);
  153. }
  154. public:
  155. static inline void apply(BoxIn const& box_in, BoxOut& box_out)
  156. {
  157. typedef typename coordinate_type<BoxIn>::type in_coordinate_type;
  158. in_coordinate_type lon_min = geometry::get<min_corner, 0>(box_in);
  159. in_coordinate_type lat_min = geometry::get<min_corner, 1>(box_in);
  160. in_coordinate_type lon_max = geometry::get<max_corner, 0>(box_in);
  161. in_coordinate_type lat_max = geometry::get<max_corner, 1>(box_in);
  162. math::normalize_spheroidal_box_coordinates
  163. <
  164. typename coordinate_system<BoxIn>::type::units,
  165. IsEquatorial,
  166. in_coordinate_type
  167. >(lon_min, lat_min, lon_max, lat_max);
  168. apply_to_coordinates
  169. <
  170. typename coordinate_system<BoxIn>::type::units,
  171. typename coordinate_system<BoxOut>::type::units
  172. >(lon_min, lat_min, lon_max, lat_max, box_in, box_out);
  173. }
  174. };
  175. }} // namespace detail::normalization
  176. #endif // DOXYGEN_NO_DETAIL
  177. #ifndef DOXYGEN_NO_DISPATCH
  178. namespace dispatch
  179. {
  180. template
  181. <
  182. typename GeometryIn,
  183. typename GeometryOut,
  184. typename TagIn = typename tag<GeometryIn>::type,
  185. typename TagOut = typename tag<GeometryOut>::type,
  186. typename CSTagIn = typename cs_tag<GeometryIn>::type,
  187. typename CSTagOut = typename cs_tag<GeometryOut>::type
  188. >
  189. struct normalize : detail::normalization::do_nothing
  190. {};
  191. template <typename PointIn, typename PointOut>
  192. struct normalize
  193. <
  194. PointIn, PointOut, point_tag, point_tag,
  195. spherical_equatorial_tag, spherical_equatorial_tag
  196. > : detail::normalization::normalize_point<PointIn, PointOut>
  197. {};
  198. template <typename PointIn, typename PointOut>
  199. struct normalize
  200. <
  201. PointIn, PointOut, point_tag, point_tag,
  202. spherical_polar_tag, spherical_polar_tag
  203. > : detail::normalization::normalize_point<PointIn, PointOut, false>
  204. {};
  205. template <typename PointIn, typename PointOut>
  206. struct normalize
  207. <
  208. PointIn, PointOut, point_tag, point_tag, geographic_tag, geographic_tag
  209. > : detail::normalization::normalize_point<PointIn, PointOut>
  210. {};
  211. template <typename BoxIn, typename BoxOut>
  212. struct normalize
  213. <
  214. BoxIn, BoxOut, box_tag, box_tag,
  215. spherical_equatorial_tag, spherical_equatorial_tag
  216. > : detail::normalization::normalize_box<BoxIn, BoxOut>
  217. {};
  218. template <typename BoxIn, typename BoxOut>
  219. struct normalize
  220. <
  221. BoxIn, BoxOut, box_tag, box_tag,
  222. spherical_polar_tag, spherical_polar_tag
  223. > : detail::normalization::normalize_box<BoxIn, BoxOut, false>
  224. {};
  225. template <typename BoxIn, typename BoxOut>
  226. struct normalize
  227. <
  228. BoxIn, BoxOut, box_tag, box_tag, geographic_tag, geographic_tag
  229. > : detail::normalization::normalize_box<BoxIn, BoxOut>
  230. {};
  231. } // namespace dispatch
  232. #endif // DOXYGEN_NO_DISPATCH
  233. #ifndef DOXYGEN_NO_DETAIL
  234. namespace detail
  235. {
  236. template <typename GeometryIn, typename GeometryOut>
  237. inline void normalize(GeometryIn const& geometry_in, GeometryOut& geometry_out)
  238. {
  239. dispatch::normalize
  240. <
  241. GeometryIn, GeometryOut
  242. >::apply(geometry_in, geometry_out);
  243. }
  244. template <typename GeometryOut, typename GeometryIn>
  245. inline GeometryOut return_normalized(GeometryIn const& geometry_in)
  246. {
  247. GeometryOut geometry_out;
  248. detail::normalize(geometry_in, geometry_out);
  249. return geometry_out;
  250. }
  251. } // namespace detail
  252. #endif // DOXYGEN_NO_DETAIL
  253. }} // namespace boost::geometry
  254. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_NORMALIZE_HPP