如何使用旋转矩阵旋转3D点

时间:2014-02-18 15:31:16

标签: matlab matrix rotation matrix-multiplication rotational-matrices

下面我有两组不同的代码试图做同样的事情。我希望能够采用多个极点[n,3]并为每个omega [n x 1]创建一个旋转矩阵。我正在努力弄清楚我应该如何存储和操纵旋转矩阵。

我也试过用3D矩阵做这个,但遇到了一些不支持3D矩阵的功能问题。

function [R] = rotationMatrix(pole,omega)
% omega = degree of rotation
% pole = x y z vector signifying pole to rotate about
% Pole [x y z] and omega are in radians 
Ex = pole(:,1);
Ey = pole(:,2);
Ez = pole(:,3);

%% 
% R11 = Ex.*Ex.*(1-cos(omega))+cos(omega);
% R12 = Ex.*Ey.*(1-cos(omega))-Ez*sin(omega);
% R13 = Ex*Ez*(1-cos(omega))+Ey*sin(omega);
% 
% R21 = Ey*Ex*(1-cos(omega))+Ez*sin(omega);
% R22 = Ey*Ey*(1-cos(omega))+cos(omega);
% R23 = Ey*Ez*(1-cos(omega))-Ex*sin(omega);
% 
% R31 = Ez*Ex*(1-cos(omega))-Ey*sin(omega);
% R32 = Ez*Ey*(1-cos(omega))+Ex*sin(omega);
% R33 = Ez*Ez*(1-cos(omega))+cos(omega);

R11 = bsxfun(@plus,bsxfun(@times,bsxfun(@times,Ex,Ex),bsxfun(@minus,1,cos(omega))),cos(omega));
R12 = bsxfun(@minus,bsxfun(@times,bsxfun(@times,Ex,Ey),bsxfun(@minus,1,cos(omega))),bsxfun(@times,Ez,sin(omega)));
R13 = bsxfun(@plus,bsxfun(@times,bsxfun(@times,Ex,Ez),bsxfun(@minus,1,cos(omega))),bsxfun(@times,Ey,sin(omega)));

R21 = bsxfun(@plus,bsxfun(@times,bsxfun(@times,Ey,Ex),bsxfun(@minus,1,cos(omega))),bsxfun(@times,Ez,sin(omega)));
R22 = bsxfun(@plus,bsxfun(@times,bsxfun(@times,Ey,Ey),bsxfun(@minus,1,cos(omega))),cos(omega));
R23 = bsxfun(@minus,bsxfun(@times,bsxfun(@times,Ey,Ez),bsxfun(@minus,1,cos(omega))),bsxfun(@times,Ex,sin(omega)));

R31 = bsxfun(@minus,bsxfun(@times,bsxfun(@times,Ez,Ex),bsxfun(@minus,1,cos(omega))),bsxfun(@times,Ey,sin(omega)));
R32 = bsxfun(@plus,bsxfun(@times,bsxfun(@times,Ez,Ey),bsxfun(@minus,1,cos(omega))),bsxfun(@times,Ex,sin(omega)));
R33 = bsxfun(@plus,bsxfun(@times,bsxfun(@times,Ez,Ez),bsxfun(@minus,1,cos(omega))),cos(omega));

R = [R11 R12 R13;
    R21 R22 R23;
    R31 R32 R33];
end

根据评论建议编辑

例如,如果我有一个2x3的矢量矩阵

     [x y z] = sph2cart(deg2rad(312),deg2rad(-37),1)
poles(:,:,1) = [ x, y, z];
poles(:,:,2) = [0.4, -0.3, 0.1];
poles(:,:,3) = [-0.1, 0.4, -0.5];

我要旋转的1x3角度矩阵

omega(:,:,1) = degtorad(65);
omega(:,:,2) = 0.92;
omega(:,:,3) = 0.48;

运行函数rotationMatrix(poles, omega)

R = rotationMatrix(poles, omega)

我以3x3x3

的形式得到了这个值矩阵
R(:,:,1) =

     0.587503610427254          0.36230591007508         -0.72358408997131
    -0.728553373601606         0.625997769999203        -0.278094900653973
     0.352206600660266         0.690551388008664         0.631735143054941
etc...

现在我想用这个矩阵3x1x3

旋转一些坐标
A(:,:,1) = [-0.604 0.720 0.342]';
A(:,:,2) = [-0.604 0.720 0.342]';
A(:,:,3) = [-0.604 0.720 0.342]';

现在我要旋转这些点,但看起来我不能做矩阵乘法..

>> Ap = R*A
Error using  * 
Inputs must be 2-D, or at least one input must be scalar.
To compute elementwise TIMES, use TIMES (.*) instead.

>> Ap = R.*A
Array dimensions must match for binary array op.

使用bsxfun也不起作用,因为它返回3x3矩阵,当它应该是3x1

Ap = bsxfun(@times,R,A)

Ap(:,:,1) =

    -0.354852180698061        -0.218832769685349         0.437044790342671
    -0.524558428993156         0.450718394399426        -0.200228328470861
     0.120454657425811         0.236168574698963          0.21605341892479
etc...

我的直接解决方案是构建一个for循环。它可能不是最有效的方法。

for m = 1:length(poles)
    Ap(m,:) = R(:,:,m)*A(:,:,m);
end

如何在3D空间中进行矩阵乘法?

干杯, 丹尼尔

0 个答案:

没有答案