creation.qbk 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. [/============================================================================
  2. Boost.Geometry Index
  3. Copyright (c) 2011-2012 Adam Wulkiewicz.
  4. Use, modification and distribution is subject to the Boost Software License,
  5. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================/]
  8. [section Creation and Modification]
  9. [h4 Template parameters]
  10. __rtree__ has 5 parameters but only 2 are required:
  11. rtree<Value,
  12. Parameters,
  13. IndexableGetter = index::indexable<Value>,
  14. EqualTo = index::equal_to<Value>,
  15. Allocator = std::allocator<Value> >
  16. * `__value__` - type of object which will be stored in the container,
  17. * `Parameters` - parameters type, inserting/splitting algorithm,
  18. * `IndexableGetter` - function object translating `__value__` to `__indexable__` (`__point__` or `__box__`) which __rtree__ can handle,
  19. * `EqualTo` - function object comparing `__value__`s,
  20. * `Allocator` - `Value`s allocator, all allocators needed by the container are created from it.
  21. [h4 Values and Indexables]
  22. __rtree__ may store `__value__`s of any type as long as passed function objects know how to interpret those `__value__`s, that is
  23. extract an `__indexable__` that the __rtree__ can handle and compare `__value__`s.
  24. The `__indexable__` is a type adapted to Point, Box or Segment concept.
  25. The examples of rtrees storing `__value__`s translatable to various `__indexable__`s are presented below.
  26. [table
  27. [[rtree<Point, ...>] [rtree<Box, ...>] [rtree<Segment, ...>]]
  28. [[[$img/index/rtree/rtree_pt.png]] [[$img/index/rtree/rstar.png]] [[$img/index/rtree/rtree_seg.png]]]
  29. ]
  30. By default function objects `index::indexable<Value>` and `index::equal_to<Value>` are defined for some typically used `__value__`
  31. types which may be stored without defining any additional classes. By default the rtree may store pure `__indexable__`s, pairs
  32. and tuples. In the case of those two collection types, the `__indexable__` must be the first stored type.
  33. * `__indexable__ = __point__ | __box__ | Segment`
  34. * `__value__ = Indexable | std::pair<__indexable__, T> | boost::tuple<__indexable__, ...> [ | std::tuple<__indexable__, ...> ]`
  35. By default `boost::tuple<...>` is supported on all compilers. If the compiler supports C++11 tuples and variadic templates
  36. then `std::tuple<...>` may be used "out of the box" as well.
  37. Examples of default `__value__` types:
  38. geometry::model::point<...>
  39. geometry::model::point_xy<...>
  40. geometry::model::box<...>
  41. geometry::model::segment<...>
  42. std::pair<geometry::model::box<...>, unsigned>
  43. boost::tuple<geometry::model::point<...>, int, float>
  44. The predefined `index::indexable<Value>` returns const reference to the `__indexable__` stored in the `__value__`.
  45. [important The translation is done quite frequently inside the container - each time the rtree needs it. ]
  46. The predefined `index::equal_to<Value>`:
  47. * for `__point__`, `__box__` and `Segment` - compares `__value__`s with geometry::equals().
  48. * for `std::pair<...>` - compares both components of the `__value__`. The first value stored in the pair is compared before the second one.
  49. If the value stored in the pair is a Geometry, `geometry::equals()` is used. For other types it uses `operator==()`.
  50. * for `tuple<...>` - compares all components of the `__value__`. If the component is a `Geometry`, `geometry::equals()`
  51. function is used. For other types it uses `operator==()`.
  52. [h4 Balancing algorithms compile-time parameters]
  53. `__value__`s may be inserted to the __rtree__ in many various ways. Final internal structure
  54. of the __rtree__ depends on algorithms used in the insertion process and parameters. The most important is
  55. nodes' balancing algorithm. Currently, three well-known types of R-trees may be created.
  56. Linear - classic __rtree__ using balancing algorithm of linear complexity
  57. index::rtree< __value__, index::linear<16> > rt;
  58. Quadratic - classic __rtree__ using balancing algorithm of quadratic complexity
  59. index::rtree< __value__, index::quadratic<16> > rt;
  60. R*-tree - balancing algorithm minimizing nodes' overlap with forced reinsertions
  61. index::rtree< __value__, index::rstar<16> > rt;
  62. [h4 Balancing algorithms run-time parameters]
  63. Balancing algorithm parameters may be passed to the __rtree__ in run-time.
  64. To use run-time versions of the __rtree__ one may pass parameters which
  65. names start with `dynamic_`.
  66. // linear
  67. index::rtree<__value__, index::dynamic_linear> rt(index::dynamic_linear(16));
  68. // quadratic
  69. index::rtree<__value__, index::dynamic_quadratic> rt(index::dynamic_quadratic(16));
  70. // rstar
  71. index::rtree<__value__, index::dynamic_rstar> rt(index::dynamic_rstar(16));
  72. The obvious drawback is a slightly slower __rtree__.
  73. [h4 Non-default parameters]
  74. Non-default R-tree parameters are described in the reference.
  75. [h4 Copying, moving and swapping]
  76. The __rtree__ is copyable and movable container. Move semantics is implemented using Boost.Move library
  77. so it's possible to move the container on a compilers without rvalue references support.
  78. // default constructor
  79. index::rtree< __value__, index::rstar<8> > rt1;
  80. // copy constructor
  81. index::rtree< __value__, index::rstar<8> > rt2(r1);
  82. // copy assignment
  83. rt2 = r1;
  84. // move constructor
  85. index::rtree< __value__, index::rstar<8> > rt3(boost::move(rt1));
  86. // move assignment
  87. rt3 = boost::move(rt2);
  88. // swap
  89. rt3.swap(rt2);
  90. [h4 Inserting and removing Values]
  91. The following code creates an __rtree__ using quadratic balancing algorithm.
  92. using namespace boost::geometry;
  93. typedef std::pair<Box, int> __value__;
  94. index::rtree< __value__, index::quadratic<16> > rt;
  95. To insert or remove a `__value__' by method call one may use the following
  96. code.
  97. __value__ v = std::make_pair(__box__(...), 0);
  98. rt.insert(v);
  99. rt.remove(v);
  100. To insert or remove a `__value__' by function call one may use the following
  101. code.
  102. __value__ v = std::make_pair(__box__(...), 0);
  103. index::insert(rt, v);
  104. index::remove(rt, v);
  105. Typically you will perform those operations in a loop in order to e.g. insert
  106. some number of `__value__`s corresponding to geometrical objects (e.g. `Polygons`)
  107. stored in another container.
  108. [h4 Additional interface]
  109. The __rtree__ allows creation, inserting and removing of Values from a range. The range may be passed as
  110. `[first, last)` Iterators pair or as a Range adapted to one of the Boost.Range Concepts.
  111. namespace bgi = boost::geometry::index;
  112. typedef std::pair<Box, int> __value__;
  113. typedef bgi::rtree< __value__, bgi::linear<32> > RTree;
  114. std::vector<__value__> values;
  115. /* vector filling code, here */
  116. // create R-tree with default constructor and insert values with insert(Value const&)
  117. RTree rt1;
  118. BOOST_FOREACH(__value__ const& v, values)
  119. rt1.insert(v);
  120. // create R-tree with default constructor and insert values with insert(Iter, Iter)
  121. RTree rt2;
  122. rt2.insert(values.begin(), values.end());
  123. // create R-tree with default constructor and insert values with insert(Range)
  124. RTree rt3;
  125. rt3.insert(values_range);
  126. // create R-tree with constructor taking Iterators
  127. RTree rt4(values.begin(), values.end());
  128. // create R-tree with constructor taking Range
  129. RTree rt5(values_range);
  130. // remove values with remove(Value const&)
  131. BOOST_FOREACH(__value__ const& v, values)
  132. rt1.remove(v);
  133. // remove values with remove(Iter, Iter)
  134. rt2.remove(values.begin(), values.end());
  135. // remove values with remove(Range)
  136. rt3.remove(values_range);
  137. Furthermore, it's possible to pass a Range adapted by one of the Boost.Range adaptors into the rtree (more complete example can be found in the *Examples* section).
  138. // create Rtree containing `std::pair<Box, int>` from a container of Boxes on the fly.
  139. RTree rt6(boxes | boost::adaptors::indexed()
  140. | boost::adaptors::transformed(pair_maker()));
  141. [h4 Insert iterator]
  142. There are functions like `std::copy()`, or __rtree__'s queries that copy values to an output iterator.
  143. In order to insert values to a container in this kind of function insert iterators may be used.
  144. Geometry.Index provide its own `bgi::insert_iterator<Container>` which is generated by
  145. `bgi::inserter()` function.
  146. namespace bgi = boost::geometry::index;
  147. typedef std::pair<Box, int> __value__;
  148. typedef bgi::rtree< __value__, bgi::linear<32> > RTree;
  149. std::vector<__value__> values;
  150. /* vector filling code, here */
  151. // create R-tree and insert values from the vector
  152. RTree rt1;
  153. std::copy(values.begin(), values.end(), bgi::inserter(rt1));
  154. // create R-tree and insert values returned by a query
  155. RTree rt2;
  156. rt1.spatial_query(Box(/*...*/), bgi::inserter(rt2));
  157. [endsect] [/ Creation and Modification /]