Skip to content

added camera auto-scaling, examples work :D #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions fastplotlib/layouts/_base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import numpy as np
from pygfx import Scene, OrthographicCamera, PerspectiveCamera, PanZoomController, OrbitController, \
Viewport, WgpuRenderer
from wgpu.gui.auto import WgpuCanvas
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion fastplotlib/layouts/_gridplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion fastplotlib/layouts/_subplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion fastplotlib/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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