MATLAB: How to rotate ellipsoid correctly in MATLAB

ellipsoidrotate

I am attempting to create, rotate, then translate an ellipsoid to the middle of two points (ex: (0,0,0) and (100,100,100)).
%set up figure
figure
xlabel('x')
ylabel('y')
zlabel('z')
grid on
%create ellipsoid at origin with:
%x semi-axis = 10, y semi-axis = 3, z %semi-axis = 3
[x,y,z] = ellipsoid(0,0,0, 10,3,3,30);
%create surface out of ellipsoid
h = surf(x,y,z);
hold on
%rotate surface -45 degrees on Y axis (Pitch)
rotate(h, [0 1 0], -45);
%rotate surface 45 degrees on Z axis (Yaw)
rotate(h, [0 0 1], 45);
%get X,Y,Z data points for translation
ch = get(gca, 'children');
X = get(ch, 'Xdata');
Y = get(ch, 'Ydata');
Z = get(ch, 'Zdata');
%create two points: one at origin, the other at (100,100,100)
pointA = [0 0 0];
pointB = [100 100 100];
%translate ellipsoid to midpoint between pointA and pointB
X = X + (pointA(1) + pointB(1))/2;
Y = Y + (pointA(2) + pointB(2))/2;
Z = Z + (pointA(3) + pointB(3))/2;
%plot rotated+translated ellipsoid
surf(X,Y,Z);
%remove original ellipsoid from figure
delete(h);
hold on
%create and plot line from (0,0,0) to (100,100,100)
lineX = [ pointA(1) pointB(1)];
lineY = [ pointA(2) pointB(2)];
lineZ = [ pointA(3) pointB(3)];
plot3(lineX,lineY,lineZ);
Here is my result:
As shown, the vertical rotation of the ellipsoid doesn't line up with the line from (0,0,0) to (100,100,100).
What am I doing wrong?

Best Answer

What am I doing wrong?
It's just the wrong sequence of rotations. The composition of a -45 degree rotation about Y followed by a 45 degree rotation about Z does not align the x-axis with the direction of your line.
>> Rz(45)*Ry(-45)*[1;0;0]
ans =
0.5000
0.5000
0.7071
One of many sequences that will do this (I don't know how you want the other y,z axes transformed) is
rotate(h, [1 0 0], -15);
rotate(h, [0 1 0], -35.264389682754661);
rotate(h, [0 0 1], 45);