matlab单位向量_[LOG]三维旋转的MATLAB实现
0x00 三维旋转的表达⽅式
三维空间中常⽤的表⽰旋转的⽅式有:
[1]旋转矩阵(rotation matrix)
[2]旋转向量(rotation vector)/⾓轴(轴⾓)(axis angle)
[3]欧拉⾓(euler angles)
[4]四元数(quaternion)
主动旋转和被动旋转:主动旋转是指将向量或坐标系逆时针围绕旋转轴旋转,被动旋转是对坐标轴进⾏的逆时针旋转,相当于主动旋转的逆操作。
0x01 rotate函数
rotate是matlab官⽅提供的三维旋转图形函数。
旋转矩阵R,再通过R得到旋转后的图像:
轴⾓,使⽤下列公式转换得到旋转矩阵R
rotate通过给定轴⾓
rotate(h,direction,alpha,origin)
[1]h是绘制对象。
[2]direction 是⼀个⼆元素或三元素向量,它与旋转轴原点共同确定旋转轴。
[3]direction ⼆元素形式中,theta 是 x 的正轴在 x-y 平⾯中的逆时针⾓度。phi 是⽅向向量在 x-y 平⾯中的仰⾓。
[4]direction 三元素形式中,指定使⽤笛卡尔坐标的轴⽅向,也就是XYZ轴。⽅向向量是从旋转原点到 P 的向量。
[5]alpha是旋转⾓度。
[6]origin是旋转轴原点,默认(0,0,0),是可选参数。
[7]旋转轴由direction(对它进⾏归⼀化处理后得到的点P)和origin(旋转原点)共同确定。
坐标轴」旋转:
旋转轴」为「坐标轴
⽰例 单
单绕「旋转轴」
clc;
clear;
[X,Y,Z] = peaks;
%
subplot(1,2,1)
surf(X,Y,Z);
title("原始");
%
subplot(1,2,2)
h=surf(X,Y,Z);
rotate(h,[1,0,0],90,[0,0,0]);
title("官⽅函数,绕X轴,逆时针旋转90°");
%matlab绘图运⽤右⼿坐标系。
%在右⼿坐标系中,旋转⾓的正⽅向为逆时针⽅向。
任意轴」旋转:
鲳鱼的做法大全
旋转轴」为「任意轴
单绕「旋转轴」
⽰例 单
clc;
clear;
[X,Y,Z] = peaks;
%
subplot(1,2,1)
surf(X,Y,Z);
title("原始");流量免费领取
首当其冲近义词
%
subplot(1,2,2)
h=surf(X,Y,Z);
direction=[1,1,0];
origin=[0,0,0];
rotate(h,direction,90,origin);
title("官⽅函数,绕direction归⼀化后得到的点P[0.7071,0.7071,0]与origin共同确定的旋转轴旋转,逆时针旋转90°");
副研究员是什么级别%[1]注意,这条旋转轴不再是某条坐标轴了。
%[2]踩坑提⽰,千万不要认为[1,1,0],90是指先绕x轴旋转90°再绕y轴旋转90°,可能被官⽅⽂档⼀处例⼦误导。
%[3]向量归⼀化指[向量中的每个元素]依次除以该[向量的模],使该向量成为单位向量,单位向量是指
模等于1的向量,⽅向任意,有⽆数个。%[4]单位向量=原向量(:)/norm(原向量);
%[5]rotate源码第44⾏计算点P也就是u。
%[6]官⽅函数⽆法原⽂档编辑,可以复制⼀份到⾃建.m⽂件即可编辑。
坐标轴」旋转:
痛经怎么办快速止痛旋转轴」为「坐标轴
连绕「旋转轴」
⽰例 连绕
clc;
clear;
[X,Y,Z] = peaks;
%
subplot(2,3,1)
surf(X,Y,Z);
title("原始");
%
subplot(2,3,4)
h1=surf(X,Y,Z);
rotate(h1,[1,0,0],60,[0,0,0]);
title("官⽅函数,绕X轴旋转,逆时针旋转60°");
%
subplot(2,3,5)
h2=surf(X,Y,Z);
rotate(h2,[1,0,0],60,[0,0,0]);
rotate(h2,[0,1,0],60,[0,0,0]);
title("官⽅函数,先绕X轴旋转,再绕Y轴旋转,逆时针旋转60°");
%
subplot(2,3,6)
h3=surf(X,Y,Z);
rotate(h3,[1,0,0],60,[0,0,0]);
rotate(h3,[0,1,0],30,[0,0,0]);
rotate(h3,[0,0,1],45,[0,0,0]);
title("官⽅函数,先绕X轴旋转60°,再绕Y轴旋转30°,最后绕Z轴旋转45°");
rotate源码:
function rotate(h,azel,alpha,origin)
%ROTATE Rotate objects about specified origin and direction.
% ROTATE(H,[THETA PHI],ALPHA) rotates the objects with handles H % through angle ALPHA about an axis described by the 2-element
% direction vector [THETA PHI] (spherical coordinates).
% All the angles are in degrees. The handles in H must be children
% of the same axes.
%
% THETA is the angle in the xy plane counterclockwi from the
% positive x axis. PHI is the elevation of the direction vector
% from the xy plane (e also SPH2CART). Positive ALPHA is defined % as the righthand-rule angle about the direction vector as it
% extends from the origin.
如愿以偿什么意思%
% ROTATE(H,[X Y Z],ALPHA) rotates the objects about the direction % vector [X Y Z] (Cartesian coordinates). The direction vector
% is the vector from the center of the plot box to (X,Y,Z).
%
% ROTATE(...,ORIGIN) us the point ORIGIN = [x0,y0,z0] as
% the center of rotation instead of the center of the plot box.
%
% See also SPH2CART, CART2SPH.
% Copyright 1984-2017 The MathWorks, Inc.
% Determine the default origin (center of plot box).
if nargin < 4
声情if ~isgraphics(h)
error(message('MATLAB:rotate:InvalidHandle'));
end
ax = ancestor(h(1),'axes');
if impty(ax) || ax==0
error(message('MATLAB:rotate:InvalidHandle'));
end
origin = sum([get(ax,'xlim')' get(ax,'ylim')' get(ax,'zlim')'])/2;
end
% find unit vector for axis of rotation
if numel(azel) == 2 % theta, phi
theta = pi*azel(1)/180;
phi = pi*azel(2)/180;
u = [cos(phi)*cos(theta); cos(phi)*sin(theta); sin(phi)];
elif numel(azel) == 3 % direction vector
u = azel(:)/norm(azel);
整脊疗法end
alph = alpha*pi/180;
cosa = cos(alph);
sina = sin(alph);
vera = 1 - cosa;
x = u(1);
y = u(2);
z = u(3);
rot = [cosa+x^2*vera x*y*vera-z*sina x*z*vera+y*sina; ... x*y*vera+z*sina cosa+y^2*vera y*z*vera-x*sina; ... x*z*vera-y*sina y*z*vera+x*sina cosa+z^2*vera]';
for i=1:numel(h)
t = get(h(i),'type');
skip = 0;
if strcmp(t,'surface') || strcmp(t,'line') || strcmp(t,'patch')
% If patch, rotate vertices
if strcmp(t,'patch')
verts = get(h(i),'Vertices');
x = verts(:,1); y = verts(:,2);
if size(verts,2)>2
z = verts(:,3);
el
z = [];
end
% If surface or line, rotate {x,y,z}data
el
x = get(h(i),'xdata');
y = get(h(i),'ydata');
z = get(h(i),'zdata');
end
if impty(z)
z = -origin(3)*ones(size(y));
end
[m,n] = size(z);
if numel(x) < m*n
[x,y] = meshgrid(x,y);
end
elif strcmp(t,'text')
p = get(h(i),'position');
x = p(1); y = p(2); z = p(3);
elif strcmp(t,'image')
x = get(h(i),'xdata');
y = get(h(i),'ydata');
z = zeros(size(x));
el
skip = 1;
end
if ~skip
[m,n] = size(x);
newxyz = [x(:)-origin(1), y(:)-origin(2), z(:)-origin(3)]; newxyz = newxyz*rot;
newx = origin(1) + reshape(newxyz(:,1),m,n);
newy = origin(2) + reshape(newxyz(:,2),m,n);
newz = origin(3) + reshape(newxyz(:,3),m,n);
if strcmp(t,'surface') || strcmp(t,'line')
t(h(i),'xdata',newx,'ydata',newy,'zdata',newz);
elif strcmp(t,'patch')
t(h(i),'Vertices',[newx,newy,newz]);
elif strcmp(t,'text')