Exemplo n.º 1
0
    def _fit_view(self, scene_state):
        """Fit the view to the scene."""
        if len(scene_state.particles) > 1:
            window_size = self.get_window_size()
            initial_size = Vector2f(
                self._initial_view_aperture_x,
                self._initial_view_aperture_x * window_size.y / window_size.x)

            lower_bounds = scene_state.lower_bounds
            upper_bounds = scene_state.upper_bounds
            required_size = (upper_bounds - lower_bounds).iscale(
                _FIT_VIEW_REL_PADDING)
            required_size = Vector2f(
                max(required_size.x,
                    initial_size.x * self._view_zoom_range[0]),
                max(required_size.y,
                    initial_size.y * self._view_zoom_range[0]))

            self.view_position = (lower_bounds + upper_bounds).iscale(0.5)
            self.view_zoom = min(initial_size.x / required_size.x,
                                 initial_size.y / required_size.y)
        elif len(scene_state.particles) == 1:
            self.view_position = Vector2f(
                *scene_state.particles['position'][0])
            self.view_zoom = 1.0
        else:
            self._reset_view()

        self._has_view_changed = True
Exemplo n.º 2
0
    def render(self, scene_state):
        """Render a new frame.

        Parameters
        ----------
        scene_state : hienoi.renderer.SceneState
            Scene state.
        """
        renderer_state = hienoi.renderer.State(
            window_size=self.get_window_size(),
            view_position=self.view_position,
            view_zoom=self._view_zoom,
            origin=self.world_to_screen(Vector2f(0.0, 0.0)),
            initial_view_aperture_x=self._initial_view_aperture_x,
            view_aperture=self.get_view_aperture(),
            grid_density=self.grid_density,
            grid_adaptive_threshold=self._grid_adaptive_threshold,
            background_color=self.background_color,
            grid_color=self.grid_color,
            grid_origin_color=self.grid_origin_color,
            show_grid=self.show_grid,
            particle_display=self.particle_display,
            point_size=self.point_size,
            edge_feather=self.edge_feather,
            stroke_width=self.stroke_width,
        )

        self._renderer.render(renderer_state, scene_state)

        if hienoi.renderer.get_info().api == GraphicsAPI.OPENGL:
            sdl2.SDL_GL_SwapWindow(self._handles.window)
Exemplo n.º 3
0
    def get_view_aperture(self):
        """Retrieve the view aperture.

        It represents the area in world units covered by the view.

        Returns
        -------
        hienoi.Vector2f
            The view aperture.
        """
        window_size = self.get_window_size()
        aperture_x = self._initial_view_aperture_x / self._view_zoom
        return Vector2f(aperture_x, aperture_x * window_size.y / window_size.x)
Exemplo n.º 4
0
    def screen_to_world(self, point):
        """Convert a point from screen space to world space coordinates.

        Parameters
        ----------
        point : hienoi.Vector2i
            Point in screen space coordinates.

        Returns
        -------
        hienoi.Vector2f
            The point in world space coordinates.
        """
        window_size = self.get_window_size()
        view_aperture = self.get_view_aperture()
        return Vector2f(
            (self.view_position.x
             + (point.x - window_size.x / 2.0)
             * view_aperture.x / window_size.x),
            (self.view_position.y
             - (point.y - window_size.y / 2.0)
             * view_aperture.y / window_size.y))
Exemplo n.º 5
0
 def _reset_view(self):
     """Reset the view position and zoom."""
     self.view_position = Vector2f(0.0, 0.0)
     self.view_zoom = 1.0
     self._has_view_changed = True
Exemplo n.º 6
0
    def __init__(self,
                 window_title='hienoi',
                 window_position=Vector2i(sdl2.SDL_WINDOWPOS_CENTERED,
                                          sdl2.SDL_WINDOWPOS_CENTERED),
                 window_size=Vector2i(800, 600),
                 window_flags=sdl2.SDL_WINDOW_RESIZABLE,
                 view_aperture_x=100.0,
                 view_zoom_range=Vector2f(1e-6, 1e+6),
                 mouse_wheel_step=0.01,
                 grid_density=10.0,
                 grid_adaptive_threshold=3.0,
                 show_grid=True,
                 background_color=Vector4f(0.15, 0.15, 0.15, 1.0),
                 grid_color=Vector4f(0.85, 0.85, 0.85, 0.05),
                 grid_origin_color=Vector4f(0.85, 0.25, 0.25, 0.25),
                 particle_display=ParticleDisplay.DISC,
                 point_size=4,
                 edge_feather=2.0,
                 stroke_width=0.0,
                 initialize_callback=None,
                 on_event_callback=None,
                 renderer=None):
        renderer = {} if renderer is None else renderer

        if sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO) != 0:
            raise RuntimeError(sdl2.SDL_GetError().decode())

        renderer_info = hienoi.renderer.get_info()
        if renderer_info.api == GraphicsAPI.OPENGL:
            sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_CONTEXT_MAJOR_VERSION,
                                     renderer_info.major_version)
            sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_CONTEXT_MINOR_VERSION,
                                     renderer_info.minor_version)
            if renderer_info.profile == GLProfile.CORE:
                sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_CONTEXT_PROFILE_MASK,
                                         sdl2.SDL_GL_CONTEXT_PROFILE_CORE)

        self._handles = _create_handles(window_title, window_position,
                                        window_size, window_flags,
                                        renderer_info)
        self._renderer = hienoi.renderer.Renderer(**renderer)

        self._initial_view_aperture_x = view_aperture_x
        self._view_zoom_range = view_zoom_range
        self._mouse_wheel_step = mouse_wheel_step
        self._grid_adaptive_threshold = grid_adaptive_threshold
        self._on_event_callback = on_event_callback
        self._listen_for_navigation = False
        self._is_view_manipulated = False

        self.view_position = Vector2f(0.0, 0.0)
        self._view_zoom = 1.0
        self.grid_density = grid_density
        self.show_grid = show_grid
        self.background_color = background_color
        self.grid_color = grid_color
        self.grid_origin_color = grid_origin_color
        self.particle_display = particle_display
        self.point_size = point_size
        self.edge_feather = edge_feather
        self.stroke_width = stroke_width
        self._navigation_action = NavigationAction.NONE
        self.quit = False

        self.user_data = UserData()
        if initialize_callback:
            initialize_callback(self)
Exemplo n.º 7
0
 def upper_bounds(self):
     return Vector2f(*numpy.amax(self.particles['position'], axis=0))
Exemplo n.º 8
0
 def lower_bounds(self):
     return Vector2f(*numpy.amin(self.particles['position'], axis=0))