1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 
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.
7 
8 // This file was modified by Oracle on 2015.
9 // Modifications copyright (c) 2015, Oracle and/or its affiliates.
10 
11 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
12 
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)
16 
17 #ifndef BOOST_GEOMETRY_POLICIES_ROBUSTNESS_GET_RESCALE_POLICY_HPP
18 #define BOOST_GEOMETRY_POLICIES_ROBUSTNESS_GET_RESCALE_POLICY_HPP
19 
20 
21 #include <cstddef>
22 
23 #include <boost/mpl/assert.hpp>
24 #include <boost/type_traits/is_floating_point.hpp>
25 #include <boost/type_traits/is_same.hpp>
26 
27 #include <boost/geometry/core/assert.hpp>
28 #include <boost/geometry/core/tag_cast.hpp>
29 
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>
36 
37 #include <boost/geometry/geometries/point.hpp>
38 #include <boost/geometry/geometries/box.hpp>
39 
40 #include <boost/geometry/policies/robustness/no_rescale_policy.hpp>
41 #include <boost/geometry/policies/robustness/rescale_policy.hpp>
42 
43 #include <boost/geometry/util/promote_floating_point.hpp>
44 
45 namespace boost { namespace geometry
46 {
47 
48 #ifndef DOXYGEN_NO_DETAIL
49 namespace detail { namespace get_rescale_policy
50 {
51 
52 template
53 <
54     typename Box,
55     typename Point,
56     typename RobustPoint,
57     typename Factor
58 >
scale_box_to_integer_range(Box const & box,Point & min_point,RobustPoint & min_robust_point,Factor & factor)59 inline void scale_box_to_integer_range(Box const& box,
60                                        Point& min_point,
61                                        RobustPoint& min_robust_point,
62                                        Factor& factor)
63 {
64     // Scale box to integer-range
65     typedef typename promote_floating_point
66         <
67             typename geometry::coordinate_type<Point>::type
68         >::type num_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())
73         || diff >= range
74         || ! boost::math::isfinite(diff))
75     {
76         factor = 1;
77     }
78     else
79     {
80         factor = boost::numeric_cast<num_type>(
81             boost::numeric_cast<boost::long_long_type>(half + range / diff));
82         BOOST_GEOMETRY_ASSERT(factor >= 1);
83     }
84 
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);
91 }
92 
93 template <typename Point, typename RobustPoint, typename Geometry, typename Factor>
init_rescale_policy(Geometry const & geometry,Point & min_point,RobustPoint & min_robust_point,Factor & factor)94 static inline void init_rescale_policy(Geometry const& geometry,
95         Point& min_point,
96         RobustPoint& min_robust_point,
97         Factor& factor)
98 {
99     if (geometry::is_empty(geometry))
100     {
101         return;
102     }
103 
104     // Get bounding boxes
105     model::box<Point> env = geometry::return_envelope<model::box<Point> >(geometry);
106 
107     scale_box_to_integer_range(env, min_point, min_robust_point, factor);
108 }
109 
110 template <typename Point, typename RobustPoint, typename Geometry1, typename Geometry2, typename Factor>
init_rescale_policy(Geometry1 const & geometry1,Geometry2 const & geometry2,Point & min_point,RobustPoint & min_robust_point,Factor & factor)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 {
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)
121     {
122         return;
123     }
124 
125     model::box<Point> env;
126     if (is_empty1)
127     {
128         geometry::envelope(geometry2, env);
129     }
130     else if (is_empty2)
131     {
132         geometry::envelope(geometry1, env);
133     }
134     else
135     {
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
142             <
143                 model::box<Point>
144             >(geometry2);
145         geometry::expand(env, env2);
146     }
147 
148     scale_box_to_integer_range(env, min_point, min_robust_point, factor);
149 }
150 
151 
152 template
153 <
154     typename Point,
155     bool IsFloatingPoint
156 >
157 struct rescale_policy_type
158 {
159     typedef no_rescale_policy type;
160 };
161 
162 // We rescale only all FP types
163 template
164 <
165     typename Point
166 >
167 struct rescale_policy_type<Point, true>
168 {
169     typedef typename geometry::coordinate_type<Point>::type coordinate_type;
170     typedef model::point
171     <
172         typename detail::robust_type<coordinate_type>::type,
173         geometry::dimension<Point>::value,
174         typename geometry::coordinate_system<Point>::type
175     > robust_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;
178 };
179 
180 template <typename Policy>
181 struct get_rescale_policy
182 {
183     template <typename Geometry>
applyboost::geometry::detail::get_rescale_policy::get_rescale_policy184     static inline Policy apply(Geometry const& geometry)
185     {
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;
189         typedef model::point
190         <
191             typename detail::robust_type<coordinate_type>::type,
192             geometry::dimension<point_type>::value,
193             typename geometry::coordinate_system<point_type>::type
194         > robust_point_type;
195 
196         point_type min_point;
197         robust_point_type min_robust_point;
198         factor_type factor;
199         init_rescale_policy(geometry, min_point, min_robust_point, factor);
200 
201         return Policy(min_point, min_robust_point, factor);
202     }
203 
204     template <typename Geometry1, typename Geometry2>
applyboost::geometry::detail::get_rescale_policy::get_rescale_policy205     static inline Policy apply(Geometry1 const& geometry1, Geometry2 const& geometry2)
206     {
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;
210         typedef model::point
211         <
212             typename detail::robust_type<coordinate_type>::type,
213             geometry::dimension<point_type>::value,
214             typename geometry::coordinate_system<point_type>::type
215         > robust_point_type;
216 
217         point_type min_point;
218         robust_point_type min_robust_point;
219         factor_type factor;
220         init_rescale_policy(geometry1, geometry2, min_point, min_robust_point, factor);
221 
222         return Policy(min_point, min_robust_point, factor);
223     }
224 };
225 
226 // Specialization for no-rescaling
227 template <>
228 struct get_rescale_policy<no_rescale_policy>
229 {
230     template <typename Geometry>
applyboost::geometry::detail::get_rescale_policy::get_rescale_policy231     static inline no_rescale_policy apply(Geometry const& )
232     {
233         return no_rescale_policy();
234     }
235 
236     template <typename Geometry1, typename Geometry2>
applyboost::geometry::detail::get_rescale_policy::get_rescale_policy237     static inline no_rescale_policy apply(Geometry1 const& , Geometry2 const& )
238     {
239         return no_rescale_policy();
240     }
241 };
242 
243 
244 }} // namespace detail::get_rescale_policy
245 #endif // DOXYGEN_NO_DETAIL
246 
247 template<typename Point>
248 struct rescale_policy_type
249     : public detail::get_rescale_policy::rescale_policy_type
250     <
251         Point,
252 #if defined(BOOST_GEOMETRY_NO_ROBUSTNESS)
253         false
254 #else
255         boost::is_floating_point
256             <
257                 typename geometry::coordinate_type<Point>::type
258             >::type::value
259         &&
260         boost::is_same
261             <
262                 typename geometry::coordinate_system<Point>::type,
263                 geometry::cs::cartesian
264             >::value
265 #endif
266     >
267 {
268     static const bool is_point
269         = boost::is_same
270             <
271                 typename geometry::tag<Point>::type,
272                 geometry::point_tag
273             >::type::value;
274 
275     BOOST_MPL_ASSERT_MSG((is_point),
276                          INVALID_INPUT_GEOMETRY,
277                          (typename geometry::tag<Point>::type));
278 };
279 
280 
281 template
282 <
283     typename Geometry1,
284     typename Geometry2,
285     typename Tag1 = typename tag_cast
286     <
287         typename tag<Geometry1>::type,
288         box_tag,
289         pointlike_tag,
290         linear_tag,
291         areal_tag
292     >::type,
293     typename Tag2 = typename tag_cast
294     <
295         typename tag<Geometry2>::type,
296         box_tag,
297         pointlike_tag,
298         linear_tag,
299         areal_tag
300     >::type
301 >
302 struct rescale_overlay_policy_type
303     // Default: no rescaling
304     : public detail::get_rescale_policy::rescale_policy_type
305         <
306             typename geometry::point_type<Geometry1>::type,
307             false
308         >
309 {};
310 
311 // Areal/areal: get rescale policy based on coordinate type
312 template
313 <
314     typename Geometry1,
315     typename Geometry2
316 >
317 struct rescale_overlay_policy_type<Geometry1, Geometry2, areal_tag, areal_tag>
318     : public rescale_policy_type
319         <
320             typename geometry::point_type<Geometry1>::type
321         >
322 {};
323 
324 
325 template <typename Policy, typename Geometry>
get_rescale_policy(Geometry const & geometry)326 inline Policy get_rescale_policy(Geometry const& geometry)
327 {
328     return detail::get_rescale_policy::get_rescale_policy<Policy>::apply(geometry);
329 }
330 
331 template <typename Policy, typename Geometry1, typename Geometry2>
get_rescale_policy(Geometry1 const & geometry1,Geometry2 const & geometry2)332 inline Policy get_rescale_policy(Geometry1 const& geometry1, Geometry2 const& geometry2)
333 {
334     return detail::get_rescale_policy::get_rescale_policy<Policy>::apply(geometry1, geometry2);
335 }
336 
337 
338 }} // namespace boost::geometry
339 
340 
341 #endif // BOOST_GEOMETRY_POLICIES_ROBUSTNESS_GET_RESCALE_POLICY_HPP
342