How can i plot multiple objects in same 3D space i.e. plot3 ?

37 views (last 30 days)
I need to visualize multiple objects i.e. 2D circle and 2D ellipse in same 3d space (plot3)..
i have shared codes of both cirle and ellipse .. please guide me.
%CODE FOR 2D CIRCLE IN 3D SPACE
center = [0.5 0 0.4]; %[x y z]
radius = 0.1;
dt = 0.25;
t = (0:dt:10)';
theta = t*(2*pi/t(end))-(pi/2);
points = center + radius*[0*ones(size(theta)) cos(theta) sin(theta)];
hold on;
plot3(points(:,1),points(:,2),points(:,3),'-*w', 'LineWidth', 0.002);
xlabel('x');
ylabel('y');
zlabel('z');
axis auto;
view([60,10]);
grid('minor');
%CODE FOR 2D ELLIPSE IN 3D SPACE
gtobj=ellipticalFit.groundtruth([], [0,0],[0.075,0.15]/2,90); %Define the ellipe in 2D
xy=gtobj.sample(0:20:360); %Define waypoint coordinates
axis auto;
view([-41,-33])
hEllipse=plot(gtobj,{'Color','g'}); %Make plot in xy plane first
hold on
hWayPts=scatter(xy{:},'filled','k','SizeData',50);
hold off
xlabel 'X', ylabel 'Y', zlabel 'Z'
T=makehgtform('translate',[0.4,0,0.4],'yrotate',pi/2); %rototranslate the plot out of xy plane
set([hEllipse,hWayPts],'Parent',hgtransform('Matrix',T))

Answers (2)

DGM
DGM on 6 Apr 2021
Something like this?
center = [0.5 0 0.4]; %[x y z]
radius = 0.1;
dt = 0.25;
t = (0:dt:10)';
theta = t*(2*pi/t(end))-(pi/2);
points = center + radius*[0*ones(size(theta)) cos(theta) sin(theta)];
hold on;
plot3(points(:,1),points(:,2),points(:,3),'-*r', 'LineWidth', 0.002);
% what is this?
% gtobj=ellipticalFit.groundtruth([], [0,0],[0.075,0.15]/2,90);
% since i can't plot something i don't have, i'll just plot another circle
center = [0.5 0 0.4]; %[x y z]
radius = 0.08;
dt = 0.25;
t = (0:dt:10)';
theta = t*(2*pi/t(end))-(pi/2);
points = center + radius*[cos(theta) 0*ones(size(theta)) sin(theta)];
plot3(points(:,1),points(:,2),points(:,3),'-*b', 'LineWidth', 0.002);
% wait until the plots are done to add the labels and stuff
xlabel('x');
ylabel('y');
zlabel('z');
axis equal
view([60,10]);
grid('minor');
I have no idea what ellipticalFit.groundtruth is, I can't demonstrate it.

Matt J
Matt J on 6 Apr 2021
Edited: Matt J on 6 Apr 2021
Using hold on and hold off, you can add anything you want to a pre-existing 3D axis, even the results of a 2D plotting command. The result of a 2D plotting command will be placed in the xy-plane. (Note that some plotting commands like line() or surface() will add to an existing axis without the need to issue hold.)
Once you have added a 2D plot to a 3D axis, you can re-orient it arbitrarily in 3D space using hgtransform. So for example, your 3D circle could also have been generated using the 2D plot() command as follows:
center = [0.5 0 0.4]; %[x y z]
radius = 0.1;
dt = 0.25;
t = (0:dt:10)';
theta = t*(2*pi/t(end))-(pi/2);
xlabel('x');
ylabel('y');
zlabel('z');
axis auto;
view([60,10]);
grid('minor');
hold on
T=makehgtform('translate',center,'yrotate',pi/2);
plot(radius*cos(theta),radius*sin(theta),'-*r', 'Parent', hgtransform('Matrix',T))
hold off
I should caution you that hgtransform has shown some quirky behavior in R2020b as discussed in this thread,
Basically though, as long as you do not modify the axes limits after the hgtransform is created, you should be fine.
  1 Comment
Matt J
Matt J on 6 Apr 2021
Edited: Matt J on 6 Apr 2021
For an ellipse, it's the same kind of thing,
hold on
axis vis3d;
T=makehgtform('translate',center,'yrotate',pi/2);
plot(2*radius*cos(theta),radius*sin(theta),'-*r', 'Parent', hgtransform('Matrix',T))
hold off

Sign in to comment.

Categories

Find more on Graphics Performance 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!