function [nodes,elem,edges]=ReadMeshTriangleM(name)
%[...]=ReadMeshTriangleM()
%   reads the information of triangle output into Octave data structures
%
%[nodes,elem,edges]=ReadMeshTriangleM(name)
%
%   the files name.node filename.ele filename.poly are read
%   if available  the file name.map (generated by CuthillMcKee) is read too
%   these files have to be generated first by triangle
%   triangle reads its inputfile for the decription of the domain
%   The boundary markers  lead to the following boundary conditions
%     1 leads to a Dirichlet condition
%     2 leads to a Neumann condition
%
%the matrices nodes elem edges  describe the mesh 
%   
%nodes contains the x and y coordinates of the nodes and the material 
%   type at each node, i.e. nodes =[x1,y1,m1;x2,y2,m2;...;xn,yn,mn]
%elem contains the information about the elements
%   One row shows the numbers of the three nodes forming the element and
%   then the material type of the element
%edges contains the information about the boundary segments
%   One row shows the numbers of the two nodes forming the segment and
%   then the boundary markes is shown (1=Dirichlet, 2=Neumann)
%
%Currently material information is ignored
%
%see also ShowMesh, FEMEquation, FEMSolve, ShowSolution, FEMValue

[fid,msg]=fopen(strcat(name,".map"),"r");
if (fid == -1)
  nomap=true;
else
  nomap=false;
  n=fscanf(fid,"%d",1);
  map=zeros(n,1);
  for k=1:n;
    tmp=fscanf(fid,"%d",[1,1]);
    map(k)=fscanf(fid,"%e",1);
  endfor
fclose(fid);
endif


[fid,msg]=fopen(strcat(name,".node"),"r");
n=fscanf(fid,"%d",1);
if (nomap==true) map=linspace(1,n,n);endif
nodes=zeros(n,3);
tmp=fscanf(fid,"%d",[1,3]);
for k=1:n;
  tmp=fscanf(fid,"%d",1);
  nodes(map(k),:)=fscanf(fid,"%e",[1,3]);
endfor
fclose(fid);


[fid,msg]=fopen(strcat(name,".ele"),"r");
n=fscanf(fid,"%d",1);
elem=zeros(n,4);
tmp=fscanf(fid,"%d",[1,2]);
for k=1:n;
  tmp=fscanf(fid,"%d",1);
  elem(k,1:3)=fscanf(fid,"%e",[1,3]);
  for kk=1:3 elem(k,kk) = map(elem(k,kk));endfor
endfor
fclose(fid);

[fid,msg]=fopen(strcat(name,".poly"),"r");
tmp=fscanf(fid,"%d",[1,4]);
n=fscanf(fid,"%d",1);
edges=zeros(n,3);
tmp=fscanf(fid,"%d",[1,1]);
for k=1:n;
  tmp=fscanf(fid,"%d",[1,1]);
  tmp=fscanf(fid,"%1c",[1,1]);
  edges(k,:)=fscanf(fid,"%e",[1,3]);
  for kk=1:2 edges(k,kk) = map(edges(k,kk));endfor
endfor
fclose(fid);

endfunction




