Filling area between two planes in 3d plot
29 views (last 30 days)
Show older comments
I'm trying to more or less plot an amorphous blob on a 3d plot. I know the top, and I know the bottom. Is there an easy way to fill the space between? For example, if I have
surf(peaks)
hold on
surf(peaks + 5)
hold off
How could I connect the two planes so it's like a volume?
Thank you so much for your time
0 Comments
Accepted Answer
Mike Garrity
on 4 Sep 2015
The isosurface approach that Tim suggested is possible. Here's an example of how you'd do that. It's a bit involved and it uses quite a bit of memory:
%%Start with 2 surfaces
nx = 25;
ny = 25;
nz = 25;
[x,y] = meshgrid(linspace(-2,2,nx),linspace(-2,2,ny));
z1 = peaks(x,y);
z2 = peaks(x,y) + 5;
surf(x,y,z1);
hold on
surf(x,y,z2)
%%Create 3D grid containing distance to closest surface
[y3,x3,z3] = ndgrid(linspace(-2,2,ny),linspace(-2,2,nx),linspace(-10,15,nz));
v = zeros(size(z3));
for r=1:ny
for c=1:nx
for s=1:nz
d1 = z3(r,c,s) - z1(r,c);
d2 = z2(r,c) - z3(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(x3,y3,z3,v3,0)), ...
patch(isocaps(x3,y3,z3,v3,0))];
isonormals(x3,y3,z3,v3,p(1))
set(p,'FaceColor','yellow')
set(p,'EdgeColor','none')
set(p,'FaceLighting','gouraud')
view(3)
camlight right
You need both isosurface and isocaps there. The isosurface command creates the parts inside the volume (the same parts surf created) while isocaps creates the parts along the boundary. You can also combine isocaps from that example with your surface objects like this:
figure
surf(x,y,z1);
hold on
surf(x,y,z2)
isocaps(x3,y3,z3,v3,0);
3 Comments
Mike Garrity
on 4 Sep 2015
Edited: Mike Garrity
on 4 Sep 2015
If you are going to go the isocaps route, then you're probably going to need to worry about time/memory performance tradeoffs. For example, in some cases it will probably be much better to get rid of those nested for loops and use extra temporary variables instead.
[y3,x3,z3] = ndgrid(linspace(-2,2,ny), ...
linspace(-2,2,nx), ...
linspace(-10,15,nz));
v3 = min(z3 - repmat(z1,[1 1 nz]), ...
repmat(z2,[1 1 nz]) - z3);
I think that this would take a lot less time, when you have enough memory for those expanded copies of z1 & z2.
But if you really end up against the performance wall, then Tim Jackman's other suggestion about creating the patches yourself will probably be the way to go.
Ksenia Shukhin
on 31 May 2022
Hi Mike!
I have used your code for creation a 3d object, thank you a lot. But now the question is how to create stl file from this. Maybe you can help?
More Answers (1)
Tim Jackman
on 3 Sep 2015
One option would be to create an isosurface, but to do that you would need to convert your data into a 3-D gridded data. This link has some more information and a couple examples to follow:
Based on the example you provided, and quick way to fill the space in between the two surfaces would be to apply a patch. For example:
surf(peaks)
hold on
surf(peaks + 5)
hold off
Now place a polygon along one edge by supplying the X, Y, and Z coordinates along with the color you want:
patch([1;1;49;49],[1;1;1;1],[0;5;5;0],[1,0,0])
You can get more information on the patch objects here:
0 Comments
See Also
Categories
Find more on Volume Visualization 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!