# Reconstructing Nerve Structures from Unorganized Points
## Appendix A  
The approach in this pseudocode provides a deeper dive into the procedure introduced in Section 4. ```
1:functionReconstruction(unorganizedPoints)
2: Initialize necessary variables and data structures. 3: Compute minimum distances between all points in the input dataset (variable _minDist_ referenced in Equation (10). 4:while not all points are sorted do
5: Determine a start for a new cluster. 6:while current cluster is incomplete do
7:if cluster is starting then
8: Estimate average direction of nearby points using classic valley-seeking approach:
9: Calculate mean position \(M\) of candidate surrounding points. 10: Calculate angle \(\alpha\) based on direction from current point to \(M\). 11:else
12: Predict next direction using adaptive Kalman filter:
13: Use Kalman filter to predict direction of cluster growth. 14: Calculate angle \(\alpha\) based on estimated velocity components from Kalman filter. 15:endif
16:for each candidate point in surrounding do
17: Calculate its angle \(\beta\) with respect to the current point. 18: Determine the similarity score \(S\) between \(\alpha\) and \(\beta\). 19:endfor
20:if a clear maximum value in \(S\) exists then
21: Choose the point corresponding to \(\max\)\(S\) value as the next point. 22:else
23: Handle special cases: current point as the root of the graph or multiple potential next points. 24:endif
25: Add the chosen point to the current graph and cluster. 26:if omitted points exist between current and next point then
27: Include them in the cluster (as in Section 4.3). 28:endif
29:endwhile
30: Two evaluations described in Section 4.4 and in Algorithm A4 of this Appendix A. 31:endwhile
32: Merge clusters that belong to the same curves. 33:return clusters and graph. 34:endfunction
```  
**Algorithm A1** Algorithm for reconstruction from unorganized points. ```
1:functionDEFINE_AREA_G(unorganizedPointsData, minDistances, currentClusterPoint)
2: Determine minimum and maximum x and y coordinates of the points
3: Compute the average of minimum distances between the points
4: Define a radius \(r\), as in Equations (8)-(10)
5: Create a logical mesh grid representing the area
6: Create a logical circle in the image with center at the current point and radius \(r\)
7: Determine all points that lie within the circle
8: Return indices of these points
9:endfunction
```  
**Algorithm A2** Define area \(G\). ```
1:functionKalman_filter_Adaptive(unorganizedPointsData, currentClusterPoint, startingClusterPoint, previousStatePrediction, previousUncertaintyPrediction, residualHistory)
2: Define system parameters and matrices (according to Equations (14), (15), (21), and (22))
3:if Only two initial cluster points are chosen then
4: Extract starting and current position
5: Set initial state prediction based on positions
6: Define initial state uncertainty
7: Predict next state and its uncertainty
8: Initialize history of residuals
9:return Predicted state, its uncertainty, and residuals history
10:else
11: Get current measurement from data
12: Update state estimation using previous state and measurement
13: Predict next state
14: Update history of measurement residuals
15: Adjust prediction based on historical residuals (adaptive factor)
16: Predict uncertainty of the next state using the adaptive factor
17:return Predicted state, its uncertainty, and updated residuals history
18:endif
19:endfunction
```  
**Algorithm A3** Adaptive Kalman filter. ```
1:functionCheckUnusedPoints(definedClusters)
2: Initialize the counter of small clusters (three points or less)
3:if over more than half of the clusters are small then
4:return True \(\triangleright\) Recommend repeating with a larger area
5:else
6: Eliminate clusters consisting of 1 or 2 points. 7:return False
8:endif
9:endfunction
```  
**Algorithm A4** Evaluations from Section 4.4. ```
1:functionFormFinalContour(definedClusters)
2: Sort clusters by size
3:if there is only one cluster then
4: Close the cluster, in order to get a closed contour.