# A Safe Admittance Boundary Algorithm for Rehabilitation Robot Based on Space Classification Model
## 2 Safe Admittance Boundary Algorithm
### Space Classification Model  
With the isodensification of the two point clouds done, this section deals with the construction of a space classification model for HRI based on nearest neighbor density in the point cloud. First, \(\mathrm{P_{i,FK1}}\) and \(\mathrm{P_{i,FK2}}\) are combined. The two sets of data points are represented in the same coordinate system based on the position and pose relationship between the human arm and the robot. The combined point cloud \(\mathrm{P_{i,combine}},\mathrm{i=1,\ 2,\ 3,\ \ldots,\mathrm{k_{combine}}}\), is obtained and \(\mathrm{k_{combine}}\) is the size of data of \(\mathrm{P_{i,combine}}\). Next, all points in \(\mathrm{P_{i,FK1}}\), \(\mathrm{P_{i,FK2}}\), and \(\mathrm{P_{i,combine}}\) are traversed. k-NN is used to search for the nearest neighbors of each point. The reciprocal of the Euclidean distance \(\mathrm{d_{p}}\) between each point and its nearest neighbor [32] is treated as the relative density \(\rho\) of the current point. Specifically, \(\mathrm{P_{i,FK1}}\), \(\mathrm{P_{i,FK2}}\), and \(\mathrm{P_{i,combine}}\) are input as the training data set T for k-NN:  
\[\mathrm{T}=\left\{\left(\mathrm{x_{1},y_{1}}\right),\left(\mathrm{x_{2},y_{2} }\right),\ldots,\left(\mathrm{x_{N},y_{N}}\right)\right\} \tag{4}\]  
where \(\mathrm{x_{i}}\in\mathrm{X}\subseteq\mathbb{R}^{3}\) is the eigenvector of the data set: that is, the sample point Q. \(\mathrm{y_{i}}\) is the class of the data set, \(\mathrm{i=1,\ 2,\ 3,\ldots,N}\); \(\mathrm{k=1}\) is set for this model: that is, 1-NN nearest neighbor search. Hence, \(\mathrm{y_{i}}\) indicates that the class of x's nearest neighbor is the class of x. Meanwhile, the training data set is stored using a k-d tree for fast nearest neighbor search. Any point p in the point cloud to be calculated is both the current point and the target point. q is any point other than p in the training data set T (as shown in Figure 4a). The Euclidean distance \(\mathrm{dis(p,q)}\) is used to represent the distance between points p and q:  
\[\mathrm{dis(p,q)}=\sqrt[2]{\left(\mathrm{x_{p}-x_{q}}\right)^{2}+\left( \mathrm{y_{p}-y_{q}}\right)^{2}+\left(\mathrm{z_{p}-z_{q}}\right)^{2}} \tag{5}\]  
Searching for the leaf node containing the target point p in the k-d tree involves the following steps. Start from the root node and recursively go down the k-d tree. If the coordinates of the target point in the current dimension are smaller than that of the splitting point, move to the left subnode; otherwise, move to the right subnode. Repeat the above process until the leaf node is reached. At this point, the leaf node is considered to be the current nearest neighbor. Next, recursively move up the k-d tree. At each node, compare the Euclidean distance between the sample point stored in the data set and the target point and that between the current nearest neighbor and the target point. In the meantime, check whether there is a closer point near the other subnode with the same parent as the current subnode. Update the current nearest neighbor. After returning to the root node, the last current nearest neighbor is the real nearest neighbor of the target point p in the training data set T.  
Figure 4: (**a**) Euclidean distance in the nearest neighbor search; (**b**) point cloud visualization based on nearest neighbor density. Let \(d_{p}\) be the minimum Euclidean distance between point \(p\) and other points in the point cloud (as shown in Figure 4a). The relative density \(\rho\) of the current point is given thus:  
\[\rho=\frac{1}{d_{p}}=\frac{1}{min(dis(p,q))},q=1,\;2,\;3,\ldots,N,p\
eq q \tag{6}\]  
In the point cloud with uniform point distribution, \(d_{p}\) is a fixed value. The relative density \(\rho\) of any point in the point cloud can be represented by a constant \(\frac{1}{d_{p}}\); the larger the value of \(d_{p}\), the smaller the relative density \(\rho\) of the point cloud, and the smaller the value of \(d_{p}\), the larger the relative density \(\rho\) of the point cloud.