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

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!

 채택된 답변

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

Thanks for your quick response. I can see that it works, but I need the resulting coordinates of the newly rotated point (in the same frame of reference as the original points). The original variables haven't changed, and it doesn't look like any of the new variables are what I'm looking for... Many thanks for your help.
You just multiply the point by that matrix.
ang = acos(dot(u,v));
newpt = makehgtform('axisrotate',n,ang)*[pt1'; 1];
See, they're the same except for the scale factor:
newpt(1:3)/norm(newpt(1:3))
ans =
0.5394
0.1994
-0.8181
pt2/norm(pt2)
ans =
0.5394 0.1994 -0.8181
Great, thanks! I also get this error message;
Warning: Struct field assignment overwrites a value with class "double". See MATLAB R14SP2 Release Notes, Assigning Nonstructure Variables As Structures Displays Warning, for details.
after the lines ang=acos(dot(u,v)) g.Matrix=makehgtform('axisrotate',n,ang);
Does this matter? Many thanks!
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))
awinka
awinka 2015년 8월 19일
편집: awinka 2015년 8월 19일
Perfect, thanks very much for your help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

질문:

2015년 8월 18일

편집:

2015년 8월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by