From: jjspierx Spiering on
Hello all, I have searched for an answer to this question for quite some time but am unable to find a solution that works for me. Here is what I am trying to do...

I have 3 points that are within a 3D cartesian coordinate system. I want 2 of the points to form an axis and have the 3rd point rotate about this axis. I have been reading about rotation matrices but I am having trouble understanding how to create a rotation matrix and then use that matrix to transform the 3D coordinates of the 3rd point that is to rotate about the axis formed by the other 2 points. If anybody has some ideas on how to do this I would be most greatful. Thank you.
From: James Tursa on
"jjspierx Spiering" <jjspierx(a)gmail.com> wrote in message <hu0qr9$eb0$1(a)fred.mathworks.com>...
> Hello all, I have searched for an answer to this question for quite some time but am unable to find a solution that works for me. Here is what I am trying to do...
>
> I have 3 points that are within a 3D cartesian coordinate system. I want 2 of the points to form an axis and have the 3rd point rotate about this axis. I have been reading about rotation matrices but I am having trouble understanding how to create a rotation matrix and then use that matrix to transform the 3D coordinates of the 3rd point that is to rotate about the axis formed by the other 2 points. If anybody has some ideas on how to do this I would be most greatful. Thank you.

There are several methods available. One way is to form a quaternion from your two points as an eigen-axis and then pick an angle for the rotation. Then you can do a quaternion multiply to rotate the third point. Or if you need a rotation matrix instead you can easily convert the quaternion to a direction cosine matrix first. There are submissions on the FEX to do quaternion calculations. e.g., these submissions by John Fuller for tutorials and conversions:

http://www.mathworks.com/matlabcentral/fileexchange/27653-euler-angle-dcm-quaternion-and-euler-vector-conversionteaching-gui

http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors

And other FEX submissions appear to be available to do the quaternion multiply itself.

James Tursa
From: Matt J on
Here's what I use:


function R=R3d(deg,u)
%R3D - 3D Rotation matrix counter-clockwise about an axis.
%
%R=R3d(deg,axis)
%
%Input is in degrees.
%
%See also Rx,Ry,Rz,R3d,M2d,M3d

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
From: Matt J on
"jjspierx Spiering" <jjspierx(a)gmail.com> wrote in message <hu0qr9$eb0$1(a)fred.mathworks.com>...
> Hello all, I have searched for an answer to this question for quite some time but am unable to find a solution that works for me. Here is what I am trying to do...
>
> I have 3 points that are within a 3D cartesian coordinate system. I want 2 of the points to form an axis and have the 3rd point rotate about this axis. I have been reading about rotation matrices but I am having trouble understanding how to create a rotation matrix and then use that matrix to transform the 3D coordinates of the 3rd point that is to rotate about the axis formed by the other 2 points.
===========

Come to think of it, since the axis does not pass through the origin (if it did you would only need one point, not 2, to define it), it will be insufficient to perform a rotation only. You will need to do a translation as well.

The code below will generate a 4x4 matrix M expressing the rototranslation in homogeneous coordinates. You would use it as follows

M=M3d(AngleDegrees, FirstPoint-SecondPoint, SecondPoint); %transformation matrix

NewPoint=M(1:3,:) * [ThirdPoint(:);1];




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.
%
%See also: Rx,Ry,Rz, R2d, R3d, M2d


if nargin<2, x0=[0;0;0]; end

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 M=mkaff(R,t)

if nargin<2, t=[0;0;0]; end

nn=size(R,1);


M=eye(nn+1);

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


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.
%
%See also Rx,Ry,Rz,R3d,M2d,M3d

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
From: James Tursa on
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <hu11tk$g2p$1(a)fred.mathworks.com>...
> Here's what I use:
>
>
> function R=R3d(deg,u)
> %R3D - 3D Rotation matrix counter-clockwise about an axis.
> %
> %R=R3d(deg,axis)
> %
> %Input is in degrees.
> %
> %See also Rx,Ry,Rz,R3d,M2d,M3d
>
> 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

This assumes that the rotation vector intersects the origin, so any necessary translation will have to be done by the user in addition to using this function. e.g., take this example:

>> p1 = [1 0 1]';
>> p2 = [1 0 0]';
>> p3 = [2 0 1]';
>> R = R3d(180,p1-p2)
R =
-1 0 0
0 -1 0
0 0 1
>> R*p3
ans =
-2
0
1

Based on the original post as I interpret it the desired result is likely [0 0 1]', i.e. rotating p3 about an axis in space that does not intersect the origin. So to use R3d for this case one needs to pre-apply a translation and then post-apply the reverse translation, or use a form of rotation that includes the translation.

James Tursa
 |  Next  |  Last
Pages: 1 2
Prev: Tab Order in GUIDE
Next: xlswrite