Skip to content

Commit

Permalink
Merge pull request #803 from bp/surf_nv_em
Browse files Browse the repository at this point in the history
Storing surface normal vector as extra metadata (if established at time of create_xml)
  • Loading branch information
andy-beer committed Jul 8, 2024
2 parents ea9e934 + 9fa99c6 commit fa19031
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
20 changes: 18 additions & 2 deletions resqpy/surface/_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -715,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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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."""
Expand Down
9 changes: 9 additions & 0 deletions tests/unit_tests/surface/test_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fa19031

Please sign in to comment.