Uh oh!
There was an error while loading. Please reload this page.
Feature/cartesian spatial hash - Add support for computing barycentric coordinates from Cartesian coordinates - #1273
Feature/cartesian spatial hash - Add support for computing barycentric coordinates from Cartesian coordinates#1273fluidnumericsJoe wants to merge 15 commits into
Conversation
fluidnumericsJoe
commented
May 21, 2025
@philipc2 - I recall you mentioning in an issue (that I can't seem to find) about problems along the antimeridian with spatial hashing. I think this would be the PR to get such a test in to verify that the Cartesian coordinates for spatial hashing resolves that problem. |
This commit adds an option for indicating if a global grid is used. When enabled, this sets the spatial hash grid domain to [-pi,pi]x[-pi,pi] explicitly Support for cartesian barycentric coordinate calculation in the spatial query is now included.
…xarray into feature/cartesian-spatial-hash
fluidnumericsJoe
commented
May 23, 2025
@philipc2 - In this latest version, I've added a test that uses a global grid and runs a few spatial hash queries on either side of the antimeridian. I can get this to pass just fine, after a few adjustments to the hash table setup and when using the barycentric coordinates calculated with Cartesian coordinates. However, the changes to the hash table, which relate to the treatment deftest_is_inside():
"""Verifies simple test for points inside and outside an element."""verts= [(0.0, 90.0), (-180, 0.0), (0.0, -90)]
uxgrid=ux.open_grid(verts, latlon=True)
# Verify that a point outside the element returns a face id of -1face_ids, bcoords=uxgrid.get_spatial_hash().query([90.0, 0.0])
assertface_ids[0] ==-1# Verify that a point inside the element returns a face id of 0face_ids, bcoords=uxgrid.get_spatial_hash().query([-90.0, 0.0])
assertface_ids[0] ==0assertnp.allclose(bcoords[0], [0.25, 0.5, 0.25], atol=1e-06)In this test, there is a single face with vertices that do not cross the antimeridian; the face abuts the antimeridian. However, in this case I find that Previously, I had been sorting the There's another test that uses this same single face grid that is failing for the same reason at the moment. Last, the test involving the mpas dual grid is also failing for a similar reason (the I'm posting this in the hopes that I'm misunderstanding the output of Let me know if hopping on a screen share would be best to discuss |
fluidnumericsJoe
commented
May 23, 2025
Changing the vertex ordering so that vertices are listed counterclockwise in the |
This is necessary to obtain the correct orientation for detecting antimeridian faces
fluidnumericsJoe
commented
May 23, 2025
Now, only the mpas_dual test is failing. Is it possible that the dual grid creation is orienting vertices in the wrong direction (clockwise instead of counter-clockwise)?? |
aaronzedwick
commented
May 29, 2025
Yes, that is very possible. I think that is likely the issue. |
philipc2
commented
May 29, 2025
This is a good catch @fluidnumerics-joe I've had some issues with those MPAS grids before, it does appear to be an issue with the Grid and not your functionality. |
I'm also curious if you've had a chance to try out our recently optimized We've recently updated it to allow for a batch of points to be submitted, as opposed to needing to iterate for each point. |
fluidnumericsJoe
commented
May 29, 2025
Oh man! I'm gonna have to try this out in Parcels. I'll put together a few simple tests once we're on the other side of this PR and get back to you with results :) |
fluidnumericsJoe
commented
May 29, 2025
Would you want me to attempt to fix the issue in this PR or should we resolve that separately ? I suppose we'd need a different test to check orientation of vertices to confirm if this is indeed the issue.. |
aaronzedwick
commented
May 29, 2025
I think a separate issue would be most appropriate, since it isn’t a problem with anything you are making here. I think raising an issue with an example and tackling it separately would work best. |
fluidnumericsJoe
commented
May 29, 2025
@aaronzedwick - Got it. I'll see if I can come up with something that doesn't involve the spatial hashing where this might also crop up. |
fluidnumericsJoe
commented
Jul 21, 2025
I did some testing and found that the failures in spatial hash query on the MPAS dual grid failures are not related to vertex ordering within elements. I did the following to ensure that the vertices were ordered in the correct way While it did find that some elements indeed were clockwise and others were counter-clockwise, reversing the order of vertices did not resolve the issue. Digging into this a bit more |
philipc2
commented
Sep 17, 2025
Hi @fluidnumerics-joe Wanted to follow up and see if there's anything you need from us. If you think we should instead focus on the |
Closes#1187
Overview
This PR provides an option to set the
coordinate_systemfor theSpatialHashclass. This is specifically used to control how the barycentric coordinates are calculated. When set tocartesianwe use the cartesian coordinates to compute the barycentric coordinates.This algorithm assumes faces are planar (linear), which is equivalent to using a local linearization of the spherical coordinates transformation to compute the barycentric coordinates. Determining if a point is in a face requires (1) valid barycentric coordinates and (2) the point is in the plane spanned by the planar face. Meeting these two criteria reduces to ensuring that the projection error of a query point onto the planar face is bounded by the local spherical coordinate linearization error. Because of this, the
SpatialHashobject now has a new attribute_ll_tolerancethat is initialized in the privateSpatialHash._local_linearization_tolerancemethod.Additional private methods include
uxarray.grid.neighbors._barycentric_coordinates_cartesianuxarray.grid.neighbors._triangle_area_cartesianuxarray.grid.neighbors._local_projection_errorFor end users :
SpatialHash.coordinate_system = 'cartesian'should be used when grids are global or cross the antimeridian.DRAFT WORK IN PROGRESS
Expected Usage
PR Checklist
General
Testing
Documentation
_) and have been added todocs/internal_api/index.rstdocs/user_api/index.rstExamples
docs/examples/folderdocs/examples.rsttoctreedocs/gallery.ymlwith appropriate thumbnail photo indocs/_static/thumbnails/