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

DM-14690: Add ability to construct centered boxes #4

Merged
merged 3 commits into from Jun 26, 2018

Conversation

kfindeisen
Copy link
Member

This PR adds factory methods to Box2* that create a box centered on a (fractional) point. In the process of debugging the implementation, I discovered that the behavior of one of the Box constructors is more complex than documented, and updated the documentation to match the existing behavior.

The renaming of the Box2* constructors' first parameter from minimum to corner does not seem to have broken any keyword-based code in lsst_distrib.

The previous documentation said that the point passed to the
constructor was the lower left corner; this is only true if the
requested dimensions are positive.
* If the returned box is not empty, its center shall be within
* half a pixel of `center` in either dimension.
*/
static Box2I makeCenteredBox(Point2D const& center, Extent const& size, bool invert = true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's actually an adopted RFC for changing the default for invert in the constructors to false. That work hasn't been done primarily because it's not backwards-compatible, and hence requires some care and effort to track down breakage. Since this is a new API it might be better to use invert=false from the start or even drop that argument from this signature entirely, unless you have a use case for it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if we're going to mix factory methods and constructors in APIs, we should require that they behave consistently. Anything else will lead to user error.

I could set invert=false, but by the same token I'd be worried if the RFC didn't get implemented quickly and we were stuck with inconsistent behavior for an extended period of time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about just removing it? While I am concerned about blithely changing the default without taking care to avoid breakage, I think it's also true that invert is essentially never used, and if we don't have it here there'd be no expectation of consistency.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I consider removing it the worst option, barring an RFC to remove invert from the constructors as well.

The problem with defaults is that they tend to hide parameters from users. In this case, a developer who prefers to learn APIs from example rather than by consulting the documentation could well know that Box(myPoint, Extent(-3, -4)) returns a 3×4 box without knowing that Box(myPoint, Extent(-3, -4), false) is even a valid call. To such a developer, the absence of an invert parameter in makeCenteredBox would by no means imply that it interprets dimensions differently.

If we make invert=false by default, that wouldn't fix the problem of surprising default behavior, but at least the user would have an easy workaround once they realized what was going on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think not having invert is the right long-term signature for both the constructors and this function, and I have a slight preference for doing whatever makes it easiest to migrate to that state. But I won't object if you'd rather emphasize consistency in the current interface either a little (by retaining invert and making it default to False) or a lot (by retaining it with a default of true). The future and the present are both worthwhile things to emphasize.

* If the returned box is not empty, it shall be centered
* on `center`.
*/
static Box2D makeCenteredBox(Point2D const& center, Extent const& size, bool invert = true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same point as for Box2I on the default for invert.

Box2I::makeCenteredBox and Box2D::makeCenteredBox make it possible to
quickly construct boxes centered on a specific point within a pixel.

For consistency with the constructor behavior introduced in RFC-324,
the factory methods never invert the requested dimensions.
@kfindeisen kfindeisen merged commit 33d0814 into master Jun 26, 2018
@ktlim ktlim deleted the tickets/DM-14690 branch August 25, 2018 06:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants