【发布时间】:2017-04-04 23:09:15
【问题描述】:
我必须用 Matlab 分析一些 STL 文件,我用 STL 阅读器成功导入它们,但是这个函数只返回顶点和面(三角形)。
这是我正在使用的the STL reader 和this is an example STL file,由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
【问题讨论】:
-
如果你有面孔,你几乎有边缘......这本质上是一行代码,我相信如果你尝试,你可以做到。
-
应该很容易,但我的矩阵似乎每个三角形索引一次,所以我很困惑。事实是,人脸矩阵只是
1:nvertices重塑的数字列表。 -
我不明白这段代码与问题有什么关系。
标签: matlab