Skip to content

Commit

Permalink
Merge pull request #1406 from compas-dev/fix-volmesh-delete-cell
Browse files Browse the repository at this point in the history
Fix VolMesh delete cell
  • Loading branch information
tomvanmele authored Nov 1, 2024
2 parents 663a05c + 0d96709 commit fb5bc8e
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
44 changes: 32 additions & 12 deletions src/compas/datastructures/volmesh/volmesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
64 changes: 64 additions & 0 deletions tests/compas/datastructures/test_volmesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fb5bc8e

Please sign in to comment.