Clear Filters
Clear Filters

Can't write isosurface to stl file

7 views (last 30 days)
Sarinya
Sarinya on 14 Oct 2023
Edited: Dyuman Joshi on 14 Oct 2023
I've created an isosurface of a TPMS structure (Primitive, to be exact), but I'm having trouble writing it to an STL file. I've followed the example in this documentation practically to the letter:
extractIsosurface
However I keep running into two problems: First of all, trimesh(T) comes out looking like this:
And I can't figure out how to remove the lines. Second of all, I keep getting the error:
Error using fileparts
Input must be a row vector of characters, or a string scalar, or a cellstr, or a string matrix.
Error in stlwrite (line 70)
path = fileparts(filename);
Even though I used the exact same input format as the example. Am I missing something super obvious? Please help, my professor will rip my head off if I can't get this.
This is my code by the way
%% Input Parameters
unitcellsize = 5; % Cell size [mm]
length_x = 15; % Samplesize in x direction [mm]
length_y = 15; % Samplesize in y direction [mm]
length_z = 15; % Samplesize in z direction [mm]
step = 0.1; % To control the resolution [the smaller the better]
isovalue = 0.1;
%% Lactice Equation
syms x y z
f = @(x,y,z) cos((2.*pi.*x)./unitcellsize) + cos((2.*pi.*y)./unitcellsize) + cos((2.*pi.*z)./unitcellsize);
% ------------------ Sample Size -------------------
a = -length_x/2:step:length_x/2;
b = -length_y/2:step:length_y/2;
c = -length_z/2:step:length_z/2;
[X,Y,Z] = meshgrid(a,b,c);
%% Getting Coordinates
V = f(X,Y,Z);
co = isosurface(a,b,c,V,isovalue);
[faces1,verts1] = extractIsosurface(V,isovalue);
%% Gradient Plot
hold on
p = patch(co1);
isonormals(X,Y,Z,V,p)
view(3)
daspect([1 1 1])
colorbar
axis tight
camlight
lighting gouraud
box on
T = triangulation(double(faces1),double(verts1));
figure
trimesh(T)
%Export surface to .stl
stlwrite(T,'test.stl');

Answers (1)

Dyuman Joshi
Dyuman Joshi on 14 Oct 2023
Edited: Dyuman Joshi on 14 Oct 2023
I am not familiar with the extractIsosurface function or the Medical Imaging Toolbox the function is part of, so I can not provide any suggestions to make your method work.
However, I can offer a workaround method. Use this file from the FEX - stlwrite. I have renamed the function to stl_write() to not overload the built-in stlwrite() function, and so should you.
I have executed the code with the FEX submission and removed the file. (Copyrighted material
%% Input Parameters
unitcellsize = 5; % Cell size [mm]
length_x = 15; % Samplesize in x direction [mm]
length_y = 15; % Samplesize in y direction [mm]
length_z = 15; % Samplesize in z direction [mm]
step = 0.1; % To control the resolution [the smaller the better]
isovalue = 0.1;
%% Lactice Equation
%% This line is not needed
% syms x y z
f = @(x,y,z) cos((2.*pi.*x)./unitcellsize) + cos((2.*pi.*y)./unitcellsize) + cos((2.*pi.*z)./unitcellsize);
% ------------------ Sample Size -------------------
a = -length_x/2:step:length_x/2;
b = -length_y/2:step:length_y/2;
c = -length_z/2:step:length_z/2;
[X,Y,Z] = meshgrid(a,b,c);
%% Getting Coordinates
V = f(X,Y,Z);
co = isosurface(a,b,c,V,isovalue);
%Write the data to stl file
stl_write('test.stl', co);
Wrote 3 faces
%Read the stl file to visualize the output
TO = stlread('test.stl');
trimesh(TO)

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!