# Compute volume of polygonal mesh (volume enclosed by the surface)

How do I compute the volume of a polygonal mesh?
In specific, I need to compute the volume enclosed by the surface.

As you mentioned in your other post one way is to create a volume mesh of your domain and evaluate the volume of each element. If your domain is convex and has no holes you can use the 3D Delaunay filter otherwise you will need some external meshing tool capable of filling the volume of your surface.

Assuming a triangulated surface (if not use `Triangulate` filter) a different way is the following:

• Pick an arbitrary point P in space (barycenter of the surface mesh works the best).
• Sum the signed volumes of the tetrahedra formed by each face triangle and P. 

Example:

1. Open: `disk_out_ref.ex2` available in the examples directory
2. Apply `MergeBlocks` ( no need if you don’t have multiblock data)
3. Apply `Extract Surface` ( no need in your case but here the mesh has volume elements already)
4. Apply `Triangulate`
5. Apply `Programmable Filter` using this as a script
``````import numpy as np

input0 = inputs

numTriangles = input0.GetNumberOfCells()
volumeArray = np.empty(numTriangles , dtype=np.float64)
for i in range(numTriangles):
cell = input0.GetCell(i)
p1 = input0.GetPoint(cell.GetPointId(0))
p2 = input0.GetPoint(cell.GetPointId(1))
p3 = input0.GetPoint(cell.GetPointId(2))
p4 = [0,0,0] # use barycenter of surface for better results.
volumeArray[i] = vtk.vtkTetra.ComputeVolume(p1,p2,p4,p3)

output.CellData.append(volumeArray, "Volume")
``````
1. Now we just need to sum these values:
i. one way is to run `PythonCalculator` with `sum(Volume)` as expression it. It will add the value as cell data to all elements
ii. OR you can run `Descriptive Statistics` and select “Volume” as variable of interest.

these steps with ParaView 5.11 on Linux give me `1629.6572304587103` `IntegrateVariables` applied on the original data returns `1629.73`

 It works because of Divergence Theorem ! See A symbolic method for calculating the integral properties of arbitrary nonconvex polyhedra | IEEE Journals & Magazine | IEEE Xplore if you do not have access check this math - How to calculate the volume of a 3D mesh object the surface of which is made up triangles - Stack Overflow

1 Like

Thank you very much!
In the case of my data I just need the programmable filter and the PythonCalculator. Very nice Now, in another case I have a mesh with holes.
For example it can look like this:

What can I do then?

I found the vtkFillHolesFilter, but so far it does not change anything.
https://vtk.org/doc/nightly/html/classvtkFillHolesFilter.html

I am using the ExtractSurface Filter to convert the vtkUnstructuredGrid to vtkPolyData and then I apply a programmableFilter with this script:

``````import vtk
from paraview import servermanager as sm

input0 = inputs
input0 = sm.Fetch(input0)
filler = vtk.vtkFillHolesFilter()
filler.SetInputData(input0)
filler.Update()
return filler.GetOutput()
``````