Skip to content

Commit

Permalink
Fix missed ray traces in multi_ray_trace when origin points are out…
Browse files Browse the repository at this point in the history
…side bounding box (#5957)

* In multi_ray_trace retry, increase the length to the second point by the distance from the origin point to the centre of the PolyData,ensuring the raytrace will always pass through the full extent of the PolyData.

Resolves #5587

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* update test_multi_ray_trace so it properly tests the new retry code

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update tests/core/test_polydata.py

Co-authored-by: Tetsuo Koyama <tkoyama010@gmail.com>

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Tetsuo Koyama <tkoyama010@gmail.com>
  • Loading branch information
3 people committed Apr 26, 2024
1 parent a038e74 commit e39534f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
11 changes: 10 additions & 1 deletion pyvista/core/filters/poly_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2294,7 +2294,16 @@ def multi_ray_trace(
unit_directions = directions_retry / np.linalg.norm(
directions_retry, axis=1, keepdims=True
)
second_points = origins_retry + unit_directions * self.length # shape (n_retry, 3)

origin_to_centre_vector = (
(self.bounds[0] + self.bounds[1]) / 2 - origins_retry[0],
(self.bounds[2] + self.bounds[3]) / 2 - origins_retry[1],
(self.bounds[4] + self.bounds[5]) / 2 - origins_retry[2],
)
origin_to_centre_length = np.linalg.norm(origin_to_centre_vector)
second_points = origins_retry + unit_directions * (
self.length + origin_to_centre_length
) # shape (n_retry, 3)

for id_r, origin, second_point in zip(retry_ray_indices, origins_retry, second_points):
locs, indices = self.ray_trace(origin, second_point, first_point=first_point)
Expand Down
18 changes: 15 additions & 3 deletions tests/core/test_polydata.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from math import pi
import os
import pathlib
from unittest.mock import patch
import warnings

import numpy as np
Expand Down Expand Up @@ -296,14 +297,25 @@ def test_ray_trace_origin():
def test_multi_ray_trace(sphere):
pytest.importorskip('rtree')
pytest.importorskip('pyembree')
pytest.importorskip('trimesh')
origins = [[1, 0, 1], [0.5, 0, 1], [0.25, 0, 1], [0, 0, 1]]
trimesh = pytest.importorskip('trimesh')
origins = [[1, 0, 1], [0.5, 0, 1], [0.25, 0, 1], [0, 0, 5]]
directions = [[0, 0, -1]] * 4
points, ind_r, ind_t = sphere.multi_ray_trace(origins, directions, retry=True)
points, ind_r, ind_t = sphere.multi_ray_trace(origins, directions)
assert np.any(points)
assert np.any(ind_r)
assert np.any(ind_t)

# patch embree to test retry
with patch.object(
trimesh.ray.ray_pyembree.RayMeshIntersector,
'intersects_location',
return_value=[np.array([])] * 3,
):
points, ind_r, ind_t = sphere.multi_ray_trace(origins, directions, retry=True)
assert len(points) == 4
assert len(ind_r) == 4
assert len(ind_t) == 4

# check non-triangulated
mesh = pv.Cylinder()
with pytest.raises(NotAllTrianglesError):
Expand Down

0 comments on commit e39534f

Please sign in to comment.