function success=griddata_nearest_test
%GRIDDATA_NEAREST_TEST   1st visual test for griddata_nearest
%
% Compares binning to orthogional grid of random points for:
% griddata_nearest(...) with griddata(...,'nearest')
%
%See also: GRIDDATA_NEAREST

OPT.performance = 0;
OPT.plot        = 0;

%% Define the source data (radnom points)

   xi = rand(3);
   yi = rand(3);

%% Define the target grid 

  [X,Y] = meshgrid(0:.2:1,0:.2:1);
  Z = rand(size(X));

  [zi1,pi1] = griddata_nearest(X,Y,Z,xi,yi);
   zi2      = griddata        (X,Y,Z,xi,yi,'nearest');
   
   if isequal(zi1,zi2)
      success = 1;
   else
      success = 0;
   end
   
%% display results
   
   disp('griddata_nearest(...)')
   disp(zi1)

   disp('griddata(...,''nearest'')')
   disp(zi2)

%% plot results
   if OPT.plot   
   FIG = figure('name',mfilename);
   
   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  ([0 1])
      colorbar
      pcolorcorcen(X,Y,Z,[.5 .5 .5])
      title('griddata\_nearest(...)')

      for i=1:length(xi(:))
          c = i.*[1 1 1]./n;
          plot([xi(i) X(pi1(i))],[yi(i) Y(pi1(i))],'.-','color',c)
      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  ([0 1])
      colorbar
      pcolorcorcen(X,Y,Z,[.5 .5 .5])
      title('griddata(...,''nearest'')')

   pausedisp
   try
      close(FIG)
   end
   
   end % plot   
%% display terrible performance

   if OPT.performance
  [xi,yi] = meshgrid(0:.1:100,0:.1:100);

   tic
   zi1 = griddata_nearest(X,Y,Z,xi,yi);
   r1 = toc

   tic
   zi2 = griddata        (X,Y,Z,xi,yi,'nearest');
   r2 = toc
   end

%% EOF