Skip to content

Instantly share code, notes, and snippets.

@tushev
Last active February 16, 2021 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tushev/b5c162c059b5e1c0011809ae0e871015 to your computer and use it in GitHub Desktop.
Save tushev/b5c162c059b5e1c0011809ae0e871015 to your computer and use it in GitHub Desktop.
Unity2MATLAB: Simple (although not-intuitive) conversion of coordinates and rotations from left-handed coordinate system (used by Unity) to right-handed (used by camera calibration toolbox in MATLAB\Octave)
function coords = L2R_coordinates(Lcoords)
if size(Lcoords,1) > 0
if size(Lcoords,2) > 1
x = Lcoords(:,1);
y = Lcoords(:,2);
z = Lcoords(:,3);
else
x = Lcoords(1);
y = Lcoords(2);
z = Lcoords(3);
end
coords = [x z y];
else
coords = [];
end
function rvecs = LE2R_rodrigues(LE_angles)
rvecs = zeros(size(LE_angles));
for i = 1:size(LE_angles,1)
if size(LE_angles,2) > 1
aux = LE_angles(i,1);
auy = LE_angles(i,2);
auz = LE_angles(i,3);
else
aux = LE_angles(1);
auy = LE_angles(2);
auz = LE_angles(3);
end
ax = -aux;
ay = -auz;
az = -auy;
RX = rotX(ax);
RY = rotY(ay);
RZ = rotZ(az);
R = RZ*RX*RY;
if size(LE_angles,2) > 1
rvecs(i,:) = rodrigues(R);
else
rvecs = rodrigues(R);
end
end
function U = rotX(a)
U = [ 1 0 0
0 cosd(a) -sind(a);
0 sind(a) cosd(a); ];
end
function U = rotY(a)
U = [ cosd(a) 0 sind(a);
0 1 0
-sind(a) 0 cosd(a); ];
end
function U = rotZ(a)
U = [ cosd(a) -sind(a) 0;
sind(a) cosd(a) 0;
0 0 1 ];
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment