\subsection{Junctions}

The last step to complete is to mesh the junctions.
A junction is a point of a skeleton where more than 2 branches join. The process for meshing this portion of the skeleton is complex. In fact it needs to be applicable on multiple cases, for instance 3 or 4 branches (see Figure \ref{junction}, and to take into account the fact that the perfect case will not always be there.

\begin{figure}[H]
   \begin{center}
   \includegraphics[scale=0.5]{img/Junctions9}
   \caption{\label{junction}Three splines junction}
   \end{center}
\end{figure}

In the figure \ref{junction1}  you can see what we should theoretically obtain with a perfect skeleton. You can see the sphere shared between three splines joining in this junction, and the three characteristic circles associated. Those circles are tangent by pair in one point.

\begin{figure}[H]
   \begin{center}
   \includegraphics[scale=0.5]{img/JunctionTheory}
   \caption{\label{junction1}Theoretical case with perfect characteristic circles}
   \end{center}
\end{figure}

But in most of practical cases it is not like this. This is due to number's approximation on computers. The idea is then to look for the closest points of two consecutive circles and to join them. Then we join every point of the circles in one point upside and one downside.

We begin by identifying the last circles of each spline of the junction (see figure \ref{junction3}).

\begin{figure}[H]
   \begin{center}
   \includegraphics[scale=0.5]{img/Junctions3}
   \caption{\label{junction3}3-splines junction}
   \end{center}
\end{figure}

To be able to do it the first step is to cut the circles in two parts to find the upside and downside of each of them. This can be done by computing the best fitting plane for the set of circles' center points (figure \ref{junction2}). 
The equation to find is easy when there is only 3 circles : first we compute the normal vector of the plane and then the constant element of the equation.
However when there is more than 3 circles to join, so more than three points to approximate, we need to use a PCA (Principal component analysis) decomposition to find the best fitting plane.

The first thing to do is to create X the matrix of the points coordinates. 
\[
X =
 \begin{pmatrix}
  x_{1} & x_{2} & \cdots & x_{n} \\
  y_{1} & y_{2} & \cdots & y_{n} \\
  z_{1} & z_{2} & \cdots & z_{n}
 \end{pmatrix} 
 \]
Then subtract it with the centroid matrix associated.
\[ 
 X_{c} = X - X_{m}
\]
The normal vector $N$ of the best-fitting plane will be the cross product of the 2 eigenvectors $v_1,v_2$ corresponding to the two biggest singular values of the covariance matrix of $X_{c}$ : 
\[
 X_{c}X_{c}^T
 \]
 \[
 N = v_1 \wedge v_2
 \]

Then to compute the constant value of the equation we use the centroid.
\[
d = -N*X_m
\]

At the end we have the parameters of the plane in N and d.

\begin{figure}[H]
   \begin{center}
   \includegraphics[scale=0.35]{img/Junctions1}
   \includegraphics[scale=0.35]{img/Junctions2}
   \caption{\label{junction2}Best fitting plane for the set of circles' center points}
   \end{center}
\end{figure}

Then when points are sorted we connect the up-points of each circle with the up-projection of the sphere's center on itself, and idem for the down-points with the down-projection of the sphere's center.
The result of this process is presented in figure \ref{junction4}.

\begin{figure}[H]
   \begin{center}
   \includegraphics[scale=0.5]{img/Junctions4}
   \includegraphics[scale=0.5]{img/Junctions5}
   \caption{\label{junction4}Edges added and mesh result}
   \end{center}
\end{figure}

To easily see the result of the mesh on a junction, we have made some tests with only the extreme circles that we use. On real skeletons the junction are not easy to watch.
Here \ref{junction5} are two examples of a junction's mesh for 3 and 4-branches junctions.

\begin{figure}[H]
   \begin{center}
   \includegraphics[scale=0.5]{img/Junctions10}
   \includegraphics[scale=0.4]{img/Junctions11}
   \caption{\label{junction5}Mesh result on 3 and 4-branches junctions}
   \end{center}
\end{figure}
