I have a set of data points in 3D space which apparently all fall onto a specific plane. I use PCA to compute the plane parameters. The 3rd component of PCA gives me the normal
Although there were other interesting responses, this is the solution we figured out while waiting for answers:
function roti = magic_cosini(n)
b = acos(n(2) / sqrt(n(1)*n(1) + n(2)*n(2)));
bwinkel = b * 360 / 2 / pi;
if (n(1) >= 0)
rotb = [cos(-b) -sin(-b) 0; sin(-b) cos(-b) 0; 0 0 1];
else
rotb = [cos(-b) sin(-b) 0; -sin(-b) cos(-b) 0; 0 0 1];
end
n2 = n * rotb;
a = acos(n2(3) / sqrt(n2(2)*n2(2) + n2(3)*n2(3)));
awinkel = a * 360 / 2 / pi;
rota = [1 0 0; 0 cos(-a) -sin(-a); 0 sin(-a) cos(-a)];
roti = rotb * rota;
(It's returning a hopefully correct double rotation matrix)
The flaw we had before and fixed here was to esp. deal with the sign of the X component, which was not covered in the cosine computations. This made us rotate in the wrong direction once (rotating with 180° - angle).
I hope I will also find time to try Nosredna's solution! It's always good to avoid trigonometry.