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

Point In Face & Get Faces Containing Point #1056

Open
wants to merge 86 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 84 commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
75b88e8
Initial work
aaronzedwick Oct 24, 2024
998aa99
Switched to projection method
aaronzedwick Nov 5, 2024
7d7be67
Merge branch 'main' into zedwick/point-in-polygon
aaronzedwick Nov 5, 2024
e38ca24
Updated test cases, fixed spherical bug
aaronzedwick Nov 6, 2024
92bab4a
Added test cases
aaronzedwick Nov 6, 2024
354f821
Increased performance
aaronzedwick Nov 6, 2024
5fc59aa
Update test_geometry.py
aaronzedwick Nov 7, 2024
c4a4979
Merge branch 'main' into zedwick/point-in-polygon
aaronzedwick Nov 7, 2024
3b09973
updated api
aaronzedwick Nov 7, 2024
f7cb51c
Merge branch 'zedwick/point-in-polygon' of https://github.com/UXARRAY…
aaronzedwick Nov 7, 2024
22e84d5
Merge branch 'main' into zedwick/point-in-polygon
aaronzedwick Nov 20, 2024
4043c8b
Updated detection method
aaronzedwick Nov 20, 2024
95a0dea
Merge branch 'main' into zedwick/point-in-polygon
aaronzedwick Nov 21, 2024
5dbed72
Merge branch 'zedwick/point-in-polygon' of https://github.com/UXARRAY…
aaronzedwick Nov 21, 2024
c1eae89
Implemented new ray casting method
aaronzedwick Nov 21, 2024
586c8ad
Merge branch 'main' into zedwick/point-in-polygon
aaronzedwick Nov 26, 2024
b33326d
Added node crossing and point on edge checks
aaronzedwick Nov 27, 2024
0acaa2b
Merge branch 'main' into zedwick/point-in-polygon
aaronzedwick Nov 27, 2024
807ce07
Added docstrings, test case
aaronzedwick Nov 27, 2024
b4eba94
Merge branch 'zedwick/point-in-polygon' of https://github.com/UXARRAY…
aaronzedwick Nov 27, 2024
fa6fd8c
Update test_geometry.py
aaronzedwick Nov 27, 2024
0092258
Fixed point conversion bug
aaronzedwick Nov 27, 2024
1f6f51b
Update geometry.py
aaronzedwick Nov 27, 2024
5e0477b
Merge branch 'main' into zedwick/point-in-polygon
aaronzedwick Dec 4, 2024
acce252
Merge branch 'zedwick/point-in-polygon' of https://github.com/UXARRAY…
aaronzedwick Dec 4, 2024
d88d69f
Converted functions to numba, updated tests accordingly
aaronzedwick Dec 5, 2024
a379a1a
Added max face radius function
aaronzedwick Dec 5, 2024
fefe1e7
Added baseplate code for polygon containing code
aaronzedwick Dec 5, 2024
cc9f943
Added reference point setter
aaronzedwick Dec 6, 2024
73a1be4
Updated to use constructed edges
aaronzedwick Dec 10, 2024
8392e2c
Debugging
aaronzedwick Dec 11, 2024
6fea25e
Merge branch 'main' into zedwick/point-in-polygon
aaronzedwick Dec 11, 2024
200f4bb
Merge branch 'main' into zedwick/point-in-polygon
aaronzedwick Dec 16, 2024
e86e81c
Merge branch 'main' into zedwick/point-in-polygon
philipc2 Dec 17, 2024
a6b0dcc
Added unique face test cases
aaronzedwick Dec 18, 2024
36ef299
Fixed test cases, changed to `point_in_face`
aaronzedwick Dec 18, 2024
dc153dc
Convert face containing point to use updated function
aaronzedwick Dec 18, 2024
c5ed371
Added inverse indices, complete get_faces_containing_point
aaronzedwick Jan 2, 2025
d7318de
Merge branch 'main' into zedwick/point-in-polygon
aaronzedwick Jan 2, 2025
ab18797
Merge branch 'zedwick/point-in-polygon' of https://github.com/UXARRAY…
aaronzedwick Jan 2, 2025
47843d8
Merge branch 'main' into zedwick/point-in-polygon
aaronzedwick Jan 9, 2025
845cca6
Merge branch 'main' into zedwick/point-in-polygon
aaronzedwick Jan 9, 2025
88a602f
Updated to use inverse face indices
aaronzedwick Jan 9, 2025
c28772a
Fixed failing tests
aaronzedwick Jan 9, 2025
e055327
Moved function location
aaronzedwick Jan 9, 2025
46cecc4
Fixed circular import
aaronzedwick Jan 10, 2025
5dba109
Update grid.py
aaronzedwick Jan 10, 2025
d297a9e
updated comment
aaronzedwick Jan 13, 2025
bff6cd5
updated max_face_radius to handle variable face sizes
aaronzedwick Jan 13, 2025
67ea7b2
updated haversine and added test
aaronzedwick Jan 13, 2025
ce354fd
Added benchmark, made max_face_radius attribute for better performance
aaronzedwick Jan 14, 2025
7eeef4e
Merge branch 'main' into zedwick/point-in-polygon
aaronzedwick Jan 14, 2025
98a676d
Fixed benchmark
aaronzedwick Jan 14, 2025
66fba3d
added property, update benchmark
aaronzedwick Jan 14, 2025
f1caa3f
updated benchmark
aaronzedwick Jan 15, 2025
bfa621b
CI debugging
aaronzedwick Jan 15, 2025
274591c
Fixed indexing problem
aaronzedwick Jan 15, 2025
5f66225
added normalize to ensure consistency
aaronzedwick Jan 15, 2025
4c97d18
Add spherical support for `faces_containing_point`, added docs, api, …
aaronzedwick Jan 16, 2025
a14286a
Merge branch 'main' into zedwick/point-in-polygon
aaronzedwick Jan 16, 2025
60964fe
Fixed test cases
aaronzedwick Jan 16, 2025
e7721ae
Fixed test attempt #2
aaronzedwick Jan 16, 2025
a16b652
Fixed error with improper coordinates
aaronzedwick Jan 22, 2025
8239123
Merge branch 'main' into zedwick/point-in-polygon
aaronzedwick Jan 28, 2025
5ea0911
Updated method of subsetting and test case
aaronzedwick Jan 28, 2025
bbb7b90
Merge branch 'zedwick/point-in-polygon' of https://github.com/UXARRAY…
aaronzedwick Jan 28, 2025
23cdc74
pre-commit
aaronzedwick Jan 28, 2025
32ecaf4
fixed tests
aaronzedwick Jan 28, 2025
a3ba55c
Update test_grid.py
aaronzedwick Jan 28, 2025
f974db4
Added notebook
aaronzedwick Jan 28, 2025
2d7dcc7
Update point_in_face.ipynb
aaronzedwick Jan 28, 2025
ebfb0dd
Merge branch 'main' into zedwick/point-in-polygon
philipc2 Jan 31, 2025
0638cfb
Merge branch 'main' into zedwick/point-in-polygon
aaronzedwick Feb 3, 2025
bcb9f17
Merge branch 'main' into zedwick/point-in-polygon
philipc2 Feb 4, 2025
37feaee
Merge branch 'main' into zedwick/point-in-polygon
rajeeja Feb 4, 2025
fdb28e4
Merge branch 'main' into zedwick/point-in-polygon
philipc2 Feb 4, 2025
568689c
Merge branch 'main' into zedwick/point-in-polygon
philipc2 Feb 5, 2025
44406e2
Merge branch 'main' into zedwick/point-in-polygon
aaronzedwick Feb 5, 2025
3175c53
Added tests, updated numba, fixed bug
aaronzedwick Feb 5, 2025
683e2cc
Merge branch 'zedwick/point-in-polygon' of https://github.com/UXARRAY…
aaronzedwick Feb 5, 2025
ef22d37
Merge branch 'main' into zedwick/point-in-polygon
philipc2 Feb 6, 2025
6557c93
Merge branch 'main' into zedwick/point-in-polygon
aaronzedwick Feb 7, 2025
a93b79a
Addressed review comments
aaronzedwick Feb 7, 2025
76acbc9
Merge branch 'main' into zedwick/point-in-polygon
aaronzedwick Feb 11, 2025
c20a147
Added error tolerance for points close to nodes
aaronzedwick Feb 12, 2025
2e3dc71
turned numba on
aaronzedwick Feb 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions benchmarks/mpas_ocean.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,28 @@ def time_const_lat(self, resolution, lat_step):
self.uxgrid.cross_section.constant_latitude(lat)


class PointInPolygon:
param_names = ['resolution']
params = ['480km', '120km']

def setup(self, resolution):
self.uxgrid = ux.open_grid(file_path_dict[resolution][0])
self.uxgrid.normalize_cartesian_coordinates()

_ = self.uxgrid.face_edge_connectivity

point = np.array([0.0, 0.0, 1.0])
res = self.uxgrid.get_faces_containing_point(point)

def teardown(self, resolution):
del self.uxgrid

def time_face_search(self, resolution):
point_xyz = np.array([self.uxgrid.face_x[0].values, self.uxgrid.face_y[0].values, self.uxgrid.face_z[0].values])

self.uxgrid.get_faces_containing_point(point=point_xyz)


class ZonalAverage(DatasetBenchmark):
def setup(self, resolution, *args, **kwargs):
self.uxds = ux.open_dataset(file_path_dict[resolution][0], file_path_dict[resolution][1])
Expand Down
2 changes: 2 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ Descriptors
Grid.partial_sphere_coverage
Grid.global_sphere_coverage
Grid.triangular
Grid.max_face_radius

Attributes
~~~~~~~~~~
Expand All @@ -158,6 +159,7 @@ Methods
Grid.calculate_total_face_area
Grid.normalize_cartesian_coordinates
Grid.construct_face_centers
Grid.get_faces_containing_point

Inheritance of Xarray Functionality
-----------------------------------
Expand Down
170 changes: 170 additions & 0 deletions docs/user-guide/point_in_face.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "336fe698-c5a1-481e-9aea-9de158174a96",
"metadata": {},
"source": [
"# Point In Face"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a5dbe362-99b5-4c2d-a148-cd3816cb3334",
"metadata": {},
"outputs": [],
"source": [
"import uxarray as ux\n",
"import numpy as np\n",
"from uxarray.grid.utils import _get_cartesian_face_edge_nodes\n",
"from uxarray.grid.geometry import point_in_face\n",
"\n",
"grid_path = \"../../test/meshfiles/ugrid/quad-hexagon/grid.nc\"\n",
"grid = ux.open_grid(grid_path)"
]
},
{
"cell_type": "markdown",
"id": "30c4cff2-f760-46d3-9ff9-11f48a51cea8",
"metadata": {},
"source": [
"The function `point_in_face` determines whether or not a given face contains a point. The function parameters are as follows:\n",
"\n",
"* `edges_xyz` are the supplied edges of the face, with shape [n_edges, 2]\n",
"* `point_xyz` the cartesian point which is being checked. The dtype should be `np.float64`\n",
"* `inclusive` bool flag indicating whether to classify a point that lies on either the edges or nodes as within the face"
]
},
{
"cell_type": "markdown",
"id": "5e70681b-e7c3-4817-bcdc-3241c0f3e46e",
"metadata": {},
"source": [
"We can take a face and check to see if it's face center lies within the face."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c8c6f19a-38f2-4211-8a7b-d5f383eb5dc5",
"metadata": {},
"outputs": [],
"source": [
"# Get the face edge arrays of each face in the grid\n",
"faces_edge_cartesian = _get_cartesian_face_edge_nodes(\n",
" grid.face_node_connectivity.values,\n",
" grid.n_face,\n",
" grid.n_max_face_nodes,\n",
" grid.node_x.values,\n",
" grid.node_y.values,\n",
" grid.node_z.values,\n",
")\n",
"\n",
"face_center = np.array(\n",
" [grid.face_x.values[0], grid.face_y.values[0], grid.face_z.values[0]],\n",
" dtype=np.float64,\n",
")\n",
"point_in_face(faces_edge_cartesian[0], face_center, inclusive=True)"
]
},
{
"cell_type": "markdown",
"id": "5a656ef1-3898-4586-8ca7-3d43f7517adc",
"metadata": {},
"source": [
"We can do something similar but with the nodes of the face, to showcase the inclusive flag use."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7e686262-da40-4b86-84af-9ac2a10887ca",
"metadata": {},
"outputs": [],
"source": [
"face_node = np.array(\n",
" [grid.node_x.values[0], grid.node_y.values[0], grid.node_z.values[0]],\n",
" dtype=np.float64,\n",
")\n",
"inclusive = point_in_face(faces_edge_cartesian[0], face_node, inclusive=True)\n",
"non_inclusive = point_in_face(faces_edge_cartesian[0], face_node, inclusive=False)\n",
"\n",
"print(\n",
" f\"Node is in the face when inclusive is set to True: {inclusive}\\nNode is not in the face when inclusive is set to False: {non_inclusive}\"\n",
")"
]
},
{
"cell_type": "markdown",
"id": "c3e3ead1-40cf-44c7-84b2-ca7b4c4bbdac",
"metadata": {},
"source": [
"# Get Faces Containing Point"
]
},
{
"cell_type": "markdown",
"id": "632c7222-154e-459a-b063-98b4260729e8",
"metadata": {},
"source": [
"Inside UXarray we can also retrieve any faces that contain a given point. By simply passing in a point, the function will return an array that contains the indices of the faces that the point is contained within. A point can be contained within many faces if the point is on a node or an edge. This is because many different faces may use the same node, and an edge will likely have two faces saddling it. This function considers these points as part of the faces that contain that edge or node. If an empty array is returned, this is likely because the grid is a partial and the point is within a region that has no faces. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3f2277e6-0073-4432-a424-e86efbaee1b8",
"metadata": {},
"outputs": [],
"source": [
"# Face center\n",
"grid.get_faces_containing_point(face_center)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "39ee9098-d639-4605-b15b-9d73085498a9",
"metadata": {},
"outputs": [],
"source": [
"# Node which is apart of 3 different faces\n",
"grid.get_faces_containing_point(face_node)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "daa70b10-514c-4942-ba7b-1b76bef626f9",
"metadata": {},
"outputs": [],
"source": [
"# Pole point which this partial grid does not cover\n",
"random_xyz = np.array([0, 0, 1], dtype=np.float64)\n",
"grid.get_faces_containing_point(random_xyz)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
3 changes: 3 additions & 0 deletions docs/userguide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ These user guides provide detailed explanations of the core functionality in UXa
`Dual Mesh Construction <user-guide/dual-mesh.ipynb>`_
Construct the Dual Mesh of an unstructured grid

`Point in Face <user-guide/point_in_face.ipynb>`_
Check if a point is within a face and find the faces containing a point

Supplementary Guides
--------------------
Expand Down Expand Up @@ -119,3 +121,4 @@ These user guides provide additional details about specific features in UXarray.
user-guide/from-points.ipynb
user-guide/holoviz.ipynb
user-guide/from_file.ipynb
user-guide/point_in_face.ipynb
Loading
Loading