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 out = list_shapes(shape)
0055 out = '';
0056 if isempty(shape)
0057 out = who('*','-file','shape_library.mat');
0058 disp('Available shapes:');
0059 else
0060 s = load('shape_library.mat',shape);
0061 try
0062 out = fieldnames(s.(shape));
0063 catch, not_found(shape); return; end
0064 disp(['Shape ' shape ' contains:']);
0065 end
0066 disp(out);
0067
0068 function show_shape(shape)
0069
0070 if isempty(shape)
0071 eidors_msg('SHAPE_LIBRARY: you must specify a shape to show');
0072 return
0073 end
0074 s = load('shape_library.mat',shape);
0075 try
0076 fields = fieldnames(s.(shape));
0077 catch, not_found(shape); return; end
0078 s = s.(shape);
0079
0080 colormap gray
0081 imagesc(s.pic.X, s.pic.Y, s.pic.img);
0082 set(gca,'YDir','normal');
0083 hold all
0084 str = {};
0085 for i = 1:numel(fields)
0086 if strcmp(fields{i},'electrodes'), continue, end
0087 if isnumeric(s.(fields{i})) && size(s.(fields{i}),2)==2
0088 plot(s.(fields{i})(:,1),s.(fields{i})(:,2),'-o','LineWidth',2)
0089 str =[str fields(i)];
0090 end
0091 end
0092 if isfield(s,'electrodes')
0093 el = s.electrodes;
0094 h = plot(el(:,1),el(:,2),'*');
0095 c = get(h, 'Color');
0096 str = [str {'electrodes'}];
0097 for i = 1:length(el)
0098 text(1.1*el(i,1),1.1*el(i,2),num2str(i),...
0099 'HorizontalAlignment','Center','Color',c);
0100 end
0101 end
0102 legend(str,'Location','NorthEastOutside','Interpreter','none');
0103 axis equal
0104 axis tight
0105 title(shape,'Interpreter','none')
0106 xlabel(s.copyright)
0107 hold off
0108
0109
0110
0111 function out = get_shape(shape,fields)
0112 out = {};
0113 if isempty(shape)
0114 eidors_msg('SHAPE_LIBRARY: you must specify a shape to get');
0115 return
0116 end
0117
0118 s = load('shape_library.mat',shape);
0119 if ~isfield(s,shape),not_found(shape); return; end
0120 if isempty(fields)
0121 out =s.(shape);
0122 else
0123 if iscell(fields{1}) fields = fields{1}; end
0124 s = s.(shape);
0125 for i = 1:numel(fields)
0126 out(i) = {s.(fields{i})};
0127 end
0128 if i == 1, out = out{1}; end
0129 end
0130
0131
0132 function add_shape
0133 eidors_msg(['SHAPE_LIBRARY: To contribute a shape contact ' ...
0134 '<a href="http://eidors3d.sourceforge.net/faq.shtml#maintainers">' ...
0135 'EIDORS maintainers</a>']);
0136
0137
0138 function not_found(shape)
0139 eidors_msg(['SHAPE_LIBRARY: Didn''t find shape ' shape]);
0140
0141
0142 function do_unit_test
0143 shape_library('list');
0144 shape_library('list','a-shape-we-dont-have');
0145 shape_library('list','pig_23kg');
0146 shape_library('show');
0147 shape_library('show','pig_23kg');
0148 shape_library('get');
0149 shape_library('get','a-shape-we-dont-have');
0150 shape_library('get','pig_23kg')
0151 shape_library('get','pig_23kg','heart')
0152 shape_library('get','pig_23kg','boundary','lungs')
0153 shape_library('get','pig_23kg',{'boundary','lungs'})
0154 shape_library('add');