You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从自相交VTK Polydata的重复点拆分出多个凸多边形?

自相交VTK多边形拆分及逆时针方向调整方案

1. 依赖环境安装

先确保安装所需库:

pip install pyvista shapely matplotlib

2. 读取VTK数据并提取几何信息

从VTK文件中提取多边形的点坐标与面索引:

import pyvista as pv
from shapely.geometry import Polygon
from shapely.ops import unary_union

# 读取自相交多边形的VTK文件
poly_data = pv.read('poly_11.vtk')

# 提取2D点坐标(默认取XY平面)
points = poly_data.points[:, :2]
# 提取面的点索引(跳过VTK faces格式的第一个点数标记)
face_indices = poly_data.faces[1:]
# 构建原始多边形的点列表
original_points = [points[i] for i in face_indices]

3. 拆分自相交多边形

利用shapely自动处理无效多边形,拆分出多个有效凸多边形:

# 创建自相交的无效多边形
invalid_polygon = Polygon(original_points)
# 通过unary_union拆分自相交区域,得到多个有效多边形
split_polygons = unary_union(invalid_polygon)

4. 调整为逆时针方向

使用shapely的orient函数将多边形外环强制转为逆时针,匹配matplotlib路径要求:

oriented_polys = []
for poly in split_polygons.geoms:
    # 参数1指定逆时针方向
    oriented_poly = poly.orient(1)
    oriented_polys.append(oriented_poly)

5. 结果验证(可选)

用matplotlib可视化拆分后的多边形:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
for idx, poly in enumerate(oriented_polys):
    x, y = poly.exterior.xy
    ax.plot(x, y, label=f'凸多边形 {idx+1}')
ax.legend()
plt.axis('equal')
plt.show()

关键说明

  • unary_union会自动识别自相交区域,将单个无效多边形分解为多个有效凸多边形(本例目标为2个)。
  • orient函数通过参数控制方向,1对应逆时针,-1对应顺时针,完全适配matplotlib路径的坐标要求。
  • 若原始VTK数据为3D模型,需先将点投影到指定2D平面(如XY、XZ)后再处理。

内容的提问来源于stack exchange,提问作者PBrockmann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 22:07:20