Skip to content

Commit 066c0f5

Browse files
CopilotLicini
andcommitted
Use built-in triangulation from to_vertices_and_faces
- Replaced manual fan triangulation with to_vertices_and_faces(triangulated=True) - Cleaner and more maintainable code - Uses the mesh's built-in triangulation method - All tests pass with same accuracy Co-authored-by: Licini <17893605+Licini@users.noreply.github.com>
1 parent 6e1bc67 commit 066c0f5

File tree

1 file changed

+11
-24
lines changed
  • src/compas/datastructures/mesh

1 file changed

+11
-24
lines changed

src/compas/datastructures/mesh/mesh.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3920,31 +3920,18 @@ def volume(self):
39203920
# Unify cycles to ensure consistent face orientation
39213921
mesh_copy.unify_cycles()
39223922

3923+
# Use built-in triangulation to get triangulated faces
3924+
vertices, faces = mesh_copy.to_vertices_and_faces(triangulated=True)
3925+
39233926
volume = 0.0
3924-
for fkey in mesh_copy.faces():
3925-
vertices = mesh_copy.face_vertices(fkey)
3926-
# Get coordinates for all vertices of the face
3927-
coords = [mesh_copy.vertex_coordinates(v) for v in vertices]
3928-
3929-
# Triangulate the face if it has more than 3 vertices
3930-
if len(coords) == 3:
3931-
triangles = [coords]
3932-
else:
3933-
# Use simple fan triangulation from first vertex
3934-
triangles = []
3935-
for i in range(1, len(coords) - 1):
3936-
triangles.append([coords[0], coords[i], coords[i + 1]])
3937-
3938-
# Calculate signed volume contribution from each triangle
3939-
for triangle in triangles:
3940-
# Signed volume of tetrahedron formed by triangle and origin
3941-
# V = (1/6) * (a · (b × c)) where a, b, c are the vertices
3942-
a, b, c = triangle
3943-
# Calculate cross product of b and c
3944-
bc = cross_vectors(b, c)
3945-
# Calculate dot product with a
3946-
vol = dot_vectors(a, bc) / 6.0
3947-
volume += vol
3927+
for face in faces:
3928+
# Each face is now a triangle (3 vertices)
3929+
a, b, c = [vertices[i] for i in face]
3930+
# Signed volume of tetrahedron formed by triangle and origin
3931+
# V = (1/6) * (a · (b × c)) where a, b, c are the vertices
3932+
bc = cross_vectors(b, c)
3933+
vol = dot_vectors(a, bc) / 6.0
3934+
volume += vol
39483935

39493936
return abs(volume)
39503937

0 commit comments

Comments
 (0)