TL;DR:
我有一堆四面体,我想知道哪一个是它的4个(或更少)相邻的(面共享)四面体,我通过检查它们共享的三维点来做到这一点。这太慢了。
Graph.element.nodeId
.neighbours
.nodes.positions
三角剖分的输出有两个矩阵,
TRI
和
points
,第一个是
Ntri x 4
数组,在每一行中有每个四面体的节点ID,而第二行是点的列表
Npoint x 3
我目前正在构建下面代码中的图形,但是对于任何适当大小的网格来说,它都是非常慢的。几乎占用所有时间的一行是
find
行(在代码中标记),找到当前元素的邻居的部分。
当前算法为每个四面体获取其所有节点,然后查找包含这些节点的所有其他四面体。然后它过滤掉所有与当前四面体不包含3个相同节点的四面体,只留下当前四面体的邻居。
function graph=trimesh2graph(TRI,points)
nD=size(points,2);
% preallocate.
graph.elements(size(TRI,1)).neighbours=[];
% For each element, find its node Ids and neighbouring elements
for ii=1:size(TRI,1)
nodeids=TRI(ii,:);
elem=[];
for jj=1:(nD+1)
[iind,~]=find(nodeids(jj)==TRI); % this is 80% of the time
elem=[elem; iind];
end
u = unique(elem);
% of all elements that have shared nodes with the current element,
% get only the ones that have 3 shared nodes.
graph.elements(ii).neighbours = int32((u(histc(elem,u)==nD)));
% some other code
end
% some other code
x = gallery('uniformdata',[30 1],0);
y = gallery('uniformdata',[30 1],1);
z = gallery('uniformdata',[30 1],2);
vertices=[x,y,z];
TRI= delaunay(x,y,z)
trimesh2graph(TRI,vertices);
如何提高此代码的性能?我希望它需要一种不同的方法,而不仅仅是更快的命令。我看了一点沃罗诺图,但似乎发现(
)无论如何都需要做。
注:我不一定需要MATLAB代码,如果你知道一个算法来解决问题请回答,我会在MATLAB代码。