python - pyvista 射线追踪仅检测到一个交叉点

标签 python polygon vtk pyvista

我尝试使用 pyvista 检测射线与多边形的交点。在这种情况下,多边形是立方体。立方体由 2d 中的点定义,然后进行挤压。为什么这个简单的例子(光线与立方体相交)只检测到一个相交?

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)
poly_surface = polygon_extruded.extract_surface()
poly_mesh = poly_surface.triangulate()

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

# Perform ray trace
points, ind = poly_mesh.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(poly_mesh, 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()

最佳答案

poly_mesh 法线缺失,这是根据所使用的 vtk 方法的文档所必需的。 https://vtk.org/doc/nightly/html/classvtkAbstractCellLocator.html#a027818794762a37868a3dccc53ad6e81

This method assumes that the data set is a vtkPolyData that describes a closed surface, and the intersection points that are returned in 'points' alternate between entrance points and exit points.

因此,如果首先计算法线,它就会按预期工作。

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)
poly_surface = polygon_extruded.extract_surface()
# Note this line and following line are the only changes
poly_mesh = poly_surface.triangulate().compute_normals()

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

# Perform ray trace
points, ind = poly_mesh.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(poly_mesh, 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()

关于python - pyvista 射线追踪仅检测到一个交叉点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77013612/

相关文章:

c++ - vtkStandardNewMacro 给出错误 C4430 : missing type specifier

rendering - 使用 VTK 渲染的 3D 模型的 2D 边界框

Python:有没有办法安排 Celery 任务只在特定时间范围内运行?

python - 如何知道 Celery 中事件组 {task} 是否启用或禁用

Python:在整数的二进制表示中查找最长的二进制间隙

polygon - 在固定位置用最少数量的重叠多边形覆盖多边形

python - 使用超网格搜索和 10 倍 CV 调整参数后,随机森林模型的 AUC 更低

c++ - 在 OpenGL 中使用多边形绘制二维粗圆弧

java - 通过插值在重心坐标处获得适当的 UV

c++ - 如何获得两个组合的 vtkPolyData 表面的边界?