import torch


def cam_project(points, K):
    """
    :param points: torch.Tensor of shape [b, n, 3]
    :param K: torch.Tensor intrinsics matrix of shape [b, 3, 3]
    :return: torch.Tensor points projected to 2d using K, shape: [b, n, 2]
    """
    b = points.shape[0]
    n = points.shape[1]

    points_K = torch.matmul(
        K.reshape(b, 1, 3, 3).repeat(1, n, 1, 1),
        points.reshape(b, n, 3, 1)
    )  # shape: [b, n, 3, 1]

    points_2d = points_K[:, :, :2, 0] / points_K[:, :, [2], 0]  # shape: [b, n, 2]

    return points_2d