how to calculate volume of irregular shapes in python

Ali_d

I have an irregular shape and want to calculate its volume in python. I have seen this solution but I could not figure out how to code it in python. I also uploaded a figure showing my irregular box. Black lines are parallel and figure is in 3d. I have the cordinates of 8 points of the corners as numpy array.

import numpy as np
corners = np.array([[0.,0.,0.], [0.,1.,0.1], [1.,1.,0.1], [0.,1.,0.], # lower rectangle
                    [0.,0.,1.], [0.,1.,1.1], [1.,1.,1.1], [0.,1.,1.]]# upper rectangle

For this simple point the volume should be 1 m^3 but in reality my shapes are much more irregular. I very much apperiate if anyone can help me to calculate the volume of such shapes in python.

enter image description here

Stéphane Laurent

If your object is convex, you can split it into tetrahedra with the help of a Delaunay triangulation.

import numpy as np
from scipy.spatial import Delaunay

corners = np.array([
    [0.,0.,0.], [0.,1.,0.1], [1.,1.,0.1], [0.,1.,0.], # lower rectangle
    [0.,0.,1.], [0.,1.,1.1], [1.,1.,1.1], [0.,1.,1.]  # upper rectangle
])

tri = Delaunay(corners)

Here are the Delaunay tetrahedra:

>>> tetrahedra = corners[tri.simplices]
array([[[0. , 1. , 0. ],
        [0. , 1. , 0.1],
        [1. , 1. , 0.1],
        [0. , 0. , 0. ]],

       [[0. , 0. , 1. ],
        [0. , 1. , 0.1],
        [1. , 1. , 0.1],
        [0. , 0. , 0. ]],

       [[0. , 1. , 1. ],
        [0. , 0. , 1. ],
        [0. , 1. , 0.1],
        [1. , 1. , 0.1]],

       [[0. , 1. , 1. ],
        [0. , 0. , 1. ],
        [1. , 1. , 1.1],
        [1. , 1. , 0.1]],

       [[0. , 1. , 1. ],
        [0. , 0. , 1. ],
        [1. , 1. , 1.1],
        [0. , 1. , 1.1]]])

The way to get the volume of a tetrahedron is well known:

def volume_tetrahedron(tetrahedron):
    matrix = np.array([
        tetrahedron[0] - tetrahedron[3],
        tetrahedron[1] - tetrahedron[3],
        tetrahedron[2] - tetrahedron[3]
    ])
    return abs(np.linalg.det(matrix))/6

So, calculate the volumes of the Delaunay tetrahedra:

volumes = np.array([volume_tetrahedron(t) for t in tetrahedra])

and sum them:

>>> volume = np.sum(volumes)
0.5166666666666667

I insist on one point: your object must be convex. Otherwise the Delaunay algorithm triangulates the convex hull of your object.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How calculate the area of irregular object in an image (opencv - Python 3.8)?

How to get irregular shapes of parameters in TensorFlow

Find volume defined by irregular data points - python

How to calculate area of geometric shapes

Pattern of irregular clickable shapes

Irregular shapes in Delphi

Grid with clicable irregular shapes

How to apply texture on irregular shapes in 2d image?

How to calculate the volume of glass juice?

how to flatten an irregular list in python

How to calculate 'np.where' for irregular dataframe datas?

How to calculate the total volume of multiple overlapping parallelepipeds?

How to calculate the volume of the ship under a surface

How to merge two irregular data frames in python

How python PriorityQueue works with irregular tuple object

How to calculate the common volume/intersection between 2, 2D kde plots in python?

How to calculate numerically a volume integral over a complicated 3D shape in Python?

How to change volume with Python?

Making Clickable areas on a image with irregular shapes

Calculating the area covered by the objects of irregular shapes in an image

Best Elliptical Fit for irregular shapes in an image

Measure real size of irregular shapes in picture

How can I calculate the area inside non contiguous shapes in an image?

How to calculate total perimeter of shapes in JavaScript exercise and write proper function

How to multiply arrays of different shapes in Python?

How to add shapes based on strings in Python?

How to slow down the speed of these moving shapes in python?

How to create and move multiple shapes in python

Tried to write an Object Oriented Python program to calculate volume of cylinder