简体   繁体   中英

3D rotation of a sphere around a fixed point in Matlab

I have a sphere centered on the origin of the axes. I use the rotate3d function to allow its rotation. However, when I rotate it, it seems to move in the space with having a fixed point for the rotation. I would like to fix the origin as rotation center. How can I achieve it?

Here is my code:

function ex
global state;
fh = figure('Menu','none','Toolbar','none','Units','characters',...
    'Renderer','OpenGL');
hPanAni = uipanel('parent',fh,'Units','characters','Position',...
    [22.6 10.4 53 23],'title','Controls','FontSize',11,...
    'FontAngle','italic','FontWeight','bold');
hIniAni = uicontrol(hPanAni,'Style','pushbutton','Units','normalized',...
    'Position',[0.14 0.75 0.5 0.12],'String','Spin',...
    'FontSize',10,'Callback',@hIniAniCallback);
hFinAni = uicontrol(hPanAni,'Style','pushbutton','Units','normalized',...
    'Position',[0.14 0.5 0.5 0.12],'String','Stop',...
    'FontSize',10,'Callback',@hFinAniCallback);
hResetAni = uicontrol(hPanAni,'Style','pushbutton','Units','normalized',...
    'Position',[0.14 0.25 0.5 0.12],'String','Reset',...
    'FontSize',10,'Callback',@hResetAniCallback);
hPantSim = uipanel('Parent',fh,'Units','characters',...
    'Position',[107.87 8 157.447 42],'BorderType','none','title',...
    'Screen','FontSize',11,'FontAngle','italic',...
    'FontWeight','bold');
hPantSimInt = uipanel('Parent',hPantSim,'Units','normalized','Position',...
    [0 0 1 1],'BorderType','line','BackgroundColor','k');
axes('units','normalized','position',[0,0,1,1],'Parent',...
    hPantSimInt);
stars = rand(60,2);
scatter(stars(:,1),stars(:,2),6,'y','Marker','+');
axis off;
ah4 = axes('Parent',hPantSimInt,'Units','normalized','Position',...
    [0 0 1 1],'Color','none','Visible','off','DataAspectRatio',...
    [1 1 1],'NextPlot','add');
T1 = 0:pi/1000:2*pi;
Fin = numel(T1);
if (Fin>1000)
    Incr = floor(Fin/1000);
else
    Incr = 1;
end
Y = zeros(numel(T1),3);
Y(:,1) = 7000*cos(T1);
Y(:,2) = 7000*sin(T1);
R_esf = 6378;
[x_esf,y_esf,z_esf] = sphere(50);
x_esf = R_esf*x_esf;
y_esf = R_esf*y_esf;
z_esf = R_esf*z_esf;
props.FaceColor= 'texture';
props.EdgeColor = 'none';
props.Parent = ah4;
surface(x_esf,y_esf,z_esf,props);
handles.psat = line('parent',ah4,'XData',Y(1,1), 'YData',Y(1,2),...
    'ZData',Y(1,3),'Marker','o', 'MarkerSize',10,'MarkerFaceColor','b');
line([0 1.5*R_esf],[0 0],[0 0],'LineWidth',3,'Color','g');
line([0 0],[0 1.5*R_esf],[0 0],'LineWidth',3,'Color','g');
line([0 0],[0 0],[0 1.5*R_esf],'LineWidth',3,'Color','g');
pbaspect([1 1 1]);
axis vis3d;
rotate3d(ah4);
view([atan2(Y(1,2),Y(1,1)),0]);
az = 0;
k = 2;
ind_ini = 0;
state = 0;
       function hIniAniCallback(hObject,evt)
           tic;
            if (ind_ini == 1)
              return;  
            end
            ind_ini = 1;
            state = 0;
            while (k<=Fin)
            set(handles.psat,'XData',Y(k,1),'YData',Y(k,2),'ZData',Y(k,3));
            pause(0.002);
            if (k ==  Fin)
                toc;
            end
            k = k + Incr;

            if (state == 1)
                state = 0;
                break;
            end
            end 
       end

    function hFinAniCallback(hObject,evt)
        ind_ini = 0;
        state = 1;
    end
 function hResetAniCallback(hObject,evt)
    set([handles.psat],'Visible','off');
    ind_ini = 0;
    state = 1;
    az = 0;
    k = 2;
    handles.psat = line('parent',ah4,'XData',Y(1,1), 'YData',Y(1,2),...
    'ZData',Y(1,3),'Marker','o', 'MarkerSize',10,'MarkerFaceColor','b');
end

end

The problem is that the 3d rotation is done with respect to the center of your axes, and not with respect to the origin. After you add the green lines along the axes, the limits of the x,y,z axes is automatically changed, the center of sphere is no longer positioned in the center of the figure. Add the following line after drawing all the lines:

ax_limits = 2*[-R_esf R_esf];
set (ah4, 'xlim', ax_limits, 'ylim', ax_limits, 'zlim', ax_limits)

The '2' factor is just to prevent the sphere from filling your axes tightly. You can set it to whatever value you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM