Skip to content

Commit c88b6e8

Browse files
CopilotLicini
andcommitted
Add mesh copy and unify_cycles before volume calculation
- Create a copy of the mesh to avoid in-place modifications - Call unify_cycles() on the copy to ensure consistent face orientation - Deep copy vertices list since volume_polyhedron modifies it in place - Updated documentation to reflect internal operations Co-authored-by: Licini <17893605+Licini@users.noreply.github.com>
1 parent 504be2a commit c88b6e8

File tree

1 file changed

+13
-1
lines changed
  • src/compas/datastructures/mesh

1 file changed

+13
-1
lines changed

src/compas/datastructures/mesh/mesh.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3900,6 +3900,9 @@ def volume(self):
39003900
The volume is only meaningful for closed meshes. For open meshes, this method
39013901
returns None.
39023902
3903+
The mesh is copied internally and face cycles are unified to ensure correct
3904+
orientation before computing the volume.
3905+
39033906
Examples
39043907
--------
39053908
>>> from compas.datastructures import Mesh
@@ -3912,7 +3915,16 @@ def volume(self):
39123915
if not self.is_closed():
39133916
return None
39143917

3915-
vertices, faces = self.to_vertices_and_faces()
3918+
# Make a copy to avoid modifying the original mesh
3919+
mesh_copy = self.copy()
3920+
# Unify cycles to ensure consistent face orientation
3921+
mesh_copy.unify_cycles()
3922+
3923+
# Get vertices and faces from the unified copy
3924+
# Make a copy of vertices list since volume_polyhedron modifies it in place
3925+
vertices, faces = mesh_copy.to_vertices_and_faces()
3926+
vertices = [v[:] for v in vertices] # Deep copy of vertex coordinates
3927+
39163928
return abs(volume_polyhedron((vertices, faces)))
39173929

39183930
def centroid(self):

0 commit comments

Comments
 (0)