From c089a7ad0efd54bb844478d093f48c09a7533a68 Mon Sep 17 00:00:00 2001 From: kushalkolar Date: Tue, 20 Dec 2022 03:20:07 -0500 Subject: [PATCH 1/2] added camera auto-scaling, examples work :D --- fastplotlib/layouts/_base.py | 43 +++++++++++++++++++++++++++++++++ fastplotlib/layouts/_subplot.py | 6 ++++- fastplotlib/plot.py | 2 +- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/fastplotlib/layouts/_base.py b/fastplotlib/layouts/_base.py index 68221ad11..903beb625 100644 --- a/fastplotlib/layouts/_base.py +++ b/fastplotlib/layouts/_base.py @@ -1,3 +1,4 @@ +import numpy as np from pygfx import Scene, OrthographicCamera, PerspectiveCamera, PanZoomController, OrbitController, \ Viewport, WgpuRenderer from wgpu.gui.auto import WgpuCanvas @@ -37,6 +38,13 @@ def __init__( self.camera ) + # camera.far and camera.near clipping planes get + # wonky with setting controller.distance = 0 + if isinstance(self.camera, OrthographicCamera): + self.controller.distance = 0 + # also set a initial zoom + self.controller.zoom(0.8 / self.controller.zoom_value) + self.renderer.add_event_handler(self.set_viewport_rect, "resize") self._graphics: List[Graphic] = list() @@ -135,6 +143,15 @@ def center_graphic(self, graphic, zoom: float = 1.3): self.controller.zoom(zoom) def center_scene(self, zoom: float = 1.3): + """ + Auto-center the scene, does not scale. + + Parameters + ---------- + zoom: float, default 1.3 + apply a zoom after centering the scene + + """ if not len(self.scene.children) > 0: return @@ -148,6 +165,32 @@ def center_scene(self, zoom: float = 1.3): self.controller.zoom(zoom) + def auto_scale(self, maintain_aspect: bool = False, zoom: float = 0.8): + """ + Auto-scale the camera w.r.t to the scene + + Parameters + ---------- + maintain_aspect: bool, default ``False`` + maintain the camera aspect ratio for all dimensions, if ``False`` the camera + is scaled according to the bounds in each dimension. + + zoom: float, default 0.8 + zoom value for the camera after auto-scaling, if zoom = 1.0 then the graphics + in the scene will fill the entire canvas. + """ + self.center_scene() + self.camera.maintain_aspect = maintain_aspect + + width, height, depth = np.ptp(self.scene.get_world_bounding_box(), axis=0) + + self.camera.width = width + self.camera.height = height + + # self.controller.distance = 0 + + self.controller.zoom(zoom / self.controller.zoom_value) + def get_graphics(self): return self._graphics diff --git a/fastplotlib/layouts/_subplot.py b/fastplotlib/layouts/_subplot.py index 55fdacff7..4cd6020eb 100644 --- a/fastplotlib/layouts/_subplot.py +++ b/fastplotlib/layouts/_subplot.py @@ -88,8 +88,12 @@ def __init__( self.set_title(self.name) def _create_graphic(self, graphic_class, *args, **kwargs): + if "center" in kwargs.keys(): + center = kwargs.pop("center") + else: + center = False graphic = graphic_class(*args, **kwargs) - self.add_graphic(graphic, center=False) + self.add_graphic(graphic, center=center) return graphic diff --git a/fastplotlib/plot.py b/fastplotlib/plot.py index a5f5a3a44..dc5c3170c 100644 --- a/fastplotlib/plot.py +++ b/fastplotlib/plot.py @@ -91,6 +91,6 @@ def render(self): def show(self): self.canvas.request_draw(self.render) - self.center_scene() + self.auto_scale(maintain_aspect=True, zoom=0.95) return self.canvas From dfdbe72cc46ab9bfb49b1e5bf4d34b2798e52434 Mon Sep 17 00:00:00 2001 From: kushalkolar Date: Tue, 20 Dec 2022 03:30:09 -0500 Subject: [PATCH 2/2] forgot gridplot autoscale in show() --- fastplotlib/layouts/_gridplot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastplotlib/layouts/_gridplot.py b/fastplotlib/layouts/_gridplot.py index 1e322c587..cf030b37e 100644 --- a/fastplotlib/layouts/_gridplot.py +++ b/fastplotlib/layouts/_gridplot.py @@ -175,7 +175,7 @@ def show(self): self.canvas.request_draw(self.render) for subplot in self: - subplot.center_scene() + subplot.auto_scale(maintain_aspect=True, zoom=0.95) return self.canvas pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy