Last active
August 29, 2015 13:58
-
-
Save logc/10272165 to your computer and use it in GitHub Desktop.
kNN with Boost::Geometry
This file contains 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 <functional> | |
#include <iostream> | |
#include <random> | |
#include <boost/geometry.hpp> | |
#include <boost/geometry/geometries/point.hpp> | |
#include <boost/geometry/index/rtree.hpp> | |
namespace bg = boost::geometry; | |
namespace bgi = boost::geometry::index; | |
typedef bg::model::point<float, 2, bg::cs::cartesian> point; | |
typedef bg::model::box<point> box; | |
typedef std::pair<point, unsigned> value; | |
int main(int argc, char *argv[]) | |
{ | |
bgi::rtree< value, bgi::quadratic<16> > rtree; | |
// create some values | |
std::default_random_engine generator; | |
std::uniform_real_distribution<float> distribution(0, 10); | |
auto next_rand = std::bind ( distribution, generator ); | |
float x, y; | |
for ( unsigned i = 0 ; i < 100000 ; ++i ) | |
{ | |
x = next_rand(); | |
y = next_rand(); | |
point p = point(x, y); | |
rtree.insert(std::make_pair(p, i)); | |
} | |
// search for nearest neighbours | |
std::vector<value> returned_values; | |
point sought = point(5, 5); | |
box bounding_box(point(5 - 2, 5 - 2), point(5 + 2, 5 + 2)); | |
rtree.query( | |
bgi::within(bounding_box) && | |
bgi::satisfies([&](value const& v) {return bg::distance(v.first, sought) < 2;}), | |
std::back_inserter(returned_values)); | |
// print returned values | |
std::cout << returned_values.size() << std::endl; | |
//value to_print_out; | |
//for (size_t i = 0; i < returned_values.size(); i++) { | |
//to_print_out = returned_values[i]; | |
//float x = to_print_out.first.get<0>(); | |
//float y = to_print_out.first.get<1>(); | |
//std::cout << "Select point: " << to_print_out.second << std::endl; | |
//std::cout << "x: " << x << ", y: " << y << std::endl; | |
//} | |
return 0; | |
} |
That's a nice example how one can find points within a circle combining the predicates!
But to be perfectly clear. The example doesn't present the kNN (k Nearest Neighbors) query but a spatial query. The kNN query returns some number of values nearest to the sought point. The spatial query returns ALL points for which some spatial predicate is met. Like in this case, all values within a circle centered at point(5, 5)
are returned.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Look at my fork, for more appropriate testing: https://gist.github.com/weidenrinde/89e835611721f0c1d907