1 // Boost.Geometry Index
2 //
3 // boxes union/intersection area/volume
4 //
5 // Copyright (c) 2011-2018 Adam Wulkiewicz, Lodz, Poland.
6 //
7 // This file was modified by Oracle on 2019.
8 // Modifications copyright (c) 2019 Oracle and/or its affiliates.
9 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
10 //
11 // Use, modification and distribution is subject to the Boost Software License,
12 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
13 // http://www.boost.org/LICENSE_1_0.txt)
14 
15 #ifndef BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_INTERSECTION_CONTENT_HPP
16 #define BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_INTERSECTION_CONTENT_HPP
17 
18 #include <boost/geometry/algorithms/detail/disjoint/box_box.hpp>
19 #include <boost/geometry/algorithms/detail/overlay/intersection_box_box.hpp>
20 
21 #include <boost/geometry/index/detail/algorithms/content.hpp>
22 
23 namespace boost { namespace geometry { namespace index { namespace detail {
24 
25 // Util to distinguish between default and non-default index strategy
26 template <typename Box, typename Strategy>
disjoint_box_box(Box const & box1,Box const & box2,Strategy const &)27 inline bool disjoint_box_box(Box const& box1, Box const& box2, Strategy const&)
28 {
29     return geometry::detail::disjoint::disjoint_box_box(box1, box2,
30                 typename Strategy::disjoint_box_box_strategy_type());
31 }
32 
33 template <typename Box>
disjoint_box_box(Box const & box1,Box const & box2,default_strategy const &)34 inline bool disjoint_box_box(Box const& box1, Box const& box2, default_strategy const& )
35 {
36     typedef typename strategy::disjoint::services::default_strategy<Box, Box>::type strategy_type;
37     return geometry::detail::disjoint::disjoint_box_box(box1, box2, strategy_type());
38 }
39 
40 /**
41  * \brief Compute the area, volume, ... of the intersection of b1 and b2
42  */
43 template <typename Box, typename Strategy>
intersection_content(Box const & box1,Box const & box2,Strategy const & strategy)44 inline typename default_content_result<Box>::type intersection_content(Box const& box1, Box const& box2, Strategy const& strategy)
45 {
46     bool const intersects = ! index::detail::disjoint_box_box(box1, box2, strategy);
47 
48     // NOTE: the code below may be inconsistent with the disjoint_box_box()
49     // however intersection_box_box checks if the boxes intersect on the fly so it should be ok
50     // but this also means that disjoint_box_box() is probably not needed
51 
52     if ( intersects )
53     {
54         Box box_intersection;
55         bool const ok = geometry::detail::intersection::intersection_box_box
56                             <
57                                 0, geometry::dimension<Box>::value
58                             >::apply(box1, box2, 0, box_intersection, 0);
59         if ( ok )
60         {
61             return index::detail::content(box_intersection);
62         }
63     }
64     return 0;
65 }
66 
67 template <typename Box>
intersection_content(Box const & box1,Box const & box2)68 inline typename default_content_result<Box>::type intersection_content(Box const& box1, Box const& box2)
69 {
70     return intersection_content(box1, box2, default_strategy());
71 }
72 
73 }}}} // namespace boost::geometry::index::detail
74 
75 #endif // BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_INTERSECTION_CONTENT_HPP
76