get_rescale_policy.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2014-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2014-2015 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2014-2015 Mateusz Loskot, London, UK.
  5. // Copyright (c) 2014-2015 Adam Wulkiewicz, Lodz, Poland.
  6. // This file was modified by Oracle on 2015-2020.
  7. // Modifications copyright (c) 2015-2020, Oracle and/or its affiliates.
  8. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  9. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  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_POLICIES_ROBUSTNESS_GET_RESCALE_POLICY_HPP
  14. #define BOOST_GEOMETRY_POLICIES_ROBUSTNESS_GET_RESCALE_POLICY_HPP
  15. #include <cstddef>
  16. #include <type_traits>
  17. #include <boost/geometry/core/assert.hpp>
  18. #include <boost/geometry/core/config.hpp>
  19. #include <boost/geometry/core/coordinate_promotion.hpp>
  20. #include <boost/geometry/core/tag_cast.hpp>
  21. #include <boost/geometry/algorithms/envelope.hpp>
  22. #include <boost/geometry/algorithms/expand.hpp>
  23. #include <boost/geometry/algorithms/is_empty.hpp>
  24. #include <boost/geometry/algorithms/detail/recalculate.hpp>
  25. #include <boost/geometry/algorithms/detail/get_max_size.hpp>
  26. #include <boost/geometry/core/static_assert.hpp>
  27. #include <boost/geometry/geometries/point.hpp>
  28. #include <boost/geometry/geometries/box.hpp>
  29. #include <boost/geometry/policies/robustness/no_rescale_policy.hpp>
  30. #include <boost/geometry/policies/robustness/rescale_policy.hpp>
  31. #include <boost/geometry/policies/robustness/robust_type.hpp>
  32. #include <boost/geometry/util/numeric_cast.hpp>
  33. #include <boost/geometry/util/type_traits.hpp>
  34. // TEMP
  35. #include <boost/geometry/strategies/envelope/cartesian.hpp>
  36. #include <boost/geometry/strategies/envelope/geographic.hpp>
  37. #include <boost/geometry/strategies/envelope/spherical.hpp>
  38. namespace boost { namespace geometry
  39. {
  40. #ifndef DOXYGEN_NO_DETAIL
  41. namespace detail { namespace get_rescale_policy
  42. {
  43. template
  44. <
  45. typename Box,
  46. typename Point,
  47. typename RobustPoint,
  48. typename Factor
  49. >
  50. inline void scale_box_to_integer_range(Box const& box,
  51. Point& min_point,
  52. RobustPoint& min_robust_point,
  53. Factor& factor)
  54. {
  55. // Scale box to integer-range
  56. typedef typename promote_floating_point
  57. <
  58. typename geometry::coordinate_type<Point>::type
  59. >::type num_type;
  60. num_type const diff = util::numeric_cast<num_type>(detail::get_max_size(box));
  61. num_type const range = 10000000.0; // Define a large range to get precise integer coordinates
  62. num_type const half = 0.5;
  63. if (math::equals(diff, num_type())
  64. || diff >= range
  65. || ! boost::math::isfinite(diff))
  66. {
  67. factor = 1;
  68. }
  69. else
  70. {
  71. factor = util::numeric_cast<num_type>(
  72. util::numeric_cast<boost::long_long_type>(half + range / diff));
  73. BOOST_GEOMETRY_ASSERT(factor >= 1);
  74. }
  75. // Assign input/output minimal points
  76. detail::assign_point_from_index<0>(box, min_point);
  77. num_type const two = 2;
  78. boost::long_long_type const min_coordinate
  79. = util::numeric_cast<boost::long_long_type>(-range / two);
  80. assign_values(min_robust_point, min_coordinate, min_coordinate);
  81. }
  82. template
  83. <
  84. typename Point, typename RobustPoint, typename Geometry,
  85. typename Factor, typename Strategy
  86. >
  87. static inline void init_rescale_policy(Geometry const& geometry,
  88. Point& min_point,
  89. RobustPoint& min_robust_point,
  90. Factor& factor,
  91. Strategy const& strategy)
  92. {
  93. if (geometry::is_empty(geometry))
  94. {
  95. return;
  96. }
  97. // Get bounding box
  98. model::box<Point> env = geometry::return_envelope
  99. <
  100. model::box<Point>
  101. >(geometry, strategy);
  102. scale_box_to_integer_range(env, min_point, min_robust_point, factor);
  103. }
  104. // NOTE: Actually it should take 2 separate strategies, one for each geometry
  105. // in case one of them was e.g. a Box
  106. template
  107. <
  108. typename Point, typename RobustPoint, typename Geometry1, typename Geometry2,
  109. typename Factor, typename Strategy1, typename Strategy2
  110. >
  111. static inline void init_rescale_policy(Geometry1 const& geometry1,
  112. Geometry2 const& geometry2,
  113. Point& min_point,
  114. RobustPoint& min_robust_point,
  115. Factor& factor,
  116. Strategy1 const& strategy1,
  117. Strategy2 const& strategy2)
  118. {
  119. // Get bounding boxes (when at least one of the geometries is not empty)
  120. bool const is_empty1 = geometry::is_empty(geometry1);
  121. bool const is_empty2 = geometry::is_empty(geometry2);
  122. if (is_empty1 && is_empty2)
  123. {
  124. return;
  125. }
  126. model::box<Point> env;
  127. if (is_empty1)
  128. {
  129. geometry::envelope(geometry2, env, strategy2);
  130. }
  131. else if (is_empty2)
  132. {
  133. geometry::envelope(geometry1, env, strategy1);
  134. }
  135. else
  136. {
  137. // The following approach (envelope + expand) may not give the
  138. // optimal MBR when then two geometries are in the spherical
  139. // equatorial or geographic coordinate systems.
  140. // TODO: implement envelope for two (or possibly more geometries)
  141. geometry::envelope(geometry1, env, strategy1);
  142. model::box<Point> env2 = geometry::return_envelope
  143. <
  144. model::box<Point>
  145. >(geometry2, strategy2);
  146. geometry::expand(env, env2, strategy1);
  147. }
  148. scale_box_to_integer_range(env, min_point, min_robust_point, factor);
  149. }
  150. template
  151. <
  152. typename Point,
  153. bool IsFloatingPoint
  154. >
  155. struct rescale_policy_type
  156. {
  157. typedef no_rescale_policy type;
  158. };
  159. // We rescale only all FP types
  160. template
  161. <
  162. typename Point
  163. >
  164. struct rescale_policy_type<Point, true>
  165. {
  166. typedef typename geometry::coordinate_type<Point>::type coordinate_type;
  167. typedef model::point
  168. <
  169. typename detail::robust_type<coordinate_type>::type,
  170. geometry::dimension<Point>::value,
  171. typename geometry::coordinate_system<Point>::type
  172. > robust_point_type;
  173. typedef typename promote_floating_point<coordinate_type>::type factor_type;
  174. typedef detail::robust_policy<Point, robust_point_type, factor_type> type;
  175. };
  176. template <typename Policy>
  177. struct get_rescale_policy
  178. {
  179. template <typename Geometry, typename Strategy>
  180. static inline Policy apply(Geometry const& geometry,
  181. Strategy const& strategy)
  182. {
  183. typedef typename point_type<Geometry>::type point_type;
  184. typedef typename geometry::coordinate_type<Geometry>::type coordinate_type;
  185. typedef typename promote_floating_point<coordinate_type>::type factor_type;
  186. typedef model::point
  187. <
  188. typename detail::robust_type<coordinate_type>::type,
  189. geometry::dimension<point_type>::value,
  190. typename geometry::coordinate_system<point_type>::type
  191. > robust_point_type;
  192. point_type min_point;
  193. robust_point_type min_robust_point;
  194. factor_type factor;
  195. init_rescale_policy(geometry, min_point, min_robust_point,
  196. factor, strategy);
  197. return Policy(min_point, min_robust_point, factor);
  198. }
  199. template <typename Geometry1, typename Geometry2, typename Strategy1, typename Strategy2>
  200. static inline Policy apply(Geometry1 const& geometry1, Geometry2 const& geometry2,
  201. Strategy1 const& strategy1,
  202. Strategy2 const& strategy2)
  203. {
  204. typedef typename point_type<Geometry1>::type point_type;
  205. typedef typename geometry::coordinate_type<Geometry1>::type coordinate_type;
  206. typedef typename promote_floating_point<coordinate_type>::type factor_type;
  207. typedef model::point
  208. <
  209. typename detail::robust_type<coordinate_type>::type,
  210. geometry::dimension<point_type>::value,
  211. typename geometry::coordinate_system<point_type>::type
  212. > robust_point_type;
  213. point_type min_point;
  214. robust_point_type min_robust_point;
  215. factor_type factor;
  216. init_rescale_policy(geometry1, geometry2, min_point, min_robust_point,
  217. factor, strategy1, strategy2);
  218. return Policy(min_point, min_robust_point, factor);
  219. }
  220. };
  221. // Specialization for no-rescaling
  222. template <>
  223. struct get_rescale_policy<no_rescale_policy>
  224. {
  225. template <typename Geometry, typename EnvelopeStrategy>
  226. static inline no_rescale_policy apply(Geometry const& , EnvelopeStrategy const&)
  227. {
  228. return no_rescale_policy();
  229. }
  230. template <typename Geometry1, typename Geometry2, typename EnvelopeStrategy1, typename EnvelopeStrategy2>
  231. static inline no_rescale_policy apply(Geometry1 const& , Geometry2 const& ,
  232. EnvelopeStrategy1 const& , EnvelopeStrategy2 const& )
  233. {
  234. return no_rescale_policy();
  235. }
  236. };
  237. }} // namespace detail::get_rescale_policy
  238. #endif // DOXYGEN_NO_DETAIL
  239. template
  240. <
  241. typename Point,
  242. typename CSTag = typename geometry::cs_tag<Point>::type
  243. >
  244. struct rescale_policy_type
  245. : public detail::get_rescale_policy::rescale_policy_type
  246. <
  247. Point,
  248. #if defined(BOOST_GEOMETRY_USE_RESCALING)
  249. std::is_floating_point
  250. <
  251. typename geometry::coordinate_type<Point>::type
  252. >::type::value
  253. &&
  254. std::is_same
  255. <
  256. CSTag,
  257. geometry::cartesian_tag
  258. >::value
  259. #else
  260. false
  261. #endif
  262. >
  263. {
  264. BOOST_GEOMETRY_STATIC_ASSERT(
  265. (util::is_point<Point>::value),
  266. "Point type expected.",
  267. Point);
  268. };
  269. template
  270. <
  271. typename Geometry1,
  272. typename Geometry2,
  273. typename CSTag = typename geometry::cs_tag<Geometry1>::type,
  274. typename Tag1 = typename tag_cast
  275. <
  276. typename tag<Geometry1>::type,
  277. box_tag,
  278. pointlike_tag,
  279. linear_tag,
  280. areal_tag
  281. >::type,
  282. typename Tag2 = typename tag_cast
  283. <
  284. typename tag<Geometry2>::type,
  285. box_tag,
  286. pointlike_tag,
  287. linear_tag,
  288. areal_tag
  289. >::type
  290. >
  291. struct rescale_overlay_policy_type
  292. // Default: no rescaling
  293. : public detail::get_rescale_policy::rescale_policy_type
  294. <
  295. typename geometry::point_type<Geometry1>::type,
  296. false
  297. >
  298. {};
  299. // Areal/areal: get rescale policy based on coordinate type
  300. template
  301. <
  302. typename Geometry1,
  303. typename Geometry2,
  304. typename CSTag
  305. >
  306. struct rescale_overlay_policy_type<Geometry1, Geometry2, CSTag, areal_tag, areal_tag>
  307. : public rescale_policy_type
  308. <
  309. typename geometry::point_type<Geometry1>::type,
  310. CSTag
  311. >
  312. {};
  313. template <typename Policy, typename Geometry>
  314. inline Policy get_rescale_policy(Geometry const& geometry)
  315. {
  316. typename geometry::strategies::envelope::services::default_strategy
  317. <
  318. Geometry,
  319. model::box<typename point_type<Geometry>::type>
  320. >::type strategy;
  321. return detail::get_rescale_policy::get_rescale_policy<Policy>::apply(geometry, strategy);
  322. }
  323. template
  324. <
  325. typename Policy, typename Geometry, typename Strategy,
  326. std::enable_if_t<std::is_void<typename geometry::tag<Strategy>::type>::value, int> = 0
  327. >
  328. inline Policy get_rescale_policy(Geometry const& geometry, Strategy const& strategy)
  329. {
  330. return detail::get_rescale_policy::get_rescale_policy
  331. <
  332. Policy
  333. >::apply(geometry, strategy);
  334. }
  335. template
  336. <
  337. typename Policy, typename Geometry1, typename Geometry2,
  338. std::enable_if_t<! std::is_void<typename geometry::tag<Geometry2>::type>::value, int> = 0
  339. >
  340. inline Policy get_rescale_policy(Geometry1 const& geometry1, Geometry2 const& geometry2)
  341. {
  342. typename geometry::strategies::envelope::services::default_strategy
  343. <
  344. Geometry1,
  345. model::box<typename point_type<Geometry1>::type>
  346. >::type strategy1;
  347. typename geometry::strategies::envelope::services::default_strategy
  348. <
  349. Geometry2,
  350. model::box<typename point_type<Geometry2>::type>
  351. >::type strategy2;
  352. return detail::get_rescale_policy::get_rescale_policy
  353. <
  354. Policy
  355. >::apply(geometry1, geometry2, strategy1, strategy2);
  356. }
  357. template <typename Policy, typename Geometry1, typename Geometry2, typename Strategy>
  358. inline Policy get_rescale_policy(Geometry1 const& geometry1, Geometry2 const& geometry2,
  359. Strategy const& strategy)
  360. {
  361. return detail::get_rescale_policy::get_rescale_policy
  362. <
  363. Policy
  364. >::apply(geometry1, geometry2, strategy, strategy);
  365. }
  366. }} // namespace boost::geometry
  367. #endif // BOOST_GEOMETRY_POLICIES_ROBUSTNESS_GET_RESCALE_POLICY_HPP