# Can someone please explain the cube to sphere mapping formula to me?

I am wondering if anyone could explain how the following formula works, it is supposed to take the input as a point on a cube then map that to points on a sphere, please go gentle on me, I'm in 9th grade O_O

$$\begin{bmatrix}x' \\ y' \\ z'\end{bmatrix}=\begin{bmatrix} x\sqrt{1-\frac{y^2}{2}-\frac{z^2}{2}+\frac{y^2z^2}{3}} \\ y\sqrt{1-\frac{z^2}{2}-\frac{x^2}{2}+\frac{z^2x^2}{3}} \\ z\sqrt{1-\frac{x^2}{2}-\frac{y^2}{2}+\frac{x^2y^2}{3}} \end{bmatrix}$$

Thanks.

• That's a remarkable formula. I initially thought it might be simply a radial scaling, but it isn't. Where did you find it? – Rahul Mar 11 '12 at 4:50
• A report on procedural planetary generation, I asked the person for the some advice on the subject and he gave me his report. It uses that forumlar to transform a planet into something that terrain can be generated on to (or I think so anyway D:) – Darestium Mar 11 '12 at 5:15
• Oh, it's also on this blog post called "Mapping a Cube to a Sphere". That and its previous post seem to have some explanation. – Rahul Mar 11 '12 at 5:17
• What do you mean by "how it works"? Do you want to understand why the resulting points lie on a sphere? Or how someone came up with this formula? Or how to use it? – joriki Mar 11 '12 at 8:50
• You use the input as a point on a cube then that point is transformed to a cooresponding point on a sphere, So I am assuming you just use it as another location. I do not understand however, why the resulting points lie on a sphere, or how someone came up with this formula. – Darestium Mar 11 '12 at 9:04

Some indications on how the author arrived at the formula are given in the blog posts that Rahul linked to. Another approach is to consider the expression $1-(1-x^2)(1-y^2)(1-z^2)$. The second term is zero whenever one of the coordinates is $\pm1$, and thus in particular on the unit cube. Thus, if we can associate $(x,y,z)$ with a vector whose square has this form, then it will follow that this vector is a unit vector, and thus located on the unit sphere, whenever $(x,y,z)$ is on the unit cube.
The two-dimensional case of the unit square and unit circle is a bit easier. Here we want the square of the vector to be $1-(1-x^2)(1-y^2)=x^2+y^2-x^2y^2$. Of course there are all sorts of vectors that we could use, but if we want our vector be roughly similar to $(x,y)$, we could consider an expression that gets the $x^2$ term from the first component and the $y^2$ term from the second component. That leaves the $-x^2y^2$ term to be dealt with, and it makes sense to distribute that symmetrically over the two components. Thus we want $(x',y')$ with $x'^2=x^2-x^2y^2/2$ and $y'^2=y^2-x^2y^2/2$, and that yields precisely the expressions given in the blog post for the square and the circle, $x'=x\sqrt{1-y^2/2}$ and $y'=y\sqrt{1-x^2/2}$.
If we want to do the same thing for the cube and sphere, we just have some more terms to distribute: $1-(1-x^2)(1-y^2)(1-z^2)=x^2+y^2+z^2-x^2y^2-y^2z^2-z^2x^2+x^2y^2z^2$. There's one term for each pair of components, and we can distribute this in equal parts over those two components; and there's one term that contains all three components, which we can distribute in equal parts over all three components. The result is the formula you give.
I'm not saying that the author arrived at the formula in this manner, or that it was easy to come up with the formula (things are always a lot easier with hindsight), but that looking at it in this way allows a more systematic understanding of the formula by drawing a connection to the fact that a product (in this case $(1-x^2)(1-y^2)(1-z^2)$) is zero if and only if at least one of its factors is zero.