Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement more detailed __lt__ for component sorting #1875

Open
mgjarrett opened this issue Sep 13, 2024 · 1 comment · May be fixed by #1882
Open

Implement more detailed __lt__ for component sorting #1875

mgjarrett opened this issue Sep 13, 2024 · 1 comment · May be fixed by #1882
Labels
feature request Smaller user request

Comments

@mgjarrett
Copy link
Contributor

mgjarrett commented Sep 13, 2024

Components on a block are sorted by their getBoundingCircleOuterDiameter.

def __lt__(self, other):
"""
True if a circle encompassing this object has a smaller diameter than one encompassing another component.
This allows sorting because the Python sort functions only use this method.
"""
thisOD = self.getBoundingCircleOuterDiameter(cold=True)
thatOD = other.getBoundingCircleOuterDiameter(cold=True)
try:
return thisOD < thatOD
except Exception:
raise ValueError(
"Components 1 ({} with OD {}) and 2 ({} and OD {}) cannot be ordered because their "
"bounding circle outer diameters are not comparable.".format(
self, thisOD, other, thatOD
)
)

Sometimes, two components have the same outer diameter, in which case the sorting done by __lt__ will depend on the order of comparison. i.e.,

a < b evaluates to False
b < a also evaluates to False

As a result,

sorted([a, b]) = [a, b]
while
sorted([b, a]) = [b, a]

Ideally, the outcome of sorted will not be dependent on the order of the input.

We should fall back to getCircleInnerDiameter for comparison when sorting components when getBoundingCircleOuterDiameter is equivalent. I pushed an example of what I think the sorting should do here:

main...component_sort

@mgjarrett mgjarrett added enhancement New feature or request feature request Smaller user request and removed enhancement New feature or request labels Sep 13, 2024
@john-science
Copy link
Member

Love it.

Improved consistency, I support this idea.

@mgjarrett mgjarrett linked a pull request Sep 16, 2024 that will close this issue
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Smaller user request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants