简体   繁体   English

Matlab中球绕固定点的3D旋转

[英]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. 我使用rotate3d函数允许其旋转。 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. 问题是3d旋转是相对于轴心而不是相对于原点进行的。 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. 在沿轴添加绿线之后,x,y,z轴的界限会自动更改,球体的中心不再位于图形的中心。 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. “ 2”因素只是为了防止球体紧密地填充轴。 You can set it to whatever value you need. 您可以将其设置为所需的任何值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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