From b82741cf041b94093e7c4a61470ce53ea7c3b01d Mon Sep 17 00:00:00 2001 From: Andy Beer Date: Mon, 8 Jul 2024 13:50:16 +0200 Subject: [PATCH 1/2] storing Surface normal vector as extra metadata if established at time of create_xml() --- resqpy/surface/_surface.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/resqpy/surface/_surface.py b/resqpy/surface/_surface.py index e44c33d0..c45e4b17 100644 --- a/resqpy/surface/_surface.py +++ b/resqpy/surface/_surface.py @@ -122,6 +122,7 @@ def __init__(self, self.set_from_mesh_file(mesh_file, mesh_format, quad_triangles = quad_triangles) elif tsurf_file is not None: self.set_from_tsurf_file(tsurf_file) + self._load_normal_vector_from_extra_metadata() @classmethod def from_tri_mesh(cls, tri_mesh, exclude_nans = False): @@ -1203,7 +1204,12 @@ def create_xml(self, if not self.title: self.title = 'surface' - tri_rep = super().create_xml(add_as_part = False, title = title, originator = originator) + em = None + if self.normal_vector is not None: + assert len(self.normal_vector) == 3 + em = {'normal vector': f'{self.normal_vector[0]},{self.normal_vector[1]},{self.normal_vector[2]}'} + + tri_rep = super().create_xml(add_as_part = False, title = title, originator = originator, extra_metadata = em) # todo: if crs_uuid is None, attempt to set to surface patch crs uuid (within patch loop, below) if crs_uuid is not None: @@ -1309,6 +1315,16 @@ def create_xml(self, return tri_rep + def _load_normal_vector_from_extra_metadata(self): + if self.normal_vector is None and self.extra_metadata is not None: + nv_str = self.extra_metadata.get('normal vector') + if nv_str is not None: + nv_words = nv_str.split(',') + assert len(nv_words) == 3, f'failed to convert normal vector string into triplet: {nv_str}' + self.normal_vector = np.empty(3, dtype = float) + for i in range(3): + self.normal_vector[i] = float(nv_words[i]) + def distill_triangle_points(t, p): """Returns a (triangles, points) pair with points distilled as only those used from p.""" From 9fa99c6465a6003331c747527ac236f18a06e3a5 Mon Sep 17 00:00:00 2001 From: Andy Beer Date: Mon, 8 Jul 2024 14:02:57 +0200 Subject: [PATCH 2/2] unit test for surface normal vector (and minor buglet fixed) --- resqpy/surface/_surface.py | 2 +- tests/unit_tests/surface/test_surface.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/resqpy/surface/_surface.py b/resqpy/surface/_surface.py index c45e4b17..bd92539f 100644 --- a/resqpy/surface/_surface.py +++ b/resqpy/surface/_surface.py @@ -716,7 +716,7 @@ def unit_adjusted_points(self): _, p = self.triangles_and_points() crs = rqc.Crs(self.model, uuid = self.crs_uuid) - if crs.xy_units == self.z_units: + if crs.xy_units == crs.z_units: return p unit_adjusted_p = p.copy() wam.convert_lengths(unit_adjusted_p[:, 2], crs.z_units, crs.xy_units) diff --git a/tests/unit_tests/surface/test_surface.py b/tests/unit_tests/surface/test_surface.py index 7c064f02..d12e2c58 100644 --- a/tests/unit_tests/surface/test_surface.py +++ b/tests/unit_tests/surface/test_surface.py @@ -59,6 +59,15 @@ def test_faces_for_surface(tmp_model): assert surf is not None assert surf.node_count() == 4 assert surf.triangle_count() == 2 + assert_array_almost_equal(surf.normal(), (-0.707107, 0.0, 0.707107)) + surf.write_hdf5() + surf.create_xml() + surf = rqs.Surface(tmp_model, uuid = surf.uuid) + assert surf is not None + assert surf.node_count() == 4 + assert surf.triangle_count() == 2 + assert surf.normal_vector is not None + assert_array_almost_equal(surf.normal_vector, (-0.707107, 0.0, 0.707107)) for mode in ['staffa', 'regular', 'auto']: gcs = rqgs.find_faces_to_represent_surface(grid, surf, name = mode, mode = mode) assert gcs is not None