get_rescale_policy.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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, 2019.
  7. // Modifications copyright (c) 2015, 2019, 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 <boost/mpl/assert.hpp>
  17. #include <boost/type_traits/is_floating_point.hpp>
  18. #include <boost/type_traits/is_same.hpp>
  19. #include <boost/geometry/core/assert.hpp>
  20. #include <boost/geometry/core/config.hpp>
  21. #include <boost/geometry/core/tag_cast.hpp>
  22. #include <boost/geometry/algorithms/envelope.hpp>
  23. #include <boost/geometry/algorithms/expand.hpp>
  24. #include <boost/geometry/algorithms/is_empty.hpp>
  25. #include <boost/geometry/algorithms/detail/recalculate.hpp>
  26. #include <boost/geometry/algorithms/detail/get_max_size.hpp>
  27. #include <boost/geometry/policies/robustness/robust_type.hpp>
  28. #include <boost/geometry/geometries/point.hpp>
  29. #include <boost/geometry/geometries/box.hpp>
  30. #include <boost/geometry/policies/robustness/no_rescale_policy.hpp>
  31. #include <boost/geometry/policies/robustness/rescale_policy.hpp>
  32. #include <boost/geometry/util/promote_floating_point.hpp>
  33. namespace boost { namespace geometry
  34. {
  35. #ifndef DOXYGEN_NO_DETAIL
  36. namespace detail { namespace get_rescale_policy
  37. {
  38. template
  39. <
  40. typename Box,
  41. typename Point,
  42. typename RobustPoint,
  43. typename Factor
  44. >
  45. inline void scale_box_to_integer_range(Box const& box,
  46. Point& min_point,
  47. RobustPoint& min_robust_point,
  48. Factor& factor)
  49. {
  50. // Scale box to integer-range
  51. typedef typename promote_floating_point
  52. <
  53. typename geometry::coordinate_type<Point>::type
  54. >::type num_type;
  55. num_type const diff = boost::numeric_cast<num_type>(detail::get_max_size(box));
  56. num_type const range = 10000000.0; // Define a large range to get precise integer coordinates
  57. num_type const half = 0.5;
  58. if (math::equals(diff, num_type())
  59. || diff >= range
  60. || ! boost::math::isfinite(diff))
  61. {
  62. factor = 1;
  63. }
  64. else
  65. {
  66. factor = boost::numeric_cast<num_type>(
  67. boost::numeric_cast<boost::long_long_type>(half + range / diff));
  68. BOOST_GEOMETRY_ASSERT(factor >= 1);
  69. }
  70. // Assign input/output minimal points
  71. detail::assign_point_from_index<0>(box, min_point);
  72. num_type const two = 2;
  73. boost::long_long_type const min_coordinate
  74. = boost::numeric_cast<boost::long_long_type>(-range / two);
  75. assign_values(min_robust_point, min_coordinate, min_coordinate);
  76. }
  77. template
  78. <
  79. typename Point, typename RobustPoint, typename Geometry,
  80. typename Factor, typename EnvelopeStrategy
  81. >
  82. static inline void init_rescale_policy(Geometry const& geometry,
  83. Point& min_point,
  84. RobustPoint& min_robust_point,
  85. Factor& factor,
  86. EnvelopeStrategy const& strategy)
  87. {
  88. if (geometry::is_empty(geometry))
  89. {
  90. return;
  91. }
  92. // Get bounding box
  93. model::box<Point> env = geometry::return_envelope
  94. <
  95. model::box<Point>
  96. >(geometry, strategy);
  97. scale_box_to_integer_range(env, min_point, min_robust_point, factor);
  98. }
  99. // NOTE: Actually it should take 2 separate strategies, one for each geometry
  100. // in case one of them was e.g. a Box
  101. template
  102. <
  103. typename Point, typename RobustPoint, typename Geometry1, typename Geometry2,
  104. typename Factor, typename EnvelopeStrategy1, typename EnvelopeStrategy2
  105. >
  106. static inline void init_rescale_policy(Geometry1 const& geometry1,
  107. Geometry2 const& geometry2,
  108. Point& min_point,
  109. RobustPoint& min_robust_point,
  110. Factor& factor,
  111. EnvelopeStrategy1 const& strategy1,
  112. EnvelopeStrategy2 const& strategy2)
  113. {
  114. // Get bounding boxes (when at least one of the geometries is not empty)
  115. bool const is_empty1 = geometry::is_empty(geometry1);
  116. bool const is_empty2 = geometry::is_empty(geometry2);
  117. if (is_empty1 && is_empty2)
  118. {
  119. return;
  120. }
  121. model::box<Point> env;
  122. if (is_empty1)
  123. {
  124. geometry::envelope(geometry2, env, strategy2);
  125. }
  126. else if (is_empty2)
  127. {
  128. geometry::envelope(geometry1, env, strategy1);
  129. }
  130. else
  131. {
  132. // The following approach (envelope + expand) may not give the
  133. // optimal MBR when then two geometries are in the spherical
  134. // equatorial or geographic coordinate systems.
  135. // TODO: implement envelope for two (or possibly more geometries)
  136. geometry::envelope(geometry1, env, strategy1);
  137. model::box<Point> env2 = geometry::return_envelope
  138. <
  139. model::box<Point>
  140. >(geometry2, strategy2);
  141. geometry::expand(env, env2, strategy1.get_box_expand_strategy());
  142. }
  143. scale_box_to_integer_range(env, min_point, min_robust_point, factor);
  144. }
  145. template
  146. <
  147. typename Point,
  148. bool IsFloatingPoint
  149. >
  150. struct rescale_policy_type
  151. {
  152. typedef no_rescale_policy type;
  153. };
  154. // We rescale only all FP types
  155. template
  156. <
  157. typename Point
  158. >
  159. struct rescale_policy_type<Point, true>
  160. {
  161. typedef typename geometry::coordinate_type<Point>::type coordinate_type;
  162. typedef model::point
  163. <
  164. typename detail::robust_type<coordinate_type>::type,
  165. geometry::dimension<Point>::value,
  166. typename geometry::coordinate_system<Point>::type
  167. > robust_point_type;
  168. typedef typename promote_floating_point<coordinate_type>::type factor_type;
  169. typedef detail::robust_policy<Point, robust_point_type, factor_type> type;
  170. };
  171. template <typename Policy>
  172. struct get_rescale_policy
  173. {
  174. template <typename Geometry, typename EnvelopeStrategy>
  175. static inline Policy apply(Geometry const& geometry,
  176. EnvelopeStrategy const& strategy)
  177. {
  178. typedef typename point_type<Geometry>::type point_type;
  179. typedef typename geometry::coordinate_type<Geometry>::type coordinate_type;
  180. typedef typename promote_floating_point<coordinate_type>::type factor_type;
  181. typedef model::point
  182. <
  183. typename detail::robust_type<coordinate_type>::type,
  184. geometry::dimension<point_type>::value,
  185. typename geometry::coordinate_system<point_type>::type
  186. > robust_point_type;
  187. point_type min_point;
  188. robust_point_type min_robust_point;
  189. factor_type factor;
  190. init_rescale_policy(geometry, min_point, min_robust_point,
  191. factor, strategy);
  192. return Policy(min_point, min_robust_point, factor);
  193. }
  194. template <typename Geometry1, typename Geometry2, typename EnvelopeStrategy1, typename EnvelopeStrategy2>
  195. static inline Policy apply(Geometry1 const& geometry1, Geometry2 const& geometry2,
  196. EnvelopeStrategy1 const& strategy1,
  197. EnvelopeStrategy2 const& strategy2)
  198. {
  199. typedef typename point_type<Geometry1>::type point_type;
  200. typedef typename geometry::coordinate_type<Geometry1>::type coordinate_type;
  201. typedef typename promote_floating_point<coordinate_type>::type factor_type;
  202. typedef model::point
  203. <
  204. typename detail::robust_type<coordinate_type>::type,
  205. geometry::dimension<point_type>::value,
  206. typename geometry::coordinate_system<point_type>::type
  207. > robust_point_type;
  208. point_type min_point;
  209. robust_point_type min_robust_point;
  210. factor_type factor;
  211. init_rescale_policy(geometry1, geometry2, min_point, min_robust_point,
  212. factor, strategy1, strategy2);
  213. return Policy(min_point, min_robust_point, factor);
  214. }
  215. };
  216. // Specialization for no-rescaling
  217. template <>
  218. struct get_rescale_policy<no_rescale_policy>
  219. {
  220. template <typename Geometry, typename EnvelopeStrategy>
  221. static inline no_rescale_policy apply(Geometry const& , EnvelopeStrategy const&)
  222. {
  223. return no_rescale_policy();
  224. }
  225. template <typename Geometry1, typename Geometry2, typename EnvelopeStrategy1, typename EnvelopeStrategy2>
  226. static inline no_rescale_policy apply(Geometry1 const& , Geometry2 const& ,
  227. EnvelopeStrategy1 const& , EnvelopeStrategy2 const& )
  228. {
  229. return no_rescale_policy();
  230. }
  231. };
  232. }} // namespace detail::get_rescale_policy
  233. #endif // DOXYGEN_NO_DETAIL
  234. template
  235. <
  236. typename Point,
  237. typename CSTag = typename geometry::cs_tag<Point>::type
  238. >
  239. struct rescale_policy_type
  240. : public detail::get_rescale_policy::rescale_policy_type
  241. <
  242. Point,
  243. #if defined(BOOST_GEOMETRY_USE_RESCALING)
  244. boost::is_floating_point
  245. <
  246. typename geometry::coordinate_type<Point>::type
  247. >::type::value
  248. &&
  249. boost::is_same
  250. <
  251. CSTag,
  252. geometry::cartesian_tag
  253. >::value
  254. #else
  255. false
  256. #endif
  257. >
  258. {
  259. static const bool is_point
  260. = boost::is_same
  261. <
  262. typename geometry::tag<Point>::type,
  263. geometry::point_tag
  264. >::type::value;
  265. BOOST_MPL_ASSERT_MSG((is_point),
  266. INVALID_INPUT_GEOMETRY,
  267. (typename geometry::tag<Point>::type));
  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. #ifndef DOXYGEN_NO_DETAIL
  314. namespace detail { namespace get_rescale_policy
  315. {
  316. // get envelope strategy compatible with relate strategy based on geometry tag
  317. // and strategy cs_tag
  318. template
  319. <
  320. typename Geometry,
  321. typename Strategy,
  322. typename Tag = typename geometry::tag<Geometry>::type,
  323. typename CSTag = typename Strategy::cs_tag
  324. >
  325. struct get_envelope_strategy
  326. {
  327. typedef typename Strategy::envelope_strategy_type type;
  328. static inline type apply(Strategy const& strategy)
  329. {
  330. return strategy.get_envelope_strategy();
  331. }
  332. };
  333. template <typename Geometry, typename Strategy, typename CSTag>
  334. struct get_envelope_strategy<Geometry, Strategy, box_tag, CSTag>
  335. {
  336. typedef typename Strategy::envelope_box_strategy_type type;
  337. static inline type apply(Strategy const& )
  338. {
  339. return type();
  340. }
  341. };
  342. // NOTE: within::xxx_point_point shouldn't have a getter for envelope strategy
  343. // so dispatch by CStag. In the future strategies should probably be redesigned.
  344. template <typename Geometry, typename Strategy>
  345. struct get_envelope_strategy<Geometry, Strategy, point_tag, cartesian_tag>
  346. {
  347. typedef strategy::envelope::cartesian_point type;
  348. static inline type apply(Strategy const& )
  349. {
  350. return type();
  351. }
  352. };
  353. template <typename Geometry, typename Strategy>
  354. struct get_envelope_strategy<Geometry, Strategy, point_tag, spherical_tag>
  355. {
  356. typedef strategy::envelope::spherical_point type;
  357. static inline type apply(Strategy const& )
  358. {
  359. return type();
  360. }
  361. };
  362. template <typename Geometry, typename Strategy>
  363. struct get_envelope_strategy<Geometry, Strategy, multi_point_tag, cartesian_tag>
  364. {
  365. typedef strategy::envelope::cartesian_point type;
  366. static inline type apply(Strategy const& )
  367. {
  368. return type();
  369. }
  370. };
  371. template <typename Geometry, typename Strategy>
  372. struct get_envelope_strategy<Geometry, Strategy, multi_point_tag, spherical_tag>
  373. {
  374. typedef strategy::envelope::spherical_point type;
  375. static inline type apply(Strategy const& )
  376. {
  377. return type();
  378. }
  379. };
  380. // utility for backward-compatibility either treating the argument as geometry
  381. // or envelope strategy for get_rescale_policy
  382. template
  383. <
  384. typename Geometry2OrStrategy,
  385. typename Tag = typename geometry::tag<Geometry2OrStrategy>::type
  386. >
  387. struct get_rescale_policy_geometry_or_strategy
  388. {
  389. template <typename Policy, typename Geometry>
  390. static inline Policy apply(Geometry const& geometry, Geometry2OrStrategy const& geometry2)
  391. {
  392. typename geometry::strategy::envelope::services::default_strategy
  393. <
  394. typename geometry::tag<Geometry>::type,
  395. typename geometry::cs_tag<Geometry>::type
  396. >::type strategy1;
  397. typename geometry::strategy::envelope::services::default_strategy
  398. <
  399. typename geometry::tag<Geometry2OrStrategy>::type,
  400. typename geometry::cs_tag<Geometry2OrStrategy>::type
  401. >::type strategy2;
  402. return detail::get_rescale_policy::get_rescale_policy
  403. <
  404. Policy
  405. >::apply(geometry, geometry2, strategy1, strategy2);
  406. }
  407. };
  408. template <typename Strategy>
  409. struct get_rescale_policy_geometry_or_strategy<Strategy, void>
  410. {
  411. template <typename Policy, typename Geometry>
  412. static inline Policy apply(Geometry const& geometry, Strategy const& strategy)
  413. {
  414. return detail::get_rescale_policy::get_rescale_policy
  415. <
  416. Policy
  417. >::apply(geometry,
  418. get_envelope_strategy
  419. <
  420. Geometry, Strategy
  421. >::apply(strategy));
  422. }
  423. };
  424. }} // namespace detail::get_rescale_policy
  425. #endif // DOXYGEN_NO_DETAIL
  426. template <typename Policy, typename Geometry>
  427. inline Policy get_rescale_policy(Geometry const& geometry)
  428. {
  429. typename geometry::strategy::envelope::services::default_strategy
  430. <
  431. typename geometry::tag<Geometry>::type,
  432. typename geometry::cs_tag<Geometry>::type
  433. >::type strategy;
  434. return detail::get_rescale_policy::get_rescale_policy<Policy>::apply(geometry, strategy);
  435. }
  436. template <typename Policy, typename Geometry, typename Geometry2OrStrategy>
  437. inline Policy get_rescale_policy(Geometry const& geometry, Geometry2OrStrategy const& geometry2_or_strategy)
  438. {
  439. // if the second argument is a geometry use default strategy
  440. // otherwise assume it's envelope strategy for the first argument
  441. return detail::get_rescale_policy::get_rescale_policy_geometry_or_strategy
  442. <
  443. Geometry2OrStrategy
  444. > ::template apply<Policy, Geometry>(geometry, geometry2_or_strategy);
  445. }
  446. template <typename Policy, typename Geometry1, typename Geometry2, typename IntersectionStrategy>
  447. inline Policy get_rescale_policy(Geometry1 const& geometry1, Geometry2 const& geometry2,
  448. IntersectionStrategy const& strategy)
  449. {
  450. return detail::get_rescale_policy::get_rescale_policy
  451. <
  452. Policy
  453. >::apply(geometry1, geometry2,
  454. detail::get_rescale_policy::get_envelope_strategy
  455. <
  456. Geometry1, IntersectionStrategy
  457. >::apply(strategy),
  458. detail::get_rescale_policy::get_envelope_strategy
  459. <
  460. Geometry2, IntersectionStrategy
  461. >::apply(strategy));
  462. }
  463. }} // namespace boost::geometry
  464. #endif // BOOST_GEOMETRY_POLICIES_ROBUSTNESS_GET_RESCALE_POLICY_HPP