OpenCASCADE BRepMesh - 2D Delaunay Triangulation


OpenCASCADE BRepMesh - 2D Delaunay Triangulation

eryar@163.com

Abstract. OpenCASCADE package BRepMesh can compute the Delaunay’s triangulation with the algorithm of Watson. It can be used for 2d plane or on surface by meshing in UV parametric space. The blog focus on the usage of the triangulation tool to triangulate 2d points.

Key Words. BRepMesh, Delaunay Triangulation, 

1.Introduction

點集的三角剖分Triangulation主要用於幾何數據的可視化,在所有的造型內核中都有三角剖分的功能,用來生成模型的網格數據交給圖形接口,如OpenGL等來顯示。OpenCASCADE中使用類BRepMesh_IncrementalMesh來將TopoDS_Shape進行三角剖分得到顯示數據。其原理根據其名字可以這樣解釋,使用了增量算法,不停的剖分直到結果的三角形滿足精度要求。

https://www.opencascade.com/content/brepmeshincremental-mesh-algorithm

OpenCASCADE的BRepMesh只能用於二維點集的三角剖分,所以對於任意曲面的三角剖分,可以對其參數空間UV使用增量算法進行剖分,直到最終的三角剖分滿足顯示精度要求,最后將參數空間UV映射回實際的三維模型空間。所以三角剖分的關鍵就成了尋找合理的剖分點,在盡量少的剖分點情況下,使剖分滿足顯示精度要求。

本文主要介紹如何使用OpenCASCADE中BRepMesh來對二維點集進行三角剖分,最后將剖分結果在Draw Test Harness中進行可視化,便於實時查看剖分結果。

2.Code Example

使用BRepMesh直接對二維點集進行三角剖分,代碼如下所示:

/*
Copyright(C) 2017 Shing Liu(eryar@163.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include <math_BullardGenerator.hxx>

#include <BRepMesh.hxx>
#include <BRepMesh_Delaun.hxx>
#include <BRepMesh_DataStructureOfDelaun.hxx>

#pragma comment(lib, "TKernel.lib")
#pragma comment(lib, "TKMath.lib")

#pragma comment(lib, "TKG2d.lib")
#pragma comment(lib, "TKG3d.lib")
#pragma comment(lib, "TKGeomBase.lib")
#pragma comment(lib, "TKGeomAlgo.lib")

#pragma comment(lib, "TKBRep.lib")
#pragma comment(lib, "TKTopAlgo.lib")
#pragma comment(lib, "TKMesh.lib")

void testMesh(Standard_Integer thePointCount)
{
    std::ofstream aTclFile("d:/mesh.tcl");

    math_BullardGenerator aRandom;

    BRepMesh::Array1OfVertexOfDelaun aVertices(1, thePointCount);

    for (Standard_Integer i = aVertices.Lower(); i <= aVertices.Upper(); ++i)
    {
        gp_XY aPoint;
        aPoint.SetX(aRandom.NextReal() * aVertices.Upper());
        aPoint.SetY(aRandom.NextReal() * aVertices.Upper());

        BRepMesh_Vertex aVertex(aPoint, i, BRepMesh_Frontier);

        aVertices.SetValue(i, aVertex);

        // output point to Draw Test Harness.
        aTclFile << "vpoint p" << i << " " << aPoint.X() << " " << aPoint.Y() << " 0" << std::endl;
    }

    BRepMesh_Delaun aDelaunay(aVertices);
    Handle(BRepMesh_DataStructureOfDelaun) aMeshStructure = aDelaunay.Result();

    const BRepMesh::MapOfInteger& aTriangles = aMeshStructure->ElementsOfDomain();
    BRepMesh::MapOfInteger::Iterator aTriangleIt(aTriangles);
    for (aTriangleIt; aTriangleIt.More(); aTriangleIt.Next())
    {
        const Standard_Integer aTriangleId = aTriangleIt.Key();
        const BRepMesh_Triangle& aCurrentTriangle = aMeshStructure->GetElement(aTriangleId);

        if (aCurrentTriangle.Movability() == BRepMesh_Deleted)
        {
            continue;
        }

        Standard_Integer aTriangleVerts[3];
        aMeshStructure->ElementNodes(aCurrentTriangle, aTriangleVerts);

        // output line to Draw Test Harness.
        aTclFile << "vline l" << aTriangleId << "1 p" << aTriangleVerts[0] << " p" << aTriangleVerts[1] << std::endl;
        aTclFile << "vline l" << aTriangleId << "2 p" << aTriangleVerts[1] << " p" << aTriangleVerts[2] << std::endl;
        aTclFile << "vline l" << aTriangleId << "3 p" << aTriangleVerts[2] << " p" << aTriangleVerts[0] << std::endl;
    }

    aTclFile.close();
}

int main(int argc, char* argv[])
{
    testMesh(500);

    return 0;
}

 

程序使用隨機數據生成的點集進行三角剖分並將三角剖分結果輸出到D盤mesh.tcl文件,在Draw Test Harness中導入mesh.tcl即可看到剖分結果,如下圖所示:

3.Conclusion

BRepMesh可以對二維點集進行三角剖分,使用簡單,只需要將點集傳入類BRepMesh_Delaun即可。

將三角剖分結果生成Draw Test Harness腳本的方法,可以用來方便地將剖分結果可視化。自己開發程序的時候也可采用這個方法將造型的模型數據在Draw Test Harness中顯示。

如果三角剖分的點集中有孔需要去除,OpenCASCADE應該也提供了這個功能,有待發掘。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM