]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/geom2d/distancePoints.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / geom2d / distancePoints.m
1 %% Copyright (c) 2011, INRA
2 %% 2004-2011, David Legland <david.legland@grignon.inra.fr>
3 %% 2011 Adapted to Octave by Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
4 %%
5 %% All rights reserved.
6 %% (simplified BSD License)
7 %%
8 %% Redistribution and use in source and binary forms, with or without
9 %% modification, are permitted provided that the following conditions are met:
10 %%
11 %% 1. Redistributions of source code must retain the above copyright notice, this
12 %%    list of conditions and the following disclaimer.
13 %%     
14 %% 2. Redistributions in binary form must reproduce the above copyright notice, 
15 %%    this list of conditions and the following disclaimer in the documentation
16 %%    and/or other materials provided with the distribution.
17 %%
18 %% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 %% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 %% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 %% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 %% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
23 %% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 %% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
25 %% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 %% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 %% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 %% POSSIBILITY OF SUCH DAMAGE.
29 %%
30 %% The views and conclusions contained in the software and documentation are
31 %% those of the authors and should not be interpreted as representing official
32 %% policies, either expressed or implied, of copyright holder.
33
34 %% -*- texinfo -*-
35 %% @deftypefn {Function File} {@var{d} = } distancePoints (@var{p1}, @var{p2})
36 %% @deftypefnx {Function File} {@var{d} = } distancePoints (@var{p1}, @var{p2}, @var{norm})
37 %% @deftypefnx {Function File} {@var{d} = } distancePoints (@dots{}, 'diag')
38 %% Compute distance between two points.
39 %%
40 %%   Returns the Euclidean distance between points @var{p1} and @var{p2}.
41 %%   If @var{p1} and @var{p2} are two arrays of points, result is a N1xN2 array
42 %%   containing distance between each point of @var{p1} and each point of @var{p2}. 
43 %%
44 %%   Is @var{norm} is given, computes distance using the specified norm. @var{norm}=2 corresponds to usual
45 %%   euclidean distance, @var{norm}=1 corresponds to Manhattan distance, @var{norm}=inf
46 %%   is assumed to correspond to maximum difference in coordinate. Other
47 %%   values (>0) can be specified.
48 %%
49 %%   When 'diag' is given, computes only distances between @var{p1}(i,:) and @var{p2}(i,:).
50 %%
51 %%   @seealso{points2d, minDistancePoints}
52 %% @end deftypefn
53
54 function dist = distancePoints(p1, p2, varargin)
55
56   %% Setup options
57
58   % default values
59   diag = false;
60   norm = 2;
61
62   % check first argument: norm or diag
63   if ~isempty(varargin)
64       var = varargin{1};
65       if isnumeric(var)
66           norm = var;
67       elseif strncmp('diag', var, 4)
68           diag = true;
69       end
70       varargin(1) = [];
71   end
72
73   % check last argument: diag
74   if ~isempty(varargin)
75       var = varargin{1};
76       if strncmp('diag', var, 4)
77           diag = true;
78       end
79   end
80
81
82   % number of points in each array and their dimension
83   n1  = size(p1, 1);
84   n2  = size(p2, 1);
85   d   = size(p1, 2);
86
87   if diag
88       % compute distance only for apparied couples of pixels
89       dist = zeros(n1, 1);
90       if norm==2
91           % Compute euclidian distance. this is the default case
92           % Compute difference of coordinate for each pair of point
93           % and for each dimension. -> dist is a [n1*n2] array.
94           for i=1:d
95               dist = dist + (p2(:,i)-p1(:,i)).^2;
96           end
97           dist = sqrt(dist);
98       elseif norm==inf
99           % infinite norm corresponds to maximal difference of coordinate
100           for i=1:d
101               dist = max(dist, abs(p2(:,i)-p1(:,i)));
102           end
103       else
104           % compute distance using the specified norm.
105           for i=1:d
106               dist = dist + power((abs(p2(:,i)-p1(:,i))), norm);
107           end
108           dist = power(dist, 1/norm);
109       end
110   else
111       % compute distance for all couples of pixels
112       dist = zeros(n1, n2);
113       if norm==2
114           % Compute euclidian distance. this is the default case
115           % Compute difference of coordinate for each pair of point
116           % and for each dimension. -> dist is a [n1*n2] array.
117           for i=1:d
118               % equivalent to:
119               % dist = dist + ...
120               %   (repmat(p1(:,i), [1 n2])-repmat(p2(:,i)', [n1 1])).^2;
121               dist = dist + (p1(:, i*ones(1, n2))-p2(:, i*ones(n1, 1))').^2;
122           end
123           dist = sqrt(dist);
124       elseif norm==inf
125           % infinite norm corresponds to maximal difference of coordinate
126           for i=1:d
127               dist = max(dist, abs(p1(:, i*ones(1, n2))-p2(:, i*ones(n1, 1))'));
128           end
129       else
130           % compute distance using the specified norm.
131           for i=1:d
132               % equivalent to:
133               % dist = dist + power((abs(repmat(p1(:,i), [1 n2]) - ...
134               %     repmat(p2(:,i)', [n1 1]))), norm);
135               dist = dist + power((abs(p1(:, i*ones(1, n2))-p2(:, i*ones(n1, 1))')), norm);
136           end
137           dist = power(dist, 1/norm);
138       end
139   end
140
141 endfunction
142
143 %!shared pt1,pt2,pt3,pt4
144 %!  pt1 = [10 10];
145 %!  pt2 = [10 20];
146 %!  pt3 = [20 20];
147 %!  pt4 = [20 10];
148
149 %!assert (distancePoints(pt1, pt2), 10, 1e-6);
150 %!assert (distancePoints(pt2, pt3), 10, 1e-6);
151 %!assert (distancePoints(pt1, pt3), 10*sqrt(2), 1e-6);
152 %!assert (distancePoints(pt1, pt2, 1), 10, 1e-6);
153 %!assert (distancePoints(pt2, pt3, 1), 10, 1e-6);
154 %!assert (distancePoints(pt1, pt3, 1), 20, 1e-6);
155 %!assert (distancePoints(pt1, pt2, inf), 10, 1e-6);
156 %!assert (distancePoints(pt2, pt3, inf), 10, 1e-6);
157 %!assert (distancePoints(pt1, pt3, inf), 10, 1e-6);
158 %!assert (distancePoints(pt1, [pt1; pt2; pt3]), [0 10 10*sqrt(2)], 1e-6);
159
160 %!test
161 %!  array1 = [pt1;pt2;pt3];
162 %!  array2 = [pt1;pt2;pt3;pt4];
163 %!  res = [0 10 10*sqrt(2) 10; 10 0 10 10*sqrt(2); 10*sqrt(2) 10 0 10];
164 %!  assert (distancePoints(array1, array2), res, 1e-6);
165 %!  assert (distancePoints(array2, array2, 'diag'), [0;0;0;0], 1e-6);
166
167 %!test
168 %!  array1 = [pt1;pt2;pt3];
169 %!  array2 = [pt2;pt3;pt1];
170 %!  assert (distancePoints(array1, array2, inf, 'diag'), [10;10;10], 1e-6);
171
172 %!shared pt1,pt2,pt3,pt4
173 %!  pt1 = [10 10 10];
174 %!  pt2 = [10 20 10];
175 %!  pt3 = [20 20 10];
176 %!  pt4 = [20 20 20];
177
178 %!assert (distancePoints(pt1, pt2), 10, 1e-6);
179 %!assert (distancePoints(pt2, pt3), 10, 1e-6);
180 %!assert (distancePoints(pt1, pt3), 10*sqrt(2), 1e-6);
181 %!assert (distancePoints(pt1, pt4), 10*sqrt(3), 1e-6);
182 %!assert (distancePoints(pt1, pt2, inf), 10, 1e-6);
183 %!assert (distancePoints(pt2, pt3, inf), 10, 1e-6);
184 %!assert (distancePoints(pt1, pt3, inf), 10, 1e-6);
185 %!assert (distancePoints(pt1, pt4, inf), 10, 1e-6);
186
187
188 %!shared pt1,pt2,pt3,pt4
189 %!  pt1 = [10 10 30];
190 %!  pt2 = [10 20 30];
191 %!  pt3 = [20 20 30];
192 %!  pt4 = [20 20 40];
193
194 %!assert (distancePoints(pt1, pt2, 1), 10, 1e-6);
195 %!assert (distancePoints(pt2, pt3, 1), 10, 1e-6);
196 %!assert (distancePoints(pt1, pt3, 1), 20, 1e-6);
197 %!assert (distancePoints(pt1, pt4, 1), 30, 1e-6);
198
199 %!test
200 %!  array1 = [pt1;pt2;pt3];
201 %!  array2 = [pt2;pt3;pt1];
202 %!  assert (distancePoints(array1, array2, 'diag'), [10;10;10*sqrt(2)], 1e-6);
203 %!  assert (distancePoints(array1, array2, 1, 'diag'), [10;10;20], 1e-6);
204