3D Animation using lines. What is handle and mode in this code?
Mostra commenti meno recenti
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?
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Geographic Plots in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
