gpt4 book ai didi

c++ - qhull 库 - C++ 接口(interface)

转载 作者:IT老高 更新时间:2023-10-28 22:38:53 36 4
gpt4 key购买 nike

qhull 库 (qhull.org) 在他的网站上提供了几个示例,但所有关于 C++ 的信息对我来说都不是很有用。

我正在尝试制作从文件中读取的 3D 点的简单凸包,我无法使用网站中建议的将 qhull.exe 作为外部应用程序调用的技术,因为我需要制作我在数据点中进行的一些修改的几个凸包。

我找不到执行此操作的简单示例,有人可以帮我完成这项任务吗?任何信息都会很有用。

谢谢

最佳答案

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

This answer works for windows, with visual studio 2012/3. I don't know how or if it works on other platforms

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

libqhull/
libqhullcpp/

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

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);
//...
}

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

最后是如何使用代码:

//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 : 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));
}
}
}

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

关于c++ - qhull 库 - C++ 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19530731/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com