MATLAB Answers

rotation matrix 3D point data

116 views (last 30 days)
ha ha
ha ha on 11 May 2018
Edited: ha ha on 24 Nov 2018
Let' say , I have the 3d point data in format [xi yi zi] of 176 point as show in attachment file test.txt. The 3d point data is as below figure (shown in OXY plane):
Now, I want to find rotate the data around axis OZ , and 1 edge of the rectangle // Ox, the other //Oy as the below image. How to find new coordinate?

Accepted Answer

ha ha
ha ha on 11 May 2018
Edited: ha ha on 11 May 2018
%%Load input 3D data
clear;clc;filename = 'test.txt'; inputfile = importfile(filename);
P = inputfile(:,1:3);%get data=coordinate(x,y,z) from set of data
x = P(:,1) ; y = P(:,2) ;z = P(:,3) ; % get (x,y,z) coordinate
x0 = x-mean(x) ; y0 = y-mean(y) ; z0 = z-mean(z) ; % remove mean
P1 = [x0 y0 z0] ; %this step to bring the coord of P near to the origin. The new coord will be created
scatter3(P1(:,1),P1(:,2),P1(:,3),'b.');%plot new coord of P1
HA=[min(P1(:,1)) min(P1(:,2)) max(P1(:,3))+1];%just for better visualaztion
hold on;scatter3(HA(:,1),HA(:,2),HA(:,3),'g.');%just for better visualaztion
%%Finding principal vector of 3D data P
e1=PCA(:,1)'; e2=PCA(:,2)' ;e3=PCA(:,3)'; % 3 principal vector(3 eigenvector) of "input data"
n1=[1 0 0] ; n2=[0 1 0] ; n3=[0 0 1]; % 3 unit vector Ox,Oy,Oz
transformation matrix from "e" space to "n" space
R=[e2;e1;e3]; % rotation matrix , match with xyz (e1//n2, e2//n1, e3//n3)
% R=[e1;e2;e3]; % rotation matrix , match with xyz (e1//n1, e2//n2, e3//n3)
% R=[e3;e2;e1]; % If e1//n3, e2//n2, e3//n1
%%Finding the new rotate data
newdata1=(R*P1')';%new data corresponding to P1 coordinate
hold on; scatter3(newdata1(:,1),newdata1(:,2),newdata1(:,3),'r.');
%%Plot the original & rotation 3D data
hold on;scatter3(newdata(:,1),newdata(:,2),newdata(:,3),'r.');
HA=[min(P(:,1)) min(P(:,2)) max(P(:,3))+1];%just for better visualaztion
hold on;scatter3(HA(:,1),HA(:,2),HA(:,3),'g.');%just for better visualaztion
legend('original data(P)','rotated data(newdata)');title({'Plot the rotation matrix 3D point data';'(FINAL RESULT)'});

More Answers (1)

KSSV on 11 May 2018
You need to rotate your data by certain angle to achieve what you shown. You need to know Affine transformations. Check the below code.
coor = load('test.txt') ;
x = coor(:,1) ; y = coor(:,2) ; % (x,y) points
x0 = x-mean(x) ; y0 = y-mean(y) ; % remove mean
A = [x0 y0 ones(size(x))] ;
th = 38 ; % angle in degrees by which data is rotated
T = [cosd(th) sind(th) 0 ; -sind(th) cosd(th) 0 ; 0 0 1] ; % TRansformation matrix
At = A*T ; % rotate the dat
At = [At(:,1)+mean(x) At(:,2)+mean(y)] ; % Add mean to At
plot(x,y,'.r') ;
hold on
plot(At(:,1),At(:,2),'.b') ;
ha ha
ha ha on 11 May 2018
ok. agree, but where 38 degree come from? I guess this is the angle between rectangle & Oy?

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!