Ejemplo n.º 1
0
def get_angle_axis_matrix(u, v, default_rot_axis):
    '''
    returns a rotation matrix that will align u with v. If the angle of rotation is 0 then it
    returns an identity matrix, if angle is 180 then it rotates about the default_rot_axis
    otherwise this function returns a matrix corresponding to angle-axis rotation that is
    implemented in GeomTransform.rotate
    '''
    u = GT.normalize(u)
    v = GT.normalize(v)
    axis_of_rotation = GT.normalize(np.cross(u, v))
    cos_theta = np.dot(u, v)
    # due to floating point error abs(u.v) can be > 1, in which case arccos will be nan
    if -1. <= cos_theta <= 1.:
        angle_deg = np.rad2deg(np.arccos(cos_theta))
    else: # here it's either -1-eps or 1 + eps
        angle_deg = 0. if cos_theta > 0. else 180.
    M = np.eye(4)
    if abs(angle_deg - 180.) < 1e-6:
        M = GT.rotate(angle_deg, default_rot_axis)
        axis_of_rotation = default_rot_axis
    elif abs(angle_deg) > 1e-6 and abs(angle_deg - 180.) > 1e-6:
        M = GT.rotate(angle_deg, axis_of_rotation)
        
    return M, angle_deg, axis_of_rotation