Skip to content

Commit

Permalink
Merge branch 'main' into feature/GRIDEDIT-1407_api_for_edge_length
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacarniato authored Sep 4, 2024
2 parents cd2a78b + fe2c317 commit e4c864b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
45 changes: 45 additions & 0 deletions meshkernel/meshkernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,51 @@ def mesh2d_get_face_polygons(self, num_edges: int) -> GeometryList:

return face_polygons

def mesh2d_get_filtered_face_polygons(
self, property: Mesh2d.Property, min_value: float, max_value: float
) -> GeometryList:
"""Gets the polygons matching the metric value within the minimum and maximum value.
Args:
property (Mesh2d.Property): The property used to filter the locations
min_value(float): The minimum value of the metric.
max_value(float): The maximum value of the metric.
Returns:
GeometryList: The resulting face polygons
"""
c_geometry_list_dimension = c_int()

self._execute_function(
self.lib.mkernel_mesh2d_get_filtered_face_polygons_dimension,
self._meshkernelid,
c_int(property),
c_double(min_value),
c_double(max_value),
byref(c_geometry_list_dimension),
)

n_coordinates = c_geometry_list_dimension.value
x_coordinates = np.empty(n_coordinates, dtype=np.double)
y_coordinates = np.empty(n_coordinates, dtype=np.double)

face_polygons = GeometryList(
x_coordinates=x_coordinates, y_coordinates=y_coordinates
)
c_face_polygons = CGeometryList.from_geometrylist(face_polygons)

self._execute_function(
self.lib.mkernel_mesh2d_get_filtered_face_polygons,
self._meshkernelid,
c_int(property),
c_double(min_value),
c_double(max_value),
byref(c_face_polygons),
)

return face_polygons

def mesh2d_get_node_index(self, x: float, y: float, search_radius: float) -> int:
"""Finds the node closest to a point within a given search radius.
Expand Down
3 changes: 1 addition & 2 deletions meshkernel/py_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ class Mesh2d:
@unique
class Property(IntEnum):
"""Different properties on a 2D mesh."""

ORTHOGONALITY = (0,)
ORTHOGONALITY = 0,
EDGE_LENGTHS = 1

def __init__(
Expand Down
29 changes: 28 additions & 1 deletion tests/test_mesh2d_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
InterpolationValues,
MakeGridParameters,
Mesh2d,
Mesh2dLocation,
MeshKernel,
MeshKernelError,
MeshRefinementParameters,
Expand Down Expand Up @@ -2306,7 +2307,6 @@ def test_mesh2d_deletion_and_get_orthogonality(
mesh2d = mk.mesh2d_get()
assert len(values) == len(mesh2d.edge_x)


cases_get_property = [
(
Mesh2d.Property.ORTHOGONALITY,
Expand Down Expand Up @@ -2392,3 +2392,30 @@ def test_mesh2d_get_property(
property_list = mk.mesh2d_get_property(property)

assert property_list.values == approx(expected_values, abs=1e-6)

def test_mesh2d_get_filtered_face_polygons():
"""Test mesh2d_get_filtered_face_polygons,
getting the polygons of faces with all edges having bad orthogonality values
"""
mk = MeshKernel()

edge_nodes = np.array(
[0, 1, 1, 2, 2, 3, 0, 3, 1, 4, 0, 4, 0, 5, 3, 5, 3, 6, 2, 6, 2, 7, 1, 7],
dtype=np.int32,
)

node_x = np.array([57.0, 49.1, 58.9, 66.7, 48.8, 65.9, 67.0, 49.1], dtype=np.double)
node_y = np.array([23.6, 14.0, 6.9, 16.2, 23.4, 24.0, 7.2, 6.7], dtype=np.double)

input_mesh2d = Mesh2d(node_x, node_y, edge_nodes)
mk.mesh2d_set(input_mesh2d)

face_polygons = mk.mesh2d_get_filtered_face_polygons(
Mesh2d.Property.ORTHOGONALITY, 0.04, 1.0
)

expected_coordinates_x = np.array([57.0, 49.1, 58.9, 66.7, 57.0], dtype=np.double)
expected_coordinates_y = np.array([23.6, 14.0, 6.9, 16.2, 23.6], dtype=np.double)

assert face_polygons.x_coordinates == approx(expected_coordinates_x, abs=1e-6)
assert face_polygons.y_coordinates == approx(expected_coordinates_y, abs=1e-6)

0 comments on commit e4c864b

Please sign in to comment.