# help converting 2D rotating affine matrix to 3D rotating tia sal22

Discussion in 'MATLAB' started by Rick T, Mar 18, 2011.

1. ### Rick TGuest

help converting 2D rotating affine matrix to 3D rotating

Greetings All
I have a function that creates an affine matrix that works for
2D rotation around an arbitrary point but now I would like
it to work for 3D rotations also. The working function example is
below
with it's example function:

%affine matrix rotation about a point
rtaffine[theta,rpx,rpy,sigx,sigy,sigz]
%crx cry rpx rpy represent center of rotation
function [rotsig,theta,crpx,crpy,sigx,sigy,sigz] =
rtaffine(theta,rpx,rpy,sigx,sigy,sigz) %format in
rrtaffine[theta,rpx,rpy,sigx,sigy,sigz]
rotsig=[];
%affinematrix=[];
siga=[sigx;sigy;sigz];

r00 = cosd(theta); r01 = -sind(theta); r10 = sind(theta); r11 =
cosd(theta);
affinematrix = [r00, r01, rpx(1,1) - r00*rpx(1,1) - r01*rpy(1,1);...
r10, r11, rpy(1,1) - r10*rpx(1,1) - r11*rpy(1,1);...
0, 0, 1];
rotsig=affinematrix*siga; %new affine matrix
endfunction

Example code to use with function below:

clear all, clc,clf,tic
t= linspace(0,2*pi,1000); %from 0 to 1 with 100 equal spaces between
freq=1;
y=sin(freq*(t)); %pi will change phase of angle (t+pi)
y2=sin(freq*(t)); %pi will change phase of angle (t+pi)
z = ones(size(y));
siga=[t;y;z];
siga2=[t;y2;z];
rpcalc=[t(1,size(t,2)/2),0]; %size(b,2) %will count all the rows with
a 1, count all colmns with 2
rp=[t(1,size(t,2)/2),0]; %rotation point half way on x/t axis
rotsigt=[];
for theta=0:1:360
rotsig=rtaffine(theta,rp(1,1),rp(1,2),siga(1,,siga(2,,siga(3,);

rotsig2=rtaffine((theta*2),rp(1,1),rp(1,2),siga(1,,siga(2,,siga(3,);
rotsigcomb=rotsig+rotsig2;
axis([-10 15 -10 10])

plot(0,0,'g*',rp(1,1),rp(1,2),'r*',rotsig(1,,rotsig(2,,'b-',rotsig2(1,,rotsig2(2,,'r',rotsigcomb(1,,rotsigcomb(2,,'g')

%plot(0,0,'g*',rp(1,1),rp(1,2),'r*',rotsigcomb(1,,rotsigcomb(2,,rotsig2(1,,rotsig2(2,)
axis([-10 15 -10 10])
title(['Phase Shift is ',num2str(theta),' deg']);
grid on
pause(.001)
%rotsigt=[rotsigt,,rotsig,];
end
fprintf('- Complete re-import test in %4.4fsec or %4.4fmins\n',toc,toc/
60);

Any suggestions?
tia sal22

Rick T, Mar 18, 2011

2. ### Matt JGuest

=================

You mean 3D rotation about an axis? Here's what I use:

function M=M3d(deg,u,x0)
%Generate 4x4 roto-translation matrix for the rotation around an arbitrary line in 3D.
%
% M=M3d(deg,u,x0)
%
%in:
%
% deg: The counter-clockwise rotation about the line in degrees.
% u,x0: 3D vectors specifying the line in parametric form x(t)=x0+t*u
% Default for x0=0 (pure rotation).
%out:
%
% M: A 4x4 homogenous coordinate transform matrix representing
% the roto-translation.

provide('x0', 0);

x0=x0); u=u)/norm(u);

AxisShift=x0-(x0.'*u).*u;

Mshift=mkaff(eye(3),-AxisShift);

Mroto=mkaff(R3d(deg,u));

M=inv(Mshift)*Mroto*Mshift;

Matt J, Mar 18, 2011

3. ### Matt JGuest

Whoops. It calls a few other functions that I forgot to include

function M=M3d(deg,u,x0)
%Generate roto-translation matrix for the rotation around an arbitrary line in 3D.
%
% M=M3d(deg,u,x0)
%
%in:
%
% deg: The counter-clockwise rotation about the line in degrees.
% u,x0: 3D vectors specifying the line in parametric form x(t)=x0+t*u
% Default for x0=0 (pure rotation).
%out:
%
% M: A 4x4 homogenous coordinate transform matrix representing
% the roto-translation.
%

provide('x0', 0);

x0=x0); u=u)/norm(u);

AxisShift=x0-(x0.'*u).*u;

Mshift=mkaff(eye(3),-AxisShift);

Mroto=mkaff(R3d(deg,u));

M=inv(Mshift)*Mroto*Mshift;

function R=R3d(deg,u)
%R3D - 3D Rotation matrix counter-clockwise about an axis.
%
%R=R3d(deg,axis)
%
% deg: The counter-clockwise rotation about the axis in degrees.
%

R=eye(3);
u=u)/norm(u);
x=deg; %abbreviation

for ii=1:3

v=R,ii);

R,ii)=v*cosd(x) + cross(u,v)*sind(x) + (u.'*v)*(1-cosd(x))*u;
%Rodrigues' formula

end

function M=mkaff(varargin)
% M=mkaff(R,t)
% M=mkaff(R)
% M=mkaff(t)
%
%Makes an affine transformation matrix, either in 2D or 3D.
%For 3D transformations, this has the form
%
% M=[R,t;[0 0 0 1]]
%
%where R is a square matrix and t is a translation vector (column or row)
%
%When multiplied with vectors [x;y;z;1] it gives [x';y';z;1] which accomplishes the
%the corresponding affine transformation
%
% [x';y';z']=R*[x;y;z]+t
%

if nargin==1

switch numel(varargin{1})

case {4,9} %Only rotation provided, 2D or 3D

R=varargin{1};
nn=size(R,1);
t=zeros(nn,1);

case {2,3}

t=varargin{1};
nn=length(t);
R=eye(nn);

end
else

[R,t]=deal(varargin{1:2});
nn=size(R,1);
end

t=t);

M=eye(nn+1);

M(1:end-1,1:end-1)=R;
M(1:end-1,end)=t);

Matt J, Mar 18, 2011
4. ### Rick TGuest

Thanks Matt but your function doesn't seem to work can you post an

tia sal22

Rick T, Mar 21, 2011
5. ### Matt JGuest

Matt J, Mar 28, 2011