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 6626 2023-11-21 15:35:02Z aadler $
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     % shapes are stored in dir shape_library/
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         % remove ".mat"
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 %   s = load('shape_library.mat',shape);
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 %figure
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');% give error
0159     shape_library('list','pig_23kg');
0160     shape_library('show'); % fail gracefully
0161     shape_library('show','pig_23kg');
0162     shape_library('get'); %fail gracefully
0163     shape_library('get','a-shape-we-dont-have');% give error
0164     out=shape_library('get','pig_23kg');% give a struct
0165     out=shape_library('get','pig_23kg','heart');% give array
0166     out=shape_library('get','pig_23kg','boundary','lungs');% give cell array
0167     out=shape_library('get','pig_23kg',{'boundary','lungs'});% give cell array
0168     shape_library('add');

Generated on Sun 29-Dec-2024 11:41:59 by m2html © 2005