Matlab 三角剖分的边/顶点矩阵
我必须用Matlab分析一些STL文件,并用STL读取器成功导入它们,但该函数只返回顶点和面(三角形) 这是我正在使用的,并且是由Matlab 三角剖分的边/顶点矩阵,matlab,Matlab,我必须用Matlab分析一些STL文件,并用STL读取器成功导入它们,但该函数只返回顶点和面(三角形) 这是我正在使用的,并且是由gmsh工具与gmsh-2-format stl-bin t4.geo一起生成的。在这种情况下,STL函数的代码位于末尾 mesh = stlread("t4.stl"); 是否有一个函数可用于从此类三角剖分中获取顶点/边邻接矩阵 function [F,V,N] = stlbinary(M) F = []; V = []; N = [];
gmsh
工具与gmsh-2-format stl-bin t4.geo一起生成的。在这种情况下,STL函数的代码位于末尾
mesh = stlread("t4.stl");
是否有一个函数可用于从此类三角剖分中获取顶点/边邻接矩阵
function [F,V,N] = stlbinary(M)
F = [];
V = [];
N = [];
if length(M) < 84
error('MATLAB:stlread:incorrectFormat', ...
'Incomplete header information in binary STL file.');
end
% Bytes 81-84 are an unsigned 32-bit integer specifying the number of faces
% that follow.
numFaces = typecast(M(81:84),'uint32');
%numFaces = double(numFaces);
if numFaces == 0
warning('MATLAB:stlread:nodata','No data in STL file.');
return
end
T = M(85:end);
F = NaN(numFaces,3);
V = NaN(3*numFaces,3);
N = NaN(numFaces,3);
numRead = 0;
while numRead < numFaces
% Each facet is 50 bytes
% - Three single precision values specifying the face normal vector
% - Three single precision values specifying the first vertex (XYZ)
% - Three single precision values specifying the second vertex (XYZ)
% - Three single precision values specifying the third vertex (XYZ)
% - Two unused bytes
i1 = 50 * numRead + 1;
i2 = i1 + 50 - 1;
facet = T(i1:i2)';
n = typecast(facet(1:12),'single');
v1 = typecast(facet(13:24),'single');
v2 = typecast(facet(25:36),'single');
v3 = typecast(facet(37:48),'single');
n = double(n);
v = double([v1; v2; v3]);
% Figure out where to fit these new vertices, and the face, in the
% larger F and V collections.
fInd = numRead + 1;
vInd1 = 3 * (fInd - 1) + 1;
vInd2 = vInd1 + 3 - 1;
V(vInd1:vInd2,:) = v;
F(fInd,:) = vInd1:vInd2;
N(fInd,:) = n;
numRead = numRead + 1;
end
end
函数[F,V,N]=STL二进制(M)
F=[];
V=[];
N=[];
如果长度(M)<84
错误('MATLAB:stlread:incorrectFormat'。。。
“二进制STL文件中的头信息不完整。”);
结束
%字节81-84是指定面数的无符号32位整数
%接下来就是。
numface=typecast(M(81:84),'uint32');
%numFaces=双(numFaces);
如果numface==0
警告('MATLAB:stlread:nodata','STL文件中没有数据');
返回
结束
T=M(85:end);
F=NaN(numFaces,3);
V=NaN(3*numface,3);
N=NaN(numFaces,3);
numRead=0;
而numRead
假设你的脸是一个n乘3的数组F
:
% temporary array
T = [F(:,1) F(:,2) ; F(:,1) F(:,3) ; F(:,2) F(:,3)];
% get the edges
E = unique([min(T,[],2), max(T,[],2)],'rows');
% build the adjacency matrix
n = max(E(:,2));
A = sparse(E(:,1), E (:,2), ones(size(E,1),1), n, n);
A = A + A';
注意:稀疏数组通常适用于这种邻接矩阵,特别是在大范围内
最好的方法是,假设你的脸是一个n×3的数组
F
:
% temporary array
T = [F(:,1) F(:,2) ; F(:,1) F(:,3) ; F(:,2) F(:,3)];
% get the edges
E = unique([min(T,[],2), max(T,[],2)],'rows');
% build the adjacency matrix
n = max(E(:,2));
A = sparse(E(:,1), E (:,2), ones(size(E,1),1), n, n);
A = A + A';
注意:稀疏数组通常适用于这种邻接矩阵,特别是在大范围内
最好,如果你有脸,你几乎有边。。。这基本上是一行代码,我相信如果你尝试的话你可以做到。应该很容易,但是我的矩阵,似乎,每个三角形都有一个索引,所以我很困惑。事实上,脸矩阵只是一组经过重塑的数字。我不明白这个代码与这个问题有什么关系。如果你有脸,你几乎有边。。。这基本上是一行代码,我相信如果你尝试的话你可以做到。应该很容易,但是我的矩阵,似乎,每个三角形都有一个索引,所以我很困惑。事实上,脸矩阵只是一组经过重塑的数字。我不明白这个代码与这个问题有什么关系。谢谢!不过我有一个奇怪的错误,似乎
a
不是正方形的:当添加其对称性时,a=a+a'代码>,我有这个错误使用+矩阵维度的错误必须一致。
。事实上,矩阵的一个维度多于另一个维度:>size(A)ans=4616 4617
,这怎么可能?是的,这可能会发生,具体取决于您的数据。为了考虑这一点,我编辑了我的答案。您只需将邻接矩阵的大小精确到sparse
函数即可。谢谢!我有一个问题。我尝试了一个非常简单的网格,一个2x2的正方形网格,这个网格有9个顶点,8个面,12条边。但是,输出矩阵是邻接矩阵:[24×24 double]
,而它应该是12x9(或最多9x12)。“我错过了什么吗?”senseiwa奇怪地说。邻接矩阵应该是9乘9,周期。你能提供数据吗?(顶点和面列表)当然,它作为Dropbox链接存在于问题中。谢谢!不过我有一个奇怪的错误,似乎a
不是正方形的:当添加其对称性时,a=a+a'代码>,我有这个错误使用+矩阵维度的错误必须一致。
。事实上,矩阵的一个维度多于另一个维度:>size(A)ans=4616 4617
,这怎么可能?是的,这可能会发生,具体取决于您的数据。为了考虑这一点,我编辑了我的答案。您只需将邻接矩阵的大小精确到sparse
函数即可。谢谢!我有一个问题。我尝试了一个非常简单的网格,一个2x2的正方形网格,这个网格有9个顶点,8个面,12条边。但是,输出矩阵是邻接矩阵:[24×24 double]
,而它应该是12x9(或最多9x12)。“我错过了什么吗?”senseiwa奇怪地说。邻接矩阵应该是9乘9,周期。你能提供数据吗?(顶点和面列表)当然,它作为Dropbox链接存在问题。