Skip to content

Commit

Permalink
Merge pull request #793 from bp/triangle_side_lengths
Browse files Browse the repository at this point in the history
adding Surface.edge_lengths() method
  • Loading branch information
andy-beer committed Mar 20, 2024
2 parents ff95bcc + 4975547 commit 7bce703
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
24 changes: 24 additions & 0 deletions resqpy/surface/_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,30 @@ def distinct_edges_and_counts(self):
assert triangles is not None
return triangulate.edges(triangles)

def edge_lengths(self, required_uom = None):
"""Returns float array of shape (N, 3) being triangle edge lengths.
arguments:
required_uom (str, optional): the required length uom for the resulting edge lengths; default is crs xy units
returns:
numpy float array of shape (N, 3) where N is the number of triangles
"""

t, p = self.triangles_and_points()
crs = rqc.Crs(self.model, uuid = self.crs_uuid)
if required_uom is None:
required_uom = crs.xy_units
if crs.xy_units != required_uom or crs.z_units != required_uom:
p = p.copy()
wam.convert_lengths(p[:, :2], crs.xy_units, required_uom)
wam.convert_lengths(p[:, 2], crs.z_units, required_uom)
t_end = np.empty_like(t)
t_end[:, :2] = t[:, 1:]
t_end[:, 2] = t[:, 0]
edge_v = p[t_end, :] - p[t, :]
return vec.naive_lengths(edge_v)

def set_from_triangles_and_points(self, triangles, points):
"""Populate this (empty) Surface object from an array of triangle corner indices and an array of points."""

Expand Down
13 changes: 13 additions & 0 deletions tests/unit_tests/surface/test_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -1308,3 +1308,16 @@ def test_set_to_split_surface(example_model_and_crs):
split_surf.set_to_split_surface(surf, line, delta_xyz)
split_surf.write_hdf5()
split_surf.create_xml()


def test_edge_lengths(example_model_and_crs):
model, crs = example_model_and_crs
surf = rqs.Surface(model, crs_uuid = crs.uuid, title = 'pair')
corners = np.array([(1.0, 1.0, 0.0), (1.0, 4.0, 0.0), (5.0, 1.0, 0.0), (5.0, 4.0, 0.0)], dtype = float)
surf.set_to_triangle_pair(corners)
surf.write_hdf5()
surf.create_xml()
lengths = surf.edge_lengths()
assert_array_almost_equal(lengths, np.array([(3.0, 4.0, 5.0), (5.0, 3.0, 4.0)], dtype = float))
lengths = surf.edge_lengths(required_uom = 'cm')
assert_array_almost_equal(lengths, np.array([(300.0, 400.0, 500.0), (500.0, 300.0, 400.0)], dtype = float))

0 comments on commit 7bce703

Please sign in to comment.