convert patch structure to graph() object?
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Is there an easier / better way to convert a patch() faces / vertices structure to a graph object?
%Where S is a struct with faces and vertices
%such as returned by isosurface()
%patch() can deal directly with such struct
s = S.faces;
v = S.vertices;
t = circshift(S.faces, [0 -1]);
G = graph(s(:), t(:));
Although this code is not long, it took a bit of thinking about and experimenting to come up with; it feels to me as if there should be a better method.
Each row of the faces is a list of vertex numbers, with an implied return to the first vertex. This code constructs source and target lists by taking all adjacent pairs from the rows, including wrapping back from first to last.
0 comentarios
Respuestas (3)
J. Alex Lee
el 22 de Sept. de 2023
Editada: J. Alex Lee
el 22 de Sept. de 2023
Are you sure your method is giving you the graph you want? It looks like it counts edges twice, but not exactly.
So my question is: there must be a better way to construct the self-adjacency matrix other than this brute force?
% create structure by isosurface
[x,y,z] = meshgrid([-3:1:3]);
V = x.*exp(-x.^2 -y.^2 -z.^2);
S = isosurface(x,y,z,V,1e-4);
% shortcut for connectivity matrix
c = S.faces;
%% Walter's graph
%Where S is a struct with faces and vertices
%such as returned by isosurface()
%patch() can deal directly with such struct
s = S.faces;
v = S.vertices;
t = circshift(S.faces, [0 -1]);
G = graph(s(:), t(:));
figure(1);
plot(G)
%% alternate graph
% number of nodes
n = height(S.vertices);
% construct self-adjacency matrix
A = spalloc(n,n,n*n);
for i = 1:n
[row,col] = find(S.faces==i);
v = unique(c(row,:));
A = A + sparse(i,v,1,n,n);
end
% have to remove the diagonals
A = A .* ~speye(n);
G_B = graph(A)
figure(2);
plot(G_B)
2 comentarios
Walter Roberson
el 22 de Sept. de 2023
Editada: Walter Roberson
el 22 de Sept. de 2023
J. Alex Lee
el 23 de Sept. de 2023
I now better understand how our methods are related: both concepts were discussed a while ago triangulation to adjacency.
I update my code for creating the adjacency matrix based on Akira's answer, using circshift for more generality - I guess the path through the sorted node pair list is more "direct" than this...but anyway for completeness.
c = S.faces;
t = circshift(c,[0 -1]);
A = sparse(c(:),t(:),1,n,n);
A = A | A.';
J. Alex Lee
el 23 de Sept. de 2023
Found a more built-in way but you have to convert the structure to a triangulation
% create structure by isosurface
[x,y,z] = meshgrid([-3:1:3]);
V = x.*exp(-x.^2 -y.^2 -z.^2);
S = isosurface(x,y,z,V,1e-4);
TR = triangulation(S.faces,S.vertices);
st = edges(TR);
G = graph(st(:,1),st(:,2));
plot(G)
0 comentarios
Ver también
Categorías
Más información sobre Polygons en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!