convert patch structure to graph() object?
조회 수: 10 (최근 30일)
이전 댓글 표시
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
답변 (3개)
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)
figure(2);
plot(G_B)
댓글 수: 2
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
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)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Polygons에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!