
Rotate a 3D plane such that its new normal coincides with or parallel to a given vector
19 views (last 30 days)
Show older comments
Hi
I have a plan which contains 4 points c1 , c2, c3, c4(forms rectangle/square). M is normal to the plane. Now I need to rotate the plane such that its normal is paralle to or coincides to vector N. I tried using vrrotvec, axang2rotm , vrrotvec2mat but unable to get the correct output. I am attaching a figure where rectangle or square before rotation is in red and rectangle or square after rotation is in blue and the new normal is green. As you can see green line is not perpendicular or normal to the blue rectangle

Below image is same image but in different line of sight

function [c1n, c2n, c3n, c4n,rotationMatrixCheck, check] = rotateScreen(height, width, positionVector1,...
positionVector2)
c1 = [height/2, width/2, 0];
c2 = [-height/2, width/2, 0];
c3 = [-height/2, -width/2, 0];
c4 = [height/2, -width/2, 0];
M = cross(c3-c1, c3-c2)/ norm(cross(c3-c1, c3-c2));
N = (positionVector1-positionVector2)/ norm(positionVector1-positionVector2);
r = vrrotvec(M,N);
%rmat = axang2rotm(r);
rmat = vrrotvec2mat(r);
r1 = vrrotmat2vec(rmat);
if r1 == r
disp('same')
else
disp('not same')
end
rotationMatrixCheck = rmat* rmat'
c1n = rmat*c1';
c2n = rmat*c2';
c3n = rmat*c3';
c4n=rmat*c4';
v=[c1;c2];
plot3(v(:,1),v(:,2),v(:,3),'r')
hold on
v=[c1;c4];
plot3(v(:,1),v(:,2),v(:,3),'r')
hold on
v=[c2;c3];
plot3(v(:,1),v(:,2),v(:,3),'r')
hold on
v=[c3;c4];
plot3(v(:,1),v(:,2),v(:,3),'r')
hold on
v=[c1n';c2n'];
plot3(v(:,1),v(:,2),v(:,3),'b')
hold on
v=[c1n';c4n'];
plot3(v(:,1),v(:,2),v(:,3),'b')
hold on
v=[c2n';c3n'];
plot3(v(:,1),v(:,2),v(:,3),'b')
hold on
v=[c3n';c4n'];
plot3(v(:,1),v(:,2),v(:,3),'b')
hold on
%plot3([0,0,0]);
normalNew = cross(c3n-c1n, c3n-c2n)/ norm(cross(c3n-c1n, c3n-c2n));
check = cross(normalNew,N);
v1 = [positionVector1;positionVector2];
%v = [subpath_point;[S.x,S.y,S.z]];
plot3(v1(:,1),v1(:,2),v1(:,3),'g');
xlabel("X axis")
ylabel("Y axis")
zlabel("Z axis")
hold off
end
Input used to get the images above is
[c1n, c2n, c3n, c4n, rotationMatrixCheck, check] = rotateScreen(4, 4, [ 10 2 5],[1 20 -3])
In short I need to find rotation matrix such that 3D planes new normal is parallel to or coincides with the given vector
0 Comments
Answers (1)
Milan Bansal
on 9 Oct 2023
Hi Swetha Rani Polemoni
As per my understanding you are facing issues while rotating a rectangle or square such its new normal aligns with the given vector N.
Firstly, find the rotation axis and angle of rotation between the vector N and the normal M to the rectangle/square.
Use the "axang2rotm" to calculate the rotation matrix which can be used to rotate the plane by the angle of rotation.
Refer to the following code to modify the function.
function [c1n, c2n, c3n, c4n,rotationMatrixCheck, check] = rotateScreen(height, width, positionVector1,...
positionVector2)
c1 = [height/2, width/2, 0];
c2 = [-height/2, width/2, 0];
c3 = [-height/2, -width/2, 0];
c4 = [height/2, -width/2, 0];
M = cross(c3-c1, c3-c2)/ norm(cross(c3-c1, c3-c2));
N = (positionVector1-positionVector2)/ norm(positionVector1-positionVector2);
% Calculate rotation axis and angle of rotation
rotationAxis = cross(M, N);
rotationAxis = rotationAxis / norm(rotationAxis);
rotationAngle = acos(dot(M, N) / (norm(M) * norm(N)));
% Create a rotation matrix using the axis-angle representation
rmat = axang2rotm([rotationAxis, rotationAngle]);
rotationMatrixCheck = rmat* rmat'
c1n = rmat*c1';
c2n = rmat*c2';
c3n = rmat*c3';
c4n=rmat*c4';
v=[c1;c2];
plot3(v(:,1),v(:,2),v(:,3),'r')
hold on
v=[c1;c4];
plot3(v(:,1),v(:,2),v(:,3),'r')
hold on
v=[c2;c3];
plot3(v(:,1),v(:,2),v(:,3),'r')
hold on
v=[c3;c4];
plot3(v(:,1),v(:,2),v(:,3),'r')
hold on
v=[c1n';c2n'];
plot3(v(:,1),v(:,2),v(:,3),'b')
hold on
v=[c1n';c4n'];
plot3(v(:,1),v(:,2),v(:,3),'b')
hold on
v=[c2n';c3n'];
plot3(v(:,1),v(:,2),v(:,3),'b')
hold on
v=[c3n';c4n'];
plot3(v(:,1),v(:,2),v(:,3),'b')
hold on
%plot3([0,0,0]);
normalNew = cross(c3n-c1n, c3n-c2n)/ norm(cross(c3n-c1n, c3n-c2n));
check = cross(normalNew,N);
v1 = [positionVector1;positionVector2];
%v = [subpath_point;[S.x,S.y,S.z]];
plot3(v1(:,1),v1(:,2),v1(:,3),'g');
xlabel("X axis")
ylabel("Y axis")
zlabel("Z axis")
hold off
end

Refer to the following documentation link to learn more about the "axang2rotm" function.
Hope this helps!
0 Comments
See Also
Categories
Find more on Geometric Transformation and Image Registration 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!