Triangulation_2 < Traits,TDS > 是CGAL中描述二维三角形剖分的一个模板类。实例化时, 它的第一个模板参数要传入一个几何核心类, 第二个模板参数是一个三角剖分的数据结构类(这个参数有一个默认的值)。这个模板类定义了三角剖分的基本用户接口, 它也是其它三角剖分类(如Delaunay_triangulation_2、Regular_triangulation_2)的基类。 因此它是基于CGAL进行二维三角形网格生成和优化算法开发,必须要掌握好这个类。
如图·,对于三角面f来说,它一共有三个顶点、三条边,还有三个相邻的三角面。顶点index排序按照逆时针,并且相邻面neighbor face和边Edge的index分别与对面的顶点index保持一致(如下图neighbor(i), Edge(i)与点i在空间上相对)。
在CGAL中,一个三角形面有三个顶点和三个相邻三角面的数据(不显式地存在边的数据,这也是我觉得很CGAL中很bug的一件事,太不实用了!)。用一个三角面的内部访问函数vertex(i), neighbor(i)来获取相应的顶点、相邻三角面,其数据类型分别为Vertex_handle, Face_handle. 通过这几个指针类型,可以访问到对应的数据。
顶点数据访问
- CDT::Point pt = vh->point(); 其中vh是数据指针Vertex_handle,具体访问x, y值就可以通过pt.x(), pt.y()来访问。注意,和一般的点数据不同,以上的x, y数据访问多一个括号!(表示通过成员函数访问成员变量,而不能直接访问其内部的成员变量)
- 三个顶点的数据可以通过访问一个三角面的属性:用Face_handle fh; fh->vertex(i), i=0,1,2,来访问,而且0,1,2分别按照逆时针来排序。
- 三个顶点还可以通过其中一个点的index遍历,对应的index分别为i, ccw(i), cw(i)来访问。
- 某个顶点也可以通过对边Edge来访问,Edge是一个二元组,由typedef std::pair<Face_handle, int> Edge;定义,因此访问Edge_hanle eh; eh->first得到的是它所属的一个三角面,eh->second得到的是它在该所属三角面的边索引index(而对顶点和这个索引一致);因而结合这两者就可以利用eh->first->vertex(eh->second);访问其余两个点就可以通过先得到三角面指向Face_handle fh = eh->first(); 然后fh->vertex(cdt.cw(eh->second));与fh->vertex(cdt.cw(eh->second))进行顶点访问。
三角形的创建:
1. 用默认构造函数 TriangulationDSFaceBase_2 f;
2. 用三点Vertex_handle v0, v1, v2初始化三角形
TriangulationDSFaceBase_2 f ( Vertex_handle v0, Vertex_handle v1, Vertex_handle v2);
3. 用三点Vertex_handle v0, v1, v2初始化三角形三个顶点,用Face_handle n0, n1, n2初始化三角形相邻的三个三角形
TriangulationDSFaceBase_2 f ( Vertex_handle v0, Vertex_handle v1, Vertex_handle v2, Face_handle n0, Face_handle n1, Face_handle n2);
三角的访问函数:
- Vertex_handlef.vertex (int i); //返回三角面f的第i个顶点(i= 0, 1, 2), 类型为Vertex_handle
- Face_handlef.neighbor ( int i); //返回三角面f的第i个相邻三角面(i= 0, 1, 2), 类型为Face_handle
- intf.dimension (); //返回三角面维数
- boolf.has_vertex ( Vertex_handle v); //如果v是三角面f的一个顶点,返回true;否则为false
- boolf.has_vertex ( Vertex_handle v, int& i); //如上,并且设置该顶点在三角面f的index为i
- boolf.has_neighbor ( Face_handle n) //返回三角面n是否为三角面f的相邻三角
- boolf.has_neigbor ( Face_handle n, int& i) //如上,并且设置该三角面在三角面f的相邻index
- intf.index ( Vertex_handle v); //返回顶点v在三角面f上的index
- int f.index ( Face_handle n); //返回三角n在f相邻三角的index
三角的设置函数
- voidf.set_vertex ( int i, Vertex_handle v) //设置三角面f的第i个顶点是v (i=0, 1, 2)
- voidf.set_vertices ( Vertex_handle v0, Vertex_handle v1, Vertex_handle v2)
- voidf.set_neighbor ( int i, Face_handle n) //设置三角面f的第i个相邻面为n (i=0, 1, 2)
- voidf.set_neighbors ( Face_handle n0, Face_handle n1, Face_handle n2)
约束三角剖分(CDT)中的数据遍历
1. 在Triangulation_2<Traits,TDS> 中,要遍历所有的节点、边或三角面,要用到相应的 iterator, 如:
Finite_faces_iterator
Finite_edges_iterator
Finite_vertices_iterator
//例:首先包含所需要的头文件和定义一些数据结构
01 | #include <CGAL/Exact_predicates_inexact_constructions_kernel.h> |
02 | #include <CGAL/Constrained_Delaunay_triangulation_2.h> |
03 | #include <CGAL/Triangulation_conformer_2.h> |
07 | typedef CGAL::Exact_predicates_inexact_constructions_kernel K; |
08 | typedef CGAL::Constrained_Delaunay_triangulation_2<K> CDT; |
09 | typedef CDT::Point Point; |
10 | typedef CDT::Vertex_handle Vertex_handle; |
13 | CDT::Finite_faces_iterator f_iter; |
14 | for (f_iter=cdt.finite_faces_begin(); f_iter!=cdt.finite_faces_end(); f_iter++) |
16 | for ( int i=0; i<3; i++) |
18 | Point p = f_iter->vertex(i)->point(); |
19 | cout<< "(" <<p.x()<< "," <<p.y()<< ")" <<endl; |
//用Finite_edges_iterator遍历CDT中的所有边Edge,输出每条边对应的两个顶点数据
01 | CDT::Finite_edges_iterator e_iter; |
02 | for (e_iter=cdt.finite_edges_begin(); e_iter!=cdt.finite_edges_end(); e_iter++) |
04 | for ( int i=0; i<2; i++) |
06 | Vertex_handle f_v1 = e_iter->first->vertex(cdt.cw(e_iter->second)); |
07 | Vertex_handle f_v2 = e_iter->first->vertex(cdt.ccw(e_iter->second)); |
09 | Point p1 = f_v1->point(); |
10 | Point p2 = f_v2->point(); |
11 | cout<< "(" <<p1.x()<< "," <<p1.y()<< ")" <<endl;</span> |
1 | cout<< "(" <<p2.x()<< "," <<p2.y()<< ")" <<endl;</span> |
}
}
2. 要遍历一个节点周围的节点、边或单元,要用到 circulator, 如:
Face_circulator
Edge_circulator
Vertex_circulator