将面顶点网格导出到 STL
Exporting face-vertex mesh to STL
我正在尝试为陀螺仪网格添加厚度并将其导出到 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
如您所见,我将 "bad faces"(即 F 矩阵的行)替换为 NaN
,但这仅适用于可视化。当我尝试将生成的面和顶点传递给 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,但它包括这些 "bad faces",如下所示(放大视图) :
感谢@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
我正在尝试为陀螺仪网格添加厚度并将其导出到 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
如您所见,我将 "bad faces"(即 F 矩阵的行)替换为 NaN
,但这仅适用于可视化。当我尝试将生成的面和顶点传递给 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,但它包括这些 "bad faces",如下所示(放大视图) :
感谢@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)