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>
5 %% All rights reserved.
6 %% (simplified BSD License)
8 %% Redistribution and use in source and binary forms, with or without
9 %% modification, are permitted provided that the following conditions are met:
11 %% 1. Redistributions of source code must retain the above copyright notice, this
12 %% list of conditions and the following disclaimer.
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.
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.
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.
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.
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}.
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.
49 %% When 'diag' is given, computes only distances between @var{p1}(i,:) and @var{p2}(i,:).
51 %% @seealso{points2d, minDistancePoints}
54 function dist = distancePoints(p1, p2, varargin)
62 % check first argument: norm or diag
67 elseif strncmp('diag', var, 4)
73 % check last argument: diag
76 if strncmp('diag', var, 4)
82 % number of points in each array and their dimension
88 % compute distance only for apparied couples of pixels
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.
95 dist = dist + (p2(:,i)-p1(:,i)).^2;
99 % infinite norm corresponds to maximal difference of coordinate
101 dist = max(dist, abs(p2(:,i)-p1(:,i)));
104 % compute distance using the specified norm.
106 dist = dist + power((abs(p2(:,i)-p1(:,i))), norm);
108 dist = power(dist, 1/norm);
111 % compute distance for all couples of pixels
112 dist = zeros(n1, n2);
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.
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;
125 % infinite norm corresponds to maximal difference of coordinate
127 dist = max(dist, abs(p1(:, i*ones(1, n2))-p2(:, i*ones(n1, 1))'));
130 % compute distance using the specified norm.
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);
137 dist = power(dist, 1/norm);
143 %!shared pt1,pt2,pt3,pt4
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);
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);
168 %! array1 = [pt1;pt2;pt3];
169 %! array2 = [pt2;pt3;pt1];
170 %! assert (distancePoints(array1, array2, inf, 'diag'), [10;10;10], 1e-6);
172 %!shared pt1,pt2,pt3,pt4
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);
188 %!shared pt1,pt2,pt3,pt4
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);
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);