# Least-Squares Estimation of Tangent Space for a Triangular Mesh
## 3 Method
### Solution Method for Constrained Normal Vectors  
Certain triangular meshes are equipped with vertex-normal vectors and texture coordinates. However, computation of tangent space is still necessary in these cases. When the normal vector, \(\mathbf{n}_{i}\), is provided, the third column of the optimal rotation matrix, \(\mathbf{R}_{i}\), should be fixed to \(\mathbf{n}_{i}\). Directly solving this constrained optimization can be challenging. Instead, we can rotate the relative positions \((\mathbf{p}_{j}-\mathbf{p}_{i})\) for all \(j\in\mathcal{N}_{i}\) such that \(\mathbf{n}_{i}\) aligns with the \(w\)-axis. This approach simplifies the task of finding the optimal 3D rotation to determine the optimal 2D rotation about the \(w\)-axis. After this rotation, \((\mathbf{p}_{j}-\mathbf{p}_{i})\) can be projected onto the \(w\)-plane, as their distances along the \(w\)-axis remain unchanged. Consequently, the problem is reduced to finding the optimal 2D rotation between two sets of vertex positions on the texture plane.  
Alternatively, we can directly deal with the projection of \((\mathbf{p}_{j}-\mathbf{p}_{i})\) onto the plane perpendicular to \(\mathbf{n}_{i}\) in world space, as depicted in Figure 4. This involves calculating the vector rejection of \((\mathbf{p}_{j}-\mathbf{p}_{i})\) from \(\mathbf{n}_{i}\). The vector rejection of \(\mathbf{a}\) from \(\mathbf{b}\) is defined as \(\mathbf{a}_{\perp\mathbf{b}}=(\mathbf{a}-\mathbf{a}_{\parallel\mathbf{b}})\), where \(\mathbf{a}_{\parallel\mathbf{b}}\) is the vector projection of \(\mathbf{a}\) onto \(\mathbf{b}\), calculated as \((\mathbf{a}\cdot\frac{\mathbf{b}}{\|\mathbf{b}\|})\frac{\mathbf{b}}{\|\mathbf{ b}\|}\). The problem is then reduced to finding the optimal rotation between the two sets of corresponding vectors \((\bar{\mathbf{p}}_{j}-\bar{\mathbf{p}}_{i})\) and \((\mathbf{p}_{j}-\mathbf{p}_{i})_{\perp\mathbf{n}_{i}}\) for all \(j\in\mathcal{N}_{i}\). This can be formulated as finding the rotation matrix, \(\mathbf{R}_{i}\), that minimizes  
\[\sum_{j\in\mathcal{N}_{i}}w_{ij}\|\mathbf{R}_{i}(\bar{\mathbf{p}}_{j}-\bar{ \mathbf{p}}_{i})-(\mathbf{p}_{j}-\mathbf{p}_{i})_{\perp\mathbf{n}_{i}}\|^{2}. \tag{9}\]Then, \(\mathbf{R}_{i}\) can be obtained with the polar decomposition of the covariance matrix:  
\[\mathbf{A}_{i}=\sum_{j\in\mathcal{N}_{i}}w_{ij}(\mathbf{p}_{j}-\mathbf{p}_{i})_{ \perp\mathbf{n}_{i}}(\bar{\mathbf{p}}_{j}-\bar{\mathbf{p}}_{i})^{\mathrm{T}}= \mathbf{R}_{i}\mathbf{S}_{i}. \tag{10}\]  
Here, the third column of \(\mathbf{R}_{i}\) aligns with \(\mathbf{n}_{i}\) because any out-of-plane rotation increases the distances between the two sets of vectors. This method is preferable to the first method, as it is consistent with the approach for unconstrained normal vectors, with the addition of vector rejection.  
By utilizing \((\mathbf{p}_{j}-\mathbf{p}_{i})_{\perp\mathbf{n}_{i}}=(\mathbf{I}-\mathbf{n}_ {i}\mathbf{n}_{i}^{\mathrm{T}})(\mathbf{p}_{j}-\mathbf{p}_{i})\), Equation (10) can be further reformulated as follows:  
\[\mathbf{A}_{i}=(\mathbf{I}-\mathbf{n}_{i}\mathbf{n}_{i}^{\mathrm{T}})\sum_{j \in\mathcal{N}_{i}}w_{ij}(\mathbf{p}_{j}-\mathbf{p}_{i})(\bar{\mathbf{p}}_{j }-\bar{\mathbf{p}}_{i})^{\mathrm{T}}=\mathbf{R}_{i}\mathbf{S}_{i}. \tag{11}\]  
This equation is quite similar to Equation (8), but it involves projecting the covariance matrix itself onto the plane perpendicular to \(\mathbf{n}_{i}\) before decomposing it into the rotation matrix and the stretch matrix. It is worth noting that computing the tangent space using Equation (11) is slightly more computationally efficient compared to Equation (10).