Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions spatialmath/geom3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,8 +1149,11 @@ def intersect_volume(self, bounds: ArrayLike6) -> Tuple[Points3, Rn]:
The number of
columns N is either:

- 0, when the line is outside the plot volume or,
- 2 when the line pierces the bounding volume.
- 0, when the line is outside the plot volume,
- 1, when the line touches the volume at a single point, or
- 2, when the line pierces the bounding volume.

Edge and corner intersections are returned only once.


See also :meth:`plot` :meth:`point`
Expand Down Expand Up @@ -1200,8 +1203,15 @@ def intersect_volume(self, bounds: ArrayLike6) -> Tuple[Points3, Rn]:

# print(' HIT');

# put them in ascending order
intersections.sort()
# A convex cuboid has at most two distinct boundary intersections.
# Edge/corner hits can be found once for each incident face. Keep the
# extremal parameters without a tolerance that could merge a thin chord.
intersections = sorted(float(lam[0]) for lam in intersections)
if len(intersections) > 1:
if intersections[0] == intersections[-1]:
intersections = intersections[:1]
else:
intersections = [intersections[0], intersections[-1]]
p = self.point(intersections)

return namedtuple("intersect_volume", "p lam")(p, intersections)
Expand Down
43 changes: 43 additions & 0 deletions tests/test_line_volume_unique.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import numpy as np
import numpy.testing as nt
import pytest

from spatialmath import Line3


@pytest.mark.parametrize("scale", [1, -1, 1e-8, 1e8])
@pytest.mark.parametrize("direction", [[1, 1, 1], [1, 1, 0], [1, 0, 0]])
def test_volume_unique_chord_endpoints(scale, direction):
direction = np.array(direction, dtype=float)
line = Line3.PointDir([0, 0, 0], scale * direction)
result = line.intersect_volume(np.array([-1, 1, -1, 1, -1, 1]))
assert result.p.shape == (3, 2)
assert np.asarray(result.lam).shape == (2,)
expected = np.column_stack((-direction, direction))
if scale < 0:
expected = expected[:, ::-1]
nt.assert_allclose(result.p, expected, atol=1e-14)
nt.assert_allclose(line.point(result.lam), result.p)
assert result.lam[0] < result.lam[1]


def test_volume_single_vertex_tangency():
line = Line3.PointDir([1, 1, 0], [1, -1, 0])
result = line.intersect_volume(np.array([-1, 1, -1, 1, -1, 1]))
nt.assert_allclose(result.p, [[1], [1], [0]])
assert np.asarray(result.lam).shape == (1,)


def test_volume_outside():
line = Line3.PointDir([0, 2, 0], [1, 0, 0])
result = line.intersect_volume(np.array([-1, 1, -1, 1, -1, 1]))
assert result.p.shape == (3, 0)
assert len(result.lam) == 0


def test_volume_thin_chord_remains_two_points():
line = Line3.PointDir([0, 0, 0], [1, 0, 0])
result = line.intersect_volume(np.array([-1e-12, 1e-12, -1, 1, -1, 1]))
assert result.p.shape == (3, 2)
nt.assert_allclose(result.p[0], [-1e-12, 1e-12], atol=0)
assert result.lam[0] < result.lam[1]