def _update_scene(self):
        # update camera
        camera = IntrinsicsCamera(
            self.camera.intrinsics.fx,
            self.camera.intrinsics.fy,
            self.camera.intrinsics.cx,
            self.camera.intrinsics.cy,
        )
        cn = next(iter(self._scene.get_nodes(name=self.camera.frame)))
        cn.camera = camera
        pose_m = self.camera.pose.matrix.copy()
        pose_m[:, 1:3] *= -1.0
        cn.matrix = pose_m
        self._scene.main_camera_node = cn

        # update workspace
        for obj_key in self.state.workspace_keys:
            next(
                iter(self._scene.get_nodes(name=obj_key))
            ).matrix = self.state[obj_key].pose.matrix

        # update object
        for obj_key in self.state.obj_keys:
            next(
                iter(self._scene.get_nodes(name=obj_key))
            ).matrix = self.state[obj_key].pose.matrix
    def _reset_scene(self, scale_factor=1.0):
        """Resets the scene.

        Parameters
        ----------
        scale_factor : float
            optional scale factor to apply to the image dimensions
        """
        # delete scene
        if self._scene is not None:
            self._scene.clear()
            del self._scene

        # create scene
        scene = Scene()

        # setup camera
        camera = IntrinsicsCamera(
            self.camera.intrinsics.fx,
            self.camera.intrinsics.fy,
            self.camera.intrinsics.cx,
            self.camera.intrinsics.cy,
        )
        pose_m = self.camera.pose.matrix.copy()
        pose_m[:, 1:3] *= -1.0
        scene.add(camera, pose=pose_m, name=self.camera.frame)
        scene.main_camera_node = next(
            iter(scene.get_nodes(name=self.camera.frame))
        )

        material = MetallicRoughnessMaterial(
            baseColorFactor=np.array([1, 1, 1, 1.0]),
            metallicFactor=0.2,
            roughnessFactor=0.8,
        )

        # add workspace objects
        for obj_key in self.state.workspace_keys:
            obj_state = self.state[obj_key]
            obj_mesh = Mesh.from_trimesh(obj_state.mesh, material=material)
            T_obj_world = obj_state.pose.matrix
            scene.add(obj_mesh, pose=T_obj_world, name=obj_key)

        # add scene objects
        for obj_key in self.state.obj_keys:
            obj_state = self.state[obj_key]
            obj_mesh = Mesh.from_trimesh(obj_state.mesh, material=material)
            T_obj_world = obj_state.pose.matrix
            scene.add(obj_mesh, pose=T_obj_world, name=obj_key)

        # add light (for color rendering)
        light = DirectionalLight(color=np.ones(3), intensity=1.0)
        scene.add(light, pose=np.eye(4))
        ray_light_nodes = self._create_raymond_lights()
        [scene.add_node(rln) for rln in ray_light_nodes]

        self._scene = scene
Exemple #3
0
def get_pyrender_cam(intrinsics, cam_pose, scene):
    R, tvec = from_pyrender_pose(cam_pose)

    import itertools
    bbx = scene.bounds
    abs_z_values = []
    for (x, y, z) in itertools.product(list(bbx[:, 0]), list(bbx[:, 1]),
                                       list(bbx[:, 2])):
        tmp = np.array([x, y, z]).reshape((3, 1))
        tmp = np.dot(R, tmp) + tvec
        abs_z_values.append(np.abs(tmp[2, 0]))

    znear = np.min(abs_z_values) * 0.9
    zfar = np.max(abs_z_values) * 1.1
    print('znear, zfar: {},{}'.format(znear, zfar))

    fx, fy, cx, cy = intrinsics
    cam = IntrinsicsCamera(fx=fx, fy=fy, cx=cx, cy=cy, znear=znear, zfar=zfar)

    return cam
Exemple #4
0
    def _reset_scene(self, scale_factor=1.0):
        """ Resets the scene.

        Parameters
        ----------
        scale_factor : float
            optional scale factor to apply to the image dimensions
        """
        # delete scene
        if self._scene is not None:
            self._scene.clear()
            del self._scene

        # create scene
        scene = Scene()

        # setup camera
        camera = IntrinsicsCamera(self.camera.intrinsics.fx, self.camera.intrinsics.fy, 
                                  self.camera.intrinsics.cx, self.camera.intrinsics.cy)
        pose_m = self.camera.pose.matrix.copy()
        pose_m[:,1:3] *= -1.0
        scene.add(camera, pose=pose_m, name=self.camera.frame)
        scene.main_camera_node = next(iter(scene.get_nodes(name=self.camera.frame)))

        # add workspace objects
        for obj_key in self.state.workspace_keys:
            obj_state = self.state[obj_key]
            obj_mesh = Mesh.from_trimesh(obj_state.mesh)
            T_obj_world = obj_state.pose.matrix
            scene.add(obj_mesh, pose=T_obj_world, name=obj_key)

        # add scene objects
        for obj_key in self.state.obj_keys:
            obj_state = self.state[obj_key]
            obj_mesh = Mesh.from_trimesh(obj_state.mesh)
            T_obj_world = obj_state.pose.matrix
            scene.add(obj_mesh, pose=T_obj_world, name=obj_key)

        self._scene = scene
Exemple #5
0
def main(mesh_path):
    # rendering conf
    ambient_light = 0.8
    directional_light = 1.0
    img_res = 512
    cam_f = 500
    cam_c = img_res / 2.0

    scene = Scene(ambient_light=np.array(
        [ambient_light, ambient_light, ambient_light, 1.0]))

    mesh_v, mesh_vc, mesh_f = load_ply_data(mesh_path)
    mesh_ = trimesh.Trimesh(vertices=mesh_v,
                            faces=mesh_f,
                            vertex_colors=mesh_vc)
    points_mesh = Mesh.from_trimesh(mesh_, smooth=True, material=None)
    mesh_node = scene.add(points_mesh)

    cam = IntrinsicsCamera(fx=cam_f, fy=cam_f, cx=cam_c, cy=cam_c)
    cam_pose = np.array([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0],
                         [0.0, 0.0, 1.0, 2.0], [0.0, 0.0, 0.0, 1.0]])

    direc_l = DirectionalLight(color=np.ones(3), intensity=directional_light)
    light_node_1 = scene.add(direc_l,
                             pose=np.matmul(rotationy(30), rotationx(45)))
    direc_l = DirectionalLight(color=np.ones(3), intensity=directional_light)
    light_node_2 = scene.add(direc_l,
                             pose=np.matmul(rotationy(-30), rotationx(45)))
    direc_l = DirectionalLight(color=np.ones(3), intensity=directional_light)
    light_node_3 = scene.add(direc_l,
                             pose=np.matmul(rotationy(-180), rotationx(45)))
    direc_l = DirectionalLight(color=np.ones(3),
                               intensity=(directional_light - 0.5))
    light_node_4 = scene.add(direc_l,
                             pose=np.matmul(rotationy(0), rotationx(-10)))

    ################
    # rendering
    cam_node = scene.add(cam, pose=cam_pose)
    render_flags = {
        'flip_wireframe': False,
        'all_wireframe': False,
        'all_solid': False,
        'shadows': True,
        'vertex_normals': False,
        'face_normals': False,
        'cull_faces': True,
        'point_size': 1.0,
    }
    viewer_flags = {
        'mouse_pressed': False,
        'rotate': False,
        'rotate_rate': np.pi / 6.0,
        'rotate_axis': np.array([0.0, 1.0, 0.0]),
        'view_center': np.array([0.0, 0.0, 0.0]),
        'record': False,
        'use_raymond_lighting': False,
        'use_direct_lighting': False,
        'lighting_intensity': 3.0,
        'use_perspective_cam': True,
        'window_title': 'DIT',
        'refresh_rate': 25.0,
        'fullscreen': False,
        'show_world_axis': False,
        'show_mesh_axes': False,
        'caption': None,
        'save_one_frame': False,
    }
    v = Viewer(scene,
               viewport_size=(512, 512),
               render_flags=render_flags,
               viewer_flags=viewer_flags,
               run_in_thread=False)
    v.close()