From f47aa91c49af61657c8d8bb5d8aa60556e251518 Mon Sep 17 00:00:00 2001 From: Huite Bootsma Date: Sun, 13 Oct 2024 21:30:38 +0200 Subject: [PATCH] Fix line_box_clip test. Return NO_INTERSECTION consistently across methods. --- numba_celltree/algorithms/cohen_sutherland.py | 6 ++++++ numba_celltree/algorithms/liang_barsky.py | 5 +++++ tests/test_algorithms/test_line_box_clip.py | 11 +++++++++++ 3 files changed, 22 insertions(+) diff --git a/numba_celltree/algorithms/cohen_sutherland.py b/numba_celltree/algorithms/cohen_sutherland.py index 43c20b3..c58c512 100644 --- a/numba_celltree/algorithms/cohen_sutherland.py +++ b/numba_celltree/algorithms/cohen_sutherland.py @@ -92,4 +92,10 @@ def cohen_sutherland_line_box_clip(a: Point, b: Point, box: Box) -> Tuple[Point, b = Point(x, y) k2 = get_clip(b, box) + # Recompute (dx, dy) with new points. + dx = b.x - a.x + dy = b.y - a.y + if dx == 0.0 and dy == 0.0: + return NO_INTERSECTION + return True, a, b diff --git a/numba_celltree/algorithms/liang_barsky.py b/numba_celltree/algorithms/liang_barsky.py index cde1190..5e5eb38 100644 --- a/numba_celltree/algorithms/liang_barsky.py +++ b/numba_celltree/algorithms/liang_barsky.py @@ -54,6 +54,11 @@ def liang_barsky_line_box_clip( elif t < t1: t1 = t + # TODO: Can this check be set as the first thing in the for loop for early + # exits? + if t0 == t1: + return NO_INTERSECTION + c = Point(a.x + t0 * dx, a.y + t0 * dy) d = Point(a.x + t1 * dx, a.y + t1 * dy) return True, c, d diff --git a/tests/test_algorithms/test_line_box_clip.py b/tests/test_algorithms/test_line_box_clip.py index 3d3c50a..1b4702a 100644 --- a/tests/test_algorithms/test_line_box_clip.py +++ b/tests/test_algorithms/test_line_box_clip.py @@ -130,6 +130,8 @@ def assert_expected( b = Point(*b) # c, d are the clipped points actual, actual_c, actual_d = line_clip(a, b, box) + print(actual_c, c) + print(actual_d, d) assert intersects is actual assert np.allclose(actual_c, c, equal_nan=True) assert np.allclose(actual_d, d, equal_nan=True) @@ -281,3 +283,12 @@ def assert_expected( (2.0, 0.0), (2.0, 2.0), ) + + # Diagonal line of length (1, 1), touching upper left corner. + assert_expected( + (-1.0, 1.0), + (0.0, 2.0), + False, + (np.nan, np.nan), + (np.nan, np.nan), + )