필터 지우기
필터 지우기

Rotation of Ellipse to a specific vector

조회 수: 11 (최근 30일)
Akash V
Akash V 2021년 8월 22일
댓글: Akash V 2021년 8월 22일
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
DGM
DGM 2021년 8월 22일
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:
Akash V
Akash V 2021년 8월 22일
I am quite new to matlab. I tried using rotate() but it is not aligning properly at all. Idk whether its the mistake of (roll pitch yaw) angle value itself or whether I have to follow a specific sequence of rotation. Here is the code :
semi_axis_x = 0.085962489000000
semi_axis_y = 0.119483388000000
semi_axis_z = 0.240855299000000
vector_1_x = 0.551221118000000
vector_1_y = 0.832495512000000
vector_1_z = -0.055736010000000
vector_2_x = 0.015328738000000
vector_2_y = 0.056685361000000
vector_2_z = 0.998274411000000
vector_3_x = -0.834218382000000
vector_3_y = 0.551124299000000
vector_3_z = -0.018485062000000
[X,Y,Z] = ellipsoid(0,0,0,semi_axis_x,semi_axis_y,semi_axis_z);
hold on
s = surf(X,Y,Z);
plot3([0 vector_1_x],[0 vector_1_y],[0 vector_1_z],'b');
hold on
plot3([0 vector_2_x],[0 vector_2_y],[0 vector_2_z],'r');
hold on
plot3([0 vector_3_x],[0 vector_3_y],[0 vector_3_z],'y');
axis tight
axis equal
xlabel('My x label')
ylabel('My y label')
zlabel('My z label')

댓글을 달려면 로그인하십시오.

채택된 답변

Wan Ji
Wan Ji 2021년 8월 22일
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
Wan Ji
Wan Ji 2021년 8월 22일
편집: Wan Ji 2021년 8월 22일
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
Akash V
Akash V 2021년 8월 22일
Thanks a lot for the code and your time :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by