C ++ ConvexHull of points算法(及其索引)

时间:2015-03-13 19:04:57

标签: c++ algorithm convex-hull

我需要用C ++实现我的代码,我需要计算ConvexHull的点并获得ConvexHull上的点索引,但是我找不到用C做的方法吗?

在Matlab和Python中,你只需要将点数组传递给ConvexHull函数就可以了,它会返回索引吗?我们在c ++中有相同的东西吗?

2 个答案:

答案 0 :(得分:1)

我需要获取C ++中点云的凸包索引,并且还为PCL或Boost提供的函数不返回点索引感到烦恼。您说对了,QHull文档似乎很复杂。因此,我去看了PCL source codes并自己提取了它。这就是我得到的

extern "C"
{
    #include <qhull/qhull.h>
}

pcl::PointCloud<pcl::PointXYZ>::Ptr cloud; // some point cloud
int dimension = 2, size = cloud->size();
coordT* points = reinterpret_cast<coordT*> (calloc (size*dimension, sizeof(coordT)));
for (size_t i=0; i<size; ++i)
{
    points[2*i] = static_cast<coordT> (cloud->points[i].x);
    points[2*i+1] = static_cast<coordT> (cloud->points[i].y);
}
int exitcode = qh_new_qhull(dimension, size, points, true, const_cast<char*> ("qhull FA"), NULL, NULL);
if (exitcode != 0 || qh num_vertices == 0)
{
    std::cerr << "ERROR: qhull was unable to compute a convex hull for the given point cloud" << std::endl;
}
pcl::PointIndices::Ptr hull_indices (new pcl::PointIndices());
vertexT * vertex;
FORALLvertices
{
    hull_indices->indices.push_back(qh_pointid(vertex->point));
}
qh_freeqhull(!qh_ALL);
int curlong, totlong;
qh_memfreeshort(&curlong, &totlong);

请注意,这是针对2D点云的。对于其他维度或数据类型,您可以进行相应的修改。希望有帮助!

答案 1 :(得分:0)

QHull是凸壳,Delaunay三角测量和朋友的事实标准。 QHull提供了许多语言和界面 - 以及一些非常好的参考文献的页面链接。