Adding a polyshape to a 3D plot (a la patch)

19 views (last 30 days)
In the attached .mat file is a polyshape object which, when plotted in 2D, looks like an annulus,
Hpgon = plot(pgon); axis equal
Now, however, I would like to add this shape to the xy-plane of a 3D plot axis, filled with the same color. My first thought was to use patch(), as follows
vertices3D=pgon.Vertices;
vertices3D(:,3)=0;
faces3D=1:size(vertices3D,1);
patch('Vertices',vertices3D,'Faces',faces3D,'FaceColor',Hpgon.FaceColor);
camva auto
axis vis3d
This fails however, resulting in the unfilled plot below. I can see that patch() is having trouble interpreting this as a closed shape, but am not sure what the most efficient remedy would be. What is the best way to give patch() the same smarts as polyshape.plot()? Might there be an altogether different alternative to patch? It seems a shame that I cannot somehow do this directly with the polyshape object

Accepted Answer

Matt J
Matt J on 26 Mar 2019
Edited: Matt J on 26 Mar 2019
Happily, I found a solution that circumvents patch() altogether and pretty much makes direct use of polyshape, as I was hoping for. It seems that any 2D plotting command can also be used to plot into an existing 3D axis. So, I can just use plot.polyshape() to add the annulus to a 3D plot directly. One can also apparently use hgtransforms to re-orient polyshapes in 3D space arbitrarily, as in the example below. This is great - it basically means that the full power of polyshape is directly available in 3D as well as 2D.
load('example.mat')
t = 0:pi/10:2*pi;
figure
[X,Y,Z] = cylinder((2+cos(t))*10);
surf(X,Y,Z-.5)
M=[ 1.0000 0 -0.0035 0
0 1.0000 0 0
0.0035 0 1.0000 0
0 0 0 1.0000];
t=hgtransform('Matrix',M);
Hpgon=plot(pgon,'Parent',t,'FaceColor','r');
axis vis3d

More Answers (1)

KSSV
KSSV on 26 Mar 2019
Edited: KSSV on 26 Mar 2019
load('example.mat')
Hpgon = plot(pgon); axis equal
vertices3D=pgon.Vertices;
vertices3D(:,3)=0;
faces3D=1:size(vertices3D,1);
vertices3D = [ NaN NaN NaN ;vertices3D ] ;
patch('Vertices',vertices3D,'Faces',faces3D,'FaceColor',Hpgon.FaceColor);
camva auto
axis vis3d
untitled.png
  10 Comments
Matt J
Matt J on 26 Mar 2019
I think it might be an OpenGL issue. I am using working through Remote Desktop right now, and am forced to use software-based OpenGL rendering,
>> opengl software
I'll check again when I get to the office.
Matt J
Matt J on 26 Mar 2019
After more testing, it appears that this technique doesn't really work, unfortunately - not with either example data set. I think this will be apparent if you try to plot the patch in a separate figure from the polyshape, like below.
load('example.mat')
figure(1)
Hpgon = plot(pgon); axis equal
figure(2);
vertices3D=pgon.Vertices;
vertices3D(:,3)=0;
faces3D=1:size(vertices3D,1);
vertices3D = [ NaN NaN NaN ;vertices3D ] ;
patch('Vertices',vertices3D,'Faces',faces3D,'FaceColor',Hpgon.FaceColor);
drawnow
axis([0.6*[-100,100,-100,100],-40,40])
camva auto
axis vis3d

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!