Use boost r-tree to find the nearest point

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/index/rtree.hpp>

using PointType = boost::geometry::model::point<float,3,boost::geometry::cs::cartesian>;

// Build the r-tree
boost::geometry::index::rtree< PointType,boost::geometry::index::quadratic<16> > rtree;
for(...) {
  rtree.insert( PointType( x, y, z ) );
}

// Find the nearest point
PointType roiPoint( x, y, z );
std::vector< PointType > nearestPoints;
rtree.query( boost::geometry::index::nearest( roiPoint, 1 ), std::back_inserter( nearestPoints ) );
const auto nearest = nearestPoints[0];
std::cout << nearest.get<0>() << ", "<< nearest.get<1>() << ", " << nearest.get<2>() << std::endl;

Leave a Reply

Your email address will not be published. Required fields are marked *