-
Notifications
You must be signed in to change notification settings - Fork 4
Add a zoom API for CameraController #64
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -202,6 +202,37 @@ class CameraController(EventedBase): | |||||||||
| Camera : Camera class that uses controllers | ||||||||||
| """ | ||||||||||
|
|
||||||||||
| @abstractmethod | ||||||||||
| def zoom( | ||||||||||
| self, | ||||||||||
| camera: Camera, | ||||||||||
| factor: float, | ||||||||||
| center: Position3D | None = None, | ||||||||||
| ) -> None: | ||||||||||
| """Zoom the camera by a given factor. | ||||||||||
|
|
||||||||||
| "Zoom" is not a single well-defined camera operation — it could mean changing | ||||||||||
| the field of view (shrinking how many world units are visible, as in an | ||||||||||
| orthographic projection) or moving the camera physically in relation to a focal | ||||||||||
| point (as in perspective orbit). The correct behavior really depends on the | ||||||||||
| interactive paradigm in place, hence the placement of this method here. | ||||||||||
|
|
||||||||||
| Parameters | ||||||||||
| ---------- | ||||||||||
| camera : Camera | ||||||||||
| The camera to manipulate. | ||||||||||
| factor : float | ||||||||||
| Zoom factor. Values greater than 1 zoom in (fewer world units visible, | ||||||||||
| objects appear larger). Values less than 1 zoom out. A value of | ||||||||||
| 1.0 produces no change. | ||||||||||
| center : Position3D, optional | ||||||||||
| A 3D world-space anchor point that should remain fixed on screen | ||||||||||
| during the zoom. If None, zooms around the camera's current center. | ||||||||||
| Controllers that operate purely in 3D (e.g. Orbit) may ignore this | ||||||||||
| and use their own focal point instead. | ||||||||||
| """ | ||||||||||
| ... | ||||||||||
|
|
||||||||||
| @abstractmethod | ||||||||||
| def handle_event(self, event: Event, camera: Camera) -> bool: | ||||||||||
| """ | ||||||||||
|
|
@@ -330,40 +361,42 @@ def handle_event(self, event: Event, camera: Camera) -> bool: | |||||||||
| # Note that while panning adjusts the camera's transform matrix, zooming | ||||||||||
| # adjusts the projection matrix. | ||||||||||
| elif isinstance(event, WheelEvent): | ||||||||||
| # Zoom while keeping the position under the cursor fixed. | ||||||||||
| _dx, dy = event.angle_delta | ||||||||||
| if dy: | ||||||||||
| # Step 1: Adjust the projection matrix to zoom in or out. | ||||||||||
| zoom = self._zoom_factor(dy) | ||||||||||
| camera.projection = camera.projection.scaled( | ||||||||||
| (1 if self.lock_x else zoom, 1 if self.lock_y else zoom, 1.0) | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| # Step 2: Adjust the transform matrix to maintain the position | ||||||||||
| # under the cursor. The math is largely borrowed from | ||||||||||
| # https://github.com/pygfx/pygfx/blob/520af2d5bb2038ec309ef645e4a60d502f00d181/pygfx/controllers/_panzoom.py#L164 | ||||||||||
|
|
||||||||||
| # Find the distance between the world ray and the camera | ||||||||||
| zoom_center = np.asarray(event.world_ray.origin)[:2] | ||||||||||
| camera_center = np.asarray(camera.transform.map((0, 0)))[:2] | ||||||||||
| # Compute the world distance before the zoom | ||||||||||
| delta_screen1 = zoom_center - camera_center | ||||||||||
| # Compute the world distance after the zoom | ||||||||||
| delta_screen2 = delta_screen1 * zoom | ||||||||||
| # The pan is the difference between the two | ||||||||||
| pan = (delta_screen2 - delta_screen1) / zoom | ||||||||||
| camera.transform = camera.transform.translated( | ||||||||||
| ( | ||||||||||
| pan[0] if not self.lock_x else 0, | ||||||||||
| pan[1] if not self.lock_y else 0, | ||||||||||
| ) | ||||||||||
| ) | ||||||||||
| self.zoom(camera, self._zoom_factor(dy), center=event.world_ray.origin) | ||||||||||
| handled = True | ||||||||||
|
|
||||||||||
| return handled | ||||||||||
|
|
||||||||||
| def zoom( | ||||||||||
| self, | ||||||||||
| camera: Camera, | ||||||||||
| factor: float, | ||||||||||
| center: Position3D | None = None, | ||||||||||
| ) -> None: | ||||||||||
| # Step 1: Scale the projection matrix to zoom in or out. | ||||||||||
| camera.projection = camera.projection.scaled( | ||||||||||
| (1 if self.lock_x else factor, 1 if self.lock_y else factor, 1.0) | ||||||||||
| ) | ||||||||||
| if center is not None: | ||||||||||
| # Step 2: Translate the camera to keep `center` fixed on screen. | ||||||||||
| # Math borrowed from: | ||||||||||
| # https://github.com/pygfx/pygfx/blob/520af2d5bb2038ec309ef645e4a60d502f00d181/pygfx/controllers/_panzoom.py#L164 | ||||||||||
| zoom_center = np.asarray(center)[:2] | ||||||||||
| camera_center = np.asarray(camera.transform.map((0, 0)))[:2] | ||||||||||
| # Compute the world distance before and after the zoom | ||||||||||
| delta_screen1 = zoom_center - camera_center | ||||||||||
| delta_screen2 = delta_screen1 * factor | ||||||||||
| # The pan is the difference between the two | ||||||||||
| pan = (delta_screen2 - delta_screen1) / factor | ||||||||||
| camera.transform = camera.transform.translated( | ||||||||||
| (pan[0] if not self.lock_x else 0, pan[1] if not self.lock_y else 0) | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| def _zoom_factor(self, delta: float) -> float: | ||||||||||
| # Magnifier stolen from pygfx | ||||||||||
| # (one wheel click is typically +/-120, so this results in a zoom factor | ||||||||||
| # of 0.9x or 1.1x per click. Growth is exponential for faster scrolling) | ||||||||||
| return 2 ** (delta * 0.001) | ||||||||||
|
|
||||||||||
|
|
||||||||||
|
|
@@ -562,15 +595,28 @@ def handle_event(self, event: Event, camera: Camera) -> bool: | |||||||||
| elif isinstance(event, WheelEvent): | ||||||||||
| _dx, dy = event.angle_delta | ||||||||||
| if dy: | ||||||||||
| dr = camera.transform.map((0, 0, 0))[:3] - center_array | ||||||||||
| zoom = self._zoom_factor(dy) | ||||||||||
| camera.transform = camera.transform.translated(dr * (zoom - 1)) | ||||||||||
| handled = True | ||||||||||
| # Magnifier stolen from pygfx | ||||||||||
| # (one wheel click is typically +/-120, so this results in a zoom factor | ||||||||||
| # of 0.9x or 1.1x per click. Growth is exponential for faster scrolling) | ||||||||||
| self.zoom(camera, self._zoom_factor(dy)) | ||||||||||
| handled = True | ||||||||||
|
|
||||||||||
| if isinstance(event, MouseEvent): | ||||||||||
| self._last_canvas_pos = event.canvas_pos | ||||||||||
| return handled | ||||||||||
|
|
||||||||||
| def zoom( | ||||||||||
| self, | ||||||||||
| camera: Camera, | ||||||||||
| factor: float, | ||||||||||
| center: Position3D | None = None, | ||||||||||
| ) -> None: | ||||||||||
|
||||||||||
| ) -> None: | |
| ) -> None: | |
| if not math.isfinite(factor) or factor <= 0: | |
| raise ValueError("factor must be a finite number greater than 0") |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -48,6 +48,9 @@ def orthographic(width: float = 1, height: float = 1, depth: float = 1) -> Trans | |||||
| width = width if width else 1e-6 | ||||||
| height = height if height else 1e-6 | ||||||
| depth = depth if depth else 1e-6 | ||||||
| # NOTE: In a right-handned coordinate system, the camera looks down -Z, so we need | ||||||
|
||||||
| # NOTE: In a right-handned coordinate system, the camera looks down -Z, so we need | |
| # NOTE: In a right-handed coordinate system, the camera looks down -Z, so we need |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -73,7 +73,7 @@ def _validate_ray(maybe_ray: Ray | None) -> Ray: | |||||||||||||||||||||||||||||||||||||||||||||||||
| return maybe_ray | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_panzoom_pan() -> None: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_panzoom_mouse() -> None: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| """Tests panning behavior of PanZoom.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| interaction = snx.PanZoom() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cam = snx.Camera(interactive=True, controller=interaction) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -96,7 +96,7 @@ def test_panzoom_pan() -> None: | |||||||||||||||||||||||||||||||||||||||||||||||||
| np.testing.assert_allclose(cam.transform.root, expected.root) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_panzoom_zoom() -> None: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_panzoom_scroll() -> None: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| """Tests zooming behavior of PanZoom.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| interaction = snx.PanZoom() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cam = snx.Camera(interactive=True, controller=interaction) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -115,7 +115,70 @@ def test_panzoom_zoom() -> None: | |||||||||||||||||||||||||||||||||||||||||||||||||
| np.testing.assert_allclose(cam.projection.root, expected.root) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_orbit_orbiting() -> None: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_panzoom_zoom() -> None: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| """Tests zooming via the public zoom() API without a center.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| interaction = snx.PanZoom() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cam = snx.Camera(interactive=True, controller=interaction) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| factor = 0.5 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| before = cam.projection | ||||||||||||||||||||||||||||||||||||||||||||||||||
| interaction.zoom(cam, factor) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| expected = before.scaled((factor, factor, 1)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| np.testing.assert_allclose(cam.projection.root, expected.root) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # No center provided — transform should be unchanged | ||||||||||||||||||||||||||||||||||||||||||||||||||
| np.testing.assert_allclose(cam.transform.root, snx.Transform().root) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_panzoom_zoom_with_center() -> None: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| """Tests that zoom() applies a compensating translation to keep center fixed.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| interaction = snx.PanZoom() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cam = snx.Camera(interactive=True, controller=interaction) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| factor = 0.5 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| center = (0.5, 0.3, 0.0) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| interaction.zoom(cam, factor, center=center) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # Projection should be scaled | ||||||||||||||||||||||||||||||||||||||||||||||||||
| expected_proj = snx.Transform().scaled((factor, factor, -1)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| np.testing.assert_allclose(cam.projection.root, expected_proj.root) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # Transform should have been panned by the compensating amount | ||||||||||||||||||||||||||||||||||||||||||||||||||
| zoom_center = np.array(center[:2]) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| camera_center = np.zeros(2) # camera was at origin before zoom | ||||||||||||||||||||||||||||||||||||||||||||||||||
| delta_screen1 = zoom_center - camera_center | ||||||||||||||||||||||||||||||||||||||||||||||||||
| delta_screen2 = delta_screen1 * factor | ||||||||||||||||||||||||||||||||||||||||||||||||||
| pan = (delta_screen2 - delta_screen1) / factor | ||||||||||||||||||||||||||||||||||||||||||||||||||
| expected_transform = snx.Transform().translated((pan[0], pan[1])) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+137
to
+147
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| interaction.zoom(cam, factor, center=center) | |
| # Projection should be scaled | |
| expected_proj = snx.Transform().scaled((factor, factor, -1)) | |
| np.testing.assert_allclose(cam.projection.root, expected_proj.root) | |
| # Transform should have been panned by the compensating amount | |
| zoom_center = np.array(center[:2]) | |
| camera_center = np.zeros(2) # camera was at origin before zoom | |
| delta_screen1 = zoom_center - camera_center | |
| delta_screen2 = delta_screen1 * factor | |
| pan = (delta_screen2 - delta_screen1) / factor | |
| expected_transform = snx.Transform().translated((pan[0], pan[1])) | |
| before_proj = cam.projection | |
| before_transform = cam.transform | |
| interaction.zoom(cam, factor, center=center) | |
| # Projection should be scaled relative to the camera's prior projection | |
| expected_proj = before_proj.scaled((factor, factor, 1)) | |
| np.testing.assert_allclose(cam.projection.root, expected_proj.root) | |
| # Transform should have been panned by the compensating amount | |
| zoom_center = np.array(center[:2]) | |
| camera_center = before_transform.root[3, :2] | |
| delta_screen1 = zoom_center - camera_center | |
| delta_screen2 = delta_screen1 * factor | |
| pan = (delta_screen2 - delta_screen1) / factor | |
| expected_transform = before_transform.translated((pan[0], pan[1])) |
Copilot
AI
Apr 13, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These assignments create unused variables (zoom and desired_tform). Ruff enables F rules for tests, so this will fail linting. Remove the unused variables or use them in the assertion.
| zoom = interaction._zoom_factor(delta) | |
| desired_tform = snx.Transform().translated((0, 0, starting_dist)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In
PanZoom.zoom(),factoris part of the public API but it is used as a divisor when computingpan(and as a scale multiplier). Calling this withfactor <= 0will either raise (division by zero) or behave nonsensically (negative scaling). Consider validatingfactor(e.g., raiseValueErrorwhenfactor <= 0or non-finite) early in the method.