


GMSH_STL2TET creates a tetrahedral mesh from an stl file
mdl = gmsh_stl2tet(stlfile, maxh, extra) where:
mdl - EIDORS fwd_model struct
stlfile - either:
- a path to an stl file
OR
- a struct with .vertices and .faces
maxh - maximum edge length (default: coarse mesh)
extra - extra command line options to gmsh
If stlfile is a struct, stl_write will be called first and an STL file
written in a temporary location.
CASHING: Calls are cashed iff stlfile is a struct.
NOTE: Only one surface per file is allowed.
See also CALL_GMSH, STL_WRITE


0001 function mdl = gmsh_stl2tet(stlfile, maxh, extra) 0002 %GMSH_STL2TET creates a tetrahedral mesh from an stl file 0003 % mdl = gmsh_stl2tet(stlfile, maxh, extra) where: 0004 % mdl - EIDORS fwd_model struct 0005 % stlfile - either: 0006 % - a path to an stl file 0007 % OR 0008 % - a struct with .vertices and .faces 0009 % maxh - maximum edge length (default: coarse mesh) 0010 % extra - extra command line options to gmsh 0011 % 0012 % If stlfile is a struct, stl_write will be called first and an STL file 0013 % written in a temporary location. 0014 % 0015 % CASHING: Calls are cashed iff stlfile is a struct. 0016 % 0017 % NOTE: Only one surface per file is allowed. 0018 % 0019 % See also CALL_GMSH, STL_WRITE 0020 0021 % (C) Bartlomiej Grychtol, 2012-2021. 0022 % $Id: gmsh_stl2tet.m 6276 2022-04-07 07:41:51Z bgrychtol $ 0023 0024 if nargin < 3 0025 extra = []; 0026 end 0027 if nargin > 1 && ~isempty(maxh) 0028 extra = [' -clmax ' num2str(maxh), ' ', extra]; 0029 end 0030 0031 if isstruct(stlfile) 0032 opt.cache_obj = {stlfile.vertices, stlfile.faces}; 0033 mdl = eidors_cache(@do_gmsh_stl2tet,{stlfile, extra}, opt); 0034 else 0035 mdl = do_gmsh_stl2tet(stlfile, extra); 0036 end 0037 0038 0039 0040 function mdl = do_gmsh_stl2tet(stlfile, extra) 0041 if isstruct(stlfile) 0042 stem = tempname; 0043 stl_write(stlfile, [stem, '.stl']) 0044 stlname = [stem '.stl']; 0045 else 0046 stem = strrep(stlfile,'.stl',''); 0047 stlname = stlfile; 0048 end 0049 %TODO: Some of this could be exposed as options (Algorithm, Optimize, ...) 0050 fid = fopen([stem '.geo'],'w'); 0051 fprintf(fid,'Merge "%s";\n',stlname); 0052 fprintf(fid,'Surface Loop(1) = {1};\n'); 0053 fprintf(fid,'Volume(2) = {1};\n'); 0054 fprintf(fid,'Physical Volume(3) = {2};\n'); 0055 fprintf(fid,'Mesh.Algorithm3D=4;\n'); %1=delaunay (tetgen) and 4=frontal (netgen) 0056 fprintf(fid,'Mesh.OptimizeNetgen=1;\n'); 0057 fclose(fid); 0058 0059 call_gmsh([stem '.geo'], 3, extra); 0060 0061 mdl = gmsh_mk_fwd_model([stem '.msh'],[],[],[]); 0062 0063 delete([stem '.geo']); 0064 delete([stem '.msh']); 0065 if isstruct(stlfile) 0066 delete(stlname); 0067 end