The Gradient and the Hessian of the Distance between Point and Triangle in 3 D

Computation of the distance between point and triangle in 3D is a common task in numerical analysis. The input values of the algorithm are coordinates of three points of the triangle and one point from which the distance is determined. An existing algorithm is extended to compute the gradient and the Hessian of that distance with respect to coordinates of involved points. Derivation of exact expressions for gradient and Hessian is presented, and numerical accuracy is evaluated for various cases. The algorithm has O(1) time and space complexity. The included open-source code may be used in applications where derivatives of point-triangle distance are required.


Introduction
The algorithm developed by Eberly [1] evaluates the distance function between a point and a triangle in 3D.The input parameters are coordinates of four points, which are involved in the point-triangle setup.Three points are vertices of the triangle, with the remaining point being the one to which the distance is computed.The motivation for this work is the need to obtain the gradient and the Hessian of that distance with respect to the input variables.Each of the four input points possesses three coordinates, giving 12 independent variables for the distance function.The distance function, accordingly, has 12 first derivatives and 12 × 12 second derivatives, the components of the symmetric Hessian matrix.
Our approach is to differentiate the distance function provided by Eberly [1] and extend that algorithm with evaluation of first and second derivatives.The characteristics of the algorithm remain the same, i.e., the time and space complexity of the algorithm is O (1).A linear array of first derivatives and a 2D array of second derivatives are added to the output.The main contribution of the authors is the derivation of the expressions for the gradient and the Hessian of the distance.A potential application of this algorithm is in finding penetration penalty forces and their differentials for colliding polygonal objects in finite element (FE) simulations.
Numerical simulations of mechanical systems often involve contact interactions, and the penalty method is a common way of addressing this problem [2].Implicit FE approaches rely on calculating derivatives of the forces generated in collisions, which in turn require first and second derivatives of the distance function.The FE simulation ARCSim [3] relies on penalty contact resolution for modeling deformation and fracture.ARCSim includes an approximation technique for obtaining the gradient and the Hessian of the distance function based on the surface normal of the interacting object.The approximation is not always accurate, which results in reduction of time steps, which sometimes halts the simulation.
In another method described by Fisher and Lin [4], the distance from the interior point to the surface of the object is precomputed on the nodes of interior polygonal mesh, representing a distance field.This discretized field allows one to estimate the spacial gradient of the distance.The accuracy is limited by the resolution of the mesh.In general, such approximation techniques are inaccurate, and there is a need for developing and utilizing the exact formula.

Mathematical Formulation
Figure 1 shows the point p 0 (x 0 , x 1 , x 2 ) and the triangle with vertices p 1 (x 3 , x 4 , x 5 ), p 2 (x 6 , x 7 , x 8 ) and p 3 (x 9 , x 10 , x 11 ).The projection point p c has the barycentric coordinates ζ 1 , ζ 2 , ζ 3 : ( The squared distance between the point and the triangle is Figure 1.Distance f is determined between the point p 0 and its projection p c onto the triangle p 1 p 2 p 3 .Vectors e 0 , e 1 and v are used in calculating f . To obtain the gradient of s, Expression (2) is differentiated: where δ ij denotes the Kronecker symbol.Expressions for the barycentric coordinates ζ m are given by Eberly [1], who introduces the following scalar coefficients: where e 0 = p 2 − p 1 , e 1 = p 3 − p 1 and v = p 1 − p 0 .The barycentric coordinates are then Expression ( 5) is used when p c belongs to the interior region of the triangle.Cases when p c belongs to an edge or coincides with one of the vertices are discussed in the next section.To obtain derivatives of ζ 2 and ζ 3 , Expression (5) is differentiated: where Coefficients a, b, c, d, e (4) can be expanded in terms of x i : Then, gradients of Equation ( 9) are Substituting Equation (10) into Equations ( 6) and (7) allows for obtaining ∂s ∂x i in terms of x i .The second derivatives of s are procured in the same manner.First, Expression (3) is differentiated to obtain where Second derivatives of barycentric coordinates are obtained by differentiating Expressions ( 6) and (7): where . Similarly to Equation (8): Second derivatives of the coefficients a, b, c, d, e are constants that are included in Appendix A. The first and the second derivatives of the distance f = √ s are then expressed as:

Point-Edge and Point-Point Cases
If the projection of p 0 falls outside of the interior area of the triangle (Figure 2), the closest point p c coincides with one of the vertices or belongs to one of the edges of the triangle.In the point-point case (Figure 3a), the squared distance is expressed as where p c is one of p 1 , p 2 or p 3 .The derivatives of s with respect to x i are obtained trivially, and the details are not discussed here.In the point-edge case, one of the barycentric coordinates of p c is zero, which simplifies Expression (2).Let p m and p n be the vertices of the edge, to which the closest point p c belongs ( Figure 3b).Then p c is expressed as the linear combination: where ζ k is the non-zero barycentric coordinate of p c .This coordinate can be found as [5]: Similarly to the previous derivations, Expression (20) can be differentiated to obtain the first and the second derivatives of the ζ k , which are then substituted into Expressions (3) and (11).

Algorithm and Testing
The calculations are implemented in double-precision floating-point arithmetic in C, and the tests are performed with squared distance s and its first and second derivatives.The first step is to determine the partition to which p c belongs (Figure 2).This step coincides with the original point-triangle algorithm [1], but subsequent calculations are extended to evaluate ∂s ∂x i and ∂ 2 s ∂x i ∂x j .If p c belongs to partitions 1, 3, 5, then the point-line algorithm is invoked.For partitions 2, 4, 6, point-point calculations are performed.For partition 0, the point-plane algorithm is used.
For testing, 10 million cases were generated, about 2/3 of which are random coordinates that come from the uniform distribution in the range [−1, 1].The remaining cases come from the finite element simulation of fracture, where the point p c is often close to one of the triangle's vertices.Most of the cases that come from the simulation have a low ratio of the distance to the shortest edge of the triangle, in the range between 10 −8 and 10 −5 .Such test cases result in a lower accuracy of the final answer than the random arrangements of points.
To evaluate the accuracy of the proposed algorithm, calculations are first performed using arbitrary precision arithmetic with at least 17 digits calculated precisely.These results are denoted s p , and are compared to the results obtained in floating-point arithmetic s c , ∂s c ∂x i , ∂ 2 s c ∂x i ∂x j .Relative errors are computed separately for the squared distance, its first derivatives and its second derivatives: E 0 is the relative error of the squared distance and is used as a baseline to compare with the relative errors of the first and second derivatives E 1 and E 2 .Ideally, the values of E 0 , E 1 and E 2 would have the same order of magnitude.However, due to the large number of algebraic operations, the accuracy of the calculation of the second derivative is lower.The values E 0 , E 1 and E 2 cover a wide range of values.In about half of the cases, the precision for calculating second derivatives is better than 10 −13 .However, the practical interest lies in investigating the worst cases because one incorrect calculation could affect a whole scientific study.

Discussion
The highest errors among all test cases are shown in Table 1.The values come from separate test cases: E 0 max is the maximum error for s, E 1 max is the maximum error for ∂s c ∂x i and E 2 max is the maximum error for ∂ 2 s c ∂x i ∂x j .E 2 max is higher than E 0 max by two orders of magnitude, which is a good result, considering that it is the least accurate of 10 million tests.The tests show that the precision is adequate for all cases, including the ones form the collision simulation.

Error Measure Value
Cases with the lowest accuracy usually correspond to the variables whose absolute values are very small, and computer simulations are often robust against such cases.For example, results of the grain interaction simulation where the current algorithm is applied are shown on Figure 4.The simulation advances with large time steps even when multiple fragments interact with each other.
The ratio between the distance and the largest edge of the triangle is one of the factors that affect the precision.When this ratio drops below 10 −10 , the accuracy of the result is likely to deteriorate.The problem of the round-off error is common in numerical analysis and should be addressed properly.If the influence of the round-off error is suspected when applying this algorithm, additional testing should be performed.In some cases, calculations can be performed with arbitrary-precision arithmetic to yield accurate results.

Conclusions
The presented algorithm has O(1) time and space complexity.Only static memory allocation takes place.The algorithm has several branches that evaluate algebraic expressions sequentially, with each branch completing in constant time.The main contribution of the authors is the derivation of the exact formulae for the gradient and the Hessian of the distance function.Additionally, testing of the reference implementation was performed to ensure that adequate precision is met.The proposed algorithm may be used in applications where point-triangle distance derivatives are required.The potential future work may include precision testing for more complex cases.The C code is available as open-source [6] and may be modified by the research community as needed.a, b, c, d

Figure 2 .Figure 3 .
Figure 2. Partitioning of the plane by the triangle domain.Different domains are distinguished by the values of barycentric coordinates of the projection of p 0 onto the plane of the triangle.

Figure 4 .
Figure 4. Simulation of falling and colliding grains performed with implicit finite element method.

Table 1 .
Relative errors for squared distance and its first and second derivatives.The maximum values from 10 million test cases are shown.