function [d,closest_pt] = NE_dist_pt_line(point,line)
% NE_dist_pt_line - distance between a point and a line
% Call: d = NE_dist_pt_line(point,line);
% On input:
%     point: 3D point
%     line:  2x3 matrix of 2 points
% On output:
%     d: Euclidean distance between point and line
% Author:
%     Tom Henderson
%     7 January 2000/modified for NE Summer 2023
% Custom Functions Used:
%     none
% Method:
%     find point on line so that distance to point is minimized
%

pt1 = line(1,:);
pt2 = line(2,:);
numer = (point(1)-pt1(1))*(pt2(1)-pt1(1)) ...
   + (point(2)-pt1(2))*(pt2(2)-pt1(2)) ...
   + (point(3)-pt1(3))*(pt2(3)-pt1(3));
denom = cv_dist_pt_pt(pt1,pt2)^2;
t = numer/denom;
closest_pt = pt1 + t*(pt2-pt1);
d = cv_dist_pt_pt(closest_pt,point);
