# Three-Dimensional (3D) Model-Based Lower Limb Stump Automatic Orientation
## Materials and Methods  
The anonymous experimental data for the study were obtained from the Federal Scientific Center of Rehabilitation of the Disabled named after [PERSON]. The study was performed in accordance with the ethical standards presented in the Declaration of Helsinki. The study protocol was reviewed and approved by the local expert collegiate council before the beginning of the study. All patients provided their written informed consent prior to their inclusion in the study. The study focused on the patients with unilateral, bilateral and multiple amputation defects with various causes of amputation defects, with one lower limb having one defect at the lower leg level. The following patients were excluded from enrollment in the study: those with skin defects such as non-healing wounds; those with trophic ulcers; those exhibiting tremor of the extremities due to various diseases; those with protrusion of bone sawdust under the skin that prevents prosthetics; those suffering from either acute myocardial infarction or acute cerebrovascular accident within 4 months prior to the study; those exhibiting mental illness in the acute stage; those with contagious infectious diseases; and those with complication of the main and/or concomitant disease with the appointment of bed rest. Among seven patients selected for detailed investigation, four were male and three female, aged between 45 and 58 years old. Mathematical analysis and functional programming tools (Python 3.3.7 with Mesh library, www.python.org) were used for visual data processing, model design and computer simulations. The loop optimization method was used to select the points with maximum displacement [24]. Multidimensional regression method was used for 3D-model cross-section fitting [25]. For 3D-model orientation, quaternions [26] and rotation matrices have been used [27]. The developed algorithm (see Algorithm 1) performs an automatic orientation of the 3D-model. The key steps of the algorithm are as follows:  
```
1. Data import (e.g., reading from file)
2. Selection of points with maximum coordinates (loop optimization method)
3. Primary 3D-model spatial orientation
4. Setting cross-section under control by the operator
5. Arrangement of the points defining the cross-section
6. Finding cross-section central points
7. Calculation of the virtual anatomical axis
8. Secondary 3D-model spatial orientation
9. Data export (e.g., saving to file)
```  
**Algorithm 1**  
In the first step a digital representation obtained by a 3D scanner is imported as an array of coordinates containing the points \(\mathbf{r}_{i}=(x_{i},y_{i},z_{i})\). Once imported, all entries are sorted according to their coordinates along the Z axis. Figure 1 illustrates the visual representation of the input model, where x, y, z are Cartesian axes, while a is the virtual anatomical axis. The spatial orientation of the model is determined by the angles between the projections of the anatomical axis a on the xy, xz and zy planes. Next, the points \(\mathbf{r}_{i}=(x_{i},y_{i},z_{i})\) characterized by the maximum coordinates (step 2 of the algorithm) have been selected as:  
\[\overset{\rightarrow}{r_{max}}=\underset{z}{\max}\overset{\rightarrow}{r}_{i} \tag{1}\]  
\[\overset{\rightarrow}{r_{min}}=\underset{z}{\min}\overset{\rightarrow}{r}_{i} \tag{2}\]  
To enable the rotation of the digital 3D model, we employed a set of transformations following the rotation matrices approach. In 3D space rotation around the Z-axis is described by the matrix transformation. \[\text{R}_{z}\ (\theta)=\left( \begin{array}{ccc}\cos(\theta)&\sin(\theta)&0\\ -\sin(\theta)&\cos(\theta)&0\\ 0&0&1\end{array} \right) \tag{3}\]Similarly, rotation around the X-axis has the form:  
\[\mathrm{R}_{x}\ (\varphi)=\left( \begin{array}{ccc}1&0&0\\ 0&\cos(\varphi)&\sin(\varphi)\\ 0&-\sin(\varphi)&\cos(\varphi)\end{array} \right) \tag{4}\]  
Rotation around the Y-axis is given by:  
\[\mathrm{R}_{y}\ (\omega)=\left( \begin{array}{ccc}\cos(\omega)&0&-\sin(\omega)\\ 0&1&0\\ \sin(\omega)&0&\cos(\omega)\end{array}