BachiLi / diffvg

Differentiable Vector Graphics Rasterization
https://people.csail.mit.edu/tzumao/diffvg/
Apache License 2.0
902 stars 152 forks source link

Rendering problem with variable thickness and multiple paths #43

Open colormotor opened 1 year ago

colormotor commented 1 year ago

Hi, Rendering multiple paths with variable thickness seems to be broken. Looks like it might be an issue with the bounding box computation. image

Here is a code snippet to reproduce the issue:

import matplotlib.pyplot as plt
import numpy as np
import torch
import torch
import pydiffvg

pydiffvg.set_use_gpu(torch.cuda.is_available())
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
import skimage
import skimage.io

canvas_width, canvas_height = 256, 256

plt.figure(figsize=(10,5))
for num_shapes in range(1, 3):
    torch.random.manual_seed(331)

    plt.subplot(1, 2, num_shapes)
    plt.title('%d paths'%(num_shapes))
    num_control_points = torch.tensor([0, 0])
    shapes = []
    for i in range(num_shapes):
        points = torch.rand((3, 2))*canvas_width
        thickness = torch.tensor([10.0,
                                20.0,
                                30.0])
        path = pydiffvg.Path(num_control_points = num_control_points,
                            points = points,
                            is_closed = False,
                            stroke_width = thickness)
        shapes.append(path)
    path_group = pydiffvg.ShapeGroup(shape_ids = torch.tensor(list(range(num_shapes))), #list(range(2))),
                                    fill_color = None,
                                    stroke_color = torch.tensor([0.6, 0.3, 0.6, 0.8]))
    shape_groups = [path_group]
    scene_args = pydiffvg.RenderFunction.serialize_scene(\
        canvas_width, canvas_height, shapes, shape_groups)

    render = pydiffvg.RenderFunction.apply
    img = render(256, # width
                256, # height
                2,   # num_samples_x
                2,   # num_samples_y
                0,   # seed
                None, # background_image
                *scene_args)
    plt.imshow(img.detach().cpu())
plt.show()
colormotor commented 1 year ago

Found a quick fix with 2a9d9d7