-
Notifications
You must be signed in to change notification settings - Fork 1
Description
Hi, maybe relevant to your interests: I ended up porting celltree to numba because I rarely use C or C++ and I needed extra functionality (search for polygon and line intersects, not just points).
To do so, I needed a bunch of algorithms. The small ones can be found here: https://github.com/Deltares/numba_celltree/blob/main/numba_celltree/geometry_utils.py
E.g. the same point in polygon algorithm: https://github.com/Deltares/numba_celltree/blob/9da5d941f27cae2b42498206b8625ff3ff42b9e3/numba_celltree/geometry_utils.py#L93
I also included a number of other algorithms:
https://github.com/Deltares/numba_celltree/tree/main/numba_celltree/algorithms
│ 1 │ barycentric_triangle.py │
│ 2 │ barycentric_wachspress.py │
│ 3 │ cohen_sutherland.py │
│ 4 │ cyrus_beck.py │
│ 5 │ liang_barsky.py │
│ 6 │ separating_axis.py │
│ 7 │ sutherland_hodgman.py |
I use the barycentric stuff for interpolation, cyrus_beck, liang_barsky, sutherland_hodgman are for clipping. Separating axes to identify whether two (convex) polygons intersect.
It's all pretty lightweight, I defined some Point, Vector named tuples. Everything has a higher up function which vectorizes it.
Test coverage is okay -- but I'm sure it's still missing a lot of (literal) edge cases.
It uses numba instead of Cython, because I find it a lot easier to distribute and to debug (just switch JIT compiling off temporarily).
If you were okay with numba, you could just copy it. But porting to Cython should also be straightforward -- depends on where you'd like to go with this project. (There's quite some active development on the Python compilation front, see e.g. https://lpython.org/ or mojo, etc. -- I might end up switching compilers). Requirements are minimal: just numba and numpy.
Shared interests
It's worth mentioning: I think my interests are very similar, I have numpy arrays that represent spatial data and I'd like to operate on them efficiently. Shapely is generally too expensive. Currently, whenever I need something new I just add it to numba_celltree because most of it goes through there. But ideally, we'd have a nice Python package that generally available. I'm also open to renaming numba_celltree and accepting PRs of course!
Ideally, I'd also like to support 3D geometry to add Ugrid3D unstructured grids support to numba_celltree. My gut feeling is that that's a lot more work than 2D, and it would be a shame to have it under-generalized or under-utilized. It's also possible that using vtk is just the much easier approach for 3D, but I haven't researched that very carefully.
Julia
Finally: whenever you're looking for readable implementations of computational geometry, it's worth checking out the Julia ecosystem:
https://github.com/JuliaGeometry
https://github.com/JuliaGeo/GeometryOps.jl
The Julia community, though small, seems to be moving forward a lot faster, because a straightforward choice to implement things in Julia and get good performance. In Python you have to make choices about Cython, C, C++, Fortran, Rust, or some JIT compilers which support a subset of Python -- all of which seems to generate so much friction and scatters efforts so that things don't get off the ground. Calling Julia from Python isn't very attractive at this moment, but the Julia people seem to making exciting progress at static compilation. That would probably be my preferred solution -- at that point I'd port the celltree stuff again, but then to Julia. Until then, porting from Julia to Python is often very straightforward.