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.

Community
  • 1
  • 1
ryanjdillon
  • 17,658
  • 9
  • 85
  • 110
  • 2
    You may find [this](https://web.archive.org/web/20090312012019/http://www.niksula.cs.hut.fi/~hkankaan/Homepages/calcvolume.html) helpful – ali_m Jun 16 '13 at 02:08
  • 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)`? – Armin Rigo Jun 16 '13 at 11:28
  • The latter would suffice, but I would certainly be interested in a general solution. – ryanjdillon Jun 16 '13 at 17:08
  • @ali_m 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. – ryanjdillon Jun 16 '13 at 22:51
  • 1
    @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](http://www.ecse.rpi.edu/~wrf/Research/Short_Notes/volume.html) for example. – ali_m Jun 16 '13 at 23:12
  • Given that you have the vertices, you may find my [self-answer](http://stackoverflow.com/a/19661317/2583476) 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. – Chris H Oct 30 '13 at 08:46

2 Answers2

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..

marcosaedro
  • 103
  • 4
MagnusBadel
  • 106
  • 3
  • 5
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.
  • 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! – ryanjdillon Oct 08 '13 at 08:14
  • 1
    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. – Morten Lind Dec 28 '19 at 00:47