clear

tissues = {'FAT','FAT2','GM','MARROW','SKIN-MUSCLES','SKULL','VESSELS',...
    'WM','CSF','MUSCLES','DURA'};
%1. Fat 2. Fat2 3. GM 4. Marrow 5. Skin-muscles 6. Skull 7. Vessels
%8. WM 9. CSF 10. Muscles 11. Dura
for i = 1:length(tissues)
    data(i) = loadnifti(sprintf('datasets/2b/%s.nii.gz',tissues{i}));
    data(i).NIFTIData = mat2gray(data(i).NIFTIData);
end
seg.wm = data(8).NIFTIData; %WM
seg.gm = data(3).NIFTIData; %GM
seg.csf = data(9).NIFTIData  + data(7).NIFTIData + data(11).NIFTIData; % CSF + Vessels
seg.skull = data(6).NIFTIData + data(4).NIFTIData; % Skull + Marrow
seg.scalp = data(5).NIFTIData + data(10).NIFTIData + data(1).NIFTIData + data(2).NIFTIData; %Skin-muscles + muscles + fat/fat2

cfg.samplingwm = 3.4;
cfg.samplinggm = 3.4;
cfg.samplingcsf = 4;
cfg.samplingskull = 5;
cfg.samplingscalp = 6;
cfg.meshfield = 10;
cfg.maxvol = 100;
[node,elem,face] = brain2mesh(seg,cfg);

%%%%%%%%%%%%
%%Plotting%%
%%%%%%%%%%%%
subplot(1,2,1)
hold on
plotmesh(node,elem(elem(:,5)==5,:),'x>137','FaceColor',[1 1 1],'EdgeAlpha',0.85) %%wm
plotmesh(node,elem(elem(:,5)==4,:),'x>137','FaceColor',[0.35 0.35 0.35],'EdgeAlpha',0.85) %%pial
plotmesh(node,elem(elem(:,5)==3,:),'x>137','FaceColor',[0.2 0.6 1],'EdgeAlpha',0.85) %%csf
plotmesh(node,elem(elem(:,5)==2,:),'x>137','FaceColor',[1 1 0.9],'EdgeAlpha',0.85) %%bone
plotmesh(node,elem(elem(:,5)==1,:),'x>137','FaceColor',[1 0.8 0.7],'EdgeAlpha',0.85) %%scalp
view([270 0])
axis off
subplot(1,2,2)
hold on
plotmesh(node,elem(elem(:,5)==5,:),'y>185','FaceColor',[1 1 1],'EdgeAlpha',0.85) %%wm
plotmesh(node,elem(elem(:,5)==4,:),'y>185','FaceColor',[0.35 0.35 0.35],'EdgeAlpha',0.85) %%pial
plotmesh(node,elem(elem(:,5)==3,:),'y>185','FaceColor',[0.2 0.6 1],'EdgeAlpha',0.85) %%csf
plotmesh(node,elem(elem(:,5)==2,:),'y>185','FaceColor',[1 1 0.9],'EdgeAlpha',0.85) %%bone
plotmesh(node,elem(elem(:,5)==1,:),'y>185','FaceColor',[1 0.8 0.7],'EdgeAlpha',0.85) %%scalp
view([0 0])
axis off

figure;
subplot(1,2,1)
plotmesh(node,elem(elem(:,5)==5,:),'FaceColor',[1 1 1],'EdgeAlpha',0.85)
axis off
subplot(1,2,2)
plotmesh(node,elem(elem(:,5)==4,:),'FaceColor',[0.35 0.35 0.35])
axis off
