Skip to content

pybind: Add R2 bindings#546

Merged
jmr merged 8 commits intogoogle:masterfrom
deustis:deustis/r2_bindings
Mar 19, 2026
Merged

pybind: Add R2 bindings#546
jmr merged 8 commits intogoogle:masterfrom
deustis:deustis/r2_bindings

Conversation

@deustis
Copy link
Contributor

@deustis deustis commented Feb 22, 2026

Add pybind11 bindings for R1Interval, R2Point, and R2Rect

(Part of a series of addressing #524)

@deustis deustis changed the title pybind: Add S1Interval bindings pybind: Add R2 bindings Feb 22, 2026
@deustis deustis marked this pull request as draft February 22, 2026 19:29
@deustis
Copy link
Contributor Author

deustis commented Feb 22, 2026

This is a child of PR #534. Diffs are showing both PRs... not sure how to set the upstream to my fork. Leaving this in draft until we land the upstream.

if (x_empty != y_empty) {
throw py::value_error(
"Invalid R2Rect: x and y intervals must be both empty or both "
"non-empty");
Copy link
Member

Choose a reason for hiding this comment

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

more info here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Member

@jmr jmr left a comment

Choose a reason for hiding this comment

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

I'm also not sure about the "expose all the things" approach. We've never exposed R2* in the old SWIG or new google3 bindings and no one complained.

Maybe you want to do a useful subset / the SWIG subset first.

"order.\n\n"
"Vertex 0 is in the lower-left corner. The argument is reduced "
"modulo 4.")
.def("vertex_ij",
Copy link
Member

Choose a reason for hiding this comment

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

A single overloaded vertex might make sense. I'm not sure and don't have strong feelings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I left a note about this here:
https://github.com/google/s2geometry/pull/546/changes#diff-68b4b8a36a193cbacbd9e072b0026059a3554ed10ee9779d07856a8110628193R61

Python overloads are generally frowned on but if you prefer to match the C++ behavior I could get behind that too. Or we could drop this method as it is pretty obscure.

Merge branch 'master' into deustis/r2_bindings.

Add pybind11 bindings for R1Interval, R2Point, and R2Rect.

(Part of a series of addressing google#524.)
@deustis deustis force-pushed the deustis/r2_bindings branch from 8a5bb2b to f3c5782 Compare March 5, 2026 16:57
@deustis
Copy link
Contributor Author

deustis commented Mar 5, 2026

I'm also not sure about the "expose all the things" approach. We've never exposed R2* in the old SWIG or new google3 bindings and no one complained. Maybe you want to do a useful subset / the SWIG subset first.

I believe R1Interval is exposed in SWIG, but you're right that the others are not. It looks like methods on S2CellID like R2Point GetCenterUV() const; are also not exposed. I was trying to be a bit more comprehensive but I'll defer to your judgement...

How about this?: Let's try to land this PR and then I'll move on to some more useful classes like S2LatLng, S2CellId, and S2Cell. If I hit more of these missing primitives we can have a discussion before exposing them.

@deustis deustis marked this pull request as ready for review March 5, 2026 17:16
@deustis
Copy link
Contributor Author

deustis commented Mar 12, 2026

@jwr, please see above and let me know how you'd like to proceed?:
#546 (comment)


**Method Overloads:**
- Some methods accept more than one type for the same argument. For example, `R2Rect.contains()` accepts either an `R2Point` or another `R2Rect`.
- When a C++ class has overloads with significantly different behavior, the Python bindings use distinct method names instead. For example, `R2Rect` provides `vertex(k)` for CCW vertex index and `vertex_ij(i, j)` for axis-direction vertex access.
Copy link
Member

Choose a reason for hiding this comment

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

Not for this PR, but if we consider these "significantly different", we should rename them in C++, too. I was thinking of them as pretty much the same, but it could make sense to rename in C++.

@jmr
Copy link
Member

jmr commented Mar 14, 2026

One thing would be to support __hash__, but let's just finish this one and come back to that later. #553

@deustis deustis requested a review from jmr March 16, 2026 18:09
@deustis
Copy link
Contributor Author

deustis commented Mar 18, 2026

@jmr, friendly nudge, let me know you'd like to see any other changes on this?

@jmr jmr merged commit fde443b into google:master Mar 19, 2026
9 checks passed
@jmr
Copy link
Member

jmr commented Mar 19, 2026

Sorry about the delay. Thanks for the contribution!

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.

2 participants