Clear Filters
Clear Filters

Create volume out of two surfaces

14 views (last 30 days)
Kim
Kim on 21 Aug 2019
Edited: Teerapong Poltue on 8 Jun 2021
I'm trying to create a volume out of two surfaces for a parameter study. The volume should also be exportable as stl-file. The code for the surfaces is shown below.
% Points in xy-plane
nx = 100;
px = linspace(1,50,nx);
pyi = 5+sin(0.8*px);% inside
py = 6+sin(0.8*px);% outside
% rotation x-axis
n=25;
t=(0:n)'*0.5*pi/n;
figure;
surf(ones(n+1,1)*px,cos(t)*pyi,sin(t)*pyi);% inside
hold on
surf(ones(n+1,1)*px,cos(t)*py,sin(t)*py);% outside
I tried to apply the code from Filling area between two planes in 3d plot but I'm not able to make it work. If I use the code below, I get the error report that the indices on the left side are not compatible with the size of the right side at the line v3(r,c,s) = min(d1,d2);
%%Create 3D grid containing distance to closest surface
[x,y,z] = ndgrid(linspace(0,50,nx),linspace(0,50,nx),linspace(0,50,nx));
v = zeros(size(z));
for r=1:n+1
for c=1:n+1
for s=1:n+1
d1 = z(r,c,s) - sin(t)*pyi(r,c);
d2 = sin(t)*py(r,c) - z(r,c,s);
if d1 < 0
v3(r,c,s) = d1;
elseif d2 < 0
v3(r,c,s) = d2;
else
v3(r,c,s) = min(d1,d2);
end
end
end
end
%%Create isosurface
figure
p = [patch(isosurface(y,x,z,v3,0)), ...
patch(isocaps(y,x,z,v3,0))];
isonormals(y,x,z,v3,p(1))
set(p,'FaceColor','yellow')
set(p,'EdgeColor','none')
set(p,'FaceLighting','gouraud')
view(3)
camlight right
I'd be very grateful for your help.
  2 Comments
KSSV
KSSV on 21 Aug 2019
YOu want volume with cubes or tetrahedrons?
Kim
Kim on 21 Aug 2019
Since I want to be able to export it as stl tetrahedrons would be best.

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 21 Aug 2019
Edited: Bruno Luong on 21 Aug 2019
Tetra mesh generation. Let you do exportation in STL file
% Points in xy-plane
nx = 100;
px = linspace(1,50,nx);
pyi = 5+sin(0.8*px);% inside
py = 6+sin(0.8*px);% outside
% rotation x-axis
n=25;
t=(0:n)'*0.5*pi/n;
XYZI = cat(3,ones(n+1,1)*px, cos(t)*pyi, sin(t)*pyi);
XYZE = cat(3,ones(n+1,1)*px, cos(t)*py, sin(t)*py);
isbdri = false(size(XYZI,1),size(XYZI,2));
isbdri([1 end],:) = true;
isbdri(:,[1 end]) = true;
isbdre = false(size(XYZE,1),size(XYZE,2));
isbdre([1 end],:) = true;
isbdre(:,[1 end]) = true;
XYZI = reshape(XYZI,[],3);
n = size(XYZI,1);
XYZE = reshape(XYZE,[],3);
XYZ = [XYZI; XYZE];
isbdr = [isbdri; isbdre];
T = delaunay(XYZ);
b = T<=n;
keep = any(b,2) & any(~b,2) & sum(isbdr(T),2)<=3;
T = T(keep,:);
% This will take long time
close all
tetramesh(T,XYZ,'FaceColor','g')
  2 Comments
Kim
Kim on 21 Aug 2019
Thank you very much!
Teerapong Poltue
Teerapong Poltue on 8 Jun 2021
Edited: Teerapong Poltue on 8 Jun 2021
I tried to apply this code to my work, but it isn't work. Did you have any suggestion for my work.
I would like to connect two surfaces to be a solid, the it will be exported as an .stl file for futher CAD.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!