-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildGraphMatrixFromFv.m
More file actions
48 lines (40 loc) · 1.03 KB
/
buildGraphMatrixFromFv.m
File metadata and controls
48 lines (40 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function G = buildGraphMatrixFromFv(fvOrFaces)
%
% This method creates a binary or weighted graph, depending on the input.
% If fvOrFaces is a struct with the fields "vertices" and "faces", then the
% euclidean distance between neighbouring vertices is used as graph weight.
% If fvOrFaces is only a faces variable, the output is a binary graph of
% the connectivity specified by faces.
%
% Author: Florian Bernard (2016)
%
if ( isstruct(fvOrFaces) )
vertexFeatures = fvOrFaces.vertices;
faces = fvOrFaces.faces;
weightedGraph = true;
else
faces = fvOrFaces;
weightedGraph = false;
end
N = numel(unique(faces(:)));
G = sparse(N,N);
for f=1:size(faces,1)
neighs = faces(f,:);
% for all edges do
edges = nchoosek(neighs,2);
for e=1:size(edges,1)
v1Idx = edges(e,1);
v2Idx = edges(e,2);
if ( weightedGraph )
v1 = vertexFeatures(v1Idx,:);
v2 = vertexFeatures(v2Idx,:);
vertDist = norm(v1-v2);
G(v1Idx,v2Idx) = vertDist;
G(v2Idx,v1Idx) = vertDist;
else
G(v1Idx,v2Idx) = 1;
G(v2Idx,v1Idx) = 1;
end
end
end
end