## 2 Double B-Spline Curve-Fitting for Tool-Tip Position and Tool Orientation under Fitting Tolerance
### Tool-Tip Position Curve-Fitting
\tag{5}\]Since the knot vector \(\mathbf{U}\) has been decided and each tool-tip position point has been assigned a parameter value, system of linear equations is established:  
\[\mathbf{p}_{k}=\mathbf{C}(\bar{u}_{k})=\sum_{i=0}^{m}N_{i,3}(\bar{u}_{k}) \mathbf{P}_{i},\quad k=0,1,\ldots,m. \tag{6}\]  
The control points are acquired by solving system of linear equations shown in Equation (6) using the inverse B-spline basis function matrix. Therefore, the initial curve interpolating all the data points defined by Equation (1) is resolved, and the point error is satisfied. However, the initial curve might exceed chord error at certain curve segments. Assuming that the distance between the initial curve in the parameter interval \([\bar{u}_{k-1},\bar{u}_{k}]\) and the corresponding line segment \(\overline{\mathbf{p}_{k-1}\mathbf{p}_{k}}\) is greater than \(\varepsilon_{p}\), the local refinement process is expected to be limited in the parameter interval so that the remaining portion of the curve is not affected. In order to increase the flexibility of the local curve segment that needs to be refined, control points are added using knots insertion. Let \(u_{j}\) be the knot inserted to \([\bar{u}_{k-1},\bar{u}_{k}]\), the new control points can be calculated as:  
\[\left\{ \begin{array}{l}\mathbf{c}_{i}=\mathbf{P}_{i},\quad i=0,\ldots,k-d-1 \\ \mathbf{c}_{i}=(1-\alpha_{i})\mathbf{P}_{i-1}+\alpha_{i}\mathbf{P}_{i},\quad i =k-d,\ldots,k-1\\ \mathbf{c}_{i+1}=\mathbf{P}_{i},\quad i=k,\ldots,m,\end{array} \right. \tag{7}\]  
where \(\alpha_{i}={(u_{j}-u_{k-1})}/{(u_{k+d-1}-u_{k-1})}\). Denote \(\bar{u}_{k-1}\) and \(\bar{u}_{k}\) as \(u_{s}\) and \(u_{e}\) after knot insertion. Denote the distance between the control point \(\mathbf{P}_{i}(i=s-1,\ldots,e-p)\) and \(\overline{\mathbf{p}_{k-1}\mathbf{p}_{k}}\) as \(D_{i}\). \(D_{s-1}\) and \(D_{e-p}\) which must be not greater than \(\varepsilon_{p}\) due to strong convex hull property of B-spline curves [33], otherwise it is necessary to insert more knots to \([\bar{u}_{k-1},\bar{u}_{k}]\). To keep the portion outside \([\bar{u}_{k-1},\bar{u}_{k}]\) unchanged, we can only adjust the control points \(\mathbf{P}_{j}(j=s,\ldots,e-p-1)\). If \(D_{j}>\varepsilon_{p}\), replace \(\mathbf{P}_{j}\) with \(\mathbf{P}_{j}^{\prime}=\mathbf{P}_{j}+\alpha_{j}\mathbf{W}\) where \(\mathbf{W}\) is the unit vector that points from \(\mathbf{P}_{j}\) to \(\mathbf{P}_{j}^{\prime}\) and \(\alpha_{j}\) is the distance moved along \(\mathbf{W}\). Assign \(\mathbf{W}\) to the unit vector of the direction that is perpendicular to \(\overline{\mathbf{p}_{k-1}\mathbf{p}_{k}}\) and \(\alpha_{j}\) to the value of \(D_{j}-\varepsilon_{p}\). After all the local curve segments are refined, the final curve satisfies the fitting tolerance while maintaining C2-continuity. Flow of the proposed curve-fitting scheme is shown in Figure 2.  
Figure 2: Flowchart of the proposed curve-fitting scheme. We present a planar example with five points and fitting tolerance of 0.05 mm as a numerical illustration of the proposed curve-fitting scheme. The result of the B-spline curve fitted to five data points is shown in Figure 3. The initial curve that satisfies point error exceeds chord error bound at segment 1 and segment 2, which are zoomed in to get a clearer view. Then the two segments is locally refined to obtain the final error-constrained curve. The fitting error and curvature results are demonstrated in Figure 4. As can be seen, the fitting error of the traditional global curve-fitting method exceeds the fitting tolerance. The proposed curve-fitting method, as an improvement of the global curve-fitting method, can get the fitting error strictly under the fitting tolerance. Compare to the local corner-smoothing method, the proposed method has smaller curvature extrema under the same geometric continuity. Notice that the local corner-smoothing method has lower fitting error in most areas because only the corner parts are replaced.