如何在PyVista中创建含内部孔洞边界的自定义约束Delaunay图?
问题:PyVista约束Delaunay图无法识别自定义内部孔洞
我正在为期末项目手动实现Ruppert's算法,需要以约束Delaunay图作为输入,PyVista的delaunay_2d()函数可生成此类图。参考官方示例(圆形改方形)能生成带内部镂空的网格,但自定义输入的内外矩形中,内部矩形未被识别为孔洞,仅生成外框的约束Delaunay图,求问题原因及解决思路。
参考官方示例代码
import pyvista as pv squar = pv.Polygon(n_sides=4, radius=8, fill=False) squar = squar.rotate_z(45, inplace=False) hole1 = pv.Polygon(center=(2,3,0), n_sides=4, radius=1) hole2 = pv.Polygon(center=(-2,-3,-0), n_sides=4, radius=0.5) comb = hole1 + hole2 + squar comb.plot(cpos='xy',show_edges=True) tess = comb.delaunay_2d(edge_source=comb) tess.plot(cpos='xy', show_edges=True)
自定义输入代码(存在问题)
points1 = [[1,0,0],[1,1,0],[0,1,0],[0,0,0]] points2 = [[0.25,0.5,0],[0.25,0.25,0],[0.5,0.25,0],[0.5,0.5,0]] faces2 = [4, 0, 1, 2, 3] lines1 = [5, 0, 1, 2, 3, 0] lines2 = [5, 0, 1, 2, 3, 0] rect1 = pv.PolyData(points1, lines=lines1) rect2 = pv.PolyData(points2, faces2, lines=lines2) PSLG = rect2 + rect1 PSLG.plot(cpos='xy', show_edges=True) tess = PSLG.delaunay_2d(edge_source=PSLG) tess.plot(cpos='xy', show_edges=True)
问题原因
- 轮廓方向与结构冲突:官方示例中所有轮廓都是
fill=False的线结构,且孔洞与外边界的点顺序方向相反(PyVista通过winding方向区分外边界和孔洞:外边界逆时针,孔洞顺时针,或反之)。你给内部矩形添加了填充面,同时内外矩形点顺序方向一致,导致无法识别孔洞。 - PolyData结构混乱:内部矩形同时包含填充面和线结构,与仅有线结构的外矩形组合后,
delaunay_2d无法正确解析约束边和孔洞的层级关系。
解决思路
- 统一用线结构定义轮廓:移除内部矩形的填充面,仅保留闭合线环,避免结构冲突。
- 反转内部孔洞的点顺序:调整内部矩形的点顺序,使其与外轮廓的winding方向相反,让PyVista能区分内外。
- 确保轮廓闭合:所有线结构需以起始点收尾,保持闭合(你的
lines定义符合要求)。
修正后的代码
import pyvista as pv # 外矩形:逆时针点顺序 points1 = [[1,0,0],[1,1,0],[0,1,0],[0,0,0]] lines1 = [5, 0, 1, 2, 3, 0] rect1 = pv.PolyData(points1, lines=lines1) # 内矩形:顺时针点顺序(与外轮廓方向相反) points2 = [[0.25,0.5,0],[0.5,0.5,0],[0.5,0.25,0],[0.25,0.25,0]] lines2 = [5, 0, 1, 2, 3, 0] rect2 = pv.PolyData(points2, lines=lines2) # 组合轮廓 PSLG = rect1 + rect2 PSLG.plot(cpos='xy', show_edges=True) # 生成约束Delaunay图 tess = PSLG.delaunay_2d(edge_source=PSLG) tess.plot(cpos='xy', show_edges=True)
内容的提问来源于stack exchange,提问作者Alexander Hu
相关产品推荐
相关产品推荐

