]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Boost.Geometry Index |
2 | // | |
3 | // Quickbook Examples | |
4 | // | |
5 | // Copyright (c) 2011-2013 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_interprocess | |
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 | #include <boost/foreach.hpp> | |
20 | ||
21 | #include <boost/interprocess/managed_shared_memory.hpp> | |
22 | #include <boost/interprocess/allocators/allocator.hpp> | |
23 | ||
24 | #include <vector> | |
25 | #include <string> | |
26 | #include <cstdlib> //std::system | |
27 | ||
28 | //For parent process argc == 1, for child process argc > 1 | |
29 | int main(int argc, char *argv[]) | |
30 | { | |
31 | using namespace boost::interprocess; | |
32 | namespace bg = boost::geometry; | |
33 | namespace bgm = bg::model; | |
34 | namespace bgi = bg::index; | |
35 | ||
36 | typedef bgm::point<float, 2, bg::cs::cartesian> P; | |
37 | typedef bgm::box<P> B; | |
38 | ||
39 | typedef bgi::linear<32, 8> Par; | |
40 | typedef bgi::indexable<B> I; | |
41 | typedef bgi::equal_to<B> E; | |
42 | typedef allocator<B, managed_shared_memory::segment_manager> Alloc; | |
43 | typedef bgi::rtree<B, Par, I, E, Alloc> Rtree; | |
44 | ||
45 | //Parent process | |
46 | if ( argc == 1 ) | |
47 | { | |
48 | struct shm_remove | |
49 | { | |
50 | shm_remove() { shared_memory_object::remove("MySharedMemory"); } | |
51 | ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); } | |
52 | } remover; | |
53 | ||
54 | managed_shared_memory segment(create_only, "MySharedMemory", 65536); | |
55 | ||
56 | std::cout << "Parent: Constructing container\n"; | |
57 | ||
58 | Rtree * tree = segment.construct<Rtree>("Rtree")(Par(), I(), E(), Alloc(segment.get_segment_manager())); | |
59 | ||
60 | std::cout << "Parent: Filling container with 100 boxes\n"; | |
61 | ||
62 | for ( float i = 0 ; i < 100 ; i += 1 ) | |
63 | tree->insert(B(P(i, i), P(i+0.5f, i+0.5f))); | |
64 | ||
65 | std::cout << "Parent: Tree content\n"; | |
66 | Rtree::bounds_type bb = tree->bounds(); | |
67 | std::cout << "[(" << bg::get<0>(bb.min_corner()) << ", " << bg::get<1>(bb.min_corner()) | |
68 | << ")(" << bg::get<0>(bb.max_corner()) << ", " << bg::get<1>(bb.max_corner()) << ")]\n"; | |
69 | ||
70 | std::cout << "Parent: Running child process\n"; | |
71 | ||
72 | std::string s(argv[0]); s += " child "; | |
73 | if ( 0 != std::system(s.c_str()) ) | |
74 | return 1; | |
75 | ||
76 | if ( segment.find<Rtree>("Rtree").first ) | |
77 | return 1; | |
78 | ||
79 | std::cout << "Parent: Container was properly destroyed by the child process\n"; | |
80 | } | |
81 | //Child process | |
82 | else | |
83 | { | |
84 | managed_shared_memory segment(open_only, "MySharedMemory"); | |
85 | ||
86 | std::cout << "Child: Searching of the container in shared memory\n"; | |
87 | ||
88 | Rtree * tree = segment.find<Rtree>("Rtree").first; | |
89 | ||
90 | std::cout << "Child: Querying for objects intersecting box = [(45, 45)(55, 55)]\n"; | |
91 | ||
92 | std::vector<B> result; | |
93 | unsigned k = tree->query(bgi::intersects(B(P(45, 45), P(55, 55))), std::back_inserter(result)); | |
94 | ||
95 | std::cout << "Child: Found objects:\n"; | |
96 | std::cout << k << "\n"; | |
97 | BOOST_FOREACH(B const& b, result) | |
98 | { | |
99 | std::cout << "[(" << bg::get<0>(b.min_corner()) << ", " << bg::get<1>(b.min_corner()) | |
100 | << ")(" << bg::get<0>(b.max_corner()) << ", " << bg::get<1>(b.max_corner()) << ")]\n"; | |
101 | } | |
102 | std::cout << "\n"; | |
103 | ||
104 | std::cout << "Child: Destroying container\n"; | |
105 | ||
106 | segment.destroy<Rtree>("Rtree"); | |
107 | } | |
108 | ||
109 | return 0; | |
110 | }; | |
111 | ||
112 | //] |