Skip to content

Commit 504be2a

Browse files
CopilotLicini
andcommitted
Refactor volume() to use existing volume_polyhedron function
Simplified implementation by reusing the existing volume_polyhedron function from compas.geometry instead of reimplementing the algorithm. This reduces code duplication and improves maintainability. Co-authored-by: Licini <17893605+Licini@users.noreply.github.com>
1 parent 77c3fab commit 504be2a

File tree

1 file changed

+3
-28
lines changed
  • src/compas/datastructures/mesh

1 file changed

+3
-28
lines changed

src/compas/datastructures/mesh/mesh.py

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
from compas.geometry import distance_line_line
4343
from compas.geometry import distance_point_plane
4444
from compas.geometry import distance_point_point
45-
from compas.geometry import dot_vectors
4645
from compas.geometry import length_vector
4746
from compas.geometry import midpoint_line
4847
from compas.geometry import normal_polygon
@@ -53,6 +52,7 @@
5352
from compas.geometry import sum_vectors
5453
from compas.geometry import transform_points
5554
from compas.geometry import vector_average
55+
from compas.geometry import volume_polyhedron
5656
from compas.itertools import linspace
5757
from compas.itertools import pairwise
5858
from compas.itertools import window
@@ -3912,33 +3912,8 @@ def volume(self):
39123912
if not self.is_closed():
39133913
return None
39143914

3915-
volume = 0.0
3916-
for fkey in self.faces():
3917-
vertices = self.face_vertices(fkey)
3918-
# Get coordinates for all vertices of the face
3919-
coords = [self.vertex_coordinates(v) for v in vertices]
3920-
3921-
# Triangulate the face if it has more than 3 vertices
3922-
if len(coords) == 3:
3923-
triangles = [coords]
3924-
else:
3925-
# Use simple fan triangulation from first vertex
3926-
triangles = []
3927-
for i in range(1, len(coords) - 1):
3928-
triangles.append([coords[0], coords[i], coords[i + 1]])
3929-
3930-
# Calculate signed volume contribution from each triangle
3931-
for triangle in triangles:
3932-
# Signed volume of tetrahedron formed by triangle and origin
3933-
# V = (1/6) * |a · (b × c)| where a, b, c are the vertices
3934-
a, b, c = triangle
3935-
# Calculate cross product of b and c
3936-
bc = cross_vectors(b, c)
3937-
# Calculate dot product with a
3938-
vol = dot_vectors(a, bc) / 6.0
3939-
volume += vol
3940-
3941-
return abs(volume)
3915+
vertices, faces = self.to_vertices_and_faces()
3916+
return abs(volume_polyhedron((vertices, faces)))
39423917

39433918
def centroid(self):
39443919
"""Calculate the mesh centroid.

0 commit comments

Comments
 (0)