[英]How to find row indices of large row matrix with n common elements in matlab?
我正在嘗試尋找成對的行,它們用2個公共元素索引一個矩陣。 我有一個3D對象的三角剖分,想通過相鄰三角形的角度過濾三角形。 因此,我必須找到共享邊的三角形。 為此,我必須找到具有兩個公共點的.tri文件行。
我有350ish .tri文件,每個文件都有7000x3暗淡的顏色。 我已經找到了以下資源: MathWorks鏈接可接受的答案花費了我的mac air 15分鍾,而Matt Figs的答案每個.tri文件大約需要8分鍾(我有350個!)。
如何完成此過程? 有人告訴我嘗試在C語言中對此部分進行編碼(我從未使用過C語言)。 我也考慮過只是嘗試在Linux服務器上安裝Matlab並在其中運行它(我也從未這樣做)。 關於如何用C編寫程序或使用AWS服務器的任何建議?
位於此處的3D文件 : 3D文件這是我運行的代碼:(任何清理它的建議也將不勝感激)
addpath('/Users/len/Desktop/javaplex/nonrigid3d')
files1 = dir('/Users/len/Desktop/javaplex/nonrigid3d/*.tri');
files2 = dir('/Users/len/Desktop/javaplex/nonrigid3d/*.vert');
time=cputime;
for k =1:length(files1)
X=load(files1(k).name);
Pt=load(files2(k).name);
%define a sparse matrix for holding angles between 2-simplices
W=sparse(length(X),length(X));
% Find common vertices: try with C
Xs = sort(X,2);
S = size(X,1);
L = S*(S+1)/2;
F = sparse(L,L);
cnt = 0;
Ntoshare = 2;
for ii = 1:S
for jj = ii+1:S
cnt = cnt + 1;
if sum(ismembc(Xs(ii,:),Xs(jj,:)))==Ntoshare
F(jj,ii) = 1;
end
end
end
[I,J] = find(F); % pairs of rows that have 2 common vertices.
cmn=[I,J];
% Calculate angle and store in sparse matrix. row and col reference triangles
for i=1:length(cmn)
normal1=cross(Pt(X(cmn(i,1),1),:)-Pt(X(cmn(i,1),2),:) ,Pt(X(cmn(i,1),1),:)-Pt(X(cmn(i,1),3),:));
normal2=cross(Pt(X(cmn(i,2),1),:)-Pt(X(cmn(i,2),2),:), Pt(X(cmn(i,2),1),:)-Pt(X(cmn(i,2),3),:));
theta = acos((dot(normal1,normal2)/(norm(normal1)*norm(normal2))));
W(cmn(i,1),cmn(i,2))=theta;
end
% save W as a txt file as nameofimage.txt in current directory
[i,j,val]=find(W);
data_dump=[i,j,val];
l=files1(k).name;
filename = strrep(l, '.tri','.txt');
dlmwrite(filename, data_dump, 'delimiter',' ');
已經有很多內置方法可用於對三角形網格進行計算。 看看triangulation
分類。 下面的代碼行將像代碼一樣計算非邊界三角形之間的角度。
%%// Generate triangulation data structure
Tri = triangulation(X, Pt);
%%// Find neighboring triangles
edgeAttachments = Tri.edgeAttachments(Tri.edges);
neighbors = cell2mat(edgeAttachments(cellfun(@numel, edgeAttachments)==2));
%%// Compute angles
normals1 = Tri.faceNormal(neighbors(:,1));
normals2 = Tri.faceNormal(neighbors(:,2));
angles = acos(dot(normals1, normals2, 2)); %// faceNormals are already normalized
%%// Generate the output you are looking for
data_dump = [neighbors, angles];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.