Home > vbmeg > external > iso2mesh > savenirfast.m

savenirfast

PURPOSE ^

SYNOPSIS ^

function savenirfast(v,f,filestub, nodeseg, nodeprop, proptype)

DESCRIPTION ^

 savenirfast(nirfaststruct,filestub)
    or
 savenirfast(v,f,filestub, nodeseg, proptype, proptype)

 save a tetrahedral or surface mesh and associated properties to NIRFAST format

 author: Qianqian Fang, <q.fang at neu.edu>

 input:
      nirfaststruct: a structure storing the NIRFAST mesh data, type 
         'help readnirfast' to read more; alternatively one can use:
      v: input, node list, the first 3 columns are the x/y/z positions,
         the remaining columns are combined with nodeprop as node-based
         (optical) parameters
      f: input, tetrahedral or surface element list, dimension (ne,3)
      filestub: output file stub, output will include multiple files
          filestub.node: node file
          filestub.elem: element file to store the surface or tet mesh
          filestub.param: parameter file
          filestub.region: node label file
      nodeseg: optional, an integer label field to group nodes into
         segmentations, same length as v, number starting from 0; or empty
      nodeprop: optional, additional nodal parameters, typically defined
         as mua (1/mm), musp (1/mm) and refractive index (n)l; row number
         equals to that of v, column number is user-defined
      proptype: optional, the type of the node-property. by default it is 
         'stnd' - for standard properties; one can also define multi-row
         header using a cell-array.

 example:
    [node,face,elem]=meshabox([0 0 0],[10 10 10],0.3,1);
    savenirfast(node,elem,'test', [], ones(size(node)), 'user');
    mymesh=readnirfast('test')
    plotmesh([mymesh.nodes mymesh.bndvtx], mymesh.elements,'x>5')

 -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function savenirfast(v,f,filestub, nodeseg, nodeprop, proptype)
0002 %
0003 % savenirfast(nirfaststruct,filestub)
0004 %    or
0005 % savenirfast(v,f,filestub, nodeseg, proptype, proptype)
0006 %
0007 % save a tetrahedral or surface mesh and associated properties to NIRFAST format
0008 %
0009 % author: Qianqian Fang, <q.fang at neu.edu>
0010 %
0011 % input:
0012 %      nirfaststruct: a structure storing the NIRFAST mesh data, type
0013 %         'help readnirfast' to read more; alternatively one can use:
0014 %      v: input, node list, the first 3 columns are the x/y/z positions,
0015 %         the remaining columns are combined with nodeprop as node-based
0016 %         (optical) parameters
0017 %      f: input, tetrahedral or surface element list, dimension (ne,3)
0018 %      filestub: output file stub, output will include multiple files
0019 %          filestub.node: node file
0020 %          filestub.elem: element file to store the surface or tet mesh
0021 %          filestub.param: parameter file
0022 %          filestub.region: node label file
0023 %      nodeseg: optional, an integer label field to group nodes into
0024 %         segmentations, same length as v, number starting from 0; or empty
0025 %      nodeprop: optional, additional nodal parameters, typically defined
0026 %         as mua (1/mm), musp (1/mm) and refractive index (n)l; row number
0027 %         equals to that of v, column number is user-defined
0028 %      proptype: optional, the type of the node-property. by default it is
0029 %         'stnd' - for standard properties; one can also define multi-row
0030 %         header using a cell-array.
0031 %
0032 % example:
0033 %    [node,face,elem]=meshabox([0 0 0],[10 10 10],0.3,1);
0034 %    savenirfast(node,elem,'test', [], ones(size(node)), 'user');
0035 %    mymesh=readnirfast('test')
0036 %    plotmesh([mymesh.nodes mymesh.bndvtx], mymesh.elements,'x>5')
0037 %
0038 % -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
0039 %
0040 
0041 if(nargin<2)
0042     error('you must provide at least 2 inputs');
0043 end
0044 
0045 if(nargin==2)
0046     filestub=f;
0047     node=v.nodes;
0048     f=v.elements;
0049     proptype=v.type;
0050     if(isfield(v,'region'))
0051         nodeseg=v.region;
0052     end
0053     if(isfield(v,'mua'))
0054         nodeprop=[v.mua v.mus v.ri];
0055     end
0056     if(isfield(v,'bndvtx'))
0057         isboundary=v.bndvtx;
0058     end
0059     v=node;
0060 end
0061 
0062 if(size(v,2)>3)
0063     if(nargin>4)
0064         nodeprop=[v(:,4:end) nodeprop];
0065     else
0066         nodeprop=v(:,4:end);
0067     end
0068 else
0069     if(nargin<5)
0070         nodeprop=[];
0071     end
0072 end
0073 
0074 if(nargin<6)
0075     proptype='stnd';
0076 end
0077 
0078 if(nargin<4 || isempty(nodeseg))
0079     nodeseg=zeros(size(v,1),1);
0080 end
0081 
0082 if(nargin<6)
0083     proptype='stnd';
0084 end
0085 
0086 if(size(f,2)>4)
0087     f(:,5:end)=[];
0088 end
0089 
0090 if(size(v,2)<3)
0091     error('v must contain at least 3 columns, and f must have at least 4 columns');
0092 end
0093 
0094 if(~exist('isboundary','var'))
0095     face=surfedge(f);
0096     isboundary=ismember(1:size(v,1), face(:));
0097 end
0098 
0099 fid=fopen([filestub,'.node'],'wt');
0100 if(fid==-1)
0101     error('Saving node file failed, check permission or disk space.');
0102 end
0103 fprintf(fid,'%d\t%.16f\t%.16f\t%.16f\n',[isboundary(:) v(:,1:3)]');
0104 fclose(fid);
0105 
0106 if(size(f,2)<2 || size(f,2)>4)
0107     error('element list f must contain 3 or 4 columns');
0108 end
0109 
0110 fid=fopen([filestub,'.elem'],'wt');
0111 if(fid==-1)
0112     error('Saving elem file failed, check permission or disk space.');
0113 end
0114 fprintf(fid,'%6d\t%6d\t%6d\t%6d\t\n',f');
0115 fclose(fid);
0116 
0117 if(~isempty(nodeseg))
0118     if(numel(nodeseg)~=size(v,1))
0119         error('nodeseg must have the same length as v');
0120     end
0121     fid=fopen([filestub,'.region'],'wt');
0122     if(fid==-1)
0123         error('Saving regin file failed, check permission or disk space.');
0124     end
0125     fprintf(fid,'%d\n',nodeseg(:));
0126     fclose(fid);
0127 end
0128 
0129 if(~isempty(nodeprop))
0130     if(size(nodeprop,1)~=size(v,1))
0131         error('nodeprop must have the same row number as v');
0132     end
0133     fid=fopen([filestub,'.param'],'wt');
0134     if(fid==-1)
0135         error('Saving param file failed, check permission or disk space.');
0136     end
0137     if(iscell(proptype))
0138         proptype=strjoin(proptype,'\n');
0139     end
0140     fprintf(fid,[proptype '\n']);
0141     fprintf(fid,[repmat('%.16f\t', 1, size(nodeprop,2)) '\n'],nodeprop');
0142     fclose(fid);
0143 end

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005