Skip to content

Commit

Permalink
Fix line_box_clip test. Return NO_INTERSECTION consistently across me…
Browse files Browse the repository at this point in the history
…thods.
  • Loading branch information
Huite committed Oct 13, 2024
1 parent cc7567f commit f47aa91
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions numba_celltree/algorithms/cohen_sutherland.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions numba_celltree/algorithms/liang_barsky.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions tests/test_algorithms/test_line_box_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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),
)

0 comments on commit f47aa91

Please sign in to comment.