
% Moo K. Chung mkchung@wisc.edu
% October 2010
%
% Lecture 6.
% Topology and graph data structure


% computing Euler characteristic
I=imread('toy-key.tif');
figure;imshow(I)
eul = bweuler(I)

% Exercise: write a code computing Euler characteristic of 
%           a binary object in 3D. 



%---------------------------
% persistence diagram

x=[0:0.002:1]';
s=10*(x - 0.5).^2 + cos(7*pi*x)/2 +x
%s= x
%s=zeros(length(x),1);
e=normrnd(0,0.2,length(x),1);
Y=s+e;

figure;
plot(x,Y,'ro', 'MarkerEdgeColor',[0.5 0.5 0.5], 'MarkerFaceColor',[0.5 0.5 0.5], 'MarkerSize',5)
hold on;

k=100; sigma=0.001
% 1D heat kernel smoothing
[wfs, beta]=WFS_COS(Y,x,k,sigma);

plot(x,wfs,'r','LineWidth',2);
set(gcf,'Color','w') 


pairs=pairing_1D(x,wfs)
figure;
plot(pairs(:,1), pairs(:,2),'o', 'MarkerEdgeColor',[0 0 1], 'MarkerFaceColor',[.2 .2 1], 'MarkerSize',10)
set(gcf,'Color','w') 



% Cloud point data
endp = mouse2points(500,500);


% Rips complex construction

figure;
imshow('toy-key.tif')
set(gcf,'Color','white','InvertHardcopy','off');
[adj, prob, vertices] = points2graph(endp,50, 'rips'); % do not go beyond 50 to speed up demonstration
figure_graph_color(adj,vertices, ones(size(adj,1),1))


% white matter boundary visualization
nii = load_nii('mean_diffeomorphic_initial6_fa.nii');  
d=nii.img;
surf = isosurface(d); 
coord=surf.vertices;
temp=coord;
temp(:,2)=coord(:,1);
temp(:,1)=coord(:,2);
surf.vertices=temp;
% mesh visualization
figure;figure_patch(surf,[0.9 0.9 0.9],0.2);hold on;
axis off
%view([-90 90])
set(gcf,'Color','w');
hold on;



% fiber bundle visualization
load SL.mat
for i=1:30:10000 % subsampling 1 for every 30 tracts
    tract=SL{i}'; 
    if size(tract,2)> 10
        hold on
        plot3(tract(1,:),tract(2,:),tract(3,:),'b')
    end;
end;

% obtain end points of tracts
endp=get_endp_tracts(SL,30);

hold on; plot3(endp(:,1,1),endp(:,1,2), endp(:,1,3),'.r')
hold on; plot3(endp(:,2,1),endp(:,2,2), endp(:,2,3),'.r')
set(gcf,'Color','white','InvertHardcopy','off');

% Epsilon-neighbor graph construction

figure;figure_patch(surf,[0.9 0.9 0.9],0.1);
hold on;
[adj, prob, vertices]=tract2graph(SL, 10);
figure_dtigraph(adj,vertices);




