# The Gradient and the Hessian of the Distance between Point and Triangle in 3D
## 2 Mathematical Formulation  
Figure 1 shows the point \(\mathbf{p}_{0}(x_{0},x_{1},x_{2})\) and the triangle with vertices \(\mathbf{p}_{1}(x_{3},x_{4},x_{5})\), \(\mathbf{p}_{2}(x_{6},x_{7},x_{8})\) and \(\mathbf{p}_{3}(x_{9},x_{10},x_{11})\). The projection point \(\mathbf{p}_{c}\) has the barycentric coordinates \(\zeta_{1},\zeta_{2},\zeta_{3}\):  
\[\mathbf{p}_{c}=\zeta_{1}\mathbf{p}_{1}+\zeta_{2}\mathbf{p}_{2}+\zeta_{3} \mathbf{p}_{3}. \tag{1}\]  
The squared distance between the point and the triangle is  
\[s=|\mathbf{p}_{0}-\mathbf{p}_{c}|^{2}=\sum_{k=0}^{2}\left(x_{k}-\sum_{m=1}^{3 }\zeta_{m}x_{k+3m}\right)^{2}. \tag{2}\]  
To obtain the gradient of \(s\), Expression (2) is differentiated:  
\[\frac{\partial s}{\partial x_{i}}=2\sum_{k=0}^{2}\left(x_{k}-\sum_{m=1}^{3} \zeta_{m}x_{k+3m}\right)\left(\delta_{(k)(i)}-\sum_{m=1}^{3}\left(\zeta_{m} \delta_{(k+3m)(i)}+x_{k+3m}\frac{\partial\zeta_{m}}{\partial x_{i}}\right) \right), \tag{3}\]  
where \(\delta_{ij}\) denotes the Kronecker symbol. Expressions for the barycentric coordinates \(\zeta_{m}\) are given by [PERSON] [1], who introduces the following scalar coefficients:  
\[a=\mathbf{e}_{0}\cdot\mathbf{e}_{0},b=\mathbf{e}_{0}\cdot\mathbf{e}_{1},c= \mathbf{e}_{1}\cdot\mathbf{e}_{1},d=\mathbf{e}_{0}\cdot\mathbf{v},e=\mathbf{e }_{1}\cdot\mathbf{v}, \tag{4}\]  
where \(\mathbf{e}_{0}=\mathbf{p}_{2}-\mathbf{p}_{1}\), \(\mathbf{e}_{1}=\mathbf{p}_{3}-\mathbf{p}_{1}\) and \(\mathbf{v}=\mathbf{p}_{1}-\mathbf{p}_{0}\). The barycentric coordinates are then  
\[\zeta_{1}=1-(\zeta_{2}+\zeta_{3});\zeta_{2}=\frac{be-cd}{ac-b^{2}};\zeta_{3}= \frac{bd-ae}{ac-b^{2}}. \tag{5}\]  
Expression (5) is used when \(\mathbf{p}_{c}\) belongs to the interior region of the triangle. Cases when \(\mathbf{p}_{c}\) belongs to an edge or coincides with one of the vertices are discussed in the next section. To obtain derivatives of \(\zeta_{2}\) and \(\zeta_{3}\), Expression (5) is differentiated:  
\[\frac{\partial\zeta_{2}}{\partial x_{i}}=\frac{1}{\Delta}\left(e\frac{ \partial b}{\partial x_{i}}+b\frac{\partial e}{\partial x_{i}}-d\frac{ \partial c}{\partial x_{i}}-c\frac{\partial d}{\partial x_{i}}\right)-\frac{1 }{\Delta^{2}}\frac{\partial\Delta}{\partial x_{i}}\left(be-cd\right), \tag{6}\]  
\[\frac{\partial\zeta_{3}}{\partial x_{i}}=\frac{1}{\Delta}\left(d\frac{ \partial b}{\partial x_{i}}+b\frac{\partial d}{\partial x_{i}}-e\frac{ \partial a}{\partial x_{i}}-a\frac{\partial e}{\partial x_{i}}\right)-\frac{1 }{\Delta^{2}}\frac{\partial\Delta}{\partial x_{i}}\left(bd-ae\right), \tag{7}\]  
Figure 1: Distance \(f\) is determined between the point \(\mathbf{p}_{0}\) and its projection \(\mathbf{p}_{c}\) onto the triangle \(\mathbf{p}_{1}\mathbf{p}_{2}\mathbf{p}_{3}\). Vectors \(\mathbf{e}_{0}\), \(\mathbf{e}_{1}\) and \(\mathbf{v}\) are used in calculating \(f\). where \(\Delta=ac-b^{2}\) and \(\frac{\partial\Delta}{\partial x_{i}}=a\frac{\partial c}{\partial x_{i}}+c\frac{ \partial\alpha}{\partial x_{i}}-2b\frac{\partial b}{\partial x_{i}}\). Derivatives of \(\zeta_{1}\) are  
\[\frac{\partial\zeta_{1}}{\partial x_{i}}=-\left(\frac{\partial\zeta_{2}}{ \partial x_{i}}+\frac{\partial\zeta_{3}}{\partial x_{i}}\right). \tag{8}\]  
Coefficients \(a,b,c,d,e\) (4) can be expanded in terms of \(x_{i}\):  
\[ \begin{split} a&=(x_{6}-x_{3})^{2}+(x_{7}-x_{4})^{2 }+(x_{8}-x_{5})^{2},\\ b&=(x_{9}-x_{3})(x_{6}-x_{3})+(x_{10}-x_{4})(x_{7}-x_{4} )+(x_{11}-x_{5})(x_{8}-x_{5}),\\ c&=(x_{9}-x_{3})^{2}+(x_{10}-x_{4})^{2}+(x_{11}-x_{5} )^{2},\\ d&=(x_{3}-x_{0})(x_{6}-x_{3})+(x_{4}-x_{1})(x_{7}-x_{4} )+(x_{5}-x_{2})(x_{8}-x_{5}),\\ e&=(x_{9}-x_{3})(x_{3}-x_{0})+(x_{10}-x_{4})(x_{4} -x_{1})+(x_{11}-x_{5})(x_{5}-x_{2}).\end{split}  \tag{9}\]  
Then, gradients of Equation (9) are  
\[