Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a smooth rotation of a 3D plot in MATLAB?

If I try to rotate camera around my current figure with plot3 using

while true; camorbit(0.9,-0.1); drawnow; end

then the rotation periodically hangs for a while (example) even on 8-core MacPro.

Can I make it smooth?

EDIT1:

While there is no solution for my original question yet, I've managed to make a better movie with getframe function. It doesn't allow recording free-hand rotation, though, and is quite buggy in MATLAB2010b for Mac.

%# fix wrong figure position in MATLAB2010b for Mac - depends on your layout
correctedPosition = get(gcf,'Position') + [21 -125 0 0];

fps = 60; sec = 10;

vidObj = VideoWriter('newfile.avi');
vidObj.Quality = 100;
vidObj.FrameRate = fps;

open(vidObj);
for i=1:fps*sec
  camorbit(0.9,-0.1);
  writeVideo(vidObj,getframe(gcf, correctedPosition));
end
close(vidObj);

EDIT2:

I created a similar thread at MATLAB Central.

EDIT3:

You can try it yourself downloading one of my figures.

like image 799
Andrei Fokau Avatar asked Dec 02 '10 19:12

Andrei Fokau


People also ask

How do you rotate a 3D plot in Matlab?

r = rotate3d creates a rotate3d object for the current figure. This syntax is useful for customizing the rotation mode and style. r = rotate3d( fig ) creates a rotate3d object for the specified figure.

How do you rotate a 3D plot?

Rotating 3D GraphsTo rotate the 3D graph around the X axis press CTRL and drag. To rotate around the Y axis, press SHIFT and drag.

How do you rotate a 90 degree plot in Matlab?

Description. B = rot90( A ) rotates array A counterclockwise by 90 degrees. For multidimensional arrays, rot90 rotates in the plane formed by the first and second dimensions. B = rot90( A , k ) rotates array A counterclockwise by k*90 degrees, where k is an integer.


2 Answers

I would say it's the large number of points you are drawing that's causing the slowdown. One option is to downsample.. Also you could use lower-level functions to draw (check this related post for a comparison of plot3/scatter3/line performance).

Consider the animation below optimized for speed:

[X Y Z] = sphere(64);
X = X(:); Y = Y(:); Z = Z(:);

%# set-up figure
hFig = figure('Backingstore','off', 'renderer','zbuffer');

%# use lower-level function LINE
line(0.50*[X,X], 0.50*[Y,Y], 0.50*[Z,Z], 'LineStyle','none', 'Marker','.', 'MarkerSize',1, 'Color','r')
line(0.75*[X,X], 0.75*[Y,Y], 0.75*[Z,Z], 'LineStyle','none', 'Marker','.', 'MarkerSize',1, 'Color','g')
line(1.00*[X,X], 1.00*[Y,Y], 1.00*[Z,Z], 'LineStyle','none', 'Marker','.', 'MarkerSize',1, 'Color','b')
view(3)

%# freeze the aspect ratio to override stretch-to-fill behaviour
axis vis3d

%# fix the axes limits manually
%#set(gca, 'xlim',[-1 1], 'ylim',[-1 1], 'zlim',[-1 1])
axis manual

%# maybe even remove the tick labels
%set(gca, 'xticklabel',[], 'yticklabel',[], 'zticklabel',[])

%# animate (until figure is closed)
while ishandle(hFig); camorbit(0.9,-0.1); drawnow; end

alt text

Note how we are using the Z-buffer renderer, and turned off the Backingstore property.


EDIT:

If I understood correctly, what you are trying to do is to record a screencast (using a 3rd-party app), while you manually rotate the figure, but in your case these manual rotations are "jumpy". On the other animating your figure with CAMORBIT/VIEW in a while-loop is running smooth...

I propose an alternative solution: start by rotating the figure using the mouse and write these view configurations at each step (azimuth,elevation). Then you can replay them using the VIEW function while recording the video, something like:

v = [...];   %# matrix where each row specify Az/El of view
for i=1:size(v,1)
    view( v(i,:) )
    drawnow
end

The downside is that you will have to press/rotate/release using the mouse in small steps (the ROTATE3D object does not expose a mouse-motion event)

I wrote a simple function to help you in this process. It loads the saved figure, enable 3d-rotation, and keeps track of the intermediate position at each step. Once finished, press the "Done" button to return the list of views...

function v = rotationDemo(figFileName)
    views = [];                     %# list of views (Az,El)

    hFig = hgload(figFileName);     %# load the saved figure

    views(1,:) = get(gca,'View');   %# store initial view

    %# add a button, used to terminate the process
    hButton = uicontrol('Style','pushbutton', 'Position',[400 1 80 20], ...
                        'String','Done?', 'Callback',@buttonCallback);
    set(hFig, 'Toolbar','figure')   %# restore toolbar

    %# start 3d rotation, and handle post-callback to record intermediate views
    h = rotate3d(hFig);             %# get rotation object
    set(h, 'ActionPostCallback',@rotateCallback)
    set(h, 'Enable','on')           %# enable rotation

    msgbox('Rotate the view step-by-step', 'rotate3d', 'warn', 'modal')

    uiwait(hFig)                    %# wait for user to click button
    delete(hButton)                 %# delete button on finish
    set(h, 'Enable','off')          %# disable rotation
    v = round(views);               %# return the list of views

    %# callback functions
    function rotateCallback(o,e)
        views(end+1,:) = get(e.Axes,'View');  %# add current view to list
    end
    function buttonCallback(o,e)
        uiresume(gcbf)                        %# uiresume(hFig)
    end
end

alt text

You can call the above function, then replay the animation:

v = rotationDemo('smooth_rotation.fig');
for i=1:size(v,1)
    view(v(i,:))
    drawnow
end

We can smooth the transitions by simple interpolation:

v = rotationDemo('smooth_rotation.fig');
n = size(v,1);
nn = linspace(1,n,100)';     %'# use 100 steps
vv = round( [interp1(v(:,1),nn) interp1(v(:,2),nn)] );
for i=1:size(vv,1)
    view(vv(i,:))
    DRAWNOW                  %# or PAUSE(..) to slow it down
end

As a side note, I should mention that ROTATE3D and CAMORBIT have different effects. ROTATE3D changes the View property of the current axis, while CAMORBIT controls the camera properties CameraTarget/CameraPosition/CameraUpVector of the current axis.

like image 81
Amro Avatar answered Nov 15 '22 17:11

Amro


I recognize the same jerking movements that you are talking about on a regular MATLAB Figure. But when I tried running the Amro's code, created a movie (*.AVI), it looks smooth on my Mac notebook also.

The movie making code that I used is the following:

% Added the 'Visible' property of the figure 'off' while making a movie (although I am not exactly certain if this will make the situation better) like so:

hFig = figure('Backingstore','off','visible','off','renderer','zbuffer');

% Then, I replaced Amro's while-loop with a simple AVI production loop, as follows:

aviobj=avifile('test.avi'); %creates AVI file

for I=1:360

camorbit(0.9,-0.1); drawnow;

aviobj=addframe(aviobj,hFig); %adds frames to the AVI file

end

aviobj=close(aviobj); %closes AVI file

close(hFig); %close hFig

Question: Would it help to decimate some points or to create a density map before rendering the figure?

[Ref. on various Rendering Options: http://www.mathworks.com/support/tech-notes/1200/1201.html ]

I hope the comments above would be of any help.

like image 45
Y.T. Avatar answered Nov 15 '22 17:11

Y.T.