Boost logo

Geometry :

Subject: [geometry] Rtree comparable_distance
From: Turokhunter (turokhunter_at_[hidden])
Date: 2017-05-29 21:38:41

Hi Everyone,

I'm trying to change the distance function for RTree. I have looked into the
online version of the mailing list and found a post that helped me get
started :

I followed the steps in that post but the comparable_distance function I
wrote doesn't get triggered. I was wondering if anyone knew the reason why.
For convenience, I posted the code down below.

#ifndef RTREE_HPP
#define RTREE_HPP

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/geometry/index/rtree.hpp>
#include <glm/glm.hpp>
#include <vector>

boost::geometry::cs::cartesian, x, y, z)

class RTree
  typedef boost::geometry::model::point<float, 3,
boost::geometry::cs::cartesian> point;
  typedef std::pair<glm::vec3, unsigned> indexed_point_t;

  void populateRTree(std::vector<glm::vec3> &pos);
  std::vector<unsigned> getIdNearestToPoint(glm::vec2 pos);

  typedef boost::geometry::index::rtree<indexed_point_t,
boost::geometry::index::rstar&lt;8>> Tree;
  std::shared_ptr<Tree> rtree;

#endif // RTREE_HPP

#include "rtree.hpp"

namespace boost {
  namespace geometry {

    double comparable_distance(glm::vec3 const& p1, glm::vec3 const& p2){
      double dx = p1.x - p2.x;
      double dy = p1.y - p2.y;
      std::cout<<"point to point math"<<std::endl;
      return std::sqrt(dx*dx + dy*dy)/(p1.z + p2.z);

    template &lt;typename Box>
    double comparable_distance(glm::vec3 const& p, Box const& b)
        // calculate the point/box distance
        std::cout<<"point to box math"<<std::endl;
        return 1;
} // namespace

  rtree = 0;

void RTree::populateRTree(std::vector&lt;glm::vec3> &positions){

  namespace bg = boost::geometry;
  namespace bgi = boost::geometry::index;
  std::vector<indexed_point_t> index_points(positions.size());
  unsigned int idx = 0;
  for(auto pos : positions){
      index_points[idx] = std::make_pair(pos, idx);

  rtree = std::make_shared<Tree>(index_points.begin(),


std::vector<unsigned> RTree::getIdNearestToPoint(glm::vec2 pos){
  namespace bgi = boost::geometry::index;
  std::vector<indexed_point_t> results;

  rtree->query(bgi::nearest(glm::vec3(pos.x,pos.y, 0), 2),

  std::vector<unsigned> index;
  for(auto result : results){
  return index;

Thanks for your help,
Tarik Crnovrsanin

View this message in context:
Sent from the Boost Geometry mailing list archive at

Geometry list run by mateusz at