I don't know if there's already a function for that, but the algorithm is fairly straight-forward:
- Define a random apex point
a . Inside or outside the model, it doesn't matter. Though, it should be more accurate if you use a close one, like the center of the bounding box.
- Iterate over all triangles. Take the face consisting of these 3 vertices and connect it with the apex to form a pyramid.
- Calculate the volume of this pyramid. It is equal to one sixth of the parallelepiped / triple product.
- Sum up the volumes of the pyramids.
In the image below, the green volumes are added and the yellow volumes are automatically subtracted. Yellow volumes are negative because of the normal which points towards the apex.
Below's the formula for a single pyramid's volume.
Note that this won't work if your model has inside faces. It needs to be a simple shell.
Fun fact: You can use this to check if a model has holes or other "errors". For that, repeat the volume calculation several times with different apex points. If there's a big variance in the values, it indicates "errors".