# How does VTKMassProperties compute volume?

**URL:** https://discourse.vtk.org/t/how-does-vtkmassproperties-compute-volume/6256
**Category:** Support
**Created:** [July 28, 2021, 5:42am UTC](https://discourse.vtk.org/t/how-does-vtkmassproperties-compute-volume/6256 "2021-07-28T05:42:05Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![DFlyer](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dflyer/32/1598_2.png) [@DFlyer](https://discourse.vtk.org/u/DFlyer)
#### Post date: [July 28, 2021, 5:42am UTC](https://discourse.vtk.org/t/how-does-vtkmassproperties-compute-volume/6256/1 "2021-07-28T05:42:05Z")

</div>

Hello,  
What is the math behind the volume computation performed in VTKMassProperties? Also what does Volume - X, Y and Z mean?

(I could see a research paper being cited, but can someone explain in a layman terms?)

Thanks

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [July 28, 2021, 4:30pm UTC](https://discourse.vtk.org/t/how-does-vtkmassproperties-compute-volume/6256/2 "2021-07-28T16:30:58Z")

</div>

I think it computes the surface integral of the Z position vector over all the triangles that make up the surface. What I mean by Z position vector is the Z component of the (X,Y,Z) position vector, multiplied by the unit vector in the Z direction. Okay, that’s not the best explanation… you know how a position vector (or any vector) can be expressed as the sum of vectors in the X, Y, and Z directions? So the Z position vector is just the Z part.

So for every point on the surface, you take the dot product of the Z position vector at that point with the surface normal, and integrate that over the entire surface. Except this isn’t done point-by-point, but rather triangle-by-triangle. That’s the basic gist I get when I read the code. ~~The choice of Z rather than X or Y seems to be arbitrary.~~

Edit: I guess I should add an explanation of why this works. Imagine a simple surface, like a sphere. Image a line along the Z direction that passes through the sphere, it will intersect the surface twice. There will be a certain distance between the “entry” point and the “exit” point. Now imagine a line along Z for every possible (X,Y) position, a whole bunch of parallel lines in a dense grid. You can see how the sum of all the entry-to-exit distances for all these lines will give you the volume of the sphere. Well, you can measure the “entry” Z position and “exit” Z position separately, in fact the only difference is the direction of the surface normal with respect to the Z direction. And this works for any shape of surface.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [July 28, 2021, 6:20pm UTC](https://discourse.vtk.org/t/how-does-vtkmassproperties-compute-volume/6256/3 "2021-07-28T18:20:13Z")

</div>

Just some small correction/additions to my previous reply.

The method that I described above is applied not only in the Z but also in the X and Y directions. Hence the VolumeX, VolumeY, and VolumeZ returned by the class.

The Volume that is returned is a weighted average of VolumeX, VolumeY, and VolumeZ. Referring to my entry/exit point discussion above, if the line is nearly perpendicular to surface it’s possible to make a better measurement than if the line is nearly tangential to the surface. So the algorithm uses weighting factors according to how often a particular direction gives the “best” result while iterating over the triangles.
