def _render_opengl(data_source,
                   field=None,
                   window_size=None,
                   cam_position=None,
                   cam_focus=None):
    """High level wrapper for Interactive Data Visualization

    Parameters
    ----------
    data_source : :class:`yt.data_objects.data_containers.AMR3DData`,
                  :class:`yt.data_objects.static_output.Dataset`
        This is the source to be rendered, which can be any arbitrary yt
        3D object
    field : string, tuple, optional
        The field to be rendered. If unspecified, this will use the
        default_field for your dataset's frontend--usually ('gas', 'density').
    window_size : 2 element tuple of ints
        The width and the height of the Interactive Data Visualization window.
        For performance reasons it is recommended to use values that are natural
        powers of 2.
    cam_position : 3 element YTArray, optional
        The camera position in physical coordinates. If unspecified,
        data_source's domain right edge will be used.
    cam_focus: 3 element YTArray, optional
        The focus defines the point the camera is pointed at. If unspecified,
        data_source's domain center will be used.

    Examples
    --------

    >>> import yt
    >>> ds = yt.load("Enzo_64/DD0046/DD0046")
    >>> yt.interactive_render(ds)

    """

    try:
        import cyglfw3  # NOQA
        import OpenGL.GL  # NOQA
    except ImportError:
        raise ImportError(
            "This functionality requires the cyglfw3 and PyOpenGL "
            "packages to be installed.")

    from .interactive_loop import RenderingContext
    from .interactive_vr import (
        BlockCollection,
        MeshSceneComponent,
        SceneGraph,
        TrackballCamera,
    )

    if isinstance(data_source, Dataset):
        dobj = data_source.all_data()
    else:
        dobj = data_source
    if field is None:
        field = dobj.ds.default_field
        if field not in dobj.ds.derived_field_list:
            raise YTSceneFieldNotFound("""Could not find field '%s' in %s.
                  Please specify a field in create_scene()""" %
                                       (field, dobj.ds))
        mylog.info("Setting default field to %s" % field.__repr__())
    if window_size is None:
        window_size = (1024, 1024)
    if cam_position is None:
        cam_position = dobj.ds.domain_right_edge
        if hasattr(dobj.ds.index, "meshes"):
            # unstructured mesh datasets tend to have tight
            # domain boundaries, do some extra padding here.
            cam_position = 3.0 * dobj.ds.domain_right_edge
    if cam_focus is None:
        cam_focus = dobj.ds.domain_center

    rc = RenderingContext(*window_size)

    if hasattr(dobj.ds.index, "meshes"):
        scene = MeshSceneComponent(dobj, field)
    else:
        scene = SceneGraph()
        collection = BlockCollection()
        collection.add_data(dobj, field)
        scene.add_collection(collection)

    aspect_ratio = window_size[1] / window_size[0]
    far_plane = np.linalg.norm(cam_focus - cam_position) * 2.0
    near_plane = 0.01 * far_plane

    c = TrackballCamera(
        position=cam_position,
        focus=cam_focus,
        near_plane=near_plane,
        far_plane=far_plane,
        aspect_ratio=aspect_ratio,
    )
    rc.start_loop(scene, c)
Exemple #2
0
def create_scene(data_source, field=None, lens_type="plane-parallel"):
    r""" Set up a scene object with sensible defaults for use in volume
    rendering.

    A helper function that creates a default camera view, transfer
    function, and image size. Using these, it returns an instance
    of the Scene class, allowing one to further modify their rendering.

    This function is the same as volume_render() except it doesn't render
    the image.

    Parameters
    ----------
    data_source : :class:`yt.data_objects.data_containers.AMR3DData`
        This is the source to be rendered, which can be any arbitrary yt
        3D object
    field: string, tuple, optional
        The field to be rendered. If unspecified, this will use the
        default_field for your dataset's frontend--usually ('gas', 'density').
        A default transfer function will be built that spans the range of
        values for that given field, and the field will be logarithmically
        scaled if the field_info object specifies as such.
    lens_type: string, optional
        This specifies the type of lens to use for rendering. Current
        options are 'plane-parallel', 'perspective', and 'fisheye'. See
        :class:`yt.visualization.volume_rendering.lens.Lens` for details.
        Default: 'plane-parallel'

    Returns
    -------
    sc: Scene
        A :class:`yt.visualization.volume_rendering.scene.Scene` object
        that was constructed during the rendering. Useful for further
        modifications, rotations, etc.

    Examples
    --------

    >>> import yt
    >>> ds = yt.load("Enzo_64/DD0046/DD0046")
    >>> sc = yt.create_scene(ds)
    """

    data_source = data_source_or_all(data_source)
    sc = Scene()
    if field is None:
        field = data_source.ds.default_field
        if field not in data_source.ds.derived_field_list:
            raise YTSceneFieldNotFound("""Could not find field '%s' in %s.
                  Please specify a field in create_scene()""" %
                                       (field, data_source.ds))
        mylog.info("Setting default field to %s", field.__repr__())

    if hasattr(data_source.ds.index, "meshes"):
        source = MeshSource(data_source, field=field)
    else:
        source = VolumeSource(data_source, field=field)

    sc.add_source(source)
    sc.add_camera(data_source=data_source, lens_type=lens_type)
    return sc