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/_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 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