Code covered by the BSD License  

Highlights from
Distance between a point and a triangle in 3D

5.0

5.0 | 3 ratings Rate this file 14 Downloads (last 30 days) File Size: 2.99 KB File ID: #22857
image thumbnail

Distance between a point and a triangle in 3D

by Gwendolyn Fischer

 

02 Feb 2009 (Updated 14 Oct 2010)

pointTriangleDistance calculates the distance between a point and a triangle in 3D.

| Watch this File

File Information
Description

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.

MATLAB release MATLAB 7.0.4 (R14SP2)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
13 Aug 2009 creek

Good work.

27 Oct 2009 Thomas O'Shea  
31 Oct 2009 jixian

good job,thank you, i learn a lot.

12 Oct 2010 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

Please login to add a comment or rating.
Updates
13 Aug 2009

Fixed a Bug spotted by Jorge Riera

13 Oct 2010

Bug Fix because of typo. Thanks to Gordon.

14 Oct 2010

Bug Fix due to yet another typo.

Tag Activity for this File
Tag Applied By Date/Time
computational geometry Gwendolyn Fischer 02 Feb 2009 12:59:50
graphics Gwendolyn Fischer 02 Feb 2009 12:59:50
geometry Gwendolyn Fischer 02 Feb 2009 12:59:50
triangle Gwendolyn Fischer 02 Feb 2009 12:59:50
intersection allan nichoslee@hotmail.com lee 13 Aug 2009 02:39:33
3d residue error jixian 31 Oct 2009 04:05:52

Contact us at files@mathworks.com