function visclass(varargin)
//-----------------------------------------------------------------------  
// function visclass(varargin)
//
// visualization of classification regions in multi dimensions, default range
// is x,y = -3,...,+3, colorsfor classes: red, blue, green, yellow, purple.
//
// input:    varargin  : run visclass('gui') for seeing the arguments   
//                       
//  output:  none
//
//-----------------------------------------------------------------------  
//   $RCSfile: visclass.sci,v $
//  $Revision: 1.4 $
//      $Date: 2003/12/16 18:08:11 $
//  Copyright: IIF-LMB $Author: haasdonk $
//
//-----------------------------------------------------------------------
//  $Log: 
//-----------------------------------------------------------------------
  
  
  [out,in] = argn(0);
  classi_def="no";
  colm = [1, 0, 0; 0, 0, 1 ; 0, 1, 0; 0, 0, 0];
// $$$   colm=[1, 0, 0; 0, 0, 1 ];
// $$$   colm = [0, 0, 1; 0, 0, 0 ; 0, 0, 0; 0,0,0; 0, 0, 0];
  col_no=size(colm,1);
  dem="no";
  dim="3d";
  nshades=16;
  origin=[-3 ;-3];
  p1=1; p2=2;
  plot_labels=[];
  plot_points=[];
  ppts="no"
  recall="no";
  resol=[31 31]; 
  Size=[6 6];   
  varlist=list();
  
  i=1;
  while i<=length(varargin),    
    if varargin(i)=="origin",
      origin=varargin(i+1); 
      if size(origin,1)==1,
	origin=origin';
      end;      
      i=i+2;
    else      
      if varargin(i)=="projections",
	p1=varargin(i+1);
	p2=varargin(i+2);
	if p1<=0 | p2<=0
	  error('Both projections should be positive integers');
	end;
	i=i+3;
      else
	if varargin(i)=="size",
	  Size=varargin(i+1);
	  if length(Size)~=2,
	    error('length of ""size"" vector should be: 2');
	  end
	  
	  i=i+2;
	else
	  if varargin(i)=="resolution",
	    resol=varargin(i+1);
	    i=i+2;
	  else
	    if varargin(i)=="plot_points",
	      ppts="yes"
	      
	      plot_points = varargin(i+1);
	      plot_labels = varargin(i+2);
	      if size(plot_points,2)~=size(plot_labels,2),
		if size(plot_points,2)~=size(plot_labels,1),
		  error('length of plot_labels should be equal to the"+...
			" number of plot_points');
		end
		plot_labels = plot_labels';
	      end
	      
	      i=i+3;
	    else
	      if  varargin(i) == "shades",
		nshades= varargin(i+1),
		i=i+2;
	      else		
		if varargin(i)=="class_color",
		  cnum=varargin(i+1);
		  nc = varargin(i+2);
		  if  type(cnum)~=1 | type(nc)~=1,
		    error('Incorrect type of arguments for class-colour!');
		  end; 
		  
		  si=size(nc);
		  if si(2)~=3,
		    if si(1)~=3,
		      error('colour size should be 3, i.e. [R G B]');
		    end;
		    nc = nc';
		  end;		  
		  
		  colm(cnum,:)=nc;	
		  if cnum>col_no,		    
		    col_no = cnum;
		  end;
		  
		  i=i+3;
		else
		  
		  if  varargin(i) == "2d" | varargin(i) ==  "3d",
		    dim = varargin(i),
		    i = i+1; 
		  else
		    if  varargin(i) == "gui", 
		      dem = "yes";
		      i = i+1;
		    else		      
		      if varargin(i) == "patrec_demos",
			recall="yes";
			i=i+1;
		      else
			
			if varargin(i) == "classifier",
                          
                          classifier=varargin(i+1);
			  classi_def="yes";
			  varlist=list(varargin(i+2:$));
			  i=i+2+length(varlist);
			else
			  s=varargin(i);
			  error('Undefined input argument : '+string(s));
			  i=i+1;
			end;
		      end;
		    end;		      
		  end;
		end;
	      end;
	    end;
	  end;
	end;
      end;
    end;
  end;
  
  
    
  if dem =="yes",
    wi= new_window();    
    xset('window',wi);
    xname('visclass(""'+dim+'"")');
    xset('wpos',380,250);
    xset('wdim',300*1.5561798,300);
  end  
  
  //xset("colormap",colm);  
  
  if classi_def=="no",
     
    classifier=dumb_class_2d;
  end
  if classifier~=dumb_class_2d,
    vl=length(varlist); 
    
    if vl==0 | vl==1,
      //global DATA
      //exec(DATA+'data.sci');
      
      path1 = get_function_path('visclass');
      path2 = 'macros/visualization/visclass.sci';
      dir_path = part(path1, 1:length(path1)-length(path2) );
      exec(dir_path+'data/data.sci', -1);      
    end;

    if vl==0,
      if classifier==bayes_class,
	//disp(length(varlist))
	varlist=list(0.5,m1,K1,0.5,m2,K2);
      else 
	if classifier==nneigh_class,
	varlist=list(V,l);
	else
	  if classifier==polynom_class,
	    varlist=list(1,V,l);
	  end
	end
      end
    else
      if vl==1 & classifier==polynom_class,
	//global DATA
	//exec(DATA+'data.sci');
	
        varlist(2)= V;
	varlist(3)= l;
      end
    end
  end
  
  DIM = length(origin);
  //checking whether projections are in the range of given dimension
  if p1>DIM| p2>DIM,
    error('Both the projections should be in the ranage of dimension');
  end;
  proj1=zeros(DIM,1); proj2=zeros(DIM,1);
  proj1(p1)=1; proj2(p2)=1;
  
  //vectors with the given resolution
  x_range = 0:resol(1)-1; 
  y_range = 0:resol(2)-1;
  
  //values on x and y axes(in the range from (0,0) to Size())
  xi = x_range * Size(1)/(resol(1)-1);
  yi = y_range * Size(2)/(resol(2)-1);

  //pairing up all the xi & yi i.e. [xr;yr] = pair(xi,yi)
  xr=repmat(xi,1,size(yi,2));
  yr=yi.*.ones(1,size(xi,2));
// $$$   xr=xi.*.ones(1,size(yi,2));
// $$$   yr=repmat(yi,1,size(xi,2));
   
  //points to be classified by the given classifier
  xp = xr .*.(proj1/norm(proj1));
  yp = yr .*.(proj2/norm(proj2));
  op = repmat(origin,1, resol(1)*resol(2));
  
  points =  op + xp + yp;
  [clas, cert]= classifier(points,varlist(:));
    
  certmat = matrix(cert,resol(1),resol(2));
 
  //simple surface-graph of certainties
  // surface graph of certainties with classdependent colors
  // midpoints of facets classified again 
  
  [xx,yy,zz] = genfac3d(xi,yi,certmat);
  
  // xr = [0 1 2 3 ... x_res 0 1 2 3 ... 0 1 ....x_res]
  // yr = [0 0 0         0   1 1 1   ... y_res ..y_res]
  
  xm = sum(xx,'r')/4;
  ym = sum(yy,'r')/4;
  
  // map xm , ym to high-dimension then classifying
  
  xmp = xm.*.(proj1/norm(proj1));
  ymp = ym.*.(proj2/norm(proj2));
  omp = repmat(origin,1,size(xm,2));
  
  pointsm = omp + xmp + ymp;
  [cl, ce]= classifier(pointsm,varlist(:));
  
  xx=xx+origin(p1); 
  yy=yy+origin(p2);

  if dim=='3d',
    //3-dimensional plotting
    new_colm=colm;
    //xset('color',0);
    xset('dashes',xget('color')-1)
    xset("colormap",new_colm); 
    xset('dashes',xget('color')-1)
    plot3d(xx,yy,list(zz,cl),250,-300,"x"+ string(p1)+ "@x"+ ...
	   string(p2)+"@cert");   
    //disp(xget('color'));
    p=[xx;yy];
    c=colm;
    //xset('dashes',xget('color')-1)
    
  else
    //2-dimensional plotting     
    clas_max=max(clas);
    if clas_max>col_no,      
      colm=[colm;zeros(clas_max-col_no,3)];
    end
    cl_max=max(cl);
    new_colm=zeros(cl_max,3);
    new_colm(clas,:)=colm(clas,:);
// $$$     colm=new_colm;
    col_no=size(new_colm,1);
    new_colm=new_colm.*.ones(nshades,1);
    div=repmat(([1:nshades]'/nshades),col_no,3);
    new_colm=new_colm.*div;
    xset("colormap",new_colm);
    col= xget("color");
    dash=xget('dashes');
    cert(find(cert>1))=1;
    cert(find(cert<0))=0;
    plotvals = (clas-1)*nshades+ 1 + cert*(nshades-1);
    plotmatrix =round(matrix(plotvals,resol(1),resol(2)));
    PM=plotmatrix(:,$:-1:1)';    
    Matplot(PM,'020');
    col=size(xget('colormap'),1)+1;
    //disp(xget('dashes'));
    //disp(xget("color"))
    xbasr();
    xset('dashes',col);
    //disp(xget('dashes'));
    //disp(xget("color"));
    p=PM; c=new_colm; 
    mx= min(xx); MX= max(xx);
    my= min(yy); MY= max(yy);
    plot2d([mx,MX],[my, MY],0,"021");    	    
    //xset('dashes',2);
    xstring(mx+(MX-mx)/2,my-(MY-my)/7,['x'+string(p1)]);
    xstring(mx-(MX-mx)/9  ,my+ (MY-my)/2,['x'+string(p2)]);
    //disp(xget("color"));
    
    //plotting the special points
    if ppts=="yes", 
      [pm1, pm2]= size(plot_points);
      if pm1==2,	
	plot_label = plot_labels* nshades;
	xset('mark',4,4);
	//col = xget("color");
	//disp(col)
	//col=size(new_colm,1);
	x_dim=[-.010; 0; .010; 0]*(MX-mx);                                          
	y_dim=[0; -.010; 0; .010]*(MY-my);
	num_pts=length(plot_labels);
	xpols1=repmat(x_dim,1,num_pts)+plot_points(1,:).*.ones(x_dim);
	ypols1=repmat(y_dim,1,num_pts)+plot_points(2,:).*.ones(y_dim);
	xpols2=repmat(x_dim*.7,1,num_pts)+plot_points(1,:).*.ones(x_dim);
	ypols2=repmat(y_dim*.7,1,num_pts)+plot_points(2,:).*.ones(y_dim);
	//disp(xpols1); disp(ypols1);
	xfpolys(xpols1,ypols1,zeros(1:num_pts)+col);
	xfpolys(xpols2,ypols2,plot_label);
	
	//xset('color',col+1);
	//xset('color',size(xget('colormap'),1)+2)
	//xpoly(plot_points(1,:),plot_points(2,:),'marks');
	//xset('mark',4,1);
// $$$ 	execstr('xset(''color'','+string(plot_label)+');xpoly(['+ ...
// $$$ 		string(plot_points(1,:))+'],['+string(plot_points(2,:))+ ...
// $$$ 		'],''marks'');');
	xset('colormap',new_colm);	
      end
    end
  end

    
  //-------------------------------------------
  //if gui or demo option is selceted for demonstration of visclass
  
  
  if dem == "yes",
    
    // creating a figure 
    f =figure('position', [10 250 350 350],'tag','visclass_gui_f_fig');    
    //title of the figure
    title = uicontrol(f,'style','text','position',[80 310 170 ...
		    20],'fontsize',15,'fontweight',"bold", ...
		      'string',"Visclass - GUI");     
    //------------------------------------------------------   
    //first level
    ori=strcat(string(origin),',');
    
    e1 = uicontrol(f,'style','edit','position',[15 271 70 25], ...
		  'String',ori(1),'tag','visclass_gui_e1');
    t1 = uicontrol(f,'style','text','position',[15 251 70 ...
		    20], 'string',"origin");   
    
    e21 = uicontrol(f,'style','edit','position',[100 271 30 25], ...
		    'String',string(p1),'tag','visclass_gui_e21');
    e22 = uicontrol(f,'style','edit','position',[135 271 30 25], ...
		    'String',string(p2),'tag','visclass_gui_e22');
    t2 = uicontrol(f,'style','text','position',[100 251 65 ...
		    20], 'string',"projections");
        
    e31 = uicontrol(f,'style','edit','position',[185 271 30 25], ...
		   'String',string(Size(1)),'tag','visclass_gui_e31');
    e32 = uicontrol(f,'style','edit','position',[220 271 30 25], ...
		   'String',string(Size(2)),'tag','visclass_gui_e32');
    t3 = uicontrol(f,'style','text','position',[180 251 65 ...
		    20], 'string',"size");
        
    e41 = uicontrol(f,'style','edit','position',[270 271 30 25], ...
		   'String',string(resol(1)),'tag','visclass_gui_e41');
    e42 = uicontrol(f,'style','edit','position',[305 271 30 25], ...
		   'String',string(resol(2)),'tag','visclass_gui_e42');
    t4 = uicontrol(f,'style','text','position',[260 251 75 20], ...
		   'String',"resolution");
    
    //------------------------------------------------------------
    //second level
    
    version = getversion();
    vernum = eval(part(version,8:10));
    
    if (vernum>=2.7) then      
      col = uicontrol(f,'style','slider','position',[15,186,100,20], ...
		    'min',1,'max',col_no,'tag','visclass_gui_col');
    else
      col = uicontrol(f,'style','slider','position',[15,141,100,20], ...
		    'min',1,'max',col_no,'tag','visclass_gui_col');
    end;
    
    
    set(col,'value',1);
    
    tcol = uicontrol(f,'style','text','position',[15 161 165 20], ...
		   'String',"class-color number"); 
    
    e5 = uicontrol(f,'style','edit','position',[130 181 35 25], ...
		    'String',string(1),'tag','visclass_gui_e5');
    
    puCC = uicontrol(f,'style','pushbutton','position',[175,181,35,25], ...
		     'String'," set",'tag','visclass_gui_pucc');
    
    e5R = uicontrol(f,'style','edit','position',[240 211 50 25], ...
		    'String',string(colm(1,1)),'tag','visclass_gui_e5r');
    t5R = uicontrol(f,'style','text','position',[230 211 10 25], ...
		    'String',"R:");
    
    e5G = uicontrol(f,'style','edit','position',[240 181 50 25], ...
		    'String',string(colm(1,2)),'tag','visclass_gui_e5g');
    t5G = uicontrol(f,'style','text','position',[230 181 10 25], ...
		    'String',"G:");
    
    e5B = uicontrol(f,'style','edit','position',[240 151 50 25], ...
		    'String',string(colm(1,3)),'tag','visclass_gui_e5b');
    t5B = uicontrol(f,'style','text','position',[230 151 10 25], ...
		    'String',"B:");
    
    puRGB = uicontrol(f,'style','pushbutton','position',[300,181,35,25], ...
		     'String',"  set"); 
    
    //------------------------------------------------------------
    // third level    
    
    if (vernum>=2.7) then
      sh = uicontrol(f,'style','slider','position',[15,111,100,20], ...
		   'min',5,'max',100,'tag','visclass_gui_sh');
    else
      sh = uicontrol(f,'style','slider','position',[15,216,100,20], ...
		   'min',5,'max',100,'tag','visclass_gui_sh');
    end;
    
    set(sh,'value',nshades)
    
    t6 = uicontrol(f,'style','text','position',[15 86 100 20], ...
		   'String',"shades");
    e6 = uicontrol(f,'style','edit','position',[130 106 35 25], ...
		   'String',string(nshades),'tag','visclass_gui_e6');
    puSH=uicontrol(f,'style','pushbutton','position',[175,106,35,25], ...
		   'String'," set",'tag','visclass_gui_push');    


    cbPP = uicontrol(f,'style','checkbox','position',[230 106 30 25], ...
		   'tag','visclass_gui_cbpp');
    t7 = uicontrol(f,'style','text','position',[214 86 60 20], ...
		   'String',"plot points");

    cb2d = uicontrol(f,'style','checkbox','position',[297 106 30 25], ...
		   'tag','visclass_gui_cb2d');
    t8 = uicontrol(f,'style','text','position',[285 86 50 20], ...
		   'String',"2d-mode");
   	
    if dim=='2d',
    set(cb2d,'value','1');
    //disp(get(cb2d,'value'))
    end
    
    if ppts=="yes",
    set(cbPP,'value','1');
    end
    
    //-----------------------------------------------

    //button for drawing  
    puD = uicontrol(f,'style','pushbutton','position',[15,31,40,25], ...
		    'String'," draw");
    //help button
    puH=uicontrol(f,'style','pushbutton','position',[115 31 40 25], ...
		  'String',"  help ");
    //exit button
    puE=uicontrol(f,'style','pushbutton','position', [215 31 40 25], ...
		  'String', "  exit");
        
    // call backs for the push buttons	    
    
    set(col,'callback',"visclass_gui_call_back(''cc_slider'')"); 
    set(puCC,'callback',"visclass_gui_call_back(''cc_button'')");
    set(puRGB,'callback',"visclass_gui_call_back(''rgb_button'')");
    set(sh,'callback',"visclass_gui_call_back(''sh_slider'')"); 
    set(puSH,'callback',"visclass_gui_call_back(''sh_button'')");  
    set(puD,'callback',"visclass_gui_call_back(''draw'')");
    set(puH ,'callback',"visclass_gui_call_back(''help'')");
    set(puE ,'callback',"visclass_gui_call_back(''exit'')");
    
    sh_slider_lock =0
    
    param= ['classi_def','colm','col_no','classifier','dem',...
	    'dim','DIM','nshades', 'origin', 'p1','p2','plot_points', ...
	    'plot_labels','recall','resol','sh_slider_lock','Size', ...
	    'varlist','wi'];
    
    
    param1='fig_'+param +'_'+ string(f);
//    execstr('global '+param1);
    global param1;
    execstr(param1+ '=' + param);    
    global  param param1
    
    
    if recall=="no",
     // global vis_gui_lock
      vis_gui_lock=1;
      set(f,'vis_gui_lock',1)
      while evstr(get(f,'vis_gui_lock'))==1 
	//just for blocking other operations
      end;
    end;    
  end;  
  
endfunction;

