]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/geom2d/distancePointEdge.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / geom2d / distancePointEdge.m
1 %% Copyright (c) 2007-2011, David Legland <david.legland@grignon.inra.fr>
2 %% Copyright (c) 2012, Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
3 %%
4 %% All rights reserved.
5 %% (simplified BSD License)
6 %%
7 %% Redistribution and use in source and binary forms, with or without
8 %% modification, are permitted provided that the following conditions are met:
9 %%
10 %% 1. Redistributions of source code must retain the above copyright notice, this
11 %%    list of conditions and the following disclaimer.
12 %%
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.
16 %%
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.
28 %%
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.
32
33 %% -*- texinfo -*-
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
38 %%
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.
43 %%
44 %% If @var{opt} is true (or equivalent), the optput is cmpatible with the original function:
45 %% @table @samp
46 %% @item 1
47 %% If @var{point} is 1-by-2 array, the result is Ne-by-1 array computed for each edge.
48 %% @item 2
49 %% If @var{edge} is a 1-by-4 array, the result is Np-by-1 computed for each point.
50 %% @item 3
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,:)}.
53 %% @end table
54 %%
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).
58 %%
59 %% @seealso{edges2d, points2d, distancePoints, distancePointLine}
60 %% @end deftypefn
61
62 %% Rewritten to accept different numbers of points and edges.
63 %% 2012 - Juan Pablo Carbajal
64
65 function [dist, tp] = distancePointEdge(point, edge, opt=[])
66
67   Np = size (point,1);
68   Ne = size (edge,1);
69   edge = edge.';
70
71   % direction vector of each edge
72   dx = edge(3,:) - edge(1,:);
73   dy = edge(4,:) - edge(2,:);
74
75   % compute position of points projected on the supporting line
76   % (Size of tp is the max number of edges or points)
77
78   delta = dx .* dx + dy .* dy;
79   mask = delta < eps;
80   delta(mask) = 1;
81   warning ('off', 'Octave:broadcast');
82   tp = ((point(:, 1) - edge(1, :)) .* dx + (point(:, 2) - edge(2, :)) .* dy) ./ delta;
83   tp(:,mask) = 0;
84
85   % change position to ensure projected point is located on the edge
86   tp(tp < 0) = 0;
87   tp(tp > 1) = 1;
88
89   % coordinates of projected point
90   p0x = (edge(1,:) + tp .* dx);
91   p0y = (edge(2,:) + tp .* dy);
92
93   % compute distance between point and its projection on the edge
94   dist = sqrt((point(:,1) - p0x) .^ 2 + (point(:,2) - p0y) .^ 2);
95
96   warning ('on', 'Octave:broadcast');
97
98   %% backwards compatibility
99   if opt
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.");
103     end
104     if Np == Ne
105       dist = diag(dist)(:);
106       tp = diag(tp)(:);
107     elseif Np == 1
108       dist = dist.';
109       tp = tp.';
110     end
111   end
112
113 endfunction
114
115 %% Original code
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)
119 %%if Ne > 1
120 %%  tp(delta < eps) = 0;
121 %%elseif delta < eps
122 %%  tp(:) = 0;
123 %%end
124
125 %%% change position to ensure projected point is located on the edge
126 %%tp(tp < 0) = 0;
127 %%tp(tp > 1) = 1;
128
129 %%% coordinates of projected point
130 %%p0 = [edge(:,1) + tp .* dx, edge(:,2) + tp .* dy];
131
132 %%% compute distance between point and its projection on the edge
133 %%dist = sqrt((point(:,1) - p0(:,1)) .^ 2 + (point(:,2) - p0(:,2)) .^ 2);