0001 function [ddsum, ddmin] = vb_distance_min3d(Vold, Vnew, Rmax, Zstep)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 if ~exist('Zstep','var') | isempty(Zstep), Zstep = 0.002; end;
0020
0021
0022 Npoint = size(Vnew,1);
0023 ddmin = zeros(Npoint,1);
0024 inew = zeros(Npoint,1);
0025
0026 Nold = size(Vold,1);
0027 dd = zeros(Nold,1);
0028 iold = zeros(Nold,1);
0029 Vold2 = zeros(Nold,3);
0030
0031
0032 Zmax = max(Vnew(:,3));
0033 Zmin = min(Vnew(:,3));
0034 Nstep = ceil((Zmax-Zmin)/Zstep);
0035
0036 if Nstep == 0,
0037 Nstep = 1;
0038 Zlist = [Zmax , Zmax + Zstep/2];
0039 else
0040 Zstep = (Zmax-Zmin)/Nstep;
0041 Zlist = Zmin:Zstep:Zmax;
0042 Zlist(Nstep+1) = Zmax + Zstep/2;
0043 end
0044
0045 if ~exist('Rmax','var') | isempty(Rmax), Rmax = 0.003; end;
0046
0047
0048
0049 for n=1:Nstep,
0050
0051 inew = find( Vnew(:,3) >= Zlist(n) ...
0052 & Vnew(:,3) < Zlist(n+1) );
0053 iold = find( Vold(:,3) >= (Zlist(n) - Rmax) ...
0054 & Vold(:,3) < (Zlist(n+1) + Rmax) );
0055
0056 Nnew = length(inew);
0057 Nold2 = length(iold);
0058 Vold2 = Vold(iold,:);
0059
0060
0061
0062 if Nold2 == 0, ddmin(inew) = Rmax; continue; end;
0063
0064 for i=1:Nnew,
0065 k = inew(i);
0066 Vnew2 = Vnew( k,:);
0067
0068
0069 dd = (Vold2(:,1) - Vnew2(1)).^2 ...
0070 + (Vold2(:,2) - Vnew2(2)).^2 ...
0071 + (Vold2(:,3) - Vnew2(3)).^2;
0072
0073
0074 ddmin(k) = min(dd);
0075 end;
0076
0077 end
0078
0079
0080 ixmax = find( ddmin >= Rmax );
0081
0082 Nmax = length(ixmax);
0083
0084
0085
0086
0087 for i=1:Nmax,
0088 k = ixmax(i);
0089 Vnew2 = Vnew( k,:);
0090
0091
0092 dd = (Vold(:,1) - Vnew2(1)).^2 ...
0093 + (Vold(:,2) - Vnew2(2)).^2 ...
0094 + (Vold(:,3) - Vnew2(3)).^2;
0095
0096
0097 ddmin(k) = min(dd);
0098 end;
0099
0100 ddsum = sum(ddmin)/Npoint;