简体   繁体   中英

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. The goal is to tilt the surface so that all of the z values are the same, meaning that the surface is level.

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. Then, simply take the solution where the maximum number of z-coordinates fall within a narrow range. 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) .

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) .

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). 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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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