Skip to content

Commit fb5bc8e

Browse files
authored
Merge pull request #1406 from compas-dev/fix-volmesh-delete-cell
Fix VolMesh delete cell
2 parents 663a05c + 0d96709 commit fb5bc8e

File tree

3 files changed

+98
-12
lines changed

3 files changed

+98
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
### Changed
1313

14+
* Fixed bug in `VolMesh.delete_cell`.
15+
1416
### Removed
1517

1618

src/compas/datastructures/volmesh/volmesh.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,42 +1023,62 @@ def delete_cell(self, cell):
10231023
-------
10241024
None
10251025
1026+
Raises
1027+
------
1028+
KeyError
1029+
If the cell does not exist.
1030+
10261031
See Also
10271032
--------
10281033
:meth:`delete_vertex`, :meth:`delete_halfface`
10291034
1035+
Notes
1036+
-----
1037+
Remaining unused vertices are not automatically deleted.
1038+
Use :meth:`remove_unused_vertices` to accomplish this.
1039+
10301040
"""
1031-
cell_vertices = self.cell_vertices(cell)
10321041
cell_faces = self.cell_faces(cell)
10331042

1043+
# remove edge data
10341044
for face in cell_faces:
10351045
for edge in self.halfface_halfedges(face):
1046+
# this should also use a key map
10361047
u, v = edge
10371048
if (u, v) in self._edge_data:
10381049
del self._edge_data[u, v]
10391050
if (v, u) in self._edge_data:
10401051
del self._edge_data[v, u]
10411052

1042-
for vertex in cell_vertices:
1043-
if len(self.vertex_cells(vertex)) == 1:
1044-
del self._vertex[vertex]
1045-
1053+
# remove face data
10461054
for face in cell_faces:
10471055
vertices = self.halfface_vertices(face)
1048-
for u, v in uv_from_vertices(vertices):
1049-
self._plane[u][v][face] = None
1050-
if self._plane[v][u][face] is None:
1051-
del self._plane[u][v][face]
1052-
del self._plane[v][u][face]
1053-
del self._halfface[face]
10541056
key = "-".join(map(str, sorted(vertices)))
10551057
if key in self._face_data:
10561058
del self._face_data[key]
10571059

1058-
del self._cell[cell]
1060+
# remove cell data
10591061
if cell in self._cell_data:
10601062
del self._cell_data[cell]
10611063

1064+
# remove planes and halffaces
1065+
for face in cell_faces:
1066+
vertices = self.halfface_vertices(face)
1067+
for u, v, w in uvw_from_vertices(vertices):
1068+
if self._plane[w][v][u] is None:
1069+
del self._plane[u][v][w]
1070+
del self._plane[w][v][u]
1071+
if not self._plane[u][v]:
1072+
del self._plane[u][v]
1073+
if not self._plane[w][v]:
1074+
del self._plane[w][v]
1075+
else:
1076+
self._plane[u][v][w] = None
1077+
del self._halfface[face]
1078+
1079+
# remove cell
1080+
del self._cell[cell]
1081+
10621082
def remove_unused_vertices(self):
10631083
"""Remove all unused vertices from the volmesh object.
10641084

tests/compas/datastructures/test_volmesh.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,67 @@ def test_cells_where_predicate():
244244
# ==============================================================================
245245
# Methods
246246
# ==============================================================================
247+
248+
249+
def test_delete_cell_of_volmesh_with_1_1_1():
250+
volmesh = VolMesh.from_meshgrid(1, 1, 1, 1, 1, 1)
251+
nov = volmesh.number_of_vertices()
252+
noe = volmesh.number_of_edges()
253+
nof = volmesh.number_of_faces()
254+
noc = volmesh.number_of_cells()
255+
256+
volmesh.delete_cell(0)
257+
258+
assert volmesh.number_of_vertices() == nov
259+
assert volmesh.number_of_cells() == noc - 1
260+
assert volmesh.number_of_edges() == noe - 12
261+
assert volmesh.number_of_faces() == nof - 6
262+
263+
264+
@pytest.mark.parametrize(
265+
"c",
266+
[0, 1],
267+
)
268+
def test_delete_cell_of_volmesh_with_2_1_1(c):
269+
volmesh = VolMesh.from_meshgrid(1, 1, 1, 2, 1, 1)
270+
nov = volmesh.number_of_vertices()
271+
noe = volmesh.number_of_edges()
272+
nof = volmesh.number_of_faces()
273+
noc = volmesh.number_of_cells()
274+
275+
volmesh.delete_cell(c)
276+
277+
assert volmesh.number_of_vertices() == nov
278+
assert volmesh.number_of_cells() == noc - 1
279+
assert volmesh.number_of_edges() == noe - 8
280+
assert volmesh.number_of_faces() == nof - 5
281+
282+
283+
@pytest.mark.parametrize(
284+
"c",
285+
[0, 1, 2],
286+
)
287+
def test_delete_cell_of_volmesh_with_3_1_1(c):
288+
volmesh = VolMesh.from_meshgrid(1, 1, 1, 3, 1, 1)
289+
nov = volmesh.number_of_vertices()
290+
noe = volmesh.number_of_edges()
291+
nof = volmesh.number_of_faces()
292+
noc = volmesh.number_of_cells()
293+
294+
volmesh.delete_cell(c)
295+
296+
if c == 0:
297+
assert volmesh.number_of_vertices() == nov
298+
assert volmesh.number_of_cells() == noc - 1
299+
assert volmesh.number_of_edges() == noe - 8
300+
assert volmesh.number_of_faces() == nof - 5
301+
elif c == 1:
302+
assert volmesh.number_of_vertices() == nov
303+
assert volmesh.number_of_cells() == noc - 1
304+
assert volmesh.number_of_edges() == noe - 4
305+
assert volmesh.number_of_faces() == nof - 4
306+
elif c == 2:
307+
assert volmesh.number_of_vertices() == nov
308+
assert volmesh.number_of_cells() == noc - 1
309+
assert volmesh.number_of_edges() == noe - 8
310+
assert volmesh.number_of_faces() == nof - 5

0 commit comments

Comments
 (0)