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

Add spatial hashing #1169

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

fluidnumerics-joe
Copy link
Collaborator

@fluidnumerics-joe fluidnumerics-joe commented Feb 24, 2025

Closes #1126

Overview

This PR adds the SpatialHash class in the neighbors.py module. This class is used to encapsulate the necessary information and methods for conducting unstructured grid searches using spatial hashes. As part of the initialization of the SpatialHash object, an associated unstructured grid is used to construct a uniformly spaced structured grid, called the "hash grid", and relate cell indices in the hash grid to elements in the unstructured grid. This hash table is a critical element of the spatial hash grid search as it is used to create a short-list of elements to search within.

The SpatialHash.query method returns the element id that a point (or element ids that a list of points) reside within alonside the barycentric coordinates. I've included helper methods to compute the barycentric coordinates of a point in relation to a given unstructured grid face (it works for convex faces n>=3 vertices). The barycentric coordinates are used to determine if a point is inside or outside a face; if all the barycentric coordinates are between [0,1], then a point is inside a face.

Currently, only spherical coordinates (in radians or degrees) are supported for this search.

Expected Usage

import uxarray as ux
grid_path = "../../test/meshfiles/fesom/soufflet-netcdf/grid.nc"
uxgrid = ux.open_grid(grid_path)

uxds = ux.open_dataset(grid_path, data_path)

# Construct the `SpatialHash` object
spatial_hash = uxgrid.get_spatial_hash(
    reconstruct="False",
)

# Query the `SpatialHash` for the face id and the barycentric coordinates.
face_ids, bcoords = spatial_hash.query([0.9, 1.8])

PR Checklist

General

  • An issue is linked created and linked
  • Add appropriate labels
  • Filled out Overview and Expected Usage (if applicable) sections

Testing

  • Adequate tests are created if there is new functionality
  • Tests cover all possible logical paths in your function
  • Tests are not too basic (such as simply calling a function and nothing else)

Documentation

  • Docstrings have been added to all new functions
  • Docstrings have updated with any function changes
  • Internal functions have a preceding underscore (_) and have been added to docs/internal_api/index.rst
  • User functions have been added to docs/user_api/index.rst

Examples

  • Any new notebook examples added to docs/examples/ folder
  • Clear the output of all cells before committing
  • New notebook files added to docs/examples.rst toctree
  • New notebook files added to new entry in docs/gallery.yml with appropriate thumbnail photo in docs/_static/thumbnails/

fluidnumerics-joe and others added 4 commits February 18, 2025 16:38
I attempted to stay close to the KD-Tree and BallTree API. However, the
purpose of the SpatialHash class is to help locate the faces that a
list of coordinates lie within. Because of this, the intent of the
`query` function for the SpatialHash is a bit different than that for
the KD and Ball trees.

Additional support routines are provided for assessing whether a point
is inside or outside a polygon. This is done by calculating the
barycentric coordinates of a point in a convex polygon. The method is
defined so that the sum of the barycentric coordinates is exactly one.
Because of this, if any of the barycentric coordinates are negative, we
immediately know that a point is outside the polygon.

All coordinate calculations are done using (lon,lat) in radians for the
SpatialHash class. Cartesian coordinates are not supported in this
commit.
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@philipc2 philipc2 added the new feature New feature or request label Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggested Feature : Spatial Hashing and Point Cloud Interpolation
2 participants