11

Suppose I have two boxes (each of them is a rectangular cuboid, aka rectangular parallelepiped). I need to write a function that decides if the box with dimensions (a, b, c) can fit into the box with dimensions (A, B, C), assuming any rotations by any angles are allowed (not only by 90°).

The tricky part is the edges of the inner box may be not parallel to corresponding edges of the outer one. For example, a box that is very thin in the dimensions (a, b) but with length 1 < c < √3 can fit into a unit cube (1, 1, 1) if placed along its main diagonal.

I've seen questions [1], [2] but they seem to cover only rotations by 90°.

Community
  • 1
  • 1
HWᅠ
  • 211
  • 2
  • 4
  • 1
    belongs on math.stackexchange.com – PengOne Nov 30 '13 at 23:59
  • 1
    I think you should try solving the 2d version first. – Abhishek Bansal Dec 01 '13 at 04:35
  • I agree. For 2d you have 2 limiting cases (where one box barely fits inside the other): 1) both axes parallel, 2) neither axis parallel (long narrow inner box along diagonal of outer box). In 3d I think you have 8 cases: 1) 3 axes parallel, 2) 1 axis parallel (on a 2d diagonal), 3-5) 3d diagonal, with one of two edges of the inner box parallel to one of three faces in the corner of the outer box. Just to say that may complicate any fit-check algorithm. – PaulQ Jan 11 '14 at 02:45

5 Answers5

2

Not a complete answer, but a good start is to determine the maximum diameter that fits inside the larger box (inscribe the box in a circle) and the minimum diameter needed for the smaller box. That gives a first filter for possibility. This also tells you how to orient the smaller box within the larger one.

PengOne
  • 48,188
  • 17
  • 130
  • 149
2

If one box can fit inside the other than it can fit also if boxes have same center. So only rotation is enough to check, translation is not needed to check.

2D case: For boxes X=(2A,2B) and x=(2a,2b) positioned around (0,0). That means corners of X are (+-A, +-B).

 ---------(A,B)
            |
 -----------(a,b)
(0,0)         |
 -----------(a,-b)
            |
 ---------(A,-B)

Be rotating x around (0,0), corners are always on circle C with radius sqrt(a^2+b^2). If part of circle lie inside box X, and if part inside X has enough arc length to place 2 points on distance 2a or 2b, than x can fit inside X. For that we need to calculate intersection of C with lines x=A and y=B, and calculate distance between these intersection. If distance is equal or grater than 2a or 2b, than x can fit inside X.

3D case: Similar. For boxes X=(2A,2B,2C) and x=(2a,2b,2c) positioned around (0,0,0). Rotating x around (0,0,0), all corners move on sphere with radius sqrt(a^2+b^2+c^2). To see is there enough space on sphere-box intersection part, find intersection of sphere with planes x=A, y=B and z=C, and check is there enough space to fit any of quads (2a,2b), (2a,2c) or (2b,2c) on that sphere part. It is enough to check are points on part border on sufficient distance. For this part I'm not sure about efficient approach, maybe finding 'center' of intersection part and checking it's distance to border can help.

Ante
  • 5,350
  • 6
  • 23
  • 46
  • Nice idea. The check at the end of the 3D case still is really hard. Your idea with finding the center and checking the distance (basically finding the largest circle on the sphere between the other 3 circles which are the result of the sphere/plane intersection) would be easy but probably is not enough as it would assume e.g. a==b – Andreas Kahler Jan 09 '15 at 09:23
  • BTW: In both the 2d and the 3d case you still have to first check the simple axis-aligned case. If (a – Andreas Kahler Jan 09 '15 at 09:28
0

You basically have to check several cases, some trivial and some needing minimization searches.

First, there are 4 3-parallel-axis cases. If any of them passes (with @jean's test), you fit. Otherwise, continue to the next test cases:

Next, there are 18 2d diagonal cases, where one axis is parallel and the other two are diagonal, with one angle degree of freedom. Discard a case if the parallel axis doesn't fit; otherwise find the minimum of some "impingement" metric function of the single rotation angle. Then check for any actual impingement at that angle. The impingement metric has to be some continuous measure of how the inner box (4 corners) are staying inside the 2 faces of the outer box, allowing that sometimes they may go outside during the search for the minimum impingement angle. Hopefully a) there are a predictable maximum number of minima, and hopefully b) if there is a possible fit, then a fit is guaranteed at the angle of one of these minima.

If none of those cases passes without impingement, then move on to the larger number of 3d no-parallel-axes cases, where the rotation parameter is now three angles instead of one, and you have to search for a (hopefully limited number of) minima of the impingement metric, and test there for actual impingement.

Not really elegant, I think. This is similar to another thread asking how long of a line of given width can fit inside a 2d box of given dimensions. I didn't consider the parallel-axis case there, but the diagonal case requires solving a quartic equation (much worse than a quadratic equation). You may have a similar problem for your one-parallel-axis cases, if you want to go analytic instead of searching for minima of an impingement metric. The analytic solution for the no-parallel-axis 3d diagonal cases probably involves solving (for the correct root of) an even higher order equation.

PaulQ
  • 308
  • 2
  • 9
0

In fact, any box A with dimensions (a1, a2, a3) can fit in an other box B with dimensions (b1, b2, b3), in the following conditions:

i) Every ai is less than or equal to every bi with i = 1. 2. 3;

ii) Any ai has to be less than or equal to sqrt(b1^2+b2^2+b3^2), the main diagonal of B (diagB). Any box A with one of its dimensions equal to diagB, has the other two dimensions equal to 0, since any plane orthogonal to it would extend outside the box B.

iii) The sum of a1, a2 and a3 must be less than or equal to diagB.

From these, we can see that the greatest dimension ai of a box A for it to fit box B, given ai > bi, should lie in the interval (bi, diagB). Thus, any box with one dimension bigger than any dimension of a box containing it will necessarily placed along the latter's main diagonal.

Put it simply: A(a1, a2, a3) fits in B(b1, b2, b3) iff a1+a2+a3 <= diagB.

-2

Can you get box dimensions? Say a0,a1,a2 are the dimensions of box A ordered by size and b0,b1,b2 are the dimensions of box B ordered by size.

A fits inside B if (a0 <= b0 AND a1 <= b1 AND a2 <= b2)

jean
  • 4,159
  • 4
  • 31
  • 52
  • True only for the case of parallel axes, but not for the other cases where the inner box has to go on a 2d or 3d diagonal. – PaulQ Jan 11 '14 at 02:50