Next Article in Journal
Distributed Combinatorial Maps for Parallel Mesh Processing
Previous Article in Journal
Solutions to the Sub-Optimality and Stability Issues of Recursive Pole and Zero Distribution Algorithms for the Approximation of Fractional Order Models
 
 
Font Type:
Arial Georgia Verdana
Font Size:
Aa Aa Aa
Line Spacing:
Column Width:
Background:
Article

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

Faculty of Engineering and Applied Science, Memorial University of Newfoundland, 40 Arctic Ave, St. John’s, NL A1B 3X7, Canada
*
Author to whom correspondence should be addressed.
Algorithms 2018, 11(7), 104; https://doi.org/10.3390/a11070104
Submission received: 5 June 2018 / Revised: 2 July 2018 / Accepted: 10 July 2018 / Published: 12 July 2018

Abstract

:
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.

1. 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.

2. 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 :
p c = ζ 1 p 1 + ζ 2 p 2 + ζ 3 p 3 .
The squared distance between the point and the triangle is
s = p 0 - p c 2 = k = 0 2 x k - m = 1 3 ζ m x k + 3 m 2 .
To obtain the gradient of s, Expression (2) is differentiated:
s x i = 2 k = 0 2 x k - m = 1 3 ζ m x k + 3 m δ ( k ) ( i ) - m = 1 3 ζ m δ ( k + 3 m ) ( i ) + x k + 3 m ζ m x i ,
where δ i j denotes the Kronecker symbol. Expressions for the barycentric coordinates ζ m are given by Eberly [1], who introduces the following scalar coefficients:
a = e 0 · e 0 , b = e 0 · e 1 , c = e 1 · e 1 , d = e 0 · v , e = e 1 · v ,
where e 0 = p 2 - p 1 , e 1 = p 3 - p 1 and v = p 1 - p 0 . The barycentric coordinates are then
ζ 1 = 1 - ( ζ 2 + ζ 3 ) ; ζ 2 = b e - c d a c - b 2 ; ζ 3 = b d - a e a c - b 2 .
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:
ζ 2 x i = 1 Δ e b x i + b e x i - d c x i - c d x i - 1 Δ 2 Δ x i b e - c d ,
ζ 3 x i = 1 Δ d b x i + b d x i - e a x i - a e x i - 1 Δ 2 Δ x i b d - a e ,
where Δ = a c - b 2 and Δ x i = a c x i + c a x i - 2 b b x i . Derivatives of ζ 1 are
ζ 1 x i = - ζ 2 x i + ζ 3 x i .
Coefficients a , b , c , d , e (4) can be expanded in terms of x i :
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 ) .
Then, gradients of Equation (9) are
a x i = 2 × [ 0 , 0 , 0 , x 3 - x 6 , x 4 - x 7 , x 5 - x 8 , x 6 - x 3 , x 7 - x 4 , x 8 - x 5 , 0 , 0 , 0 ] , b x i = [ 0 , 0 , 0 , 2 x 3 - x 6 - x 9 , 2 x 4 - x 7 - x 10 , 2 x 5 - x 8 - x 11 , x 9 - x 3 , x 10 - x 4 , x 11 - x 5 , x 6 - x 3 , x 7 - x 4 , x 8 - x 5 ] , c x i = 2 × [ 0 , 0 , 0 , x 3 - x 9 , x 4 - x 10 , x 5 - x 11 , 0 , 0 , 0 , x 9 - x 3 , x 10 - x 4 , x 11 - x 5 ] , d x i = [ x 3 - x 6 , x 4 - x 7 , x 5 - x 8 , x 0 - 2 x 3 + x 6 , x 1 - 2 x 4 + x 7 , x 2 - 2 x 5 + x 8 , x 3 - x 0 , x 4 - x 1 , x 5 - x 2 , 0 , 0 , 0 ] , e x i = [ x 3 - x 9 , x 4 - x 10 , x 5 - x 11 , x 0 - 2 x 3 + x 9 , x 1 - 2 x 4 + x 10 , x 2 - 2 x 5 + x 11 , 0 , 0 , 0 , x 3 - x 0 , x 4 - x 1 , x 5 - x 2 ] .
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
2 s x i x j = 2 k = 0 2 ( ξ ) 2 + 2 ξ ξ ,
where
ξ = x k - m = 1 3 ζ m x k + 3 m ,
ξ = δ ( k ) ( i ) - m = 1 3 ζ m δ ( k + 3 m ) ( i ) + x k + 3 m ζ m x i ,
ξ = - m = 1 3 ζ m x j δ ( k + 3 m ) ( i ) + ζ m x i δ ( k + 3 m ) ( j ) + x k + 3 m 2 ζ m x i x j .
Second derivatives of barycentric coordinates are obtained by differentiating Expressions (6) and (7):
2 ζ 2 x i x j = 1 Δ b x j e x i + b x i e x j - c x j d x i - c x i d x j + 1 Δ e 2 b x j x i + b 2 e x j x i - d 2 c x j x i - c 2 d x j x i + 1 Δ 2 Δ x j d c x i + c d x i - e b x i - b e x i + 1 Δ 2 Δ x i d c x j + c d x j - e b x j - b e x j + 2 Δ 3 Δ x i Δ x j - 1 Δ 2 2 Δ x i x j b e - c d ,
2 ζ 3 x i x j = 1 Δ b x j d x i + b x i d x j - a x j e x i - a x i e x j + 1 Δ d 2 b x j x i + b 2 d x j x i - e 2 a x j x i - a 2 e x j x i + 1 Δ 2 Δ x j e a x i + a e x i - d b x i - b d x i + 1 Δ 2 Δ x i e a x j + a e x j - d b x j - b d x j + 2 Δ 3 Δ x i Δ x j - 1 Δ 2 2 Δ x i x j b d - a e ,
where 2 Δ x i x j = a x j c x i + a x i c x j + c 2 a x i x j + a 2 c x i x j - 2 b x i b x j - 2 b 2 b x i x j . Similarly to Equation (8):
2 ζ 1 x i x j = - 2 ζ 2 x i x j + 2 ζ 3 x i x j .
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:
f x i = 1 2 s s x i ,
2 f x i x j = 1 2 s 2 s x i x j - 1 4 s 3 / 2 s x i s x j .

3. 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
s = p 0 - p c 2 ,
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:
p c = ( 1 - ζ k ) p m + ζ k p n ,
where ζ k is the non-zero barycentric coordinate of p c . This coordinate can be found as [5]:
ζ k = ( p m - p 0 ) · ( p n - p m ) p n - p m 2 .
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).

4. 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 , s p x i , 2 s p x i x j 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 = ( s c - s p ) / s p ,
E 1 = max i s c x i - s p x i / s p x i ,
E 2 = max i , j 2 s c x i x j - 2 s p x i x j / 2 s p x i x j .
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 m a x is the maximum error for s, E 1 m a x is the maximum error for s c x i and E 2 m a x is the maximum error for 2 s c x i x j . E 2 m a x is higher than E 0 m a x 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.
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.

5. 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.

Author Contributions

Conceptualization, I.G.; Software, I.G.; Writing—Original Draft Preparation, I.G.; Writing—Review and Editing, R.T. and R.S.; Supervision, R.T.

Funding

This research was funded by the Natural Sciences and Engineering Research Council (NSERC) of Canada and the Research Development Corporation of Newfoundland and Labrador (RDC).

Acknowledgments

The authors thank C-CORE for providing computing resources, office space, and creating productive working environment.

Conflicts of Interest

The authors declare no conflict of interest.

Appendix A. Second Derivatives of Coefficients a,b,c,d,e

2 a x i x j = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 - 2 0 0 0 0 0 0 0 0 0 2 0 0 - 2 0 0 0 0 0 0 0 0 0 2 0 0 - 2 0 0 0 0 0 0 - 2 0 0 2 0 0 0 0 0 0 0 0 0 - 2 0 0 2 0 0 0 0 0 0 0 0 0 - 2 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ,
2 b x i x j = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 - 1 0 0 - 1 0 0 0 0 0 0 2 0 0 - 1 0 0 - 1 0 0 0 0 0 0 2 0 0 - 1 0 0 - 1 0 0 0 - 1 0 0 0 0 0 1 0 0 0 0 0 0 - 1 0 0 0 0 0 1 0 0 0 0 0 0 - 1 0 0 0 0 0 1 0 0 0 - 1 0 0 1 0 0 0 0 0 0 0 0 0 - 1 0 0 1 0 0 0 0 0 0 0 0 0 - 1 0 0 1 0 0 0 ,
2 c x i x j = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 - 2 0 0 0 0 0 0 2 0 0 0 0 0 - 2 0 0 0 0 0 0 2 0 0 0 0 0 - 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 - 2 0 0 0 0 0 2 0 0 0 0 0 0 - 2 0 0 0 0 0 2 0 0 0 0 0 0 - 2 0 0 0 0 0 2 ,
2 d x i x j = 0 0 0 1 0 0 - 1 0 0 0 0 0 0 0 0 0 1 0 0 - 1 0 0 0 0 0 0 0 0 0 1 0 0 - 1 0 0 0 1 0 0 - 2 0 0 1 0 0 0 0 0 0 1 0 0 - 2 0 0 1 0 0 0 0 0 0 1 0 0 - 2 0 0 1 0 0 0 - 1 0 0 1 0 0 0 0 0 0 0 0 0 - 1 0 0 1 0 0 0 0 0 0 0 0 0 - 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ,
2 e x i x j = 0 0 0 1 0 0 0 0 0 - 1 0 0 0 0 0 0 1 0 0 0 0 0 - 1 0 0 0 0 0 0 1 0 0 0 0 0 - 1 1 0 0 - 2 0 0 0 0 0 1 0 0 0 1 0 0 - 2 0 0 0 0 0 1 0 0 0 1 0 0 - 2 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 - 1 0 0 1 0 0 0 0 0 0 0 0 0 - 1 0 0 1 0 0 0 0 0 0 0 0 0 - 1 0 0 1 0 0 0 0 0 0 .

References

  1. Eberly, D. Distance between Point and Triangle in 3D. 1999. Available online: https://www.geometrictools.com/Documentation/DistancePoint3Triangle3.pdf.pdf (accessed on 9 July 2018).
  2. Laursen, T.A. Computational Contact and Impact Mechanics: Fundamentals of Modeling Interfacial Phenomena in Nonlinear Finite Element Analysis; Springer Science & Business Media: Heidelberg/Berlin, Germany, 2013. [Google Scholar]
  3. Pfaff, T.; Narain, R.; De Joya, J.M.; O’Brien, J.F. Adaptive tearing and cracking of thin sheets. ACM Trans. Graph. 2014, 33, 110. [Google Scholar] [CrossRef]
  4. Fisher, S.; Lin, M.C. Fast penetration depth estimation for elastic bodies using deformed distance fields. In Proceedings of the Intelligent Robots and Systems, Maui, HI, USA, 29 October–3 November 2001; Volume 1, pp. 330–336. [Google Scholar]
  5. Weisstein, E.W. Point-Line Distance–3-Dimensional. MathWorld–A Wolfram Web Resource. Available online: http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html (accessed on 10 July 2018).
  6. Gribanov, I. Distance Derivatives, GitHub Repository. Available online: https://github.com/Spear520/dist/ (accessed on 10 July 2018).
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.
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.
Algorithms 11 00104 g001
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 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.
Algorithms 11 00104 g002
Figure 3. The closest point p c may coincide with (a) one of triangle’s vertices or (b) belong to one of the edges.
Figure 3. The closest point p c may coincide with (a) one of triangle’s vertices or (b) belong to one of the edges.
Algorithms 11 00104 g003
Figure 4. Simulation of falling and colliding grains performed with implicit finite element method.
Figure 4. Simulation of falling and colliding grains performed with implicit finite element method.
Algorithms 11 00104 g004
Table 1. Relative errors for squared distance and its first and second derivatives. The maximum values from 10 million test cases are shown.
Table 1. Relative errors for squared distance and its first and second derivatives. The maximum values from 10 million test cases are shown.
Error MeasureValue
E 0 m a x 3.78 × 10 - 5
E 1 m a x 2.75 × 10 - 5
E 2 m a x 1.27 × 10 - 3

Share and Cite

MDPI and ACS Style

Gribanov, I.; Taylor, R.; Sarracino, R. The Gradient and the Hessian of the Distance between Point and Triangle in 3D. Algorithms 2018, 11, 104. https://doi.org/10.3390/a11070104

AMA Style

Gribanov I, Taylor R, Sarracino R. The Gradient and the Hessian of the Distance between Point and Triangle in 3D. Algorithms. 2018; 11(7):104. https://doi.org/10.3390/a11070104

Chicago/Turabian Style

Gribanov, Igor, Rocky Taylor, and Robert Sarracino. 2018. "The Gradient and the Hessian of the Distance between Point and Triangle in 3D" Algorithms 11, no. 7: 104. https://doi.org/10.3390/a11070104

Note that from the first issue of 2016, this journal uses article numbers instead of page numbers. See further details here.

Article Metrics

Back to TopTop