# Reconstructing Nerve Structures from Unorganized Points
## 4 Materials and Methods
### Trajectory Renewing
\begin{bmatrix}\frac{q^{3}}{3}&\frac{q^{2}}{2}&0&0\\ \frac{q^{2}}{2}&q&0&0\\ 0&0&\frac{q^{2}}{3}&\frac{q^{2}}{2}\\ 0&0&\frac{q^{2}}{2}&q\end{bmatrix} , \tag{22}\]  
where \(\sigma_{x}^{2}\) and \(\sigma_{y}^{2}\) are the variances of the \(x\) and \(y\) coordinate measurement noises, respectively, while the parameter \(q\) must be chosen in advance. The parameter \(q\) represents a deviation from the assumed constant velocity model. Since it is generally unknown, here, we adopted its value as \(q=1\), while having in mind that the adaptation described in the following paragraphs will be able to indirectly model its unfixed value. In order to detect the deviation from a linear trajectory, it is necessary to adapt the Kalman filter. We used the adaptation approach through the fading factor \(S_{k+1}\)[36, 37, 38, 39]. This implies changing the covariance matrix of the state estimation Equation (17):  
\[\mathbf{P}_{k+1/k}=S_{k+1}\mathbf{F}\mathbf{P}_{k/k}\mathbf{F}^{T}+\mathbf{Q} _{k+1}. \tag{23}\]  
The fading factor \(S_{k+1}\) represents the measure of model nonstationarity at time step \(k+1\). It is based on the deviation of the theoretic covariance matrix (\(\mathbf{M}_{k+1}\)) from the covariance matrix estimated on a sample (\(\mathbf{N}_{k+1}\)) for the transformed prediction error sequence \(\mathbf{HF}\{\mathbf{x}_{k+1}-\hat{\mathbf{x}}_{k+1/k}\}\):  
\[S_{k+1}=max\{1,tr(\mathbf{N}_{k+1})/tr(\mathbf{M}_{k+1})\}, \tag{24}\]  
and where:  
\[\mathbf{M}_{k+1}=\mathbf{H}\mathbf{F}_{k/k}\mathbf{F}^{T}\mathbf{H}^{T}, \tag{25}\]\[\mathbf{N}_{k+1}=\mathbf{P}_{V_{k+1}}-\mathbf{H}\mathbf{Q}_{k+1}\mathbf{H}^{T}- \mathbf{R}, \tag{26}\]  
where \(P_{V_{k+1}}\) is the covariance matrix of the predicted residual vector. The predicted residual vector and its covariance matrix are given by:  
\[\mathbf{V}_{k+1}=\mathbf{z}_{k+1}-\mathbf{H}\mathbf{x}_{k+1/k}, \tag{27}\]  
\[\mathbf{P}_{V_{k+1}}=E(V_{k+1}V_{k+1}^{T})\cong\frac{1}{k}\sum_{i=1}^{k} \mathbf{V}_{k+1}\mathbf{V}_{k+1}^{T}. \tag{28}\]  
In an ideal case, when the trajectory is linear and the distances between the consecutive points are equal, \(\mathbf{M}_{k}\) and \(\mathbf{N}_{k}\) share the same value. In this case, the Kalman filter works in nominal mode (\(S_{k}=1\)). The adaptability of the Kalman filter is displayed through the situation when the measurements start to significantly differ from the predicted ones. This situation has a direct effect on the value of \(\mathbf{N}_{k}\), which then as a result increases the value of the fading factor (\(S_{k}>1\)) and of the covariance matrix of the state estimation. As a result, the algorithm depends less on the previous and more on the new measurements. After each state vector estimation \(\hat{\mathbf{x}}_{k+1/k}\), an area \(G(X)\) is formed around each newly found point, i.e., graph node, according to Relations (1), (2), and (8). For each unassigned point from \(G(X)\), a measure of accordance between the points and the trajectory is set. It is calculated in the manner of (7):  
\[S_{xy}=\cos\alpha_{xy}, \tag{29}\]  
where \(\alpha_{xy}\) is the angle between the vector \((Y-X)\) and the speed vector \(\hat{x}_{k+1}\mathbf{i}_{x}+\hat{y}_{k+1}\mathbf{i}_{y}\), where \(\mathbf{i}_{x}\) and \(\mathbf{i}_{y}\) are orthogonal unit vectors along the \(x\)- and \(y\)-axis, respectively. The point that maximizes the measure \(S_{xy}\) is chosen as the successor of the point \(X\).