% function nodulesui2
%to verify and edit nodule coordinates (add to or remove from the automatically detected potential nodules)
%v4 uses the xygg

%load dataset lists
if ~exist('mpd')
    mpd=uigetdir(pwd,['select the main project folder']);
end
load([mpd '\dataset.mat']);

for s=1:size(imgpth,1);
    c_data=dataset(s,:);
    c_imgpth=imgpth(s,:);
    c_resfol=resfol(s,:);
    if str2num(c_data(end))>0
        if ~exist([c_resfol '/nodattchG.mat'])
            ok=1;
            while ~isempty(ok)
                %% load images and data
                disp(['processing ' c_data '...']);
                im=imread(c_imgpth); %%display original image
                warning('off','images:imshow:magnificationmustbefitfordockedfigure')
                fo=figure; imshow(im); hold on; set(gcf,'numbertitle','off','name', c_data)
                
                %%load data from results folder for this line/plant/plane
                load([c_resfol '/individrootsg.mat'],'attg','xygg'); xyg=xygg;
                load([c_resfol '/nodAttchmts.mat'],'nID','natt','nx','ny');
                
                title('encircle nodulated areas ')
                bttn=1; xb=[]; yb=[]; h=[];
                while bttn==1,
                    [xtmp, ytmp, bttn] = ginput(1);
                    if bttn==1
                        xb=cat(1,xb,xtmp);
                        yb=cat(1,yb,ytmp);
                        if size(xb,1)>1,
                            if ~isempty(h)
                                set(h,'visible','off')
                            end
                            h=line(xb,yb,'linestyle','--','color','y');
                        end;
                    end;
                end;
                set(h,'visible','off')
                save([c_resfol '/nodarea.mat'],'xb','yb');
                
                %remove auto nodules outside of the nodulation zone
                in=inpolygon(nx,ny,xb,yb);
                nx(in==0)=[];
                ny(in==0)=[];
                natt(in==0,:)=[];
                nID(in==0)=[];
                axis([min(xb) max(xb) min(yb) max(yb)])
                
                %% add primary root
                load([c_resfol '/primaryS.mat'],'prixyS');
                load([c_resfol '/primary.mat'],'topx','topy');
                xyg=[xyg;prixyS zeros(size(prixyS,1),1)]; %ID=0
                attg=[topx topy;attg]; %root attchmnt = primary root tip
                
                noddat=[];
                %% for each root
                for k=1:max(xyg(:,3))+1; %for each individ root
                    %find and plot the root
                    rtID=k-1;
                    rtind=find(xyg(:,3)==rtID);
                    
                    %see if it's in the nod zone
                    in=inpolygon(xyg(rtind,1),xyg(rtind,2),xb,yb);
                    if sum(in)>0
                        crt=xyg(rtind,1:2);
                        rtp=plot(crt(:,1),crt(:,2),'g','linewidth',2);
                        
                        %find and plot assoc auto nodules
                        nodind=find(nID==rtID);
                        %                 np=plot(natt(nodind,1),natt(nodind,2),'.m');
                        np=plot(nx(nodind),ny(nodind),'om');
                        
                        %% add or delete nodules
                        title('add or delete nodules to this root if needed')
                        [nxg nyg pp]=ginzoompan_mix(nx(nodind),ny(nodind),'om');
                        
                        %% find and plot attachment points
                        natp=[];
                        for ngn=1:length(nxg)
                            d=distance(crt, [nxg(ngn) nyg(ngn)]);
                            [v i]=min(d);
                            natp(ngn,1:2)=crt(i,1:2);
                            np2=plot(natp(end,1),natp(end,2),'.w');
                        end
                        
                        %                 %% calc spacing between all nodules along that root
                        %                 if~isempty(xg)
                        %                     d=distance(natp,attg(k,:));
                        %                     xy2=sortrows([natp d],3);%put in order
                        %                     xy2=xy2(:,1:2);
                        %                     if rtID>0
                        %                         spc=[];
                        %                         xy3=[attg(rtID,:);xy2]; %add attchmnt pt
                        %                         for j=1:length(xg)
                        %                             spc(j,1)=distance(xy3(j,:),xy3(j+1,:));
                        %                         end
                        %                     else
                        %                         spc=nan(size(xg));
                        %                     end
                        
                        if ~isempty(nxg)
                            noddat=cat(1,noddat,[nxg nyg natp rtID*ones(size(nxg))]) ;
                        end
                        set(pp,'visible','off')
                        set(rtp,'visible','off')
                        set(np,'visible','off')
                        plot(nxg,nyg,'ow')
                    end
                    
                    
%                     set(rtp,'visible','off')
%                     set(np,'visible','off')
%                     plot(nxg,nyg,'ow')
                end
                save([c_resfol '/nodattchG.mat'],'noddat');
                ok=input('Press enter if okay, 1 to redo: ');
            end
        end
    end
end

