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 diff --git a/src/compas/datastructures/volmesh/volmesh.py b/src/compas/datastructures/volmesh/volmesh.py index a934b26b63c8..6c23c547d1a8 100644 --- a/src/compas/datastructures/volmesh/volmesh.py +++ b/src/compas/datastructures/volmesh/volmesh.py @@ -1023,42 +1023,62 @@ def delete_cell(self, cell): ------- None + Raises + ------ + KeyError + If the cell does not exist. + See Also -------- :meth:`delete_vertex`, :meth:`delete_halfface` + Notes + ----- + Remaining unused vertices are not automatically deleted. + Use :meth:`remove_unused_vertices` to accomplish this. + """ - cell_vertices = self.cell_vertices(cell) 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. diff --git a/tests/compas/datastructures/test_volmesh.py b/tests/compas/datastructures/test_volmesh.py index d0100d4bcbab..60795d929f21 100644 --- a/tests/compas/datastructures/test_volmesh.py +++ b/tests/compas/datastructures/test_volmesh.py @@ -244,3 +244,67 @@ def test_cells_where_predicate(): # ============================================================================== # Methods # ============================================================================== + + +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