I seem to get incorrect results when using boost to query for points. Using a separate algorithm (using BSTs), I get an answer of 2000 points on average for each query but using boost I get an answer of 10 points. This is the fist time I am using Boost, so can some one help me on what I am messing up.
About the code: I put 1M random points (with x,y between 0 and 1). Then I run queries for 100 small random regions and count number of matches.
#include <iostream>
#include <vector>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/index/rtree.hpp>
using namespace std;
namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;
typedef bg::model::point<double, 2, bg::cs::cartesian> point;
typedef bg::model::box<point> box;
struct particle
{
double x;
double y;
};
int main(int argc, char *argv[])
{
int N=1000000;
clock_t start,stop;
vector<particle> myvec(N);
vector<particle>::iterator cii;
//Set vector values
for (cii = myvec.begin(); cii != myvec.end(); ++cii)
{
cii->x =1.0*rand()/RAND_MAX;
cii->y =1.0*rand()/RAND_MAX;
}
//Build R-tree
start=clock();
bgi::rtree<point, bgi::quadratic<16> > rtree;
for (cii=myvec.begin();cii!=myvec.end(); ++cii)
{
double x = cii->x;
double y = cii->y;
point p(x,y);
rtree.insert(p);
}
stop=clock();
cout<<"Time for building R tree "<<(stop-start)/(double) CLOCKS_PER_SEC<<endl;
//Build Query List
vector<particle> querylist(100);
for (cii = querylist.begin(); cii != querylist.end(); ++cii)
{
cii->x =1.0*rand()/RAND_MAX;
cii->y =1.0*rand()/RAND_MAX;
}
//Query R-tree
start=clock();
for (cii = querylist.begin(); cii != querylist.end(); ++cii)
{
double x = cii->x;
double y = cii->y;
double lx = x - .001;
double ux = x + .001;
double ly = y - .001;
double uy = y + .001;
point p1(lx,ly), p2(ux,uy);
box query_box(p1, p2);
vector<point> queryresult;
rtree.query(bgi::intersects(query_box), std::back_inserter(queryresult));
std::cout << "The intersection has " << (queryresult.size()) << " elements:\n";
}
stop=clock();
cout<<"Time for query "<<(stop-start)/(double) CLOCKS_PER_SEC<<endl;
return 0;
}
Your sample is not statistically sound.
Let's make the query boxes significantly larger:
Results in exactly the expected number of hits:
Live On Coliru
Code
Notes
rand()
with Boost Random (uniform_real<>
distribution)instead of filling a vector each time, just measure the number of query results:
instead of "anecdotally" reporting some "intersection" counts, do some proper statistics using Boost Accumulator:
Full Listing
Scaling
Note that the results scale:
And even