DataSet.intersect_with_line

DataSet.intersect_with_line#

DataSet.intersect_with_line(
pointa: VectorLike[float],
pointb: VectorLike[float],
*,
tolerance: float | None = None,
deduplicate_points: bool = False,
) tuple[NumpyArray[float], NumpyArray[int]][source]#

Locate points and cell ids that intersect a line.

Added in version 0.49.

Warning

This filter internally builds and caches a vtkStaticCellLocator. If the mesh’s geometry is modified, the cache will no longer be valid.

Parameters:
pointasequence[float]

Length 3 coordinate of the start of the line.

pointbsequence[float]

Length 3 coordinate of the end of the line.

tolerancefloat, optional

The absolute tolerance to use to find cells along line. The default value is the epsilon (eps) of float32 dtype using numpy.finfo.

deduplicate_pointsbool, default: False

By default, duplicate intersection points may be returned if an intersection point is shared by multiple cells; in this case, the same point is returned for each cell. Set this to True to only return a set of unique intersection points.

Returns:
numpy.ndarray, numpy.ndarray

Tuple of arrays. The first is a 2D float array of intersection points, the second is a 1D int array with indices of the cell IDs corresponding to the intersection points. The number of intersection points always matches the number of intersection cell ids.

Examples

Intersect a line with a surface mesh.

>>> import pyvista as pv
>>> mesh = pv.Sphere()
>>> points, cell_ids = mesh.intersect_with_line([0.0, 0, 0], [1.0, 0, 0])
>>> points
array([[0.4992667, 0.       , 0.       ],
       [0.4992667, 0.       , 0.       ]], dtype=float32)
>>> cell_ids
array([   86, 1653])

Observe that two identical points are returned since two adjacent cells were intersected. Use deduplicate_points to return unique intersection points only.

>>> points, cell_ids = mesh.intersect_with_line(
...     [0.0, 0, 0], [1.0, 0, 0], deduplicate_points=True
... )
>>> points
array([[0.4992667, 0.       , 0.       ]], dtype=float32)
>>> cell_ids
array([86])

Intersect a line with a 3D cell. Here we create a single HEXAHEDRON from ImageData.

>>> mesh = pv.ImageData(dimensions=(2, 2, 2)).to_hexahedra()

Intersecting the cell returns a single intersection point where the line first “hits” the cell.

>>> pointa, pointb = (-1.0, 0.5, 0.5), (1.0, 0.5, 0.5)
>>> mesh.intersect_with_line(pointa, pointb)
(array([[0. , 0.5, 0.5]]), array([0]))

Reversing the point order returns a different intersection point on the opposide side of the cell.

>>> mesh.intersect_with_line(pointb, pointa)
(array([[1. , 0.5, 0.5]]), array([0]))

Converting the cell to a surface mesh will yield both intersections since each face is now a separate cell.

>>> mesh.extract_surface(algorithm=None).intersect_with_line(pointa, pointb)
(array([[0. , 0.5, 0.5],
       [1. , 0.5, 0.5]]), array([2, 3]))

An intersection is still found if the line coincides with one of the cell’s edges.

>>> mesh.intersect_with_line((0, 0, 0), (1, 0, 0))
(array([[0., 0., 0.]]), array([0]))

Similarly, intersections are found when the line is coincident with planar cells.

>>> mesh = pv.Plane(i_resolution=2, j_resolution=2)
>>> mesh.intersect_with_line((0, 0, 0), (1, 0, 0))
(array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]], dtype=float32), array([0, 1, 2, 3]))