Imported Upstream version 1.64.0
[platform/upstream/boost.git] / boost / geometry / algorithms / detail / disjoint / box_box.hpp
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2
3 // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2015 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2015 Mateusz Loskot, London, UK.
6 // Copyright (c) 2013-2015 Adam Wulkiewicz, Lodz, Poland.
7
8 // This file was modified by Oracle on 2013-2017.
9 // Modifications copyright (c) 2013-2017, Oracle and/or its affiliates.
10
11 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
12 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
13
14 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
15 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
16
17 // Use, modification and distribution is subject to the Boost Software License,
18 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
19 // http://www.boost.org/LICENSE_1_0.txt)
20
21 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISJOINT_BOX_BOX_HPP
22 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISJOINT_BOX_BOX_HPP
23
24 #include <cstddef>
25
26 #include <boost/geometry/core/access.hpp>
27 #include <boost/geometry/core/tags.hpp>
28
29 #include <boost/geometry/algorithms/dispatch/disjoint.hpp>
30
31 #include <boost/geometry/util/normalize_spheroidal_coordinates.hpp>
32 #include <boost/geometry/util/select_most_precise.hpp>
33
34
35 namespace boost { namespace geometry
36 {
37
38 #ifndef DOXYGEN_NO_DETAIL
39 namespace detail { namespace disjoint
40 {
41
42 template
43 <
44     typename Box1, typename Box2,
45     std::size_t Dimension = 0,
46     std::size_t DimensionCount = dimension<Box1>::value,
47     typename CSTag = typename tag_cast
48                         <
49                             typename cs_tag<Box1>::type,
50                             spherical_tag
51                         >::type
52 >
53 struct box_box
54 {
55     template <typename Strategy>
56     static inline bool apply(Box1 const& box1, Box2 const& box2, Strategy const&)
57     {
58         return apply(box1, box2);
59     }
60
61     static inline bool apply(Box1 const& box1, Box2 const& box2)
62     {
63         if (get<max_corner, Dimension>(box1) < get<min_corner, Dimension>(box2))
64         {
65             return true;
66         }
67         if (get<min_corner, Dimension>(box1) > get<max_corner, Dimension>(box2))
68         {
69             return true;
70         }
71         return box_box
72             <
73                 Box1, Box2,
74                 Dimension + 1, DimensionCount
75             >::apply(box1, box2);
76     }
77 };
78
79
80 template <typename Box1, typename Box2, std::size_t DimensionCount, typename CSTag>
81 struct box_box<Box1, Box2, DimensionCount, DimensionCount, CSTag>
82 {
83     static inline bool apply(Box1 const& , Box2 const& )
84     {
85         return false;
86     }
87 };
88
89
90 template <typename Box1, typename Box2, std::size_t DimensionCount>
91 struct box_box<Box1, Box2, 0, DimensionCount, spherical_tag>
92 {
93     template <typename Strategy>
94     static inline bool apply(Box1 const& box1, Box2 const& box2, Strategy const&)
95     {
96         return apply(box1, box2);
97     }
98
99     static inline bool apply(Box1 const& box1, Box2 const& box2)
100     {
101         typedef typename geometry::select_most_precise
102             <
103                 typename coordinate_type<Box1>::type,
104                 typename coordinate_type<Box2>::type
105             >::type calc_t;
106         typedef typename coordinate_system<Box1>::type::units units_t;
107         typedef math::detail::constants_on_spheroid<calc_t, units_t> constants;
108
109         calc_t const b1_min = get<min_corner, 0>(box1);
110         calc_t const b1_max = get<max_corner, 0>(box1);
111         calc_t const b2_min = get<min_corner, 0>(box2);
112         calc_t const b2_max = get<max_corner, 0>(box2);
113
114         // min <= max <=> diff >= 0
115         calc_t const diff1 = b1_max - b1_min;
116         calc_t const diff2 = b2_max - b2_min;
117
118         // check the intersection if neither box cover the whole globe
119         if (diff1 < constants::period() && diff2 < constants::period())
120         {
121             // calculate positive longitude translation with b1_min as origin
122             calc_t const diff_min = math::longitude_distance_unsigned<units_t>(b1_min, b2_min);
123             calc_t const b2_min_transl = b1_min + diff_min; // always right of b1_min
124
125             if (b2_min_transl > b1_max                                // b2_min right of b1_max
126              && b2_min_transl - constants::period() + diff2 < b1_min) // b2_max left of b1_min
127             {
128                 return true;
129             }
130         }
131
132         return box_box
133             <
134                 Box1, Box2,
135                 1, DimensionCount
136             >::apply(box1, box2);
137     }
138 };
139
140
141 /*!
142     \brief Internal utility function to detect if boxes are disjoint
143     \note Is used from other algorithms, declared separately
144         to avoid circular references
145  */
146 template <typename Box1, typename Box2>
147 inline bool disjoint_box_box(Box1 const& box1, Box2 const& box2)
148 {
149     return box_box<Box1, Box2>::apply(box1, box2);
150 }
151
152
153 }} // namespace detail::disjoint
154 #endif // DOXYGEN_NO_DETAIL
155
156
157 #ifndef DOXYGEN_NO_DISPATCH
158 namespace dispatch
159 {
160
161
162 template <typename Box1, typename Box2, std::size_t DimensionCount>
163 struct disjoint<Box1, Box2, DimensionCount, box_tag, box_tag, false>
164     : detail::disjoint::box_box<Box1, Box2, 0, DimensionCount>
165 {};
166
167
168 } // namespace dispatch
169 #endif // DOXYGEN_NO_DISPATCH
170
171
172 }} // namespace boost::geometry
173
174
175 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISJOINT_BOX_BOX_HPP