File Exchange

## Distance between a point and a triangle in 3D

version 1.6.0.0 (2.99 KB) by
pointTriangleDistance calculates the distance between a point and a triangle in 3D.

Updated 14 Oct 2010

Calculate the distance of a given point P from a triangle TRI.
Point P is a row vector of the form 1x3. The triangle is a matrix
formed by three rows of points TRI = [P1;P2;P3] each of size 1x3.
dist = pointTriangleDistance(TRI,P) returns the distance of the point P to the triangle TRI.
[dist,PP0] = pointTriangleDistance(TRI,P) additionally returns the closest point PP0 to P on the triangle TRI.

### Cite As

Gwendolyn Fischer (2021). Distance between a point and a triangle in 3D (https://www.mathworks.com/matlabcentral/fileexchange/22857-distance-between-a-point-and-a-triangle-in-3d), MATLAB Central File Exchange. Retrieved .

MoHa

Hello,
Thank you for sharing the function. I have a question about it.
To analyze the deformation of a surface, for example, I would like to monitor this surface (facade of a building) every 2 hours. So every 2 hours I get a new point cloud which I have to compare with a reference area.my meshed surface and each point cloud consists of several points (about 400000 points), which have only x-, y- and z-coordinates. How can I use this function to represent the distance between the surface and the point cloud as a colored figure?

may i know how to run the code?

Sathish Thiyagarajan

Great function! I've found that using a different version of the dot product i.e. sum(E0.*E0) vs dot(E0,E0) speeds up execution considerably in Matlab 2016b.

AF

Gordon

Very nice, and faster than my implementation. However, there's an error somewhere in there. Try plugging the following triangle and point in:

tri=[-0.351987256452018,-2.20321765626083,2.02537449847930;-1.63376366769586,1.51438594345573,0.862041724660671;-0.688413485634898,-1.58953522933881,-1.14541245917166];
p=[1.38048000996529,3.61393939643154,3.46074586955261];

[tridist,tripoint]=pointTriangleDistance(tri,p);
tridist
norm(p-tripoint)

output:
tridist =
6.1630
ans =
4.4997

But tridist should be the same as the norm from the initial point to the closest point (output from the function as PP0). Plotting it, it looks like PP0 is getting to the right spot, so there must be an issue with that particular path through the code. Notice that if you truncate the values I gave above, the correct answer is reached (i.e. if you only use 4 places past the decimal).

Using R2010a

jixian

good job,thank you, i learn a lot.

Thomas O'Shea

creek

Good work.

##### MATLAB Release Compatibility
Created with R14SP2
Compatible with any release
##### Platform Compatibility
Windows macOS Linux