2013-10-23 27 views
12

qhullライブラリ(qhull.org)には彼のウェブサイトから始めるいくつかの例がありますが、C++に関するすべての情報はあまり役に立ちません。qhull Library - C++ Interface

ファイルから読み込んだ3Dポイントの単純な凸包を作成しようとしていますが、私は、外部アプリケーションとしてqhull.exeを呼び出すというWebサイトで提案されているテクニックを使用できません。私がデータポイントで行ったいくつかの修正からいくつかの凸包を作ります。

私はこれを行うための簡単な例を見つけることができません、誰かが私にこの仕事のいくつかの助けを与えることができますか?どんな情報でも役立ちます。

おかげで、私はC++で自分自身をQhullを使用して苦労していたし、ウェブ上の任意の有用な例を見つけることができませんでした、anddddd最終的に有効な結果を得ることに成功しましたので

+0

windowsまたはlinux? – pippin1289

+0

私は窓を使用しています –

答えて

10

、私は将来のためにここに私のコードを投稿していますつかいます。

この回答は、Visual Studio 2012/3を使用しているWindowsで有効です。私はあなたが追加する必要がある唯一のファイルである、here からqhullソースファイルをダウンロードしてVSでプロジェクトを開いた後、物事を開始するには、そこでどのように知っているか、それは他のプラットフォーム上で動作するかどう

はありません以下の2つのディレクトリ:

Qhull.h

libqhull/... 
libqhullcpp/... 

プロジェクトにこれらのファイルを追加した後は、(明らかにあなたがあなた自身の方法を使用することができ、これは私の方法である)次のコードを追加します

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

私は私のプロジェクトからこのコードをコピーして、より有益が、避難所であることを、それを少し変更しましたこの例をまとめました。

+0

これはスレッドセーフではありません – Raaj

+0

@Raaj他人のためにそれを指摘してくれてありがとう。私の答えをスレッドセーフなコードで編集してみてください。私はそれを見直して受け入れます。 – ZivS

+0

ありません。私は全体のインターネット/ github/codeprojectのすべてを見てきました。 Convex/ConcaveハルアルゴリズムのC/C++の例はありません。 – Raaj