clear
data = loadminc('datasets/2c/subject04_crisp_v.mnc');
% 23 - CSF, 46 - GM, 70 - WM, 93 - FAT, 116 - Muscle, 139 - Muscle/Skin
% 162 - Skull, 209 - Connective Tissues, 232 - Dura , 255 - Marrow

data = data(1:2:end,1:2:end,1:2:end); %Coarsening the data

seg.wm =  data;
seg.gm =  data;
seg.csf =  data;
seg.skull =  data;
seg.scalp = data;

seg.wm(seg.wm == 70) = 1; seg.wm(seg.wm ~=1) = 0; %WM
seg.gm(seg.gm == 46) = 1; seg.gm(seg.gm~=1) = 0;%GM
seg.csf(seg.csf==23|seg.csf==185|seg.csf==232) = 1; seg.csf(seg.csf~=1) = 0; % CSF + Vessels
seg.skull(seg.skull==162|seg.skull==255) = 1; seg.skull(seg.skull~=1) = 0; % Skull + Marrow
seg.scalp(seg.scalp==116|seg.scalp==139|seg.scalp==209|seg.scalp==93) = 1; seg.scalp(seg.scalp~=1) = 0; %Skin-muscles + muscles + fat/fat2

[node,elem,face] = brain2mesh(seg);

% 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>167','FaceColor',[1 1 1],'EdgeAlpha',0.85) %%wm
plotmesh(node,elem(elem(:,5)==4,:),'x>167','FaceColor',[0.35 0.35 0.35],'EdgeAlpha',0.85) %%pial
plotmesh(node,elem(elem(:,5)==3,:),'x>167','FaceColor',[0.2 0.6 1],'EdgeAlpha',0.85) %%csf
plotmesh(node,elem(elem(:,5)==2,:),'x>167','FaceColor',[1 1 0.9],'EdgeAlpha',0.85) %%bone
plotmesh(node,elem(elem(:,5)==1,:),'x>167','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>155','FaceColor',[1 1 1],'EdgeAlpha',0.85) %%wm
plotmesh(node,elem(elem(:,5)==4,:),'y>155','FaceColor',[0.35 0.35 0.35],'EdgeAlpha',0.85) %%pial
plotmesh(node,elem(elem(:,5)==3,:),'y>155','FaceColor',[0.2 0.6 1],'EdgeAlpha',0.85) %%csf
plotmesh(node,elem(elem(:,5)==2,:),'y>155','FaceColor',[1 1 0.9],'EdgeAlpha',0.85) %%bone
plotmesh(node,elem(elem(:,5)==1,:),'y>155','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
