Rotate normal vector onto axis plane

前端 未结 3 1722

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

相关标签:
3条回答
  • 2021-02-05 22:08

    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.

    0 讨论(0)
  • 2021-02-05 22:25

    If you have a plane, you have a normal vector and an origin. I wouldn't do any "rotations" at all. You're just a few vector operations away from your answer.

    • Let's call your plane's normal vector the new z axis.
    • You can generate the new y axis by crossing the old x axis with the new z axis (your plane's normal).
    • Generate the new x axis by crossing the new z with the new y.
    • Make all your new axis vectors into unit vectors (length 1).
    • For every point you have, create a vector that's from your new origin to the point (vector subtraction of point - plane_origin). Just dot with the new x and new y unit vectors and you get a pair (x,y) you can plot!

    If you have cross and dot product functions already, this is just a few lines of code. I know it works because most of the 3D videogames I wrote worked this way.

    Tricks:

    • Pay attention to which directions your vectors are pointing. If they point the wrong way, negate the resultant vector or change the order of the cross product.
    • You have trouble if your plane's normal is exactly the same as your original x axis.
    0 讨论(0)
  • 2021-02-05 22:26

    How about:

    Decompose the normal vector into a vector in the XY-plane and a Z vector. Then apply a rotation around the Z-axis to line up the XY vector with one of the axis. Then find the dot product of the the normal with the Z-axis, and rotate along which ever of X,Y you lined up with.

    The idea is to line the normal vector up with Z, and by doing that your plane is now the XY plane.

    0 讨论(0)
提交回复
热议问题