function mesh=Initial_mesh_From_Tool(Domain,hsize)

%mesh=Initial_Mesh_From_Tool(p,e,t) %,e,Dirichlet,Neumann)

% INITIAL_MESH_From_Tool generates initial mesh data structure from the 
% information obtained by the pdetool.
% 
% USAGE
%    mesh = Initial_Mesh_From_Tool(p,e,t)
%
% INPUT 
%       p:  array of nodes (required) 
%       t:  array of element indices (required)
%       e:  array for boundary information
%
% OUTPUT
%    mesh:  current mesh
%
% Hehu Xie 05-17-2009
[p,e,t] = initmesh(Domain,'Hmax',hsize);
node = p';
elem = t(1:3,:)';
edge_length = [sum((node(elem(:,3),:)-node(elem(:,2),:)).^2,2),...
               sum((node(elem(:,1),:)-node(elem(:,3),:)).^2,2),...
               sum((node(elem(:,2),:)-node(elem(:,1),:)).^2,2)];
edge_length = edge_length';
[v,Index] = max(edge_length);
[v,Ind_2] = find(Index==2);
elem(Ind_2,:) = elem(Ind_2,[2,3,1]);
[v,Ind_3] = find(Index==3);
elem(Ind_3,:) = elem(Ind_3,[3,1,2]);

bd_Ind = find(e(7,:)==0);
Dirichlet = e(1:2,bd_Ind)';
%Dirichlet = e(1:2,:)';
mesh = Initial_FEM_matlab(node,elem,Dirichlet,[]);               
%--------------------------------------------------------------------------
% End of function INITMESH
%--------------------------------------------------------------------------