9
3

spatial_query.hpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Boost.Geometry Index
  2. //
  3. // R-tree spatial query visitor implementation
  4. //
  5. // Copyright (c) 2011-2014 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_VISITORS_SPATIAL_QUERY_HPP
  15. #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_VISITORS_SPATIAL_QUERY_HPP
  16. namespace boost { namespace geometry { namespace index {
  17. namespace detail { namespace rtree { namespace visitors {
  18. template <typename Value, typename Options, typename Translator, typename Box, typename Allocators, typename Predicates, typename OutIter>
  19. struct spatial_query
  20. : public rtree::visitor<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag, true>::type
  21. {
  22. typedef typename Options::parameters_type parameters_type;
  23. typedef typename index::detail::strategy_type<parameters_type>::type strategy_type;
  24. typedef typename rtree::node<Value, parameters_type, Box, Allocators, typename Options::node_tag>::type node;
  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. typedef typename Allocators::size_type size_type;
  28. static const unsigned predicates_len = index::detail::predicates_length<Predicates>::value;
  29. inline spatial_query(parameters_type const& par, Translator const& t, Predicates const& p, OutIter out_it)
  30. : tr(t), pred(p), out_iter(out_it), found_count(0), strategy(index::detail::get_strategy(par))
  31. {}
  32. inline void operator()(internal_node const& n)
  33. {
  34. typedef typename rtree::elements_type<internal_node>::type elements_type;
  35. elements_type const& elements = rtree::elements(n);
  36. // traverse nodes meeting predicates
  37. for (typename elements_type::const_iterator it = elements.begin();
  38. it != elements.end(); ++it)
  39. {
  40. // if node meets predicates
  41. // 0 - dummy value
  42. if ( index::detail::predicates_check
  43. <
  44. index::detail::bounds_tag, 0, predicates_len
  45. >(pred, 0, it->first, strategy) )
  46. {
  47. rtree::apply_visitor(*this, *it->second);
  48. }
  49. }
  50. }
  51. inline void operator()(leaf const& n)
  52. {
  53. typedef typename rtree::elements_type<leaf>::type elements_type;
  54. elements_type const& elements = rtree::elements(n);
  55. // get all values meeting predicates
  56. for (typename elements_type::const_iterator it = elements.begin();
  57. it != elements.end(); ++it)
  58. {
  59. // if value meets predicates
  60. if ( index::detail::predicates_check
  61. <
  62. index::detail::value_tag, 0, predicates_len
  63. >(pred, *it, tr(*it), strategy) )
  64. {
  65. *out_iter = *it;
  66. ++out_iter;
  67. ++found_count;
  68. }
  69. }
  70. }
  71. Translator const& tr;
  72. Predicates pred;
  73. OutIter out_iter;
  74. size_type found_count;
  75. strategy_type strategy;
  76. };
  77. template <typename Value, typename Options, typename Translator, typename Box, typename Allocators, typename Predicates>
  78. class spatial_query_incremental
  79. : public rtree::visitor<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag, true>::type
  80. {
  81. typedef typename Options::parameters_type parameters_type;
  82. typedef typename index::detail::strategy_type<parameters_type>::type strategy_type;
  83. public:
  84. typedef typename rtree::node<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type node;
  85. typedef typename rtree::internal_node<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type internal_node;
  86. typedef typename rtree::leaf<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type leaf;
  87. typedef typename Allocators::size_type size_type;
  88. typedef typename Allocators::const_reference const_reference;
  89. typedef typename Allocators::node_pointer node_pointer;
  90. typedef typename rtree::elements_type<internal_node>::type::const_iterator internal_iterator;
  91. typedef typename rtree::elements_type<leaf>::type leaf_elements;
  92. typedef typename rtree::elements_type<leaf>::type::const_iterator leaf_iterator;
  93. static const unsigned predicates_len = index::detail::predicates_length<Predicates>::value;
  94. inline spatial_query_incremental()
  95. : m_translator(NULL)
  96. // , m_pred()
  97. , m_values(NULL)
  98. , m_current()
  99. // , m_strategy()
  100. {}
  101. inline spatial_query_incremental(parameters_type const& params, Translator const& t, Predicates const& p)
  102. : m_translator(::boost::addressof(t))
  103. , m_pred(p)
  104. , m_values(NULL)
  105. , m_current()
  106. , m_strategy(index::detail::get_strategy(params))
  107. {}
  108. inline void operator()(internal_node const& n)
  109. {
  110. typedef typename rtree::elements_type<internal_node>::type elements_type;
  111. elements_type const& elements = rtree::elements(n);
  112. m_internal_stack.push_back(std::make_pair(elements.begin(), elements.end()));
  113. }
  114. inline void operator()(leaf const& n)
  115. {
  116. m_values = ::boost::addressof(rtree::elements(n));
  117. m_current = rtree::elements(n).begin();
  118. }
  119. const_reference dereference() const
  120. {
  121. BOOST_GEOMETRY_INDEX_ASSERT(m_values, "not dereferencable");
  122. return *m_current;
  123. }
  124. void initialize(node_pointer root)
  125. {
  126. rtree::apply_visitor(*this, *root);
  127. search_value();
  128. }
  129. void increment()
  130. {
  131. ++m_current;
  132. search_value();
  133. }
  134. void search_value()
  135. {
  136. for (;;)
  137. {
  138. // if leaf is choosen, move to the next value in leaf
  139. if ( m_values )
  140. {
  141. if ( m_current != m_values->end() )
  142. {
  143. // return if next value is found
  144. Value const& v = *m_current;
  145. if (index::detail::predicates_check
  146. <
  147. index::detail::value_tag, 0, predicates_len
  148. >(m_pred, v, (*m_translator)(v), m_strategy))
  149. {
  150. return;
  151. }
  152. ++m_current;
  153. }
  154. // no more values, clear current leaf
  155. else
  156. {
  157. m_values = 0;
  158. }
  159. }
  160. // if leaf isn't choosen, move to the next leaf
  161. else
  162. {
  163. // return if there is no more nodes to traverse
  164. if ( m_internal_stack.empty() )
  165. return;
  166. // no more children in current node, remove it from stack
  167. if ( m_internal_stack.back().first == m_internal_stack.back().second )
  168. {
  169. m_internal_stack.pop_back();
  170. continue;
  171. }
  172. internal_iterator it = m_internal_stack.back().first;
  173. ++m_internal_stack.back().first;
  174. // next node is found, push it to the stack
  175. if (index::detail::predicates_check
  176. <
  177. index::detail::bounds_tag, 0, predicates_len
  178. >(m_pred, 0, it->first, m_strategy))
  179. {
  180. rtree::apply_visitor(*this, *(it->second));
  181. }
  182. }
  183. }
  184. }
  185. bool is_end() const
  186. {
  187. return 0 == m_values;
  188. }
  189. friend bool operator==(spatial_query_incremental const& l, spatial_query_incremental const& r)
  190. {
  191. return (l.m_values == r.m_values) && (0 == l.m_values || l.m_current == r.m_current );
  192. }
  193. private:
  194. const Translator * m_translator;
  195. Predicates m_pred;
  196. std::vector< std::pair<internal_iterator, internal_iterator> > m_internal_stack;
  197. const leaf_elements * m_values;
  198. leaf_iterator m_current;
  199. strategy_type m_strategy;
  200. };
  201. }}} // namespace detail::rtree::visitors
  202. }}} // namespace boost::geometry::index
  203. #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_VISITORS_SPATIAL_QUERY_HPP