3D matrix rotations to make two matrices have the same 3D direction cosines

6 views (last 30 days)
I have two different points, and I'm trying to rotate one of the points about [0,0,0] so the 3D direction cosines (angle to the axes from the origin) will be equal - like aligning the points, but length of the vector doesn't matter, just the angle from the origin.
I have calculated the direction cosine for each axis and each point. I then tried subtracting the direction cosine angles then rotating one point by this, in each direction in turn. But the direction cosines of the new point position don't match... I feel like I might be doing something wrong when calculating the rotation angle required?
Any help would be great, thanks!

Accepted Answer

Mike Garrity
Mike Garrity on 18 Aug 2015
Usually the best way to think about this is as rotation about an axis. Consider this example:
origin = [0 0 0];
pt1 = randn(1,3);
pt2 = randn(1,3);
scatter3(origin(1),origin(2),origin(3),'Marker','o')
hold on
line([origin(1) pt1(1)],[origin(2) pt1(2)],[origin(3) pt1(3)],'Color','red')
line([origin(1) pt2(1)],[origin(2) pt2(2)],[origin(3) pt2(3)],'Color','green')
axis vis3d
axis equal
xlim([-2 2])
ylim([-2 2])
zlim([-2 2])
We want an axis which is normal to the plane which passes through our two points and the origin. We can get that with a cross product:
u = pt1-origin;
v = pt2-origin;
u = u/norm(u);
v = v/norm(v);
n = cross(u,v);
n = n/norm(n)
line([origin(1) n(1)],[origin(2) n(2)],[origin(3) n(3)],'Color',[.75 .75 .75])
Now we can generate rotation matrices like so:
makehgtform('axisrotate',n,ang);
So all we need is the correct value for ang. The dot product of the two vectors is the cosine of the angle:
g = hgtransform;
line([origin(1) pt1(1)], ...
[origin(2) pt1(2)], ...
[origin(3) pt1(3)], ...
'Color','blue','LineStyle',':','LineWidth',2,'Parent',g)
ang = acos(dot(u,v));
g.Matrix = makehgtform('axisrotate',n,ang);
The one thing to watch out for he is that you need to do a little bookkeeping with the sign of your dot product. Sometimes you'll find yourself going around the axis the "wrong" way.
Does that make sense?
  5 Comments
Mike Garrity
Mike Garrity on 19 Aug 2015
That "dot notation" for working with graphics objects was introduced in R2014b. In earlier versions, that line would look like this:
set(g,'Matrix',makehgtform('axisrotate',n,ang))

Sign in to comment.

More Answers (0)

Categories

Find more on Axes Appearance in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!