You can calculate the volume of a point cloud's mesh or a fully corrected depth map, using M3dmetVolume(). You can, and in some cases must, specify a reference object to delimit the volume.
When calculating the volume of a mesh without holes, you do not need to specify a reference object. When calculating the volume of a depth map (with or without holes) or a mesh that has holes, a reference object is required to define a closed shape for which to calculate the volume. In either case, you might need to specify a reference object to limit the volume to calculate.
You can specify that the reference is the XY (Z=0) plane, a defined 3D plane geometry, or a fully corrected depth map (when the source is also a depth map). By default, for a depth map as a source, the reference is the XY (Z=0) plane; however, for a mesh, there is no default reference (if the mesh has holes, you must explicitly pass a reference object).
When you specify a reference object and the source is a depth map or a mesh with holes, M3dmetVolume() extends the boundaries of the source object onto it, to define the closed 3D shape for which to calculate the volume. If the source is a mesh, its boundaries are extended perpendicular to the reference object. If the source is a depth map image buffer, its boundaries are extended in the Z-direction until they meet the reference object.
Whether or not a reference object is required, you can specify one to limit the volume to calculate. For example, you can calculate just the volume above the reference object (M_ABOVE), under the reference object (M_UNDER), or the volume that is above the reference object minus the volume below it (M_DIFFERENCE).
These calculations are made, given the position of the source object relative to the reference object. If the reference object is a plane, any volume on the same side as the plane's normal is considered to be above the plane. If the reference object is a depth map, a given pixel in the source depth map is considered above a corresponding pixel (at the same X- and Y-coordinate) in the reference depth map if its Z-coordinate is greater.
By default, M3dmetVolume() calculates the sum of all volumes between the source and reference objects, whether above or below the reference object.
If the source is a depth map with holes, you can use M3dimFillGaps() to fill them before calculating the volume.
If the source is a mesh with disconnected triangles, the volumes for each set of connected triangles are calculated separately and summed in the final result. This can lead to unexpected results if two sets of triangles overlap, or if one set of triangles is above another with respect to the reference object.
The following steps describe how the volume of a mesh with holes is calculated.
When calculating the volume of a mesh with holes, it is the orientation of the meshed point cloud's triangles that defines how M3dmetVolume() calculates the volume.
An arbitrary orientation (relative to the reference plane) is guessed for one triangle.
The orientation of each triangle in the mesh is extrapolated from the first triangle.
If any triangle in the mesh crosses the reference plane, the triangle is subdivided into triangles that do not.
For each triangle above the reference plane, a triangular prism from the triangle to the plane is defined.
If the triangle was orientated away from the reference plane, the volume of the prism adds to the total calculated volume (illustrated in green). If the triangle was orientated towards the reference plane, the volume of the prism subtracts from the total calculated volume (illustrated in red).
The absolute value of the total calculated volume is returned.