Пример #1
0
 def add_ground(self, device):
     self.materials.append(
         pyredner.Material(
             diffuse_reflectance=torch.tensor([0.8, 0.8, 0.8],
                                              dtype=torch.float32,
                                              device=device),
             specular_reflectance=torch.tensor([0.1, 0.1, 0.1],
                                               dtype=torch.float32,
                                               device=device),
             roughness=torch.tensor([0.5],
                                    dtype=torch.float32,
                                    device=device)))
     self.shapes.append(
         pyredner.Shape(
             vertices=torch.tensor([[-100, 0, -100], [-100, 0, 100],
                                    [100, 0, -100], [100, 0, 100]],
                                   dtype=torch.float32,
                                   device=device),
             indices=torch.tensor([[0, 1, 2], [1, 3, 2]],
                                  dtype=torch.int32,
                                  device=device),
             normals=torch.tensor([[0, 1, 0], [0, 1, 0], [0, 1, 0]],
                                  dtype=torch.float32,
                                  device=device),
             uvs=torch.tensor([[0, 0], [0, 1], [1, 0], [1, 1]],
                              dtype=torch.float32,
                              device=device),
             material_id=len(self.materials) - 1,
         ))
     self.ground_idx = (len(self.materials) - 1, len(self.shapes) - 1)
Пример #2
0
 def __init__(self,
              camera: pyredner.Camera,
              shapes: List[pyredner.Shape] = [],
              shape_id=dict(),
              materials: List[pyredner.Material] = [],
              area_lights: List[pyredner.AreaLight] = [],
              objects: Optional[List[pyredner.Object]] = None,
              envmap: Optional[pyredner.EnvironmentMap] = None):
     self.camera = camera
     self.envmap = envmap
     self.shape_id = shape_id
     if objects is None:
         self.shapes = shapes
         self.materials = materials
         self.area_lights = area_lights
     else:
         # Convert objects to shapes/materials/lights
         shapes = []
         materials = []
         area_lights = []
         material_dict = {}
         current_material_id = 0
         for obj in objects:
             mid = -1
             if obj.material in material_dict:
                 mid = material_dict[obj.material]
             else:
                 mid = current_material_id
                 material_dict[obj.material] = current_material_id
                 materials.append(obj.material)
                 current_material_id += 1
             if obj.light_intensity is not None:
                 current_shape_id = len(shapes)
                 area_light = pyredner.AreaLight(
                     shape_id=current_shape_id,
                     intensity=obj.light_intensity,
                     two_sided=obj.light_two_sided)
                 area_lights.append(area_light)
             shape = pyredner.Shape(vertices=obj.vertices,
                                    indices=obj.indices,
                                    material_id=mid,
                                    uvs=obj.uvs,
                                    normals=obj.normals,
                                    uv_indices=obj.uv_indices,
                                    normal_indices=obj.normal_indices,
                                    colors=obj.colors)
             shapes.append(shape)
         self.shapes = shapes
         #self.shape_id = shape_id
         self.materials = materials
         self.area_lights = area_lights
Пример #3
0
 def load_obj(cls, path, learn_tex_label, device):
     mtl_map, mesh_list, _ = pyredner.load_obj(path)
     mtl_id_map = dict()
     materials = list()
     cnt = 0
     for k, v in mtl_map.items():
         mtl_id_map[k] = cnt
         cnt += 1
         materials.append(v)
     assert (learn_tex_label in mtl_id_map)
     learn_tex_idx = mtl_id_map[learn_tex_label]
     shapes = list()
     for mtl_name, mesh in mesh_list:
         shapes.append(
             pyredner.Shape(vertices=mesh.vertices,
                            indices=mesh.indices,
                            uvs=mesh.uvs,
                            normals=mesh.normals,
                            material_id=mtl_id_map[mtl_name]))
     return cls(materials, shapes, learn_tex_idx)
Пример #4
0
mat_red = pyredner.Material(\
    diffuse_reflectance = torch.tensor([0.75, 0.35, 0.35],
    device = pyredner.get_device()))
mat_black = pyredner.Material(\
    diffuse_reflectance = torch.tensor([0.0, 0.0, 0.0],
    device = pyredner.get_device()))
materials = [mat_green,mat_red,mat_black]
tri0_vertices = torch.tensor(\
    [[-1.7,1.0,0.0], [1.0,1.0,0.0], [-0.5,-1.0,0.0]],
    device = pyredner.get_device())
tri1_vertices = torch.tensor(\
    [[-1.0,1.5,1.0], [0.2,1.5,1.0], [0.2,-1.5,1.0]],
    device = pyredner.get_device())
tri0_indices = torch.tensor([[0, 1, 2]], dtype = torch.int32, device = pyredner.get_device())
tri1_indices = torch.tensor([[0, 1, 2]], dtype = torch.int32, device = pyredner.get_device())
shape_tri0 = pyredner.Shape(tri0_vertices, tri0_indices, None, None, 0)
shape_tri1 = pyredner.Shape(tri1_vertices, tri1_indices, None, None, 1)
light_vertices = torch.tensor(\
    [[-1.0,-1.0,-7.0],[1.0,-1.0,-7.0],[-1.0,1.0,-7.0],[1.0,1.0,-7.0]],
    device = pyredner.get_device())
light_indices = torch.tensor([[0,1,2],[1,3,2]], dtype = torch.int32, device = pyredner.get_device())
shape_light = pyredner.Shape(light_vertices, light_indices, None, None, 2)
shapes = [shape_tri0, shape_tri1, shape_light]
light_intensity = torch.tensor([20.0,20.0,20.0])
# The first argument is the shape id of the light
light = pyredner.AreaLight(2, light_intensity)
area_lights = [light]
scene = pyredner.Scene(cam, shapes, materials, area_lights)
args = pyredner.RenderFunction.serialize_scene(\
    scene = scene,
    num_samples = 256,
Пример #5
0
    fisheye=False)

mat_grey = pyredner.Material(\
    diffuse_reflectance = \
        torch.tensor([0.4, 0.4, 0.4], device = pyredner.get_device()),
    specular_reflectance = \
        torch.tensor([0.5, 0.5, 0.5], device = pyredner.get_device()),
    roughness = \
        torch.tensor([0.05], device = pyredner.get_device()))

materials = [mat_grey]

vertices, indices, uvs, normals = pyredner.generate_sphere(128, 64)
shape_sphere = pyredner.Shape(\
    vertices = vertices,
    indices = indices,
    uvs = uvs,
    normals = normals,
    material_id = 0)
shapes = [shape_sphere]

envmap = pyredner.imread('sunsky.exr')
if pyredner.get_use_gpu():
    envmap = envmap.cuda(device=pyredner.get_device())
envmap = pyredner.EnvironmentMap(envmap)
scene = pyredner.Scene(cam, shapes, materials, [], envmap)
scene_args = pyredner.RenderFunction.serialize_scene(\
    scene = scene,
    num_samples = 256,
    max_bounces = 1)
render = pyredner.RenderFunction.apply
img = render(0, *scene_args)
Пример #6
0
cam = pyredner.Camera(position = position,
                     look_at = look_at,
                     up = up,
                     fov = fov,
                     clip_near = clip_near,
                     resolution = resolution)

mat_grey = pyredner.Material(\
    diffuse_reflectance = torch.tensor([0.5, 0.5, 0.5],
    device = pyredner.get_device()))
materials = [mat_grey]
vertices = torch.tensor([[-1.7,1.0,0.0], [1.0,1.0,0.0], [-0.5,-1.0,0.0]],
                        device = pyredner.get_device())
indices = torch.tensor([[0, 1, 2]], dtype = torch.int32,
                       device = pyredner.get_device())
shape_triangle = pyredner.Shape(vertices, indices, None, None, 0)
light_vertices = torch.tensor([[-1.0,-1.0,-9.0],[1.0,-1.0,-9.0],[-1.0,1.0,-9.0],[1.0,1.0,-9.0]],
                              device = pyredner.get_device())
light_indices = torch.tensor([[0,1,2],[1,3,2]], dtype = torch.int32,
                             device = pyredner.get_device())
shape_light = pyredner.Shape(light_vertices, light_indices, None, None, 0)
shapes = [shape_triangle, shape_light]
light_intensity = torch.tensor([30.0,30.0,30.0])
light = pyredner.Light(1, light_intensity)
lights = [light]
scene = pyredner.Scene(cam, shapes, materials, lights)
args = pyredner.RenderFunction.serialize_scene(\
    scene = scene,
    num_samples = 16,
    max_bounces = 1)
Пример #7
0
# 3D objects in redner are called "Shape".
# All shapes in the scene are stored in a Python list,
# the index of a shape in the list is its shape id.
# Right now, a shape is always a triangle mesh, which has a list of
# triangle vertices and a list of triangle indices.
# The vertices are a Nx3 torch float tensor,
# and the indices are a Mx3 torch integer tensor.
# Optionally, for each vertex you can specify its UV coordinate for texture mapping,
# and a normal for Phong interpolation.
# Each shape also needs to be assigned a material using material id,
# which is the index of the material in the material array.
# If you are using GPU, make sure to copy all tensors of the shape to GPU memory.
shape_triangle = pyredner.Shape(\
    vertices = torch.tensor([[-1.7, 1.0, 0.0], [1.0, 1.0, 0.0], [-0.5, -1.0, 0.0]],
        device = pyredner.get_device()),
    indices = torch.tensor([[0, 1, 2]], dtype = torch.int32,
        device = pyredner.get_device()),
    uvs = None,
    normals = None,
    material_id = 0)
# Merely having a single triangle is not enough for physically-based rendering.
# We need to have a light source. Here we setup the shape of a quad area light source,
# similary to the previous triangle.
shape_light = pyredner.Shape(\
    vertices = torch.tensor([[-1.0, -1.0, -7.0],
                             [ 1.0, -1.0, -7.0],
                             [-1.0,  1.0, -7.0],
                             [ 1.0,  1.0, -7.0]], device = pyredner.get_device()),
    indices = torch.tensor([[0, 1, 2],[1, 3, 2]],
        dtype = torch.int32, device = pyredner.get_device()),
    uvs = None,
    normals = None,
Пример #8
0
def parse_shape(node, material_dict, shape_id, device, shape_group_dict=None):
    if node.attrib['type'] == 'obj' or node.attrib['type'] == 'serialized':
        to_world = torch.eye(4)
        serialized_shape_id = 0
        mat_id = -1
        light_intensity = None
        filename = ''
        max_smooth_angle = -1
        for child in node:
            if 'name' in child.attrib:
                if child.attrib['name'] == 'filename':
                    filename = child.attrib['value']
                elif child.attrib['name'] == 'toWorld':
                    to_world = parse_transform(child)
                elif child.attrib['name'] == 'shapeIndex':
                    serialized_shape_id = int(child.attrib['value'])
                elif child.attrib['name'] == 'maxSmoothAngle':
                    max_smooth_angle = float(child.attrib['value'])
            if child.tag == 'ref':
                mat_id = material_dict[child.attrib['id']]
            elif child.tag == 'emitter':
                for grandchild in child:
                    if grandchild.attrib['name'] == 'radiance':
                        light_intensity = parse_vector(
                            grandchild.attrib['value'])
                        if light_intensity.shape[0] == 1:
                            light_intensity = torch.tensor(\
                                         [light_intensity[0],
                                          light_intensity[0],
                                          light_intensity[0]])

        if node.attrib['type'] == 'obj':
            # Load in CPU for rebuild_topology
            _, mesh_list, _ = pyredner.load_obj(filename,
                                                obj_group=False,
                                                device=torch.device('cpu'))
            vertices = mesh_list[0][1].vertices
            indices = mesh_list[0][1].indices
            uvs = mesh_list[0][1].uvs
            normals = mesh_list[0][1].normals
            uv_indices = mesh_list[0][1].uv_indices
            normal_indices = mesh_list[0][1].normal_indices
        else:
            assert (node.attrib['type'] == 'serialized')
            mitsuba_tri_mesh = redner.load_serialized(filename,
                                                      serialized_shape_id)
            vertices = torch.from_numpy(mitsuba_tri_mesh.vertices)
            indices = torch.from_numpy(mitsuba_tri_mesh.indices)
            uvs = torch.from_numpy(mitsuba_tri_mesh.uvs)
            normals = torch.from_numpy(mitsuba_tri_mesh.normals)
            if uvs.shape[0] == 0:
                uvs = None
            if normals.shape[0] == 0:
                normals = None
            uv_indices = None  # Serialized doesn't use different indices for UV & normal
            normal_indices = None

        # Transform the vertices and normals
        vertices = torch.cat((vertices, torch.ones(vertices.shape[0], 1)),
                             dim=1)
        vertices = vertices @ torch.transpose(to_world, 0, 1)
        vertices = vertices / vertices[:, 3:4]
        vertices = vertices[:, 0:3].contiguous()
        if normals is not None:
            normals = normals @ (torch.inverse(torch.transpose(to_world, 0,
                                                               1))[:3, :3])
            normals = normals.contiguous()
        assert (vertices is not None)
        assert (indices is not None)
        if max_smooth_angle >= 0:
            if normals is None:
                normals = torch.zeros_like(vertices)
            new_num_vertices = redner.rebuild_topology(\
                redner.float_ptr(vertices.data_ptr()),
                redner.int_ptr(indices.data_ptr()),
                redner.float_ptr(uvs.data_ptr() if uvs is not None else 0),
                redner.float_ptr(normals.data_ptr() if normals is not None else 0),
                redner.int_ptr(uv_indices.data_ptr() if uv_indices is not None else 0),
                int(vertices.shape[0]),
                int(indices.shape[0]),
                max_smooth_angle)
            print('Rebuilt topology, original vertices size: {}, new vertices size: {}'.format(\
                int(vertices.shape[0]), new_num_vertices))
            vertices.resize_(new_num_vertices, 3)
            if uvs is not None:
                uvs.resize_(new_num_vertices, 2)
            if normals is not None:
                normals.resize_(new_num_vertices, 3)

        lgt = None
        if light_intensity is not None:
            lgt = pyredner.AreaLight(shape_id, light_intensity)

        vertices = vertices.to(device)
        indices = indices.to(device)
        if uvs is not None:
            uvs = uvs.to(device)
        if normals is not None:
            normals = normals.to(device)
        if uv_indices is not None:
            uv_indices = uv_indices.to(device)
        if normal_indices is not None:
            normal_indices = normal_indices.to(device)
        return pyredner.Shape(vertices,
                              indices,
                              uvs=uvs,
                              normals=normals,
                              uv_indices=uv_indices,
                              normal_indices=normal_indices,
                              material_id=mat_id), lgt
    elif node.attrib['type'] == 'rectangle':
        indices = torch.tensor([[0, 2, 1], [1, 2, 3]], dtype=torch.int32)
        vertices = torch.tensor([[-1.0, -1.0, 0.0], [-1.0, 1.0, 0.0],
                                 [1.0, -1.0, 0.0], [1.0, 1.0, 0.0]])
        uvs = None
        normals = None
        to_world = torch.eye(4)
        mat_id = -1
        light_intensity = None
        for child in node:
            if 'name' in child.attrib:
                if child.attrib['name'] == 'toWorld':
                    to_world = parse_transform(child)
            if child.tag == 'ref':
                mat_id = material_dict[child.attrib['id']]
            elif child.tag == 'emitter':
                for grandchild in child:
                    if grandchild.attrib['name'] == 'radiance':
                        light_intensity = parse_vector(
                            grandchild.attrib['value'])
                        if light_intensity.shape[0] == 1:
                            light_intensity = torch.tensor(\
                                         [light_intensity[0],
                                          light_intensity[0],
                                          light_intensity[0]])
        # Transform the vertices
        # Transform the vertices and normals
        vertices = torch.cat((vertices, torch.ones(vertices.shape[0], 1)),
                             dim=1)
        vertices = vertices @ torch.transpose(to_world, 0, 1)
        vertices = vertices / vertices[:, 3:4]
        vertices = vertices[:, 0:3].contiguous()
        if normals is not None:
            normals = normals @ (torch.inverse(torch.transpose(to_world, 0,
                                                               1))[:3, :3])
            normals = normals.contiguous()
        assert (vertices is not None)
        assert (indices is not None)
        lgt = None
        if light_intensity is not None:
            lgt = pyredner.AreaLight(shape_id, light_intensity)

        vertices = vertices.to(device)
        indices = indices.to(device)
        if uvs is not None:
            uvs = uvs.to(device)
        if normals is not None:
            normals = normals.to(device)
        return pyredner.Shape(vertices,
                              indices,
                              uvs=uvs,
                              normals=normals,
                              material_id=mat_id), lgt
    # Add instance support
    # TODO (simply transform & create a new shape now)
    elif node.attrib['type'] == 'instance':
        shape = None
        for child in node:
            if 'name' in child.attrib:
                if child.attrib['name'] == 'toWorld':
                    to_world = parse_transform(child)
            if child.tag == 'ref':
                shape = shape_group_dict[child.attrib['id']]
        # transform instance
        vertices = shape.vertices
        normals = shape.normals
        vector1 = torch.ones(vertices.shape[0], 1, device=vertices.device)
        to_world = to_world.to(vertices.device)
        vertices = torch.cat((vertices, vector1), dim=1)
        vertices = vertices @ torch.transpose(to_world, 0, 1)
        vertices = vertices / vertices[:, 3:4]
        vertices = vertices[:, 0:3].contiguous()
        if normals is not None:
            normals = normals @ (torch.inverse(torch.transpose(to_world, 0,
                                                               1))[:3, :3])
            normals = normals.contiguous()
        # assert(vertices is not None)
        # assert(indices is not None)
        # lgt = None
        # if light_intensity is not None:
        #     lgt = pyredner.AreaLight(shape_id, light_intensity)

        return pyredner.Shape(vertices,
                              shape.indices,
                              uvs=shape.uvs,
                              normals=normals,
                              material_ids=shape.material_id), None
    else:
        print('Shape type {} is not supported!'.format(node.attrib['type']))
        assert (False)
Пример #9
0
mat_checkerboard = pyredner.Material(\
    diffuse_reflectance = checkerboard_texture)
mat_black = pyredner.Material(\
    diffuse_reflectance = torch.tensor([0.0, 0.0, 0.0],
    device = pyredner.get_device()))
materials = [mat_checkerboard, mat_black]
vertices = torch.tensor(
    [[-1.0, -1.0, 0.0], [-1.0, 1.0, 0.0], [1.0, -1.0, 0.0], [1.0, 1.0, 0.0]],
    device=pyredner.get_device())
indices = torch.tensor([[0, 1, 2], [1, 3, 2]],
                       dtype=torch.int32,
                       device=pyredner.get_device())
uvs = torch.tensor([[0.05, 0.05], [0.05, 0.95], [0.95, 0.05], [0.95, 0.95]],
                   device=pyredner.get_device())
shape_plane = pyredner.Shape(vertices=vertices,
                             indices=indices,
                             uvs=uvs,
                             material_id=0)
light_vertices = torch.tensor([[-1.0, -1.0, -7.0], [1.0, -1.0, -7.0],
                               [-1.0, 1.0, -7.0], [1.0, 1.0, -7.0]],
                              device=pyredner.get_device())
light_indices = torch.tensor([[0, 1, 2], [1, 3, 2]],
                             dtype=torch.int32,
                             device=pyredner.get_device())
shape_light = pyredner.Shape(light_vertices, light_indices, 1)
shapes = [shape_plane, shape_light]
light_intensity = torch.tensor([20.0, 20.0, 20.0])
# The first argument is the shape id of the light
light = pyredner.AreaLight(1, light_intensity)
area_lights = [light]
scene = pyredner.Scene(cam, shapes, materials, area_lights)
args = pyredner.RenderFunction.serialize_scene(\
Пример #10
0
    def __init__(self,
                 framework,
                 filename,
                 dims,
                 label_names,
                 normalize_params,
                 background,
                 pose,
                 num_classes,
                 attack_type="benign"):

        self.NUM_CLASSES = num_classes
        self.framework = framework.to(pyredner.get_device())
        self.image_dims = dims
        self.label_names = label_names
        self.framework_params = normalize_params

        # self.objects = pyredner.load_obj(filename, return_objects=True)
        self.material_map, mesh_list, self.light_map = pyredner.load_obj(
            filename)
        for _, mesh in mesh_list:
            mesh.normals = pyredner.compute_vertex_normal(
                mesh.vertices, mesh.indices)

        vertices = []

        self.modifiers = []
        self.input_adv_list = []
        self.input_orig_list = []
        self.targeted = False
        self.clamp_fn = "tanh"

        self.attack_type = attack_type

        if attack_type == "CW":
            for _, mesh in mesh_list:
                vertices.append(mesh.vertices)
                modifier = torch.zeros(mesh.vertices.size(),
                                       requires_grad=True,
                                       device=pyredner.get_device())
                self.modifiers.append(modifier)
                self.input_orig_list.append(
                    tanh_rescale(torch_arctanh(mesh.vertices)))
                mesh.vertices = tanh_rescale(
                    torch_arctanh(mesh.vertices) + modifier)

                self.input_adv_list.append(mesh.vertices)
                mesh.vertices.retain_grad()
        else:
            for _, mesh in mesh_list:
                vertices.append(mesh.vertices)
                mesh.vertices = Variable(mesh.vertices, requires_grad=True)
                mesh.vertices.retain_grad()

        material_id_map = {}
        self.materials = []
        count = 0
        for key, value in self.material_map.items():
            material_id_map[key] = count
            count += 1
            self.materials.append(value)

        self.shapes = []
        self.cw_shapes = []
        for mtl_name, mesh in mesh_list:
            # assert(mesh.normal_indices is None)
            self.shapes.append(
                pyredner.Shape(vertices=mesh.vertices,
                               indices=mesh.indices,
                               material_id=material_id_map[mtl_name],
                               uvs=mesh.uvs,
                               normals=mesh.normals,
                               uv_indices=mesh.uv_indices))

        self.camera = pyredner.automatic_camera_placement(self.shapes,
                                                          resolution=(512,
                                                                      512))
        # Compute the center of the teapot
        self.center = torch.mean(torch.cat(vertices), 0)
        self.translation = torch.tensor([0., 0., 0.],
                                        device=pyredner.get_device(),
                                        requires_grad=True)

        self.angle_input_adv_list = []
        self.angle_input_orig_list = []
        self.pose = pose
        if attack_type == "CW":
            self.euler_angles_modifier = torch.tensor(
                [0., 0., 0.], device=pyredner.get_device(), requires_grad=True)
            if pose == 'forward':
                self.euler_angles = tanh_rescale(
                    torch_arctanh(
                        torch.tensor([0., 0., 0.],
                                     device=pyredner.get_device())) +
                    self.euler_angles_modifier)
                self.angle_input_orig_list.append(
                    tanh_rescale(
                        torch_arctanh(
                            torch.tensor([0., 0., 0.],
                                         device=pyredner.get_device()))))
            elif pose == 'top':
                self.euler_angles = tanh_rescale(
                    torch_arctanh(
                        torch.tensor([0.35, 0., 0.],
                                     device=pyredner.get_device())) +
                    self.euler_angles_modifier)
                self.angle_input_orig_list.append(
                    tanh_rescale(
                        torch_arctanh(
                            torch.tensor([0., 0., 0.],
                                         device=pyredner.get_device()))))
            elif pose == 'left':
                self.euler_angles = tanh_rescale(
                    torch_arctanh(
                        torch.tensor([0., 0.50, 0.],
                                     device=pyredner.get_device())) +
                    self.euler_angles_modifier)
                self.angle_input_orig_list.append(
                    tanh_rescale(
                        torch_arctanh(
                            torch.tensor([0., 0., 0.],
                                         device=pyredner.get_device()))))
            elif pose == 'right':
                self.euler_angles = tanh_rescale(
                    torch_arctanh(
                        torch.tensor([0., -0.50, 0.],
                                     device=pyredner.get_device())) +
                    self.euler_angles_modifier)
                self.angle_input_orig_list.append(
                    tanh_rescale(
                        torch_arctanh(
                            torch.tensor([0., 0., 0.],
                                         device=pyredner.get_device()))))

            self.angle_input_adv_list.append(self.euler_angles)
        else:
            if pose == 'forward':
                self.euler_angles = torch.tensor([0., 0., 0.],
                                                 device=pyredner.get_device(),
                                                 requires_grad=True)
            elif pose == 'top':
                self.euler_angles = torch.tensor([0.35, 0., 0.],
                                                 device=pyredner.get_device(),
                                                 requires_grad=True)
            elif pose == 'left':
                self.euler_angles = torch.tensor([0., 0.50, 0.],
                                                 device=pyredner.get_device(),
                                                 requires_grad=True)
            elif pose == 'right':
                self.euler_angles = torch.tensor([0., -0.50, 0.],
                                                 device=pyredner.get_device(),
                                                 requires_grad=True)

        self.light_init_vals = torch.tensor([20000.0, 30000.0, 20000.0],
                                            device=pyredner.get_device())
        if attack_type == "CW":
            self.light_input_orig_list = []
            self.light_input_adv_list = []
            delta = 1e-6  # constant for stability
            self.light_modifier = torch.tensor([0., 0., 0.],
                                               device=pyredner.get_device(),
                                               requires_grad=True)
            # redner can't accept negative light intensities, so we have to be a bit creative and work with lighting norms instead and then rescale them afterwards...
            tanh_factor = tanh_rescale(
                torch_arctanh(self.light_init_vals /
                              torch.norm(self.light_init_vals)) +
                self.light_modifier / torch.norm(self.light_modifier + delta))
            self.light_intensity = torch.norm(
                self.light_init_vals) * torch.clamp(tanh_factor, 0, 1)

            self.light_input_orig_list.append(self.light_init_vals /
                                              torch.norm(self.light_init_vals))
            self.light_input_adv_list.append(self.light_intensity)
            self.light = pyredner.PointLight(
                position=(self.camera.position + torch.tensor(
                    (0.0, 0.0, 100.0))).to(pyredner.get_device()),
                intensity=self.light_intensity)
        else:
            self.light = pyredner.PointLight(
                position=(self.camera.position + torch.tensor(
                    (0.0, 0.0, 100.0))).to(pyredner.get_device()),
                intensity=Variable(torch.tensor((20000.0, 30000.0, 20000.0),
                                                device=pyredner.get_device()),
                                   requires_grad=True))

        background = pyredner.imread(background)
        self.background = background.to(pyredner.get_device())
Пример #11
0
    bitangent = bitangent.div(torch.norm(bitangent))

    lightPos = [
        torch.add(torch.sub(pos, tangent), bitangent),
        torch.add(torch.add(pos, tangent), bitangent),
        torch.sub(torch.sub(pos, tangent), bitangent),
        torch.sub(torch.add(pos, tangent), bitangent)
    ]
    for i in range(4):
        lightPos[i] = lightPos[i] + torch.tensor([0.0, 0.0, -15.0])
    lightPos = torch.cat(lightPos, 0)
    print(lightPos)
    lights.append(pyredner.Shape(\
        vertices = torch.tensor(lightPos, device = pyredner.get_device()),
        indices = torch.tensor([[0, 2,1],[1, 2, 3]],
            dtype = torch.int32, device = pyredner.get_device()),
        uvs = None,
        normals = None,
        material_id = 0))

for ind, pos in enumerate(camLocs):
    #-4.5, 0.0, 0.1 - 2.5
    pos = torch.tensor([100, 0.0, -3.0])
    normal = pos.div(torch.norm(pos - torch.tensor([-3.0, 0.0, -3.0])))
    pos = normal * radius2

    up = torch.tensor([0.0, 1.0, 0.0])

    tangent = torch.cross(normal, up)
    tangent = tangent.div(torch.norm(tangent))
    bitangent = torch.cross(normal, tangent)
Пример #12
0
    [[-0.3, 0.5], [0.2, 0.6], [-0.5, -0.3], [0.5, -0.4]],
    device = pyredner.get_device())
tri_vertices_2d = torch.tensor(\
    [[-0.6, 0.3], [0.4, 0.5], [-0.1, -0.2]],
    device = pyredner.get_device())
# We need to pad the depth coordinates for these vertices
# We'll assign depth = 1 for the quad, depth = 0 for the triangle,
# so the triangle will block the quad.
quad_vertices = torch.cat((quad_vertices_2d,
    torch.ones(quad_vertices_2d.shape[0], 1, device = pyredner.get_device())), dim=1).contiguous()
tri_vertices = torch.cat((tri_vertices_2d,
    torch.zeros(tri_vertices_2d.shape[0], 1, device = pyredner.get_device())), dim=1).contiguous()
quad_indices = torch.tensor([[0, 1, 2], [1, 2, 3]], dtype = torch.int32, device = pyredner.get_device())
tri_indices = torch.tensor([[0, 1, 2]], dtype = torch.int32, device = pyredner.get_device())
shape_quad = pyredner.Shape(\
    vertices = quad_vertices,
    indices = quad_indices,
    material_id = 0)
shape_tri = pyredner.Shape(\
    vertices = tri_vertices,
    indices = tri_indices,
    material_id = 1)
shapes = [shape_quad, shape_tri]

# Setup the scene. We don't need lights.
scene = pyredner.Scene(camera = cam,
                       shapes = shapes,
                       materials = materials)
# We output the shape id, so that we can shape it later
args = pyredner.RenderFunction.serialize_scene(\
    scene = scene,
    num_samples = 16,
Пример #13
0
# Setup camera
cam = pyredner.Camera(
    position=torch.tensor([0.0, 30.0, 200.0]),
    look_at=torch.tensor([0.0, 30.0, 0.0]),
    up=torch.tensor([0.0, 1.0, 0.0]),
    fov=torch.tensor([45.0]),  # in degree
    clip_near=1e-2,  # needs to > 0
    resolution=(256, 256),
    fisheye=False)

#
mesh = mesh_list[0][1]
shapes = [pyredner.Shape(\
        vertices = mesh.vertices,
        indices = mesh.indices,
        uvs = mesh.uvs,
        normals = mesh.normals,
        material_id = 0)]

tex_path = '../tutorials/teapot.png'
tex_tensor = pyredner.imread(tex_path)
if pyredner.get_use_gpu():
    tex_tensor = tex_tensor.cuda(device=pyredner.get_device())

diffuse_reflectance = tex_tensor
materials = [pyredner.Material(diffuse_reflectance=diffuse_reflectance)]

# Construct the scene.
# Don't setup any light sources, only use primary visibility.
scene = pyredner.Scene(cam, shapes, materials, area_lights=[], envmap=None)
Пример #14
0
def generate_poses(model_path, output_path):
    # Init logger
    log = dict()

    # Load renderer configs
    material_map, mesh_list, light_map = pyredner.load_obj(model_path)
    material_id_map = {}
    materials = []
    count = 0
    for key, value in material_map.items():
        material_id_map[key] = count
        count += 1
        materials.append(value)

    shapes = []
    for mtl_name, mesh in mesh_list:
        shapes.append(
            pyredner.Shape(vertices=mesh.vertices,
                           indices=mesh.indices,
                           uvs=mesh.uvs,
                           normals=mesh.normals,
                           material_id=material_id_map[mtl_name]))

    envmap = pyredner.EnvironmentMap(
        torch.tensor(imread('./datasets/envmaps/one/sunsky.exr'),
                     dtype=torch.float32,
                     device=pyredner.get_device()))

    # Object pose parameters
    euler_angles = [0.0, 0.0, 0.0]
    translation = [0.0, -0.75, 0.0]
    up = [0.0, 1.0, 0.0]
    distance = 7.0

    # Setup base scene to modify during iterations
    cam_params = camera_parameters(euler_angles, translation, distance, up)

    camera = pyredner.Camera(position=torch.tensor(cam_params[0],
                                                   dtype=torch.float32),
                             look_at=torch.tensor(cam_params[1],
                                                  dtype=torch.float32),
                             up=torch.tensor(cam_params[2],
                                             dtype=torch.float32),
                             fov=torch.tensor([45.0]),
                             clip_near=1e-2,
                             resolution=(opt.resolution, opt.resolution),
                             fisheye=False)

    scene = pyredner.Scene(camera,
                           shapes,
                           materials,
                           area_lights=[],
                           envmap=envmap)

    # Generate alphamasks
    for i in range(opt.num_elev):
        # Set elevation angle
        elev_pc = i / opt.num_elev
        elevation = opt.max_elev * elev_pc + opt.min_elev * (1 - elev_pc)
        euler_angles[1] = elevation

        # Calculate number of azimuthal iterations
        num_azimuth = int(opt.num_elev * math.sin(math.pi / 2 - elevation))
        for j in range(num_azimuth):
            # Set azimuthal angle
            azimuth_pc = j / num_azimuth
            azimuth = math.pi * 2 * azimuth_pc

            euler_angles[0] = azimuth

            print('Params: Elevation - {:.4f}\tAzimuth - {:.4f}'\
                    .format(elevation, azimuth))

            # Set Camera params
            cam_params = camera_parameters(euler_angles, translation, distance,
                                           up)

            # Update scene params
            scene.camera = pyredner.Camera(
                position=torch.tensor(cam_params[0], dtype=torch.float32),
                look_at=torch.tensor(cam_params[1], dtype=torch.float32),
                up=torch.tensor(cam_params[2], dtype=torch.float32),
                fov=torch.tensor([45.0]),
                clip_near=1e-2,
                resolution=(opt.resolution, opt.resolution),
                fisheye=False)
            args = pyredner.RenderFunction.serialize_scene(
                scene=scene,
                num_samples=1,
                max_bounces=1,
                channels=[redner.channels.alpha])

            out = pyredner.RenderFunction.apply(1, *args)

            fn = gen_hash(6)
            imwrite(out, os.path.join(output_path, '{}.png'.format(fn)))
            log[fn] = {'elevation': elevation, 'azimuth': azimuth}
    return log
Пример #15
0
mat_checkerboard = pyredner.Material(\
    diffuse_reflectance = checkerboard_texture)
mat_black = pyredner.Material(\
    diffuse_reflectance = torch.tensor([0.0, 0.0, 0.0],
    device = pyredner.get_device()))
materials = [mat_checkerboard, mat_black]
vertices = torch.tensor(
    [[-1.0, -1.0, 0.0], [-1.0, 1.0, 0.0], [1.0, -1.0, 0.0], [1.0, 1.0, 0.0]],
    device=pyredner.get_device())
indices = torch.tensor([[0, 1, 2], [1, 3, 2]],
                       dtype=torch.int32,
                       device=pyredner.get_device())
uvs = torch.tensor([[0.05, 0.05], [0.05, 0.95], [0.95, 0.05], [0.95, 0.95]],
                   device=pyredner.get_device())
shape_plane = pyredner.Shape(vertices, indices, uvs, None, 0)
light_vertices = torch.tensor([[-1.0, -1.0, -7.0], [1.0, -1.0, -7.0],
                               [-1.0, 1.0, -7.0], [1.0, 1.0, -7.0]],
                              device=pyredner.get_device())
light_indices = torch.tensor([[0, 1, 2], [1, 3, 2]],
                             dtype=torch.int32,
                             device=pyredner.get_device())
shape_light = pyredner.Shape(light_vertices, light_indices, None, None, 1)
shapes = [shape_plane, shape_light]
light_intensity = torch.tensor([20.0, 20.0, 20.0])
# The first argument is the shape id of the light
light = pyredner.AreaLight(1, light_intensity)
area_lights = [light]
scene = pyredner.Scene(cam, shapes, materials, area_lights)
args = pyredner.RenderFunction.serialize_scene(\
    scene = scene,
Пример #16
0
                     clip_near = clip_near,
                     resolution = resolution)

mat_grey = pyredner.Material(\
    diffuse_reflectance = torch.tensor([0.5, 0.5, 0.5],
    device = pyredner.get_device()))
mat_black = pyredner.Material(\
    diffuse_reflectance = torch.tensor([0.0, 0.0, 0.0],
    device = pyredner.get_device()))
materials = [mat_grey, mat_black]

floor_vertices = torch.tensor([[-20.0,0.0,-20.0],[-20.0,0.0,20.0],[20.0,0.0,-20.0],[20.0,0.0,20.0]],
    device = pyredner.get_device())
floor_indices = torch.tensor([[0,1,2], [1,3,2]],
    device = pyredner.get_device(), dtype = torch.int32)
shape_floor = pyredner.Shape(floor_vertices, floor_indices, 0)
blocker_vertices = torch.tensor(\
    [[-0.5,10.0,-0.5],[-0.5,10.0,0.5],[0.5,10.0,-0.5],[0.5,10.0,0.5]],
    device = pyredner.get_device())
blocker_indices = torch.tensor([[0,1,2], [1,3,2]],
    device = pyredner.get_device(), dtype = torch.int32)
shape_blocker = pyredner.Shape(blocker_vertices, blocker_indices, 0)
light_vertices = torch.tensor(\
    [[-0.1,15,-0.1],[-0.1,15,0.1],[0.1,15,-0.1],[0.1,15,0.1]],
    device = pyredner.get_device())
light_indices = torch.tensor([[0,2,1], [1,2,3]],
    device = pyredner.get_device(), dtype = torch.int32)
shape_light = pyredner.Shape(light_vertices, light_indices, 1)
shapes = [shape_floor, shape_blocker, shape_light]
light_intensity = torch.tensor([5000.0, 5000.0, 5000.0])
# The first argument is the shape id of the light
Пример #17
0
    up=torch.tensor([0.0, 1.0, 0.0]),
    fov=torch.tensor([45.0]),  # in degree
    clip_near=1e-2,  # needs to > 0
    resolution=(256, 256))

# Set "use_vertex_color = True" to use vertex color
mat_vertex_color = pyredner.Material(use_vertex_color=True)
materials = [mat_vertex_color]

vertices, indices, uvs, normals = pyredner.generate_sphere(128, 64)
# For the target we randomize the vertex color.
vertex_color = torch.zeros_like(vertices).uniform_(0.0, 1.0)
shape_sphere = pyredner.Shape(\
    vertices = vertices,
    indices = indices,
    uvs = uvs,
    normals = normals,
    colors = vertex_color, # use the 'colors' field in Shape to store the color
    material_id = 0)
shapes = [shape_sphere]

envmap = pyredner.imread('sunsky.exr')
if pyredner.get_use_gpu():
    envmap = envmap.cuda(device=pyredner.get_device())
envmap = pyredner.EnvironmentMap(envmap)
scene = pyredner.Scene(cam, shapes, materials, [], envmap)
scene_args = pyredner.RenderFunction.serialize_scene(\
    scene = scene,
    num_samples = 256,
    max_bounces = 1,
    channels = [redner.channels.radiance, redner.channels.vertex_color])
Пример #18
0
# Setup materials
material_id_map = {}
materials = []
count = 0
for key, value in material_map.items():
    material_id_map[key] = count
    count += 1
    materials.append(value)

# Setup geometries
shapes = []
for mtl_name, mesh in mesh_list:
    shapes.append(pyredner.Shape(\
        vertices = mesh.vertices,
        indices = mesh.indices,
        uvs = mesh.uvs,
        normals = mesh.normals,
        material_id = material_id_map[mtl_name]))

# We don't setup any light source here

# Construct the scene
scene = pyredner.Scene(cam, shapes, materials, area_lights=[], envmap=None)
# Serialize the scene
# Here we specify the output channels as "depth", "shading_normal"
scene_args = pyredner.RenderFunction.serialize_scene(\
    scene = scene,
    num_samples = 16,
    max_bounces = 0,
    channels = [redner.channels.depth, redner.channels.shading_normal])
Пример #19
0
def parse_shape(node, material_dict, shape_id, shape_group_dict=None):
    if node.attrib['type'] == 'obj' or node.attrib['type'] == 'serialized':
        to_world = torch.eye(4)
        serialized_shape_id = 0
        mat_id = -1
        light_intensity = None
        filename = ''
        for child in node:
            if 'name' in child.attrib:
                if child.attrib['name'] == 'filename':
                    filename = child.attrib['value']
                elif child.attrib['name'] == 'toWorld':
                    to_world = parse_transform(child)
                elif child.attrib['name'] == 'shapeIndex':
                    serialized_shape_id = int(child.attrib['value'])
            if child.tag == 'ref':
                mat_id = material_dict[child.attrib['id']]
            elif child.tag == 'emitter':
                for grandchild in child:
                    if grandchild.attrib['name'] == 'radiance':
                        light_intensity = parse_vector(
                            grandchild.attrib['value'])
                        if light_intensity.shape[0] == 1:
                            light_intensity = torch.tensor(\
                                         [light_intensity[0],
                                          light_intensity[0],
                                          light_intensity[0]])

        if node.attrib['type'] == 'obj':
            _, mesh_list, _ = pyredner.load_obj(filename)
            vertices = mesh_list[0][1].vertices.cpu()
            indices = mesh_list[0][1].indices.cpu()
            uvs = mesh_list[0][1].uvs
            normals = mesh_list[0][1].normals
            if uvs is not None:
                uvs = uvs.cpu()
            if normals is not None:
                normals = normals.cpu()
        else:
            assert (node.attrib['type'] == 'serialized')
            mitsuba_tri_mesh = redner.load_serialized(filename,
                                                      serialized_shape_id)
            vertices = torch.from_numpy(mitsuba_tri_mesh.vertices)
            indices = torch.from_numpy(mitsuba_tri_mesh.indices)
            uvs = torch.from_numpy(mitsuba_tri_mesh.uvs)
            normals = torch.from_numpy(mitsuba_tri_mesh.normals)
            if uvs.shape[0] == 0:
                uvs = None
            if normals.shape[0] == 0:
                normals = None

        # Transform the vertices and normals
        vertices = torch.cat((vertices, torch.ones(vertices.shape[0], 1)),
                             dim=1)
        vertices = vertices @ torch.transpose(to_world, 0, 1)
        vertices = vertices / vertices[:, 3:4]
        vertices = vertices[:, 0:3].contiguous()
        if normals is not None:
            normals = normals @ (torch.inverse(torch.transpose(to_world, 0,
                                                               1))[:3, :3])
            normals = normals.contiguous()
        assert (vertices is not None)
        assert (indices is not None)
        lgt = None
        if light_intensity is not None:
            lgt = pyredner.AreaLight(shape_id, light_intensity)

        if pyredner.get_use_gpu():
            # Copy to GPU
            vertices = vertices.cuda()
            indices = indices.cuda()
            if uvs is not None:
                uvs = uvs.cuda()
            if normals is not None:
                normals = normals.cuda()
        return pyredner.Shape(vertices, indices, uvs, normals, mat_id), lgt
    elif node.attrib['type'] == 'rectangle':
        indices = torch.tensor([[0, 2, 1], [1, 2, 3]], dtype=torch.int32)
        vertices = torch.tensor([[-1.0, -1.0, 0.0], [-1.0, 1.0, 0.0],
                                 [1.0, -1.0, 0.0], [1.0, 1.0, 0.0]])
        uvs = None
        normals = None
        to_world = torch.eye(4)
        mat_id = -1
        light_intensity = None
        for child in node:
            if 'name' in child.attrib:
                if child.attrib['name'] == 'toWorld':
                    to_world = parse_transform(child)
            if child.tag == 'ref':
                mat_id = material_dict[child.attrib['id']]
            elif child.tag == 'emitter':
                for grandchild in child:
                    if grandchild.attrib['name'] == 'radiance':
                        light_intensity = parse_vector(
                            grandchild.attrib['value'])
                        if light_intensity.shape[0] == 1:
                            light_intensity = torch.tensor(\
                                         [light_intensity[0],
                                          light_intensity[0],
                                          light_intensity[0]])
        # Transform the vertices
        # Transform the vertices and normals
        vertices = torch.cat((vertices, torch.ones(vertices.shape[0], 1)),
                             dim=1)
        vertices = vertices @ torch.transpose(to_world, 0, 1)
        vertices = vertices / vertices[:, 3:4]
        vertices = vertices[:, 0:3].contiguous()
        if normals is not None:
            normals = normals @ (torch.inverse(torch.transpose(to_world, 0,
                                                               1))[:3, :3])
            normals = normals.contiguous()
        assert (vertices is not None)
        assert (indices is not None)
        lgt = None
        if light_intensity is not None:
            lgt = pyredner.AreaLight(shape_id, light_intensity)

        if pyredner.get_use_gpu():
            # Copy to GPU
            vertices = vertices.cuda()
            indices = indices.cuda()
            if uvs is not None:
                uvs = uvs.cuda()
            if normals is not None:
                normals = normals.cuda()
        return pyredner.Shape(vertices, indices, uvs, normals, mat_id), lgt
    # Add instance support
    # TODO (simply transform & create a new shape now)
    elif node.attrib['type'] == 'instance':
        shape = None
        for child in node:
            if 'name' in child.attrib:
                if child.attrib['name'] == 'toWorld':
                    to_world = parse_transform(child)
                    if pyredner.get_use_gpu():
                        to_world = to_world.cuda()
            if child.tag == 'ref':
                shape = shape_group_dict[child.attrib['id']]
        # transform instance
        vertices = shape.vertices
        normals = shape.normals
        vector1 = torch.ones(vertices.shape[0], 1)
        vertices = torch.cat(
            (vertices, vector1.cuda() if pyredner.get_use_gpu() else vector1),
            dim=1)
        vertices = vertices @ torch.transpose(to_world, 0, 1)
        vertices = vertices / vertices[:, 3:4]
        vertices = vertices[:, 0:3].contiguous()
        if normals is not None:
            normals = normals @ (torch.inverse(torch.transpose(to_world, 0,
                                                               1))[:3, :3])
            normals = normals.contiguous()
        # assert(vertices is not None)
        # assert(indices is not None)
        # lgt = None
        # if light_intensity is not None:
        #     lgt = pyredner.AreaLight(shape_id, light_intensity)

        return pyredner.Shape(vertices, shape.indices, shape.uvs, normals,
                              shape.material_id), None
    else:
        print('Shape type {} is not supported!'.format(node.attrib['type']))
        assert (False)
Пример #20
0
def parse_shape(node, material_dict, shape_id, shape_group_dict=None):
    if node.attrib['type'] == 'obj' or node.attrib['type'] == 'serialized':
        print(node.attrib['id'])
        to_world = torch.eye(4)
        serialized_shape_id = 0
        mat_id = -1
        light_intensity = None
        filename = ''
        mat_name2id = {}
        for child in node:
            if 'name' in child.attrib:
                if child.attrib['name'] == 'filename':
                    filename = child.attrib['value']
                elif child.attrib['name'] == 'toWorld':
                    to_world = parse_transform(child)
                elif child.attrib['name'] == 'shapeIndex':
                    serialized_shape_id = int(child.attrib['value'])
            if child.tag == 'ref':
                mat_id = material_dict[child.attrib['id']]
                if 'name' in child.attrib.keys(
                ) and child.attrib['name'] != 'bsdf':
                    mat_name2id[child.attrib['name']] = child.attrib['id']
            elif child.tag == 'bsdf':
                #TODO hack! use default diffuse if countering internal declaration bsdf
                mat_id = 0
                # node_id, material = parse_material(child)
                # if node_id is not None:
                #     material_dict[node_id] = len(materials)
                #     materials.append(material)
            elif child.tag == 'emitter':
                for grandchild in child:
                    if grandchild.attrib['name'] == 'radiance':
                        light_intensity = parse_vector(
                            grandchild.attrib['value'])
                        if light_intensity.shape[0] == 1:
                            light_intensity = torch.tensor(\
                                         [light_intensity[0],
                                          light_intensity[0],
                                          light_intensity[0]])

        if node.attrib['type'] == 'obj':
            _, mesh_list, _ = pyredner.load_obj.load_obj_fast(
                filename, is_load_mtl=False)
            shape_list = []
            for mesh in mesh_list:
                mat_name = mesh[0]
                vertices = mesh[1].vertices.cpu()
                indices = mesh[1].indices.cpu()
                uvs = mesh[1].uvs
                normals = mesh[1].normals
                if uvs is not None:
                    uvs = uvs.cpu()
                if normals is not None:
                    normals = normals.cpu()

                # Transform the vertices and normals
                vertices = torch.cat(
                    (vertices, torch.ones(vertices.shape[0], 1)), dim=1)
                vertices = vertices @ torch.transpose(to_world, 0, 1)
                vertices = vertices / vertices[:, 3:4]
                vertices = vertices[:, 0:3].contiguous()
                if normals is not None:
                    normals = normals @ (torch.inverse(
                        torch.transpose(to_world, 0, 1))[:3, :3])
                    normals = normals.contiguous()
                assert (vertices is not None)
                assert (indices is not None)
                lgt = None
                if light_intensity is not None:
                    lgt = pyredner.AreaLight(shape_id, light_intensity)

                if pyredner.get_use_gpu():
                    # Copy to GPU
                    vertices = vertices.cuda()
                    indices = indices.cuda()
                    if uvs is not None:
                        uvs = uvs.cuda()
                    if normals is not None:
                        normals = normals.cuda()
                # Assign material
                if mat_name != '' and mat_name is not None:  # skip no material mesh
                    mat_id = material_dict[mat_name2id[mat_name]]
                shape_list.append(
                    pyredner.Shape(vertices, indices, uvs, normals, mat_id))
            return shape_list, lgt
        else:
            assert (node.attrib['type'] == 'serialized')
            mitsuba_tri_mesh = redner.load_serialized(filename,
                                                      serialized_shape_id)
            vertices = torch.from_numpy(mitsuba_tri_mesh.vertices)
            indices = torch.from_numpy(mitsuba_tri_mesh.indices)
            uvs = torch.from_numpy(mitsuba_tri_mesh.uvs)
            normals = torch.from_numpy(mitsuba_tri_mesh.normals)
            if uvs.shape[0] == 0:
                uvs = None
            if normals.shape[0] == 0:
                normals = None

            # Transform the vertices and normals
            vertices = torch.cat((vertices, torch.ones(vertices.shape[0], 1)),
                                 dim=1)
            vertices = vertices @ torch.transpose(to_world, 0, 1)
            vertices = vertices / vertices[:, 3:4]
            vertices = vertices[:, 0:3].contiguous()
            if normals is not None:
                normals = normals @ (torch.inverse(
                    torch.transpose(to_world, 0, 1))[:3, :3])
                normals = normals.contiguous()
            assert (vertices is not None)
            assert (indices is not None)
            lgt = None
            if light_intensity is not None:
                lgt = pyredner.AreaLight(shape_id, light_intensity)

            if pyredner.get_use_gpu():
                # Copy to GPU
                vertices = vertices.cuda()
                indices = indices.cuda()
                if uvs is not None:
                    uvs = uvs.cuda()
                if normals is not None:
                    normals = normals.cuda()
            return pyredner.Shape(vertices, indices, uvs, normals, mat_id), lgt
    elif node.attrib['type'] == 'rectangle':
        indices = torch.tensor([[0, 2, 1], [1, 2, 3]], dtype=torch.int32)
        vertices = torch.tensor([[-1.0, -1.0, 0.0], [-1.0, 1.0, 0.0],
                                 [1.0, -1.0, 0.0], [1.0, 1.0, 0.0]])
        uvs = None
        normals = None
        to_world = torch.eye(4)
        mat_id = -1
        light_intensity = None
        for child in node:
            if 'name' in child.attrib:
                if child.attrib['name'] == 'toWorld':
                    to_world = parse_transform(child)
            if child.tag == 'ref':
                mat_id = material_dict[child.attrib['id']]
            elif child.tag == 'emitter':
                for grandchild in child:
                    if grandchild.attrib['name'] == 'radiance':
                        light_intensity = parse_vector(
                            grandchild.attrib['value'])
                        if light_intensity.shape[0] == 1:
                            light_intensity = torch.tensor(\
                                         [light_intensity[0],
                                          light_intensity[0],
                                          light_intensity[0]])
        # Transform the vertices
        # Transform the vertices and normals
        vertices = torch.cat((vertices, torch.ones(vertices.shape[0], 1)),
                             dim=1)
        vertices = vertices @ torch.transpose(to_world, 0, 1)
        vertices = vertices / vertices[:, 3:4]
        vertices = vertices[:, 0:3].contiguous()
        if normals is not None:
            normals = normals @ (torch.inverse(torch.transpose(to_world, 0,
                                                               1))[:3, :3])
            normals = normals.contiguous()
        assert (vertices is not None)
        assert (indices is not None)
        lgt = None
        if light_intensity is not None:
            lgt = pyredner.AreaLight(shape_id, light_intensity)

        if pyredner.get_use_gpu():
            # Copy to GPU
            vertices = vertices.cuda()
            indices = indices.cuda()
            if uvs is not None:
                uvs = uvs.cuda()
            if normals is not None:
                normals = normals.cuda()
        return pyredner.Shape(vertices, indices, uvs, normals, mat_id), lgt
    # Add instance support
    # TODO (simply transform & create a new shape now)
    elif node.attrib['type'] == 'instance':
        shape = None
        for child in node:
            if 'name' in child.attrib:
                if child.attrib['name'] == 'toWorld':
                    to_world = parse_transform(child)
                    if pyredner.get_use_gpu():
                        to_world = to_world.cuda()
            if child.tag == 'ref':
                shape_ = shape_group_dict[child.attrib['id']]
        shape_list = []
        for shape in list(shape_):
            # transform instance
            vertices = shape.vertices
            normals = shape.normals
            vector1 = torch.ones(vertices.shape[0], 1)
            vertices = torch.cat(
                (vertices,
                 vector1.cuda() if pyredner.get_use_gpu() else vector1),
                dim=1)
            vertices = vertices @ torch.transpose(to_world, 0, 1)
            vertices = vertices / vertices[:, 3:4]
            vertices = vertices[:, 0:3].contiguous()
            if normals is not None:
                normals = normals @ (torch.inverse(
                    torch.transpose(to_world, 0, 1))[:3, :3])
                normals = normals.contiguous()
            # assert(vertices is not None)
            # assert(indices is not None)
            # lgt = None
            # if light_intensity is not None:
            #     lgt = pyredner.AreaLight(shape_id, light_intensity)
            shape_list.append(
                pyredner.Shape(vertices, shape.indices, shape.uvs, normals,
                               shape.material_id))

        return shape_list, None
    else:
        print('Shape type {} is not supported!'.format(node.attrib['type']))
        assert (False)
Пример #21
0
# so the triangle will block the quad.
quad_vertices = torch.cat(
    (quad_vertices_2d,
     torch.ones(quad_vertices_2d.shape[0], 1, device=pyredner.get_device())),
    dim=1).contiguous()
tri_vertices = torch.cat(
    (tri_vertices_2d,
     torch.zeros(tri_vertices_2d.shape[0], 1, device=pyredner.get_device())),
    dim=1).contiguous()
quad_indices = torch.tensor([[0, 1, 2], [1, 2, 3]],
                            dtype=torch.int32,
                            device=pyredner.get_device())
tri_indices = torch.tensor([[0, 1, 2]],
                           dtype=torch.int32,
                           device=pyredner.get_device())
shape_quad = pyredner.Shape(quad_vertices, quad_indices, None, None, 0)
shape_tri = pyredner.Shape(tri_vertices, tri_indices, None, None, 1)
shapes = [shape_quad, shape_tri]

# Setup the scene. We don't need lights.
scene = pyredner.Scene(cam, shapes, materials, [])
# We output the shape id, so that we can shape it later
args = pyredner.RenderFunction.serialize_scene(\
    scene = scene,
    num_samples = 16,
    # Set max bounces to 0, we don't need lighting.
    max_bounces = 0,
    # Use the diffuse color as the output
    channels = [redner.channels.diffuse_reflectance])

# Render the scene as our target image.
Пример #22
0
                                   device=pyredner.get_device(),
                                   requires_grad=True)

mat_grey = pyredner.Material(diffuse_reflectance)

# The material list of the scene #
materials = [mat_grey]

# Now we build a list of shapes using the list loaded from the Wavefront object file.
# Meshes loaded from .obj files may have different indices for uvs and normals,
# we use mesh.uv_indices and mesh.normal_indices to access them.
# This mesh does not have normal_indices so the value is None.

shape_diamond = pyredner.Shape(vertices=mesh.vertices / 20,
                               indices=mesh.indices,
                               uvs=None,
                               normals=mesh.normals,
                               material_id=0)

shape_light = pyredner.Shape(\
    vertices = torch.tensor([[-1.0, -1.0, -7.0],
                             [ 2.0, -1.0, -7.0],
                             [-1.0,  1.0, -7.0],
                             [ 2.0,  1.0, -7.0]], device = pyredner.get_device()),
    indices = torch.tensor([[0, 1, 2],[1, 3, 2]],
        dtype = torch.int32, device = pyredner.get_device()),
    uvs = None,
    normals = None,
    material_id = 0)

# The shape list of our scene contains two shapes:
Пример #23
0
# Setup camera
cam = pyredner.Camera(
    position=torch.tensor([0.0, 30.0, 200.0]),
    look_at=torch.tensor([0.0, 30.0, 0.0]),
    up=torch.tensor([0.0, 1.0, 0.0]),
    fov=torch.tensor([45.0]),  # in degree
    clip_near=1e-2,  # needs to > 0
    resolution=(256, 256),
    fisheye=False)

# Get a list of shapes
shapes = []
for mtl_name, mesh in mesh_list:
    shapes.append(pyredner.Shape(\
        vertices = mesh.vertices,
        indices = mesh.indices,
        uvs = mesh.uvs,
        normals = mesh.normals,
        material_id = 0)) # Set all materials to the generic texture

render = pyredner.RenderFunction.apply

tex_path = '../tutorials/teapot.png'
tex_tensor = pyredner.imread(tex_path)
if pyredner.get_use_gpu():
    tex_tensor = tex_tensor.cuda(device=pyredner.get_device())

### TEST 1: regular 3-channels texture rasterization

generic_texture = tex_tensor

materials = [pyredner.Material(generic_texture=generic_texture)]