PDA

Click to See Complete Forum and Search --> : [RESOLVED] block within a block


lookingfor help
Dec 21st, 2009, 07:55 PM
I originally posted this in the VB forum as I am using VB6 code but was advised to re-post it here....

I want to be able to calculate the volume of a small rectangular block that is within a larger rectangular block.

I know the centroids of both blocks and the extent of the blocks in the x, y and z directions. The x,y,z axes of both blocks are parallel. For example, the extent of the large block is 15x, 20y, 12z and the extent of the small block is 4x, 3y, 3z.

I wonder if their is an efficient way to determine a) if the small block is completely within the large block or b) if the small block is partly within the large block. If b, then how to calculate the volume that part of the small block within the large block.
Thanks for any ideas.

jemidiah
Dec 21st, 2009, 09:27 PM
Since the axes are all parallel, this isn't difficult. Perhaps you wanted something more complicated, or somehow more optimized than the first solution that comes to my mind. However, I doubt you could optimize it much. You could make sure whatever "if" statements you have are usually "true", which is a generic lookahead processor optimization.

For each axis, determine the intervals covered on that axis by each box. Intersect them to find the part of the small box inside the large box. Multiply the length of the intersected intervals to find b). If b) is the same volume as the small box, a) is true; otherwise, a) is false.

To be explicit...
Use your example. Suppose the centroid of the large box is (4, 0, -3), and the centroid of the small box is (16, 0, -1). The "radii" of the large box are (15, 20, 12), and of the small box are (4, 3, 3). I'll just assume these are radii instead of "diameters"; the answer is analogous, anyway. The intervals of the boxes are then...

x_large: 4 +/- 15 = [-11, 19]
y_large: 0 +/- 20 = [-20, 20]
z_large: -3 +/- 12 = [-15, 9]

x_small: 16 +/- 4 = [12, 20]
y_small: 0 +/- 3 = [-3, 3]
z_small: -1 +/- 3 = [-4, 2]

x_intersect: [12, 19]
y_intersect: [-3, 3]
z_intersect: [-4, 2]

b) (19-12)*(3-(-3))*(2-(-4)) = 7*6*6 = 228
a) small box volume = (20-12)*(3-(-3))*(2-(-4)) = 8*6*6 = 252 != 228; false


Edit: the "intersect" function takes in two intervals, say [a, b], [c, d], and determines their overlap. The output is [max(a, c), min(b, d)]. By convention, say an interval [a, b] with a >= b has length 0. Otherwise, [a, b] has length (b-a).

lookingfor help
Dec 22nd, 2009, 06:44 PM
Many thanks Jemidiah that certainly enabled me to get a solution.
I thought there may have some method involving vectors (about which I know little) but your solution works well.

jemidiah
Dec 23rd, 2009, 07:58 PM
Anything I can come up with using vectors is needlessly complicated (and therefore less efficient). They can largely be thought of as points in space, with some useful operations. They might be useful if the axes aren't parallel, but I haven't thought of that case since you apparently don't need it.