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

如何在Python中提取并存储UnstructuredGrid特定y坐标对应的x、z坐标?

基于VTK/PyVista处理UnstructuredGrid并提取目标坐标

先搞懂UnstructuredGrid的核心逻辑

UnstructuredGrid本质就是空间点集加单元集合:

  • 点:就是你要的(x,y,z)坐标,存成一个(n,3)的数组
  • 单元:用点的索引组合成的几何形状(比如四面体、六面体),和你提取坐标的需求无关,不用管它

1. 提取网格所有点坐标

用PyVista的话,UnstructuredGrid对象直接带.points属性,一步拿到坐标数组:

import pyvista as pv
import numpy as np

# 假设你的网格已经生成好,存在grid变量里
all_points = grid.points  # 直接得到shape=(n,3)的numpy数组

2. 筛选特定y坐标的点

如果是找等于某个固定y值的点,考虑浮点数精度用近似匹配:

target_y = 10.0  # 你指定的y值
# 用atol控制精度,比如1e-6
mask = np.isclose(all_points[:, 1], target_y, atol=1e-6)
filtered_points = all_points[mask]  # shape=(m,3),m是符合条件的点数量

要是需要y在某个区间,改成区间判断:

y_min, y_max = 8.0, 12.0
mask = (all_points[:,1] >= y_min) & (all_points[:,1] <= y_max)
filtered_points = all_points[mask]

3. 生成图像正面坐标(光线追踪输入)

场景1:按轴向取最外层点(比如z最大的面)

如果正面是沿着某个轴的最外层(比如z轴正方向的面),直接筛选z最大的点:

max_z = all_points[:,2].max()
front_mask = np.isclose(all_points[:,2], max_z, atol=1e-6)
front_points = all_points[front_mask]

场景2:提取网格表面的正面点(通用情况)

如果要提取整个网格的表面,再筛选朝向特定方向的点:

# 先提取网格的表面(转成PolyData)
surface = grid.extract_surface()
# 计算表面点的法向量(默认指向外部)
surface.compute_normals(inplace=True)
normals = surface.point_normals

# 假设视线方向是(0,0,-1)(从z正方向看向原点)
view_dir = np.array([0,0,-1])
# 点积大于0表示法线朝向视线方向,也就是正面点
front_mask = np.dot(normals, view_dir) > 0
front_points = surface.points[front_mask]

补充:访问点对应的数值数据

如果你的网格带点数据(比如分割后的标签),直接用point_data提取:

# 假设点数据存在名为"segment_label"的字段里
point_labels = grid.point_data["segment_label"]
# 筛选y=target_y且标签为1的点
target_points = all_points[mask & (point_labels == 1)]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 12:01:43