%*************** FONCTION ROI Pos ****************

M_rot = Volume(Num_vol).M_rot;
M_rot_for_Pos = inv(M_rot(1:3,1:3));

if strcmp(action,'ROI_draw')

   if get(hdl.roi_draw,'value')
	
	   set(gcf,'WindowButtonDownFcn','set(gcf,''WindowButtonMotionFcn'',''draw'')')
	   set(gcf,'WindowButtonUpFcn','affichevol(3,''ROI_draw_end'')')
   else
	   set(gcf,'WindowButtonUpFcn','')
	   set(gcf,'WindowButtonDownFcn','')
	   zoom;zoom;
   end
	realy_refresh = 1;

elseif strcmp(action,'ROI_draw_end'),

   set(gcf,'WindowButtonMotionFcn','');

   hdl_poly = draw(0);

 if ~isempty(hdl_poly)
   xp = get(hdl_poly,'XData'); yp = get(hdl_poly,'YData'); 

   delete(hdl_poly) ; clear draw;

   Bw1 = remplie(xp',yp',NbLigne,NbLigne);

   [x,y] = find(Bw1);
 
   k = size(Pos,1) ;
   l = length(x);
   for m=1:l
      Pos(k+m,:) = [x(m) y(m) coupe] * M_rot_for_Pos;
   end

 else 
   realy_refresh = 1;
 end
   
elseif strcmp(action,'ChoisiPos'),

   if get(hdl.roi_draw,'value')
	set(hdl.roi_draw,'value',0)
	set(gcf,'WindowButtonUpFcn','');
	set(gcf,'WindowButtonDownFcn','')
	   zoom;zoom;
   end

   if strcmp( get(gcf,'WindowButtonDownFcn') , 'zoom down')
	zzz=2	;
	zoom
   end

    hold on
    k = size(Pos,1)+1;
    
    while (1==1) 
       [x,y,buton] = ginput(1);
       
       if buton==1
          l = 1; go = 1;
          while (l<k & go )
             comp = ( Pos(l,:) == [round(y) round(x) coupe]*M_rot_for_Pos );
             if ( (comp(1)==1 & comp(2)==1) & comp(3)==1)
                go = 0;
             end
             l = l+1;
          end
          if go
             Pos(k,:) = [round(y) round(x) coupe]*M_rot_for_Pos;
             k=k+1;
          end
             
       elseif buton==3
          l = 1; go = 1; 
          while (l<k & go )
             comp = ( Pos(l,:) == [round(y) round(x) coupe]*M_rot_for_Pos);
             if ( (comp(1)==1 & comp(2)==1) & comp(3)==1)
                go = 0;
             end
             l = l+1;
          end
          if go
             Volume(hdl.Num).Pos = Pos;
	     realy_refresh = 1;
		if exist('zzz'), zoom; end
             return
          else
             realy_refresh = 1;
	     Pos(l-1,:) = [];
             k=k-1;
          end
       end;
       Volume(hdl.Num).Pos = Pos;       
       Coupeprog
    end
    

elseif strcmp(action,'ROItomask');
%no more use for the object

  M = Volume(hdl.Num).M;
  DIM = size(Volume(hdl.Num).data)

  workdir=pwd;
  cd (Result_path)
  
  [Fname, Pname] = uiputfile('*', 'where');
  if Fname
    Fname = cat(2,Pname,Fname)
    
    mask =  deal(struct(...
		      'fname',	[Fname '.img'],...
		      'dim',		[DIM,spm_type('uint8')],...
		      'mat',		M,...
		      'pinfo',	[1 0 0]',...
		      'descrip',	'mask from ROI')); 
  
    mask = spm_create_image(mask);

    Bw=zeros(DIM);
    for k=1:size(Pos,1)
      Bw(Pos(k,2),DIM(1)+1-Pos(k,1),Pos(k,3))=1;
    end
    
    mask = spm_write_vol(mask,Bw);
  
  end
  cd (workdir)


elseif strcmp(action,'clean_SelectPos');
	
	   set(gcf,'WindowButtonDownFcn','set(gcf,''WindowButtonMotionFcn'',''draw'')')
	   set(gcf,'WindowButtonUpFcn','affichevol(3,''clean_SelectPos_end'')')
	realy_refresh = 1;

elseif strcmp(action,'clean_SelectPos_end');

	   set(gcf,'WindowButtonUpFcn','')
	   set(gcf,'WindowButtonDownFcn','')
	   zoom;zoom;
	realy_refresh = 1;


   set(gcf,'WindowButtonMotionFcn','');

   hdl_poly = draw(0);

 if ~isempty(hdl_poly)
   xp = get(hdl_poly,'XData'); yp = get(hdl_poly,'YData'); 

   delete(hdl_poly) ; clear draw;

   Bw = remplie(xp',yp',NbLigne,NbLigne);

   [x,y] = find(Bw);

   tmp_pos = Pos*(M_rot(1:3,1:3));
 
   ind = find(tmp_pos(:,3)==coupe);
   l = length(ind);
   m=1;
   for k = 1:l
      p = ind(k);
      if Bw(tmp_pos(p,1),tmp_pos(p,2))==1
         sup(m) = ind(k);
         m = m+1;
       else
      end

   end
   if exist('sup')
     tmp_pos(sup,:) = [];
     Pos = tmp_pos*inv(M_rot(1:3,1:3));
   end
end
   
   
elseif strcmp(action,'SavePos');

   s = struct('XYZ',Pos','mat',Volume(hdl.Num).M);
   roi_o = maroi_pointlist(s,'vox');
   l = spm_input('Label for ROI', '+1', 's', '');
   roi_o = label(roi_o,l);

   marsbar('saveroi',roi_o);
   realy_refresh = 1;

elseif strcmp(action,'loadPos');

   workdir=pwd;
   cd (Result_path)
   [Fname, Pname] = uigetfile('*.mat', 'where'); Fname = cat(2,Pname,Fname);   
   cd (workdir)  

   if Fname
     roi_o = maroi('load',Fname);
   else 
     return;
   end

   if ~isempty(Pos)
      choix = questdlg('keep the existing ROI', ...
                         'the choice', ...
                         'yes','no','no');
      switch choix
	case 'yes'
   	case 'no'
   		Pos =  []; 
	end
   end

   sp = mars_space(Volume(hdl.Num).Vol);
   Pos = [Pos ; voxpts(roi_o,sp)']	;
          
   
elseif strcmp(action,'CleanPos');
   Pos=[];
   set(hdl.Pos,'Xdata',0,'Ydata',0);

   realy_refresh = 1;

end;











