rotate 3d body according to a normal in matlab
I have 3d body (triangle mesh) and a surfuce normal to one of the faces. I want to rotate the body such that the Z axis would be parallel to the normal. How 开发者_开发问答to find the rotation matrix?
If you parent the body to an hgtransform object, then you can use the makehgtform command to create a transform matrix for that object. The simplest way is to use this form of makehgtform:
h = hgtransform;
m = makehgtform('axisrotate', [ax ay az], r);
set(h, 'Matrix', m);
The axisrotate option lets you rotate around an arbitrary axis. In this case, [ax ay az] is a vector which is normal to the plane you want to rotate in. That's the plane which passes through the two vectors you're trying to rotate between. So you use the cross product of the Z axis and the normal. That gives you a vector which is normal to both of your vectors, and that defines the orientation of a plane which passes through both of them.
Now you need to figure out the rotation angle. The value r is the angle you want to rotate around that axis in radians. There are a couple of ways to get that. It's simplest if your two vectors are normalized. Then the magnitude of the cross product is equal to the sin of the angle, and the magnitude of the dot product is equal to the cos of the angle. Does that make sense?
You can easily implement it by yourself. The matrix for general rotations can be found at wikipedia. You just need to know cos() and sin() functions for the angles. If your surface normal vector n is a vector of unit length (i.e. |n|=1), then you already know the corresponding cos().
| cos(angle between normal and x-axis) |
n = | cos(angle between normal and y-axis) |
| cos(angle between normal and z-axis) |
精彩评论