到底什么是 OutputIterator 以及如何构建一个与 CGAL Kd_tree::search 一起使用的 OutputIterator?

发布于 2024-12-15 12:02:41 字数 1576 浏览 2 评论 0原文

我使用 CGAL 的 Kd 树实现以及模糊球体作为查询对象来获取以某个点为中心、半径 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文件夹下的nearest_neighbor_searching.cpp文件中获取并修改了注释“Printpoints”下方的行(我的版本是3.9)。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉生梦死 2024-12-22 12:02:41

在 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);

In the C++ standard library, there are five kinds of iterators:

  • Input iterator
  • Output iterator
  • Forward iterator
  • Bidirectional iterator
  • Random access iterator

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);
笑忘罢 2024-12-22 12:02:41

CGAL 中的事情已经发生了变化,除了积分之外,你还可以存储其他东西。查看使用任意点的示例在用户手册中输入点属性映射

Things have evolved in CGAL, that is you can store other things than just points. Have a look at the Examples for Using an Arbitrary Point Type with Point Property Maps in the User Manual.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文