Home > bioelectromagnetism > mesh_plot.m

mesh_plot

PURPOSE ^

mesh_plot - plot a mesh from the p structure

SYNOPSIS ^

function [p] = mesh_plot(p),

DESCRIPTION ^

 mesh_plot - plot a mesh from the p structure

 Usage: [p] = mesh_plot(p)

 The p structure is described in eeg_toolbox_defaults.

 Here, the p structure should contain the following fields:

 p.mesh.current -  which mesh to plot (integer)
                   cell of p.mesh.data, see MESH_CHECK
 p.mesh.data    -  contains the vertices and faces in
                   p.mesh.data.vertices{p.mesh.current}
                   p.mesh.data.faces{p.mesh.current}
                   If p.mesh.data is empty, a gui is opened
                   for loading a mesh data file(s).

 This function can plot electrodes on a given scalp mesh
 when p.mesh.data.meshtype contains a 'scalp' mesh
 and p.elec.data is not empty.  However, this function
 does not provide any coregistration/alignment of these
 two datasets and at present it converts the electrode
 data by swapping x/y and converting from centimeters to
 meters (see code for details on why).  It plots given 
 electrodes in blue circles and the nearest vertex 
 points in red dots.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [p] = mesh_plot(p),
0002 
0003 % mesh_plot - plot a mesh from the p structure
0004 %
0005 % Usage: [p] = mesh_plot(p)
0006 %
0007 % The p structure is described in eeg_toolbox_defaults.
0008 %
0009 % Here, the p structure should contain the following fields:
0010 %
0011 % p.mesh.current -  which mesh to plot (integer)
0012 %                   cell of p.mesh.data, see MESH_CHECK
0013 % p.mesh.data    -  contains the vertices and faces in
0014 %                   p.mesh.data.vertices{p.mesh.current}
0015 %                   p.mesh.data.faces{p.mesh.current}
0016 %                   If p.mesh.data is empty, a gui is opened
0017 %                   for loading a mesh data file(s).
0018 %
0019 % This function can plot electrodes on a given scalp mesh
0020 % when p.mesh.data.meshtype contains a 'scalp' mesh
0021 % and p.elec.data is not empty.  However, this function
0022 % does not provide any coregistration/alignment of these
0023 % two datasets and at present it converts the electrode
0024 % data by swapping x/y and converting from centimeters to
0025 % meters (see code for details on why).  It plots given
0026 % electrodes in blue circles and the nearest vertex
0027 % points in red dots.
0028 %
0029 
0030 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:35 $
0031 
0032 % Licence:  GNU GPL, no express or implied warranties
0033 % History:  02/2002, Darren.Weber_at_radiology.ucsf.edu
0034 %
0035 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0036 
0037 if ~exist('p','var'),[p] = eeg_toolbox_defaults; end
0038 
0039 if isfield(p,'mesh'),
0040   if isfield(p.mesh,'data'),
0041     if isempty(p.mesh.data),
0042      [p] = gui_mesh_open(p);
0043       return;
0044     end
0045   else
0046    [p] = gui_mesh_open(p);
0047   end
0048 else
0049  [p] = gui_mesh_open(p);
0050   return;
0051 end
0052 
0053 if isfield(p,'mesh'),
0054   if isfield(p.mesh,'current'),
0055     if isempty(p.mesh.current),
0056       p.mesh.current = 1;
0057     end
0058   else
0059     p.mesh.current = 1;
0060   end
0061 else
0062   msg = sprintf('No mesh field in p struct.\n');
0063   error(msg);
0064 end
0065 
0066 
0067 % -- 'Coregister' electrodes, if required
0068 
0069 if strmatch('scalp',p.mesh.data.meshtype{p.mesh.current}) > 0,
0070   if ~isempty(p.elec.data),
0071    [p] = mesh_fit_elec(p);
0072     p.mesh.current = mesh_check(p,'scalp');
0073   end
0074 end
0075 
0076 
0077 % -- Get the vertices and faces
0078 
0079 vertices = p.mesh.data.vertices{p.mesh.current};
0080 faces    = p.mesh.data.faces{p.mesh.current};
0081 
0082 if isempty(vertices),
0083   fprintf('...p.mesh.current points to an empty cell\n');
0084   return
0085 end
0086 
0087 % -- Get the figure or create it
0088 
0089 if isfield(p.mesh.plot,'fig'),
0090   figure(p.mesh.plot.fig);
0091 else
0092   p.mesh.plot.fig = figure;
0093 end
0094 
0095 set(gcf,'color',[0 0 0]);
0096 
0097 set(gca,'Projection','perspective')
0098 %set(gca,'Projection','orthographic')
0099 set(gca,'DataAspectRatio',[1 1 1]);
0100 
0101 
0102 % -- Get the handles to the current patch
0103 
0104 if isfield(p.mesh.plot,'Hpatch'),
0105   if p.mesh.plot.overlay,
0106     % leave the current plot as is
0107     hold on;
0108   else
0109     % clear all plot handles
0110     hold off;
0111     
0112     for i = 1:length(p.mesh.plot.Hpatch),
0113       if ishandle(p.mesh.plot.Hpatch{i}),
0114         delete(p.mesh.plot.Hpatch{i});
0115       end
0116     end
0117     p.mesh.plot.Hpatch = cell(0);
0118   end
0119 end
0120 p.mesh.plot.Hpatch{p.mesh.current} = patch('Vertices',vertices,'Faces',faces);
0121 
0122 
0123 
0124 
0125 
0126 axis off tight vis3d
0127 
0128 
0129 set(gcf,'Renderer','opengl'); lighting phong
0130 
0131 % Create the light source
0132 if isfield(p.mesh.plot,'Hlight'),
0133   for i = 1:length(p.mesh.plot.Hlight),
0134     if ishandle(p.mesh.plot.Hlight(i)),
0135       delete(p.mesh.plot.Hlight(i));
0136     end
0137   end
0138   p.mesh.plot.Hlight = [];
0139 end
0140 
0141 H.light(1) = camlight(  0,  0,'local'); % top
0142 H.light(2) = camlight(-75,  0); % left
0143 H.light(3) = camlight( 75,  0); % right
0144 %H.light(4) = camlight(  0,-90,'infinite'); % back
0145 %H.light(5) = camlight(  0, 90,'infinite'); % front
0146 %H.light(6) = camlight(  0,180,'infinite'); % bottom
0147 
0148 % for i = 1:length(H.light),
0149 %   % mute the intensity of the lights
0150 %   color = [.8 1 1] / length(H.light) / 1.2;
0151 %   set(H.light(i),'color',color);
0152 %   %get(H.light(i),'color')
0153 % end
0154 p.mesh.plot.Hlight = H.light;
0155 
0156 
0157 % MATERIAL([ka kd ks n sc]) sets the ambient/diffuse/specular strength,
0158 %    specular exponent and specular color reflectance of the objects.
0159 %reflect = material('dull');
0160 if ~isfield(p.mesh.plot,'reflect')
0161   p.mesh.plot.ambient = 0.5;
0162   p.mesh.plot.diffuse = 0.4;
0163   p.mesh.plot.specular = 0.05;
0164   p.mesh.plot.specexp = 5;
0165   p.mesh.plot.speccolor = 1;
0166 end
0167 if ~isfield(p.mesh.plot,'edgecolor'),
0168   % plot the cortex without edgecolor
0169   switch p.mesh.data.meshtype{p.mesh.current},
0170     case 'cortex',      p.mesh.plot.edgecolor = 'none';
0171     case 'pial',        p.mesh.plot.edgecolor = 'none';
0172     case 'smoothwm',    p.mesh.plot.edgecolor = 'none';
0173     case 'white',       p.mesh.plot.edgecolor = 'none';
0174     otherwise,          p.mesh.plot.edgecolor = '.7 .7 .7';
0175   end
0176 end
0177 
0178 if ~isfield(p.mesh.plot,'facecolor'),
0179   p.mesh.plot.facecolor = '.9 .8 .7';
0180 end
0181 if ~isfield(p.mesh.plot,'facealpha'),
0182   p.mesh.plot.facealpha = '1';
0183 end
0184 
0185 if findstr(p.mesh.plot.edgecolor,'none'),
0186   set(p.mesh.plot.Hpatch{p.mesh.current},'EdgeColor','none');
0187 else
0188   set(p.mesh.plot.Hpatch{p.mesh.current},'EdgeColor',str2num(p.mesh.plot.edgecolor));
0189 end
0190 if findstr(p.mesh.plot.facecolor,'none'),
0191   set(p.mesh.plot.Hpatch{p.mesh.current},'FaceColor','none');
0192 else
0193   set(p.mesh.plot.Hpatch{p.mesh.current},'FaceColor',str2num(p.mesh.plot.facecolor));
0194 end
0195 if p.mesh.plot.facealpha,
0196   set(p.mesh.plot.Hpatch{p.mesh.current},'FaceAlpha',str2num(p.mesh.plot.facealpha));
0197 else
0198   set(p.mesh.plot.Hpatch{p.mesh.current},'FaceAlpha',1);
0199 end
0200 set(p.mesh.plot.Hpatch{p.mesh.current},'AmbientStrength',p.mesh.plot.ambient);
0201 set(p.mesh.plot.Hpatch{p.mesh.current},'DiffuseStrength',p.mesh.plot.diffuse);
0202 set(p.mesh.plot.Hpatch{p.mesh.current},'SpecularStrength',p.mesh.plot.specular);
0203 set(p.mesh.plot.Hpatch{p.mesh.current},'SpecularExponent',p.mesh.plot.specexp);
0204 set(p.mesh.plot.Hpatch{p.mesh.current},'SpecularColorReflectance',p.mesh.plot.speccolor);
0205 
0206 
0207 % -- Plot the electrodes, if scalp and electrodes available
0208 if strmatch('scalp',p.mesh.data.meshtype{p.mesh.current}) > 0,
0209   if ~isempty(p.elec.data),
0210    [p] = elec_plot(p);
0211   end
0212 end
0213 
0214 
0215 
0216 % -- setup the viewer rotation command
0217 
0218 if isequal(exist('mouse_rotate'),2),
0219   userdata = get(p.mesh.plot.fig,'userdata');
0220   if isempty(userdata),
0221     mouse_rotate('init')
0222   else
0223     mouse_rotate('init',get(p.mesh.plot.fig,'userdata'));
0224   end
0225 else
0226   rotate3d;
0227 end
0228 
0229 
0230 if nargout < 1,[p] = []; end
0231 
0232 return
0233 
0234 
0235 
0236 
0237 
0238 
0239 
0240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0242 function[p] = elec_plot(p),
0243 
0244 elecN = mesh_check(p,'elec');
0245 
0246 if isempty(elecN),
0247   warning('MESH_FIT_ELEC: No ''elec'' mesh in p struct.\n');
0248 end
0249 
0250 % Get the scalp vertices and faces
0251 % that correspond to the electrodes
0252 elecvert  = p.mesh.data.vertices{elecN};
0253 
0254 x = elecvert(:,1);
0255 y = elecvert(:,2);
0256 z = elecvert(:,3);
0257 
0258 hold on
0259 scatter3(x,y,z,70,'b','filled');
0260 
0261 %     % add electrode labels
0262 %     for e = 1:length(x),
0263 %         % relocate xyz outside of mesh
0264 %         c = [0 0 0];
0265 %         r = sqrt(x(e).^2 + y(e).^2 + z(e).^2); r = r + (r .* 0.05);
0266 %         v = sphere_project([x(e),y(e),z(e)],r,c);
0267 %         H(e)  = text(v(1),v(2),v(3),p.elec.data.label{e},...
0268 %                      'HorizontalAlignment','center',...
0269 %                      'VerticalAlignment','middle');
0270 %     end
0271 
0272 
0273 %     % plot regions of electrodes in different colors
0274 %     if isfield(p.elec.data,'regions'),
0275 %         if ~isempty(p.elec.data.regions),
0276 %             elecregions = p.elec.data.regions;
0277 %             C = zeros(length(x),1); % generate color array for electrodes
0278 %             colors = rand(length(x),1);
0279 %             for r = 1:length(elecregions),
0280 %                 % assign random colors to electrode indices
0281 %                 C(elecregions(r).elec) = colors(r);
0282 %                 %C(elecregions(r).elec) = r;
0283 %             end
0284 %             map = colormap(colorcube);
0285 %             %map = colormap(hsv);
0286 %             %map = colormap(lines);
0287 %             scatter3(x,y,z,80,C,'b','filled');
0288 %
0289 % %             % attempt to plot an interpolation of these regions
0290 % %             p.colorMap.map = map;
0291 % %             p.volt.data = repmat(C,1,2);
0292 % %             p.volt.timeArray = [1 2];
0293 % %             p.volt.points    = 2
0294 % %             p.volt.sampleHz  = 1000;
0295 % %             p.volt.sampleRate = 1;
0296 % %             p.volt.sampleTime = 1;
0297 % %             p.volt.channels   = length(x);
0298 % %             p.volt.epochStart = 1;
0299 % %             p.volt.epochEnd   = 2;
0300 % %             p.elec.plotSurf     = 0;
0301 % %             p.mesh.plotSurf     = 1;
0302 % %             p.clickTimePoint    = 0;
0303 % %             p.volt.samplePoint         = 1;
0304 % %             eeg_contours_engine(p)
0305 %         end
0306 %     end
0307 
0308 hold off
0309 return

Generated on Mon 15-Aug-2005 15:36:19 by m2html © 2003