Map 2d texture coordinates to 2d sphere projection

2.4k Views Asked by At

I have an image that I use to texture a unit sphere. The sphere is then projected to 2D from various angles and saved as image. For the input image, I have some coordinates $(x,y)$ for which I want to get equivalents $(x',y')$ in the output image, as shown in red in the images below.

INPUT IMAGE

OUTPUT IMAGE

However, I got stuck after transforming the image coordinates into normalized cartesian coordinates. More specific, from normalized cartesian coordinates I want to obtain the 3d unit sphere coordinates. I found some formulas that should help with this but they don't seem to work:

r = 1
x_3d = r * cos(pi*(y - 0.5)) * cos(pi*2*x) 
y_3d = r * cos(pi*(y - 0.5)) * sin(pi*2*x) 
z_3d = r * sin(pi*(y - 0.5)) 

I also tried this (with $R = 1$ and $S = 1$) and that with $\text{radius} = 1$ and $\rho = 1$ but don't seem right either.

What is the right equation for it?

For 3d to 2d projection I plan to use 3D projection on a 2D plane ( weak maths ressources ).

Is there an easier way of obtaining the output coordinates? Thank you.

1

There are 1 best solutions below

0
On

What you are describing is Mercator Projection.

You wrap your flat image around the sphere as a cylinder with a vertical axis (any axis will do, but you might as well rotate it so the axis is vertical). Then everything projects onto the sphere horizontally towards the axis.

This is easiest to express in cylindrical, not spherical, coordinates. Since you are using $y$ as your vertical axis ($z$ is far more common, but it isn't necessary), cylindrical coordinates have the form $(r, \theta, y)$ with $r \ge 0$ and $-\pi < \theta \le \pi$ and correspond to rectangular (i.e., Cartesian) coordinates by $$(r, \theta, y)_{C} = (r\sin \theta, y, r\cos\theta)_R\\(x, y, z)_R = \left(\sqrt{x^2 + z^2}, \operatorname{atan2}(z, x), y\right)_C$$

If $\phi$ is the angle the point makes with the origin and the horizontal plane $y = 0$, positive when $y > 0$, then the relationship between cylindrical and spherical coordinates $(\rho, \theta, \phi)$ is $$(r, \theta, y)_C = \left(\sqrt{r^2 + y^2}, \theta,\tan^{-1}\left(\frac yr\right)\right)_S\\(\rho, \theta, \phi)_S = (\rho\cos\phi, \theta, \rho\sin\phi)_C$$

Calling your image coordinates $(u,v)$ with $-1\le u, v \le 1$, the coordinate $v$ maps directly to $y$, while $\theta = \pi u$. The radial coordinate is found from the equation of the unit sphere: $r^2 + y^2 = 1$.

So the mapping is: $$\begin{align}(u, v) \mapsto& \ \left(\sqrt{1-v^2}, \pi u, v\right)_C\\=&\ \left(\sqrt{1-v^2}\sin\pi u, v, \sqrt{1-v^2}\cos\pi u\right)_R\\=&\ \left(1, \pi u, \sin^{-1}v\right)_S\end{align}$$

If your plan is to then parallel project the sphere onto the plane (much as one would see it if viewing from a long distance away on the $z$ axis), then the easiest form to use is rectangular coordinates, as you can just drop the $z$ coordinate for the parallel projection:

$$(u, v) \mapsto \left(\sqrt{1-v^2}\sin\pi u, v, \sqrt{1-v^2}\cos\pi u\right)_R \mapsto \left(\sqrt{1-v^2}\sin\pi u, v\right)$$ Though you will need to restrict to $-\frac 12 \le u \le \frac 12$ to get only the front side of the sphere.