-
Notifications
You must be signed in to change notification settings - Fork 44
Feature/cartesian spatial hash - Add support for computing barycentric coordinates from Cartesian coordinates #1273
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
base: main
Are you sure you want to change the base?
Conversation
@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
@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 def test_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 -1
face_ids, bcoords = uxgrid.get_spatial_hash().query([90.0, 0.0])
assert face_ids[0] == -1
# Verify that a point inside the element returns a face id of 0
face_ids, bcoords = uxgrid.get_spatial_hash().query([-90.0, 0.0])
assert face_ids[0] == 0
assert np.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 |
Changing the vertex ordering so that vertices are listed counterclockwise in the
|
This is necessary to obtain the correct orientation for detecting antimeridian faces
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)?? |
Yes, that is very possible. I think that is likely the issue. |
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. |
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 :) |
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.. |
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. |
@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. |
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 |
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_system
for theSpatialHash
class. This is specifically used to control how the barycentric coordinates are calculated. When set tocartesian
we 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
SpatialHash
object now has a new attribute_ll_tolerance
that is initialized in the privateSpatialHash._local_linearization_tolerance
method.Additional private methods include
uxarray.grid.neighbors._barycentric_coordinates_cartesian
uxarray.grid.neighbors._triangle_area_cartesian
uxarray.grid.neighbors._local_projection_error
For 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.rst
docs/user_api/index.rst
Examples
docs/examples/
folderdocs/examples.rst
toctreedocs/gallery.yml
with appropriate thumbnail photo indocs/_static/thumbnails/