%this function get a mesh struct (contains vertices and triangles of the mesh). get also
%the indices of the center points of the geodesic mask in the vertices array
%and array crop_size (the maximal length of a path on the surface from the center point) of the geodesic mask.
%D is distances matrix. optional parameter.
%the function returns the mesh of the geodesic mask - the cropped  mesh
%the mesh of the geodesic mask will contain the union of the geodesic masks
%of each center and crop_size

function  [cropped_mesh] =geodesic_mask(mesh, center_ind, crop_size, D)
if (nargin<4)
    opt.mode='single';
    source = repmat(Inf, [size(mesh.X) 1]);
    source(round(rand*(length(mesh.X)-1)+1),1) = 0;
    D      = fastmarch (mesh.TRIV, mesh.X, mesh.Y, mesh.Z,source,opt.mode);
    size(D);
end
vertices_to_keep= zeros(1,length(mesh.X));

a=1;
figure
trisurf(mesh.TRIV, mesh.X, mesh.Y, mesh.Z, D(:,end)); axis image;
drawnow;


for ii=1: length(crop_size)
    vertices_to_keep=((D(center_ind(ii),:) < crop_size(ii)) &(D(center_ind(ii),:) >0))  |  (vertices_to_keep);
    vertices_to_keep(center_ind(ii))=1;
end
cropped_mesh = meshSelectVertices(mesh,vertices_to_keep');
