Skip to content

Commit 9622349

Browse files
Prefer barycentric_coordinates implementation in parcels.spatialhash
Also removed dependencies on uxarray internal API in the process of making this change
1 parent 88e416b commit 9622349

File tree

1 file changed

+60
-47
lines changed

1 file changed

+60
-47
lines changed

parcels/uxgrid.py

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
import numpy as np
66
import uxarray as ux
7-
from uxarray.grid.coordinates import _lonlat_rad_to_xyz
8-
from uxarray.grid.neighbors import _barycentric_coordinates
97

10-
from parcels.field import FieldOutOfBoundError # Adjust import as necessary
8+
from parcels.field import FieldOutOfBoundError
9+
from parcels.spatialhash import _barycentric_coordinates
1110
from parcels.xgrid import _search_1d_array
1211

1312
from .basegrid import BaseGrid
@@ -141,52 +140,66 @@ def _get_barycentric_coordinates_cartesian(self, y, x, fi):
141140
axis=-1,
142141
)
143142

144-
bcoord = np.asarray(_barycentric_coordinates_cartesian(nodes, cart_coord))
143+
bcoord = np.asarray(_barycentric_coordinates(nodes, cart_coord))
145144

146145
return bcoord
147146

148147

149-
def _barycentric_coordinates_cartesian(nodes, point, min_area=1e-8):
148+
def _lonlat_rad_to_xyz(
149+
lon,
150+
lat,
151+
):
152+
"""Converts Spherical latitude and longitude coordinates into Cartesian x,
153+
y, z coordinates.
150154
"""
151-
Compute the barycentric coordinates of a point P inside a convex polygon using area-based weights.
152-
So that this method generalizes to n-sided polygons, we use the Waschpress points as the generalized
153-
barycentric coordinates, which is only valid for convex polygons.
154-
155-
Parameters
156-
----------
157-
nodes : numpy.ndarray
158-
Cartesian coordinates (x,y,z) of each corner node of a face
159-
point : numpy.ndarray
160-
Cartesian coordinates (x,y,z) of the point
161-
162-
Returns
163-
-------
164-
numpy.ndarray
165-
Barycentric coordinates corresponding to each vertex.
166-
167-
"""
168-
n = len(nodes)
169-
sum_wi = 0
170-
w = []
171-
172-
for i in range(0, n):
173-
vim1 = nodes[i - 1]
174-
vi = nodes[i]
175-
vi1 = nodes[(i + 1) % n]
176-
a0 = _triangle_area_cartesian(vim1, vi, vi1)
177-
a1 = max(_triangle_area_cartesian(point, vim1, vi), min_area)
178-
a2 = max(_triangle_area_cartesian(point, vi, vi1), min_area)
179-
sum_wi += a0 / (a1 * a2)
180-
w.append(a0 / (a1 * a2))
181-
182-
barycentric_coords = [w_i / sum_wi for w_i in w]
183-
184-
return barycentric_coords
185-
186-
187-
def _triangle_area_cartesian(A, B, C):
188-
"""Compute the area of a triangle given by three points."""
189-
d1 = B - A
190-
d2 = C - A
191-
d3 = np.cross(d1, d2)
192-
return 0.5 * np.linalg.norm(d3)
155+
x = np.cos(lon) * np.cos(lat)
156+
y = np.sin(lon) * np.cos(lat)
157+
z = np.sin(lat)
158+
159+
return x, y, z
160+
161+
162+
# def _barycentric_coordinates_cartesian(nodes, point, min_area=1e-8):
163+
# """
164+
# Compute the barycentric coordinates of a point P inside a convex polygon using area-based weights.
165+
# So that this method generalizes to n-sided polygons, we use the Waschpress points as the generalized
166+
# barycentric coordinates, which is only valid for convex polygons.
167+
168+
# Parameters
169+
# ----------
170+
# nodes : numpy.ndarray
171+
# Cartesian coordinates (x,y,z) of each corner node of a face
172+
# point : numpy.ndarray
173+
# Cartesian coordinates (x,y,z) of the point
174+
175+
# Returns
176+
# -------
177+
# numpy.ndarray
178+
# Barycentric coordinates corresponding to each vertex.
179+
180+
# """
181+
# n = len(nodes)
182+
# sum_wi = 0
183+
# w = []
184+
185+
# for i in range(0, n):
186+
# vim1 = nodes[i - 1]
187+
# vi = nodes[i]
188+
# vi1 = nodes[(i + 1) % n]
189+
# a0 = _triangle_area_cartesian(vim1, vi, vi1)
190+
# a1 = max(_triangle_area_cartesian(point, vim1, vi), min_area)
191+
# a2 = max(_triangle_area_cartesian(point, vi, vi1), min_area)
192+
# sum_wi += a0 / (a1 * a2)
193+
# w.append(a0 / (a1 * a2))
194+
195+
# barycentric_coords = [w_i / sum_wi for w_i in w]
196+
197+
# return barycentric_coords
198+
199+
200+
# def _triangle_area_cartesian(A, B, C):
201+
# """Compute the area of a triangle given by three points."""
202+
# d1 = B - A
203+
# d2 = C - A
204+
# d3 = np.cross(d1, d2)
205+
# return 0.5 * np.linalg.norm(d3)

0 commit comments

Comments
 (0)