0001 function out = shape_library(action, shape, varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 n_IN = nargin;
0036 if n_IN < 2
0037 shape = '';
0038 end
0039
0040 switch action
0041 case 'UNIT_TEST'
0042 do_unit_test;
0043 return
0044 case 'list'
0045 out = list_shapes(shape);
0046 case 'show'
0047 show_shape(shape);
0048 case 'get'
0049 out = get_shape(shape,varargin);
0050 case 'add'
0051 add_shape;
0052 end
0053
0054 function files = get_shape_list()
0055
0056 pth = mfilename('fullpath');
0057 file_list = dir([pth,'/*.mat']);
0058 if isempty(file_list)
0059 error('Can''t find shape_library');
0060 end
0061 for i=1:length(file_list)
0062
0063 files{i,1} = file_list(i).name(1:end-4);
0064 end
0065
0066 function out = list_shapes(shape)
0067 out = get_shape_list();
0068 if isempty(shape)
0069 disp('Available shapes:');
0070 else
0071
0072 s = cell2struct(out,out);
0073 if ~isfield(s,shape)
0074 not_found(shape); out=[]; return;
0075 else
0076 out = get_shape(shape,'');
0077 end
0078 disp(['Shape ' shape ' contains:']);
0079 end
0080 disp(out);
0081
0082 function show_shape(shape)
0083
0084 if isempty(shape)
0085 eidors_msg('SHAPE_LIBRARY: you must specify a shape to show');
0086 return
0087 end
0088 s = get_shape(shape,'');
0089
0090 colormap gray
0091 imagesc(s.pic.X, s.pic.Y, s.pic.img);
0092 set(gca,'YDir','normal');
0093 hold all
0094 str = {};
0095
0096 fields = fieldnames(s);
0097 for i = 1:numel(fields)
0098 if strcmp(fields{i},'electrodes'), continue, end
0099 if isnumeric(s.(fields{i})) && size(s.(fields{i}),2)==2
0100 plot(s.(fields{i})(:,1),s.(fields{i})(:,2),'-o','LineWidth',2)
0101 str =[str fields(i)];
0102 end
0103 end
0104 if isfield(s,'electrodes')
0105 el = s.electrodes;
0106 h = plot(el(:,1),el(:,2),'*');
0107 c = get(h, 'Color');
0108 str = [str {'electrodes'}];
0109 for i = 1:length(el)
0110 text(1.1*el(i,1),1.1*el(i,2),num2str(i),...
0111 'HorizontalAlignment','Center','Color',c);
0112 end
0113 end
0114 legend(str,'Location','NorthEastOutside','Interpreter','none');
0115 axis equal
0116 axis tight
0117 title(shape,'Interpreter','none')
0118 xlabel(s.copyright)
0119 hold off
0120
0121
0122
0123 function out = get_shape(shape,fields)
0124 if isempty(shape)
0125 eidors_msg('SHAPE_LIBRARY: you must specify a shape to get');
0126 out=[];
0127 return
0128 end
0129
0130 try
0131 out=load([mfilename('fullpath'),'/',shape]);
0132 catch
0133 not_found(shape); out=[]; return;
0134 end
0135
0136 if ~isempty(fields)
0137 if iscell(fields{1}) fields = fields{1}; end
0138 s = out; out = {};
0139 for i = 1:numel(fields)
0140 out(i) = {s.(fields{i})};
0141 end
0142 if i == 1, out = out{1}; end
0143 end
0144
0145
0146 function add_shape
0147 eidors_msg(['SHAPE_LIBRARY: To contribute a shape contact ' ...
0148 '<a href="http://eidors3d.sourceforge.net/faq.shtml#maintainers">' ...
0149 'EIDORS maintainers</a>']);
0150
0151
0152 function not_found(shape)
0153 eidors_msg(['SHAPE_LIBRARY: Didn''t find shape ' shape]);
0154
0155
0156 function do_unit_test
0157 shape_library('list');
0158 shape_library('list','a-shape-we-dont-have');
0159 shape_library('list','pig_23kg');
0160 shape_library('show');
0161 shape_library('show','pig_23kg');
0162 shape_library('get');
0163 shape_library('get','a-shape-we-dont-have');
0164 out=shape_library('get','pig_23kg');
0165 out=shape_library('get','pig_23kg','heart');
0166 out=shape_library('get','pig_23kg','boundary','lungs');
0167 out=shape_library('get','pig_23kg',{'boundary','lungs'});
0168 shape_library('add');