|
42 | 42 | from compas.geometry import distance_line_line |
43 | 43 | from compas.geometry import distance_point_plane |
44 | 44 | from compas.geometry import distance_point_point |
45 | | -from compas.geometry import dot_vectors |
46 | 45 | from compas.geometry import length_vector |
47 | 46 | from compas.geometry import midpoint_line |
48 | 47 | from compas.geometry import normal_polygon |
|
53 | 52 | from compas.geometry import sum_vectors |
54 | 53 | from compas.geometry import transform_points |
55 | 54 | from compas.geometry import vector_average |
| 55 | +from compas.geometry import volume_polyhedron |
56 | 56 | from compas.itertools import linspace |
57 | 57 | from compas.itertools import pairwise |
58 | 58 | from compas.itertools import window |
@@ -3912,33 +3912,8 @@ def volume(self): |
3912 | 3912 | if not self.is_closed(): |
3913 | 3913 | return None |
3914 | 3914 |
|
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))) |
3942 | 3917 |
|
3943 | 3918 | def centroid(self): |
3944 | 3919 | """Calculate the mesh centroid. |
|
0 commit comments