if a mesh in python is rotated parallel to a vector

Solved it! created two vectors as numpy arrays, vector 1 and vector2 and found the open3d commands to center the mesh and apply the rotation matrix between the two vectors. Think this is susceptible to gimbal lock but I cant get my head around quaternions and it works well enough for my usage scenario.

#this function to create a rotation matrix was taken from https://stackoverflow.com/q/63525482/6010071
def rotation_matrix_from_vectors(vec1, vec2):

    a, b = (vec1 / np.linalg.norm(vec1)).reshape(3), (vec2 / np.linalg.norm(vec2)).reshape(3)
    v = np.cross(a, b)
    c = np.dot(a, b)
    s = np.linalg.norm(v)
    kmat = np.array([[0, -v[2], v[1]], [v[2], 0, -v[0]], [-v[1], v[0], 0]])
    rotation_matrix = np.eye(3) + kmat + kmat.dot(kmat) * ((1 - c) / (s ** 2))
    return rotation_matrix
#to move the mesh to 0,0,0 (using open3d library)
# to rotate the mesh to variable vector2, i set it to np.array([0,0,1]) = in line with z axis again using open3d library

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top