convert patch structure to graph() object?

조회 수: 10 (최근 30일)
Walter Roberson
Walter Roberson 2023년 9월 22일
편집: 2024년 3월 7일
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.

답변 (3개)

J. Alex Lee
J. Alex Lee 2023년 9월 22일
편집: J. Alex Lee 2023년 9월 22일
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)
G_B =
graph with properties: Edges: [276×2 table] Nodes: [97×0 table]
figure(2);
plot(G_B)
  댓글 수: 2
Walter Roberson
Walter Roberson 2023년 9월 22일
편집: Walter Roberson 2023년 9월 22일
Interesting point. Let's try
% 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]);
st = unique(sort([s(:), t(:)],2), 'rows');
G = graph(st(:,1), st(:,2))
G =
graph with properties: Edges: [276×1 table] Nodes: [97×0 table]
figure(1);
plot(G)
... all the more reason why it would be better to have a function to handle this.
J. Alex Lee
J. Alex Lee 2023년 9월 23일
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
J. Alex Lee 2023년 9월 23일
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)

浩
2024년 3월 7일
편집: 2024년 3월 7일

카테고리

Help CenterFile Exchange에서 Polygons에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by