保存CGAL alpha形状表面网格

Pla*_*iac 5 c++ 3d geometry mesh cgal

我从未使用过CGAL,几乎没有C/C++经验.但是在Google之后,我设法使用visual studio 2010在Windows 7 64位机器上编译了示例"Alpha_shapes_3"(\ CGAL-4.1-beta1\examples\Alpha_shapes_3).

在此输入图像描述

现在,如果我们检查程序"ex_alpha_shapes_3"的源代码,我们会注意到一个名为"bunny_1000"的数据文件是红色点集群所在的红色.现在我的问题是如何更改源代码,以便在为给定点计算alpha形状之后,alpha形状的表面网格被保存/写入外部文件.它可以是多边形列表及其各自的3D顶点.我想这些多边形将定义alpha形状的表面网格.如果我能做到这一点,我可以在我熟悉的外部工具中看到alpha形状生成程序的输出.

我知道这很简单,但我对CGAL的知识知之甚少.

我知道你猜测有代码,但我再次粘贴完成.

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Delaunay_triangulation_3.h>
#include <CGAL/Alpha_shape_3.h>

#include <fstream>
#include <list>
#include <cassert>

typedef CGAL::Exact_predicates_inexact_constructions_kernel Gt;

typedef CGAL::Alpha_shape_vertex_base_3<Gt>          Vb;
typedef CGAL::Alpha_shape_cell_base_3<Gt>            Fb;
typedef CGAL::Triangulation_data_structure_3<Vb,Fb>  Tds;
typedef CGAL::Delaunay_triangulation_3<Gt,Tds>       Triangulation_3;
typedef CGAL::Alpha_shape_3<Triangulation_3>         Alpha_shape_3;

typedef Gt::Point_3                                  Point;
typedef Alpha_shape_3::Alpha_iterator               Alpha_iterator;

int main()
{
  std::list<Point> lp;

  //read input
  std::ifstream is("./data/bunny_1000");
  int n;
  is >> n;
  std::cout << "Reading " << n << " points " << std::endl;
  Point p;
  for( ; n>0 ; n--)    {
    is >> p;
    lp.push_back(p);
  }

  // compute alpha shape
  Alpha_shape_3 as(lp.begin(),lp.end());
  std::cout << "Alpha shape computed in REGULARIZED mode by default"
            << std::endl;

  // find optimal alpha value
  Alpha_iterator opt = as.find_optimal_alpha(1);
  std::cout << "Optimal alpha value to get one connected component is "
            <<  *opt    << std::endl;
  as.set_alpha(*opt);
  assert(as.number_of_solid_components() == 1);
  return 0;
} 
Run Code Online (Sandbox Code Playgroud)

经过互联网搜索后,我发现可能需要使用类似的东西

std::list<Facet> facets;
alpha_shape.get_alpha_shape_facets
(
  std::back_inserter(facets),Alpha_shape::REGULAR
);
Run Code Online (Sandbox Code Playgroud)

但我仍然完全无法在上面的代码中使用它!

slo*_*iot 8

如此处所述,facet是一对(Cell_handle c,int i),被定义为与索引i的顶点相对的c中的facet.在此页面上,您将了解单元格的顶点索引.

在下面的代码示例中,我添加了一个小输出,cout通过复制顶点打印OFF文件.要做一些干净的事情,您可以使用a std::map<Alpha_shape_3::Vertex_handle,int>来关联每个顶点的唯一索引,或者像这些示例中那样向顶点添加信息.

/// collect all regular facets
std::vector<Alpha_shape_3::Facet> facets;
as.get_alpha_shape_facets(std::back_inserter(facets), Alpha_shape_3::REGULAR);

std::stringstream pts;
std::stringstream ind;

std::size_t nbf=facets.size();
for (std::size_t i=0;i<nbf;++i)
{ 
  //To have a consistent orientation of the facet, always consider an exterior cell
  if ( as.classify( facets[i].first )!=Alpha_shape_3::EXTERIOR )
    facets[i]=as.mirror_facet( facets[i] );
  CGAL_assertion(  as.classify( facets[i].first )==Alpha_shape_3::EXTERIOR  );

  int indices[3]={
    (facets[i].second+1)%4,
    (facets[i].second+2)%4,
    (facets[i].second+3)%4,
  };

  /// according to the encoding of vertex indices, this is needed to get
  /// a consistent orienation
  if ( facets[i].second%2==0 ) std::swap(indices[0], indices[1]);


  pts << 
  facets[i].first->vertex(indices[0])->point() << "\n" <<
  facets[i].first->vertex(indices[1])->point() << "\n" <<
  facets[i].first->vertex(indices[2])->point() << "\n"; 
  ind << "3 " << 3*i << " " << 3*i+1 << " " << 3*i+2 << "\n";
}

std::cout << "OFF "<< 3*nbf << " " << nbf << " 0\n";
std::cout << pts.str();
std::cout << ind.str();
Run Code Online (Sandbox Code Playgroud)