3

I'd like to do the opposite of the example specified here: https://en.wikipedia.org/wiki/UV_mapping

Can somebody explain to me how to do it?

Thanks,

For any point $P$ on the sphere, calculate $\hat d$, that being the unit vector from $P$ to the sphere's origin.

Assuming that the sphere's poles are aligned with the $Y$ axis, UV coordinates in the range $[0, 1]$ can then be calculated as follows: \begin{align} u &= 0.5 + \frac{\text{arctan2}(d_z, d_x)}{2\pi} \\ v &= 0.5 - \frac{\arcsin(d_y)}{\pi} \end{align}

David K
  • 98,388
Bushes
  • 131
  • 2
    please add the example also to the question, so that your question is a stand alone one and has not to rely on possibly changing references. – user190080 Aug 13 '15 at 11:41

2 Answers2

2

Given that $\hat d$ is a unit vector satisfying the equations

\begin{align} u &= 0.5 + \frac{\text{arctan2}(d_z, d_x)}{2\pi} \quad \text{and} \\ v &= 0.5 - \frac{\arcsin(d_y)}{\pi}, \end{align}

we have

\begin{align} \text{arctan2}(d_z, d_x) &= 2\pi(u - 0.5) \quad \text{and} \\ \arcsin(d_y) &= \pi(0.5 - v) \end{align}

where $u,v \in [0,1]$. The $d_y$ coordinate is easily dealt with: $$d_y = \sin(\pi(0.5 - v)).$$

The $\text{arctan2}$ function is a little harder to invert because we have only one output parameter for two input parameters. But the input to the function can be known up to some positive factor $r > 0$:

\begin{align} d_z & = r \cos(2\pi(u - 0.5)) \quad \text{and} \\ d_x & = r \sin(2\pi(u - 0.5)). \end{align}

(That assumes $\text{arctan2}(\cos\theta, \sin\theta) = \theta$ for $-\pi < \theta < \pi$; if this is the version of $\text{arctan2}$ such that $\text{arctan2}(\sin\theta, \cos\theta) = \theta$, which is more usual in software libraries, then swap the sin and cos functions in the formulas above.)

With the added constraint that $d_x^2 + d_y^2 + d_z^2 = 1$, we have $r^2 + \sin^2(\pi(0.5 - v))$ and therefore $r = \cos(\pi(0.5 - v))$. Putting all of this together,

\begin{align} d_x & = \cos(\pi(0.5 - v)) \sin(2\pi(u - 0.5)), \\ d_y & = \sin(\pi(0.5 - v)), & \text{and} \\ d_z & = \cos(\pi(0.5 - v)) \cos(2\pi(u - 0.5)). \end{align}

David K
  • 98,388
0

Would $$ (r\cos u\sin v, r\sin u \sin v, r\cos v) $$ be what you're looking for? In this case, $r$ is the radius of the sphere, $u$ goes from $0^\circ$ to $360^\circ$ (or from $-180^\circ$ to $180^\circ$ if you want) while $v$ goes from $-90^\circ$ to $90^\circ$.

The triple $(r, u, v)$ is called spherical coordinates of the point with the rectangular coordinates as above.

Arthur
  • 199,419
  • Yeah think I've seen what I'm looking for thanks! – Bushes Aug 13 '15 at 12:39
  • 2
    The coordinates $u,v$ in the question are scaled and translated from the standard spherical coordinates; also, the polar angle in these coordinates is measured from the $y$ axis rather than from the $z$ axis and is zeroed at the equator rather than at the pole. So you will have to do a little more work on the reverse transformation, but the standard spherical-coordinates formula at least gives you a general idea to work with. – David K Aug 13 '15 at 13:12