TITLE: Compute the distance from a point in $\textbf{R}^3$ to Boy's surface
QUESTION [9 upvotes]: We're given a parametric surface $S\subset\textbf{R}^3$, and an arbitrary $\boldsymbol x\in\textbf{R}^3$ where $\boldsymbol x := (x_1,x_2,x_3)$.
We'd like to compute the distance from $\boldsymbol x$ to $S$.
Note. By distance I mean smallest (Euclidean) distance.
Example. Let $S\subset\textbf{R}^3$ be the image of the map
$$
\begin{align}
[0;2\pi] \times [0;\pi] & \longrightarrow \textbf{R}^3 \\
(\theta, \varphi) & \longmapsto (s_1, s_2, s_3) \\
(\theta, \varphi) & \longmapsto (\cos(\theta)\sin(\varphi),\ \sin(\theta)\sin(\varphi),\ \cos(\varphi))
\end{align}
$$
(ie. $S$ is the unit sphere centered at $\boldsymbol 0\in\textbf{R}^3$). Then the (smallest Euclidean) distance $d$ from $\boldsymbol x \in \textbf{R}^3$ to $S\subset\textbf{R}^3$, is (I think)
$$ d = |\boldsymbol x - \boldsymbol 0| - 1 = |\boldsymbol x| - 1,$$
where $|\boldsymbol a|$ is the norm of $\boldsymbol a$.

The case of interest is the distance from $\boldsymbol x\in\textbf{R}^3$ to the Kusner-Bryant parametrization of Boy's surface (an immersion of $\textbf{R}P^2$ inside $\textbf{R}^3$), which is a function of a complex parameter $w\in\textbf{C}$ in the complex unit disk $\textbf{D}$ (ie. $|w| \leq 1$).
Now $S\subset\textbf{R}^3$ is the image of the map
$$
\begin{align}
\textbf{D}\subset\textbf{C} & \longrightarrow \textbf{R}^3 \\
w & \longmapsto (s_1, s_2, s_3) \\
w & \longmapsto (gg_1, gg_2, gg_3)
\end{align}
$$
where
$$
\begin{align}
g1 & := -{3 \over 2} \text{Im}\left[{w(1-w^4) \over g_4}\right] \\
g2 & := -{3 \over 2} \text{Re}\left[{w(1+w^4) \over g_4}\right] \\
g3 & := \text{Im}\left[{1 + w^6 \over g_4}\right] - {1 \over 2} \\
g4 & := w^3(w^3 + \sqrt5) - 1\\
g & := {1 \over g_1^2 + g_2^2 + g_3^2}
\end{align}
$$
What is an expression for the distance from $\boldsymbol x$ to $S$?
(An application of this is to render the surface)

REPLY [3 votes]: I am not really solving your questions, since there are a lot of nasty partial derivatives and calculations that I am not even going to try, but I believe thet are doable with a lot of patience (and pain). 
Identify $\mathbf{D}$ with the closed unit ball $\overline{B(\mathbf{0},1)}$ in $\mathbb{R}^2$ and consider the mapping $\mathbf{f}: \overline{B(\mathbf{0},1)}\to \mathbb{R}^3$ given by $\mathbf{f}(w_1,w_2)=(gg_1,gg_2,gg_3)$. You want to compute
$$\min_{(w_1,w_2)\in \overline{B(\mathbf{0},1)}} \vert \mathbf{x}-\mathbf{f}(w_1,w_2)\vert,$$
or equivalently, 
$$\min_{(w_1,w_2)\in \overline{B(\mathbf{0},1)}} \vert \mathbf{x}-\mathbf{f}(w_1,w_2)\vert^2.$$
Since $\overline{B(\mathbf{0},1)}$ is compact, the minimum exists. If it is reached at a point $(a,b)$ in  $B(\mathbf{0},1)$, then it must be a critical point of the function $h(w_1,w_2):=\vert \mathbf{x}-\mathbf{f}(w_1,w_2)\vert^2,$
which means that you need to compute $$\frac{\partial{h}}{\partial w_1}(w_1,w_2)=-2(\mathbf{x}-\mathbf{f}(w_1,w_2))\cdot\frac{\partial{\mathbf{f}}}{\partial w_1}(w_1,w_2)=0,\\
\frac{\partial{h}}{\partial w_2}(w_1,w_2)=-2(\mathbf{x}-\mathbf{f}(w_1,w_2))\cdot\frac{\partial{\mathbf{f}}}{\partial w_2}(w_1,w_2)=0.$$  Really painful, given the form of $\mathbf f$. 
Otherwise the maximum is reached at a point on $\partial B(\mathbf{0},1)$ and so you can find it by using polar coordinates and studying the minimum of
$\varphi(\theta)=\vert \mathbf{x}-\mathbf{f}(\cos\theta,\sin\theta)\vert^2$ for $\theta\in [0,2\pi]$. 
If the point $\mathbf{x}$ is very close to $S$, then the closest point $\mathbf{s}\in S$ to $\mathbf{x}$ will be unique and will satisfy the relation
$$ \mathbf{x}=\mathbf{s}+\mathbf{\nu}(\mathbf{s})|\mathbf{x}-\mathbf{s}|,$$
where $\mathbf{\nu}(\mathbf{s})$ is a unit normal to $S$ at $\mathbf{s}$ (see link). 
Again to find the normal you will need to find the partial derivatives  $\frac{\partial{\mathbf{f}}}{\partial w_1}$ and $\frac{\partial{\mathbf{f}}}{\partial w_2}$ at $\mathbf{s}$ and then calculate its tensor product.