function triangle2EasyMesh(name,flag)
%triangle2EasyMesh(name,flag)
%   triangle2EasyMesh("mytest.1")  transform triangle output to EasyMesh output
%       using the result of the Cuthill--McKee algorithm
%   triangle2EasyMesh("mytest.1",1)  transform triangle output to EasyMesh output
%       using the the original node numbering

[infile,msg]=fopen(strcat(name,".node"),"r");
Nnodes=fscanf(infile,"%d",1); % number of nodes
fclose(infile);

map=zeros(Nnodes,1);
if (nargin>1)
   map=linspace(1,Nnodes,Nnodes);
else
   map=zeros(1,Nnodes);
   [fmap,msg]=fopen(strcat(name,".map"),"r");
   tmp=fscanf(fmap,"%d",1);
   for i=1:Nnodes
      tmp=fscanf(fmap,"%d",1); 
      map(i)=fscanf(fmap,"%d",1);
   endfor;
   fclose(fmap);
endif

map2=map;
for i=1:Nnodes
  map2(map(i))=i;
endfor

[infile,msg]=fopen(strcat(name,".node"),"r");
  tmp=fscanf(infile,"%d",[1,4]);
[outfile,msg]=fopen(strcat(name,".n"),"w");
fprintf(outfile," %d \n",Nnodes);
x=zeros(1,Nnodes);y=x;t=x;
for i=1:Nnodes
  tmp=fscanf(infile,"%d",1);
  x(i)=fscanf(infile,"%f",1);
  y(i)=fscanf(infile,"%f",1);
  t(i)=fscanf(infile,"%d",1);
endfor
fclose(infile);
for i=1:Nnodes
  fprintf(outfile," %d: %f %f %d \n",...
    i-1,x(map2(i)),y(map2(i)),t(map2(i)));
endfor
fclose(outfile);

[infile,msg]=fopen(strcat(name,".ele"),"r");
  Nele=fscanf(infile,"%d",1);
  tmp=fscanf(infile,"%d",[1,2]);
[outfile,msg]=fopen(strcat(name,".e"),"w");
fprintf(outfile," %d \n",Nele);
for i=1:Nele
  tmp=fscanf(infile,"%d",1);
  ele=fscanf(infile,"%d",[1,3]);
  fprintf(outfile," %d: %d %d %d 0 0 0 0 0 0 0.0 0.0\n",...
         i-1,map(ele(1))-1,map(ele(2))-1,map(ele(3))-1);
endfor
fclose(infile);
fclose(outfile);

[infile,msg]=fopen(strcat(name,".sparse"),"r");
  Nele=fscanf(infile,"%d",1);
  Nedge=fscanf(infile,"%d",1);
[outfile,msg]=fopen(strcat(name,".s"),"w");
fprintf(outfile," %d \n",Nedge);
for i=1:Nedge
  edge=fscanf(infile,"%d",[1,2]);
  fprintf(outfile," %d: %d %d 0 0 0\n",i-1,map(edge(1))-1,map(edge(2))-1);
endfor
fclose(infile);
fclose(outfile);


endfunction




