% mmd_im_decom_priority.m
%
% image domain multi-material decomposition with priority list
%
% run mmd_setup.m first
%
% 5 materials:
% 1. fat
% 2. blood
% 3. iodine contrast agent (6% iodine + 94%water)
% 4. cortical bone
% 5. air
%
% units:cm
%
% Yong Long, 2013-12-25
%
%

savefig on

if ~isvar('yi'), printm 'run setup first'
    mmd_setup
    
    f.home = which('mmd_im_decom'); % find setup.m
    [f.home dummy] = fileparts(f.home);
    clear dummy

%     if streq(f.home, '/home/Yong.Long/src/irt_yl/mmd')
%         f.dir = '/home/Yong.Long/data/mmd/2015-04-15/';
%     else
%         error('wrong directory')
%     end
end

% image domain multi-material decomposition
% convential polynomial inverse for sinogram domain decomposition
if ~isvar('fbp_con'), printm 'convential sinogram decomposition' 
    dtype = {'water', 'iodine'};   
    sls_de = de_ftab_sls('max', [50 20], 'min', [0 0], 'n', [101 31]);  
    mas_de = xray_read_mac(dtype);
    ftab_de = de_ftab_yl(xrs, mas_de, 'sls', sls_de, 'ctype', 'pre10');   
        
    energies = [70 140];
       
   [shat_con fbp_con xlac_con] = mmdect_im_setup(hfi, ftab_de, sg, ig, dtype,...
            'energies', energies);
end
     
if ~isvar('triplets'), printm 'triplets: library of possible material triplets' 
   ntype = {'omnipaque300', 'bone'; 'omnipaque300', 'adipose'};
   triplets = make_material_triplets(mtype, 'ntype', ntype);
end

if ~isvar('priority_triplets'), printm 'priority list' 
    plist = [3 4 2 1 5];
   for i = 1 : size(triplets, 1)
       priority_triplets(i, :) = triplets(plist(i), :);
   end
end

% image domain multi-material decomposition
% mendonca:13:afm
if ~isvar('xmm_con'), printm 'convential sinogram decomposition' 
 
       pixmin = - 10^-3;
       pixmax = 1 + 10^-3;
       
       xmm_con = mmd_dect_im_priority(ig.maskit(xlac_con), mtype, energies, ...
           'triplets', priority_triplets, 'pixmin', pixmin, 'pixmax', pixmax);
       xmm_con = ig.embed(xmm_con);
 
       f.dir = [f.dir '/im/'];
       file_con = [f.dir 'im_priority_34215_results.fld']; % model mismatch (size difference)
       fld_write(file_con, xmm_con)

      
      
      if 0      
            im(xmm_con, [0 1], 'adipose, blood, omnipaque300, bone, air'), cbar
             savefig('eps_c', f.dir, 'mmd_id_modified')
      end
end




