Repository: ManimCommunity/manim
File: manim/renderer/vectorized_mobject_rendering.py (Python)

Code before:

import collections
from typing import TYPE_CHECKING, Any

import numpy as np

if TYPE_CHECKING:
    from manim.renderer.opengl_renderer import (
        OpenGLRenderer,
        OpenGLVMobject,
    )
    from manim.typing import MatrixMN

from ..utils import opengl
from ..utils.space_ops import cross2d, earclip_triangulation
from .shader import Shader

__all__ = [
    "render_opengl_vectorized_mobject_fill",
    "render_opengl_vectorized_mobject_stroke",
]


def build_matrix_lists(
    mob: OpenGLVMobject,
) -> collections.defaultdict[Any, list[OpenGLVMobject]]:
    root_hierarchical_matrix = mob.hierarchical_model_matrix()
    matrix_to_mobject_list = collections.defaultdict(list)
    if mob.has_points():
        matrix_to_mobject_list[tuple(root_hierarchical_matrix.ravel())].append(mob)
    mobject_to_hierarchical_matrix = {mob: root_hierarchical_matrix}
    dfs = [mob]
    while dfs:
        parent = dfs.pop()
        for child in parent.submobjects:
            child_hierarchical_matrix = (
                mobject_to_hierarchical_matrix[parent] @ child.model_matrix
            )
            mobject_to_hierarchical_matrix[child] = child_hierarchical_matrix
            if child.has_points():
                matrix_to_mobject_list[tuple(child_hierarchical_matrix.ravel())].append(
                    child,
                )
            dfs.append(child)
    return matrix_to_mobject_list


def render_opengl_vectorized_mobject_fill(
    renderer: OpenGLRenderer, mobject: OpenGLVMobject
) -> None:
    matrix_to_mobject_list = build_matrix_lists(mobject)

Review comment (suggestion):
The keys of this defaultdict are tuples of floats:
```suggestion
) -> collections.defaultdict[tuple[float, ...], list[OpenGLVMobject]]:
```

Code after:

import collections
from collections.abc import Iterable, Sequence
from typing import TYPE_CHECKING

import numpy as np

if TYPE_CHECKING:
    from manim.renderer.opengl_renderer import (
        OpenGLRenderer,
        OpenGLVMobject,
    )
    from manim.typing import MatrixMN

from ..utils import opengl
from ..utils.space_ops import cross2d, earclip_triangulation
from .shader import Shader

__all__ = [
    "render_opengl_vectorized_mobject_fill",
    "render_opengl_vectorized_mobject_stroke",
]


def build_matrix_lists(
    mob: OpenGLVMobject,
) -> collections.defaultdict[tuple[float, ...], list[OpenGLVMobject]]:
    root_hierarchical_matrix = mob.hierarchical_model_matrix()
    matrix_to_mobject_list = collections.defaultdict(list)
    if mob.has_points():
        matrix_to_mobject_list[tuple(root_hierarchical_matrix.ravel())].append(mob)
    mobject_to_hierarchical_matrix = {mob: root_hierarchical_matrix}
    dfs = [mob]
    while dfs:
        parent = dfs.pop()
        for child in parent.submobjects:
            child_hierarchical_matrix = (
                mobject_to_hierarchical_matrix[parent] @ child.model_matrix
            )
            mobject_to_hierarchical_matrix[child] = child_hierarchical_matrix
            if child.has_points():
                matrix_to_mobject_list[tuple(child_hierarchical_matrix.ravel())].append(
                    child,
                )
            dfs.append(child)
    return matrix_to_mobject_list


def render_opengl_vectorized_mobject_fill(
    renderer: OpenGLRenderer, mobject: OpenGLVMobject
) -> None: