How to find the shared volume of n overlapping cubes.Where each cube is described by two points in 3D space.(x1, y1, z1) being one corner of the cube and (x2, y2, z2) being the opposite corner.And the sides of each of the cubes are parallel to the axis.
-
$\begingroup$ actually i am trying it for two days i can't understandand i gogoled it but couldn't get anything exactly....... $\endgroup$– Md Soman KhanOct 8, 2016 at 12:14
-
$\begingroup$ i can find the rectangle shared volume with four co-ordinates in 2D..but it is cube and it has only two opposite co-ordinates $\endgroup$– Md Soman KhanOct 8, 2016 at 12:22
3 Answers
Each cube is bound by six planes. The intersection area has to satisfy all of these constraints, which will reduce to 6 active constaints. To reduce the constraints, for each dimension find the greatest lower bound and the smallest upper bound. Once you determine the active constraints then it should be straight forward to find the volume.
For an axis-aligned cuboid defined by two vertices, $(x_1, y_1, z_1)$ and $(x_2, y_2, z_2)$, diagonally opposite each other, you can use $$\begin{array}{} x_{min} = \min(x_1, x_2) \\ y_{min} = \min(y_1, y_2) \\ z_{min} = \min(z_1, z_2) \\ x_{max} = \max(x_1, x_2) \\ y_{max} = \max(y_1, y_2) \\ z_{max} = \max(z_1, z_2) \end{array}$$ to find the extents of the cuboid along each coordinate axis. The eight vertices of the cuboid are then $$\begin{array}{} \left ( x_{min}, y_{min}, z_{min} \right ) \\ \left ( x_{max}, y_{min}, z_{min} \right ) \\ \left ( x_{min}, y_{max}, z_{min} \right ) \\ \left ( x_{max}, y_{max}, z_{min} \right ) \\ \left ( x_{min}, y_{min}, z_{max} \right ) \\ \left ( x_{max}, y_{min}, z_{max} \right ) \\ \left ( x_{min}, y_{max}, z_{max} \right ) \\ \left ( x_{max}, y_{max}, z_{max} \right ) \end{array}$$ Similarly, you only need $\min$ and $\max$ to find the extents of the intersection, if two axis-aligned cuboids intersect. It is the exact same operation as in two dimensions or one dimension, just done for all three coordinate axes.
As the 2 given answers to this old issue do not give IMHO a correct answer, I have decided to write my own one.
The intersection of any number of cubes with faces parallel to coordinate axes is a cuboid (rectangular parallelepiped) with volume given by the usual formula length $\times $ width $\times $ height.
Let the $i$th cube be defined as the set of $(x,y,z)$ such that
$$\begin{cases}x_{1,i} < x < x_{2,i}\\y_{1,i} < y < y_{2,i}\\ z_{1,i} < z < z_{2,i}\end{cases}$$
Let
$$\begin{cases} X_1=\max(x_{1,i}), \ \ & X_2=\min(x_{1,i})\\ Y_1=\max(y_{1,i}), \ \ & Y_2=\min(y_{1,i})\\ Z_1=\max(z_{1,i}), \ \ & Z_2=\min(z_{1,i}) \end{cases}$$
(please note that we take the max of the min and the min of the max).
The parallelipiped is the set of points $(x,y,z)$ given by
$$\begin{cases}X_1<x<X_2\\Y_1<y<Y_2\\Z_1<z<Z_2\end{cases}$$
If it is non void, i.e., if :
$$X_1<X_2 \ \ \text{and} \ \ Y_1<Y_2 \ \ \text{and} \ \ Z_1<Z_2$$
its volume is
$$(X_2-X_1)\times(Y_2-Y_1)\times(Z_2-Z_1)$$