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 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 and the triangle with vertices , and . The projection point has the barycentric coordinates :
Figure 1.
Distance f is determined between the point and its projection onto the triangle . Vectors , and are used in calculating f.
The squared distance between the point and the triangle is
To obtain the gradient of s, Expression (2) is differentiated:
where denotes the Kronecker symbol. Expressions for the barycentric coordinates are given by Eberly [1], who introduces the following scalar coefficients:
where , and . The barycentric coordinates are then
Expression (5) is used when belongs to the interior region of the triangle. Cases when belongs to an edge or coincides with one of the vertices are discussed in the next section. To obtain derivatives of and , Expression (5) is differentiated:
where and . Derivatives of are
Coefficients (4) can be expanded in terms of :
Then, gradients of Equation (9) are
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 are constants that are included in Appendix A. The first and the second derivatives of the distance are then expressed as:
3. Point-Edge and Point-Point Cases
If the projection of falls outside of the interior area of the triangle (Figure 2), the closest point 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 is one of or . The derivatives of s with respect to are obtained trivially, and the details are not discussed here.
Figure 2.
Partitioning of the plane by the triangle domain. Different domains are distinguished by the values of barycentric coordinates of the projection of onto the plane of the triangle.
Figure 3.
The closest point may coincide with (a) one of triangle’s vertices or (b) belong to one of the edges.
In the point-edge case, one of the barycentric coordinates of is zero, which simplifies Expression (2). Let and be the vertices of the edge, to which the closest point belongs ( Figure 3b). Then is expressed as the linear combination:
where is the non-zero barycentric coordinate of . This coordinate can be found as [5]:
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 belongs (Figure 2). This step coincides with the original point-triangle algorithm [1], but subsequent calculations are extended to evaluate and . If 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 of which are random coordinates that come from the uniform distribution in the range . The remaining cases come from the finite element simulation of fracture, where the point 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 and . 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 and are compared to the results obtained in floating-point arithmetic . Relative errors are computed separately for the squared distance, its first derivatives and its second derivatives:
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 and . Ideally, the values of , and 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 , and cover a wide range of values. In about half of the cases, the precision for calculating second derivatives is better than . 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: is the maximum error for s, is the maximum error for and is the maximum error for . is higher than 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.
Table 1.
Relative errors for squared distance and its first and second derivatives. The maximum values from 10 million test cases are shown.
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.
Figure 4.
Simulation of falling and colliding grains performed with implicit finite element method.
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 , 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
References
- 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).
- 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]
- 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]
- 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]
- 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).
- Gribanov, I. Distance Derivatives, GitHub Repository. Available online: https://github.com/Spear520/dist/ (accessed on 10 July 2018).
© 2018 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (http://creativecommons.org/licenses/by/4.0/).