简体   繁体   English

旋转3D表面直到在MATLAB上水平

[英]Rotate 3D Surface until Level on MATLAB

I have yet to start creating the code because I am a little stuck and would like some help to guide me in the right direction. 我尚未开始创建代码,因为我有些困惑,希望获得一些帮助以指导我朝正确的方向发展。 I have to use MATLAB to solve the following issue: I am given the x,y,z of a 3D surface (about 300 points) that is tilted by an arbitrary angle theta about the x axis and tilted by an arbitrary angle alpha about the y axis. 我必须使用MATLAB来解决以下问题:给定3D表面(大约300个点)的x,y,z,该表面绕x轴倾斜任意角度theta,相对于x轴倾斜任意角度alpha y轴。 The goal is to tilt the surface so that all of the z values are the same, meaning that the surface is level. 目的是使表面倾斜,使所有z值都相同,这意味着表面是水平的。

I have tried using rotation matrices, but it has not worked out how I expected. 我曾尝试使用旋转矩阵,但仍未达到我的预期。 Any suggestions and ideas are greatly appreciated. 任何建议和想法都将不胜感激。

It's not elegant, but you could try brute forcing the solution by rotating the graph in set increments about both the x and y axes. 这不是很优雅,但是您可以尝试通过围绕x和y轴以设定的增量旋转图形来强行强制解决方案。 Then, simply take the solution where the maximum number of z-coordinates fall within a narrow range. 然后,简单地采取最大z坐标数在一个狭窄范围内的解决方案。 This would mean that the surface is approximately level. 这意味着表面大约是水平的。

A similar approach but a bit more formal would be to calculated the vector parallel to the surface and knowing the direction of this vector, the rotation parameters got really easy to obtain by c=norm((a,b,c))*cos(angle) . 一种类似的方法,但更为正式的方法是计算与表面平行的矢量并知道该矢量的方向,通过c=norm((a,b,c))*cos(angle)

If you have a plane-like surface, you could fit it the to a plane equation a*x+b*y+c*z+d=0 and the vector normal to plane is (a,b,c) . 如果您有一个类似于平面的曲面,则可以将其拟合到一个平面方程a*x+b*y+c*z+d=0且垂直于平面的向量为(a,b,c)

If you have another type of surface -- that is normally what one has -- you may still find the vector of the plane useful, as the plane may be parallel to the surface or may give the direction to compare to made the rotation. 如果您拥有另一种类型的表面(通常是这种类型的表面),您可能仍然会发现该平面的矢量很有用,因为该平面可能与该表面平行,或者可能给出进行旋转的比较方向。

Depending on how you implement it, it will just rotate having point of rotation at the origin (0,0,0). 根据实现方式的不同,它会旋转,旋转点位于原点(0,0,0)。 If you plane is far from the origin, both figures will be far from each other. 如果您的飞机离原点很远,则两个数字将彼此远离。 A simple translation can solve it. 一个简单的翻译就可以解决。

With some random point around a plane i got it: 在飞机周围有一些随机点,我明白了: 旋转飞机

With the code that i used. 与我使用的代码。

n=300; 
x= 20.*rand(n,1)-10; %interval
y= 20.*rand(n,1)-10; %interval
%if you want to plot a plane
z=(2*(y+rand(n,1)*0.3)+2*(x+rand(n,1)*0.3)-7)/.4+rand(n,1)*0.3;

figure()

plot3(x,y,z,'.')
hold on

%here i put the plane average data on zero
Centerplane=[mean(x) mean(y) mean(z)];
xc=x-mean(x); 
yc=y-mean(y);
zc=z-mean(z);

%get the 'a' and 'b' component of the plane equation (z=a*x +b*y +d)
A=[xc yc]; B=zc;
r=A\B %%%Matlab!
r = vrrotvec( (([r(1) r(2) 1]/norm([r(1) r(2) 1]))),[0 0 1]);
RM=vrrotvec2mat(r); %get the rotation matrix
rdata=[xc,yc,zc]*RM; %rotate data

% put in the proper position back
rt_c_x=rdata(:,1)+mean(x);
rt_c_y=rdata(:,2)+mean(y);
rt_c_z=rdata(:,3)+mean(z);

%and plot
plot3(rt_c_x,rt_c_y,rt_c_z,'c.')

A=[x y]; B=z; %default data
r=A\B %%%Matlab!
r = vrrotvec( (([r(1) r(2) 1]/norm([r(1) r(2) 1]))),[0 0 1]);
RM=vrrotvec2mat(r); %get the rotation matrix
rdata2=[x,y,z]*RM; %rotate data

%and plot
plot3(rdata2(:,1),rdata2(:,2),rdata2(:,3),'g.')

xlabel('X')
ylabel('Y')
zlabel('Z')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM