Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 103 lines (89 sloc) 3.57 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
function [brain_figh brain_view] = plot_brain(surf_brain, viewpoint, varargin)
FSL_DISPLACEMENT = [-1 -17 19];
    DEFAULT_BRAIN_COLOR = [.7 .7 .7];
    
    use_fsl_displacement = false;
    brain_color = DEFAULT_BRAIN_COLOR;
    
    parse_inputs(varargin);
    
    if(~exist('viewpoint', 'var') || isempty(viewpoint)), viewpoint = [180 90]; end
    if(ischar(surf_brain)), surf_brain = cellstr(surf_brain); end
    
    if(iscellstr(surf_brain))
        surf_files = surf_brain;
        clear surf_brain;
        
        for i=1:length(surf_files)
            [d f e] = fileparts(surf_files{i});
            if(strcmp(e, '.mat'))
                surf_brain(i) = load(surf_files{i});
            else
                % See if it's a Freesurfer surf file
                try
                    [s.vertices s.faces] = read_surf(surf_files{i});
                    s.vertices = s.vertices + repmat(FSL_DISPLACEMENT, size(s.vertices, 1), 1); %correction for FSL weirdness
                    s.faces = s.faces + 1; % switch from 0-based to 1-based indexing
                    surf_brain(i) = s; clear s;
                catch exception
                    error('Unknown surface filetype in: %s\nError: %s\n', surf_files{i}, exception.message());
                    fclose('all');
                end
            end
        end
    end
    
% delete(findobj(gcf, 'type', 'light'));
    
    for i=1:length(surf_brain)
        if(isfield(surf_brain(i), 'vertices'))
            vertices = surf_brain(i).vertices;
            faces = surf_brain(i).faces;
        elseif(isfield(surf_brain(i), 'bnd'))
            vertices = surf_brain(i).bnd.pnt;
            faces = surf_brain(i).bnd.tri;
        elseif(isfield(surf_brain(i), 'coords'))
            vertices = surf_brain(i).coords;
            faces = surf_brain(i).faces;
        end
        trisurf(faces, vertices(:,1), vertices(:,2), vertices(:,3), ...
            'FaceVertexCData', repmat(brain_color(:)', [size(vertices, 1) 1]), ...
            'FaceColor', 'interp', ...
            'FaceVertexAlphaData', .2 * ones(size(vertices, 1), 1), ...
            'Tag', 'brain');
        hold on;
    end
    
    
    % Set viewing params
    shading interp;
    lighting gouraud;
    material dull;
    axis off
    axis equal;
    camproj('orthographic');
    %hold on;
    
    update_view_and_light(viewpoint);
% view(viewpoint);
 % camlight('headlight', 'infinite');
    set(gcf, 'color', 'white');
    brain_figh = gcf();
    brain_view = @update_view_and_light;
    
    
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Nested
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function parse_inputs(args)
if(~exist('args', 'var') || isempty(args)), return; end
        for i=1:length(args)
            if(ischar(args{i}))
                switch(lower(args{i}))
                    case 'use_fsl_displacement'
                        use_fsl_displacement = args{i+1};
                    case 'brain_color'
                        brain_color = args{i+1};
                        assert(numel(brain_color) == 3);
                    case 'translucency'
                        brain_color = args{i+1};
                        assert(numel(brain_color) == 3);
                end
            end
        end
    end
function update_view_and_light(viewpoint)
view(viewpoint);
        %delete(findobj(gcf, 'type', 'light'));
        delete(findobj(gca, 'type', 'light'));
        camlight('headlight', 'infinite');
    end
end
Something went wrong with that request. Please try again.