point_in_box.hpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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-2018.
  6. // Modifications copyright (c) 2015-2018, 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/core/cs.hpp>
  18. #include <boost/geometry/strategies/covered_by.hpp>
  19. #include <boost/geometry/strategies/within.hpp>
  20. #include <boost/geometry/util/normalize_spheroidal_coordinates.hpp>
  21. namespace boost { namespace geometry { namespace strategy
  22. {
  23. namespace within
  24. {
  25. #ifndef DOXYGEN_NO_DETAIL
  26. namespace detail
  27. {
  28. struct within_coord
  29. {
  30. template <typename Value1, typename Value2>
  31. static inline bool apply(Value1 const& value, Value2 const& min_value, Value2 const& max_value)
  32. {
  33. return value > min_value && value < max_value;
  34. }
  35. };
  36. struct covered_by_coord
  37. {
  38. template <typename Value1, typename Value2>
  39. static inline bool apply(Value1 const& value, Value2 const& min_value, Value2 const& max_value)
  40. {
  41. return value >= min_value && value <= max_value;
  42. }
  43. };
  44. template <typename Geometry, std::size_t Dimension, typename CSTag>
  45. struct within_range
  46. : within_coord
  47. {};
  48. template <typename Geometry, std::size_t Dimension, typename CSTag>
  49. struct covered_by_range
  50. : covered_by_coord
  51. {};
  52. // NOTE: the result would be the same if instead of structs defined below
  53. // the above xxx_range were used with the following arguments:
  54. // (min_value + diff_min, min_value, max_value)
  55. struct within_longitude_diff
  56. {
  57. template <typename CalcT>
  58. static inline bool apply(CalcT const& diff_min, CalcT const& min_value, CalcT const& max_value)
  59. {
  60. CalcT const c0 = 0;
  61. return diff_min > c0
  62. && (min_value + diff_min < max_value
  63. /*|| max_value - diff_min > min_value*/);
  64. }
  65. };
  66. struct covered_by_longitude_diff
  67. {
  68. template <typename CalcT>
  69. static inline bool apply(CalcT const& diff_min, CalcT const& min_value, CalcT const& max_value)
  70. {
  71. return min_value + diff_min <= max_value
  72. /*|| max_value - diff_min >= min_value*/;
  73. }
  74. };
  75. template <typename Geometry,
  76. typename CoordCheck,
  77. typename DiffCheck>
  78. struct longitude_range
  79. {
  80. template <typename Value1, typename Value2>
  81. static inline bool apply(Value1 const& value, Value2 const& min_value, Value2 const& max_value)
  82. {
  83. typedef typename select_most_precise
  84. <
  85. Value1, Value2
  86. >::type calc_t;
  87. typedef typename geometry::detail::cs_angular_units<Geometry>::type units_t;
  88. typedef math::detail::constants_on_spheroid<calc_t, units_t> constants;
  89. if (CoordCheck::apply(value, min_value, max_value))
  90. {
  91. return true;
  92. }
  93. // min <= max <=> diff >= 0
  94. calc_t const diff_ing = max_value - min_value;
  95. // if containing covers the whole globe it contains all
  96. if (diff_ing >= constants::period())
  97. {
  98. return true;
  99. }
  100. // calculate positive longitude translation with min_value as origin
  101. calc_t const diff_min = math::longitude_distance_unsigned<units_t, calc_t>(min_value, value);
  102. return DiffCheck::template apply<calc_t>(diff_min, min_value, max_value);
  103. }
  104. };
  105. // spherical_equatorial_tag, spherical_polar_tag and geographic_cat are casted to spherical_tag
  106. template <typename Geometry>
  107. struct within_range<Geometry, 0, spherical_tag>
  108. : longitude_range<Geometry, within_coord, within_longitude_diff>
  109. {};
  110. template <typename Geometry>
  111. struct covered_by_range<Geometry, 0, spherical_tag>
  112. : longitude_range<Geometry, covered_by_coord, covered_by_longitude_diff>
  113. {};
  114. template
  115. <
  116. template <typename, std::size_t, typename> class SubStrategy,
  117. typename CSTag, // cartesian_tag or spherical_tag
  118. std::size_t Dimension,
  119. std::size_t DimensionCount
  120. >
  121. struct relate_point_box_loop
  122. {
  123. template <typename Point, typename Box>
  124. static inline bool apply(Point const& point, Box const& box)
  125. {
  126. if (! SubStrategy<Point, Dimension, CSTag>::apply(get<Dimension>(point),
  127. get<min_corner, Dimension>(box),
  128. get<max_corner, Dimension>(box))
  129. )
  130. {
  131. return false;
  132. }
  133. return relate_point_box_loop
  134. <
  135. SubStrategy,
  136. CSTag,
  137. Dimension + 1, DimensionCount
  138. >::apply(point, box);
  139. }
  140. };
  141. template
  142. <
  143. template <typename, std::size_t, typename> class SubStrategy,
  144. typename CSTag,
  145. std::size_t DimensionCount
  146. >
  147. struct relate_point_box_loop<SubStrategy, CSTag, DimensionCount, DimensionCount>
  148. {
  149. template <typename Point, typename Box>
  150. static inline bool apply(Point const& , Box const& )
  151. {
  152. return true;
  153. }
  154. };
  155. } // namespace detail
  156. #endif // DOXYGEN_NO_DETAIL
  157. struct cartesian_point_box
  158. {
  159. template <typename Point, typename Box>
  160. static inline bool apply(Point const& point, Box const& box)
  161. {
  162. return detail::relate_point_box_loop
  163. <
  164. detail::within_range,
  165. cartesian_tag,
  166. 0, dimension<Point>::value
  167. >::apply(point, box);
  168. }
  169. };
  170. struct spherical_point_box
  171. {
  172. template <typename Point, typename Box>
  173. static inline bool apply(Point const& point, Box const& box)
  174. {
  175. return detail::relate_point_box_loop
  176. <
  177. detail::within_range,
  178. spherical_tag,
  179. 0, dimension<Point>::value
  180. >::apply(point, box);
  181. }
  182. };
  183. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  184. namespace services
  185. {
  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. cartesian_tag, cartesian_tag
  193. >
  194. {
  195. typedef within::cartesian_point_box type;
  196. };
  197. // spherical_equatorial_tag, spherical_polar_tag and geographic_cat are casted to spherical_tag
  198. template <typename Point, typename Box>
  199. struct default_strategy
  200. <
  201. Point, Box,
  202. point_tag, box_tag,
  203. pointlike_tag, areal_tag,
  204. spherical_tag, spherical_tag
  205. >
  206. {
  207. typedef within::spherical_point_box type;
  208. };
  209. } // namespace services
  210. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  211. } // namespace within
  212. namespace covered_by
  213. {
  214. struct cartesian_point_box
  215. {
  216. template <typename Point, typename Box>
  217. static inline bool apply(Point const& point, Box const& box)
  218. {
  219. return within::detail::relate_point_box_loop
  220. <
  221. within::detail::covered_by_range,
  222. cartesian_tag,
  223. 0, dimension<Point>::value
  224. >::apply(point, box);
  225. }
  226. };
  227. struct spherical_point_box
  228. {
  229. template <typename Point, typename Box>
  230. static inline bool apply(Point const& point, Box const& box)
  231. {
  232. return within::detail::relate_point_box_loop
  233. <
  234. within::detail::covered_by_range,
  235. spherical_tag,
  236. 0, dimension<Point>::value
  237. >::apply(point, box);
  238. }
  239. };
  240. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  241. namespace services
  242. {
  243. template <typename Point, typename Box>
  244. struct default_strategy
  245. <
  246. Point, Box,
  247. point_tag, box_tag,
  248. pointlike_tag, areal_tag,
  249. cartesian_tag, cartesian_tag
  250. >
  251. {
  252. typedef covered_by::cartesian_point_box type;
  253. };
  254. // spherical_equatorial_tag, spherical_polar_tag and geographic_cat are casted to spherical_tag
  255. template <typename Point, typename Box>
  256. struct default_strategy
  257. <
  258. Point, Box,
  259. point_tag, box_tag,
  260. pointlike_tag, areal_tag,
  261. spherical_tag, spherical_tag
  262. >
  263. {
  264. typedef covered_by::spherical_point_box type;
  265. };
  266. } // namespace services
  267. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  268. } // namespace covered_by
  269. }}} // namespace boost::geometry::strategy
  270. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_POINT_IN_BOX_HPP