MATLAB: Plane in 3D space

3d plotsinterpolationplane

I'm trying to define a plane in 3D for interpolation. Since it needs to be able to be any plan, my though was to first define x and y and then rotate them. Then define z, independently of x and y. That way I wanted to z-plane tilt around the x axis and rotate with x and y. In short, it would all be defined by a center point and two angles (rotation and plane tilt). However, this doesn't work when i start rotating x and y. Tilt is working fine, but the rotation seems to cause z to be place wrong or smth. Can anyone help a bit? I posted the code below…
% Set variables (later to be function input
cp = [0.0 -0.0 0.04]; % Center point coordinats [m]
xsize = 0.08; % Size along x-dim of the sample plane [m]
ysize = 0.08; % Size aling y-dim of the sample plane [m]
N = 500; % Spatiel resolution [px]
angR = 90; % Rotational angle [deg]
angT = 90; % Tilt angle [deg]
% Define standard x and y
x = linspace(-xsize/2, xsize/2, N);
y = linspace(-ysize/2*cosd(angT), ysize/2*cosd(angT), N);
% Set rotation matrix
R = [cosd(angR) -sind(angR); ...
sind(angR) cosd(angR)];
% Rotate x and y coords
XY = R*[x; y];
X = cp(1)+repmat(XY(1,:), [N 1]);
Y = cp(2)+repmat(XY(2,:)', [1 N]);
% Define Z (does not depend on X or Y)
Z = linspace((-ysize/2)*sind(angT), (ysize/2)*sind(angT), N);
Z = cp(3)+repmat(Z', [1 N]);

Best Answer

Without really trying to look at your maths, let me suggest that you will have less issues if you treat your 3D objects in a standard way and work backwards from there.
The normal way to define a plane in 3D is to specify 3 vectors: centre, up, and right. The last two define the plane's axis system and are usually unit vectors (to obtain the forward-vector of the axis you take the cross-product of up and right).
These can go into a 3x3 transformation matrix as follows:
T = [right;forward;up];
Normally, you use a 4x4 (or sometimes 4x3) transformation matrix, but let's stick with the 3x3.
You can define your rotation (rotation about Z-axis) as you already have, but in matrix form:
rotZ = @(x) [cosd(x) -sind(x) 0; sind(x) cosd(x) 0; 0 0 1];
And a the tilt is rotation about the X-axis:
rotX = @(x) [1 0 0; 0 cosd(x) -sind(x); 0 sind(x) cosd(x)];
[by the way, your rotations all seem to be clockwise, whereas anticlockwise is more standard -- I've stuck with clockwise here]
Now your initial plane transformation is the unit. Z is up, and the X- and Y-axes are on the plane.
T = diag([1 1 1]); % Unit transform
You're ready to rotate and tilt your plane:
% First tilt by 90 degrees, then rotate by 45 degrees.
S = rotZ(45) * rotX(90) * T;
You could pull your axes out like so:
X = S(1,:);
Y = S(2,:);
Z = S(3,:);
... and then linspace a mesh by projecting onto these new axes (remembering they should be unit-length)...
But remember that an arbitrary transformation will not define a mesh with a 3 simple vectors of x,y, and z co-ordinates. Every single x,y,z vector in a mesh could be unique. So that would be the wrong thing to do.
Instead you need to rotate the whole mesh. First, generate the original mesh:
x = linspace(-xsize,xsize,N) / 2;
y = linspace(-ysize,ysize,N) / 2;
[mx,my,mz] = meshgrid(x,y,0);
M = [mx(:) my(:) mz(:)];
M contains co-ords of untransformed mesh points organised in rows.
Mtrans = M * S;
Finally you add your centre point back to the transformed mesh.
Mfinal = Mtrans + repmat(cp, size(M,1), 1);
Mfinal now contains your whole mesh as a collection of points, rotated and translated however you like. It's up to you to work out what to do with it. =)