Wednesday, July 06, 2005

Mapping a Cube to a Sphere

After the last post, I got to thinking about how to come up with a mapping from the cube

img1

to the points in the unit sphere. In the last post I used the mapping

img2

Taking the length of the new point gives

img3

So you can see that if x or y are either -1 or +1, this yeilds a vector with unit length. So, beginning with this, I decided that I'd work backward from the guess that if I had a mapping from the cube to the unit sphere, the length of a generated point would need to be

img4

Which leads to the mapping

img5

Trying this out yields some pretty nice results. This is the cube and its mapping onto the unit sphere. The thick lines are lines where at least two of the components are -1 or +1.

cubesphere

And here's another view from inside the sphere

sphereInside

Sorry this post is a little light on justification. I might try to explain this by looking at the level curves defined by constant x, y and z, but I'll save that for another day (maybe).

20 comments:

  1. wow, didn't sphere mapping could be done that way. Sw33t!!!!

    ReplyDelete
  2. Nice, but an inverse transform would be nice. How do you take coordinates on the sphere and transform them to coordinates on the cube?

    I've been working on it for a few minutes, but the algebra gets a little messy ;)

    ReplyDelete
  3. That is very elegant - I like it!

    ReplyDelete
  4. Inverse transform would be nice idd !!!

    ReplyDelete
  5. I would also love to see an inverse of this transformation.

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. Please could you tell me inverse transformation - thx

    the_andreyp@hotmail.com

    thx )

    ReplyDelete
  8. nice job..inverse transformation would be great

    ReplyDelete
  9. Have you got any idea the transformations between that cube sphere you have and a conventional longitude/ latitude model?

    ReplyDelete
  10. Just a random comment - shouldn't the view from inside the sphere, at its very center - look exactly as if you were inside the cube?

    ReplyDelete
  11. I've posted a solution for the reverse mapping from sphere to cube on my blog: http://petrocket.blogspot.com/2010/04/sphere-to-cube-mapping.html

    I also did a reverse mapping for the 2D version if anyone wants that.

    ReplyDelete
  12. Thank you :) Saved me a lot of time and effort

    ReplyDelete
  13. Wow really loved the formula, Have you worked on transferring rectangular cube to ellipsoid? same thing for your previous post. How to transfer from rectangle to ellipse?

    Would you please email me if you have the answer?

    Thanks

    ReplyDelete
  14. Answering the question posed by Anonymous above:

    Let V be a vector from the center to a point on the sphere.
    Take the greatest of the 3 coordinates and use it to decide which face of the cube you're onto. For instance (1, 0, 0) means positive-X side of the cube.
    Use the other two coordinates as planar 2D coordinates within that face.

    (see cubic environment mapping)

    ReplyDelete
  15. This is very useful. Thank you.

    ReplyDelete
  16. You are Genious!!! Good work.

    ReplyDelete
  17. Amazing! That's exactly what I'm looking for!

    ReplyDelete
  18. Could you help me out with this but going from sphere mapping to cube? I want to use a panoramic texture in a cube environment.

    Maik

    ReplyDelete
  19. Very interesting indeed. Could you be so kind as to post the mapping of a sphere to a rectangular prism having sides A, B, and C? That would be very useful to me.

    ReplyDelete