import torch


def alpha2rot_y(alpha, x, cx, fx):
    """
    Get rotation_y by alpha + theta - 180
    alpha : Observation angle of object, ranging [-pi..pi]
    x : Object center x to the camera center (x-W/2), in pixels
    rotation_y : Rotation ry around Y-axis in camera coordinates [-pi..pi]
    """
    rot_y = alpha + torch.atan2(x - cx, fx)
    if rot_y > 3.14159:
      rot_y -= 2 * 3.14159
    if rot_y < -3.14159:
      rot_y += 2 * 3.14159
    return rot_y