shape_library

PURPOSE ^

SHAPE_LIBRARY Common shapes for models

SYNOPSIS ^

function out = shape_library(action, shape, varargin)

DESCRIPTION ^

SHAPE_LIBRARY Common shapes for models
  SHAPE_LIBRARY('list') lists available shapes (strings)
  
  SHAPE_LIBRARY('list',SHAPE) lists available outlines for the given shape
  
  SHAPE_LIBRARY('show', SHAPE) plots available outlines for the given shape
  overlayed on the image from which they were segmented

  SHAPE_LIBRARY('get',SHAPE) returns a struct with all the information for 
  the given shape, including at least the fields:
     .boundary  - a set of [x y] points defining the boundary
     .pic.img   - an image from which the contours were extracted
     .pic.X 
     .pic.Y     - coordinate vectors to use with IMAGESC
     .copyright - source, authors and license
  Additional outlines of internal objects (e.g. lungs) are defined in
  separate fields akin to .boundary above.

  SHAPE_LIBRARY('get', SHAPE, FIELD [,FIELD2,..]) 
  SHAPE_LIBRARY('get', SHAPE, {FIELD [,FIELD2,...]})
  returns a requested outline FIELD from SHAPE. If more than one FIELD is 
  requested, a cell array of [x y] matries is returned
 
  SHAPE_LIBRARY('add') displays information on adding new shapes

EXAMPLES:
 shape_library('list');
 shape_library('list','pig_23kg');
 shape_library('get','pig_23kg','boundary','lungs')

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function out = shape_library(action, shape, varargin)
0002 %SHAPE_LIBRARY Common shapes for models
0003 %  SHAPE_LIBRARY('list') lists available shapes (strings)
0004 %
0005 %  SHAPE_LIBRARY('list',SHAPE) lists available outlines for the given shape
0006 %
0007 %  SHAPE_LIBRARY('show', SHAPE) plots available outlines for the given shape
0008 %  overlayed on the image from which they were segmented
0009 %
0010 %  SHAPE_LIBRARY('get',SHAPE) returns a struct with all the information for
0011 %  the given shape, including at least the fields:
0012 %     .boundary  - a set of [x y] points defining the boundary
0013 %     .pic.img   - an image from which the contours were extracted
0014 %     .pic.X
0015 %     .pic.Y     - coordinate vectors to use with IMAGESC
0016 %     .copyright - source, authors and license
0017 %  Additional outlines of internal objects (e.g. lungs) are defined in
0018 %  separate fields akin to .boundary above.
0019 %
0020 %  SHAPE_LIBRARY('get', SHAPE, FIELD [,FIELD2,..])
0021 %  SHAPE_LIBRARY('get', SHAPE, {FIELD [,FIELD2,...]})
0022 %  returns a requested outline FIELD from SHAPE. If more than one FIELD is
0023 %  requested, a cell array of [x y] matries is returned
0024 %
0025 %  SHAPE_LIBRARY('add') displays information on adding new shapes
0026 %
0027 %EXAMPLES:
0028 % shape_library('list');
0029 % shape_library('list','pig_23kg');
0030 % shape_library('get','pig_23kg','boundary','lungs')
0031 
0032 % (C) 2011 Bartlomiej Grychtol. License: GPL version 2 or version 3
0033 % $Id: shape_library.m 3005 2012-06-06 13:24:34Z bgrychtol $
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 %figure
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');% give error
0145     shape_library('list','pig_23kg');
0146     shape_library('show'); % fail gracefully
0147     shape_library('show','pig_23kg');
0148     shape_library('get'); %fail gracefully
0149     shape_library('get','a-shape-we-dont-have');% give error
0150     shape_library('get','pig_23kg') % give a struct
0151     shape_library('get','pig_23kg','heart') % give array
0152     shape_library('get','pig_23kg','boundary','lungs') % give cell array
0153     shape_library('get','pig_23kg',{'boundary','lungs'}) % give cell array
0154     shape_library('add');

Generated on Fri 30-Dec-2022 19:44:54 by m2html © 2005