3D Animation using lines. What is handle and mode in this code?

조회 수: 2 (최근 30일)
Mahmud Khan
Mahmud Khan 2018년 9월 7일
댓글: Mahmud Khan 2018년 9월 11일
I have been trying to understand how to use some Matlab code that came with a book. It's meant to produce a 3D line-drawing of an object. Here is how it goes:
We start with labelling the points on the object and storing that information in XYZ:
function XYZ=spacecraftPoints
%define points on the spacecraft in local NED coordinates
XYZ = [...
1 1 0;...% point 1
1 -1 0;...% point 2
-1 -1 0;...% point 3
-1 1 0;...% point 4
1 1 0;...% point 1
1 1 -2;...% point 5
1 -1 -2;... % point 6
1 -1 0;... % point 2
1 -1 -2;... % point 6
-1 -1 -2;... % point 7
-1 -1 0;... % point 3
-1 -1 -2;... % point 7
-1 1 -2;... % point 8
-1 1 0;...% point 4
-1 1 -2;... % point 8
1 1 -2;...% point 5
1 1 0;...% point 1
1.5 1.5 0;... % point 9
1.5 -1.5 0;... % point 10
1 -1 0;...% point 2
1.5 -1.5 0;... % point 10
-1.5 -1.5 0;... % point 11
-1 -1 0;... % point 3
-1.5 -1.5 0;... % point 11
-1.5 1.5 0;... % point 12
-1 1 0;...% point 4
-1.5 1.5 0;... % point 12
1.5 1.5 0;... % point 9
]';
We define the Euler angles as phi, theta and psi and north-east-down positions as pn,pe and pd. Then we write the following code for the rotation and translation of the object:
Rotation:
function XYZ=rotate(XYZ,phi,theta,psi)
%define rotation matrix
R_roll = [...
1, 0, 0;...
0, cos(phi), -sin(phi);...
0, sin(phi), cos(phi)
];
R_pitch = [...
cos(theta), 0, sin(theta);...
0, 1, 0;...
-sin(theta), 0, cos(theta)
];
R_yaw = [...
cos(psi), -sin(psi), 0;...
sin(psi), cos(psi), 0;...
0, 0, 1
];
R = R_roll*R_pitch*R_yaw;
% rotate vertices
XYZ = R*XYZ;
Translation
function XYZ = translate(XYZ,pn,pe,pd)
XYZ = XYZ + repmat([pn;pe;pd],1,size(XYZ,2));
Then comes the bit where we are meant to produce the 3D drawing. This is where I am getting confused. The code for drawing goes like this:
function handle = drawSpacecraftBody(pn,pe,pd,phi,theta,psi, handle, mode)
%define points on spacecraft in local NED coordinates
NED = spacecraftPoints;
%rotate spacecraft by phi, theta, psi
NED = rotate(NED,phi,theta,psi);
%translate spacecraft to [pn; pe; pd]
NED = translate(NED,pn,pe,pd);
% transform vertices from NED to XYZ
R = [...
0, 1, 0;...
1, 0, 0;...
0, 0, 1;...
];
XYZ = R*NED;
%plot spacecraft
if isempty(handle),
handle = plot3(XYZ(1,:),XYZ(2,:),XYZ(3,:), 'EraseMode', mode);
else
set(handle,'XData',XYZ(1,:),'YData',XYZ(2,:),'ZData',XYZ(3,:));
drawnow
end
I can't seem to run this code because I am not sure what should I replace the "handle" and "mode" with in the input arguments of the drawSpacecraftBody.
Any suggestions?

채택된 답변

Sambit Senapati
Sambit Senapati 2018년 9월 11일
'mode' variable in the above code is used to specify the value of 'EraseMode' property for the 3D plot. Generally 'EraseMode' is set to 'none' to accumulate data in each frame of the picture. But Starting in R2014b, the 'EraseMode' property has been removed from all graphics objects. Go through this link, where other methods to animate is specified.
Now in your code do the following change:
function handle = drawSpacecraftBody(pn,pe,pd,phi,theta,psi, handle)
%define points on spacecraft in local NED coordinates
NED = spacecraftPoints;
%rotate spacecraft by phi, theta, psi
NED = rotate(NED,phi,theta,psi);
%translate spacecraft to [pn; pe; pd]
NED = translate(NED,pn,pe,pd);
% transform vertices from NED to XYZ
R = [...
0, 1, 0;...
1, 0, 0;...
0, 0, 1;...
];
XYZ = R*NED;
%plot spacecraft
if isempty(handle),
handle = plot3(XYZ(1,:),XYZ(2,:),XYZ(3,:));
else
set(handle,'XData',XYZ(1,:),'YData',XYZ(2,:),'ZData',XYZ(3,:));
drawnow
end
Now pass the 'handle' variable as an empty array as follows:
>> drawSpacecraftBody(1,1,1,pi/4,pi/4,pi/4, [])
Here I have set pn=pe=pd=1 and phi=theta=psi=pi/4.
  댓글 수: 3
Stephen23
Stephen23 2018년 9월 11일
편집: Stephen23 2018년 9월 11일
"Did you pass it an empty handle with "[]"?"
No, [] is an empty numeric array. That code only checks to see if handle is empty, it does not matter what class the empty handle has.
"Also, why do we put a space between the "," and handle or mode in functions? Is that necessary?"
No, it is optional. It has no meaning in this context.
Mahmud Khan
Mahmud Khan 2018년 9월 11일
@Stephen Cobeldick
Thanks for the clarification :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Animation에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by