Skip to content

Commit ac3be27

Browse files
committed
fix: added all required points, filtered out unnecesary points
1 parent b6ffb2b commit ac3be27

File tree

4 files changed

+500
-14
lines changed

4 files changed

+500
-14
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ build-backend = "flit_core.buildapi"
1818
[dependency-groups]
1919
dev = [
2020
"black>=25.1.0",
21+
"ipykernel>=6.30.1",
2122
"pytest>=8.4.1",
2223
]

src/load_distribution/load_distribution.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from __future__ import annotations
2+
from collections import Counter
3+
import math
24
from typing import Optional
35
from dataclasses import dataclass
46
import numpy.typing as npt
@@ -180,26 +182,41 @@ def singularities_to_polygon(los: list[Singularity], xy: bool = False) -> Polygo
180182
x_acc = []
181183
prev_x = None
182184
n = None
183-
print(sorted_sings)
185+
# Create a list of the x-ordinates required
186+
# Always starts on 0.0
187+
x_acc.append(0.0)
184188
for idx, sing in enumerate(sorted_sings):
185189
n = sing.precision
186190
eps = 10 ** (-2 * n)
187191
if prev_x != sing.x0 and prev_x is not None:
188192
x_acc.append(prev_x + eps)
189-
if sing.m == 0: # If the slope is 0.0, we have a step function which needs to be treated differently
193+
if prev_x is not None and not math.isclose(prev_x, sing.x0):
190194
x_acc.append(sing.x0)
191-
x_acc.append(sing.x0 + eps)
192-
x_acc.append(sing.x1 - eps)
193-
x_acc.append(sing.x1)
194-
else:
195-
x_acc.append(sing.x0)
196-
x_acc.append(sing.x1-eps)
197-
x_acc.append(sing.x1)
195+
x_acc.append(sing.x0 + eps)
196+
x_acc.append(sing.x1 - eps)
198197
prev_x = sing.x1
199198

199+
# There are two scenarios: sing functions that describe trapezoids/triangles
200+
# and sing functions that describe step functions (rectangles). To ensure
201+
# we get enough points to fully describe each, we end up getting too many
202+
# x ordinates in some cases. The below is a filter to look for the unnecessary
203+
# duplicate x-ordinates. The goal is to have the minimum amount to describe the
204+
# required shape, even if that means the exact x value is omitted (because we are
205+
# keeping the value immediately to the left and immediately to the right instead).
200206
x_acc = sorted(list(set(x_acc)))
207+
x_ord_count = Counter([round(x, 6) for x in x_acc])
208+
to_filter = []
209+
for key, count in x_ord_count.items():
210+
if count == 3:
211+
to_filter.append(key)
212+
for filter_val in to_filter:
213+
index = x_acc.index(filter_val)
214+
x_acc.pop(index)
215+
216+
# Now, for every x value, compute the corresponding y value
201217
y_acc = [sum([sing(x) for sing in sorted_sings]) for x in x_acc[:-1]]
202-
y_acc += [0.0]
218+
# Always ends on 0.0
219+
y_acc.append(0.0)
203220
if xy:
204221
return x_acc, y_acc
205222
else:

tests/test_load_distribution.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,25 +175,26 @@ def test_singularities_to_polygon():
175175

176176
assert (
177177
ld.singularities_to_polygon(square_45_sings[0] + square_45_sings[1]).wkt
178-
== "POLYGON ((0 0, 0 0, 5 10, 5 10, 10 0, 10 0, 0 0))"
178+
== "POLYGON ((0 0, 0 0, 5 10, 5 10, 10 0, 0 0))"
179+
# == "POLYGON ((0 0, 5 10, 5 10, 10 0, 10 0, 0 0))"
179180
)
180181
assert (
181182
ld.singularities_to_polygon(
182183
square_pac_man_sings[0] + square_pac_man_sings[1]
183184
).wkt
184-
== "POLYGON ((0 0, 0 10, 5 10, 5 10, 10 0, 10 0, 10 0, 0 0))"
185+
== "POLYGON ((0 0, 0 10, 5 10, 5 10, 10 0, 10 0, 0 0))"
185186
)
186187
assert (
187188
ld.singularities_to_polygon(
188189
square_bow_tie_sings[0] + square_bow_tie_sings[1]
189190
).wkt
190-
== "POLYGON ((0 0, 0 0, 4 10, 4 10, 6 10, 6 10, 10 0, 10 0, 10 0, 0 0))"
191+
== "POLYGON ((0 0, 0 0, 4 10, 4 10, 6 10, 6 10, 10 0, 10 0, 0 0))"
191192
)
192193
assert (
193194
ld.singularities_to_polygon(
194195
square_with_L_hole_sings[0] + square_with_L_hole_sings[1]
195196
).wkt
196-
== "POLYGON ((0 0, 0 10, 2 10, 2 4, 4 4, 4 8, 8 8, 8 8, 8 10, 10 10, 10 0, 0 0))"
197+
== "POLYGON ((0 0, 0 10, 2 10, 2 4, 4 4, 4 8, 8 8, 8 10, 10 10, 10 0, 0 0))"
197198
)
198199

199200

0 commit comments

Comments
 (0)