Skip to content
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

Typing graphing and camera #4125

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repos:
- id: check-toml
name: Validate Poetry
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.9.1
rev: v0.9.2
hooks:
- id: ruff
name: ruff lint
Expand Down
199 changes: 115 additions & 84 deletions manim/camera/camera.py

Large diffs are not rendered by default.

17 changes: 10 additions & 7 deletions manim/camera/moving_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@

__all__ = ["MovingCamera"]

from collections.abc import Iterable
from typing import Any

import numpy as np

from .. import config
from ..camera.camera import Camera

Check failure

Code scanning / CodeQL

Module-level cyclic import Error

'Camera' may not be defined if module
manim.camera.camera
is imported before module
manim.camera.moving_camera
, as the
definition
of Camera occurs after the cyclic
import
of manim.camera.moving_camera.
from ..constants import DOWN, LEFT, RIGHT, UP
from ..mobject.frame import ScreenRectangle
from ..mobject.mobject import Mobject
from ..utils.color import WHITE
from ..utils.color import WHITE, ManimColor


class MovingCamera(Camera):
Expand All @@ -33,11 +36,11 @@
def __init__(
self,
frame=None,
fixed_dimension=0, # width
default_frame_stroke_color=WHITE,
default_frame_stroke_width=0,
**kwargs,
):
fixed_dimension: int = 0, # width
default_frame_stroke_color: ManimColor = WHITE,
default_frame_stroke_width: int = 0,
**kwargs: Any,
) -> None:
"""
Frame is a Mobject, (should almost certainly be a rectangle)
determining which region of space the camera displays
Expand Down Expand Up @@ -123,7 +126,7 @@
"""
self.frame.move_to(frame_center)

def capture_mobjects(self, mobjects, **kwargs):
def capture_mobjects(self, mobjects: Iterable[Mobject], **kwargs: Any) -> None:
# self.reset_frame_center()
# self.realign_frame_shape()
super().capture_mobjects(mobjects, **kwargs)
Expand Down
34 changes: 22 additions & 12 deletions manim/camera/multi_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
__all__ = ["MultiCamera"]


from manim.mobject.types.image_mobject import ImageMobject
from collections.abc import Iterable
from typing import Any

from typing_extensions import Self

from manim.mobject.mobject import Mobject
from manim.mobject.types.image_mobject import ImageMobjectFromCamera

from ..camera.moving_camera import MovingCamera
from ..utils.iterables import list_difference_update
Expand All @@ -16,10 +22,10 @@ class MultiCamera(MovingCamera):

def __init__(
self,
image_mobjects_from_cameras: ImageMobject | None = None,
allow_cameras_to_capture_their_own_display=False,
**kwargs,
):
image_mobjects_from_cameras: Iterable[ImageMobjectFromCamera] | None = None,
allow_cameras_to_capture_their_own_display: bool = False,
**kwargs: Any,
) -> None:
"""Initialises the MultiCamera

Parameters
Expand All @@ -29,7 +35,7 @@ def __init__(
kwargs
Any valid keyword arguments of MovingCamera.
"""
self.image_mobjects_from_cameras = []
self.image_mobjects_from_cameras: list[ImageMobjectFromCamera] = []
if image_mobjects_from_cameras is not None:
for imfc in image_mobjects_from_cameras:
self.add_image_mobject_from_camera(imfc)
Expand All @@ -38,7 +44,9 @@ def __init__(
)
super().__init__(**kwargs)

def add_image_mobject_from_camera(self, image_mobject_from_camera: ImageMobject):
def add_image_mobject_from_camera(
self, image_mobject_from_camera: ImageMobjectFromCamera
) -> None:
"""Adds an ImageMobject that's been obtained from the camera
into the list ``self.image_mobject_from_cameras``

Expand All @@ -53,11 +61,13 @@ def add_image_mobject_from_camera(self, image_mobject_from_camera: ImageMobject)
assert isinstance(imfc.camera, MovingCamera)
self.image_mobjects_from_cameras.append(imfc)

def update_sub_cameras(self):
def update_sub_cameras(self) -> None:
"""Reshape sub_camera pixel_arrays"""
for imfc in self.image_mobjects_from_cameras:
pixel_height, pixel_width = self.pixel_array.shape[:2]
imfc.camera.frame_shape = (
# TODO:
# error: "MovingCamera" has no attribute "frame_shape" [attr-defined]
imfc.camera.frame_shape = ( # type: ignore[attr-defined]
imfc.camera.frame.height,
imfc.camera.frame.width,
)
Expand All @@ -66,7 +76,7 @@ def update_sub_cameras(self):
int(pixel_width * imfc.width / self.frame_width),
)

def reset(self):
def reset(self) -> Self:
"""Resets the MultiCamera.

Returns
Expand All @@ -79,7 +89,7 @@ def reset(self):
super().reset()
return self

def capture_mobjects(self, mobjects, **kwargs):
def capture_mobjects(self, mobjects: Iterable[Mobject], **kwargs: Any) -> None:
self.update_sub_cameras()
for imfc in self.image_mobjects_from_cameras:
to_add = list(mobjects)
Expand All @@ -88,7 +98,7 @@ def capture_mobjects(self, mobjects, **kwargs):
imfc.camera.capture_mobjects(to_add, **kwargs)
super().capture_mobjects(mobjects, **kwargs)

def get_mobjects_indicating_movement(self):
def get_mobjects_indicating_movement(self) -> list[Mobject]:
"""Returns all mobjects whose movement implies that the camera
should think of all other mobjects on the screen as moving

Expand Down
Loading
Loading