def project_eject_on_bouding_vol(img_file, cam_file, ply_file='./vis.ply'):
    img = Image.open(img_file)
    width, height = img.size

    center = torch.Tensor([0.0, 0.0, 0.0])
    radius = 1.0
    vol_params = ('sphere', center, radius)

    points, viewdirs, valid_mask = data_utils.get_input_data(
        cam_file, vol_params, 'cartesian', 'cartesian')

    img = np.array(img).astype(np.float32)
    img = torch.Tensor(img)
    img = img[..., :3]
    img = img.permute(2, 1, 0)

    bg_mask = torch.le(img[0, ...], 50) & torch.le(img[1, ...], 50) & torch.le(
        img[2, ...], 50)
    mask = valid_mask[0, ...] & valid_mask[1, ...] & valid_mask[2,
                                                                ...] & ~bg_mask
    points = points[:, mask]
    colors = img[:, mask]

    points = torch.transpose(torch.cat((points, colors), 0), 0, 1)
    vertices = []

    for point in points:
        vertices.append(tuple(point))

    fileIO.write_ply(ply_file, vertices, [], [])
def project_img_on_bounding_vol(img,
                                focal,
                                c2w,
                                near=2.0,
                                far=6.0,
                                vol_params=None,
                                ply_file='./vis.ply'):
    H, W, _ = img.shape

    rays_o, rays_d = data_utils.get_rays(H, W, focal, c2w)
    points = data_utils.get_3d_points([rays_o, rays_d], near, far, vol_params)
    img = torch.reshape(img, [-1, 3])

    valid_points = ~(torch.isinf(points[:, 0]) | torch.isinf(points[:, 1])
                     | torch.isinf(points[:, 2]))
    valid_points = valid_points & torch.gt(img[:, 0], 0.0) & torch.gt(
        img[:, 1], 0.0) & torch.gt(img[:, 2], 0)

    points = points[valid_points]
    colors = img[valid_points] * 255.0

    points = torch.cat((points, colors), 1)
    vertices = []

    for point in points:
        vertices.append(tuple(point))

    fileIO.write_ply(ply_file, vertices, [])
def visualize_points(points, colors, ply_file='./vis.ply'):
    if colors is None:
        colors = torch.ones_like(points)
        colors[..., 0] = 0
        colors[..., 1] = 169
        colors[..., 2] = 255

    points = torch.cat((points, colors), -1)

    vertices = []

    for point in points:
        vertices.append(tuple(point))

    fileIO.write_ply(ply_file, vertices, [], [])
def project_input_on_bounding_vol(points, colors, mask, ply_file='./vis.ply'):
    if mask is not None:
        points = points[:, mask]
        colors = colors[:, mask]

    colors = colors * 255.0

    points = torch.transpose(torch.cat((points, colors), 0), 0, 1)

    vertices = []

    for point in points:
        vertices.append(tuple(point))

    fileIO.write_ply(ply_file, vertices, [], [])
def write_ply_cam(ply_file, origin, coords):
    radius = 0.001
    color = (213, 100, 100)
    vertex_lst = []

    for coord in coords:
        verts, _ = create_cylinder_mesh(radius,
                                        origin,
                                        coord,
                                        stacks=30,
                                        slices=10)
        for vert in verts:
            vertex_lst.append(tuple(vert) + color)

    verts, _ = create_cylinder_mesh(radius,
                                    coords[0],
                                    coords[1],
                                    stacks=30,
                                    slices=10)
    for vert in verts:
        vertex_lst.append(tuple(vert) + color)

    verts, _ = create_cylinder_mesh(radius,
                                    coords[0],
                                    coords[2],
                                    stacks=30,
                                    slices=10)
    for vert in verts:
        vertex_lst.append(tuple(vert) + color)

    verts, _ = create_cylinder_mesh(radius,
                                    coords[1],
                                    coords[3],
                                    stacks=30,
                                    slices=10)
    for vert in verts:
        vertex_lst.append(tuple(vert) + color)

    verts, _ = create_cylinder_mesh(radius,
                                    coords[2],
                                    coords[3],
                                    stacks=30,
                                    slices=10)
    for vert in verts:
        vertex_lst.append(tuple(vert) + color)

    fileIO.write_ply(ply_file, vertex_lst, [], [])