Rendering a 3D shape by stacking 2D layers

I am new to using MATLAB, so I am not used to specific programming functions here. I am trying to render a 3D-shaped model by stacking 2D layers. The original 2D polar plot to start with looks like this:
t = 0:0.01:2*pi;
r = sin(t).^3 + cos(t).^3;
polar(t,r)
Or, we could also use the cartesian coordinate system instead of the polar coordinate system by using this:
I will call this layer a 'bean shape'.
The goal is to create similar bean shapes and stack them continuously in the z axis to create a 3D model. (Maybe a cartesian coordinate system should be used.)The ratio of similarity will follow y=sin(z) from 0 to pi. (So the shape starts with 0 at the bottom, becomes bigger as z increases, biggest at z=pi/2, and becomes smaller again.) The biggest layer would be the same as r = sin(t).^3 + cos(t).^3 since the ratio of similarity would be y=1. For an easier understanding, I will use 7 discrete bean shapes. Shape #1 goes on the very bottom in the z axis, then on top of Shape #1 goes Shape #2, and then #3, #4, ..., #7 (Which would be the biggest layer). On top of #7 goes #6, then #5, #4, ... #1.
The only difference is that we are not using 7 discrete layers, but stacking an infinite amount of layers (from z=0 to z=pi) continuously (which is same as integration).
I would also like to know how to calculate the volume of the 3D shape after rendering this. Thanks in advance!

 Risposta accettata

One way would be to use fimplicit3:
fun=@(x,y,z) (x.^2+y.^2).^(2)-(x.^3+y.^3).*sin(z);
fimplicit3(fun);
zlim([0,pi])

8 Commenti

I would also like to know how to calculate the volume of the 3D shape after rendering this. Thanks in advance!
That should be a simple double integral exercise:
Thank you. Is there any way to align the stacked layers to the middle (excuse my non-mathematical expressions), as done in the photo of my question? I would like the layers to be shifted gradually (in terms of x and y values) as the z value changes. For example, when , it does a parallel translation of , and when , it remains same as the original.
You just need to make the polar curve z-dependent, for example,
fimplicit3(@fun3D)
zlim([0,pi])
view(-110,80)
function out=fun3D(x,y,z)
fun2D=@(x,y,z) (x.^2+y.^2).^(2)-(x.^3+y.^3).*sin(z);
dt=0.25*sin(z); %z-dependent translation
out=fun2D(x+dt,y+dt,z);
end
Your answer really helped me on my work. Appreciate it a lot!
This might be a little closer to what you were after
h=fimplicit3(@fun3D);
set(h,'EdgeColor','none','MeshDensity',110);
zlim([0,pi])
view(-110,80)
function out=fun3D(x,y,z)
fun2D=@(x,y) (x.^2+y.^2).^(2)-(x.^3+y.^3);
fun2D=@(x,y) fun2D(x+0.25,y+0.25);
s=@(q) min(q./sin(z),1e6);
out=fun2D(s(x),s(y));
end
I'm trying to export this shape to an .stl file and I came across "surf2stl" in File Exchange. (https://mathworks.com/matlabcentral/fileexchange/4512-surf2stl) A simple example of using the surf2stl would be:
x=linspace(0,2)
y=linspace(-pi,pi)
[X,Y] = meshgrid(x,y);
Z=sqrt(X).*cos(Y);
surf2stl('fun.stl', X, Y, Z);
I have no idea on how to implement this to the 3D implicit function.
function out=fun2(x,y,z)
fun1=@(x,y,z) (x.^2+y.^2).^(2)-(x.^3+y.^3).*(z/4-z.^2).^(1/2);
dt=0.25*(z/4-z.^2).^(1/2); %z-dependent translation
out=fun1(x+dt,y+dt,z);
end
Have any ideas?
I don't know about surf2stl, but with stlwrite
you should be able to do,
x=-1:0.01:1;
y=-1:0.01:1;
z=-0:0.01:3;
[X,Y,Z]=meshgrid(x,y,z);
V=fun3D(X,Y,Z);
I=isosurface(x,y,z,V,0) ;
stlwrite('fun.stl',I);
function out=fun3D(x,y,z)
fun2D=@(x,y) (x.^2+y.^2).^(2)-(x.^3+y.^3);
fun2D=@(x,y) fun2D(x+0.25,y+0.25);
s=@(q) min(q./sin(z),1e6);
out=fun2D(s(x),s(y));
end
Thank you very much.

Accedi per commentare.

Prodotti

Release

R2021a

Richiesto:

il 3 Set 2021

Risposto:

DGM
il 30 Lug 2025

Community Treasure Hunt

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

Start Hunting!

Translated by