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 :
<http://boost-geometry.203548.n3.nabble.com/R-tree-query-nearest-nodes-with-torus-distance-strategy-td4026516.html>
.

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_REGISTER_POINT_3D(glm::vec3, double,
boost::geometry::cs::cartesian, x, y, z)

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

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

private:
  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::RTree()
{
  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);
      idx++;
  }

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

}

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::back_inserter(results));

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

Thanks for your help,
Tarik Crnovrsanin

--
View this message in context: http://boost-geometry.203548.n3.nabble.com/Rtree-comparable-distance-tp4026794.html
Sent from the Boost Geometry mailing list archive at Nabble.com.

Geometry list run by mateusz at loskot.net