function SL = get_streamlines(filename,pixsize)
%September 9, 2009.
%Nagesh Adluru.
%Wasiman Laboratory for Brain Imaging
%University of Wisconsin, Madison
%
%Function to get the streamlines in MATLAB format. Based on
%plot_streamlines.m

f = fopen(filename,'r', 'b');
fend = 0;
i = 1; %index
maxP = 0;
tic;
while (fend == 0)
    %read streamline from input file
    a = fread(f,1,'float');
    if (isempty(a))
        break;
    end
    fread(f,1,'float');
    Npoints = floor(a);
    if (Npoints>maxP)
        maxP = Npoints;
    end
    fread(f,[3 Npoints], 'float');
    i = i+1;
end
fclose(f);
disp (['Total number of streamlines: ' num2str(i-1)]);
disp (['Longest streamline: ' num2str(maxP) ' points']);

%Now actual reading.
f = fopen(filename,'r', 'b');
fend = 0;
SL=cell(i-1,1);
i=1;
while (fend == 0)
    %read streamline from input file
    a = fread(f,1,'float');
    if (isempty(a))
        break;
    end
    fread(f,1,'float');
    Npoints = floor(a);
    xyz = fread(f,[3 Npoints], 'float');
    xyz(1,:) = xyz(1,:)/pixsize(1);
    xyz(2,:) = xyz(2,:)/pixsize(2);
    xyz(3,:) = xyz(3,:)/pixsize(3);
    xyz = xyz';
    SL{i} = xyz;
    i = i+1;
end
fclose(f);
fprintf('Read the file in %f sec.',toc);
return