I'm using CGAL's Kd-tree implementation along with Fuzzy spheres as query objects to get the points enclosed in a sphere of radius r_max
centered at a point. Here is this minimal working example:
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Kd_tree.h>
#include <CGAL/Search_traits_2.h>
#include <CGAL/Fuzzy_sphere.h>
#include <iostream>
#include <fstream>
typedef CGAL::Simple_cartesian<double> K;
typedef K::Point_2 Point;
typedef CGAL::Search_traits_2<K> TreeTraits;
typedef CGAL::Kd_tree<TreeTraits> Kd_tree;
typedef Kd_tree::Tree Tree;
typedef CGAL::Fuzzy_sphere<TreeTraits> Sphere;
int main(int argc, char* argv[])
{
double r_max;
Tree tree;
/* ... fill the tree with points, set the value of r_max ...*/
// Report indices for the neighbors within a sphere
unsigned int idc_query = tree.size()/2; // test index
Tree::iterator kti = idc_query + tree.begin();
Sphere s_query(*kti, r_max);
// Print points
tree.search(std::ostream_iterator<Point>(std::cout, "\n"), s_query);
return 0;
}
I took and adapted the line below the comment "Print points" from the nearest_neighbor_searching.cpp file under the Spatial_searching folder of CGAL's examples (my version is 3.9).
The question is: Is there a way for me to set a different OutputIterator
(rather than std::ostream_iterator
) that stores a pointer/iterator/handle to the points resulting from the search in a container of sorts, instead of having the points' coordinates printed to the standard output? Thank you.
In the C++ standard library, there are five kinds of iterators:
For more information, see cplusplus.com
In your case, you need an Output iterator, ie., an object it
that can be incremented (++it
) and de-referenced (*it
) to get a non-const reference, that can be written to.
You can create an output iterator that inserts all items written to it at the end of a container using std::back_inserter
:
#include <iterator>
#include <vector>
...
std::vector<Point> points;
tree.search(std::back_inserter(points), s_query);