Rtree comparable_distance

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Rtree comparable_distance

Turokhunter
This post was updated on .
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<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 <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<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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Rtree comparable_distance

Boost Geometry mailing list
Hi Tarik,

Turokhunter Via Geometry wrote:
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.

With some compilers (e.g. gcc) the order of includes and overloads matters. The default comparable_distance() is compiled instead of overloads defined by you. You can work around it by first including GLM and defining your overloads of bg::comparable_distance() and after that including boost geometry, registering point and proceed normally.

Adam

  


_______________________________________________
Geometry mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/geometry
Loading...