intersection_content.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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-2021.
  8. // Modifications copyright (c) 2019-2021 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. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_INTERSECTION_CONTENT_HPP
  15. #define BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_INTERSECTION_CONTENT_HPP
  16. #include <boost/geometry/algorithms/detail/disjoint/box_box.hpp>
  17. #include <boost/geometry/algorithms/detail/intersection/box_box_implementation.hpp>
  18. #include <boost/geometry/index/detail/algorithms/content.hpp>
  19. #include <boost/geometry/strategies/default_strategy.hpp>
  20. #include <boost/geometry/strategies/disjoint.hpp>
  21. namespace boost { namespace geometry { namespace index { namespace detail {
  22. // Util to distinguish between default and non-default index strategy
  23. template <typename Box, typename Strategy>
  24. inline bool disjoint_box_box(Box const& box1, Box const& box2, Strategy const& s)
  25. {
  26. return geometry::detail::disjoint::disjoint_box_box(box1, box2, s);
  27. }
  28. template <typename Box>
  29. inline bool disjoint_box_box(Box const& box1, Box const& box2, default_strategy const& )
  30. {
  31. typedef typename strategy::disjoint::services::default_strategy<Box, Box>::type strategy_type;
  32. return geometry::detail::disjoint::disjoint_box_box(box1, box2, strategy_type());
  33. }
  34. /**
  35. * \brief Compute the area, volume, ... of the intersection of b1 and b2
  36. */
  37. template <typename Box, typename Strategy>
  38. inline typename default_content_result<Box>::type intersection_content(Box const& box1, Box const& box2, Strategy const& strategy)
  39. {
  40. bool const intersects = ! index::detail::disjoint_box_box(box1, box2, strategy);
  41. // NOTE: the code below may be inconsistent with the disjoint_box_box()
  42. // however intersection_box_box checks if the boxes intersect on the fly so it should be ok
  43. // but this also means that disjoint_box_box() is probably not needed
  44. if ( intersects )
  45. {
  46. Box box_intersection;
  47. bool const ok = geometry::detail::intersection::intersection_box_box
  48. <
  49. 0, geometry::dimension<Box>::value
  50. >::apply(box1, box2, 0, box_intersection, 0);
  51. if ( ok )
  52. {
  53. return index::detail::content(box_intersection);
  54. }
  55. }
  56. return 0;
  57. }
  58. template <typename Box>
  59. inline typename default_content_result<Box>::type intersection_content(Box const& box1, Box const& box2)
  60. {
  61. return intersection_content(box1, box2, default_strategy());
  62. }
  63. }}}} // namespace boost::geometry::index::detail
  64. #endif // BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_INTERSECTION_CONTENT_HPP