Distance Between Points and a Line Segment in 3D

24 次查看(过去 30 天)
Hi
I have an mx3 array of point cloud data (X,Y,Z) and a 3D line. The line is defined by a start point (1x3) and an end point (1x3).
I'd like to find the orthogonal distance between each point and the line, but the line must only extend between the start and end point (i.e. not between +/- infinity).
Any solutions to this problem would be greatly appreciated.
Many thanks!
Jack

采纳的回答

Rik
Rik 2017-9-12
This problem can be tackled by separating the points in 3 parts. Let's define some distances for clarity: dsp is the distance between the start and the current point, dep is the distance between the end point and the current point, and dse is the distance between start and end.
  • type 1 is all points where sqrt(dse^2+dep^2)>=dsp
  • type 2 is all points where sqrt(dse^2+dsp^2)>=dep
  • type 3 is all other points
For type 1, the distance to the line segment is simply dep. For type 2, the distance to the line segment is simply dsp. (to see this, draw the line as one side of a right-sided triangle and apply Pythagoras)
Type 3 is between start and end, so there we need the orthogonal distance. We can slightly modify a staff-provided answer:
function d = point_to_line(pt, v1, v2)
% pt should be nx3
% v1 and v2 are vertices on the line (each 1x3)
% d is a nx1 vector with the orthogonal distances
v1 = repmat(v1,size(pt,1),1);
v2 = repmat(v2,size(pt,1),1);
a = v1 - v2;
b = pt - v2;
d = sqrt(sum(cross(a,b,2).^2,2)) ./ sqrt(sum(a.^2,2));
You can easily separate these three cases with logical indexing.
  4 个评论
Olivier Lartillot
Olivier Lartillot 2020-2-20
Great, thanks so much! But there seems to be a mistake as I noticed in my tests that the distance gets lower when the segment length gets bigger.
.. Gravitation effect? ;)

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by