0001 function mdl = gmsh_stl2tet(stlfile, mesh_size, extra, nopt)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 if nargin < 4, nopt = 0; end
0034 if nargin < 3, extra = []; end
0035 if nargin < 2, mesh_size = []; end
0036
0037 if isstruct(stlfile)
0038 try
0039 name = stlfile.name;
0040 catch
0041 name = 'gmsh_stl2tet';
0042 end
0043 try
0044 opt.cache_obj{1} = stlfile.nodes;
0045 if isfield(stlfile, 'boundary')
0046 opt.cache_obj{2} = stlfile.boundary;
0047 else
0048 opt.cache_obj{2} = stlfile.elems;
0049 end
0050 catch
0051 opt.cache_obj = {stlfile.vertices, stlfile.faces};
0052 end
0053 opt.cache_obj{3} = mesh_size;
0054 opt.cache_obj{4} = extra;
0055 opt.cache_obj{5} = nopt;
0056 mdl = eidors_cache(@do_gmsh_stl2tet,{stlfile, mesh_size, extra, nopt}, opt);
0057 else
0058 [~, name, ext] = fileparts(stlfile); name = [name ext];
0059 mdl = do_gmsh_stl2tet(stlfile, mesh_size, extra, nopt);
0060 end
0061
0062 mdl = eidors_obj('fwd_model', mdl, 'name', name);
0063
0064
0065 function mdl = do_gmsh_stl2tet(stlfile, mesh_size, extra, nopt)
0066 if isstruct(stlfile)
0067 stem = tempname;
0068 stl_write(stlfile, [stem, '.stl'])
0069 stlname = [stem '.stl'];
0070 else
0071 stem = strrep(stlfile,'.stl','');
0072 stlname = stlfile;
0073 end
0074
0075 fid = fopen([stem '.geo'],'w');
0076 fprintf(fid,'General.Terminal=1;\n');
0077 fprintf(fid,'Merge "%s";\n',stlname);
0078 fprintf(fid,'Surface Loop(1) = {1};\n');
0079 fprintf(fid,'Volume(2) = {1};\n');
0080 fprintf(fid,'Physical Volume(3) = {2};\n');
0081
0082 if numel(mesh_size) == 1
0083 fprintf(fid,'Mesh.MeshSizeMax = %f;\n', mesh_size);
0084 elseif numel(mesh_size) == 2
0085
0086 fprintf(fid, 'Field[1] = Extend;\n');
0087 fprintf(fid, 'Field[1].SurfacesList = {1};\n');
0088 fprintf(fid, 'Field[1].SizeMax = %g;\n', mesh_size(1));
0089 fprintf(fid, 'Field[1].DistMax = %g;\n', mesh_size(2));
0090 fprintf(fid, 'Field[1].Power = 1;\n');
0091 fprintf(fid, 'Background Field = 1;\n');
0092 fprintf(fid, 'Mesh.MeshSizeExtendFromBoundary = 0;\n');
0093 fprintf(fid, 'Mesh.Algorithm3D=10;\n');
0094 end
0095 if numel(mesh_size) < 2
0096 fprintf(fid,'Mesh.Algorithm3D=4;\n');
0097 end
0098 fprintf(fid,'Mesh.OptimizeNetgen=1;\n');
0099 fprintf(fid,'Mesh 3;\n');
0100
0101
0102 for i = 1:nopt
0103 fprintf(fid, 'OptimizeMesh "Gmsh";\n');
0104 fprintf(fid, 'OptimizeMesh "Netgen";\n');
0105 end
0106
0107
0108 fprintf(fid, 'OptimizeMesh "Gmsh";\n');
0109 fclose(fid);
0110
0111 call_gmsh([stem '.geo'], extra);
0112
0113 mdl = gmsh_mk_fwd_model([stem '.msh'],[],[],[]);
0114
0115 delete([stem '.geo']);
0116 delete([stem '.msh']);
0117 if isstruct(stlfile)
0118 delete(stlname);
0119 end