function mesh = fixpos(mesh, recurse)

% FIXPOS helper function to ensure that meshes are described properly

if nargin==1
  recurse = true;
end

if isa(mesh, 'delaunayTriangulation')
  % convert to structure, otherwise the code below won't work properly
  ws = ft_warning('off', 'MATLAB:structOnObject');
  mesh = struct(mesh);
  ft_warning(ws);
end

if isnumeric(mesh) && size(mesh,2)==3
  % convert set of points into a mesh structure
  mesh = struct('pos', mesh);
end

if ~isa(mesh, 'struct')
  return
end

if numel(mesh)>1
  % loop over all individual elements
  clear tmp
  for i=1:numel(mesh)
    % this is to prevent an "Subscripted assignment between dissimilar structures" error
    tmp(i) = fixpos(mesh(i));
  end
  mesh = tmp;
  clear tmp
  return
end

% convert from MATLAB delaunayTriangulation output to FieldTrip convention
if isfield(mesh, 'Points') && isfield(mesh, 'ConnectivityList')
  mesh.pos = mesh.Points;
  switch size(mesh.ConnectivityList,2)
    case 2
      mesh.edge = mesh.ConnectivityList;
    case 3
      mesh.tri = mesh.ConnectivityList;
    case 4
      mesh.tet = mesh.ConnectivityList;
    case 8
      mesh.hex = mesh.ConnectivityList;
    otherwise
      ft_error('unsupported ConnectivityList')
  end % switch
  mesh = removefields(mesh, {'Points', 'ConnectivityList', 'Constraints', 'UnderlyingObj'});
end

% convert from BrainStorm/MNE to FieldTrip convention
if isfield(mesh, 'vertices') && isfield(mesh, 'faces')
  mesh.pos = mesh.vertices;
  mesh.tri = mesh.faces;
  mesh = rmfield(mesh, {'faces', 'vertices'});
elseif isfield(mesh, 'Vertices') && isfield(mesh, 'Faces')
  mesh.pos = mesh.Vertices;
  mesh.tri = mesh.Faces;
  mesh = rmfield(mesh, {'Faces', 'Vertices'});
end

% convert from GMesh/SimNIBS to FieldTrip convention
if isfield(mesh, 'nodes') && isfield(mesh, 'node_data')
  mesh.pos = mesh.nodes;
  mesh = rmfield(mesh, 'nodes');
  if isfield(mesh, 'triangles')
    mesh.tri = mesh.triangles;
    mesh = rmfield(mesh, 'triangles');
  end
  if isfield(mesh, 'tetrahedra')
    mesh.tet = mesh.tetrahedra;
    mesh = rmfield(mesh, 'tetrahedra');
  end
end

% replace pnt by pos
if isfield(mesh, 'pnt')
  mesh.pos = mesh.pnt;
  mesh = rmfield(mesh, 'pnt');
end

if recurse<3
  % recurse into substructures, not too deep
  fn = fieldnames(mesh);
  fn = setdiff(fn, {'cfg', 'hdr'}); % don't recurse into the cfg or hdr structure
  for i=1:length(fn)
    if isstruct(mesh.(fn{i}))
      mesh.(fn{i}) = fixpos(mesh.(fn{i}), recurse+1);
    end
  end
end
