Avoiding buggy/jerky animation - matlab

I have this code here below. It basically rotates a box in 3D. I know I can rotate the camera using camorbit but I actually want to rotate the object, not the camera around it. So the strategy is to create an element in so(3) and take the exponential map to SO(3) as the element is varied continuously over time.
% Create object
yo = unique([-1 -1 -1; perms([1 1 -1]); perms([1 -1 -1]); 1 1 1], 'rows');
yo2 = delaunay(yo(:,1), yo(:,2), yo(:,3));
box=tetramesh(yo2, yo);
% Create prescribed rotation
seedMat=skewdec(3,1)*skewdec(3,2)*skewdec(3,3); % so(3)
t=linspace(0,20,500);
r=zeros(1,4,length(t));
reqTraj=zeros(3,3,length(t));
for i=1:length(t)
reqTraj(:,:,i)=expm(seedMat*t(i)); % SO(3)
r(:,:,i) = vrrotmat2vec(expm(seedMat*t(i))); %Axis angle form
end
% Freeze axis
axis vis3d
axis([ -2 2 -2 2 -2 2])
% Rotate
for i=1:length(t)
rotate(box,r(1,1:3,i),r(1,4,i))
pause(0.001)
end
My question is why is this animation jerky? Is there something that I can do to fix it?
EDIT: Changing the last part as follows, made the buggy/jerky behavior go away, but resulted in an entirely new rotation behavior. So i am not sure if it is the correct approach or not. Can someone comment?
% Rotate
for i=2:length(t)
rotate(box,r(1,1:3,i),r(1,4,i-1)-r(1,4,i))
pause(0.001)
end

Related

Converting an if/else statement into a code that allows for more than one choice

So I am trying to convert the following code in way that I would be able to use more than just one of the choices. (What happens inside of the cases does not matter, I would simply like to figure out how I could use more than one cases at once)
%% Creating a matrix Rot representing the rotational transformation that is applied.
theta = input('Input the value of angle: ');
% Choose the direction
Dir = input('Input around which axis the rotation occurs (x, y or z): ', 's');
if Dir == 'x'
Rot = [1 0 0;0 cosd(theta) -sind(theta);0 sind(theta) cos(theta)];
elseif Dir == 'y'
Rot = [cosd(theta) 0 sind(theta);0 1 0;0 -sind(theta) cos(theta)];
elseif Dir == 'z'
Rot = [cosd(theta) -sind(theta) 0;0 sind(theta) cos(theta);0 0 1];
else
disp('Not an axis.')
Rot = input('Input Rotational Transformation Matrix: ')
end
I tried using switches/cases or conditions, but I wasn't able to obtain a different results.
The end objective of this code is to be able to choose which direction a stress tensor will be rotated. My code works for simple cases, but i would like it to be able to calculate with a 30degree rotation in x and a 45 in y for example without rerunning the code.
To answer your question about code flow, the simplest replacement to a chain of if/elseif/else is to use a switch statement.
switch Dir
% for a single axis rotation
case 'x'
% Code for a rotation about a single axes ('X')
case 'y'
% Code for a rotation about a single axes ('Y')
case 'z'
% Code for a rotation about a single axes ('Z')
%% For complex rotation about more than one axes
case 'xy'
% Code for a rotation about 2 axes ('X' and 'Y')
case 'xz'
% Code for a rotation about 2 axes ('X' and 'Z')
case 'yz'
% Code for a rotation about 2 axes ('Y' and 'Z')
case 'xyz'
% Code for a rotation about 3 axes ('X', 'Y' and 'Z')
otherwise
% advise user to re-input "Dir"
end
Alternatively, you could also use a flag system like mentionned in #Tasos Papastylianou comment under your question. It is a bit more technical to implement but a perfectly good solution too.
Now this only take care about the code flow. The actual validity of the calculations in each case is up to you. For the rotation about more than one axis, remember that the order in which you apply the rotations is important: rotating around X first, then around Y, can yield a different result than rotating around Y first then X, even if the rotation angles for each axis were the same.

How can I make the Earth rotate around it's axis in matlab?

So I want to represent the Earth rotating for a number of seconds from a tspan knowing that a full rotation happens in 86160 seconds. It means that for 239.33 seconds , the Earth rotates with a degree. The problem is that I don t know how to use the rotate command in a right way.
This is the code:
tspan=[0 :72000];
[X,Y,Z]=sphere(50);
R=6400000;
earth = imread('earth.jpg');
globe= surf(-X*R,Y*R,-Z*R);
image_file='earth.jpg';
cdata = imread(image_file);
set(globe, 'FaceColor', 'texturemap', 'CData', cdata, 'EdgeColor', 'none');
set(gcf,'Color','k')
set(gca, 'visible', 'off')
axis equal
view (90,0)
rotating=1; % 1 degree ever 239.33 seconds from tspan
rotate(earth, [0 1 0],1) % test to see if it's working
instead of rotating the earth, rotate the camera using view, try
...
axis equal
for n=0:90
view(90+n,0)
pause(1)
end

How to continuously update 2 plots and plotted Camera in same figure (MATLAB)

My goal is to continuously plot the position & orientation of camera relative to marker using MATLAB.
There are three thing to plot.(1)camera (2)all circle points (3)origin point '*'
Both the camera and points will be moving in each frame.I plotted all these three things in a static figure i.e. using hold on. as shown in the attached figure.
Now i want to plot them all continuously (real time) in the same figure as the values change.Till now i have only been able to dynamically update only one of these things i.e. the circular points for some random values. If I add another plot ,it conflicts.Can you please tell me how to update multiple plots in same figure and the plotCamera.
hF = figure;
hAx = gca;
im_pt_2world=rand(3,3);
x_o=im_pt_2world(1,1); y_o=im_pt_2world(1,2); %origin of the calibrated world points
x_ip=im_pt_2world(:,1); y_ip=im_pt_2world(:,2);
hpoints = plot3(x_ip, y_ip,zeros(size(im_pt_2world, 1),1),'ro');
% I added the "ishandle" so the program will end in case u closed the figure
while (1) & ishandle(hpoints)
%instead of using plot I directly change the data in the line
% this is faster the plot if only because you don't need to reedefine the limits and labels...
im_pt_2world=rand(3,3);
x_ip=im_pt_2world(:,1); y_ip=im_pt_2world(:,2);
set(hpoints,'ydata',y_ip);
set(hpoints,'xdata',x_ip);
drawnow %updates the display
end
The plotCamera function (Computer Vision System Toolbox) returns a handle to the graphical object, which you can manipulate programmatically. Changing the object's properties, such as Location and Orientation will move the camera in the plot. The help example for plotCamera shows how to make the camera fly in a circle:
% Plot a camera pointing along the Y-axis
R = [1 0 0;
0 0 -1;
0 1 0];
% Setting opacity of the camera to zero for faster animation.
cam = plotCamera('Location', [10 0 20], 'Orientation', R, 'Opacity', 0);
% Set view properties
grid on
axis equal
axis manual
% Make the space large enough for the animation.
xlim([-15, 20]);
ylim([-15, 20]);
zlim([15, 25]);
% Make the camera fly in a circle
for theta = 0:pi/64:10*pi
% Rotation about cameras y-axis
T = [cos(theta) 0 sin(theta);
0 1 0;
-sin(theta) 0 cos(theta)];
cam.Orientation = T * R;
cam.Location = [10 * cos(theta), 10 * sin(theta), 20];
drawnow();
end
If you really want to have fun with this, you can supply a function handle to be called when you click on the camera. For example, the function can display the image that the camera sees.

MATLAB keeps updating the wrong plot

I'm really hoping this isn't some stupid little thing I'm missing, but I've been trying to figure this thing out for a couple hours now and haven't made any progress. Basically, I've created a figure with 3 subplots. For neatness, I made a function out of setting up the plots, then I made another function that constantly updates the plot with real time data.
The problem is, in the real time update function, the third subplot works perfectly fine, but now I want my second subplot to also real-time update, which I have not gotten done yet. For some reason, whenever I put in my code to access the 2nd plot, it keeps updating the 3rd plot and writing right over it! Meanwhile, the second plot stays in it's initial state...
Here is the code...I've looked over it a billion times so I'm at a loss at this point, I don't know what else to do....like I said really hoping it isn't something dumb lol...thanks a bunch =).
EDIT: The part that's going wrong starts where it says "%refresh plot
Real Time Plot Function
function [ ] = EndoSliceViewerJP( Naviparam, DICOMparam)
%RGBparam should be included later - add +1 to nargin values
%visualizes:
%1st: RGB camera Live view
%2nd: Orientation and Position of Navigation System
%3rd: DICOM Slice relative to navigated Endoscope in a and its orientation
%in a Slice perpendicular to the endoscope
%assumes Navigation system running with referenced tool (Naviparam.tool=4 or Naviparam.tool=5)
%currently this plots slices according to Endoscope position, could add
%vector in plot that shows orientation of the scope...
disp('Endo Slice Viewer');
disp('" ": exit on space key');
global kpressed;
kpressed = 0;
global Fig
Fig=EndoSliceViewer_createFigure(1);
set(Fig.fig,'KeyPressFcn','global kpressed; global Fig; kpressed = get(Fig.fig,''CurrentChar'');');
%create matrices and filter for smoothing of Endo Slice Data
xrel=-(ones(Fig.resolEndoSlice,1)*(1:Fig.resolEndoSlice)-Fig.resolEndoSlice/2);
yrel=-(ones(Fig.resolEndoSlice,1)*(1:Fig.resolEndoSlice)-Fig.resolEndoSlice/2);
SLimage=zeros(Fig.resolEndoSlice,Fig.resolEndoSlice);
PosVec=zeros(Fig.resolEndoSlice,Fig.resolEndoSlice,3);
gfilt = fspecial('gaussian',5,1.5);
depth = 50;
exitflag = 0;
while (exitflag == 0)
%check on keyboard input
if kpressed ~= 0
switch kpressed
case 'r'
depth=depth+2
case 'f'
depth=depth-2
case ' '
exitflag = 1;
disp('**** Exit Endo Slice Viewer ****')
end
kpressed = 0;
end
if (nargin>=1) %Naviparam is passed - update Navigation View
%capture new navigation data
Naviparam=Navi_acquire(Naviparam);
Naviparam=Navi_calc_data(Naviparam);
%refreshN avigation View
%NOT YET IMPLEMENTED: UPDATE NAVIGATION PLOT
if (nargin==2) %DICOMparam is also passed - update EndoSlice View
EndoVecX=inv(DICOMparam.calib.navi2dicom(1:3,1:3))*inv(Naviparam.data.Endo_RefHomMat(1:3,1:3))*[1;0;0];
EndoVecY=inv(DICOMparam.calib.navi2dicom(1:3,1:3))*inv(Naviparam.data.Endo_RefHomMat(1:3,1:3))*[0;1;0];
EndoVecZ=inv(DICOMparam.calib.navi2dicom(1:3,1:3))*inv(Naviparam.data.Endo_RefHomMat(1:3,1:3))*[0;0;-1];
EndoVecX=EndoVecX/norm(EndoVecX);
EndoVecY=EndoVecY/norm(EndoVecY);
EndoVecZ=EndoVecZ/norm(EndoVecZ);
mask=ones(Fig.resolEndoSlice,Fig.resolEndoSlice);
S=[DICOMparam.Sx; DICOMparam.Sy; DICOMparam.Sz];
DICOMPos = DICOMparam.calib.navi2dicom*[Naviparam.data.Endo_RefOffsetPosVec;1];
for i=1:3
%Point on Plane defined by Endo Position plus distance*Viewing direction vector
PosVec(:,:,i)=(DICOMPos(i)+depth*EndoVecZ(i))+xrel*EndoVecX(i)+yrel*EndoVecY(i);
%limit positions to integer values inside DICOM data cube
PosVec(:,:,i)=round(PosVec(:,:,i));
PosVec(:,:,i)=min(max(PosVec(:,:,i),1),S(i));
%create mask to set Points outside the data cube to 0
mask=double(PosVec(:,:,i)>1).*double(PosVec(:,:,i)<S(i).*mask(:,:));
end
%access data cube via indexed labelling
XposTemp=PosVec(:,:,1); YposTemp=PosVec(:,:,2); ZposTemp=PosVec(:,:,3);
indexTemp=sub2ind(size(DICOMparam.Vd), XposTemp(:), YposTemp(:),ZposTemp(:));
SLimage(:)=DICOMparam.Vd(indexTemp(:));
SLimage=SLimage.*mask;
SLimage=imfilter(SLimage,gfilt);
%refresh plot
set(Fig.sub3im, 'cdata', SLimage);
hold on;
Fig.sub2im=plot3(PosVec(1),PosVec(2),PosVec(3),'b*',PosVec(1)+depth*EndoVecZ(1),PosVec(2)-depth*EndoVecZ(2),PosVec(3)+depth*EndoVecZ(3),'r*');
hold off;
end
end
%RGBparam is always passed - update RGB camera View
%capture new RGB data
%handles.RGBparam=RGB_acquire(handles.RGBparam);
%refresh RGB camera View
%set(Fig.sub1im, 'CData', imresize(handles.RGBparam.image,[Fig.resolEndoRGB(1) Fig.resolEndoRGB(2)]));
drawnow;
end
close(Fig.fig);
clear global;
end
And here is my function setting up the plot
function [Fig] = EndoSliceViewer_createFigure(Figindex)
%This function creates and returns a Figure object to visualizes DICOM data
%in the plane orthogonal to the endoscopic view, the RGB view of the camera
%and the orientation of the navigation
%set resolution for Endo Slice Plot
Fig.resolEndoSlice=300;
Fig.resolEndoRGB=[720 1280];
Fig.resolEndoNavi=[500 500 500];
%init figure on screen
Fig.fig=figure(Figindex); gcf;
set(Fig.fig,'Position',[50 500 1500 500],'Name','Endo Slice Viewer');
%set(Fig.fig,'KeyPressFcn','global kpressed; global Fig; kpressed = get(Fig.fig,''CurrentChar'');');
Fig.sub1=subplot(1,3,1);
Fig.sub1im=image(uint8(zeros(Fig.resolEndoRGB(1), Fig.resolEndoRGB(2),3)));
title('Endo Camera View');
daspect([1 1 1]);
Fig.sub2=subplot(1,3,2);
Fig.BoxX=[0;1;1;0;0;0;1;1;0;0;1;1;1;1;1;0;0]*Fig.resolEndoNavi(1);
Fig.BoxY=[0;0;1;1;0;0;0;1;1;1;1;1;0;0;0;0;1]*Fig.resolEndoNavi(2);
Fig.BoxZ=[0;0;0;0;0;1;1;1;1;0;0;1;1;0;1;1;1]*Fig.resolEndoNavi(3);
Fig.sub2im=plot3(Fig.BoxX,Fig.BoxY,Fig.BoxZ);
title('Navigation View');
xlim([-0.2*Fig.resolEndoNavi(1), 1.2*Fig.resolEndoNavi(1)]);
ylim([-0.2*Fig.resolEndoNavi(2), 1.2*Fig.resolEndoNavi(2)]);
zlim([-0.2*Fig.resolEndoNavi(3), 1.2*Fig.resolEndoNavi(3)]);
xlabel('X [vox]');
ylabel('Y [vox]');
zlabel('Z [vox]');
daspect([1 1 1]);
Fig.sub3=subplot(1,3,3);
Fig.sub3im=imagesc(zeros(Fig.resolEndoSlice, Fig.resolEndoSlice));
title('Endo Slice View');
xlim([0 Fig.resolEndoSlice]);
ylim([0 Fig.resolEndoSlice]);
xlabel('Xendo [vox]');
ylabel('Yendo [vox]');
daspect([1 1 1]);
colormap bone
drawnow;
%potentially: add subplot for navigation position display later
end
You need to set your second subplot as the current axes before plotting anything in it. You can use axes(Fig.sub2) before the plotting command.

Rotate and Move an half-circle Matlab

I plot an half-circle and I rotate it with :
t=linspace(0,pi,1000);
x=r*cos(t);
y=r*sin(t);
h=plot(-2+x,y);
rotate(h,[0 0 -1],1,[0.5 3.71 0]);
now, I would like to move/shif the half-circle from the center , such that the half-circle changes position but I want that it maintains also the rotation done.
Who can help me?
To shift for example 10 units in the x direction and 5 in the y direction:
set(h,'XData',get(h,'XData')+10)
set(h,'YData',get(h,'YData')+5)
As you see, it's done by modifying the properties of the h object
Disregard my above comments, you can actually shift data around after it has been plotted. You can modify the XData and YData values as long as you have a handle to the plot that you created
Do something like this
clc; close all; clear all;
t=linspace(0,pi,1000);
r = 5
x=r*cos(t);
y=r*sin(t);
h=plot(-2+x,y);
rotate(h,[0 0 -1],1,[0.5 3.71 0]);
xShift = 5;
yShift = 5;
set(h,'XData',get(h,'XData')+xShift)
set(h,'YData',get(h,'YData')+yShift)
Just an additional answer to demonstrate hgtransform, the others will work well too.
t=linspace(0,pi,1000);
x=r*cos(t);
y=r*sin(t);
h=plot(-2+x,y);
rotate(h,[0 0 -1],1,[0.5 3.71 0]);
hgt=hgtransform('parent',gca);
set(h,'parent',hgt);
Tx = makehgtform('translate',[10 -5 0]); % translate +10 x, -5 y, 0 z
set(hgt,'Matrix',Tx);
drawnow;
You could also potentially use the hgtranform to do the rotation at the same time, but it might take a bit of fiddling around to get the axis origin to work as you have above:
RxTx = makehgtform(xrotate',0.5,'yrotate',3.71,'translate',[10 -5 0]);
set(hgt,'Matrix',RxTx);
drawnow;