在C ++代码中提供qconvex的输入

时间:2015-03-19 22:51:23

标签: c++ command qhull

对于我的代码,我需要计算一系列点的Convexhull,由于某些原因,我需要使用qhull库。在这个库中有一个方法qconvex完全符合我的需要。我可以在终端运行这个命令,得到我想要的。例如,假设我有一个像points.txt

这样的输入
2   #dimension
5   #number of points
0 0
1 0
0.5 0.5
1 1
0 1

我可以在终端1中运行这些命令来获得结果:qconvex Fx < points.txtcat points.txt | qconvex -Fx,输出结果为:

4 
0
1
3
4

现在我的问题是如何在我的C ++代码中迭代地调用此命令:在我的代码中,我在彼此内部有2 for调用特定函数,每次生成10个点(存储)在float **rs_tmp;),我需要每次计算这10个点的qconvex。如何在我的代码中运行qconvex并将rs_tmp作为输入管道?我希望避免将rs_tmp写入某个临时文件并从中读取,因为我需要我的代码超快。

float **rs_tmp;
for (int i = 0; i < NUMBER; i++)
{
    for (int j = 0; j < NUMBER; j++)
    {
        rs_tmp = generate_points(label, dect[i], dect[j], fun);
        // HERE I NEED TO CALL QCONVEX SOME HOW
        // THE POINTS ARE STORED IN rs_tmp as 2-Dimensional floating points array
    }
     int size = fun.size();
     for(int i = 0; i < size; ++i)     
     {
         delete[] rs_tmp[i]; 
     }   
     delete[] rs_tmp;         
}

1 个答案:

答案 0 :(得分:2)

我已经有相同的问题已经有一段时间了,只是管理好解决它。 Qhull提供了一个非常简洁的例子,非常有用。如果你从git克隆,你可以在qhull / src / user_eg3 / user_eg3.cpp目录中看到这个例子。我花了一点时间才明白他们在做什么,但是一旦掌握了它,它实际上很容易。我删除了所有额外的选项,但是你要找的那个。

/*--------------------------------------------
-user_eg3-  main procedure of user_eg3 application
*/
int main(int argc, char **argv) {
    try{
        return user_eg3(argc, argv);
    }catch(QhullError &e){
        cerr << e.what() << std::endl;
        return e.errorCode();
    }
}//main

int user_eg3(int argc, char **argv)
{
  RboxPoints rbox;
  Qhull qhull;
  line = "";
  std::istringstream is("2 4 1 0 1 1 0 0 0 1"); // To be passed to rbox: produces a unit square where 2 is the dimension 4 is the count and the points follow. This will also accept any valid rbox flags. 
  std::stringstream output;

  rbox.appendPoints(is); // appendPoints accepts either a const char* or an istream object. See libqhullcpp/RboxPoints.h and libqhullcpp/PointCoordinates.h 

  cerr << "@@@@@@@@@@\n" << rbox << "@@@@@@@@@@\n";
  qhull.runQhull(rbox, ""); //you can set any flag you would set for qhull in the terminal inside the ""
  qhull.setOutputStream(&output); //this will take any output stream 
  qhull.outputQhull("m"); //this will take any qhull output option
  cerr << "My Output : " << output.str() << "%%\n";
  qhull.setOutputStream(&cout);
  qhull.outputQhull("n");
  return 0;
}//user_eg3

希望这有帮助。