Construct a homeomorphism from anchor ring to torus [duplicate]
I know how to write the $(x,y,z)$ coordinates of a torus using $(\theta, \phi)$: $$ \begin{align} x(\theta, \phi) &= (R+r\cos(\theta))\cos(\phi) \\ y(\theta, \phi) &= (R+r\cos(\theta))\sin(\phi) \\ z(\theta, \phi) &= r\sin(\theta) \end{align} $$
For $\theta, \phi\in [0, 2\pi]$. How can I find the inverse relation? I would like to find expressions $$ \theta(x,y,z) = \ldots \\ \phi(x,y,z)=\ldots $$
Given that I have a point on the torus in cartesian coordinates $(x, y,z)$. How can I find its expression in terms of angles $(\theta, \phi)$?
From the last expression I can get $$ \theta = \arcsin\left(\frac{z}{r}\right) $$ and then I guess from the first equation one could find $$ \phi = \arccos\left(\frac{x}{R + r\cos(\theta)}\right) $$ which works when the denominator is not zero. To avoid having the denominator being zero we need $$ R+r\cos(\theta) \neq 0 \implies \theta \neq \arccos\left(-\frac{R}{r}\right) $$
Solution 1:
Note that you have $$ \sqrt{x^2+y^2} = R+r\cos\theta$$ We have then $$ \cos\phi = \frac{x}{\sqrt{x^2+y^2}} $$ $$ \sin\phi = \frac{y}{\sqrt{x^2+y^2}} $$ $$ \cos\theta = \frac{\sqrt{x^2+y^2}-R}{r}$$ $$ \sin\theta = \frac{z}{r}$$ Which can be used to recover $\theta$ and $\phi$. You should be careful with using $\arcsin$ though, at it will give you the correct value of the angle only if said angle is in the interval $[-\frac{\pi}{2},\frac{\pi}{2}]$.
To deal with that you need to consider different cases. That is because the equation $\sin\alpha = t$ has many solutions; one is $\alpha=\arcsin t$, but $\alpha=2\pi n+\arcsin t$, $n\in\mathbb{Z}$ is also a solution, as is $\alpha=2\pi n+\pi-\arcsin t$, $n\in\mathbb{Z}$. Out of all of these solutions the correct one is the one that gives the correct sign of $\cos \alpha$ and gives angle $\alpha$ in the correct predefined interval. For example, if you want $\phi$ to be in the interval $[0,2\pi)$, the full formula for $\phi$ will be $$ \phi = \left\{\begin{array}{ll}\arcsin\frac{y}{\sqrt{x^2+y^2}} & \text{if } x\ge 0,y\ge 0 \\ \pi-\arcsin\frac{y}{\sqrt{x^2+y^2}} &\text{if } x \le 0\\ 2\pi+\arcsin\frac{y}{\sqrt{x^2+y^2}} &\text{if } x\ge 0, y<0\end{array}\right. $$ On the other hand, if you wanted the values of $\phi$ to be in the range $(-\pi,\pi]$, the formula would be $$ \phi = \left\{\begin{array}{ll}\arcsin\frac{y}{\sqrt{x^2+y^2}} & \text{if } x\ge 0 \\ \pi-\arcsin\frac{y}{\sqrt{x^2+y^2}} &\text{if } x \le 0, y\ge 0\\ -\pi-\arcsin\frac{y}{\sqrt{x^2+y^2}} &\text{if } x\le 0, y<0\end{array}\right. $$ It can be checked that if $\phi$ is definied like this, then $\sin\phi$ and $\cos\phi$ satisfy the previous equations regardless of the signs of $x$ and $y$. $\theta$ can be properly defined in an analogous way, with different formulas depending on the signs of $z$ and $\sqrt{x^2+y^2}-R$.