保存 CGAL alpha 形状表面网格 [英] saving CGAL alpha shape surface mesh

查看:36
本文介绍了保存 CGAL alpha 形状表面网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从未使用过 CGAL,几乎没有 C/C++ 经验.但随着然而,谷歌我设法编译了示例Alpha_shapes_3"(CGAL-4.1-beta1examplesAlpha_shapes_3) 在 Windows 7 64 位机器上使用视觉工作室 2010.

I have never used CGAL and have got almost no C/C++ experience. But following Google I have however managed to compile the example "Alpha_shapes_3" (CGAL-4.1-beta1examplesAlpha_shapes_3) on a Windows 7 64bit machine using visual studio 2010.

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

Now if we check the source code for the program "ex_alpha_shapes_3" we notice that a data file called "bunny_1000" is red where the 3d point cluster resides. Now my question is how can I change the source code so that after the alpha shape is computed for the given points, surface mesh of the alpha shape is saved/wrote in an external file. It can be simply the list of polygons and their respective 3D vertices. I guess these polygons will be defining the surface mesh of the alpha shape. If I can do that I can see the output of the alpha shape generation program in an external tool I am familiar with.

我知道这很简单,但我无法用我的CGAL 知识有限.

I know this is very straightforward but I could not figure this out with my limited knowledge of CGAL.

我知道你们有代码,但我再次粘贴它以供完成.

I know you gueys have the code but I am pasting it again for completion.

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

在互联网上搜索了很多之后我发现可能我们需要使用类似

After searching a lot in the internet I found that probably we need to use something like

std::list<Facet> facets;
alpha_shape.get_alpha_shape_facets
(
  std::back_inserter(facets),Alpha_shape::REGULAR
);

但我仍然完全不知道如何在上面的代码中使用它!

But I am still completely clueless how to use this in the above code!

推荐答案

如文档所述 此处,facet 是一对 (Cell_handle c,int i),定义为 c 中与索引 i 的顶点相对的 facet.在此页面上,您可以看到单元格的顶点索引是.

As documented here, a facet is a pair (Cell_handle c,int i) defined as the facet in c opposite to the vertex of index i. On this page, you have the description of how the vertex indices of a cell are.

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

In the following code sample, I added a small output that prints an OFF file on cout by duplicating the vertices. To do something clean, you can either use a std::map<Alpha_shape_3::Vertex_handle,int> to associate a unique index per vertex or add an info to the vertices like in those examples.

/// 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() << "
" <<
  facets[i].first->vertex(indices[1])->point() << "
" <<
  facets[i].first->vertex(indices[2])->point() << "
"; 
  ind << "3 " << 3*i << " " << 3*i+1 << " " << 3*i+2 << "
";
}

std::cout << "OFF "<< 3*nbf << " " << nbf << " 0
";
std::cout << pts.str();
std::cout << ind.str();

这篇关于保存 CGAL alpha 形状表面网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆