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

PyVista射线追踪仅检测到一个交点的问题求助

为何PyVista射线检测仅捕获到立方体的一个交点?

你遇到的问题源于对拉伸生成的闭合网格进行了多余的extract_surface()和triangulate()操作,导致网格的面结构或法线方向异常,使得射线检测只识别到一个交点。

具体原因:

  • polygon.extrude(..., capping=True)已经生成了闭合的立方体表面网格(包含底面、顶面和四个侧面),无需再调用extract_surface()——这个重复操作会引入不必要的网格结构变化。
  • 后续的triangulate()虽然将四边形面转为三角形,但可能导致顶面的三角形法线方向朝向网格内部,VTK的射线检测默认会忽略与法线方向反向的交点(即射线穿出网格时的交点)。

解决方案

直接使用拉伸生成的原始闭合网格进行射线检测,无需额外的表面提取和三角化操作。修改后的代码如下:

import numpy as np
import pyvista as pv

points = [(0, 0), (0, 10), (10, 10), (10, 0)]

def points_2d_to_poly(points, z):
        """Convert a sequence of 2d coordinates to polydata with a polygon."""
        faces = [len(points), *range(len(points))]
        poly = pv.PolyData([p + (z,) for p in points], faces=faces)
        return poly

# 生成闭合立方体网格,无需额外处理
polygon = points_2d_to_poly(points, 0.0)
polygon_extruded = polygon.extrude((0, 0, 6.0), capping=True)

# Define line segment
start = [5, 2, -10]
stop = [5, 2, 10]

# 直接使用原始拉伸网格进行射线检测
points, ind = polygon_extruded.ray_trace(start, stop, first_point=False)

# Create geometry to represent ray trace
ray = pv.Line(start, stop)
intersection = pv.PolyData(points)

# Render the result
p = pv.Plotter()
p.add_mesh(polygon_extruded, show_edges=True, opacity=0.5, color="w", lighting=False, label="Test Mesh")
p.add_mesh(ray, color="blue", line_width=5, label="Ray Segment")
p.add_mesh(intersection, color="maroon", point_size=25, label="Intersection Points")
p.add_legend()
p.show()

补充说明

如果确实需要使用三角化网格(比如后续有其他操作需求),可以在生成拉伸网格后直接三角化,跳过extract_surface():

poly_mesh = polygon_extruded.triangulate()
points, ind = poly_mesh.ray_trace(start, stop, first_point=False)

这样也能正确捕获到两个交点,因为原始拉伸网格的法线方向是正确的,三角化不会改变法线方向。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 07:35:40