function handles = MakeMesh(hObject, eventdata, handles)



%handles.force.mesh.angle_number = 30;

handles = ReInterpMesh(hObject, eventdata, handles);



ANGLE_NUMBER = handles.force.mesh.angle_number;
num_val      = handles.force.vertex_count;
num_vert     = ANGLE_NUMBER*(num_val-2)+2;
num_faces    = 2*ANGLE_NUMBER*(num_val-2);

handles.force.mesh.r     = zeros(num_vert, 3);
handles.force.mesh.faces = zeros(num_faces,3);


handles.force.mesh.r(1,       :) = [handles.force.r(1,1), handles.force.r(1,1), handles.force.r(1,2)];
handles.force.mesh.r(num_vert,:) = [handles.force.r(num_val,1), handles.force.r(num_val,1), handles.force.r(num_val,2)];
    

for j = 1:ANGLE_NUMBER
    
    phi = j/ANGLE_NUMBER*2*pi;
    handles.force.mesh.r((2+(j-1)*(num_val-2)):(1+(j)*(num_val-2)),:) = [cos(phi)*handles.force.r(2:(num_val-1),1), sin(phi)*handles.force.r(2:(num_val-1),1),handles.force.r(2:(num_val-1),2)];
end;
  

faces_count = 1;

  for j =  0:(ANGLE_NUMBER-2) 
    handles.force.mesh.faces(faces_count,:) = [1, (j+1)*(num_val-2)+2,(j)*(num_val-2)+2];
    faces_count = faces_count + 1;
    for i = 1:(num_val-3)
    
        handles.force.mesh.faces(faces_count,:) = [j*(num_val-2)+i+1, (j+1)*(num_val-2)+i+2,j*(num_val-2)+i+2];
        faces_count = faces_count + 1;
       
	    handles.force.mesh.faces(faces_count,:) = [(j+1)*(num_val-2)+i+1,(j+1)*(num_val-2)+i+2,j*(num_val-2)+i+1];
        faces_count = faces_count + 1;
        
    end;
   i = num_val-2;

	handles.force.mesh.faces(faces_count,:) = [(j)*(num_val-2)+1+i,(j+1)*(num_val-2)+1+i ,2+(ANGLE_NUMBER)*(num_val-2)];
    faces_count = faces_count + 1;	     
  end;
  
  j = ANGLE_NUMBER-1;
  
  handles.force.mesh.faces(faces_count,:) = [1, 2,j*(num_val-2)+2];
  faces_count = faces_count + 1;

  for i = 1:(num_val-3)
    
    handles.force.mesh.faces(faces_count,:) = [j*(num_val-2)+i+1, i+2,j*(num_val-2)+i+2];
    faces_count = faces_count + 1;
	        
    handles.force.mesh.faces(faces_count,:) = [i+1,i+2,j*(num_val-2)+i+1];
    faces_count = faces_count + 1;
	
  end;
  
  i = num_val-2;
  
  handles.force.mesh.faces(faces_count,:) = [j*(num_val-2)+i+1, i+1,j*(num_val-2)+i+2];
  faces_count = faces_count + 1;
	
%handles.force.mesh.faces
%handles.force.mesh.r
  
  
  
function  handles = ReInterpMesh(hObject, eventdata, handles)
           
% ReInterpolate the data to make the triangles closer to equalaterial.

vertex_count = handles.vertex_count


r_old = handles.force.r;

angle_number = handles.force.mesh.angle_number;

ds    = sqrt((r_old(2:vertex_count,1)-r_old(1:vertex_count-1,1)).^2+(r_old(2:vertex_count,2)-r_old(1:vertex_count-1,2)).^2);
s_old = zeros(vertex_count  ,1);

for i=1:(vertex_count-1)
    s_old(i+1) = s_old(i)+ds(i);
end;

%s_old

smax = s_old(vertex_count);

s=0;

r = zeros(1000,2);

for i = 1:1000
    r(i,:) = interp1(s_old,r_old,s);
    s = s+max(r(i,1)*2*pi/angle_number,.005);
    if s > smax
        break;
    end;
end;

r = r(1:i,:);
%i

if r(1,1) ~= 0
    r=[[0,r_old(1,2)];r];
    i = i+1;
end



if r(i,1) ~= 0
    r=[r;[0,r_old(vertex_count,2)]];
    i = i+1;
end


handles.force.r = r(1:i,:);
handles.force.vertex_count = i;

%r(1:i,:)
%i


  