|
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 |
45 | 46 | from compas.geometry import length_vector |
46 | 47 | from compas.geometry import midpoint_line |
47 | 48 | from compas.geometry import normal_polygon |
|
52 | 53 | from compas.geometry import sum_vectors |
53 | 54 | from compas.geometry import transform_points |
54 | 55 | 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 |
@@ -3920,12 +3920,33 @@ def volume(self): |
3920 | 3920 | # Unify cycles to ensure consistent face orientation |
3921 | 3921 | mesh_copy.unify_cycles() |
3922 | 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 |
| 3923 | + 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] |
3927 | 3928 |
|
3928 | | - return abs(volume_polyhedron((vertices, faces))) |
| 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 |
| 3948 | + |
| 3949 | + return abs(volume) |
3929 | 3950 |
|
3930 | 3951 | def centroid(self): |
3931 | 3952 | """Calculate the mesh centroid. |
|
0 commit comments