For an axis aligned cube, there are nice tricks. Consider, for example, the axis aligned cube with corners $(\pm 1,\pm 1,\pm 1)$ (after a scaling and a shift, everything reduces to this case).
For a point $(x,y,z)$:
If $|x|\leq 1$, $|y|\leq 1$ and $|z|>1$, then the distance is $|z|-1$ (the face is closest).
If $|x|\leq 1$, $|y|>1$, and $|z|>1$, then the distance is $\sqrt{(|y|-1)^2+(|z|-1)^2}$ (the edge is closest).
If $|x|>1$, $|y|>1$, and $|z|>1$, then the distance is $\sqrt{(|x|-1)^2+(|y|-1)^2+(|z|-1)^2}$ (the vertex is closest).
All other cases are similar. To visualize what is going on, draw a square, but extend the edges into (infinite) lines. This breaks up the space outside the box into $8$ regions, all points in each region are closest to the same edge or point. Now, doing the same thing in three dimensions results in $26$ regions and you need to figure out which region you're in.
Pseudocode:
if |x|<=1
{
if |y|<=1
d=|z|-1
else
{
if |z|<=1
d=|y|-1
else
d=sqrt((|y|-1)^2+(|z|-1)^2)
}
}
else
{
if |y|<=1
{
if |z|<=1
d=|x|-1
else
d=sqrt((|x|-1)^2+(|z|-1)^2)
}
else
if |z|<=1
d=sqrt((|x|-1)^2+(|y|-1)^2)
else
d=sqrt((|x|-1)^2+(|y|-1)^2+(|z|-1)^2)
}
}
Also as @YvesDaoust mentions in the comments, this can be rewritten as
$$
\sqrt{\max\{0,|x|-1\}^2+\max\{0,|y|-1\}^2+\max\{0,|z|-1\}^2}
$$
although, after unpacking the maximums, you get, essentially, the series of inequalities above.
As a preliminary, the equation for an ellipse is $x^2/a^2+y^2/b^2=1$, where the length of the major (long) axis is $2a$.
If we are given the center of the ellipse and a point
$P(x,y)$ outside of the ellipse, then we can calculate the distance to the edge as in your diagram by calculating the distance $d$ between point $P$ and the center of the ellipse, then subtracting the distance between the intersection and the center of the ellipse.
By the Pythagorean Theorem, $d=\sqrt{(x-c_x)^2+(y-c_y)^2}$, where $c_x$ and $c_y$ are the $x$ and $y$ coordinates of the center of the ellipse respectively. The slope $m$ of this segment is $\frac{y-c_y}{x-c_x}$, so now we have the equation of a line: $y=mx+b$, where we can find $b$ by substituting our point $P$ or the center of the ellipse $(c_x, c_y)$. We can now substitute this equation into our ellipse equation to find the point on the ellipse that the line intersects, and then use the Pythagorean Theorem again to find this distance, which we'll call $d_1$. Now, the solution is $d-d_1$.
Best Answer
First make your cube axis aligned, ie. use an (invertible) affine transformation $f:\mathbb R^3 \rightarrow \mathbb R^3$ such that the faces are all parallel to some $xy$-/$xz$-/$yz$-plane. Dont forget to apply this affine transformation to your point.
For an axis aligned box $[x_\min,x_\max]\times[y_\min,y_\max]\times[z_\min,z_\max]$ you can find the projection of a point $q = (q_x,q_y,q_z)$ outside the box by taking $p = (\mathsf{clamp}(q_x,x_\min,x_\max),\mathsf{clamp}(q_y,y_\min,y_\max),\mathsf{clamp}(q_z,z_\min,z_\max))$, where
$$\mathsf{clamp}(t,a,b) = \left\{ \begin{array}{ll}a&\text{if }t\leq a\\t & \text{if }t \in [a,b]\\ b &\text{if }b \leq t\end{array}\right.$$
Since you will likely want to have the coordinates of the projection in the original coordinate system, apply the inverse of $f$ to the point just calculated.
This should generalize to higher dimensions.