Skip to content

Commit

Permalink
Fixes for corner cases of `regridding.geometry.two_line_segment_inter…
Browse files Browse the repository at this point in the history
…section()`.
  • Loading branch information
byrdie committed Dec 2, 2023
1 parent 4cfe3c6 commit b6f3eb3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
12 changes: 7 additions & 5 deletions regridding/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def two_line_segment_intersection_parameters(
y_q2=y_q2,
)
if not bounding_boxes_intersect:
return math.nan, math.nan, math.nan
return math.inf, math.inf, 1

a = line_equation_2d(
x=x_q1,
Expand All @@ -278,8 +278,9 @@ def two_line_segment_intersection_parameters(
x2=x_p2,
y2=y_p2,
)
if regridding.math.sign(a) == regridding.math.sign(b):
return math.nan, math.nan, math.nan
if (a != 0) and (b != 0):
if regridding.math.sign(a) == regridding.math.sign(b):
return math.inf, math.inf, 1

c = line_equation_2d(
x=x_p1,
Expand All @@ -297,8 +298,9 @@ def two_line_segment_intersection_parameters(
x2=x_q2,
y2=y_q2,
)
if regridding.math.sign(c) == regridding.math.sign(d):
return math.nan, math.nan, math.nan
if (c != 0) and (d != 0):
if regridding.math.sign(c) == regridding.math.sign(d):
return math.inf, math.inf, 1

det = a - b
sdet = +a
Expand Down
10 changes: 7 additions & 3 deletions regridding/tests/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,15 @@ def test_bounding_boxes_intersect_2d(
@pytest.mark.parametrize(
argnames="x_p1,y_p1,x_p2,y_p2,x_q1,y_q1,x_q2,y_q2,sdet_expected,tdet_expected,det_expected",
argvalues=[
(0, 0, 1, 1, 2, 0, 3, 1, math.nan, math.nan, math.nan),
(-1, -1, 0, 0, 1, 0, 0, 1, math.nan, math.nan, math.nan),
(1, 0, 0, 1, -1, -1, 0, 0, math.nan, math.nan, math.nan),
(0, 0, 1, 1, 2, 0, 3, 1, math.inf, math.inf, 1),
(-1, -1, 0, 0, 1, 0, 0, 1, math.inf, math.inf, 1),
(1, 0, 0, 1, -1, -1, 0, 0, math.inf, math.inf, 1),
(-1, -1, 1, 1, 1, -1, -1, 1, 4, 4, 8),
(0, 0, 0, 1, 0, 0, 1, 0, 0, 0, -1),
(0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0),
(0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0),
(0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0),
(0, 0, 2, 2, 1, 1, 3, 3, 0, 0, 0),
],
)
def test_two_line_segment_intersection_parameters(
Expand Down

0 comments on commit b6f3eb3

Please sign in to comment.