提问人:Bulbasaur 提问时间:8/31/2023 最后编辑:Bulbasaur 更新时间:10/21/2023 访问量:171
PyVista 光线追踪仅检测一个交叉点
pyvista ray trace detects only one intersection
问:
我尝试使用 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()
答:
2赞
Matthew Flamm
10/21/2023
#1
poly_mesh
缺少法线,根据所使用的 VTK 方法的文档,这是必需的。https://vtk.org/doc/nightly/html/classvtkAbstractCellLocator.html#a027818794762a37868a3dccc53ad6e81
此方法假定数据集是描述闭合表面的 vtkPolyData,并且在“点”中返回的交点在入口点和出口点之间交替。
因此,如果先计算法线,它将按预期工作。
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()
评论
1赞
Matthew Flamm
10/21/2023
对于pyvista方法来说,最好先检查这一点,如果不可用,则计算法线,但在此之前可以使用此处的答案。ray_trace
评论
points = np.array([[5., 2., 6.]], dtype=float32)
[0, 10, 0, 10, 6, 12]
points = array([[5., 2., 0.]], dtype=float32)
[5., 2., 6.]
ray_trace()
vtkOBBTree.IntersectWithLine()
SetTolerance()
[1e-15, 1]
find_cells_intersecting_line