# Placing an ellipsoid in a rectangular box

Suppose you have an ellipsoid with semi-axes lengths $$a,b,c$$, and you a rectangular box (i.e. cuboid) of dimensions $$L, W, H$$, with its length $$L$$ along the $$x$$ axis, its width $$W$$ along the $$y$$ axis, and its height $$H$$ along the $$z$$ axis. It can be shown that if the box fits the ellipsoid exactly, i.e. the ellipsoid is tangent to all $$6$$ sides of the box, then we must have

$$\dfrac{1}{4} (L^2 + W^2 + H^2) = a^2 + b^2 + c^2 \hspace{20pt}(*)$$

It can also be shown that

$$e_1^T R D R^T e_1 = \left( \dfrac{L}{2} \right)^2 \hspace{20pt}(1)$$

$$e_2^T R D R^T e_2 = \left( \dfrac{W}{2} \right)^2 \hspace{20pt} (2)$$

$$e_3^T R D R^T e_3 = \left( \dfrac{H}{2} \right)^2 \hspace{20pt} (3)$$

where $$e_1, e_2, e_3$$ are the coordinate unit vectors, and $$R$$ is the (rotation) matrix that describes the orientation of the semi-axes of the ellipsoid with respect to the world frame, where it is assumed that $$L$$ is along the $$x$$ axis, $$W$$ is along the $$y$$ axis, and $$H$$ is along the $$z$$ axis. Matrix $$D$$ is a diagonal matrix with the diagonal entries being $$a^2, b^2, c^2$$.

Because of the relation $$(*)$$, only TWO equation of the set $$(1), (2), (3)$$ are independent, while the third follows from $$(*)$$.

Since $$R$$ can be parameterized using $$3$$ angles, and now we have two constraints on it, then it follows that there is an infinite number of rotation matrices $$R$$ that satisfy equations $$(1), (2), (3)$$. And it also follows that $$R$$ can be parameterized by a single parameter, i.e. we have a single degree of freedom for describing $$R$$.

My question, is how to find such parameterization of $$R$$ ?

• Don't you think that $e_1,e_2,e_3$ should be chosen as unit vectors along the direction of the edges of the box ? Commented Jul 22, 2023 at 13:40
• Yes. It is assumed that box is standing on the $xy$ plane with its height along the $z$ axis. But I will make this clear in my question. Commented Jul 22, 2023 at 14:04

I've found a parameterization using the tangency points, I hope it may be of help.

Let $$P$$, $$Q$$, $$R$$ be the tangency points between the ellipsoid and three concurrent faces of the cuboid (see figure below). It follows from the properties of the ellipse that the line joining the centre $$O$$ of the cuboid with the midpoint of $$PQ$$ must intersect the line of the common edge between the two faces where $$P$$ and $$Q$$ lie. If $$M$$ and $$U$$ are the projections of $$P$$ and $$Q$$ on that edge, then it is not difficult to prove that the above property entails $$PM : H = QU : L$$ and of course two analogous proportions arise from the other pairs of tangency points.

Set up a coordinate system such that the cuboid is described by $$-{L\over2}\le x\le{L\over2},\quad -{W\over2}\le y\le{W\over2},\quad -{H\over2}\le z\le{H\over2}.$$ Then we can parameterize the positions of tangency points as follows: $$P=\left({L\over2},{W\over2}t,{H\over2}u\right),\quad Q=\left({L\over2}u,{W\over2}v,{H\over2}\right),\quad R=\left({L\over2}t,{W\over2},{H\over2}v\right),$$ where parameters $$t$$, $$u$$, $$v$$ vary in the range $$-1\le t,u,v\le1$$. With this choice the above constraints are automatically fulfilled.

From now on, for the sake of simplicity, I'll consider the case of a cube $$L=W=H=2$$. The general case can be recovered with a scaling. The above coordinates become: $$P=\left(1,t,u\right),\quad Q=\left(u,v,1\right),\quad R=\left(t,1,v\right),$$

The generic equation of the ellipsoid is: $$\alpha x^2+\beta y^2+\gamma z^2+\delta xy+\eta yz+\zeta xz=1.$$ Imposing the ellipsoid to be tangent to the cube at $$P$$, $$Q$$, $$R$$ we can find the coefficients as functions of $$t$$, $$u$$, $$v$$: $$\alpha={1-v^2\over d},\quad \beta={1-u^2\over d},\quad \gamma={1-t^2\over d},\quad \delta = 2{uv-t\over d},\quad \eta = 2{tu-v\over d},\quad \zeta = 2{tv-u\over d},$$ where: $$d = 1 + 2 t u v - t^2 - u^2 - v^2.$$ The matrix associated to the ellipsoid is $$M=\pmatrix{ \alpha & \delta/2 & \zeta/2 \\ \delta/2 & \beta & \eta/2 \\ \zeta/2 & \eta/2 & \gamma \\ }$$ and we can relate it to the semi-axes $$a$$, $$b$$, $$c$$ of the ellipsoid via the invariants: $$\det M ={1\over a^2b^2c^2},\quad \text{tr}\ M = {1\over a^2}+{1\over b^2}+{1\over c^2}.$$ Inserting here the above values, we thus find: $$a^2b^2c^2=d=1 + 2 t u v - t^2 - u^2 - v^2,\quad a^2b^2+b^2c^2+a^2c^2=3-t^2-u^2-v^2,$$ which can be rearranged as: $$tuv={1\over2}(2-a^2b^2-b^2c^2-a^2c^2+a^2b^2c^2),\quad t^2+u^2+v^2=3-a^2b^2-b^2c^2-a^2c^2.$$ These equalities give then two constraints on the parameters. The third invariant of $$M$$ would simply give $$a^2+b^2+c^2=3$$, which is already known.

The second constraint means that point $$(t,u,v)$$ lies on the surface of a sphere with radius $$r=\sqrt{3-a^2b^2-b^2c^2-a^2c^2}.$$ We can then set: $$t=r\cos\theta,\quad u=r\sin\theta\cos\phi,\quad v=r\sin\theta\sin\phi,$$ and inserting into the first constraint we get: $$\sin^2\theta\cos\theta\sin2\phi= {2-a^2b^2-b^2c^2-a^2c^2+a^2b^2c^2\over r^3}.$$ If we fix the value of $$\theta$$ we can compute from there the possible values of $$\phi$$ and parameters $$t,u,v$$.