0001 function stl_write(fv, name)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
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);
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
0038
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;