1 // Boost.Geometry Index
2 //
3 // Quickbook Examples
4 //
5 // Copyright (c) 2011-2014 Adam Wulkiewicz, Lodz, Poland.
6 //
7 // Use, modification and distribution is subject to the Boost Software License,
8 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10
11 //[rtree_range_adaptors
12
13 #include <boost/geometry.hpp>
14 #include <boost/geometry/geometries/point.hpp>
15 #include <boost/geometry/geometries/box.hpp>
16
17 #include <boost/geometry/index/rtree.hpp>
18
19 // Boost.Range
20 #include <boost/range.hpp>
21 // adaptors
22 #include <boost/range/adaptor/indexed.hpp>
23 #include <boost/range/adaptor/transformed.hpp>
24
25 // a container
26 #include <vector>
27
28 // just for output
29 #include <iostream>
30
31 namespace bg = boost::geometry;
32 namespace bgi = boost::geometry::index;
33
34 // Define a function object converting a value_type of indexed Range into std::pair<>.
35 // This is a generic implementation but of course it'd be possible to use some
36 // specific types. One could also take Value as template parameter and access
37 // first_type and second_type members, etc.
38 template <typename First, typename Second>
39 struct pair_maker
40 {
41 typedef std::pair<First, Second> result_type;
42 template<typename T>
operator ()pair_maker43 inline result_type operator()(T const& v) const
44 {
45 return result_type(v.value(), v.index());
46 }
47 };
48
main()49 int main()
50 {
51 typedef bg::model::point<float, 2, bg::cs::cartesian> point;
52 typedef bg::model::box<point> box;
53
54 typedef std::vector<box> container;
55 typedef container::size_type size_type;
56
57 typedef std::pair<box, size_type> value;
58
59 // create a container of boxes
60 container boxes;
61 for ( size_type i = 0 ; i < 10 ; ++i )
62 {
63 // add a box into the container
64 box b(point(i + 0.0f, i + 0.0f), point(i + 0.5f, i + 0.5f));
65 boxes.push_back(b);
66 }
67
68 // create the rtree passing a Range
69 bgi::rtree< value, bgi::quadratic<16> >
70 rtree(boxes | boost::adaptors::indexed()
71 | boost::adaptors::transformed(pair_maker<box, size_type>()));
72
73 // print the number of values using boxes[0] as indexable
74 std::cout << rtree.count(boxes[0]) << std::endl;
75
76 return 0;
77 }
78
79 //]
80