1 %% Copyright (c) 2007-2011, David Legland <david.legland@grignon.inra.fr>
2 %% Copyright (c) 2012, Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
4 %% All rights reserved.
5 %% (simplified BSD License)
7 %% Redistribution and use in source and binary forms, with or without
8 %% modification, are permitted provided that the following conditions are met:
10 %% 1. Redistributions of source code must retain the above copyright notice, this
11 %% list of conditions and the following disclaimer.
13 %% 2. Redistributions in binary form must reproduce the above copyright notice,
14 %% this list of conditions and the following disclaimer in the documentation
15 %% and/or other materials provided with the distribution.
17 %% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 %% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 %% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 %% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 %% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 %% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 %% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 %% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 %% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 %% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 %% POSSIBILITY OF SUCH DAMAGE.
29 %% The views and conclusions contained in the software and documentation are
30 %% those of the authors and should not be interpreted as representing official
31 %% policies, either expressed or implied, of copyright holder.
34 %% @deftypefn {Function File} {@var{dist} = } distancePointEdge (@var{point}, @var{edge})
35 %% @deftypefnx {Function File} {@var{dist} = } distancePointEdge (@dots{}, @var{opt})
36 %% @deftypefnx {Function File} {[@var{dist} @var{pos}]= } distancePointEdge (@dots{})
37 %% Minimum distance between a point and an edge
39 %% Return the euclidean distance between edge @var{edge} and point @var{point}.
40 %% @var{edge} has the form: [x1 y1 x2 y2], and @var{point} is [x y].
41 %% If @var{edge} is Ne-by-4 and @var{point} is Np-by-2, then @var{dist} is
42 %% Np-by-Ne, where each row contains the distance of each point to all the edges.
44 %% If @var{opt} is true (or equivalent), the optput is cmpatible with the original function:
47 %% If @var{point} is 1-by-2 array, the result is Ne-by-1 array computed for each edge.
49 %% If @var{edge} is a 1-by-4 array, the result is Np-by-1 computed for each point.
51 %% If both @var{point} and @var{edge} are array, they must have the same number of
52 %% rows, and the result is computed for each couple @code{@var{point}(i,:),@var{edge}(i,:)}.
55 %% If the the second output argument @var{pos} is requested, the function also
56 %% returns the position of closest point on the edge. @var{pos} is comprised
57 %% between 0 (first point) and 1 (last point).
59 %% @seealso{edges2d, points2d, distancePoints, distancePointLine}
62 %% Rewritten to accept different numbers of points and edges.
63 %% 2012 - Juan Pablo Carbajal
65 function [dist, tp] = distancePointEdge(point, edge, opt=[])
71 % direction vector of each edge
72 dx = edge(3,:) - edge(1,:);
73 dy = edge(4,:) - edge(2,:);
75 % compute position of points projected on the supporting line
76 % (Size of tp is the max number of edges or points)
78 delta = dx .* dx + dy .* dy;
81 warning ('off', 'Octave:broadcast');
82 tp = ((point(:, 1) - edge(1, :)) .* dx + (point(:, 2) - edge(2, :)) .* dy) ./ delta;
85 % change position to ensure projected point is located on the edge
89 % coordinates of projected point
90 p0x = (edge(1,:) + tp .* dx);
91 p0y = (edge(2,:) + tp .* dy);
93 % compute distance between point and its projection on the edge
94 dist = sqrt((point(:,1) - p0x) .^ 2 + (point(:,2) - p0y) .^ 2);
96 warning ('on', 'Octave:broadcast');
98 %% backwards compatibility
100 if Np != Ne && (Ne != 1 && Np !=1)
101 error ("geometry:InvalidArgument", ...
102 "Sizes must be equal or one of the inputs must be 1-by-N array.");
105 dist = diag(dist)(:);
116 %%tp = ((point(:, 1) - edge(:, 1)) .* dx + (point(:, 2) - edge(:, 2)) .* dy) ./ delta;
117 %%% ensure degenerated edges are correclty processed (consider the first
118 %%% vertex is the closest)
120 %% tp(delta < eps) = 0;
125 %%% change position to ensure projected point is located on the edge
129 %%% coordinates of projected point
130 %%p0 = [edge(:,1) + tp .* dx, edge(:,2) + tp .* dy];
132 %%% compute distance between point and its projection on the edge
133 %%dist = sqrt((point(:,1) - p0(:,1)) .^ 2 + (point(:,2) - p0(:,2)) .^ 2);