From 541e79099a250b9ed1c66f717702114a42ab3f9e Mon Sep 17 00:00:00 2001 From: Connor Ferster Date: Thu, 28 Aug 2025 09:56:32 -0700 Subject: [PATCH 1/2] fix: added condition for when there is only a single singluarity functio nint he list --- src/load_distribution/load_distribution.py | 28 +++++++++++++++------- tests/test_load_distribution.py | 7 ++++++ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/load_distribution/load_distribution.py b/src/load_distribution/load_distribution.py index 9f30871..f85fa41 100644 --- a/src/load_distribution/load_distribution.py +++ b/src/load_distribution/load_distribution.py @@ -183,18 +183,27 @@ def singularities_to_polygon(los: list[Singularity], xy: bool = False) -> Polygo prev_x = None n = None # Create a list of the x-ordinates required - # Always starts on 0.0 - x_acc.append(0.0) - for idx, sing in enumerate(sorted_sings): + if len(sorted_sings) == 1: + sing = sorted_sings[0] n = sing.precision - eps = 10 ** (-2 * n) - if prev_x != sing.x0 and prev_x is not None: - x_acc.append(prev_x + eps) - if prev_x is not None and not math.isclose(prev_x, sing.x0): - x_acc.append(sing.x0) + eps = 10 ** (-2 * n - 1) + x_acc.append(sing.x0) x_acc.append(sing.x0 + eps) x_acc.append(sing.x1 - eps) - prev_x = sing.x1 + x_acc.append(sing.x1) + else: + # Always starts on 0.0 + x_acc.append(0.0) + for idx, sing in enumerate(sorted_sings): + n = sing.precision + eps = 10 ** (-2 * n) + if prev_x != sing.x0 and prev_x is not None: + x_acc.append(prev_x + eps) + if prev_x is not None and not math.isclose(prev_x, sing.x0): + x_acc.append(sing.x0) + x_acc.append(sing.x0 + eps) + x_acc.append(sing.x1 - 10*eps) + prev_x = sing.x1 # There are two scenarios: sing functions that describe trapezoids/triangles # and sing functions that describe step functions (rectangles). To ensure @@ -203,6 +212,7 @@ def singularities_to_polygon(los: list[Singularity], xy: bool = False) -> Polygo # duplicate x-ordinates. The goal is to have the minimum amount to describe the # required shape, even if that means the exact x value is omitted (because we are # keeping the value immediately to the left and immediately to the right instead). + print(f"{x_acc=}") x_acc = sorted(list(set(x_acc))) x_ord_count = Counter([round(x, 6) for x in x_acc]) to_filter = [] diff --git a/tests/test_load_distribution.py b/tests/test_load_distribution.py index 7707697..9d8a19e 100644 --- a/tests/test_load_distribution.py +++ b/tests/test_load_distribution.py @@ -196,6 +196,13 @@ def test_singularities_to_polygon(): ).wkt == "POLYGON ((0 0, 0 10, 2 10, 2 4, 4 4, 4 8, 8 8, 8 10, 10 10, 10 0, 0 0))" ) + assert ( + ld.singularities_to_polygon( + [ld.Singularity(x0=1.0, x1=3.0, m=0.0, y0=10.0, precision=6, eps=1e-12)] + + ).wkt + == "POLYGON ((1 0, 1 10, 3 10, 3 0, 1 0))" + ) def test_overlap_region_to_singularity(): From 405ee26c38728a5a4f72cbf7a2807f43937aefe0 Mon Sep 17 00:00:00 2001 From: Connor Ferster Date: Thu, 28 Aug 2025 09:57:34 -0700 Subject: [PATCH 2/2] blackify --- src/load_distribution/load_distribution.py | 2 +- tests/test_load_distribution.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/load_distribution/load_distribution.py b/src/load_distribution/load_distribution.py index f85fa41..48231f4 100644 --- a/src/load_distribution/load_distribution.py +++ b/src/load_distribution/load_distribution.py @@ -202,7 +202,7 @@ def singularities_to_polygon(los: list[Singularity], xy: bool = False) -> Polygo if prev_x is not None and not math.isclose(prev_x, sing.x0): x_acc.append(sing.x0) x_acc.append(sing.x0 + eps) - x_acc.append(sing.x1 - 10*eps) + x_acc.append(sing.x1 - 10 * eps) prev_x = sing.x1 # There are two scenarios: sing functions that describe trapezoids/triangles diff --git a/tests/test_load_distribution.py b/tests/test_load_distribution.py index 9d8a19e..fa2a46a 100644 --- a/tests/test_load_distribution.py +++ b/tests/test_load_distribution.py @@ -199,7 +199,6 @@ def test_singularities_to_polygon(): assert ( ld.singularities_to_polygon( [ld.Singularity(x0=1.0, x1=3.0, m=0.0, y0=10.0, precision=6, eps=1e-12)] - ).wkt == "POLYGON ((1 0, 1 10, 3 10, 3 0, 1 0))" )