For usage in a shader I am looking for an efficient way to convert spherical coordinates to latitude/longitude values in $[0,\pi]$, $[0,2\pi]$ respectively. (Conversion to convential long/lat with these values would be trivial)
For the spherical coordinates I'm using the mathematical convention with $\phi$ corresponding to the latitude and $\phi=0$ at the north pole and $\theta$ corresponding to the longitude.
As a starting point one could write
$$lat = \phi$$ $$long = mod(\theta, 2\pi) $$
$r$ is left out as it is constant.
Now the difficulty I'm having is extending this to convert spherical coordinates with $\phi$ not in $[0,\pi]$. In this case spherical coordinates can be written as (again ignoring $r$)
$$\vec{r} = \phi\hat{\phi} + \theta\hat{\theta}$$
I am not sure how I should express the way in which the latitude $\phi$ influences the longitude if
$$\pi<mod(\phi, 2\pi)<2\pi$$
If I'm not mistaken, one way would be to write it as a piecewise function like this:
$$ \vec{r}_{longlat}=\begin{cases} mod(\phi, \pi)\hat{\phi} + mod(\theta, 2\pi)\hat{\theta} & mod(\phi, 2\pi)\in[0,\pi]\\ (\pi-mod(\phi, \pi))\hat{\phi} + mod(\theta+\pi, 2\pi)\hat{\theta} & mod(\phi, 2\pi)\notin[0,\pi] \\ \end{cases} $$
However, I'm wondering if there's a more elegant way to go about it.
Another way to do it would be to simplify the result of converting the spherical coordinates to carthesian coordinates and then getting the spherical coordinates back from that again. In that case one finds the carthesian coordinates,
$$ \vec{p} = \begin{bmatrix} cos(\theta)sin(\phi) \\ sin(\theta)sin(\phi) \\ cos(\phi) \end{bmatrix} $$
which can be converted back to get
$$ \vec{r}_{longlat} = \begin{bmatrix} arccos(cos(\phi)) \\ atan2(cos(\theta)sin(\phi), sin(\theta)sin(\phi)) \\ \end{bmatrix} $$
Then the $arccos(cos(\phi))$ gives the ping-pong behaviour and atan2 would handle the offset on $\theta$ based on $\phi$.
Which of these two makes more sense to use? Which would you expect to be more efficient? Are there any alternatives/well known formulae I'm not considering?