point_in_box.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. // This file was modified by Oracle on 2015-2017.
  6. // Modifications copyright (c) 2015-2017, Oracle and/or its affiliates.
  7. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  8. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  9. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  10. // Use, modification and distribution is subject to the Boost Software License,
  11. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_POINT_IN_BOX_HPP
  14. #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_POINT_IN_BOX_HPP
  15. #include <boost/geometry/core/access.hpp>
  16. #include <boost/geometry/core/coordinate_dimension.hpp>
  17. #include <boost/geometry/strategies/covered_by.hpp>
  18. #include <boost/geometry/strategies/within.hpp>
  19. #include <boost/geometry/util/normalize_spheroidal_coordinates.hpp>
  20. namespace boost { namespace geometry { namespace strategy
  21. {
  22. namespace within
  23. {
  24. struct within_coord
  25. {
  26. template <typename Value1, typename Value2>
  27. static inline bool apply(Value1 const& value, Value2 const& min_value, Value2 const& max_value)
  28. {
  29. return value > min_value && value < max_value;
  30. }
  31. };
  32. struct covered_by_coord
  33. {
  34. template <typename Value1, typename Value2>
  35. static inline bool apply(Value1 const& value, Value2 const& min_value, Value2 const& max_value)
  36. {
  37. return value >= min_value && value <= max_value;
  38. }
  39. };
  40. template <typename Geometry, std::size_t Dimension, typename CSTag>
  41. struct within_range
  42. : within_coord
  43. {};
  44. template <typename Geometry, std::size_t Dimension, typename CSTag>
  45. struct covered_by_range
  46. : covered_by_coord
  47. {};
  48. // NOTE: the result would be the same if instead of structs defined below
  49. // the above xxx_range were used with the following arguments:
  50. // (min_value + diff_min, min_value, max_value)
  51. struct within_longitude_diff
  52. {
  53. template <typename CalcT>
  54. static inline bool apply(CalcT const& diff_min, CalcT const& min_value, CalcT const& max_value)
  55. {
  56. CalcT const c0 = 0;
  57. return diff_min > c0
  58. && (min_value + diff_min < max_value
  59. /*|| max_value - diff_min > min_value*/);
  60. }
  61. };
  62. struct covered_by_longitude_diff
  63. {
  64. template <typename CalcT>
  65. static inline bool apply(CalcT const& diff_min, CalcT const& min_value, CalcT const& max_value)
  66. {
  67. return min_value + diff_min <= max_value
  68. /*|| max_value - diff_min >= min_value*/;
  69. }
  70. };
  71. template <typename Geometry,
  72. typename CoordCheck,
  73. typename DiffCheck>
  74. struct longitude_range
  75. {
  76. template <typename Value1, typename Value2>
  77. static inline bool apply(Value1 const& value, Value2 const& min_value, Value2 const& max_value)
  78. {
  79. typedef typename select_most_precise
  80. <
  81. Value1, Value2
  82. >::type calc_t;
  83. typedef typename coordinate_system<Geometry>::type::units units_t;
  84. typedef math::detail::constants_on_spheroid<calc_t, units_t> constants;
  85. if (CoordCheck::apply(value, min_value, max_value))
  86. {
  87. return true;
  88. }
  89. // min <= max <=> diff >= 0
  90. calc_t const diff_ing = max_value - min_value;
  91. // if containing covers the whole globe it contains all
  92. if (diff_ing >= constants::period())
  93. {
  94. return true;
  95. }
  96. // calculate positive longitude translation with min_value as origin
  97. calc_t const diff_min = math::longitude_distance_unsigned<units_t, calc_t>(min_value, value);
  98. return DiffCheck::template apply<calc_t>(diff_min, min_value, max_value);
  99. }
  100. };
  101. // spherical_equatorial_tag, spherical_polar_tag and geographic_cat are casted to spherical_tag
  102. template <typename Geometry>
  103. struct within_range<Geometry, 0, spherical_tag>
  104. : longitude_range<Geometry, within_coord, within_longitude_diff>
  105. {};
  106. template <typename Geometry>
  107. struct covered_by_range<Geometry, 0, spherical_tag>
  108. : longitude_range<Geometry, covered_by_coord, covered_by_longitude_diff>
  109. {};
  110. template
  111. <
  112. template <typename, std::size_t, typename> class SubStrategy,
  113. typename Point,
  114. typename Box,
  115. std::size_t Dimension,
  116. std::size_t DimensionCount
  117. >
  118. struct relate_point_box_loop
  119. {
  120. static inline bool apply(Point const& point, Box const& box)
  121. {
  122. typedef typename tag_cast<typename cs_tag<Point>::type, spherical_tag>::type cs_tag_t;
  123. if (! SubStrategy<Point, Dimension, cs_tag_t>::apply(get<Dimension>(point),
  124. get<min_corner, Dimension>(box),
  125. get<max_corner, Dimension>(box))
  126. )
  127. {
  128. return false;
  129. }
  130. return relate_point_box_loop
  131. <
  132. SubStrategy,
  133. Point, Box,
  134. Dimension + 1, DimensionCount
  135. >::apply(point, box);
  136. }
  137. };
  138. template
  139. <
  140. template <typename, std::size_t, typename> class SubStrategy,
  141. typename Point,
  142. typename Box,
  143. std::size_t DimensionCount
  144. >
  145. struct relate_point_box_loop<SubStrategy, Point, Box, DimensionCount, DimensionCount>
  146. {
  147. static inline bool apply(Point const& , Box const& )
  148. {
  149. return true;
  150. }
  151. };
  152. template
  153. <
  154. typename Point,
  155. typename Box,
  156. template <typename, std::size_t, typename> class SubStrategy = within_range
  157. >
  158. struct point_in_box
  159. {
  160. static inline bool apply(Point const& point, Box const& box)
  161. {
  162. return relate_point_box_loop
  163. <
  164. SubStrategy,
  165. Point, Box,
  166. 0, dimension<Point>::type::value
  167. >::apply(point, box);
  168. }
  169. };
  170. } // namespace within
  171. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  172. namespace within { namespace services
  173. {
  174. template <typename Point, typename Box>
  175. struct default_strategy
  176. <
  177. Point, Box,
  178. point_tag, box_tag,
  179. pointlike_tag, areal_tag,
  180. cartesian_tag, cartesian_tag
  181. >
  182. {
  183. typedef within::point_in_box<Point, Box> type;
  184. };
  185. // spherical_equatorial_tag, spherical_polar_tag and geographic_cat are casted to spherical_tag
  186. template <typename Point, typename Box>
  187. struct default_strategy
  188. <
  189. Point, Box,
  190. point_tag, box_tag,
  191. pointlike_tag, areal_tag,
  192. spherical_tag, spherical_tag
  193. >
  194. {
  195. typedef within::point_in_box<Point, Box> type;
  196. };
  197. }} // namespace within::services
  198. namespace covered_by { namespace services
  199. {
  200. template <typename Point, typename Box>
  201. struct default_strategy
  202. <
  203. Point, Box,
  204. point_tag, box_tag,
  205. pointlike_tag, areal_tag,
  206. cartesian_tag, cartesian_tag
  207. >
  208. {
  209. typedef within::point_in_box
  210. <
  211. Point, Box,
  212. within::covered_by_range
  213. > type;
  214. };
  215. // spherical_equatorial_tag, spherical_polar_tag and geographic_cat are casted to spherical_tag
  216. template <typename Point, typename Box>
  217. struct default_strategy
  218. <
  219. Point, Box,
  220. point_tag, box_tag,
  221. pointlike_tag, areal_tag,
  222. spherical_tag, spherical_tag
  223. >
  224. {
  225. typedef within::point_in_box
  226. <
  227. Point, Box,
  228. within::covered_by_range
  229. > type;
  230. };
  231. }} // namespace covered_by::services
  232. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  233. }}} // namespace boost::geometry::strategy
  234. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_POINT_IN_BOX_HPP