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应该也提供了这个功能,有待发掘。

posted @ 2017-11-11 12:42  opencascade  阅读(2555)  评论(0编辑  收藏  举报