From 849e7f7c479fb31cc3e97ae7a0bf75d7fd99e2e7 Mon Sep 17 00:00:00 2001 From: tomvanmele Date: Thu, 31 Oct 2024 12:44:05 +0100 Subject: [PATCH 1/5] fix method --- src/compas/datastructures/volmesh/volmesh.py | 44 ++++++++++++++------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/src/compas/datastructures/volmesh/volmesh.py b/src/compas/datastructures/volmesh/volmesh.py index a934b26b63c8..21b667427ffd 100644 --- a/src/compas/datastructures/volmesh/volmesh.py +++ b/src/compas/datastructures/volmesh/volmesh.py @@ -1028,37 +1028,57 @@ def delete_cell(self, cell): :meth:`delete_vertex`, :meth:`delete_halfface` """ - cell_vertices = self.cell_vertices(cell) + # @gonzalo: perhaps vertices should not be removed implicitly + # we could add a flag or leave it entirely up to the user + # with something like remove_unused_vertices + + # cell_vertices = self.cell_vertices(cell) + # remove vertex data + # for vertex in cell_vertices: + # if len(self.vertex_cells(vertex)) == 1: + # del self._vertex[vertex] + cell_faces = self.cell_faces(cell) + # remove edge data for face in cell_faces: for edge in self.halfface_halfedges(face): + # this should also use a key map u, v = edge if (u, v) in self._edge_data: del self._edge_data[u, v] if (v, u) in self._edge_data: del self._edge_data[v, u] - for vertex in cell_vertices: - if len(self.vertex_cells(vertex)) == 1: - del self._vertex[vertex] - + # remove face data for face in cell_faces: vertices = self.halfface_vertices(face) - for u, v in uv_from_vertices(vertices): - self._plane[u][v][face] = None - if self._plane[v][u][face] is None: - del self._plane[u][v][face] - del self._plane[v][u][face] - del self._halfface[face] key = "-".join(map(str, sorted(vertices))) if key in self._face_data: del self._face_data[key] - del self._cell[cell] + # remove cell data if cell in self._cell_data: del self._cell_data[cell] + # remove planes and halffaces + for face in cell_faces: + vertices = self.halfface_vertices(face) + for u, v, w in uvw_from_vertices(vertices): + if self._plane[w][v][u] is None: + del self._plane[u][v][w] + del self._plane[w][v][u] + if not self._plane[u][v]: + del self._plane[u][v] + if not self._plane[w][v]: + del self._plane[w][v] + else: + self._plane[u][v][w] = None + del self._halfface[face] + + # remove cell + del self._cell[cell] + def remove_unused_vertices(self): """Remove all unused vertices from the volmesh object. From 89d56314cbae0a81bf2f7366b52b17baa61fcb8c Mon Sep 17 00:00:00 2001 From: Gonzalo Casas Date: Fri, 1 Nov 2024 09:40:51 +0100 Subject: [PATCH 2/5] Add delete_cell test --- tests/compas/datastructures/test_volmesh.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/compas/datastructures/test_volmesh.py b/tests/compas/datastructures/test_volmesh.py index d0100d4bcbab..420177becbca 100644 --- a/tests/compas/datastructures/test_volmesh.py +++ b/tests/compas/datastructures/test_volmesh.py @@ -244,3 +244,9 @@ def test_cells_where_predicate(): # ============================================================================== # Methods # ============================================================================== + +def test_delete_cell(): + hf = VolMesh.from_meshgrid(10, 10, 10, 5, 5, 5) + assert hf.number_of_cells() == 125 + hf.delete_cell(1) + assert hf.number_of_cells() == 124 \ No newline at end of file From 734e4daabb6a8cd61264d55aef1d3c2c487be75c Mon Sep 17 00:00:00 2001 From: tomvanmele Date: Fri, 1 Nov 2024 12:32:19 +0100 Subject: [PATCH 3/5] more tests --- src/compas/datastructures/volmesh/volmesh.py | 5 ++ tests/compas/datastructures/test_volmesh.py | 68 ++++++++++++++++++-- 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/src/compas/datastructures/volmesh/volmesh.py b/src/compas/datastructures/volmesh/volmesh.py index 21b667427ffd..a77762eff4c9 100644 --- a/src/compas/datastructures/volmesh/volmesh.py +++ b/src/compas/datastructures/volmesh/volmesh.py @@ -1023,6 +1023,11 @@ def delete_cell(self, cell): ------- None + Raises + ------ + KeyError + If the cell does not exist. + See Also -------- :meth:`delete_vertex`, :meth:`delete_halfface` diff --git a/tests/compas/datastructures/test_volmesh.py b/tests/compas/datastructures/test_volmesh.py index 420177becbca..60795d929f21 100644 --- a/tests/compas/datastructures/test_volmesh.py +++ b/tests/compas/datastructures/test_volmesh.py @@ -245,8 +245,66 @@ def test_cells_where_predicate(): # Methods # ============================================================================== -def test_delete_cell(): - hf = VolMesh.from_meshgrid(10, 10, 10, 5, 5, 5) - assert hf.number_of_cells() == 125 - hf.delete_cell(1) - assert hf.number_of_cells() == 124 \ No newline at end of file + +def test_delete_cell_of_volmesh_with_1_1_1(): + volmesh = VolMesh.from_meshgrid(1, 1, 1, 1, 1, 1) + nov = volmesh.number_of_vertices() + noe = volmesh.number_of_edges() + nof = volmesh.number_of_faces() + noc = volmesh.number_of_cells() + + volmesh.delete_cell(0) + + assert volmesh.number_of_vertices() == nov + assert volmesh.number_of_cells() == noc - 1 + assert volmesh.number_of_edges() == noe - 12 + assert volmesh.number_of_faces() == nof - 6 + + +@pytest.mark.parametrize( + "c", + [0, 1], +) +def test_delete_cell_of_volmesh_with_2_1_1(c): + volmesh = VolMesh.from_meshgrid(1, 1, 1, 2, 1, 1) + nov = volmesh.number_of_vertices() + noe = volmesh.number_of_edges() + nof = volmesh.number_of_faces() + noc = volmesh.number_of_cells() + + volmesh.delete_cell(c) + + assert volmesh.number_of_vertices() == nov + assert volmesh.number_of_cells() == noc - 1 + assert volmesh.number_of_edges() == noe - 8 + assert volmesh.number_of_faces() == nof - 5 + + +@pytest.mark.parametrize( + "c", + [0, 1, 2], +) +def test_delete_cell_of_volmesh_with_3_1_1(c): + volmesh = VolMesh.from_meshgrid(1, 1, 1, 3, 1, 1) + nov = volmesh.number_of_vertices() + noe = volmesh.number_of_edges() + nof = volmesh.number_of_faces() + noc = volmesh.number_of_cells() + + volmesh.delete_cell(c) + + if c == 0: + assert volmesh.number_of_vertices() == nov + assert volmesh.number_of_cells() == noc - 1 + assert volmesh.number_of_edges() == noe - 8 + assert volmesh.number_of_faces() == nof - 5 + elif c == 1: + assert volmesh.number_of_vertices() == nov + assert volmesh.number_of_cells() == noc - 1 + assert volmesh.number_of_edges() == noe - 4 + assert volmesh.number_of_faces() == nof - 4 + elif c == 2: + assert volmesh.number_of_vertices() == nov + assert volmesh.number_of_cells() == noc - 1 + assert volmesh.number_of_edges() == noe - 8 + assert volmesh.number_of_faces() == nof - 5 From c152e4256d30fa938d2d3725c21a1f1bc7a78323 Mon Sep 17 00:00:00 2001 From: tomvanmele Date: Fri, 1 Nov 2024 12:32:46 +0100 Subject: [PATCH 4/5] log --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c08242cfa0e5..de5719753ba1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +* Fixed bug in `VolMesh.delete_cell`. + ### Removed From 0d967099851464f9219d3d8aef2804910832edc9 Mon Sep 17 00:00:00 2001 From: tomvanmele Date: Fri, 1 Nov 2024 14:07:20 +0100 Subject: [PATCH 5/5] note --- src/compas/datastructures/volmesh/volmesh.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/compas/datastructures/volmesh/volmesh.py b/src/compas/datastructures/volmesh/volmesh.py index a77762eff4c9..6c23c547d1a8 100644 --- a/src/compas/datastructures/volmesh/volmesh.py +++ b/src/compas/datastructures/volmesh/volmesh.py @@ -1032,17 +1032,12 @@ def delete_cell(self, cell): -------- :meth:`delete_vertex`, :meth:`delete_halfface` - """ - # @gonzalo: perhaps vertices should not be removed implicitly - # we could add a flag or leave it entirely up to the user - # with something like remove_unused_vertices - - # cell_vertices = self.cell_vertices(cell) - # remove vertex data - # for vertex in cell_vertices: - # if len(self.vertex_cells(vertex)) == 1: - # del self._vertex[vertex] + Notes + ----- + Remaining unused vertices are not automatically deleted. + Use :meth:`remove_unused_vertices` to accomplish this. + """ cell_faces = self.cell_faces(cell) # remove edge data