I am using CGAL's K_neighbor_search module to do the nearest neighbor search problem. It's nice and easily to use. The example code shows that given a query point, it can find the nearest neighbor point from a set of points as well as the distance. However, I can only get the nearest neighbor point itself. I don't know how to get the index of the point found by the algorithm. For example, I use the following code,
std::list<Point_d> points;
Tree tree(points.begin(), points.end());
Neighbor_search search(tree, query, N);
for(Neighbor_search::iterator it = search.begin(); it != search.end(); ++it)
{
std::cout << "Point: " << it->first << "\n";
std::cout << "Distance: " << std::sqrt(it->second) << "\n";
}
the result is as follows:
Point: 222 129 161
Distance: 189.307
But how can I get the index of the result point? As for the reason of this question, I want to get the normal of the nearest neighbor point, so I need to reference the point. Could anybody help me?
If you want to use indices directly in the kd-tree you can look at this example. Points are sorted in an external vector and the kd-tree uses indices to refer to the points internally store.