1 // Boost.Geometry
2 
3 // Copyright (c) 2015-2020, Oracle and/or its affiliates.
4 
5 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
6 
7 // Distributed under the Boost Software License, Version 1.0.
8 // (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 
11 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_EXPAND_EXPAND_BY_EPSILON_HPP
12 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_EXPAND_EXPAND_BY_EPSILON_HPP
13 
14 #include <algorithm>
15 #include <cstddef>
16 #include <type_traits>
17 
18 #include <boost/geometry/core/access.hpp>
19 #include <boost/geometry/core/coordinate_dimension.hpp>
20 #include <boost/geometry/core/coordinate_type.hpp>
21 
22 #include <boost/geometry/util/math.hpp>
23 
24 #include <boost/geometry/views/detail/indexed_point_view.hpp>
25 
26 namespace boost { namespace geometry
27 {
28 
29 #ifndef DOXYGEN_NO_DETAIL
30 namespace detail { namespace expand
31 {
32 
33 template
34 <
35     typename Point,
36     template <typename> class PlusOrMinus,
37     std::size_t I = 0,
38     std::size_t D = dimension<Point>::value
39 >
40 struct corner_by_epsilon
41 {
applyboost::geometry::detail::expand::corner_by_epsilon42     static inline void apply(Point & point)
43     {
44         typedef typename coordinate_type<Point>::type coord_type;
45         coord_type const coord = get<I>(point);
46         coord_type const seps = math::scaled_epsilon(coord);
47 
48         set<I>(point, PlusOrMinus<coord_type>()(coord, seps));
49 
50         corner_by_epsilon<Point, PlusOrMinus, I+1>::apply(point);
51     }
52 
applyboost::geometry::detail::expand::corner_by_epsilon53     static inline void apply(Point & point,
54                              typename coordinate_type<Point>::type const& eps)
55     {
56         typedef typename coordinate_type<Point>::type coord_type;
57         coord_type const coord = get<I>(point);
58         coord_type const seps = math::scaled_epsilon(coord, eps);
59 
60         set<I>(point, PlusOrMinus<coord_type>()(coord, seps));
61 
62         corner_by_epsilon<Point, PlusOrMinus, I + 1>::apply(point);
63     }
64 };
65 
66 template
67 <
68     typename Point,
69     template <typename> class PlusOrMinus,
70     std::size_t D
71 >
72 struct corner_by_epsilon<Point, PlusOrMinus, D, D>
73 {
applyboost::geometry::detail::expand::corner_by_epsilon74     static inline void apply(Point const&) {}
applyboost::geometry::detail::expand::corner_by_epsilon75     static inline void apply(Point const&, typename coordinate_type<Point>::type const&) {}
76 };
77 
78 template
79 <
80     typename Box,
81     bool Enable = ! std::is_integral<typename coordinate_type<Box>::type>::value
82 >
83 struct expand_by_epsilon
84 {
applyboost::geometry::detail::expand::expand_by_epsilon85     static inline void apply(Box & box)
86     {
87         typedef detail::indexed_point_view<Box, min_corner> min_type;
88         min_type min_point(box);
89         corner_by_epsilon<min_type, std::minus>::apply(min_point);
90 
91         typedef detail::indexed_point_view<Box, max_corner> max_type;
92         max_type max_point(box);
93         corner_by_epsilon<max_type, std::plus>::apply(max_point);
94     }
95 
applyboost::geometry::detail::expand::expand_by_epsilon96     static inline void apply(Box & box,
97                              typename coordinate_type<Box>::type const& eps)
98     {
99         typedef detail::indexed_point_view<Box, min_corner> min_type;
100         min_type min_point(box);
101         corner_by_epsilon<min_type, std::minus>::apply(min_point, eps);
102 
103         typedef detail::indexed_point_view<Box, max_corner> max_type;
104         max_type max_point(box);
105         corner_by_epsilon<max_type, std::plus>::apply(max_point, eps);
106     }
107 };
108 
109 template <typename Box>
110 struct expand_by_epsilon<Box, false>
111 {
applyboost::geometry::detail::expand::expand_by_epsilon112     static inline void apply(Box &) {}
applyboost::geometry::detail::expand::expand_by_epsilon113     static inline void apply(Box &, typename coordinate_type<Box>::type const&) {}
114 };
115 
116 } // namespace expand
117 
118 template <typename Box>
expand_by_epsilon(Box & box)119 inline void expand_by_epsilon(Box & box)
120 {
121     expand::expand_by_epsilon<Box>::apply(box);
122 }
123 
124 template <typename Box>
expand_by_epsilon(Box & box,typename coordinate_type<Box>::type const & eps)125 inline void expand_by_epsilon(Box & box,
126                               typename coordinate_type<Box>::type const& eps)
127 {
128     expand::expand_by_epsilon<Box>::apply(box, eps);
129 }
130 
131 } // namespace detail
132 #endif // DOXYGEN_NO_DETAIL
133 
134 }} // namespace boost::geometry
135 
136 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_EXPAND_EXPAND_BY_EPSILON_HPP
137