stl_write

PURPOSE ^

STL_WRITE Create a text STL file from a patch struct

SYNOPSIS ^

function stl_write(fv, name)

DESCRIPTION ^

STL_WRITE Create a text STL file from a patch struct
 stl_write(fv, name) where:
  fv is face-vertex structure array (fv.faces, fv.vertices)
  name is the file name (character string), if no extension give, 'stl'
  assumed.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function stl_write(fv, name)
0002 %STL_WRITE Create a text STL file from a patch struct
0003 % stl_write(fv, name) where:
0004 %  fv is face-vertex structure array (fv.faces, fv.vertices)
0005 %  name is the file name (character string), if no extension give, 'stl'
0006 %  assumed.
0007 
0008 % (C) 2006 Eric Carlson: Public Domain
0009 % Adapted by Bartlomiej Grychtol from:
0010 % http://www.mathworks.com/matlabcentral/newsreader/view_thread/126215
0011 % $Id: stl_write.m 3227 2012-06-29 21:53:39Z aadler $
0012 
0013 if (strcmp(name((end-3):end), '.stl'))
0014      label = name(1:(end-4));
0015 else
0016      label = name;
0017      name = sprintf('%s.stl', name);
0018 end
0019 v1 = fv.vertices(fv.faces(:,2),:)-fv.vertices(fv.faces(:,1),:);
0020 v2 = fv.vertices(fv.faces(:,3),:)-fv.vertices(fv.faces(:,2),:);
0021 Norms = cross3(v1,v2);
0022 clear v1 v2
0023 v1(:,1:3) = fv.vertices(fv.faces(:,1),1:3);
0024 v2(:,1:3) = fv.vertices(fv.faces(:,2),1:3);
0025 v3(:,1:3) = fv.vertices(fv.faces(:,3),1:3);
0026 fid = fopen(name,'w');
0027 fprintf(fid,'solid %s\n',label);
0028 nf = length(fv.faces); %k = (1:nf)';
0029 for k = 1:nf
0030     fprintf(fid,'facet normal %5.5f %5.5f %5.5f\n outer loop\n vertex %5.5f %5.5f %5.5f\n vertex %5.5f %5.5f %5.5f\n vertex %5.5f %5.5f %5.5f\nendloop\n endfacet\n', ...
0031         Norms(k,1),Norms(k,2),Norms(k,3), v1(k,1), v1(k,2), v1(k,3),v2(k,1), v2(k,2), v2(k,3),v3(k,1), v3(k,2), v3(k,3) );
0032 end
0033 fprintf(fid,'endsolid %s\n',label);
0034 fclose(fid);
0035 
0036 function M=cross3(r,F)
0037 % function to calculate normalized cross product rxF/|rxF|
0038 % handles (same-size) arrays (n by 3) for r and F
0039 %
0040        M = [(r(:,2).*F(:,3) - r(:,3).*F(:,2)) ...
0041             (r(:,3).*F(:,1) - r(:,1).*F(:,3)) ...
0042             (r(:,1).*F(:,2) - r(:,2).*F(:,1))];
0043        M_mag = sqrt(sum((M.*M)')');
0044        M(:,1) = M(:,1)./M_mag;
0045        M(:,2) = M(:,2)./M_mag;
0046        M(:,3) = M(:,3)./M_mag;

Generated on Tue 31-Dec-2019 17:03:26 by m2html © 2005