使用CGAL查找输入三角网格模型独立模型个数

在对三角网格模型进行切分并且对切分模型三角化之后,可能会遇到分块数 > 2的情况,对于这种情况我们就需要得到各个分块的数量,并且将他们重组成独立的三角网格模型。

思路如下:

建立一个存放三角网格面的堆栈,遍历模型的所有的三角网格面,对每一个三角形都搜索与之相邻的三个三角形(每个边对应一个三角形),将这些三角形放入堆栈,直到堆栈为空表明已经找到属于同一个块的三角网格。再遍历以上过程,直到所有的三角网格都有属于自己的块。

在CGAL的Polyhedron_3.h头文件的Facet类里加入一个int型标志位mask,并且在构造器里将其初始化为0。接下来是具体的搜索过程,trilist是一个Facet_handle类型的堆栈。

    int noc = 0;
    Facet_handle t , s;
    
    //所有的facet的mask都为0
    for(Facet_iterator it = P1.facets_begin() , end = P1.facets_end() ;
        it != end;it++)
    {
        if(!it->mask)
        {
            noc ++ ;
            trilist.push(it);
            it->mask = noc ;
            while(trilist.size())
            {
                t = trilist.top();
                trilist.pop();
                if((s=t->halfedge()->opposite()->facet()) != NULL && (!s->mask)) {trilist.push(s);s->mask=noc;}
                if((s=t->halfedge()->next()->opposite()->facet()) != NULL && (!s->mask)){trilist.push(s);s->mask=noc;}
                if((s=t->halfedge()->prev()->opposite()->facet()) != NULL && (!s->mask)){trilist.push(s);s->mask=noc;} 
            }
        }
        //std::cout << "Mask of facet is "<< it->mask <<std::endl;
    } 
    std::cout << "Number of components is "<< noc <<std::endl;

 

posted @ 2014-12-17 17:46  wcm_94  阅读(867)  评论(0编辑  收藏  举报