在MATLAB中加载stl文件并转换为3D数组 [英] Load stl file in matlab and convert to a 3D array

查看:203
本文介绍了在MATLAB中加载stl文件并转换为3D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个stl文件,并已使用stlread函数将其加载到Matlab中.在这一点上,我有一组面和顶点.如何在3D二进制数组(如512x512x100数组)中转换这些面和顶点以获得二进制3D体积?

I have a stl file and I've loaded it in Matlab using stlread function. At this point I have a set of faces and vertices. How can I convert these faces and vertices in a 3D binary array like 512x512x100 array to obtain a binary 3D volume?

推荐答案

幸运的是.我最近正在使用STL文件,并且我编写了一些函数来做到这一点.

Ah lucky you. I am working with STL files recently and I coded some functions to do exactly this.

首先,请注意您会失去精度. STL文件以任意精度表示任意形状,并将其转换为体积会导致离散化和损失.

First, note that you lose precision. STL files represent arbitrary shapes with arbitrary precision and converting it into a volume results in discretization and losses.

也就是说,有一种非常简单的方法来知道某物体是否在闭合,连接的三角曲面的内部或外部,而不管其是否凸出:向无限和计数相交处投射射线与表面.如果是奇数,则为内部,如果是偶数,则为外部.

That said, there is a very easy method to know if something is inside or outside a closed, connected triangulated surface, regardless if its convex or not: Throw a ray to the infinite and count intersection with the surface. If odd, its inside, if even, outside.

您唯一需要的特殊代码是直线三角形相交,而MöllerTrumbore算法是最常用的代码之一.

The only special code you need is the line-triangle intersection, and the Möller Trumbore algorithm is one of the most common ones.

function in=inmesh(fv,points)
%INMESH tells you if a point is inside a closed,connected triangulated surface mesh
% Author: Ander Biguri
maxZ=max(fv.vertices(:,3));
counts=zeros(size(points,1),1);
for ii=1:size(points,1)
    ray=[points(ii,:);points(ii,1:2) maxZ+1];
    for jj=1:size(fv.faces,1)
        v=fv.vertices(fv.faces(jj,:),:);
        if all(v(:,3)<ray(1,3))
            continue;
        end
        isin=mollerTrumbore(ray, fv.vertices(fv.faces(jj,:),:));
        counts(ii)=counts(ii)+isin;
    end

end
in=mod(counts,2);
end

FileExchange 中,进行了少量修改:

From FileExchange, with small modifications:

function [flag, u, v, t] = mollerTrumbore (ray,tri)
% Ray/triangle intersection using the algorithm proposed by Moller and Trumbore (1997).
%
% IMPORTANT NOTE: Assumes infinite legth rays.
% Input:
%    ray(1,:) : origin.
%    d : direction.
%    tri(1,:), tri(2,:), tri(3,:): vertices of the triangle.
% Output:
%    flag: (0) Reject, (1) Intersect.
%    u,v: barycentric coordinates.
%    t: distance from the ray origin.
% Author: 
%    Jesus Mena

    d=ray(2,:)-ray(1,:);
    epsilon = 0.00001;

    e1 = tri(2,:)-tri(1,:);
    e2 = tri(3,:)-tri(1,:);
    q  = cross(d,e2);
    a  = dot(e1,q); % determinant of the matrix M

    if (a>-epsilon && a<epsilon) 
        % the vector is parallel to the plane (the intersection is at infinity)
        [flag, u, v, t] = deal(0,0,0,0);
        return;
    end

    f = 1/a;
    s = ray(1,:)-tri(1,:);
    u = f*dot(s,q);

    if (u<0.0)
        % the intersection is outside of the triangle
        [flag, u, v, t] = deal(0,0,0,0);
        return;          
    end

    r = cross(s,e1);
    v = f*dot(d,r);

    if (v<0.0 || u+v>1.0)
        % the intersection is outside of the triangle
        [flag, u, v, t] = deal(0,0,0,0);
        return;
    end
    if nargout>3
        t = f*dot(e2,r); % verified! 
    end
    flag = 1;
    return
end

只需生成您的观点:

yourboundaries=% get the range of your data from the STL file.
[x,y,z]=meshgrid(yourboundaries);
P=[x(:) y(:) z(:)];
in=inmesh(fv,P);
img=reshape(in,yourboundariesSize);

这篇关于在MATLAB中加载stl文件并转换为3D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆