【问题标题】:Edge/Vertex matrix from triangulation三角剖分的边/顶点矩阵
【发布时间】:2017-04-04 23:09:15
【问题描述】:

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

这是我正在使用的the STL readerthis 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


【解决方案1】:

假设你的脸在一个 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';,我有这个错误Error using + Matrix dimensions must agree.。事实上,矩阵比另一个多一个维度:&gt;&gt; size(A) ans = 4616 4617,这怎么可能?
  • 是的,这取决于您的数据。我已经编辑了我的答案以考虑到这一点。您只是想将邻接矩阵的大小精确到 sparse 函数。
  • 谢谢!我有一个问题。我尝试了一个非常简单的网格,一个 2x2 的正方形网格,然后这个网格有 9 个顶点和 8 个面,有 12 条边。然而,输出矩阵是adjacencymatrix: [24×24 double],而它应该是 12x9(或最多 9x12)。我错过了什么吗?
  • @senseiwa 奇怪。邻接矩阵应该是 9×9 周期。你能提供数据吗? (顶点和面的列表)
  • 当然,它作为 Dropbox 链接在问题中。
猜你喜欢
  • 1970-01-01
  • 2020-08-05
  • 2021-05-27
  • 1970-01-01
  • 2012-03-29
  • 1970-01-01
  • 2012-09-28
  • 2015-03-19
  • 1970-01-01
相关资源
最近更新 更多