Assume that we have marked the red line segments in the following blocks, then the two anchor points marked are the two endpoints of the red line segments respectively.
When we rotate this block, we naturally hope that this label will also rotate along with the object and adjust it to the following reasonable position,
The question now is how to obtain the transformation matrix for this annotation.
At first, I thought that we only needed to obtain the transformation matrix based on the initial red position and the ending position, and then apply it to the annotation. However, after the result, I found myself thinking simple. In the second figure above, you can continue to rotate the block around the Red Line Segment, for example, to get the following shape:
We found that the second image corresponds to the vector of the red line segment in the third image, so the calculated transformation matrix must be the same as that in the second image, the result must be the following:
The correct result should be as follows:
The root cause of this problem is:A straight line cannot determine the plane.So we can't decide the plane at. We can do it. The key issue is how to select the third point. The third point must be stable relative to the red line segment, so after the block is rotated, we can still find it back. So I thought of finding the center point of the block, and then building a plane. According to the position before and after the plane, the problem was solved by obtaining the matrix of changes.
Original matrix:
Vector3d vecx = (P2-P1). Normal ();
Vector3d refvec = (centerpoint-P1). mormal ();
Vector3d vecz = vecx. crossproduct (refvec );
Vector3d vecy = vecx. crossproduct (vecz );
Matrix3d m0 = new matrix3d ();
M0.setcoordinatesystem (P1, vecx, vecy, vecz); // orignal Transform
New matrix:
Vector3d vecx' = (P2 '-p1'). Normal ();
Vector3d refvec '= (centerpoint'-p1'). mormal ();
Vector3d vecz' = vecx'. crossproduct (refvec ');
Vector3d vecy '= vecx'. crossproduct (vecz ');
Matrix3d M1 = new matrix3d ();
M1.setcoordinatesystem (P1 ', vecx', vecy', vecz'); // new transform
Transformation Matrix:
Matrix3d transform = m1 * (m0.invert ());