Skip to content

Instantly share code, notes, and snippets.

@usagi
Created August 18, 2011 11:11
Show Gist options
  • Save usagi/1153853 to your computer and use it in GitHub Desktop.
Save usagi/1153853 to your computer and use it in GitHub Desktop.
R-Tree sample using boost::geometry::index::rtree
#include <boost/geometry.hpp>
#include <boost/geometry/extensions/index/rtree/rtree.hpp>
#include <random>
#include <iostream>
#include <exception>
int main() try{
typedef char value;
typedef double coordinate_type;
//typedef boost::geometry::model::d2::point_xy<coordinate_type> point;
const size_t dimension = 2;
typedef boost::geometry::cs::cartesian coordinate_system_type;
typedef boost::geometry::model::point<coordinate_type, dimension, coordinate_system_type> point;
typedef boost::geometry::model::box<point> box;
typedef boost::geometry::index::rtree<box, value> rtree;
std::mt19937_64 random_engine;
std::uniform_real_distribution<coordinate_type> random_distribution_coordinate(0.0, 1.0);
std::uniform_int_distribution<value> random_distribution_value('A', 'Z');
auto random_point = [&](){return point(random_distribution_coordinate(random_engine), random_distribution_coordinate(random_engine));};
auto random_value = [&](){return random_distribution_value(random_engine);};
rtree r(16,4);
r.print();
for(auto n = 0; n < 10; ++n)
r.insert( box(random_point(), random_point()), random_value() );
r.print();
std::cerr<<"---[find]---\n";
for(const auto& v: r.find(box(point(0.25, 0.25), point(0.75, 0.75))))
std::cerr<<v<<"\n";
}catch(std::exception e){
std::cerr<<e.what();
}
@usagi
Copy link
Author

usagi commented Aug 18, 2011

// gcc 4.6.1
// boost 1.47

Min/Max: 4 / 16
Leaves: 0
--> Node --------
Address: 0x1011010
Level: 1
Size: 0
|
--< Node --------

Children:

Min/Max: 4 / 16
Leaves: 10
--> Node --------
Address: 0x1011010
Level: 1
Size: 1
| ( 0.0192711 , 0.164833 ) x ( 0.967825 , 0.98152 ) |
--< Node --------
Children:
- -> Leaf --------
Size: 10
| ( 0.0192711 , 0.94666 8 ) x ( 0.710671 , 0.25048 ) -> U |
| ( 0.34467 , 0.520643 ) x ( 0.0227124 , 0.251318 ) -> K |
| ( 0.521916 , 0.543856 ) x ( 0.140039 , 0.561032 ) -> H |
| ( 0.249168 , 0.744281 ) x ( 0.41937 , 0.499774 ) -> W |
| ( 0.245533 , 0.164833 ) x ( 0.910501 , 0.320065 ) -> G |
| ( 0.0807073 , 0.76943 2 ) x ( 0.967825 , 0.715893 ) -> F |
| ( 0.9503 , 0.583883 ) x ( 0.777046 , 0.257262 ) -> L |
| ( 0.0455399 , 0.25635 9 ) x ( 0.532405 , 0.322289 ) -> L |
| ( 0.0309147 , 0.90712 ) x ( 0.0912189 , 0.696239 ) -> N |
| ( 0.361325 , 0.298767 ) x ( 0.620412 , 0.98152 ) -> D |

- -< Leaf --------

---[find]---
W
G
L
D

@awulkiew
Copy link

In the above code, old/test/preliminary version of the R-tree is used. Moreover to compile it you need to use the code from Boost.Geometry develop branch (or SVN trunk), notice the 'extensions' in the includes.

Boost.Geometry is now shipped with different R-tree implementation, see the docs: http://www.boost.org/libs/geometry or the repo: http://github.com/boostorg/geometry

@baibhavr
Copy link

I'm not sure if this is the right question to ask to, but how can I remove the point once I insert them? Example would be appreciated.

@jokoon
Copy link

jokoon commented May 19, 2016

Thanks a lot for this sample! I hope nothing changed and that sample still works...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment