Home > vbmeg > external > iso2mesh > savebinstl.m

savebinstl

PURPOSE ^

SYNOPSIS ^

function savebinstl(node,elem,fname,solidname)

DESCRIPTION ^

 savebinstl(node,elem,fname,solidname)

 save a tetrahedral mesh to a binary STL (Standard Tessellation Language) file

 author: Qianqian Fang, <q.fang at neu.edu>
 date: 2012/02/14

 input:
      node: input, surface node list, dimension Nx3
      elem: input, tetrahedral element list; if size(elem,2)==3, it is a surface
      fname: output file name
      solidname: an optional string for the name of the object

 -- 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 savebinstl(node,elem,fname,solidname)
0002 %
0003 % savebinstl(node,elem,fname,solidname)
0004 %
0005 % save a tetrahedral mesh to a binary STL (Standard Tessellation Language) file
0006 %
0007 % author: Qianqian Fang, <q.fang at neu.edu>
0008 % date: 2012/02/14
0009 %
0010 % input:
0011 %      node: input, surface node list, dimension Nx3
0012 %      elem: input, tetrahedral element list; if size(elem,2)==3, it is a surface
0013 %      fname: output file name
0014 %      solidname: an optional string for the name of the object
0015 %
0016 % -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
0017 %
0018 
0019 fid=fopen(fname,'wb');
0020 if(fid==-1)
0021     error('You do not have permission to save mesh files.');
0022 end
0023 
0024 if(nargin<3)
0025     error('incomplete input');
0026 end
0027 if(isempty(node) || size(node,2)<3)
0028     error('invalid node input');
0029 end
0030 
0031 hddata=struct('Ver',1,'Creator','iso2mesh','Date',datestr(now));
0032 if(nargin<4) 
0033     solidname='';
0034 else
0035     hddata.name=solidname;
0036 end
0037 
0038 headerstr=savejson('',hddata);
0039 headerstr=regexprep(headerstr,'[\t\n\r]','');
0040 headerstr=regexprep(headerstr,'": ','":');
0041 if(length(headerstr)>80)
0042     headerstr=headerstr(1:80); % this makes the header an invalid json string
0043 else
0044     headerstr(81)=0;
0045 end
0046 
0047 fwrite(fid,headerstr(1:80),'char');
0048 
0049 if(~isempty(elem))
0050   if(size(elem,2)>=5)
0051     elem(:,5:end)=[];
0052   end
0053   if(size(elem,2)==4)
0054     elem=meshreorient(node,elem);
0055     elem=volface(elem);
0056   end
0057   ev=surfplane(node,elem);
0058   ev=ev(:,1:3)./repmat(sqrt(sum(ev(:,1:3).*ev(:,1:3),2)),1,3);
0059 
0060   len=size(elem,1);
0061   fwrite(fid,len,'uint32');
0062 
0063   for i=1:len
0064       fwrite(fid,[ev(i,:)',node(elem(i,:),1:3)'],'float32');
0065       fwrite(fid,0,'uint16');
0066   end
0067 end
0068 
0069 fclose(fid);

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