MATLAB: Given a plane defined by three points in space, how to get the Roll and Pitch angles (Euler angles) to rotate this plane to a plane parallel to the Z=0 plane

3d plotsMATLABplane rotation

I have three points in space P1, P2, P3, that obviously define a plane in space. I want to find the Roll and Pitch anlges in order to rotate this plane to a plane parallel to the Z=0 plane. For instance to a plane with Z equal to the center point of the three points or something like that. I have written the following code (I am a biginner so it might be a very badly written code) to show the question with the end result. What is missing is the actual math to do this and the Roll and Pitch angles ?
Thank you in advnace for any help.
% The goal is to find the Roll and Pitch angles (in degrees) needed to rotate a given
% plane (defined by three points in space) to a plane parallel to the Z=0 plane.
clear;clc;
%file with the 3 coplanar points
%load('data.txt')
% The three coplanar points are
data=[-11.35 8.67 18.692; -11.39 10.67 18.68; -9.39 10.7 18.685]
% extract the three points in vectors P1, P2, P3
P1=data(1,:);
P2=data(2,:);
P3=data(3,:);
% get the normal to the plane as well as the d value for the plane equation
% ax+by+cz+d = 0
normal=cross(P1-P2, P1-P3);
normal=normal/norm(normal);
d=-(normal(1)*data(3,1)+normal(2)*data(3,2)+normal(3)*data(3,3));
% create a mesh of 100 by 100 X,Y points to calcualte their Z values
% given the equation of the palane
n = 100;
x = linspace(min(data(:,1)),max(data(:,1)),n)';
y = linspace(min(data(:,2)),max(data(:,2)),n)';
[X, Y]= meshgrid(x,y);
Z=(-d-normal(1)*X-normal(2)*Y)/normal(3);
% get a point in the plance near the center for the rotation.
% I may need it for the rotation.
xo=(data(1,1)+data(3,1))/2;
yo=(data(1,2)+data(3,2))/2;
zo=(data(1,3)+data(3,3))/2;
%% for illustration make the plane to which I want the original plane
% to rotate to but via a rotation
Xf=X;
Yf=Y;
Zf=Z./Z*zo;
%% plot the original plane, its normal vector and the plane I want the original plane to rotate to
figure(1)
surf(X,Y,Z)
axis([-12 -9 8 11 18.675 18.7])
hold on
% trying to plot the vector normal to the plane in the sample figure
plot3(xo,yo,zo,'ro')
plot3(xo,yo,zo,xo+normal(1),yo+normal(2),zo+0.007*normal(3),'ro')
hold on
% plot the plane I want to get to after the rotation
surf(Xf,Yf,Zf)
hold off

Best Answer

I want to find the Roll and Pitch anlges in order to rotate this plane to a plane parallel to the Z=0 plane.
You don't need the angles to rotate the plane. You just need the rotation matrix, which you can get directly using absor (Download).
A=orth([P1(:)-P2(:),P1(:)-P3(:)]);
B=[1 0 0; 0 1 0].';
reg=absor(A,B,'doTrans',0);
rotMatrix=reg.R;