changeset 10029:24e5d4b4e340 octave-forge

geometry: fixing bug in distancePointEdge when there were more points than edges. Now the function computes all against all distances.
author jpicarbajal
date Sat, 14 Apr 2012 16:03:10 +0000
parents 62aade508422
children 591ead50c1da
files main/geometry/inst/geom2d/distancePointEdge.m
diffstat 1 files changed, 78 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/main/geometry/inst/geom2d/distancePointEdge.m	Sat Apr 14 10:29:20 2012 +0000
+++ b/main/geometry/inst/geom2d/distancePointEdge.m	Sat Apr 14 16:03:10 2012 +0000
@@ -1,6 +1,5 @@
-%% Copyright (c) 2011, INRA
-%% 2007-2011, David Legland <david.legland@grignon.inra.fr>
-%% 2011 Adapted to Octave by Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
+%% Copyright (c) 2012, Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
+%% Derived from distancePointEdge.m by David Legland <david.legland@grignon.inra.fr>
 %%
 %% All rights reserved.
 %% (simplified BSD License)
@@ -10,8 +9,8 @@
 %%
 %% 1. Redistributions of source code must retain the above copyright notice, this
 %%    list of conditions and the following disclaimer.
-%%     
-%% 2. Redistributions in binary form must reproduce the above copyright notice, 
+%%
+%% 2. Redistributions in binary form must reproduce the above copyright notice,
 %%    this list of conditions and the following disclaimer in the documentation
 %%    and/or other materials provided with the distribution.
 %%
@@ -19,9 +18,9 @@
 %% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 %% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 %% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
-%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
+%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 %% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-%% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
+%% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 %% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 %% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 %% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
@@ -33,53 +32,100 @@
 
 %% -*- texinfo -*-
 %% @deftypefn {Function File} {@var{dist} = } distancePointEdge (@var{point}, @var{edge})
+%% @deftypefnx {Function File} {@var{dist} = } distancePointEdge (@dots, @var{opt})
+%% @deftypefnx {Function File} {[@var{dist} @var{pos}]= } distancePointEdge (@dots)
 %% Minimum distance between a point and an edge
 %%
-%%   DIST = distancePointEdge(POINT, EDGE);
-%%   Return the euclidean distance between edge EDGE and point POINT. 
-%%   EDGE has the form: [x1 y1 x2 y2], and POINT is [x y].
+%% Return the euclidean distance between edge @var{edge} and point @var{point}.
+%% @var{edge} has the form: [x1 y1 x2 y2], and @var{point} is [x y].
+%% If @var{edge} is Ne-by-4 and @var{point} is Np-by-2, then @var{dist} is
+%% Np-by-Ne, where each row contains the distance of each point to all the edges.
 %%
-%%   If EDGE is N-by-4 array, result is N-by-1 array computed for each edge.
-%%   If POINT is a N-by-2 array, the result is computed for each point.
-%%   If both POINT and EDGE are array, they must have the same number of
-%%   rows, and the result is computed for each couple point(i,:);edge(i,:).
+%% If @var{opt} is true (or equivalent), the optput is cmpatible with the original function:
+%% @table @samp
+%% @item 1
+%% If @var{point} is 1-by-2 array, the result is Ne-by-1 array computed for each edge.
+%% @item 2
+%% If @var{edge} is a 1-by-4 array, the result is Np-by-1 computed for each point.
+%% @item 3
+%% If both @var{point} and @var{edge} are array, they must have the same number of
+%% rows, and the result is computed for each couple @code{@var{point}(i,:),@var{edge}(i,:)}.
+%% @end table
 %%
-%%   [DIST POS] = distancePointEdge(POINT, EDGE);
-%%   Also returns the position of closest point on the edge. POS is
-%%   comprised between 0 (first point) and 1 (last point).
+%% If the the second output argument @var{pos} is requested, the function also
+%% returns the position of closest point on the edge. @var{pos} is comprised
+%% between 0 (first point) and 1 (last point).
 %%
-%%   @seealso{edges2d, points2d, distancePoints, distancePointLine}
+%% @seealso{edges2d, points2d, distancePoints, distancePointLine}
 %% @end deftypefn
 
-function varargout = distancePointEdge(point, edge)
+%% Rewritten to accept different numbers of points and edges.
+%% 2012 - Juan Pablo Carbajal
+
+function [dist, tp] = distancePointEdge(point, edge, opt=[])
+
+  Np = size (point,1);
+  Ne = size (edge,1);
+  edge = edge.';
+
   % direction vector of each edge
-  dx = edge(:, 3) - edge(:,1);
-  dy = edge(:, 4) - edge(:,2);
+  dx = edge(3,:) - edge(1,:);
+  dy = edge(4,:) - edge(2,:);
 
   % compute position of points projected on the supporting line
   % (Size of tp is the max number of edges or points)
+
   delta = dx .* dx + dy .* dy;
-  tp = ((point(:, 1) - edge(:, 1)) .* dx + (point(:, 2) - edge(:, 2)) .* dy) ./ delta;
-
-  % ensure degenerated edges are correclty processed (consider the first
-  % vertex is the closest)
-  tp(delta < eps) = 0;
+  warning ('off', 'Octave:broadcast');
+  tp = ((point(:, 1) - edge(1, :)) .* dx + (point(:, 2) - edge(2, :)) .* dy) ./ delta;
+  tp(:,delta < eps) = 0;
 
   % change position to ensure projected point is located on the edge
   tp(tp < 0) = 0;
   tp(tp > 1) = 1;
 
   % coordinates of projected point
-  p0 = [edge(:,1) + tp .* dx, edge(:,2) + tp .* dy];
+  p0x = (edge(1,:) + tp .* dx);
+  p0y = (edge(2,:) + tp .* dy);
 
   % compute distance between point and its projection on the edge
-  dist = sqrt((point(:,1) - p0(:,1)) .^ 2 + (point(:,2) - p0(:,2)) .^ 2);
+  dist = sqrt((point(:,1) - p0x) .^ 2 + (point(:,2) - p0y) .^ 2);
+
+  warning ('on', 'Octave:broadcast');
 
-  % process output arguments
-  varargout{1} = dist;
-  if nargout > 1
-      varargout{2} = tp;
+  %% backwards compatibility
+  if opt
+    if  Np != Ne && (Ne != 1 && Np !=1)
+      error ("geometry:InvalidArgument", ...
+      "Sizes must be equal or one of the inputs must be 1-by-N array.");
+    end
+    if Np == Ne
+      dist = diag(dist)(:);
+      tp = diag(tp)(:);
+    elseif Np == 1
+      dist = dist.';
+      tp = tp.';
+    end
   end
 
 endfunction
 
+%% Original code
+%%tp = ((point(:, 1) - edge(:, 1)) .* dx + (point(:, 2) - edge(:, 2)) .* dy) ./ delta;
+%%% ensure degenerated edges are correclty processed (consider the first
+%%% vertex is the closest)
+%%if Ne > 1
+%%  tp(delta < eps) = 0;
+%%elseif delta < eps
+%%  tp(:) = 0;
+%%end
+
+%%% change position to ensure projected point is located on the edge
+%%tp(tp < 0) = 0;
+%%tp(tp > 1) = 1;
+
+%%% coordinates of projected point
+%%p0 = [edge(:,1) + tp .* dx, edge(:,2) + tp .* dy];
+
+%%% compute distance between point and its projection on the edge
+%%dist = sqrt((point(:,1) - p0(:,1)) .^ 2 + (point(:,2) - p0(:,2)) .^ 2);