Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include <boost/geometry.hpp>
  4. #include <boost/geometry/geometries/point.hpp>
  5. #include <boost/geometry/index/rtree.hpp>
  6.  
  7.  
  8. namespace bg = boost::geometry;
  9. namespace bgi = boost::geometry::index;
  10.  
  11. typedef bg::model::point<float, 2, bg::cs::cartesian> point;
  12. typedef std::pair<point, unsigned> value;
  13.  
  14. int main(int argc, char *argv[])
  15. {
  16. bgi::rtree< value, bgi::quadratic<16> > rtree;
  17.  
  18. // create some values
  19. for ( unsigned i = 0 ; i < 10 ; ++i )
  20. {
  21. point p = point(i, i);
  22. rtree.insert(std::make_pair(p, i));
  23. }
  24.  
  25. // search for nearest neighbours
  26. std::vector<value> returned_values;
  27. point sought = point(5, 5);
  28. rtree.query(bgi::satisfies([&](value const& v) {return bg::distance(v.first, sought) < 2;}),
  29. std::back_inserter(returned_values));
  30.  
  31. // print returned values
  32. value to_print_out;
  33. for (size_t i = 0; i < returned_values.size(); i++) {
  34. to_print_out = returned_values[i];
  35. float x = to_print_out.first.get<0>();
  36. float y = to_print_out.first.get<1>();
  37. std::cout << "Select point: " << to_print_out.second << std::endl;
  38. std::cout << "x: " << x << ", y: " << y << std::endl;
  39. }
  40.  
  41. return 0;
  42. }
  43.  
  44. $ c++ -std=c++11 -I/opt/local/include -L/opt/local/lib main.cpp -o geom && ./geom
  45. Select point: 4
  46. x: 4, y: 4
  47. Select point: 5
  48. x: 5, y: 5
  49. Select point: 6
  50. x: 6, y: 6
  51.  
  52. for ( Rtree::const_query_iterator it = tree.qbegin(bgi::nearest(pt, 10000)) ;
  53. it != tree.qend() ; ++it )
  54. {
  55. // do something with value
  56. if ( has_enough_nearest_values() )
  57. break;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement