Rotation of Ellipse to a specific vector
6 views (last 30 days)
Show older comments
I have a 3d ellipse which i plotted using ellipsoid(xc,yc,zc,xr,yr,zr) function. You can also see three vectors (blue red and yellow line) which are perpendicular to each other. I want to rotate the ellipsoid such that its x_semi_axis of the ellipse align with blue line, y_semi_axis of the ellipse align with red line and z_semi_axis of the ellipse align with yellow line.Could you please share the code to do so.Thank you

2 Comments
DGM
on 22 Aug 2021
Not knowing anything about the specifics of your use case, I'd just assume you can use rotate(). There's webdocs, but there's also a few similar questions around. Here's one about using rotate() on an ellipsoid:
Accepted Answer
Wan Ji
on 22 Aug 2021
By following operator you will get what you want
clc;clear
blueLine = [-0.4, 0.8, 0]; % direction of blue line
xc = 1;
yc = 2;
zc = 3;
xr = 1;
yr = 4;
zr = 2;
figure(1)
clf
[x,y,z] = ellipsoid(xc,yc,zc,xr,yr,zr);
mesh(x,y,z)
xlabel('x')
ylabel('y')
zlabel('z')
view(30,67)
axis equal
% relative position
x = x - xc;
y = y - yc;
z = z - zc;
t = z; % y,z axis exchange
z = y;
y = t;
figure(2);clf
mesh(x,y,z)
xlabel('x')
ylabel('y')
zlabel('z')
view(30,67)
axis equal
theta = atan2(blueLine(2), blueLine(1)) - pi/2;
x = x*cos(theta) - y*sin(theta); % rotate by theta
y = x*sin(theta) + y*cos(theta);
figure(3);clf
mesh(x,y,z)
xlabel('x')
ylabel('y')
zlabel('z')
view(30,67)
axis equal
% go back to absolute coodinate
x = x + xc;
y = y + yc;
z = z + zc;
5 Comments
Wan Ji
on 22 Aug 2021
Edited: Wan Ji
on 22 Aug 2021
Yup, I have got your idea, now I believe I achieve my answer. With Axis rotation [x->e1; y->e2; z->cross(e1,e2)]. As follows
% This is an example
% x-axis to e1
% y-axis to e2
% z-axis to e3
P0 = [0,0,0]; % point origin
P1 = [3,0,0]; % point on x-axis
P2 = [0,3,0]; % point on y-axis
P3 = [0,0,3]; % point on z-axis
e1 = [1, 1, 1]/sqrt(3); % e1 vector
e2 = [-1, 0, 1]/sqrt(2); % e2 vector
figure(1); title('Axis rotation [x->e1; y->e2; z->cross(e1,e2)]')
plot3([P0(1), P1(1)], [P0(2), P1(2)], [P0(3), P1(3)],'r-','linewidth',2)
hold on
plot3([P0(1), P2(1)], [P0(2), P2(2)], [P0(3), P2(3)], 'g-','linewidth',2)
plot3([P0(1), P3(1)], [P0(2), P3(2)], [P0(3), P3(3)],'b-','linewidth',2)
text(P1(1), P1(2),P1(3),'x')
text(P2(1), P2(2),P2(3),'y')
text(P3(1), P3(2),P3(3),'z')
P1_rot = rotate3d(e1, e2, P1)*2;
P2_rot = rotate3d(e1, e2, P2)*2;
P3_rot = rotate3d(e1, e2, P3)*2;
plot3([P0(1), P1_rot(1)], [P0(2), P1_rot(2)], [P0(3), P1_rot(3)],'r--','linewidth',2)
hold on
plot3([P0(1), P2_rot(1)], [P0(2), P2_rot(2)], [P0(3), P2_rot(3)], 'g--','linewidth',2)
plot3([P0(1), P3_rot(1)], [P0(2), P3_rot(2)], [P0(3), P3_rot(3)],'b--','linewidth',2)
text(P1_rot(1), P1_rot(2),P1_rot(3),'e1','color','c')
text(P2_rot(1), P2_rot(2),P2_rot(3),'e2','color','c')
text(P3_rot(1), P3_rot(2),P3_rot(3),'e3','color','c')
grid on
[x,y,z] = ellipsoid(0,0,0,2,1,0.5); % with centre at the origin
mesh(x,y,z,'facecolor','m','edgecolor','k', 'facealpha',0.4);
xyz = rotate3d(e1,e2,[x(:),y(:),z(:)]); % rotate the ellipsoid coordinate
xp = reshape(xyz(:,1), size(x)); % recover the coordinate of x-component
yp = reshape(xyz(:,2), size(y)); % recover the coordinate of y-component
zp = reshape(xyz(:,3), size(z)); % recover the coordinate of z-component
mesh(xp,yp,zp,'facecolor','b','edgecolor','w', 'facealpha',0.4); % mesh the rotated ellipsoid

The figure speaks!
rotate3d function is given here
function p = rotate3d(e1, e2, coordinate)
% coordinate must be n*3 matrix
% rotate x-axis to e1 vector
% rotate y-axis to e2 vector
% rotate z-axis to cross(e1,e2) vector
e1 = e1 / norm(e1);
e2 = e2 / norm(e2);
e1 = reshape(e1,1,3);
e2 = reshape(e2,1,3);
e3 = cross(e1, e2);
R = [e1;e2;e3];
p = coordinate*R;
end
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!