function [X,Y,Z,d,h]=kdplot3(A,k,inc,h,g,haxis);

% KDPLOT3     Kernel Density Estimate plot for 3D data.
%
%             Example: load kddata
%                      kdplot3(lukesh1(:,1:3),'knorm',10,0,0);
%                      kdplot3(lukesh1(:,1:3),'knorm',10,0);
%                      
%             KDPLOT3(A,K,INC,H,G,HAXIS) plots the Kernel Density 
%             Estimate with kernel given by the function in K.
%
%             The data is stored in the rows of A.
%
%             Christian C. Beardah 1994

if nargin==4,
  g=1;
  haxis=[0 1 0 1 0 1];
  elseif nargin==5,
    haxis=[0 1 0 1 0 1];
end;

if nargin<6,

  x=A(:,1);       % Extract the first column of A into x.
  xmin=min(x);    % Find the minimum value of x.
  xmax=max(x);    % Find the maximum value of x. 
  xrange=xmax-xmin; % Find the range of x.

% xa holds the x 'axis' vector, defining a grid of x values where 
% the k.d. function will be evaluated and plotted.

  xa=linspace(xmin-xrange/20,xmax+xrange/20,inc); 

  y=A(:,2);     % Extract the second column of A into y.
  ymin=min(y);
  ymax=max(y);
  yrange=ymax-ymin;

% Establish an 'axis' vector for the y values.

  ya=linspace(ymin-yrange/20,ymax+yrange/20,inc);

  z=A(:,3);     % Extract the second column of A into z.
  zmin=min(z);
  zmax=max(z);
  zrange=zmax-zmin;

% Establish an 'axis' vector for the z values.

  za=linspace(zmin-zrange/20,zmax+zrange/20,inc);

  else,
    
    xa=linspace(haxis(1),haxis(2),inc);
    ya=linspace(haxis(3),haxis(4),inc);
    za=linspace(haxis(5),haxis(6),inc);

end;

[X,Y,Z]=meshgrid(xa,ya,za);
d=kdest3d(X,Y,Z,A,k,h);

if g~=0,
  slice(X,Y,Z,d,mean(xa),mean(ya),mean(za),inc);
end;