1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2014-2015 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2014-2015 Bruno Lalande, Paris, France.
5 // Copyright (c) 2014-2015 Mateusz Loskot, London, UK.
6 // Copyright (c) 2014-2015 Adam Wulkiewicz, Lodz, Poland.
8 // This file was modified by Oracle on 2015.
9 // Modifications copyright (c) 2015, Oracle and/or its affiliates.
11 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
13 // Use, modification and distribution is subject to the Boost Software License,
14 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
15 // http://www.boost.org/LICENSE_1_0.txt)
17 #ifndef BOOST_GEOMETRY_POLICIES_ROBUSTNESS_GET_RESCALE_POLICY_HPP
18 #define BOOST_GEOMETRY_POLICIES_ROBUSTNESS_GET_RESCALE_POLICY_HPP
23 #include <boost/mpl/assert.hpp>
24 #include <boost/type_traits/is_floating_point.hpp>
25 #include <boost/type_traits/is_same.hpp>
27 #include <boost/geometry/core/assert.hpp>
28 #include <boost/geometry/core/tag_cast.hpp>
30 #include <boost/geometry/algorithms/envelope.hpp>
31 #include <boost/geometry/algorithms/expand.hpp>
32 #include <boost/geometry/algorithms/is_empty.hpp>
33 #include <boost/geometry/algorithms/detail/recalculate.hpp>
34 #include <boost/geometry/algorithms/detail/get_max_size.hpp>
35 #include <boost/geometry/policies/robustness/robust_type.hpp>
37 #include <boost/geometry/geometries/point.hpp>
38 #include <boost/geometry/geometries/box.hpp>
40 #include <boost/geometry/policies/robustness/no_rescale_policy.hpp>
41 #include <boost/geometry/policies/robustness/rescale_policy.hpp>
43 #include <boost/geometry/util/promote_floating_point.hpp>
45 namespace boost { namespace geometry
48 #ifndef DOXYGEN_NO_DETAIL
49 namespace detail { namespace get_rescale_policy
59 inline void scale_box_to_integer_range(Box const& box,
61 RobustPoint& min_robust_point,
64 // Scale box to integer-range
65 typedef typename promote_floating_point
67 typename geometry::coordinate_type<Point>::type
69 num_type const diff = boost::numeric_cast<num_type>(detail::get_max_size(box));
70 num_type const range = 10000000.0; // Define a large range to get precise integer coordinates
71 num_type const half = 0.5;
72 if (math::equals(diff, num_type())
74 || ! boost::math::isfinite(diff))
80 factor = boost::numeric_cast<num_type>(
81 boost::numeric_cast<boost::long_long_type>(half + range / diff));
82 BOOST_GEOMETRY_ASSERT(factor >= 1);
85 // Assign input/output minimal points
86 detail::assign_point_from_index<0>(box, min_point);
87 num_type const two = 2;
88 boost::long_long_type const min_coordinate
89 = boost::numeric_cast<boost::long_long_type>(-range / two);
90 assign_values(min_robust_point, min_coordinate, min_coordinate);
93 template <typename Point, typename RobustPoint, typename Geometry, typename Factor>
94 static inline void init_rescale_policy(Geometry const& geometry,
96 RobustPoint& min_robust_point,
99 if (geometry::is_empty(geometry))
104 // Get bounding boxes
105 model::box<Point> env = geometry::return_envelope<model::box<Point> >(geometry);
107 scale_box_to_integer_range(env, min_point, min_robust_point, factor);
110 template <typename Point, typename RobustPoint, typename Geometry1, typename Geometry2, typename Factor>
111 static inline void init_rescale_policy(Geometry1 const& geometry1,
112 Geometry2 const& geometry2,
114 RobustPoint& min_robust_point,
117 // Get bounding boxes (when at least one of the geometries is not empty)
118 bool const is_empty1 = geometry::is_empty(geometry1);
119 bool const is_empty2 = geometry::is_empty(geometry2);
120 if (is_empty1 && is_empty2)
125 model::box<Point> env;
128 geometry::envelope(geometry2, env);
132 geometry::envelope(geometry1, env);
136 // The following approach (envelope + expand) may not give the
137 // optimal MBR when then two geometries are in the spherical
138 // equatorial or geographic coordinate systems.
139 // TODO: implement envelope for two (or possibly more geometries)
140 geometry::envelope(geometry1, env);
141 model::box<Point> env2 = geometry::return_envelope
145 geometry::expand(env, env2);
148 scale_box_to_integer_range(env, min_point, min_robust_point, factor);
157 struct rescale_policy_type
159 typedef no_rescale_policy type;
162 // We rescale only all FP types
167 struct rescale_policy_type<Point, true>
169 typedef typename geometry::coordinate_type<Point>::type coordinate_type;
172 typename detail::robust_type<coordinate_type>::type,
173 geometry::dimension<Point>::value,
174 typename geometry::coordinate_system<Point>::type
176 typedef typename promote_floating_point<coordinate_type>::type factor_type;
177 typedef detail::robust_policy<Point, robust_point_type, factor_type> type;
180 template <typename Policy>
181 struct get_rescale_policy
183 template <typename Geometry>
184 static inline Policy apply(Geometry const& geometry)
186 typedef typename point_type<Geometry>::type point_type;
187 typedef typename geometry::coordinate_type<Geometry>::type coordinate_type;
188 typedef typename promote_floating_point<coordinate_type>::type factor_type;
191 typename detail::robust_type<coordinate_type>::type,
192 geometry::dimension<point_type>::value,
193 typename geometry::coordinate_system<point_type>::type
196 point_type min_point;
197 robust_point_type min_robust_point;
199 init_rescale_policy(geometry, min_point, min_robust_point, factor);
201 return Policy(min_point, min_robust_point, factor);
204 template <typename Geometry1, typename Geometry2>
205 static inline Policy apply(Geometry1 const& geometry1, Geometry2 const& geometry2)
207 typedef typename point_type<Geometry1>::type point_type;
208 typedef typename geometry::coordinate_type<Geometry1>::type coordinate_type;
209 typedef typename promote_floating_point<coordinate_type>::type factor_type;
212 typename detail::robust_type<coordinate_type>::type,
213 geometry::dimension<point_type>::value,
214 typename geometry::coordinate_system<point_type>::type
217 point_type min_point;
218 robust_point_type min_robust_point;
220 init_rescale_policy(geometry1, geometry2, min_point, min_robust_point, factor);
222 return Policy(min_point, min_robust_point, factor);
226 // Specialization for no-rescaling
228 struct get_rescale_policy<no_rescale_policy>
230 template <typename Geometry>
231 static inline no_rescale_policy apply(Geometry const& )
233 return no_rescale_policy();
236 template <typename Geometry1, typename Geometry2>
237 static inline no_rescale_policy apply(Geometry1 const& , Geometry2 const& )
239 return no_rescale_policy();
244 }} // namespace detail::get_rescale_policy
245 #endif // DOXYGEN_NO_DETAIL
247 template<typename Point>
248 struct rescale_policy_type
249 : public detail::get_rescale_policy::rescale_policy_type
252 #if defined(BOOST_GEOMETRY_NO_ROBUSTNESS)
255 boost::is_floating_point
257 typename geometry::coordinate_type<Point>::type
262 typename geometry::coordinate_system<Point>::type,
263 geometry::cs::cartesian
268 static const bool is_point
271 typename geometry::tag<Point>::type,
275 BOOST_MPL_ASSERT_MSG((is_point),
276 INVALID_INPUT_GEOMETRY,
277 (typename geometry::tag<Point>::type));
285 typename Tag1 = typename tag_cast
287 typename tag<Geometry1>::type,
293 typename Tag2 = typename tag_cast
295 typename tag<Geometry2>::type,
302 struct rescale_overlay_policy_type
303 // Default: no rescaling
304 : public detail::get_rescale_policy::rescale_policy_type
306 typename geometry::point_type<Geometry1>::type,
311 // Areal/areal: get rescale policy based on coordinate type
317 struct rescale_overlay_policy_type<Geometry1, Geometry2, areal_tag, areal_tag>
318 : public rescale_policy_type
320 typename geometry::point_type<Geometry1>::type
325 template <typename Policy, typename Geometry>
326 inline Policy get_rescale_policy(Geometry const& geometry)
328 return detail::get_rescale_policy::get_rescale_policy<Policy>::apply(geometry);
331 template <typename Policy, typename Geometry1, typename Geometry2>
332 inline Policy get_rescale_policy(Geometry1 const& geometry1, Geometry2 const& geometry2)
334 return detail::get_rescale_policy::get_rescale_policy<Policy>::apply(geometry1, geometry2);
338 }} // namespace boost::geometry
341 #endif // BOOST_GEOMETRY_POLICIES_ROBUSTNESS_GET_RESCALE_POLICY_HPP