1 // Boost.Geometry Index
4 // Copyright (c) 2011-2015 Adam Wulkiewicz, Lodz, Poland.
6 // Use, modification and distribution is subject to the Boost Software License,
7 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_GEOMETRY_INDEX_TEST_RTREE_HPP
11 #define BOOST_GEOMETRY_INDEX_TEST_RTREE_HPP
13 #include <boost/foreach.hpp>
17 #include <geometry_index_test_common.hpp>
19 #include <boost/geometry/index/rtree.hpp>
21 #include <boost/geometry/geometries/point.hpp>
22 #include <boost/geometry/geometries/box.hpp>
23 #include <boost/geometry/geometries/segment.hpp>
25 #include <boost/geometry/index/detail/rtree/utilities/are_levels_ok.hpp>
26 #include <boost/geometry/index/detail/rtree/utilities/are_boxes_ok.hpp>
28 //#include <boost/geometry/geometries/ring.hpp>
29 //#include <boost/geometry/geometries/polygon.hpp>
33 // Set point's coordinates
35 template <typename Point>
39 template <typename T, typename C>
40 struct outside_point< bg::model::point<T, 2, C> >
42 typedef bg::model::point<T, 2, C> P;
49 template <typename T, typename C>
50 struct outside_point< bg::model::point<T, 3, C> >
52 typedef bg::model::point<T, 3, C> P;
59 // Default value generation
61 template <typename Value>
64 static Value apply(){ return Value(); }
67 // Values, input and rtree generation
69 template <typename Value>
73 template <typename T, typename C>
74 struct value< bg::model::point<T, 2, C> >
76 typedef bg::model::point<T, 2, C> P;
77 static P apply(int x, int y)
83 template <typename T, typename C>
84 struct value< bg::model::box< bg::model::point<T, 2, C> > >
86 typedef bg::model::point<T, 2, C> P;
87 typedef bg::model::box<P> B;
88 static B apply(int x, int y)
90 return B(P(x, y), P(x + 2, y + 3));
94 template <typename T, typename C>
95 struct value< bg::model::segment< bg::model::point<T, 2, C> > >
97 typedef bg::model::point<T, 2, C> P;
98 typedef bg::model::segment<P> S;
99 static S apply(int x, int y)
101 return S(P(x, y), P(x + 2, y + 3));
105 template <typename T, typename C>
106 struct value< std::pair<bg::model::point<T, 2, C>, int> >
108 typedef bg::model::point<T, 2, C> P;
109 typedef std::pair<P, int> R;
110 static R apply(int x, int y)
112 return std::make_pair(P(x, y), x + y * 100);
116 template <typename T, typename C>
117 struct value< std::pair<bg::model::box< bg::model::point<T, 2, C> >, int> >
119 typedef bg::model::point<T, 2, C> P;
120 typedef bg::model::box<P> B;
121 typedef std::pair<B, int> R;
122 static R apply(int x, int y)
124 return std::make_pair(B(P(x, y), P(x + 2, y + 3)), x + y * 100);
128 template <typename T, typename C>
129 struct value< std::pair<bg::model::segment< bg::model::point<T, 2, C> >, int> >
131 typedef bg::model::point<T, 2, C> P;
132 typedef bg::model::segment<P> S;
133 typedef std::pair<S, int> R;
134 static R apply(int x, int y)
136 return std::make_pair(S(P(x, y), P(x + 2, y + 3)), x + y * 100);
140 template <typename T, typename C>
141 struct value< boost::tuple<bg::model::point<T, 2, C>, int, int> >
143 typedef bg::model::point<T, 2, C> P;
144 typedef boost::tuple<P, int, int> R;
145 static R apply(int x, int y)
147 return boost::make_tuple(P(x, y), x + y * 100, 0);
151 template <typename T, typename C>
152 struct value< boost::tuple<bg::model::box< bg::model::point<T, 2, C> >, int, int> >
154 typedef bg::model::point<T, 2, C> P;
155 typedef bg::model::box<P> B;
156 typedef boost::tuple<B, int, int> R;
157 static R apply(int x, int y)
159 return boost::make_tuple(B(P(x, y), P(x + 2, y + 3)), x + y * 100, 0);
163 template <typename T, typename C>
164 struct value< boost::tuple<bg::model::segment< bg::model::point<T, 2, C> >, int, int> >
166 typedef bg::model::point<T, 2, C> P;
167 typedef bg::model::segment<P> S;
168 typedef boost::tuple<S, int, int> R;
169 static R apply(int x, int y)
171 return boost::make_tuple(S(P(x, y), P(x + 2, y + 3)), x + y * 100, 0);
175 template <typename T, typename C>
176 struct value< bg::model::point<T, 3, C> >
178 typedef bg::model::point<T, 3, C> P;
179 static P apply(int x, int y, int z)
185 template <typename T, typename C>
186 struct value< bg::model::box< bg::model::point<T, 3, C> > >
188 typedef bg::model::point<T, 3, C> P;
189 typedef bg::model::box<P> B;
190 static B apply(int x, int y, int z)
192 return B(P(x, y, z), P(x + 2, y + 3, z + 4));
196 template <typename T, typename C>
197 struct value< std::pair<bg::model::point<T, 3, C>, int> >
199 typedef bg::model::point<T, 3, C> P;
200 typedef std::pair<P, int> R;
201 static R apply(int x, int y, int z)
203 return std::make_pair(P(x, y, z), x + y * 100 + z * 10000);
207 template <typename T, typename C>
208 struct value< std::pair<bg::model::box< bg::model::point<T, 3, C> >, int> >
210 typedef bg::model::point<T, 3, C> P;
211 typedef bg::model::box<P> B;
212 typedef std::pair<B, int> R;
213 static R apply(int x, int y, int z)
215 return std::make_pair(B(P(x, y, z), P(x + 2, y + 3, z + 4)), x + y * 100 + z * 10000);
219 template <typename T, typename C>
220 struct value< boost::tuple<bg::model::point<T, 3, C>, int, int> >
222 typedef bg::model::point<T, 3, C> P;
223 typedef boost::tuple<P, int, int> R;
224 static R apply(int x, int y, int z)
226 return boost::make_tuple(P(x, y, z), x + y * 100 + z * 10000, 0);
230 template <typename T, typename C>
231 struct value< boost::tuple<bg::model::box< bg::model::point<T, 3, C> >, int, int> >
233 typedef bg::model::point<T, 3, C> P;
234 typedef bg::model::box<P> B;
235 typedef boost::tuple<B, int, int> R;
236 static R apply(int x, int y, int z)
238 return boost::make_tuple(B(P(x, y, z), P(x + 2, y + 3, z + 4)), x + y * 100 + z * 10000, 0);
242 #if !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
244 template <typename T, typename C>
245 struct value< std::tuple<bg::model::point<T, 2, C>, int, int> >
247 typedef bg::model::point<T, 2, C> P;
248 typedef std::tuple<P, int, int> R;
249 static R apply(int x, int y)
251 return std::make_tuple(P(x, y), x + y * 100, 0);
255 template <typename T, typename C>
256 struct value< std::tuple<bg::model::box< bg::model::point<T, 2, C> >, int, int> >
258 typedef bg::model::point<T, 2, C> P;
259 typedef bg::model::box<P> B;
260 typedef std::tuple<B, int, int> R;
261 static R apply(int x, int y)
263 return std::make_tuple(B(P(x, y), P(x + 2, y + 3)), x + y * 100, 0);
267 template <typename T, typename C>
268 struct value< std::tuple<bg::model::segment< bg::model::point<T, 2, C> >, int, int> >
270 typedef bg::model::point<T, 2, C> P;
271 typedef bg::model::segment<P> S;
272 typedef std::tuple<S, int, int> R;
273 static R apply(int x, int y)
275 return std::make_tuple(S(P(x, y), P(x + 2, y + 3)), x + y * 100, 0);
279 template <typename T, typename C>
280 struct value< std::tuple<bg::model::point<T, 3, C>, int, int> >
282 typedef bg::model::point<T, 3, C> P;
283 typedef std::tuple<P, int, int> R;
284 static R apply(int x, int y, int z)
286 return std::make_tuple(P(x, y, z), x + y * 100 + z * 10000, 0);
290 template <typename T, typename C>
291 struct value< std::tuple<bg::model::box< bg::model::point<T, 3, C> >, int, int> >
293 typedef bg::model::point<T, 3, C> P;
294 typedef bg::model::box<P> B;
295 typedef std::tuple<B, int, int> R;
296 static R apply(int x, int y, int z)
298 return std::make_tuple(B(P(x, y, z), P(x + 2, y + 3, z + 4)), x + y * 100 + z * 10000, 0);
302 #endif // #if !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
304 } // namespace generate
308 template <typename Indexable>
311 test_object(Indexable const& indexable_) : indexable(indexable_) {}
315 namespace boost { namespace geometry { namespace index {
317 template <typename Indexable>
318 struct indexable< boost::shared_ptr< test_object<Indexable> > >
320 typedef boost::shared_ptr< test_object<Indexable> > value_type;
321 typedef Indexable const& result_type;
323 result_type operator()(value_type const& value) const
325 return value->indexable;
333 template <typename T, typename C>
334 struct value< boost::shared_ptr<test_object<bg::model::point<T, 2, C> > > >
336 typedef bg::model::point<T, 2, C> P;
337 typedef test_object<P> O;
338 typedef boost::shared_ptr<O> R;
340 static R apply(int x, int y)
342 return R(new O(P(x, y)));
346 template <typename T, typename C>
347 struct value< boost::shared_ptr<test_object<bg::model::point<T, 3, C> > > >
349 typedef bg::model::point<T, 3, C> P;
350 typedef test_object<P> O;
351 typedef boost::shared_ptr<O> R;
353 static R apply(int x, int y, int z)
355 return R(new O(P(x, y, z)));
359 template <typename T, typename C>
360 struct value< boost::shared_ptr<test_object<bg::model::box<bg::model::point<T, 2, C> > > > >
362 typedef bg::model::point<T, 2, C> P;
363 typedef bg::model::box<P> B;
364 typedef test_object<B> O;
365 typedef boost::shared_ptr<O> R;
367 static R apply(int x, int y)
369 return R(new O(B(P(x, y), P(x + 2, y + 3))));
373 template <typename T, typename C>
374 struct value< boost::shared_ptr<test_object<bg::model::box<bg::model::point<T, 3, C> > > > >
376 typedef bg::model::point<T, 3, C> P;
377 typedef bg::model::box<P> B;
378 typedef test_object<B> O;
379 typedef boost::shared_ptr<O> R;
381 static R apply(int x, int y, int z)
383 return R(new O(B(P(x, y, z), P(x + 2, y + 3, z + 4))));
387 template <typename T, typename C>
388 struct value< boost::shared_ptr<test_object<bg::model::segment<bg::model::point<T, 2, C> > > > >
390 typedef bg::model::point<T, 2, C> P;
391 typedef bg::model::segment<P> S;
392 typedef test_object<S> O;
393 typedef boost::shared_ptr<O> R;
395 static R apply(int x, int y)
397 return R(new O(S(P(x, y), P(x + 2, y + 3))));
401 } //namespace generate
405 template <typename Indexable>
406 struct counting_value
408 counting_value() { counter()++; }
409 counting_value(Indexable const& i) : indexable(i) { counter()++; }
410 counting_value(counting_value const& c) : indexable(c.indexable) { counter()++; }
411 ~counting_value() { counter()--; }
413 static size_t & counter() { static size_t c = 0; return c; }
417 namespace boost { namespace geometry { namespace index {
419 template <typename Indexable>
420 struct indexable< counting_value<Indexable> >
422 typedef counting_value<Indexable> value_type;
423 typedef Indexable const& result_type;
424 result_type operator()(value_type const& value) const
426 return value.indexable;
430 template <typename Indexable>
431 struct equal_to< counting_value<Indexable> >
433 typedef counting_value<Indexable> value_type;
434 typedef bool result_type;
435 bool operator()(value_type const& v1, value_type const& v2) const
437 return boost::geometry::equals(v1.indexable, v2.indexable);
445 template <typename T, typename C>
446 struct value< counting_value<bg::model::point<T, 2, C> > >
448 typedef bg::model::point<T, 2, C> P;
449 typedef counting_value<P> R;
450 static R apply(int x, int y) { return R(P(x, y)); }
453 template <typename T, typename C>
454 struct value< counting_value<bg::model::point<T, 3, C> > >
456 typedef bg::model::point<T, 3, C> P;
457 typedef counting_value<P> R;
458 static R apply(int x, int y, int z) { return R(P(x, y, z)); }
461 template <typename T, typename C>
462 struct value< counting_value<bg::model::box<bg::model::point<T, 2, C> > > >
464 typedef bg::model::point<T, 2, C> P;
465 typedef bg::model::box<P> B;
466 typedef counting_value<B> R;
467 static R apply(int x, int y) { return R(B(P(x, y), P(x+2, y+3))); }
470 template <typename T, typename C>
471 struct value< counting_value<bg::model::box<bg::model::point<T, 3, C> > > >
473 typedef bg::model::point<T, 3, C> P;
474 typedef bg::model::box<P> B;
475 typedef counting_value<B> R;
476 static R apply(int x, int y, int z) { return R(B(P(x, y, z), P(x+2, y+3, z+4))); }
479 template <typename T, typename C>
480 struct value< counting_value<bg::model::segment<bg::model::point<T, 2, C> > > >
482 typedef bg::model::point<T, 2, C> P;
483 typedef bg::model::segment<P> S;
484 typedef counting_value<S> R;
485 static R apply(int x, int y) { return R(S(P(x, y), P(x+2, y+3))); }
488 } // namespace generate
490 // value without default constructor
492 template <typename Indexable>
493 struct value_no_dctor
495 value_no_dctor(Indexable const& i) : indexable(i) {}
499 namespace boost { namespace geometry { namespace index {
501 template <typename Indexable>
502 struct indexable< value_no_dctor<Indexable> >
504 typedef value_no_dctor<Indexable> value_type;
505 typedef Indexable const& result_type;
506 result_type operator()(value_type const& value) const
508 return value.indexable;
512 template <typename Indexable>
513 struct equal_to< value_no_dctor<Indexable> >
515 typedef value_no_dctor<Indexable> value_type;
516 typedef bool result_type;
517 bool operator()(value_type const& v1, value_type const& v2) const
519 return boost::geometry::equals(v1.indexable, v2.indexable);
527 template <typename Indexable>
528 struct value_default< value_no_dctor<Indexable> >
530 static value_no_dctor<Indexable> apply() { return value_no_dctor<Indexable>(Indexable()); }
533 template <typename T, typename C>
534 struct value< value_no_dctor<bg::model::point<T, 2, C> > >
536 typedef bg::model::point<T, 2, C> P;
537 typedef value_no_dctor<P> R;
538 static R apply(int x, int y) { return R(P(x, y)); }
541 template <typename T, typename C>
542 struct value< value_no_dctor<bg::model::point<T, 3, C> > >
544 typedef bg::model::point<T, 3, C> P;
545 typedef value_no_dctor<P> R;
546 static R apply(int x, int y, int z) { return R(P(x, y, z)); }
549 template <typename T, typename C>
550 struct value< value_no_dctor<bg::model::box<bg::model::point<T, 2, C> > > >
552 typedef bg::model::point<T, 2, C> P;
553 typedef bg::model::box<P> B;
554 typedef value_no_dctor<B> R;
555 static R apply(int x, int y) { return R(B(P(x, y), P(x+2, y+3))); }
558 template <typename T, typename C>
559 struct value< value_no_dctor<bg::model::box<bg::model::point<T, 3, C> > > >
561 typedef bg::model::point<T, 3, C> P;
562 typedef bg::model::box<P> B;
563 typedef value_no_dctor<B> R;
564 static R apply(int x, int y, int z) { return R(B(P(x, y, z), P(x+2, y+3, z+4))); }
567 template <typename T, typename C>
568 struct value< value_no_dctor<bg::model::segment<bg::model::point<T, 2, C> > > >
570 typedef bg::model::point<T, 2, C> P;
571 typedef bg::model::segment<P> S;
572 typedef value_no_dctor<S> R;
573 static R apply(int x, int y) { return R(S(P(x, y), P(x+2, y+3))); }
578 template <size_t Dimension>
585 template <typename Value, typename Box>
586 static void apply(std::vector<Value> & input, Box & qbox, int size = 1)
588 BOOST_GEOMETRY_INDEX_ASSERT(0 < size, "the value must be greather than 0");
590 for ( int i = 0 ; i < 12 * size ; i += 3 )
592 for ( int j = 1 ; j < 25 * size ; j += 4 )
594 input.push_back( generate::value<Value>::apply(i, j) );
598 typedef typename bg::traits::point_type<Box>::type P;
600 qbox = Box(P(3, 0), P(10, 9));
607 template <typename Value, typename Box>
608 static void apply(std::vector<Value> & input, Box & qbox, int size = 1)
610 BOOST_GEOMETRY_INDEX_ASSERT(0 < size, "the value must be greather than 0");
612 for ( int i = 0 ; i < 12 * size ; i += 3 )
614 for ( int j = 1 ; j < 25 * size ; j += 4 )
616 for ( int k = 2 ; k < 12 * size ; k += 5 )
618 input.push_back( generate::value<Value>::apply(i, j, k) );
623 typedef typename bg::traits::point_type<Box>::type P;
625 qbox = Box(P(3, 0, 3), P(10, 9, 11));
629 // generate_value_outside
631 template <typename Value, size_t Dimension>
632 struct value_outside_impl
635 template <typename Value>
636 struct value_outside_impl<Value, 2>
640 //TODO - for size > 1 in generate_input<> this won't be outside
641 return generate::value<Value>::apply(13, 26);
645 template <typename Value>
646 struct value_outside_impl<Value, 3>
650 //TODO - for size > 1 in generate_input<> this won't be outside
651 return generate::value<Value>::apply(13, 26, 13);
655 template <typename Rtree>
656 inline typename Rtree::value_type
659 typedef typename Rtree::value_type V;
660 typedef typename Rtree::indexable_type I;
662 return value_outside_impl<V, bg::dimension<I>::value>::apply();
665 template<typename Rtree, typename Elements, typename Box>
666 void rtree(Rtree & tree, Elements & input, Box & qbox)
668 typedef typename Rtree::indexable_type I;
671 bg::dimension<I>::value
672 >::apply(input, qbox);
674 tree.insert(input.begin(), input.end());
677 } // namespace generate
679 namespace basictest {
681 // low level test functions
683 template <typename Rtree, typename Iter, typename Value>
684 Iter find(Rtree const& rtree, Iter first, Iter last, Value const& value)
686 for ( ; first != last ; ++first )
687 if ( rtree.value_eq()(value, *first) )
692 template <typename Rtree, typename Value>
693 void compare_outputs(Rtree const& rtree, std::vector<Value> const& output, std::vector<Value> const& expected_output)
695 bool are_sizes_ok = (expected_output.size() == output.size());
696 BOOST_CHECK( are_sizes_ok );
699 BOOST_FOREACH(Value const& v, expected_output)
701 BOOST_CHECK(find(rtree, output.begin(), output.end(), v) != output.end() );
706 template <typename Rtree, typename Range1, typename Range2>
707 void exactly_the_same_outputs(Rtree const& rtree, Range1 const& output, Range2 const& expected_output)
709 size_t s1 = std::distance(output.begin(), output.end());
710 size_t s2 = std::distance(expected_output.begin(), expected_output.end());
711 BOOST_CHECK(s1 == s2);
715 typename Range1::const_iterator it1 = output.begin();
716 typename Range2::const_iterator it2 = expected_output.begin();
717 for ( ; it1 != output.end() && it2 != expected_output.end() ; ++it1, ++it2 )
719 if ( !rtree.value_eq()(*it1, *it2) )
721 BOOST_CHECK(false && "rtree.translator().equals(*it1, *it2)");
728 // alternative version of std::copy taking iterators of differnet types
729 template <typename First, typename Last, typename Out>
730 void copy_alt(First first, Last last, Out out)
732 for ( ; first != last ; ++first, ++out )
736 // test query iterators
737 template <typename QItF, typename QItL>
738 void check_fwd_iterators(QItF first, QItL last)
741 BOOST_CHECK(vinit == last);
743 #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
744 QItL vinit2 = QItL();
745 BOOST_CHECK(vinit2 == last);
749 BOOST_CHECK(def == last);
751 #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
753 BOOST_CHECK(def2 == last);
757 for ( ; it != last && first != last ; ++it, ++first)
759 BOOST_CHECK(it == first);
761 bg::index::equal_to<typename std::iterator_traits<QItF>::value_type> eq;
762 BOOST_CHECK(eq(*it, *first));
764 BOOST_CHECK(it == last);
765 BOOST_CHECK(first == last);
770 template <typename Rtree, typename Value, typename Predicates>
771 void spatial_query(Rtree & rtree, Predicates const& pred, std::vector<Value> const& expected_output)
773 BOOST_CHECK( bgi::detail::rtree::utilities::are_levels_ok(rtree) );
774 if ( !rtree.empty() )
775 BOOST_CHECK( bgi::detail::rtree::utilities::are_boxes_ok(rtree) );
777 std::vector<Value> output;
778 size_t n = rtree.query(pred, std::back_inserter(output));
780 BOOST_CHECK( expected_output.size() == n );
781 compare_outputs(rtree, output, expected_output);
783 std::vector<Value> output2;
784 size_t n2 = query(rtree, pred, std::back_inserter(output2));
786 BOOST_CHECK( n == n2 );
787 exactly_the_same_outputs(rtree, output, output2);
789 exactly_the_same_outputs(rtree, output, rtree | bgi::adaptors::queried(pred));
791 std::vector<Value> output3;
792 std::copy(rtree.qbegin(pred), rtree.qend(), std::back_inserter(output3));
794 compare_outputs(rtree, output3, expected_output);
796 std::vector<Value> output4;
797 std::copy(qbegin(rtree, pred), qend(rtree), std::back_inserter(output4));
799 exactly_the_same_outputs(rtree, output3, output4);
801 check_fwd_iterators(rtree.qbegin(pred), rtree.qend());
803 #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
805 std::vector<Value> output4;
806 std::copy(rtree.qbegin_(pred), rtree.qend_(pred), std::back_inserter(output4));
807 compare_outputs(rtree, output4, expected_output);
809 copy_alt(rtree.qbegin_(pred), rtree.qend_(), std::back_inserter(output4));
810 compare_outputs(rtree, output4, expected_output);
812 check_fwd_iterators(rtree.qbegin_(pred), rtree.qend_(pred));
813 check_fwd_iterators(rtree.qbegin_(pred), rtree.qend_());
818 // rtree specific queries tests
820 template <typename Rtree, typename Value, typename Box>
821 void intersects(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
823 std::vector<Value> expected_output;
825 BOOST_FOREACH(Value const& v, input)
826 if ( bg::intersects(tree.indexable_get()(v), qbox) )
827 expected_output.push_back(v);
829 //spatial_query(tree, qbox, expected_output);
830 spatial_query(tree, bgi::intersects(qbox), expected_output);
831 spatial_query(tree, !bgi::disjoint(qbox), expected_output);
833 /*typedef bg::traits::point_type<Box>::type P;
834 bg::model::ring<P> qring;
835 bg::convert(qbox, qring);
836 spatial_query(tree, bgi::intersects(qring), expected_output);
837 spatial_query(tree, !bgi::disjoint(qring), expected_output);
838 bg::model::polygon<P> qpoly;
839 bg::convert(qbox, qpoly);
840 spatial_query(tree, bgi::intersects(qpoly), expected_output);
841 spatial_query(tree, !bgi::disjoint(qpoly), expected_output);*/
844 template <typename Rtree, typename Value, typename Box>
845 void disjoint(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
847 std::vector<Value> expected_output;
849 BOOST_FOREACH(Value const& v, input)
850 if ( bg::disjoint(tree.indexable_get()(v), qbox) )
851 expected_output.push_back(v);
853 spatial_query(tree, bgi::disjoint(qbox), expected_output);
854 spatial_query(tree, !bgi::intersects(qbox), expected_output);
856 /*typedef bg::traits::point_type<Box>::type P;
857 bg::model::ring<P> qring;
858 bg::convert(qbox, qring);
859 spatial_query(tree, bgi::disjoint(qring), expected_output);
860 bg::model::polygon<P> qpoly;
861 bg::convert(qbox, qpoly);
862 spatial_query(tree, bgi::disjoint(qpoly), expected_output);*/
865 template <typename Tag>
868 template <typename Rtree, typename Value, typename Box>
869 static void apply(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
871 std::vector<Value> expected_output;
873 BOOST_FOREACH(Value const& v, input)
874 if ( bg::within(qbox, tree.indexable_get()(v)) )
875 expected_output.push_back(v);
877 spatial_query(tree, bgi::contains(qbox), expected_output);
879 /*typedef bg::traits::point_type<Box>::type P;
880 bg::model::ring<P> qring;
881 bg::convert(qbox, qring);
882 spatial_query(tree, bgi::contains(qring), expected_output);
883 bg::model::polygon<P> qpoly;
884 bg::convert(qbox, qpoly);
885 spatial_query(tree, bgi::contains(qpoly), expected_output);*/
890 struct contains_impl<bg::point_tag>
892 template <typename Rtree, typename Value, typename Box>
893 static void apply(Rtree const& /*tree*/, std::vector<Value> const& /*input*/, Box const& /*qbox*/)
898 struct contains_impl<bg::segment_tag>
900 template <typename Rtree, typename Value, typename Box>
901 static void apply(Rtree const& /*tree*/, std::vector<Value> const& /*input*/, Box const& /*qbox*/)
905 template <typename Rtree, typename Value, typename Box>
906 void contains(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
910 typename Rtree::indexable_type
912 >::apply(tree, input, qbox);
915 template <typename Tag>
916 struct covered_by_impl
918 template <typename Rtree, typename Value, typename Box>
919 static void apply(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
921 std::vector<Value> expected_output;
923 BOOST_FOREACH(Value const& v, input)
926 bgi::detail::return_ref_or_bounds(
927 tree.indexable_get()(v)),
930 expected_output.push_back(v);
934 spatial_query(tree, bgi::covered_by(qbox), expected_output);
936 /*typedef bg::traits::point_type<Box>::type P;
937 bg::model::ring<P> qring;
938 bg::convert(qbox, qring);
939 spatial_query(tree, bgi::covered_by(qring), expected_output);
940 bg::model::polygon<P> qpoly;
941 bg::convert(qbox, qpoly);
942 spatial_query(tree, bgi::covered_by(qpoly), expected_output);*/
947 struct covered_by_impl<bg::segment_tag>
949 template <typename Rtree, typename Value, typename Box>
950 static void apply(Rtree const& /*tree*/, std::vector<Value> const& /*input*/, Box const& /*qbox*/)
954 template <typename Rtree, typename Value, typename Box>
955 void covered_by(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
959 typename Rtree::indexable_type
961 >::apply(tree, input, qbox);
964 template <typename Tag>
967 template <typename Rtree, typename Value, typename Box>
968 static void apply(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
970 std::vector<Value> expected_output;
972 BOOST_FOREACH(Value const& v, input)
973 if ( bg::covered_by(qbox, tree.indexable_get()(v)) )
974 expected_output.push_back(v);
976 spatial_query(tree, bgi::covers(qbox), expected_output);
978 /*typedef bg::traits::point_type<Box>::type P;
979 bg::model::ring<P> qring;
980 bg::convert(qbox, qring);
981 spatial_query(tree, bgi::covers(qring), expected_output);
982 bg::model::polygon<P> qpoly;
983 bg::convert(qbox, qpoly);
984 spatial_query(tree, bgi::covers(qpoly), expected_output);*/
989 struct covers_impl<bg::point_tag>
991 template <typename Rtree, typename Value, typename Box>
992 static void apply(Rtree const& /*tree*/, std::vector<Value> const& /*input*/, Box const& /*qbox*/)
997 struct covers_impl<bg::segment_tag>
999 template <typename Rtree, typename Value, typename Box>
1000 static void apply(Rtree const& /*tree*/, std::vector<Value> const& /*input*/, Box const& /*qbox*/)
1004 template <typename Rtree, typename Value, typename Box>
1005 void covers(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
1009 typename Rtree::indexable_type
1011 >::apply(tree, input, qbox);
1014 template <typename Tag>
1015 struct overlaps_impl
1017 template <typename Rtree, typename Value, typename Box>
1018 static void apply(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
1020 std::vector<Value> expected_output;
1022 BOOST_FOREACH(Value const& v, input)
1023 if ( bg::overlaps(tree.indexable_get()(v), qbox) )
1024 expected_output.push_back(v);
1026 spatial_query(tree, bgi::overlaps(qbox), expected_output);
1028 /*typedef bg::traits::point_type<Box>::type P;
1029 bg::model::ring<P> qring;
1030 bg::convert(qbox, qring);
1031 spatial_query(tree, bgi::overlaps(qring), expected_output);
1032 bg::model::polygon<P> qpoly;
1033 bg::convert(qbox, qpoly);
1034 spatial_query(tree, bgi::overlaps(qpoly), expected_output);*/
1039 struct overlaps_impl<bg::point_tag>
1041 template <typename Rtree, typename Value, typename Box>
1042 static void apply(Rtree const& /*tree*/, std::vector<Value> const& /*input*/, Box const& /*qbox*/)
1047 struct overlaps_impl<bg::segment_tag>
1049 template <typename Rtree, typename Value, typename Box>
1050 static void apply(Rtree const& /*tree*/, std::vector<Value> const& /*input*/, Box const& /*qbox*/)
1054 template <typename Rtree, typename Value, typename Box>
1055 void overlaps(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
1059 typename Rtree::indexable_type
1061 >::apply(tree, input, qbox);
1064 //template <typename Tag, size_t Dimension>
1065 //struct touches_impl
1067 // template <typename Rtree, typename Value, typename Box>
1068 // static void apply(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
1073 //struct touches_impl<bg::box_tag, 2>
1075 // template <typename Rtree, typename Value, typename Box>
1076 // static void apply(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
1078 // std::vector<Value> expected_output;
1080 // BOOST_FOREACH(Value const& v, input)
1081 // if ( bg::touches(tree.translator()(v), qbox) )
1082 // expected_output.push_back(v);
1084 // spatial_query(tree, bgi::touches(qbox), expected_output);
1088 //template <typename Rtree, typename Value, typename Box>
1089 //void touches(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
1092 // bgi::traits::tag<typename Rtree::indexable_type>::type,
1093 // bgi::traits::dimension<typename Rtree::indexable_type>::value
1094 // >::apply(tree, input, qbox);
1097 template <typename Tag>
1100 template <typename Rtree, typename Value, typename Box>
1101 static void apply(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
1103 std::vector<Value> expected_output;
1105 BOOST_FOREACH(Value const& v, input)
1106 if ( bg::within(tree.indexable_get()(v), qbox) )
1107 expected_output.push_back(v);
1109 spatial_query(tree, bgi::within(qbox), expected_output);
1111 /*typedef bg::traits::point_type<Box>::type P;
1112 bg::model::ring<P> qring;
1113 bg::convert(qbox, qring);
1114 spatial_query(tree, bgi::within(qring), expected_output);
1115 bg::model::polygon<P> qpoly;
1116 bg::convert(qbox, qpoly);
1117 spatial_query(tree, bgi::within(qpoly), expected_output);*/
1122 struct within_impl<bg::segment_tag>
1124 template <typename Rtree, typename Value, typename Box>
1125 static void apply(Rtree const& /*tree*/, std::vector<Value> const& /*input*/, Box const& /*qbox*/)
1129 template <typename Rtree, typename Value, typename Box>
1130 void within(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
1134 typename Rtree::indexable_type
1136 >::apply(tree, input, qbox);
1139 // rtree nearest queries
1141 template <typename Rtree, typename Point>
1144 typedef typename bg::default_distance_result<Point, typename Rtree::indexable_type>::type D;
1146 template <typename Value>
1147 bool operator()(std::pair<D, Value> const& p1, std::pair<D, Value> const& p2) const
1149 return p1.first < p2.first;
1153 template <typename Rtree, typename Point>
1154 struct NearestKTransform
1156 typedef typename bg::default_distance_result<Point, typename Rtree::indexable_type>::type D;
1158 template <typename Value>
1159 Value const& operator()(std::pair<D, Value> const& p) const
1165 template <typename Rtree, typename Value, typename Point, typename Distance>
1166 inline void compare_nearest_outputs(Rtree const& rtree, std::vector<Value> const& output, std::vector<Value> const& expected_output, Point const& pt, Distance greatest_distance)
1169 bool are_sizes_ok = (expected_output.size() == output.size());
1170 BOOST_CHECK( are_sizes_ok );
1173 BOOST_FOREACH(Value const& v, output)
1175 // TODO - perform explicit check here?
1176 // should all objects which are closest be checked and should exactly the same be found?
1178 if ( find(rtree, expected_output.begin(), expected_output.end(), v) == expected_output.end() )
1180 Distance d = bg::comparable_distance(pt, rtree.indexable_get()(v));
1181 BOOST_CHECK(d == greatest_distance);
1187 template <typename Rtree, typename Value, typename Point>
1188 inline void check_sorted_by_distance(Rtree const& rtree, std::vector<Value> const& output, Point const& pt)
1190 typedef typename bg::default_distance_result<Point, typename Rtree::indexable_type>::type D;
1193 BOOST_FOREACH(Value const& v, output)
1195 D d = bg::comparable_distance(pt, rtree.indexable_get()(v));
1196 BOOST_CHECK(prev_dist <= d);
1201 template <typename Rtree, typename Value, typename Point>
1202 inline void nearest_query_k(Rtree const& rtree, std::vector<Value> const& input, Point const& pt, unsigned int k)
1204 // TODO: Nearest object may not be the same as found by the rtree if distances are equal
1205 // All objects with the same closest distance should be picked
1207 typedef typename bg::default_distance_result<Point, typename Rtree::indexable_type>::type D;
1209 std::vector< std::pair<D, Value> > test_output;
1211 // calculate test output - k closest values pairs
1212 BOOST_FOREACH(Value const& v, input)
1214 D d = bg::comparable_distance(pt, rtree.indexable_get()(v));
1216 if ( test_output.size() < k )
1217 test_output.push_back(std::make_pair(d, v));
1220 std::sort(test_output.begin(), test_output.end(), NearestKLess<Rtree, Point>());
1221 if ( d < test_output.back().first )
1222 test_output.back() = std::make_pair(d, v);
1226 // caluclate biggest distance
1227 std::sort(test_output.begin(), test_output.end(), NearestKLess<Rtree, Point>());
1228 D greatest_distance = 0;
1229 if ( !test_output.empty() )
1230 greatest_distance = test_output.back().first;
1232 // transform test output to vector of values
1233 std::vector<Value> expected_output(test_output.size(), generate::value_default<Value>::apply());
1234 std::transform(test_output.begin(), test_output.end(), expected_output.begin(), NearestKTransform<Rtree, Point>());
1236 // calculate output using rtree
1237 std::vector<Value> output;
1238 rtree.query(bgi::nearest(pt, k), std::back_inserter(output));
1241 compare_nearest_outputs(rtree, output, expected_output, pt, greatest_distance);
1243 exactly_the_same_outputs(rtree, output, rtree | bgi::adaptors::queried(bgi::nearest(pt, k)));
1245 std::vector<Value> output2(k, generate::value_default<Value>::apply());
1246 typename Rtree::size_type found_count = rtree.query(bgi::nearest(pt, k), output2.begin());
1247 output2.resize(found_count, generate::value_default<Value>::apply());
1249 exactly_the_same_outputs(rtree, output, output2);
1251 std::vector<Value> output3;
1252 std::copy(rtree.qbegin(bgi::nearest(pt, k)), rtree.qend(), std::back_inserter(output3));
1254 compare_nearest_outputs(rtree, output3, expected_output, pt, greatest_distance);
1255 check_sorted_by_distance(rtree, output3, pt);
1257 check_fwd_iterators(rtree.qbegin(bgi::nearest(pt, k)), rtree.qend());
1259 #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
1261 std::vector<Value> output4;
1262 std::copy(rtree.qbegin_(bgi::nearest(pt, k)), rtree.qend_(bgi::nearest(pt, k)), std::back_inserter(output4));
1263 exactly_the_same_outputs(rtree, output4, output3);
1265 copy_alt(rtree.qbegin_(bgi::nearest(pt, k)), rtree.qend_(), std::back_inserter(output4));
1266 exactly_the_same_outputs(rtree, output4, output3);
1268 check_fwd_iterators(rtree.qbegin_(bgi::nearest(pt, k)), rtree.qend_(bgi::nearest(pt, k)));
1269 check_fwd_iterators(rtree.qbegin_(bgi::nearest(pt, k)), rtree.qend_());
1274 // rtree nearest not found
1278 template <typename Value>
1279 bool operator()(Value const& ) const { return false; }
1282 template <typename Rtree, typename Point>
1283 void nearest_query_not_found(Rtree const& rtree, Point const& pt)
1285 typedef typename Rtree::value_type Value;
1287 std::vector<Value> output_v;
1288 size_t n_res = rtree.query(bgi::nearest(pt, 5) && bgi::satisfies(AlwaysFalse()), std::back_inserter(output_v));
1289 BOOST_CHECK(output_v.size() == n_res);
1290 BOOST_CHECK(n_res < 5);
1293 template <typename Value>
1294 bool satisfies_fun(Value const& ) { return true; }
1296 struct satisfies_obj
1298 template <typename Value>
1299 bool operator()(Value const& ) const { return true; }
1302 template <typename Rtree, typename Value>
1303 void satisfies(Rtree const& rtree, std::vector<Value> const& input)
1305 std::vector<Value> result;
1306 rtree.query(bgi::satisfies(satisfies_obj()), std::back_inserter(result));
1307 BOOST_CHECK(result.size() == input.size());
1309 rtree.query(!bgi::satisfies(satisfies_obj()), std::back_inserter(result));
1310 BOOST_CHECK(result.size() == 0);
1313 rtree.query(bgi::satisfies(satisfies_fun<Value>), std::back_inserter(result));
1314 BOOST_CHECK(result.size() == input.size());
1316 rtree.query(!bgi::satisfies(satisfies_fun<Value>), std::back_inserter(result));
1317 BOOST_CHECK(result.size() == 0);
1319 #ifndef BOOST_NO_CXX11_LAMBDAS
1321 rtree.query(bgi::satisfies([](Value const&){ return true; }), std::back_inserter(result));
1322 BOOST_CHECK(result.size() == input.size());
1324 rtree.query(!bgi::satisfies([](Value const&){ return true; }), std::back_inserter(result));
1325 BOOST_CHECK(result.size() == 0);
1329 // rtree copying and moving
1331 template <typename Rtree, typename Box>
1332 void copy_swap_move(Rtree const& tree, Box const& qbox)
1334 typedef typename Rtree::value_type Value;
1335 typedef typename Rtree::parameters_type Params;
1337 size_t s = tree.size();
1338 Params params = tree.parameters();
1340 std::vector<Value> expected_output;
1341 tree.query(bgi::intersects(qbox), std::back_inserter(expected_output));
1346 BOOST_CHECK(tree.empty() == t1.empty());
1347 BOOST_CHECK(tree.size() == t1.size());
1348 BOOST_CHECK(t1.parameters().get_max_elements() == params.get_max_elements());
1349 BOOST_CHECK(t1.parameters().get_min_elements() == params.get_min_elements());
1351 std::vector<Value> output;
1352 t1.query(bgi::intersects(qbox), std::back_inserter(output));
1353 exactly_the_same_outputs(t1, output, expected_output);
1355 // copying assignment operator
1358 BOOST_CHECK(tree.empty() == t1.empty());
1359 BOOST_CHECK(tree.size() == t1.size());
1360 BOOST_CHECK(t1.parameters().get_max_elements() == params.get_max_elements());
1361 BOOST_CHECK(t1.parameters().get_min_elements() == params.get_min_elements());
1364 t1.query(bgi::intersects(qbox), std::back_inserter(output));
1365 exactly_the_same_outputs(t1, output, expected_output);
1367 Rtree t2(tree.parameters(), tree.indexable_get(), tree.value_eq(), tree.get_allocator());
1369 BOOST_CHECK(tree.empty() == t2.empty());
1370 BOOST_CHECK(tree.size() == t2.size());
1371 BOOST_CHECK(true == t1.empty());
1372 BOOST_CHECK(0 == t1.size());
1373 // those fails e.g. on darwin 4.2.1 because it can't copy base obejcts properly
1374 BOOST_CHECK(t1.parameters().get_max_elements() == params.get_max_elements());
1375 BOOST_CHECK(t1.parameters().get_min_elements() == params.get_min_elements());
1376 BOOST_CHECK(t2.parameters().get_max_elements() == params.get_max_elements());
1377 BOOST_CHECK(t2.parameters().get_min_elements() == params.get_min_elements());
1380 t1.query(bgi::intersects(qbox), std::back_inserter(output));
1381 BOOST_CHECK(output.empty());
1384 t2.query(bgi::intersects(qbox), std::back_inserter(output));
1385 exactly_the_same_outputs(t2, output, expected_output);
1387 // those fails e.g. on darwin 4.2.1 because it can't copy base obejcts properly
1388 BOOST_CHECK(t1.parameters().get_max_elements() == params.get_max_elements());
1389 BOOST_CHECK(t1.parameters().get_min_elements() == params.get_min_elements());
1390 BOOST_CHECK(t2.parameters().get_max_elements() == params.get_max_elements());
1391 BOOST_CHECK(t2.parameters().get_min_elements() == params.get_min_elements());
1393 // moving constructor
1394 Rtree t3(boost::move(t1), tree.get_allocator());
1396 BOOST_CHECK(t3.size() == s);
1397 BOOST_CHECK(t1.size() == 0);
1398 BOOST_CHECK(t3.parameters().get_max_elements() == params.get_max_elements());
1399 BOOST_CHECK(t3.parameters().get_min_elements() == params.get_min_elements());
1402 t3.query(bgi::intersects(qbox), std::back_inserter(output));
1403 exactly_the_same_outputs(t3, output, expected_output);
1405 // moving assignment operator
1406 t1 = boost::move(t3);
1408 BOOST_CHECK(t1.size() == s);
1409 BOOST_CHECK(t3.size() == 0);
1410 BOOST_CHECK(t1.parameters().get_max_elements() == params.get_max_elements());
1411 BOOST_CHECK(t1.parameters().get_min_elements() == params.get_min_elements());
1414 t1.query(bgi::intersects(qbox), std::back_inserter(output));
1415 exactly_the_same_outputs(t1, output, expected_output);
1419 ::boost::ignore_unused_variable_warning(params);
1422 template <typename I, typename O>
1423 inline void my_copy(I first, I last, O out)
1425 for ( ; first != last ; ++first, ++out )
1429 // rtree creation and insertion
1431 template <typename Rtree, typename Value, typename Box>
1432 void create_insert(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
1434 std::vector<Value> expected_output;
1435 tree.query(bgi::intersects(qbox), std::back_inserter(expected_output));
1438 Rtree t(tree.parameters(), tree.indexable_get(), tree.value_eq(), tree.get_allocator());
1439 BOOST_FOREACH(Value const& v, input)
1441 BOOST_CHECK(tree.size() == t.size());
1442 std::vector<Value> output;
1443 t.query(bgi::intersects(qbox), std::back_inserter(output));
1444 exactly_the_same_outputs(t, output, expected_output);
1447 Rtree t(tree.parameters(), tree.indexable_get(), tree.value_eq(), tree.get_allocator());
1448 //std::copy(input.begin(), input.end(), bgi::inserter(t));
1449 my_copy(input.begin(), input.end(), bgi::inserter(t)); // to suppress MSVC warnings
1450 BOOST_CHECK(tree.size() == t.size());
1451 std::vector<Value> output;
1452 t.query(bgi::intersects(qbox), std::back_inserter(output));
1453 exactly_the_same_outputs(t, output, expected_output);
1456 Rtree t(input.begin(), input.end(), tree.parameters(), tree.indexable_get(), tree.value_eq(), tree.get_allocator());
1457 BOOST_CHECK(tree.size() == t.size());
1458 std::vector<Value> output;
1459 t.query(bgi::intersects(qbox), std::back_inserter(output));
1460 compare_outputs(t, output, expected_output);
1463 Rtree t(input, tree.parameters(), tree.indexable_get(), tree.value_eq(), tree.get_allocator());
1464 BOOST_CHECK(tree.size() == t.size());
1465 std::vector<Value> output;
1466 t.query(bgi::intersects(qbox), std::back_inserter(output));
1467 compare_outputs(t, output, expected_output);
1470 Rtree t(tree.parameters(), tree.indexable_get(), tree.value_eq(), tree.get_allocator());
1471 t.insert(input.begin(), input.end());
1472 BOOST_CHECK(tree.size() == t.size());
1473 std::vector<Value> output;
1474 t.query(bgi::intersects(qbox), std::back_inserter(output));
1475 exactly_the_same_outputs(t, output, expected_output);
1478 Rtree t(tree.parameters(), tree.indexable_get(), tree.value_eq(), tree.get_allocator());
1480 BOOST_CHECK(tree.size() == t.size());
1481 std::vector<Value> output;
1482 t.query(bgi::intersects(qbox), std::back_inserter(output));
1483 exactly_the_same_outputs(t, output, expected_output);
1487 Rtree t(tree.parameters(), tree.indexable_get(), tree.value_eq(), tree.get_allocator());
1488 BOOST_FOREACH(Value const& v, input)
1490 BOOST_CHECK(tree.size() == t.size());
1491 std::vector<Value> output;
1492 bgi::query(t, bgi::intersects(qbox), std::back_inserter(output));
1493 exactly_the_same_outputs(t, output, expected_output);
1496 Rtree t(tree.parameters(), tree.indexable_get(), tree.value_eq(), tree.get_allocator());
1497 bgi::insert(t, input.begin(), input.end());
1498 BOOST_CHECK(tree.size() == t.size());
1499 std::vector<Value> output;
1500 bgi::query(t, bgi::intersects(qbox), std::back_inserter(output));
1501 exactly_the_same_outputs(t, output, expected_output);
1504 Rtree t(tree.parameters(), tree.indexable_get(), tree.value_eq(), tree.get_allocator());
1505 bgi::insert(t, input);
1506 BOOST_CHECK(tree.size() == t.size());
1507 std::vector<Value> output;
1508 bgi::query(t, bgi::intersects(qbox), std::back_inserter(output));
1509 exactly_the_same_outputs(t, output, expected_output);
1515 template <typename Rtree, typename Box>
1516 void remove(Rtree const& tree, Box const& qbox)
1518 typedef typename Rtree::value_type Value;
1520 std::vector<Value> values_to_remove;
1521 tree.query(bgi::intersects(qbox), std::back_inserter(values_to_remove));
1522 BOOST_CHECK(0 < values_to_remove.size());
1524 std::vector<Value> expected_output;
1525 tree.query(bgi::disjoint(qbox), std::back_inserter(expected_output));
1526 size_t expected_removed_count = values_to_remove.size();
1527 size_t expected_size_after_remove = tree.size() - values_to_remove.size();
1529 // Add value which is not stored in the Rtree
1530 Value outsider = generate::value_outside<Rtree>();
1531 values_to_remove.push_back(outsider);
1536 BOOST_FOREACH(Value const& v, values_to_remove)
1538 BOOST_CHECK( r == expected_removed_count );
1539 std::vector<Value> output;
1540 t.query(bgi::disjoint(qbox), std::back_inserter(output));
1541 BOOST_CHECK( t.size() == expected_size_after_remove );
1542 BOOST_CHECK( output.size() == tree.size() - expected_removed_count );
1543 compare_outputs(t, output, expected_output);
1547 size_t r = t.remove(values_to_remove.begin(), values_to_remove.end());
1548 BOOST_CHECK( r == expected_removed_count );
1549 std::vector<Value> output;
1550 t.query(bgi::disjoint(qbox), std::back_inserter(output));
1551 BOOST_CHECK( t.size() == expected_size_after_remove );
1552 BOOST_CHECK( output.size() == tree.size() - expected_removed_count );
1553 compare_outputs(t, output, expected_output);
1557 size_t r = t.remove(values_to_remove);
1558 BOOST_CHECK( r == expected_removed_count );
1559 std::vector<Value> output;
1560 t.query(bgi::disjoint(qbox), std::back_inserter(output));
1561 BOOST_CHECK( t.size() == expected_size_after_remove );
1562 BOOST_CHECK( output.size() == tree.size() - expected_removed_count );
1563 compare_outputs(t, output, expected_output);
1569 BOOST_FOREACH(Value const& v, values_to_remove)
1570 r += bgi::remove(t, v);
1571 BOOST_CHECK( r == expected_removed_count );
1572 std::vector<Value> output;
1573 bgi::query(t, bgi::disjoint(qbox), std::back_inserter(output));
1574 BOOST_CHECK( t.size() == expected_size_after_remove );
1575 BOOST_CHECK( output.size() == tree.size() - expected_removed_count );
1576 compare_outputs(t, output, expected_output);
1580 size_t r = bgi::remove(t, values_to_remove.begin(), values_to_remove.end());
1581 BOOST_CHECK( r == expected_removed_count );
1582 std::vector<Value> output;
1583 bgi::query(t, bgi::disjoint(qbox), std::back_inserter(output));
1584 BOOST_CHECK( t.size() == expected_size_after_remove );
1585 BOOST_CHECK( output.size() == tree.size() - expected_removed_count );
1586 compare_outputs(t, output, expected_output);
1590 size_t r = bgi::remove(t, values_to_remove);
1591 BOOST_CHECK( r == expected_removed_count );
1592 std::vector<Value> output;
1593 bgi::query(t, bgi::disjoint(qbox), std::back_inserter(output));
1594 BOOST_CHECK( t.size() == expected_size_after_remove );
1595 BOOST_CHECK( output.size() == tree.size() - expected_removed_count );
1596 compare_outputs(t, output, expected_output);
1600 template <typename Rtree, typename Value, typename Box>
1601 void clear(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
1603 std::vector<Value> values_to_remove;
1604 tree.query(bgi::intersects(qbox), std::back_inserter(values_to_remove));
1605 BOOST_CHECK(0 < values_to_remove.size());
1611 std::vector<Value> expected_output;
1612 t.query(bgi::intersects(qbox), std::back_inserter(expected_output));
1613 size_t s = t.size();
1615 BOOST_CHECK(t.empty());
1616 BOOST_CHECK(t.size() == 0);
1618 BOOST_CHECK(t.size() == s);
1619 std::vector<Value> output;
1620 t.query(bgi::intersects(qbox), std::back_inserter(output));
1621 exactly_the_same_outputs(t, output, expected_output);
1625 template <typename Rtree, typename Value>
1626 void range(Rtree & tree, std::vector<Value> const& input)
1628 check_fwd_iterators(tree.begin(), tree.end());
1630 size_t count = std::distance(tree.begin(), tree.end());
1631 BOOST_CHECK(count == tree.size());
1632 BOOST_CHECK(count == input.size());
1634 count = std::distance(boost::begin(tree), boost::end(tree));
1635 BOOST_CHECK(count == tree.size());
1637 count = boost::size(tree);
1638 BOOST_CHECK(count == tree.size());
1641 BOOST_FOREACH(Value const& v, tree)
1643 boost::ignore_unused(v);
1646 BOOST_CHECK(count == tree.size());
1652 template <typename Rtree, typename Value, typename Box>
1653 void queries(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
1655 basictest::intersects(tree, input, qbox);
1656 basictest::disjoint(tree, input, qbox);
1657 basictest::covered_by(tree, input, qbox);
1658 basictest::overlaps(tree, input, qbox);
1659 //basictest::touches(tree, input, qbox);
1660 basictest::within(tree, input, qbox);
1661 basictest::contains(tree, input, qbox);
1662 basictest::covers(tree, input, qbox);
1664 typedef typename bg::point_type<Box>::type P;
1666 bg::centroid(qbox, pt);
1668 basictest::nearest_query_k(tree, input, pt, 10);
1669 basictest::nearest_query_not_found(tree, generate::outside_point<P>::apply());
1671 basictest::satisfies(tree, input);
1674 // rtree creation and modification
1676 template <typename Rtree, typename Value, typename Box>
1677 void modifiers(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
1679 basictest::copy_swap_move(tree, qbox);
1680 basictest::create_insert(tree, input, qbox);
1681 basictest::remove(tree, qbox);
1682 basictest::clear(tree, input, qbox);
1685 } // namespace basictest
1687 template <typename Value, typename Parameters, typename Allocator>
1688 void test_rtree_queries(Parameters const& parameters, Allocator const& allocator)
1690 typedef bgi::indexable<Value> I;
1691 typedef bgi::equal_to<Value> E;
1692 typedef typename Allocator::template rebind<Value>::other A;
1693 typedef bgi::rtree<Value, Parameters, I, E, A> Tree;
1694 typedef typename Tree::bounds_type B;
1696 Tree tree(parameters, I(), E(), allocator);
1697 std::vector<Value> input;
1700 generate::rtree(tree, input, qbox);
1702 basictest::queries(tree, input, qbox);
1704 Tree empty_tree(parameters, I(), E(), allocator);
1705 std::vector<Value> empty_input;
1707 basictest::queries(empty_tree, empty_input, qbox);
1710 template <typename Value, typename Parameters, typename Allocator>
1711 void test_rtree_modifiers(Parameters const& parameters, Allocator const& allocator)
1713 typedef bgi::indexable<Value> I;
1714 typedef bgi::equal_to<Value> E;
1715 typedef typename Allocator::template rebind<Value>::other A;
1716 typedef bgi::rtree<Value, Parameters, I, E, A> Tree;
1717 typedef typename Tree::bounds_type B;
1719 Tree tree(parameters, I(), E(), allocator);
1720 std::vector<Value> input;
1723 generate::rtree(tree, input, qbox);
1725 basictest::modifiers(tree, input, qbox);
1727 Tree empty_tree(parameters, I(), E(), allocator);
1728 std::vector<Value> empty_input;
1730 basictest::copy_swap_move(empty_tree, qbox);
1733 // run all tests for a single Algorithm and single rtree
1736 template <typename Value, typename Parameters, typename Allocator>
1737 void test_rtree_by_value(Parameters const& parameters, Allocator const& allocator)
1739 test_rtree_queries<Value>(parameters, allocator);
1740 test_rtree_modifiers<Value>(parameters, allocator);
1743 // rtree inserting and removing of counting_value
1745 template <typename Indexable, typename Parameters, typename Allocator>
1746 void test_count_rtree_values(Parameters const& parameters, Allocator const& allocator)
1748 typedef counting_value<Indexable> Value;
1750 typedef bgi::indexable<Value> I;
1751 typedef bgi::equal_to<Value> E;
1752 typedef typename Allocator::template rebind<Value>::other A;
1753 typedef bgi::rtree<Value, Parameters, I, E, A> Tree;
1754 typedef typename Tree::bounds_type B;
1756 Tree t(parameters, I(), E(), allocator);
1757 std::vector<Value> input;
1760 generate::rtree(t, input, qbox);
1762 size_t rest_count = input.size();
1764 BOOST_CHECK(t.size() + rest_count == Value::counter());
1766 std::vector<Value> values_to_remove;
1767 t.query(bgi::intersects(qbox), std::back_inserter(values_to_remove));
1769 rest_count += values_to_remove.size();
1771 BOOST_CHECK(t.size() + rest_count == Value::counter());
1773 size_t values_count = Value::counter();
1775 BOOST_FOREACH(Value const& v, values_to_remove)
1777 size_t r = t.remove(v);
1780 BOOST_CHECK(1 == r);
1781 BOOST_CHECK(Value::counter() == values_count);
1782 BOOST_CHECK(t.size() + rest_count == values_count);
1788 template <typename Indexable, typename Parameters, typename Allocator>
1789 void test_rtree_count(Parameters const& parameters, Allocator const& allocator)
1791 typedef std::pair<Indexable, int> Value;
1793 typedef bgi::indexable<Value> I;
1794 typedef bgi::equal_to<Value> E;
1795 typedef typename Allocator::template rebind<Value>::other A;
1796 typedef bgi::rtree<Value, Parameters, I, E, A> Tree;
1797 typedef typename Tree::bounds_type B;
1799 Tree t(parameters, I(), E(), allocator);
1800 std::vector<Value> input;
1803 generate::rtree(t, input, qbox);
1805 BOOST_CHECK(t.count(input[0]) == 1);
1806 BOOST_CHECK(t.count(input[0].first) == 1);
1810 BOOST_CHECK(t.count(input[0]) == 2);
1811 BOOST_CHECK(t.count(input[0].first) == 2);
1813 t.insert(std::make_pair(input[0].first, -1));
1815 BOOST_CHECK(t.count(input[0]) == 2);
1816 BOOST_CHECK(t.count(input[0].first) == 3);
1821 template <typename Value, typename Parameters, typename Allocator>
1822 void test_rtree_bounds(Parameters const& parameters, Allocator const& allocator)
1824 typedef bgi::indexable<Value> I;
1825 typedef bgi::equal_to<Value> E;
1826 typedef typename Allocator::template rebind<Value>::other A;
1827 typedef bgi::rtree<Value, Parameters, I, E, A> Tree;
1828 typedef typename Tree::bounds_type B;
1829 //typedef typename bg::traits::point_type<B>::type P;
1831 Tree t(parameters, I(), E(), allocator);
1832 std::vector<Value> input;
1836 bg::assign_inverse(b);
1838 BOOST_CHECK(bg::equals(t.bounds(), b));
1840 generate::rtree(t, input, qbox);
1842 b = bgi::detail::rtree::values_box<B>(input.begin(), input.end(), t.indexable_get());
1844 BOOST_CHECK(bg::equals(t.bounds(), b));
1845 BOOST_CHECK(bg::equals(t.bounds(), bgi::bounds(t)));
1847 size_t s = input.size();
1848 while ( s/2 < input.size() && !input.empty() )
1850 t.remove(input.back());
1854 b = bgi::detail::rtree::values_box<B>(input.begin(), input.end(), t.indexable_get());
1856 BOOST_CHECK(bg::equals(t.bounds(), b));
1859 BOOST_CHECK(bg::equals(t2.bounds(), b));
1862 BOOST_CHECK(bg::equals(t2.bounds(), b));
1864 t2 = boost::move(t);
1865 BOOST_CHECK(bg::equals(t2.bounds(), b));
1869 bg::assign_inverse(b);
1870 BOOST_CHECK(bg::equals(t.bounds(), b));
1873 // test rtree iterator
1875 template <typename Indexable, typename Parameters, typename Allocator>
1876 void test_rtree_range(Parameters const& parameters, Allocator const& allocator)
1878 typedef std::pair<Indexable, int> Value;
1880 typedef bgi::indexable<Value> I;
1881 typedef bgi::equal_to<Value> E;
1882 typedef typename Allocator::template rebind<Value>::other A;
1883 typedef bgi::rtree<Value, Parameters, I, E, A> Tree;
1884 typedef typename Tree::bounds_type B;
1886 Tree t(parameters, I(), E(), allocator);
1887 std::vector<Value> input;
1890 generate::rtree(t, input, qbox);
1892 basictest::range(t, input);
1893 basictest::range((Tree const&)t, input);
1896 template <typename Indexable, typename Parameters, typename Allocator>
1897 void test_rtree_additional(Parameters const& parameters, Allocator const& allocator)
1899 test_count_rtree_values<Indexable>(parameters, allocator);
1900 test_rtree_count<Indexable>(parameters, allocator);
1901 test_rtree_bounds<Indexable>(parameters, allocator);
1902 test_rtree_range<Indexable>(parameters, allocator);
1905 // run all tests for one Algorithm for some number of rtrees
1906 // defined by some number of Values constructed from given Point
1908 template<typename Point, typename Parameters, typename Allocator>
1909 void test_rtree_for_point(Parameters const& parameters, Allocator const& allocator)
1911 typedef std::pair<Point, int> PairP;
1912 typedef boost::tuple<Point, int, int> TupleP;
1913 typedef boost::shared_ptr< test_object<Point> > SharedPtrP;
1914 typedef value_no_dctor<Point> VNoDCtor;
1916 test_rtree_by_value<Point, Parameters>(parameters, allocator);
1917 test_rtree_by_value<PairP, Parameters>(parameters, allocator);
1918 test_rtree_by_value<TupleP, Parameters>(parameters, allocator);
1920 test_rtree_by_value<SharedPtrP, Parameters>(parameters, allocator);
1921 test_rtree_by_value<VNoDCtor, Parameters>(parameters, allocator);
1923 test_rtree_additional<Point>(parameters, allocator);
1925 #if !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
1926 typedef std::tuple<Point, int, int> StdTupleP;
1927 test_rtree_by_value<StdTupleP, Parameters>(parameters, allocator);
1931 template<typename Point, typename Parameters, typename Allocator>
1932 void test_rtree_for_box(Parameters const& parameters, Allocator const& allocator)
1934 typedef bg::model::box<Point> Box;
1935 typedef std::pair<Box, int> PairB;
1936 typedef boost::tuple<Box, int, int> TupleB;
1937 typedef value_no_dctor<Box> VNoDCtor;
1939 test_rtree_by_value<Box, Parameters>(parameters, allocator);
1940 test_rtree_by_value<PairB, Parameters>(parameters, allocator);
1941 test_rtree_by_value<TupleB, Parameters>(parameters, allocator);
1943 test_rtree_by_value<VNoDCtor, Parameters>(parameters, allocator);
1945 test_rtree_additional<Box>(parameters, allocator);
1947 #if !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
1948 typedef std::tuple<Box, int, int> StdTupleB;
1949 test_rtree_by_value<StdTupleB, Parameters>(parameters, allocator);
1953 template<typename Point, typename Parameters>
1954 void test_rtree_for_point(Parameters const& parameters)
1956 test_rtree_for_point<Point>(parameters, std::allocator<int>());
1959 template<typename Point, typename Parameters>
1960 void test_rtree_for_box(Parameters const& parameters)
1962 test_rtree_for_box<Point>(parameters, std::allocator<int>());
1967 template<typename Indexable, typename Parameters, typename Allocator>
1968 void modifiers(Parameters const& parameters, Allocator const& allocator)
1970 typedef std::pair<Indexable, int> Pair;
1971 typedef boost::tuple<Indexable, int, int> Tuple;
1972 typedef boost::shared_ptr< test_object<Indexable> > SharedPtr;
1973 typedef value_no_dctor<Indexable> VNoDCtor;
1975 test_rtree_modifiers<Indexable>(parameters, allocator);
1976 test_rtree_modifiers<Pair>(parameters, allocator);
1977 test_rtree_modifiers<Tuple>(parameters, allocator);
1979 test_rtree_modifiers<SharedPtr>(parameters, allocator);
1980 test_rtree_modifiers<VNoDCtor>(parameters, allocator);
1982 #if !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
1983 typedef std::tuple<Indexable, int, int> StdTuple;
1984 test_rtree_modifiers<StdTuple>(parameters, allocator);
1988 template<typename Indexable, typename Parameters, typename Allocator>
1989 void queries(Parameters const& parameters, Allocator const& allocator)
1991 typedef std::pair<Indexable, int> Pair;
1992 typedef boost::tuple<Indexable, int, int> Tuple;
1993 typedef boost::shared_ptr< test_object<Indexable> > SharedPtr;
1994 typedef value_no_dctor<Indexable> VNoDCtor;
1996 test_rtree_queries<Indexable>(parameters, allocator);
1997 test_rtree_queries<Pair>(parameters, allocator);
1998 test_rtree_queries<Tuple>(parameters, allocator);
2000 test_rtree_queries<SharedPtr>(parameters, allocator);
2001 test_rtree_queries<VNoDCtor>(parameters, allocator);
2003 #if !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
2004 typedef std::tuple<Indexable, int, int> StdTuple;
2005 test_rtree_queries<StdTuple>(parameters, allocator);
2009 template<typename Indexable, typename Parameters, typename Allocator>
2010 void additional(Parameters const& parameters, Allocator const& allocator)
2012 test_rtree_additional<Indexable, Parameters>(parameters, allocator);
2015 } // namespace testset
2017 #endif // BOOST_GEOMETRY_INDEX_TEST_RTREE_HPP