0

I have a face made from 4 xyz vertices.

I want to align it with the z axis so it is parallel with it.

If I calculate the normals I can calculate the angle between them but that is just the angle. I need an x rotation and a y rotation.

I am using numpy on Python 3.

Thanks.

iain
  • 33
  • 7
  • In 3D you should get two angles, then you could try to derive the rotation matrix necessary for aligning the face. – Thomas Lang Nov 30 '18 at 19:41

1 Answers1

1

To rotate a unit vector to the, say, 1st axis you can use QR decomp, like so:

normal = np.random.random(3)
normal /= np.sqrt(normal@normal)
some_base = np.identity(3)
some_base[:, 0] = normal
Q, R = np.linalg.qr(some_base)
Q.T@normal
# array([-1.00000000e+00, -2.77555756e-17,  1.11022302e-16])

As you can see you may have to flip one or two of the columns of Q:

if (Q.T@normal)[0] < 0:
    if np.linalg.det(Q) < 0:
        rot = (Q * [-1, 1, 1]).T
    else:
        rot = (Q * [-1, -1, 1]).T
else:
    if np.linalg.det(Q) < 0:
        rot = (Q * [1, -1, 1]).T
    else:
        rot = Q.T
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99