三角剖分的边/顶点矩阵

时间:2017-03-31 13:44:35

标签: matlab

我必须使用Matlab分析一些STL文件,并使用STL阅读器成功导入它们,但此函数仅返回顶点和面(三角形)。

我正在使用的the STL reader this is an example STL filegmsh工具生成gmsh -2 -format stl -bin t4.geo Alessandro Caliaro。如果是,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 个答案:

答案 0 :(得分:2)

假设你的面孔是n-by-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';

注意:稀疏数组通常对这种邻接矩阵很有用,特别是在大范围内。

最佳,