0

我使用 CGAL 的 Kd-tree 实现以及模糊球体作为查询对象,以获取包围在r_max以一个点为中心的半径球体中的点。这是这个最小的工作示例:

    #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;
    }

我从 CGAL 示例的 Spatial_searching 文件夹(我的版本是 3.9)下的最近的_neighbor_searching.cpp 文件中获取并修改了注释“打印点”下面的行。

问题是:有没有办法让我设置一个不同的OutputIterator(而不是std::ostream_iterator)存储指针/迭代器/句柄到在排序容器中搜索产生的点,而不是将点的坐标打印到标准输出?谢谢你。

4

2 回答 2

4

在 C++ 标准库中,有五种迭代器:

  • 输入迭代器
  • 输出迭代器
  • 前向迭代器
  • 双向迭代器
  • 随机访问迭代器

有关详细信息,请参阅cplusplus.com

在您的情况下,您需要一个输出迭代器,即一个it可以递增(++it)和取消引用(*it)的对象,以获得可以写入的非常量引用。

您可以使用以下方法创建一个输出迭代器,将所有写入它的项目插入到容器的末尾std::back_inserter

#include <iterator>
#include <vector>

...

std::vector<Point> points;
tree.search(std::back_inserter(points), s_query);
于 2011-11-13T09:24:45.493 回答
0

CGAL 中的东西已经进化了,也就是说你可以存储其他东西,而不仅仅是点。查看 用户手册中的使用任意点类型和点属性映射的示例。

于 2013-02-08T09:50:12.597 回答