are_boxes_ok.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Boost.Geometry Index
  2. //
  3. // R-tree boxes validating visitor implementation
  4. //
  5. // Copyright (c) 2011-2015 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. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_RTREE_UTILITIES_ARE_BOXES_OK_HPP
  15. #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_UTILITIES_ARE_BOXES_OK_HPP
  16. #include <boost/geometry/algorithms/equals.hpp>
  17. #include <boost/geometry/index/detail/rtree/node/node.hpp>
  18. namespace boost { namespace geometry { namespace index { namespace detail { namespace rtree { namespace utilities {
  19. namespace visitors {
  20. template <typename Value, typename Options, typename Translator, typename Box, typename Allocators>
  21. class are_boxes_ok
  22. : public rtree::visitor<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag, true>::type
  23. {
  24. typedef typename Options::parameters_type parameters_type;
  25. typedef typename rtree::internal_node<Value, parameters_type, Box, Allocators, typename Options::node_tag>::type internal_node;
  26. typedef typename rtree::leaf<Value, parameters_type, Box, Allocators, typename Options::node_tag>::type leaf;
  27. public:
  28. are_boxes_ok(parameters_type const& parameters, Translator const& tr, bool exact_match)
  29. : result(false), m_parameters(parameters), m_tr(tr), m_is_root(true), m_exact_match(exact_match)
  30. {}
  31. void operator()(internal_node const& n)
  32. {
  33. typedef typename rtree::elements_type<internal_node>::type elements_type;
  34. elements_type const& elements = rtree::elements(n);
  35. if (elements.empty())
  36. {
  37. result = false;
  38. return;
  39. }
  40. Box box_bckup = m_box;
  41. bool is_root_bckup = m_is_root;
  42. m_is_root = false;
  43. for ( typename elements_type::const_iterator it = elements.begin();
  44. it != elements.end() ; ++it)
  45. {
  46. m_box = it->first;
  47. rtree::apply_visitor(*this, *it->second);
  48. if ( result == false )
  49. return;
  50. }
  51. m_box = box_bckup;
  52. m_is_root = is_root_bckup;
  53. Box box_exp = rtree::elements_box<Box>(elements.begin(), elements.end(), m_tr,
  54. index::detail::get_strategy(m_parameters));
  55. if ( m_exact_match )
  56. result = m_is_root || geometry::equals(box_exp, m_box);
  57. else
  58. result = m_is_root || geometry::covered_by(box_exp, m_box);
  59. }
  60. void operator()(leaf const& n)
  61. {
  62. typedef typename rtree::elements_type<leaf>::type elements_type;
  63. elements_type const& elements = rtree::elements(n);
  64. // non-root node
  65. if (!m_is_root)
  66. {
  67. if ( elements.empty() )
  68. {
  69. result = false;
  70. return;
  71. }
  72. Box box_exp = rtree::values_box<Box>(elements.begin(), elements.end(), m_tr,
  73. index::detail::get_strategy(m_parameters));
  74. if ( m_exact_match )
  75. result = geometry::equals(box_exp, m_box);
  76. else
  77. result = geometry::covered_by(box_exp, m_box);
  78. }
  79. else
  80. result = true;
  81. }
  82. bool result;
  83. private:
  84. parameters_type const& m_parameters;
  85. Translator const& m_tr;
  86. Box m_box;
  87. bool m_is_root;
  88. bool m_exact_match;
  89. };
  90. } // namespace visitors
  91. template <typename Rtree> inline
  92. bool are_boxes_ok(Rtree const& tree, bool exact_match = true)
  93. {
  94. typedef utilities::view<Rtree> RTV;
  95. RTV rtv(tree);
  96. visitors::are_boxes_ok<
  97. typename RTV::value_type,
  98. typename RTV::options_type,
  99. typename RTV::translator_type,
  100. typename RTV::box_type,
  101. typename RTV::allocators_type
  102. > v(tree.parameters(), rtv.translator(), exact_match);
  103. rtv.apply_visitor(v);
  104. return v.result;
  105. }
  106. }}}}}} // namespace boost::geometry::index::detail::rtree::utilities
  107. #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_UTILITIES_ARE_BOXES_OK_HPP