qhull库 - C++接口

Nun*_*tos 14 c++ convex-hull qhull

qhull库(qhull.org)有几个例子可以在他的网站上开始,但是关于C++的所有信息对我来说都不是很有用.

我试图制作一个简单的3D点凸壳,我从一个文件中读取,我不能使用在调用qhull.exe作为外部应用程序的网站中建议的技术,因为我需要制作几个凸包从我在数据点中做出的一些修改.

我找不到这样做的简单例子,有人可以帮我完成这项任务吗?任何信息都会有用.

谢谢

Ziv*_*ivS 15

由于我很难自己使用Qhull和c ++,并且在网上找不到任何有用的例子,并且dddd终于成功获得了有效的结果,我在这里发布我的代码以备将来使用.

这个答案适用于Windows,使用visual studio 2012/3.我不知道它是如何在其他平台上运行的

所以,首先,从这里下载qhull源文件 并在VS中打开项目后,您需要添加的唯一文件是以下2个目录:

libqhull/
libqhullcpp/
Run Code Online (Sandbox Code Playgroud)

将这些文件添加到项目后,添加以下代码(这是我的方式,显然您可以使用自己的方式):

Qhull.h

namespace orgQhull{
//...
private:
    PointCoordinates *m_externalPoints;
//...
public:
    void runQhull3D(const std::vector<vec3> &points, const char* args);
    void runQhull(const PointCoordinates &points, const char *qhullCommand2);
//...
}
Run Code Online (Sandbox Code Playgroud)

Qhull.cpp

void Qhull::runQhull3D(const std::vector<vec3> &points, const char* args)
{
    m_externalPoints = new PointCoordinates(3);  //3 = dimension
    vector<double> allPoints;
    for each (vec3 p in points)
    {
        allPoints.push_back(p.x());
        allPoints.push_back(p.y());
        allPoints.push_back(p.z());
    }

    m_externalPoints->append(allPoints); //convert to vector<double>
    runQhull(*m_externalPoints, args);
}

void Qhull::runQhull(const PointCoordinates &points, const char *qhullCommand2)
{
    runQhull(points.comment().c_str(), points.dimension(), points.count(), &*points.coordinates(), qhullCommand2);
}
Run Code Online (Sandbox Code Playgroud)

最后这是如何使用代码:

//not sure all these includes are needed
#include "RboxPoints.h"
#include "QhullError.h"
#include "Qhull.h"
#include "QhullQh.h"
#include "QhullFacet.h"
#include "QhullFacetList.h"
#include "QhullLinkedList.h"
#include "QhullVertex.h"
#include "QhullSet.h"
#include "QhullVertexSet.h"
#include <vector>

int main()
{
    orgQhull::Qhull qhull;
    std::vector<vec3> vertices;
    qhull.runQhull3D(vertices, "Qt");

    QhullFacetList facets = qhull.facetList();
    for (QhullFacetList::iterator it = facets.begin(); it != facets.end(); ++it)
    {
        if (!(*it).isGood()) continue;
        QhullFacet f = *it;
        QhullVertexSet vSet = f.vertices();
        for (QhullVertexSet::iterator vIt = vSet.begin(); vIt != vSet.end(); ++vIt)
        {
            QhullVertex v = *vIt;
            QhullPoint p = v.point();
            double * coords = p.coordinates();
            vec3 aPoint = vec3(coords[0], coords[1], coords[2]);
            // ...Do what ever you want
        }
    }

    // Another way to iterate (c++11), and the way the get the normals
    std::vector<std::pair<vec3, double> > facetsNormals;
    for each (QhullFacet facet in qhull.facetList().toStdVector())
    {
        if (facet.hyperplane().isDefined())
        {
            auto coord = facet.hyperplane().coordinates();
            vec3 normal(coord[0], coord[1], coord[2]);
            double offset = facet.hyperplane().offset();
            facetsNormals.push_back(std::pair<vec3, double>(normal, offset));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,我从我的项目中复制了此代码,并对其进行了一些修改,以提供更多信息,但尚未编译此示例.