【问题标题】:Exporting face-vertex mesh to STL将面顶点网格导出到 STL
【发布时间】:2020-03-26 17:42:18
【问题描述】:

我正在尝试为陀螺仪网格添加厚度并将其导出到 STL。我可以成功绘制加厚的陀螺网格,如下所示,但这是因为我编写了一个函数来删除不需要的面,即边长超过某个限制的面。我还应该提到,所有的面都是三角形的,所以顶点和面矩阵都是 N x 3 形式。

这是检查边长并删除 too_long 的代码:

function [F] = remove_bad_faces(F,V,too_long)

%% Initiate various counters/storage matrices

bad_face_count = 0;     % Number of bad faces
bad_faces = [];         % Matrix of bad faces
bad_vertices = [];      % Matrix of bad vertices
bad_face_indexes = [];  % Matrix of bad face row indices. I know that the plural of index is not indexes.

%% Find all bad faces

for i=1:size(F,1)

   face = F(i,:);
   v1 = V(face(1),:);
   v2 = V(face(2),:);
   v3 = V(face(3),:);

   if norm(v1-v2) >= too_long || norm(v2-v3) >= too_long || norm(v1-v3) >= too_long
       bad_face_count = bad_face_count + 1;
       bad_faces = [bad_faces; face];
       bad_vertices = [bad_vertices;v1;v2;v3];
       bad_face_indexes = [bad_face_indexes,i];
   end

end

%% Remove bad faces

for j=1:size(bad_face_indexes,2)
   index = bad_face_indexes(j);
   F(index,:) = nan;
end

end

如您所见,我正在用NaN 替换“坏脸”(即 F 矩阵的行),但这仅适用于可视化。当我尝试将生成的面和顶点传递给 STL 生成函数时(请参阅here),我收到以下错误:

Index in position 2 is invalid. Array indices must be positive integers or logical values.

Error in stlwrite (line 76)
facets = reshape(facets(:,faces'), 3, 3, []);

Error in Gyroid_Mesh_Script (line 60)
stlwrite('gyroid_test.stl', F, V)

很明显NaN 是问题所在,我尝试将它与[]0 交换,但都没有奏效。非常感谢任何帮助,在此先感谢!

编辑: 当我使用[] 而不是NaN 时,我可以导出到STL,但它包括这些“坏面孔”,如下所示(放大视图):

【问题讨论】:

  • 1.我会坚持使用[] 而不是NaN0。如果你这样做,令人反感的方面就会消失。错误是一样的吗? 2.函数抱怨faces有一些非正||非逻辑值。 faces 是一个索引,所以这是有道理的。 F 有负值吗?一些旁白: 1. 您可以将 bad_face_count 移到 for 循环之外,只需将其分配为 bad_face_count=numel(bad_face_indexes); 2. 当您剔除 F 时,更简单且更容易理解的操作是 F(bad_face_indexes, :) = [];,不需要for循环。

标签: matlab mesh triangulation


【解决方案1】:

感谢@benJephunneh 的回复!这解决了一切。我正在发布更新的函数,该函数会删除边缘为 too_long 的面:

function [F] = remove_bad_faces(F,V,too_long)

%% Initiate various counters/storage matrices

bad_faces = [];         % Matrix of bad faces
bad_vertices = [];      % Matrix of bad vertices
bad_face_indexes = [];  % Matrix of bad face row indices. I know that the plural of index is not indexes.

%% Find all bad faces

for i=1:size(F,1)

   face = F(i,:);
   v1 = V(face(1),:);
   v2 = V(face(2),:);
   v3 = V(face(3),:);

   if norm(v1-v2) >= too_long || norm(v2-v3) >= too_long || norm(v1-v3) >= too_long
       bad_faces = [bad_faces; face];
       bad_vertices = [bad_vertices;v1;v2;v3];
       bad_face_indexes = [bad_face_indexes,i];
   end

end

bad_face_count = numel(bad_face_indexes);

%% Remove bad faces

F(bad_face_indexes,:) = [];

end

它还使用stlwrite('gyroid_test.stl', F, V)成功导出到STL

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 2017-11-15
    • 2021-05-10
    • 2020-08-19
    相关资源
    最近更新 更多