Triangle - Delaunay Triangulator
eryar@163.com
Abstract. Triangle is a 2D quality mesh generator and Delaunay triangulator. Triangle was created as part of the Quake project in the school of Computer Science at Carnegie Mellon University by Jonathan R. Shewchuk. Triangle is a small C program and its Delaunay refinement algorithm for quality mesh generation is a hybrid one. It includes divide-and-conquer and incremental insertion algorithms and sweepline Delaunay triangulation algorithm. This paper is focused on the usage of the Triangle and visualization the triangulation result in OpenSceneGraph.
Key words. Triangle, Delaunay Triangulator, Mesh Generator
1. Introduction
Triangle可以生成精確的Delaunay三角剖分,限定Delaunay三角剖分(Constrained Delaunay Triangulation),Conforming Delaunay Triangulation,Voronoi圖(Voronoi Diagrams)和高質(zhì)量的三角網(wǎng)格,即生成的網(wǎng)格中沒(méi)有瘦長(zhǎng)的三角形,所以適用于有限元分析(Finite Element Analysis)。
在OpenCascade6.2.0版本之前,OpenCascade中網(wǎng)格的生成就是使用了這個(gè)開(kāi)源庫(kù),由此可見(jiàn)Delaunay三角剖分算法和網(wǎng)格生成算法的重要性及廣泛應(yīng)用。
Figure 1.1 Triangle - A 2D Quality Mesh Generator and Delaunay Triangulator
下載Triangle的源程序及更多與Triangle相關(guān)信息的網(wǎng)址如下所示:
http://www.cs.cmu.edu/~quake/triangle.html
下載到源程序后,如果是Windows操作系統(tǒng),還需要在triangle.h之前做些配置,如定義以下幾個(gè)宏:
#define REAL double
#define ANSI_DECLARATORS
#include "triangle.h"
#undef REAL
在triangle.c中定義宏:#define NO_TIMER。有了上面的宏定義,可以編譯出一個(gè)triangle.exe程序了。如果要將triangle用在自己的程序中,還需要定義#define TRILIBRARY。更多宏定義可以參考源程序。
2. Triangle Usage
Triangle有很多開(kāi)關(guān),可以選擇三角剖分和生成網(wǎng)格的方式,如下圖所示:
Figure 2.1 Options for the Triangle
如對(duì)示例文件box.poly進(jìn)行三角剖分,使用命令及生成結(jié)果統(tǒng)計(jì)信息如下所示:
Figure 2.2 Triangle Usage
出現(xiàn)統(tǒng)計(jì)信息的同時(shí)也生成了一些文件,如頂點(diǎn)文件box.1.node和三角形文件box.1.ele,如下圖所示:
Figure 2.3 Nodes and Triangles data generated by Triangle
Figure 2.4 Triangulation Mesh Generated by Triangle[-pc]
Figure 2.5 Triangulation Mesh Generated by Triangle[-pqc]
3. Displaying Meshes
在下載的程序中有用于顯示網(wǎng)格的示例程序showme.c,不過(guò)只能用于Unix操作系統(tǒng),不能用于Windows。
Figure 3.1 Displaying the Meshes by ShowMe
為了在Windows操作系統(tǒng)中看到生成的網(wǎng)格,用OpenSceneGraph編寫了一個(gè)小程序TriangleViewer顯示網(wǎng)格。其中讀取node和element文件中數(shù)據(jù)的主要程序片段如下所示:
std::string TriangleMesh::ReadLine(std::ifstream &theFile)
{
std::string theBuffer;
bool IsReadNextLine = false;
do
{
getline(theFile, theBuffer);
// skip comment here.
if ('#' == theBuffer[0])
{
IsReadNextLine = true;
}
else
{
IsReadNextLine = false;
}
}
while (IsReadNextLine);
return theBuffer;
}
void TriangleMesh::BuildMesh(const std::string& aPolyFile)
{
std::stringstream ss;
std::string theNodeFileName(aPolyFile + ".node");
std::string theElementFileName(aPolyFile + ".ele");
std::ifstream theNodeFile(theNodeFileName.c_str());
std::ifstream theElementFile(theElementFileName.c_str());
Standard_Integer theIndex = 0;
Standard_Integer theNodeCount = 0;
Standard_Integer theTriangleCount = 0;
Standard_Integer theIndex1 = 0;
Standard_Integer theIndex2 = 0;
Standard_Integer theIndex3 = 0;
Standard_Real x = 0.0;
Standard_Real y = 0.0;
// Read mesh size.
ss << ReadLine(theNodeFile);
ss >> theNodeCount;
ss.str("");
ss.clear();
ss << ReadLine(theElementFile);
ss >> theTriangleCount;
mMesh = new Poly_Triangulation(theNodeCount, theTriangleCount, Standard_True);
// Read nodes information.
TColgp_Array1OfPnt2d& theNodes2d = mMesh->ChangeUVNodes();
for (Standard_Integer n = 1; n <= theNodeCount; ++n)
{
ss.str("");
ss.clear();
ss << ReadLine(theNodeFile);
ss >> theIndex >> x >> y;
theNodes2d.SetValue(theIndex, gp_Pnt2d(x, y));
}
// Read triangles information.
Poly_Array1OfTriangle& theTriangles = mMesh->ChangeTriangles();
for (Standard_Integer t = 1; t <= theTriangleCount; ++t)
{
ss.str("");
ss.clear();
ss << ReadLine(theElementFile);
ss >> theIndex >> theIndex1 >> theIndex2 >> theIndex3;
theTriangles.SetValue(theIndex, Poly_Triangle(theIndex1, theIndex2, theIndex3));
}
}
如下圖所示為顯示一個(gè)用不同命令生成的Smiley Face的網(wǎng)格:
Figure 3.2 Generate Smiley Face Mesh by Triangle [-pc]
Figure 3.3 Generate Smiley Face Mesh by Triangle [-pqc]
從上面兩幅圖中的網(wǎng)格可知,下面圖中的網(wǎng)格質(zhì)量較高,為去掉了瘦長(zhǎng)的三角形而增加了一些頂點(diǎn)。
4. Conclusions
在給Triangle程序輸入數(shù)據(jù)時(shí),頂點(diǎn)Vertex數(shù)據(jù)很好理解,只是一些二維點(diǎn),但是如果加上開(kāi)孔Hole后有些問(wèn)題。后來(lái)才知道,需要在Poly文件中的Segments部分輸入與孔相關(guān)線段形成的閉合區(qū)域,在孔Hole部分只需要輸入位于孔中的任意一個(gè)點(diǎn)即可。
將Triangle生成的結(jié)果可視化,可以看到Triangle生成的網(wǎng)格,方便看到Triangle的不同選項(xiàng)生成的網(wǎng)格效果。
在OpenCascade6.2.0版本中,就以此二維Delaunay三角剖分工具為基礎(chǔ),實(shí)現(xiàn)了任意三維曲面的三角剖分,進(jìn)而對(duì)其可視化。所以學(xué)習(xí)Triangle的用法,結(jié)合OpenCascade的源程序便于理解任意曲面的可視化實(shí)現(xiàn)的方法。
對(duì)Delaunay三角剖分算法感興趣的讀者,可以參考相關(guān)書籍[3],[4],[5],[6]。
5. References
1. Jonathan R. Shewchuk. Triangle: http://www.cs.cmu.edu/~quake/triangle.html
2. Jonathan R. Shewchuk, Triangle: Engineering a 2D Quality Mesh Generator and Delaunay Triangualtor, Springer-Verlag, Berlin, 1996
3. 汪嘉業(yè) 王文平 屠長(zhǎng)河 楊承磊. 計(jì)算幾何及應(yīng)用. 科學(xué)出版社. 2011
4. 王成恩. 面向科學(xué)計(jì)算的網(wǎng)格劃分與可視化技術(shù). 科學(xué)出版社. 2011
5. 周培德. 計(jì)算幾何-算法設(shè)計(jì)與分析. 清華大學(xué)出版社. 2008
6. Berg M D著 鄧俊輝譯. 計(jì)算幾何-算法與應(yīng)用. 清華大學(xué)出版社. 2009