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{dist} = } minDistancePoints (@var{pts})
36 %% @deftypefnx {Function File} {@var{dist} = } minDistancePoints (@var{pts1},@var{pts2})
37 %% @deftypefnx {Function File} {@var{dist} = } minDistancePoints (@dots{},@var{norm})
38 %% @deftypefnx {Function File} {[@var{dist} @var{i} @var{j}] = } minDistancePoints (@var{pts1}, @var{pts2}, @dots{})
39 %% @deftypefnx {Function File} {[@var{dist} @var{j}] = } minDistancePoints (@var{pts1}, @var{pts2}, @dots{})
40 %% Minimal distance between several points.
42 %% Returns the minimum distance between all couple of points in @var{pts}. @var{pts} is
43 %% an array of [NxND] values, N being the number of points and ND the
44 %% dimension of the points.
46 %% Computes for each point in @var{pts1} the minimal distance to every point of
47 %% @var{pts2}. @var{pts1} and @var{pts2} are [NxD] arrays, where N is the number of points,
48 %% and D is the dimension. Dimension must be the same for both arrays, but
49 %% number of points can be different.
50 %% The result is an array the same length as @var{pts1}.
52 %% When @var{norm} is provided, it uses a user-specified norm. @var{norm}=2 means euclidean norm (the default),
53 %% @var{norm}=1 is the Manhattan (or "taxi-driver") distance.
54 %% Increasing @var{norm} growing up reduces the minimal distance, with a limit
55 %% to the biggest coordinate difference among dimensions.
58 %% Returns indices @var{i} and @var{j} of the 2 points which are the closest. @var{dist}
60 %% @var{dist} = distancePoints(@var{pts}(@var{i},:), @var{pts}(@var{j},:));
62 %% If only 2 output arguments are given, it returns the indices of points which are the closest. @var{j} has the
63 %% same size as @var{dist}. for each I It verifies the relation :
64 %% @var{dist}(I) = distancePoints(@var{pts1}(I,:), @var{pts2}(@var{J},:));
70 %% % minimal distance between random planar points
71 %% points = rand(20,2)*100;
72 %% minDist = minDistancePoints(points);
74 %% % minimal distance between random space points
75 %% points = rand(30,3)*100;
76 %% [minDist ind1 ind2] = minDistancePoints(points);
78 %% distancePoints(points(ind1, :), points(ind2, :))
79 %% % results should be the same
81 %% % minimal distance between 2 sets of points
82 %% points1 = rand(30,2)*100;
83 %% points2 = rand(30,2)*100;
84 %% [minDists inds] = minDistancePoints(points1, points2);
86 %% distancePoints(points1(10, :), points2(inds(10), :))
87 %% % results should be the same
90 %% @seealso{points2d, distancePoints}
93 function varargout = minDistancePoints(p1, varargin)
97 % default norm (euclidean)
100 % flag for processing of all points
103 % process input variables
105 % specify only one array of points, not the norm
108 elseif length(varargin)==1
111 % specify two arrays of points
115 % specify array of points and the norm
121 % specify two array of points and the norm
128 % number of points in each array
132 % dimension of points
136 %% Computation of distances
139 dist = zeros(n1, n2);
141 % different behaviour depending on the norm used
143 % Compute euclidian distance. this is the default case
144 % Compute difference of coordinate for each pair of point ([n1*n2] array)
145 % and for each dimension. -> dist is a [n1*n2] array.
146 % in 2D: dist = dx.*dx + dy.*dy;
148 dist = dist + (repmat(p1(:,i), [1 n2])-repmat(p2(:,i)', [n1 1])).^2;
151 % compute minimal distance:
153 % either on all couple of points
154 mat = repmat((1:n1)', [1 n1]);
156 [minSqDist ind] = min(dist(ind));
158 % or for each point of P1
159 [minSqDist ind] = min(dist, [], 2);
162 % convert squared distance to distance
163 minDist = sqrt(minSqDist);
165 % infinite norm corresponds to maximum absolute value of differences
166 % in 2D: dist = max(abs(dx) + max(abs(dy));
168 dist = max(dist, abs(p1(:,i)-p2(:,i)));
171 % compute distance using the specified norm.
172 % in 2D: dist = power(abs(dx), n) + power(abs(dy), n);
174 dist = dist + power((abs(repmat(p1(:,i), [1 n2])-repmat(p2(:,i)', [n1 1]))), n);
177 % compute minimal distance
179 % either on all couple of points
180 mat = repmat((1:n1)', [1 n1]);
182 [minSqDist ind] = min(dist(ind));
184 % or for each point of P1
185 [minSqDist ind] = min(dist, [], 2);
188 % convert squared distance to distance
189 minDist = power(minSqDist, 1/n);
195 % convert index in array to row ad column subindices.
196 % This uses the fact that index are sorted in a triangular matrix,
197 % with the last index of each column being a so-called triangular
199 ind2 = ceil((-1+sqrt(8*ind+1))/2);
200 ind1 = ind - ind2*(ind2-1)/2;
205 %% format output parameters
207 % format output depending on number of asked parameters
209 varargout{1} = minDist;
211 % If two arrays are asked, 'ind' is an array of indices, one for each
212 % point in var{pts}1, corresponding to the result in minDist
213 varargout{1} = minDist;
216 % If only one array is asked, minDist is a scalar, ind1 and ind2 are 2
217 % indices corresponding to the closest points.
218 varargout{1} = minDist;
226 %! pts = [50 10;40 60;30 30;20 0;10 60;10 30;0 10];
227 %! assert (minDistancePoints(pts), 20);
230 %! pts = [10 10;25 5;20 20;30 20;10 30];
231 %! [dist ind1 ind2] = minDistancePoints(pts);
232 %! assert (10, dist, 1e-6);
233 %! assert (3, ind1, 1e-6);
234 %! assert (4, ind2, 1e-6);
237 %! pts = [0 80;10 60;20 40;30 20;40 0;0 0;100 0;0 100;0 -10;-10 -20];
238 %! assert (minDistancePoints([40 50], pts), 10*sqrt(5), 1e-6);
239 %! assert (minDistancePoints([25 30], pts), 5*sqrt(5), 1e-6);
240 %! assert (minDistancePoints([30 40], pts), 10, 1e-6);
241 %! assert (minDistancePoints([20 40], pts), 0, 1e-6);
244 %! pts1 = [40 50;25 30;40 20];
245 %! pts2 = [0 80;10 60;20 40;30 20;40 0;0 0;100 0;0 100;0 -10;-10 -20];
246 %! res = [10*sqrt(5);5*sqrt(5);10];
247 %! assert (minDistancePoints(pts1, pts2), res, 1e-6);
250 %! pts = [50 10;40 60;40 30;20 0;10 60;10 30;0 10];
251 %! assert (minDistancePoints(pts, 1), 30, 1e-6);
252 %! assert (minDistancePoints(pts, 100), 20, 1e-6);
255 %! pts = [0 80;10 60;20 40;30 20;40 0;0 0;100 0;0 100;0 -10;-10 -20];
256 %! assert (minDistancePoints([40 50], pts, 2), 10*sqrt(5), 1e-6);
257 %! assert (minDistancePoints([25 30], pts, 2), 5*sqrt(5), 1e-6);
258 %! assert (minDistancePoints([30 40], pts, 2), 10, 1e-6);
259 %! assert (minDistancePoints([20 40], pts, 2), 0, 1e-6);
260 %! assert (minDistancePoints([40 50], pts, 1), 30, 1e-6);
261 %! assert (minDistancePoints([25 30], pts, 1), 15, 1e-6);
262 %! assert (minDistancePoints([30 40], pts, 1), 10, 1e-6);
263 %! assert (minDistancePoints([20 40], pts, 1), 0, 1e-6);
266 %! pts1 = [40 50;25 30;40 20];
267 %! pts2 = [0 80;10 60;20 40;30 20;40 0;0 0;100 0;0 100;0 -10;-10 -20];
268 %! res1 = [10*sqrt(5);5*sqrt(5);10];
269 %! assert (minDistancePoints(pts1, pts2, 2), res1, 1e-6);
270 %! res2 = [30;15;10];
271 %! assert (minDistancePoints(pts1, pts2, 1), res2);
274 %! pts1 = [40 50;20 30;40 20];
275 %! pts2 = [0 80;10 60;20 40;30 20;40 0;0 0;100 0;0 100;0 -10;-10 -20];
276 %! dists0 = [10*sqrt(5);10;10];
278 %! [minDists inds] = minDistancePoints(pts1, pts2);
279 %! assert (dists0, minDists);
280 %! assert (inds1, inds);