function export_medit_mesh(mesh, info, ids, fname)
%EXPORT_INRIA_MESH Summary of this function goes here
%   Detailed explanation goes here

if isempty(ids)
    ids = struct( ...
        'vertices', zeros(size(mesh.Nodes,1),1), ...
        'elems', zeros(size(mesh.Elements,1),1) ...
    );
end

fid = fopen(fname, 'wt');

fprintf(fid, ' MeshVersionFormatted %d\n', info.version);
fprintf(fid, ' Dimension\n %d\n', info.dimension);

% Print vertices
fprintf(fid, ' Vertices\n %d\n', size(mesh.Nodes, 1));
for i = 1 : size(mesh.Nodes, 1)
    fprintf(fid, '%.8f %.8f %.8f %d\n', ...
        mesh.Nodes(i, 2), mesh.Nodes(i, 3), mesh.Nodes(i, 4), ids.vertices(i));
end

% Print elements
elems = {23, 'Triangles'; 34 'Tetrahedra'};

for i_type = 1 : size(elems, 1)
    idxs = find(mesh.Elements(:,2) == elems{i_type, 1});
    if isempty(idxs)
        continue
    end
    
    fprintf(fid, ' %s\n %d\n', elems{i_type, 2}, numel(idxs));
    for i = 1 : numel(idxs)
        for j = 1 : mod(elems{i_type, 1}, 10)
            fprintf(fid, ' %d', mesh.Elements(idxs(i), 4 + j));
        end
        fprintf(fid, ' %d\n', ids.elems(idxs(i)));
    end
end

% Print closing
fprintf(fid, ' End\n');

fclose(fid);


end

