Calculate the volume of a 3D polyhedron with Python?
Asked Answered
F

2

6

I am trying to figure out the best way of calculating the volume of a 3D polyhedron with Python, and I'm hoping there is a simple solution out there, which I can't seem to find.

Example polyhedron Example Polyhedron

I did find this post that describes calculating the area of a planar polygon in 3D space, but that doesn't seem to help.

Fillin answered 16/6, 2013 at 0:16 Comment(6)
You may find this helpfulAstrophysics
Do you need the volume of any polyhedra, given by a complex set of 3D vertices and faces based on them? Or instead, as your graphic seems to show, you only need the volume that is between the plane z=0 and a surface defined by z=f(x,y)?Albertinaalbertine
The latter would suffice, but I would certainly be interested in a general solution.Fillin
@Astrophysics I did find that early, but was hoping there was something already implemented; though, I will write something once I find the time if there's not. Is that solution specific to that polyhedron, or would that perhaps work for any, as shown above? Thanks.Fillin
@shootingstars I'm not aware of any existing Python implementation, but the method I linked for you should generalize to any polyhedron. There are also other methods - see here for example.Astrophysics
Given that you have the vertices, you may find my self-answer helpful. Basically it involves using a Delaunay triangulation to divide the shape into tetrahedra, then summing the volumes of the tetrahedra, which are easy to calculate.Baleen
D
9

If you only have convex polyhedrons you can use the QHull binding of scipy.spatial.ConvexHull.

    import numpy as np
    from scipy.spatial import ConvexHull

    points = np.array([[....]])  # your points
    volume = ConvexHull(points).volume

Additionally, with the module Delaunay you can triangulate your passed points into tetrahedra for other stuff..

Dehypnotize answered 21/8, 2016 at 8:11 Comment(0)
V
2

Is your polygon such that you can find a point inside so that you can connect every vertex to the point without crossing a face? If so, you can subdivide each face into triangles. You can do this easily by letting one vertex of a face be a pivot point and drawing lines from the other vertices to the pivot vertex. For instance, a pentagon gets divided into three triangles that fan from a common vertex. Each triangle will form a tetrahedron (a 3-sided pyramid) with the point inside. You can then add up the volumes of all of the tetrahedra for each face. The following is for a convex polyhedron that surrounds the origin (x=0,y=0,z=0). It assumes that there is a list of faces f, and each face has a list of vertices v.

def volume(self):
  ''' calculate volume of polyhedron '''
  vol = 0.
  for f in self.f: # the faces
    n = len(f.v)
    v2 = f.v[0] # the pivot of the fan
    x2 = v2.x
    y2 = v2.y
    z2 = v2.z
    for i in range(1,n-1): # divide into triangular fan segments
      v0 = f.v[i]
      x0 = v0.x
      y0 = v0.y
      z0 = v0.z
      v1 = f.v[i+1]
      x1 = v1.x
      y1 = v1.y
      z1 = v1.z
      # Add volume of tetrahedron formed by triangle and origin
      vol += math.fabs(x0 * y1 * z2 + x1 * y2 * z0 \
                     + x2 * y0 * z1 - x0 * y2 * z1 \
                    - x1 * y0 * z2 - x2 * y1 * z0)
 return vol/6.
Valueless answered 1/10, 2013 at 20:38 Comment(2)
What I hoped to use this for is to calculate the volume of a water basin (like a bay) where I have a a bunch of charted depths. That being the case, I don't see why I couldn't pick some arbitrary point in the center of the basin at mid-depth to do this, right? I'm wrapped up with some things, but will try this as soon as I am able. Thanks!Fillin
Technically, in the discipline of topology, if memory serves me well, this is a matter of the difference between a star-shaped and a convex body. A star-shaped body is a volume, where some point exists, from which all points in the volume can be connected with a line entirely in the body. A convex body is a special case, where every point in the body is such a point. If the case is a star-shaped body, the problem is to find such a central point, whereas if the case is a convex body, any point, e.g. any of the vertices, may be used as central point.Hardiman

© 2022 - 2024 — McMap. All rights reserved.