function edge = mesh_adjacency(FV); 

% modified from mesh_edges, to compute sparse adjacency matrix
% see also triangulation2adjacency from G. Peyr� in toolbox_graph
% mesh_edges - Calculate edge lengths of triangulation
% 
% edge = mesh_edges(FV)
% 
% FV.vertices   - vertices of mesh, Nx3 Cartesian XYZ
% FV.faces      - triangulation of vertices
% 
% edge          - edge lengths, indexed by vertex 
%                 number (sparse NxN matrix)
% 

% $Revision: 1.1 $ $Date: 2007/11/16 02:53:53 $

% 
% Licence:  GNU GPL, no implied or express warranties
% History:  07/2002, Darren.Weber_at_radiology.ucsf.edu
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

tic;
fprintf('...searching for mesh edges...');

nvertex = size(FV.vertices,1);
nface   = size(FV.faces,1);



%% === Original version commented by Yasser Alemán-Gómez (LIM, HGGM, Spain, 23/12/2016) ======== %

% % % % % % % % the 'edge' matrix is the connectivity of all vertices
% % % % % % % edgeMat = sparse(nvertex,nvertex);
% % % % % % % 
% % % % % % % for f = 1:nface,
% % % % % % %     
% % % % % % %     % compute the length of all triangle edges (Diff is [3x3])
% % % % % % % %     Diff = [FV.vertices(FV.faces(f,[1 2 3]),:) - FV.vertices(FV.faces(f,[2 3 1]),:)];
% % % % % % % %     Norm = sqrt( sum(Diff.^2, 2) );
% % % % % % %     
% % % % % % %     edge(FV.faces(f,1),FV.faces(f,2)) = 1;
% % % % % % %     edge(FV.faces(f,2),FV.faces(f,3)) = 1;
% % % % % % %     edge(FV.faces(f,3),FV.faces(f,1)) = 1;
% % % % % % %     
% % % % % % %     % make sure that all edges are symmetric
% % % % % % %     edge(FV.faces(f,2),FV.faces(f,1)) = 1;
% % % % % % %     edge(FV.faces(f,3),FV.faces(f,2)) = 1;
% % % % % % %     edge(FV.faces(f,1),FV.faces(f,3)) = 1;
% % % % % % % end

%% ==== Changes made by Yasser Alemán-Gómez (LIM, HGGM, Spain, 23/12/2016) ==== % 
f = double(FV.faces);
edge = sparse([f(:,1); f(:,1); f(:,2); f(:,2); f(:,3); f(:,3)], ...
           [f(:,2); f(:,3); f(:,1); f(:,3); f(:,1); f(:,2)], ...
           1.0);

%% End of changes made by Yasser Alemán-Gómez (LIM, HGGM, Spain, 23/12/2016) % 

t=toc;
fprintf('done (%5.2f sec).\n',t);

return
