Created
August 18, 2011 11:11
-
-
Save usagi/1153853 to your computer and use it in GitHub Desktop.
R-Tree sample using boost::geometry::index::rtree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} |
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
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.
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
// 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