到底什么是 OutputIterator 以及如何构建一个与 CGAL Kd_tree::search 一起使用的 OutputIterator?
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 C++ 标准库中,有五种迭代器:
有关详细信息,请参见 cplusplus.com
在您的情况下,您需要一个输出迭代器,即一个可以递增的对象
it
(++it
) 和取消引用 (*it
) 以获得可写入的非常量引用。您可以使用 创建一个输出迭代器,将写入其中的所有项目插入到容器的末尾
std::back_inserter
: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
: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.