我试图找到用两个公共元素索引矩阵的行对。我有一个3D对象的三角测量,并希望按相邻三角形的角度过滤三角形。所以,我必须找到共享边缘的三角形。为了做到这一点,我必须找到有两个公共点的.tri文件的行。
我有350个.tri文件,每个文件都有约7000x3的暗淡。我找到了这个资源: MathWorks link 接受的答案是我的mac air 15分钟,Matt Figs答案每个.tri文件大约需要8分钟(我有350个!)。
我如何完成此过程?我被告知尝试用C编写这部分代码(我从未使用过C)。我也考虑过在Linux服务器上安装Matlab并在那里运行它(我从来没有这样做过)。关于如何用C编写程序或使用AWS服务器的任何建议?
位于此处的3D文件:3D files 这是我运行的代码: (任何建议清理它也将不胜感激)
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',' ');
答案 0 :(得分:1)
已经有很多内置方法可以对三角网格进行计算。看看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];