%function OK=griddata_near12_test
%GRIDDATA_NEAR12_TEST   1st visual test for griddata_near12
%
% Compares binning to orthogional grid of random points for:
% griddata_nearest(...) with griddata(...,'nearest')
%
%  [...] = GRIDDATA_NEAR1(X,Y,  XI,YI,npts) is unit-tested
%  for npts=1 with (i) 3 shapes of X, (ii)3 shapes of x and 
%  (iii) an optional extra dimension for Z. Higher npts values 
%  are tested only for not-crashing, and interactively.
%
%See also: GRIDDATA_NEAR1, GRIDDATA_NEAR2
clc
OPT.plot = 1;

%% (xi, yi): Define the source data (radnom points)
%  (X,Y)   : Define the target grid 

   xi0 = -1 + 2.*rand(3);
   yi0 = -1 + 2.*rand(3);
  [X0,Y0] = meshgrid(-1:.1:1,-1:.1:1);
   Z0     = peaks(X0,Y0);

   zi2    = griddata(X0,Y0,Z0,xi0,yi0,'nearest');

success = [];

for extradim = [1] % 0 is OK for all reshapes
for RESHP  = [0 1 2] % 2D > 1D > 1D' % 0 is OK
for reshp  = [0 1 2]
    
   if     RESHP==0
       X = X0;    Y = Y0;    Z = Z0;
   elseif RESHP==1
       X = X0(:); Y = Y0(:); Z = Z0(:);
   elseif RESHP==2
       X = X0(:)';Y = Y0(:)';Z = Z0(:)';
   end
    
   if     reshp==0
       xi = xi0;    yi = yi0;    
   elseif reshp==1
       xi = xi0(:); yi = yi0(:); 
   elseif reshp==2
       xi = xi0(:)';yi = yi0(:)';
   end

   if extradim
     Z  = repmat(Z,[1 1 2]);
   end
   
   for npts = 1:5
   % whos X Y Z zi xi npts
     [pi,ri,wi] = griddata_near1(X,Y,  xi,yi,npts);
      zi1       = griddata_near2(X,Y,Z,xi,yi,pi,wi);
      
      if     reshp==0
        if extradim
          zi1 = reshape(zi1(:,:,1),size(xi0));
        else
          zi1 = reshape(zi1      ,size(xi0));
        end
      elseif reshp==1
        if extradim
          zi1 = reshape(zi1(:,1),size(xi0));
        else
          zi1 = reshape(zi1     ,size(xi0));
        end
      elseif reshp==2
        if extradim
          zi1 = reshape(zi1(:,:,1),size(xi0));
        else
          zi1 = reshape(zi1'    ,size(xi0));
        end
      end      

      
      if npts==1
        if isequal(zi1,zi2)
          success = [success 1];
        else
          success = [success 0];
        end
      end
      
   %% display results
   
      txt = ['griddata\_near1(...,npts=',num2str(npts),')'];
      
      disp(txt)
      disp(zi1)
   
      disp('griddata(...,''nearest'')')
      disp(zi2)
   
   %% plot results
   
   if OPT.plot   
   
      FIG = figure('name',mfilename);setfig2screensize(FIG)
   
      subplot(1,2,1)
      
         n = length(xi(:));
         for i=1:length(xi(:))
             c = i.*[1 1 1]./n;
             h.s(i) = scatter(xi(i),yi(i),100,zi1(i),'filled','MarkerEdgeColor',c);
             hold on
         end
         hold on
         caxis  ([-3 3])
         colorbar
         pcolorcorcen(X0,Y0,Z0,[.5 .5 .5])
         title(txt)
         
         for i=1:n
           for ipts=1:npts
             c = i.*[1 1 1]./n;
             plot([xi(i) X(pi(ipts,i))],[yi(i) Y(pi(ipts,i))],'.-','color',c)
           end
         end
      
      subplot(1,2,2)
         
         n = length(xi(:));
         for i=1:length(xi(:))
             c = i.*[1 1 1]./n;
             h.s(i) = scatter(xi(i),yi(i),100,zi1(i),'filled','MarkerEdgeColor',c);
             hold on
         end
         hold on
         caxis  ([-3 3])
         colorbar
         pcolorcorcen(X0,Y0,Z0,[.5 .5 .5])
         title('griddata(...,''nearest'')')
         
      pausedisp
      try
         close(FIG)
      end
      
   end % plot
         
   end % ipts 
end % reshape
end % RESHAPE
end % extradim

OK = all(success);

%% EOF