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

标签 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;         
}

最佳答案

我遇到同样的问题已经有一段时间了,只是设法解决了它。 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

希望这对您有所帮助。

关于c++ - 在 C++ 代码中提供 qconvex 的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29156437/

相关文章:

c++ - unsigned long long 的问题

c# - C#中SQL Server超时异常和SqlCommand超时异常的区别

php - 尝试 self 更新 Composer 时权限被拒绝

android - 为 Android 编译 Web RTC 时出错

numpy - 约束 Qhull 生成的 Voronoi 顶点的域

python - Qhull 凸包要求我输入至少 3 个点

c++ - 函数模板的别名

c++ - C++中的一种反射

c++ - 如果 "delete this"不需要访问 "somethings",那么在 "this"之后做某事是否安全?

3d - 计算 3D 平面的 Voronoi 图