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

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

 Réponse acceptée

Matt J
Matt J le 26 Mar 2019
Modifié(e) : Matt J le 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

Plus de réponses (1)

KSSV
KSSV le 26 Mar 2019
Modifié(e) : KSSV le 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 commentaires

Matt J
Matt J le 26 Mar 2019
Modifié(e) : Matt J le 26 Mar 2019
Hmmm. Seems promising, but I cannot seem to get it to generalize to other cases, for instance the second example data set attached. The result I get for that case is,
untitled.png
It is working for second case also...the logic is the first and last points are joining...you should stop joining them...I have used NaN for this. Try:
vertices3D = [ NaN NaN NaN ;vertices3D;NaN NaN NaN ] ;
Matt J
Matt J le 26 Mar 2019
No, the annulus is still unfilled. Are you getting a different result than my last plot?
KSSV
KSSV le 26 Mar 2019
I got this output:
untitled.png
Matt J
Matt J le 26 Mar 2019
Hmmm. Strange.
KSSV
KSSV le 26 Mar 2019
What version you are on?
Matt J
Matt J le 26 Mar 2019
R2018a
KSSV
KSSV le 26 Mar 2019
I am on 2017b. You can save and share only vertices3D as a mat file.....it can be checked I guess.
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.
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

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by