# 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?
– user856
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:) 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.
– user856
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? 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. 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.

• Nice, especially the last sentence. Mar 12 '12 at 8:21

Thanks for the explanation by joriki. The above formula has been explained in 1. But the formula has some limitations. The cube length can be 2 (-1 to 1 in all directions). From the above concept, mentioned by joriki or 1 we can generalize for the cube of any length (i.e 2a (-a to a in all directions)). Here 2a is the side length of the cube.

$$a^6-(a^2-x^2 )(a^2-y^2)(a^2-z^2)=x^2 a^2 (a^2-\frac{z^2}{2}-\frac{y^2}{2}+\frac{y^2 z^2}{3a^2})+y^2 a^2 (a^2-\frac{z^2}{2}-\frac{x^2}{2}+\frac{x^2 z^2}{3a^2})+z^2 a^2 (a^2-\frac{x^2}{2}-\frac{y^2}{2}+\frac{y^2 x^2}{3a^2})$$

So,

$$x'=xa \sqrt{a^2-\frac{z^2}{2}-\frac{y^2}{2}+\frac{y^2 z^2}{3a^2}}$$,

$$y'=ya \sqrt{a^2-\frac{z^2}{2}-\frac{x^2}{2}+\frac{x^2 z^2}{3a^2}}$$,

$$z'=za \sqrt{a^2-\frac{x^2}{2}-\frac{y^2}{2}+\frac{y^2 x^2}{3a^2}}$$.

From the above equation, we can easily separate the (x',y',z') as explained in 1. The above equation will readily find the mapping of the generalized length of the cube to a sphere. I hope this will be helpful.

Form the above equation we can also find the evenly distribution of points within the sphere by varying the grid size of the cube. By discretizing the cube into different grids and simultaneously mapping into the sphere will give the evenly distribution of points within the sphere.

Kindly ignore some typos.