How do I export a plot comprising of multiple functions to .stl?
Show older comments
I have a plot created using multiple parametric functions that define an archimedean spiral. I want to export the plotted figure to stl or convert it into a mesh that resembles the plotted shape, however, am unable to do so. I have tried using triangulation and mesh grid but to no avail. The plot I want to export is shown above.This plot was generated using:
% Spiral edges
x = (inner_radius + spiral_growth_rate.*angles).*cos(angles);
y = (inner_radius + spiral_growth_rate.*angles).*sin(angles);
syms theta
diff_x = diff((inner_radius +spiral_growth_rate*theta)*cos(theta));
diff_y = diff((inner_radius +spiral_growth_rate*theta)*sin(theta));
N_x = (-diff_y)/sqrt(diff_x^2 + diff_y^2);
N_y = (diff_x)/sqrt(diff_x^2 + diff_y^2);
shift_x = vpa(subs(N_x, theta, angles));
shift_y = vpa(subs(N_y, theta, angles));
outer_x_coords = x+shift_x.*thickness/2;
outer_y_coords = y+shift_y.*thickness/2;
inner_x_coords = x-shift_x.*thickness/2;
inner_y_coords = y-shift_y.*thickness/2;
% Joining curves
s = linspace(-1, 1);
inner_j_x = x(1) + s .* shift_x(1)*thickness/2;
inner_j_y = y(1) + s .* shift_y(1)*thickness/2;
outer_j_x = x(end) + s .* shift_x(end).*thickness/2;
outer_j_y = y(end) + s .* shift_y(end).*thickness/2;
x_coords = double([inner_x_coords, outer_x_coords, inner_j_x, outer_j_x]);
y_coords = double([inner_y_coords, outer_y_coords, inner_j_y, outer_j_y]);
% [X,Y] = meshgrid(x_coords, y_coords);
%
% T = delaunay(X,Y);
% trimesh(T,x,y);
figure
hold on
plot(outer_x_coords, outer_y_coords);
plot(inner_x_coords, inner_y_coords);
plot(inner_j_x, inner_j_y);
plot(outer_j_x, outer_j_y);
hold off
Accepted Answer
More Answers (1)
An example.
% some parameters
npointsperturn = 100;
nturns = 5;
trackwidth = 1;
trackthick = 0.5;
inner_radius = 10;
growth_rate = 0.3;
% just do the symbolic math once to get the required expression
% this is literally 1000 times faster, and can probably still be simplified.
angles = linspace(0,2*pi*nturns,npointsperturn*nturns);
costheta = cos(angles); % heavily reused terms
sintheta = sin(angles);
x = (inner_radius + growth_rate.*angles).*costheta;
y = (inner_radius + growth_rate.*angles).*sintheta;
denom = sqrt(angles.^2*growth_rate^2 + 2*angles*growth_rate*inner_radius + growth_rate^2 + inner_radius^2);
shift_x = -(inner_radius*costheta + growth_rate*(sintheta + angles.*costheta))./denom;
shift_y = -(inner_radius*sintheta - growth_rate*(costheta - angles.*sintheta))./denom;
% these were named backwards
xinner = x + shift_x.*trackwidth/2;
yinner = y + shift_y.*trackwidth/2;
xouter = x - shift_x.*trackwidth/2;
youter = y - shift_y.*trackwidth/2;
% just assemble an ordered vertex list
V = [flipud([xinner(:) yinner(:)]);
xouter(:) youter(:)];
% construct the edge lists in the same direction
v = 1:size(V,1);
E = [v; circshift(v,-1)].';
% TRIANGULATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% do a constrained triangulation using delaunayTriangulation
T = delaunayTriangulation(V,E);
F = T.ConnectivityList(isInterior(T),:);
V = T.Points;
% display it using patch()
patch('faces',F,'vertices',V,'facecolor','w','edgecolor','k');
axis equal; grid on
xlabel('X'); ylabel('Y')
I'm assuming the result is supposed to be a 3D solid, so that's only part of the problem
% EXTRUSION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% extrude the part into 3D
[F V] = extrude(F,V,trackthick);
% write to file
stlwrite(triangulation(F,V),'testfile.stl')
% display it using patch()
figure
patch('faces',F,'vertices',V,'facecolor','w','edgecolor','none');
view(3); camlight; view(10,33)
axis equal; grid on
xlabel('X'); ylabel('Y'); zlabel('Z')
There are other ways to do the triangulation, but this is a way.
Categories
Find more on STL (STereoLithography) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
