close all;
clear all;
pack;
clc;
%_______________________Parameters__________________
global M N N_Slice mi ma FA FA_Image_Mask pds PassHistory Speed_F_Thresh Collinearity_Thresh Speed_factor FA_Seg_Thresh
global h1 h2 h3 DTI_Tag BundleIndex BundleIndexThresh NTSP0;
global NormalDirection Ten_Sim_Thre;
global notBundleIndex notBundleIndexThresh
warning off;

r=90;

addpath f:\MatlabCodes

addr= 'Z:\Epilepsy_ANALYZE\EP1218'; 

FA_threshold=0.01;

% % %----------------------Cg-------------------------
FiberName='Cg_'; % 'ATR_';   %FX'; 
BundleIndex=2;
BundleIndexThresh=0.8;
r=90;
Speed_factor=1;
DTI_Tag=1;
notBundleIndex =1;
notBundleIndexThresh=0.3; %0.5;   %The more, the bigger Segment, but the more leakage
Collinearity_Thresh =0.8; %0.5;%0.7;
FA_Seg_Thresh=0.01;%0.02;%0.05;
Speed_F_Thresh =0.01;%0.02;%0.05;
Ten_Sim_Thre=0.2;

%___________Load DTI__________________
[filename, pathname] = uigetfile('DTI.mat','Load DTI File');
exist_f = isa(filename,'char');
if exist_f
    Inputfile = fullfile(pathname,filename);
else
    return;
end
cd (pathname);
load (Inputfile);
try
    DT=Full_Rotated_Nei_Tensor;
    FA=Full_Rotated_Nei_FA;
    
    FE=Full_Rotated_Nei_FE;
    
catch
end

FA(isnan(FA))=0;
AnisoImage =FA ;
[M N N_Slice] = size(FA);

DT=permute(DT, [4,1,2,3]);
TensorAll=double(reshape(DT,9,M*N*N_Slice));
TensorAll(:,FA <= FA_threshold)=[];

FA_Image_Mask=find(FA>FA_threshold);
% FE(:,:,:,3)=-FE(:,:,:,3);%%%%%%
FEFA(:,:,:,1)=FE(:,:,:,1).*FA;
FEFA(:,:,:,2)=FE(:,:,:,2).*FA;
FEFA(:,:,:,3)=FE(:,:,:,3).*FA;
FEFA=abs(FEFA);

pds=permute(FE, [4,1,2,3]);
pds=double(reshape(pds,3,M*N*N_Slice));

pds(:,FA <= FA_threshold)=[];


%___________Load Input ROI__________________
[filename, pathname] = uigetfile('Seeds.mat','Load InputROI File');
exist_f = isa(filename,'char');
if exist_f
    Inputfile = fullfile(pathname,filename);
else
    return;
end

load (Inputfile);
% cd (pathname);

Seeds=find(ROI~=0);
[ni nj nk]=ind2sub([M N N_Slice],Seeds);
Seeds=[ni nj nk]';

% Seeds = SeedPoints;

Sort_Seeds();

pause;

str=[ FiberName 'ROI'];
[filename, pathname] = uiputfile(str ,'Save ROI as...');
exist_f = isa(filename,'char');
if exist_f
    ROItargetfile = fullfile(pathname,filename);
else
    return;
end


ROI=zeros(M,N,N_Slice);
Label=zeros(M,N,N_Slice);

Seeds=[ Right_Superior Right_Inferior Left_Superior Left_Inferior ];

notBundleIndexThresh=0.3; %0.5;   %The more, the bigger Segment, but the more leakage
Collinearity_Thresh =0.8; %0.5;%0.7;
FA_Seg_Thresh=0.01;%0.02;%0.05;
Speed_F_Thresh =0.01;%0.02;%0.05;

CoronalSeg;

Analyze = Write_Analyze (ROItargetfile, ROI ,'int16', VDims);
ROIp=ROI;

Seeds=[Right_Posterior Right_Anterior Left_Posterior Left_Anterior];

notBundleIndexThresh=0.9;   %The more, the bigger Segment, but the more leakage

% BundleIndex=3;
% BundleIndexThresh=0.4;

AxialSeg;

Analyze = Write_Analyze (ROItargetfile, ROI ,'int16', VDims);

Analyze = Write_Analyze ([ ROItargetfile 'Label'], Label ,'int16', VDims);

cd(pathname);
save (filename, 'ROI','Act_Right_Posterior', 'Act_Right_Superior' , 'Act_Right_Anterior','Act_Left_Posterior', 'Act_Left_Superior' , 'Act_Left_Anterior' );


FT_ForCalling;

