Skip to content

Commit d18dc8f

Browse files
Add VectorNDLike type aliases (#4068)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 7eb8856 commit d18dc8f

File tree

15 files changed

+317
-229
lines changed

15 files changed

+317
-229
lines changed

docs/source/contributing/docs/types.rst

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,8 @@ typed as a :class:`~.Point3D`, because it represents a direction along
8585
which to shift a :class:`~.Mobject`, not a position in space.
8686

8787
As a general rule, if a parameter is called ``direction`` or ``axis``,
88-
it should be type hinted as some form of :class:`~.VectorND`.
89-
90-
.. warning::
91-
92-
This is not always true. For example, as of Manim 0.18.0, the direction
93-
parameter of the :class:`.Vector` Mobject should be
94-
``Point2DLike | Point3DLike``, as it can also accept ``tuple[float, float]``
95-
and ``tuple[float, float, float]``.
88+
it should be type hinted as some form of :class:`~.VectorND` or
89+
:class:`~.VectorNDLike`.
9690

9791
Colors
9892
------

manim/mobject/geometry/arc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def construct(self):
7474
Point3D,
7575
Point3DLike,
7676
QuadraticSpline,
77-
Vector3D,
77+
Vector3DLike,
7878
)
7979

8080

@@ -99,12 +99,12 @@ class TipableVMobject(VMobject, metaclass=ConvertToOpenGL):
9999
def __init__(
100100
self,
101101
tip_length: float = DEFAULT_ARROW_TIP_LENGTH,
102-
normal_vector: Vector3D = OUT,
102+
normal_vector: Vector3DLike = OUT,
103103
tip_style: dict = {},
104104
**kwargs: Any,
105105
) -> None:
106106
self.tip_length: float = tip_length
107-
self.normal_vector: Vector3D = normal_vector
107+
self.normal_vector = normal_vector
108108
self.tip_style: dict = tip_style
109109
super().__init__(**kwargs)
110110

manim/mobject/geometry/line.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
from typing_extensions import Literal, Self, TypeAlias
3636

37-
from manim.typing import Point2DLike, Point3D, Point3DLike, Vector3D
37+
from manim.typing import Point3D, Point3DLike, Vector2DLike, Vector3D, Vector3DLike
3838
from manim.utils.color import ParsableManimColor
3939

4040
from ..matrix import Matrix # Avoid circular import
@@ -175,7 +175,7 @@ def _set_start_and_end_attrs(
175175
def _pointify(
176176
self,
177177
mob_or_point: Mobject | Point3DLike,
178-
direction: Vector3D | None = None,
178+
direction: Vector3DLike | None = None,
179179
) -> Point3D:
180180
"""Transforms a mobject into its corresponding point. Does nothing if a point is passed.
181181
@@ -738,7 +738,7 @@ def construct(self):
738738

739739
def __init__(
740740
self,
741-
direction: Point2DLike | Point3DLike = RIGHT,
741+
direction: Vector2DLike | Vector3DLike = RIGHT,
742742
buff: float = 0,
743743
**kwargs: Any,
744744
) -> None:

manim/mobject/graphing/coordinate_systems.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
Point3D,
6565
Point3DLike,
6666
Vector3D,
67+
Vector3DLike,
6768
)
6869

6970
LineType = TypeVar("LineType", bound=Line)
@@ -353,8 +354,8 @@ def _get_axis_label(
353354
self,
354355
label: float | str | VMobject,
355356
axis: Mobject,
356-
edge: Vector3D,
357-
direction: Vector3D,
357+
edge: Vector3DLike,
358+
direction: Vector3DLike,
358359
buff: float = SMALL_BUFF,
359360
) -> Mobject:
360361
"""Gets the label for an axis.
@@ -2432,7 +2433,7 @@ def __init__(
24322433
y_length: float | None = config.frame_height + 2.5,
24332434
z_length: float | None = config.frame_height - 1.5,
24342435
z_axis_config: dict[str, Any] | None = None,
2435-
z_normal: Vector3D = DOWN,
2436+
z_normal: Vector3DLike = DOWN,
24362437
num_axis_pieces: int = 20,
24372438
light_source: Point3DLike = 9 * DOWN + 7 * LEFT + 10 * OUT,
24382439
# opengl stuff (?)
@@ -2519,11 +2520,11 @@ def make_func(axis):
25192520
def get_y_axis_label(
25202521
self,
25212522
label: float | str | VMobject,
2522-
edge: Vector3D = UR,
2523-
direction: Vector3D = UR,
2523+
edge: Vector3DLike = UR,
2524+
direction: Vector3DLike = UR,
25242525
buff: float = SMALL_BUFF,
25252526
rotation: float = PI / 2,
2526-
rotation_axis: Vector3D = OUT,
2527+
rotation_axis: Vector3DLike = OUT,
25272528
**kwargs: dict[str, Any],
25282529
) -> Mobject:
25292530
"""Generate a y-axis label.
@@ -2569,11 +2570,11 @@ def construct(self):
25692570
def get_z_axis_label(
25702571
self,
25712572
label: float | str | VMobject,
2572-
edge: Vector3D = OUT,
2573-
direction: Vector3D = RIGHT,
2573+
edge: Vector3DLike = OUT,
2574+
direction: Vector3DLike = RIGHT,
25742575
buff: float = SMALL_BUFF,
25752576
rotation: float = PI / 2,
2576-
rotation_axis: Vector3D = RIGHT,
2577+
rotation_axis: Vector3DLike = RIGHT,
25772578
**kwargs: Any,
25782579
) -> Mobject:
25792580
"""Generate a z-axis label.

manim/mobject/mobject.py

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
Point3D,
5555
Point3DLike,
5656
Point3DLike_Array,
57-
Vector3D,
57+
Vector3DLike,
5858
)
5959

6060
from ..animation.animation import Animation
@@ -1204,7 +1204,7 @@ def apply_to_family(self, func: Callable[[Mobject], None]) -> None:
12041204
for mob in self.family_members_with_points():
12051205
func(mob)
12061206

1207-
def shift(self, *vectors: Vector3D) -> Self:
1207+
def shift(self, *vectors: Vector3DLike) -> Self:
12081208
"""Shift by the given vectors.
12091209
12101210
Parameters
@@ -1275,14 +1275,14 @@ def construct(self):
12751275
)
12761276
return self
12771277

1278-
def rotate_about_origin(self, angle: float, axis: Vector3D = OUT, axes=[]) -> Self:
1278+
def rotate_about_origin(self, angle: float, axis: Vector3DLike = OUT) -> Self:
12791279
"""Rotates the :class:`~.Mobject` about the ORIGIN, which is at [0,0,0]."""
12801280
return self.rotate(angle, axis, about_point=ORIGIN)
12811281

12821282
def rotate(
12831283
self,
12841284
angle: float,
1285-
axis: Vector3D = OUT,
1285+
axis: Vector3DLike = OUT,
12861286
about_point: Point3DLike | None = None,
12871287
**kwargs,
12881288
) -> Self:
@@ -1350,7 +1350,7 @@ def construct(self):
13501350
)
13511351
return self
13521352

1353-
def flip(self, axis: Vector3D = UP, **kwargs) -> Self:
1353+
def flip(self, axis: Vector3DLike = UP, **kwargs) -> Self:
13541354
"""Flips/Mirrors an mobject about its center.
13551355
13561356
Examples
@@ -1470,7 +1470,7 @@ def apply_points_function_about_point(
14701470
self,
14711471
func: MultiMappingFunction,
14721472
about_point: Point3DLike | None = None,
1473-
about_edge: Vector3D | None = None,
1473+
about_edge: Vector3DLike | None = None,
14741474
) -> Self:
14751475
if about_point is None:
14761476
if about_edge is None:
@@ -1500,7 +1500,7 @@ def center(self) -> Self:
15001500
return self
15011501

15021502
def align_on_border(
1503-
self, direction: Vector3D, buff: float = DEFAULT_MOBJECT_TO_EDGE_BUFFER
1503+
self, direction: Vector3DLike, buff: float = DEFAULT_MOBJECT_TO_EDGE_BUFFER
15041504
) -> Self:
15051505
"""Direction just needs to be a vector pointing towards side or
15061506
corner in the 2d plane.
@@ -1517,7 +1517,7 @@ def align_on_border(
15171517
return self
15181518

15191519
def to_corner(
1520-
self, corner: Vector3D = DL, buff: float = DEFAULT_MOBJECT_TO_EDGE_BUFFER
1520+
self, corner: Vector3DLike = DL, buff: float = DEFAULT_MOBJECT_TO_EDGE_BUFFER
15211521
) -> Self:
15221522
"""Moves this :class:`~.Mobject` to the given corner of the screen.
15231523
@@ -1545,7 +1545,7 @@ def construct(self):
15451545
return self.align_on_border(corner, buff)
15461546

15471547
def to_edge(
1548-
self, edge: Vector3D = LEFT, buff: float = DEFAULT_MOBJECT_TO_EDGE_BUFFER
1548+
self, edge: Vector3DLike = LEFT, buff: float = DEFAULT_MOBJECT_TO_EDGE_BUFFER
15491549
) -> Self:
15501550
"""Moves this :class:`~.Mobject` to the given edge of the screen,
15511551
without affecting its position in the other dimension.
@@ -1577,12 +1577,12 @@ def construct(self):
15771577
def next_to(
15781578
self,
15791579
mobject_or_point: Mobject | Point3DLike,
1580-
direction: Vector3D = RIGHT,
1580+
direction: Vector3DLike = RIGHT,
15811581
buff: float = DEFAULT_MOBJECT_TO_MOBJECT_BUFFER,
1582-
aligned_edge: Vector3D = ORIGIN,
1582+
aligned_edge: Vector3DLike = ORIGIN,
15831583
submobject_to_align: Mobject | None = None,
15841584
index_of_submobject_to_align: int | None = None,
1585-
coor_mask: Vector3D = np.array([1, 1, 1]),
1585+
coor_mask: Vector3DLike = np.array([1, 1, 1]),
15861586
) -> Self:
15871587
"""Move this :class:`~.Mobject` next to another's :class:`~.Mobject` or Point3D.
15881588
@@ -1604,13 +1604,18 @@ def construct(self):
16041604
self.add(d, c, s, t)
16051605
16061606
"""
1607+
np_direction = np.asarray(direction)
1608+
np_aligned_edge = np.asarray(aligned_edge)
1609+
16071610
if isinstance(mobject_or_point, Mobject):
16081611
mob = mobject_or_point
16091612
if index_of_submobject_to_align is not None:
16101613
target_aligner = mob[index_of_submobject_to_align]
16111614
else:
16121615
target_aligner = mob
1613-
target_point = target_aligner.get_critical_point(aligned_edge + direction)
1616+
target_point = target_aligner.get_critical_point(
1617+
np_aligned_edge + np_direction
1618+
)
16141619
else:
16151620
target_point = mobject_or_point
16161621
if submobject_to_align is not None:
@@ -1619,8 +1624,8 @@ def construct(self):
16191624
aligner = self[index_of_submobject_to_align]
16201625
else:
16211626
aligner = self
1622-
point_to_align = aligner.get_critical_point(aligned_edge - direction)
1623-
self.shift((target_point - point_to_align + buff * direction) * coor_mask)
1627+
point_to_align = aligner.get_critical_point(np_aligned_edge - np_direction)
1628+
self.shift((target_point - point_to_align + buff * np_direction) * coor_mask)
16241629
return self
16251630

16261631
def shift_onto_screen(self, **kwargs) -> Self:
@@ -1766,22 +1771,22 @@ def stretch_to_fit_depth(self, depth: float, **kwargs) -> Self:
17661771
"""Stretches the :class:`~.Mobject` to fit a depth, not keeping width/height proportional."""
17671772
return self.rescale_to_fit(depth, 2, stretch=True, **kwargs)
17681773

1769-
def set_coord(self, value, dim: int, direction: Vector3D = ORIGIN) -> Self:
1774+
def set_coord(self, value, dim: int, direction: Vector3DLike = ORIGIN) -> Self:
17701775
curr = self.get_coord(dim, direction)
17711776
shift_vect = np.zeros(self.dim)
17721777
shift_vect[dim] = value - curr
17731778
self.shift(shift_vect)
17741779
return self
17751780

1776-
def set_x(self, x: float, direction: Vector3D = ORIGIN) -> Self:
1781+
def set_x(self, x: float, direction: Vector3DLike = ORIGIN) -> Self:
17771782
"""Set x value of the center of the :class:`~.Mobject` (``int`` or ``float``)"""
17781783
return self.set_coord(x, 0, direction)
17791784

1780-
def set_y(self, y: float, direction: Vector3D = ORIGIN) -> Self:
1785+
def set_y(self, y: float, direction: Vector3DLike = ORIGIN) -> Self:
17811786
"""Set y value of the center of the :class:`~.Mobject` (``int`` or ``float``)"""
17821787
return self.set_coord(y, 1, direction)
17831788

1784-
def set_z(self, z: float, direction: Vector3D = ORIGIN) -> Self:
1789+
def set_z(self, z: float, direction: Vector3DLike = ORIGIN) -> Self:
17851790
"""Set z value of the center of the :class:`~.Mobject` (``int`` or ``float``)"""
17861791
return self.set_coord(z, 2, direction)
17871792

@@ -1794,8 +1799,8 @@ def space_out_submobjects(self, factor: float = 1.5, **kwargs) -> Self:
17941799
def move_to(
17951800
self,
17961801
point_or_mobject: Point3DLike | Mobject,
1797-
aligned_edge: Vector3D = ORIGIN,
1798-
coor_mask: Vector3D = np.array([1, 1, 1]),
1802+
aligned_edge: Vector3DLike = ORIGIN,
1803+
coor_mask: Vector3DLike = np.array([1, 1, 1]),
17991804
) -> Self:
18001805
"""Move center of the :class:`~.Mobject` to certain Point3D."""
18011806
if isinstance(point_or_mobject, Mobject):
@@ -2114,7 +2119,7 @@ def get_extremum_along_dim(
21142119
else:
21152120
return np.max(values)
21162121

2117-
def get_critical_point(self, direction: Vector3D) -> Point3D:
2122+
def get_critical_point(self, direction: Vector3DLike) -> Point3D:
21182123
"""Picture a box bounding the :class:`~.Mobject`. Such a box has
21192124
9 'critical points': 4 corners, 4 edge center, the
21202125
center. This returns one of them, along the given direction.
@@ -2143,11 +2148,11 @@ def get_critical_point(self, direction: Vector3D) -> Point3D:
21432148

21442149
# Pseudonyms for more general get_critical_point method
21452150

2146-
def get_edge_center(self, direction: Vector3D) -> Point3D:
2151+
def get_edge_center(self, direction: Vector3DLike) -> Point3D:
21472152
"""Get edge Point3Ds for certain direction."""
21482153
return self.get_critical_point(direction)
21492154

2150-
def get_corner(self, direction: Vector3D) -> Point3D:
2155+
def get_corner(self, direction: Vector3DLike) -> Point3D:
21512156
"""Get corner Point3Ds for certain direction."""
21522157
return self.get_critical_point(direction)
21532158

@@ -2158,9 +2163,9 @@ def get_center(self) -> Point3D:
21582163
def get_center_of_mass(self) -> Point3D:
21592164
return np.apply_along_axis(np.mean, 0, self.get_all_points())
21602165

2161-
def get_boundary_point(self, direction: Vector3D) -> Point3D:
2166+
def get_boundary_point(self, direction: Vector3DLike) -> Point3D:
21622167
all_points = self.get_points_defining_boundary()
2163-
index = np.argmax(np.dot(all_points, np.array(direction).T))
2168+
index = np.argmax(np.dot(all_points, direction))
21642169
return all_points[index]
21652170

21662171
def get_midpoint(self) -> Point3D:
@@ -2217,19 +2222,19 @@ def length_over_dim(self, dim: int) -> float:
22172222
dim,
22182223
) - self.reduce_across_dimension(min, dim)
22192224

2220-
def get_coord(self, dim: int, direction: Vector3D = ORIGIN):
2225+
def get_coord(self, dim: int, direction: Vector3DLike = ORIGIN) -> float:
22212226
"""Meant to generalize ``get_x``, ``get_y`` and ``get_z``"""
22222227
return self.get_extremum_along_dim(dim=dim, key=direction[dim])
22232228

2224-
def get_x(self, direction: Vector3D = ORIGIN) -> float:
2229+
def get_x(self, direction: Vector3DLike = ORIGIN) -> float:
22252230
"""Returns x Point3D of the center of the :class:`~.Mobject` as ``float``"""
22262231
return self.get_coord(0, direction)
22272232

2228-
def get_y(self, direction: Vector3D = ORIGIN) -> float:
2233+
def get_y(self, direction: Vector3DLike = ORIGIN) -> float:
22292234
"""Returns y Point3D of the center of the :class:`~.Mobject` as ``float``"""
22302235
return self.get_coord(1, direction)
22312236

2232-
def get_z(self, direction: Vector3D = ORIGIN) -> float:
2237+
def get_z(self, direction: Vector3DLike = ORIGIN) -> float:
22332238
"""Returns z Point3D of the center of the :class:`~.Mobject` as ``float``"""
22342239
return self.get_coord(2, direction)
22352240

@@ -2300,7 +2305,7 @@ def match_depth(self, mobject: Mobject, **kwargs) -> Self:
23002305
return self.match_dim_size(mobject, 2, **kwargs)
23012306

23022307
def match_coord(
2303-
self, mobject: Mobject, dim: int, direction: Vector3D = ORIGIN
2308+
self, mobject: Mobject, dim: int, direction: Vector3DLike = ORIGIN
23042309
) -> Self:
23052310
"""Match the Point3Ds with the Point3Ds of another :class:`~.Mobject`."""
23062311
return self.set_coord(
@@ -2324,7 +2329,7 @@ def match_z(self, mobject: Mobject, direction=ORIGIN) -> Self:
23242329
def align_to(
23252330
self,
23262331
mobject_or_point: Mobject | Point3DLike,
2327-
direction: Vector3D = ORIGIN,
2332+
direction: Vector3DLike = ORIGIN,
23282333
) -> Self:
23292334
"""Aligns mobject to another :class:`~.Mobject` in a certain direction.
23302335
@@ -2431,7 +2436,7 @@ def family_members_with_points(self) -> list[Self]:
24312436

24322437
def arrange(
24332438
self,
2434-
direction: Vector3D = RIGHT,
2439+
direction: Vector3DLike = RIGHT,
24352440
buff: float = DEFAULT_MOBJECT_TO_MOBJECT_BUFFER,
24362441
center: bool = True,
24372442
**kwargs,
@@ -2464,7 +2469,7 @@ def arrange_in_grid(
24642469
rows: int | None = None,
24652470
cols: int | None = None,
24662471
buff: float | tuple[float, float] = MED_SMALL_BUFF,
2467-
cell_alignment: Vector3D = ORIGIN,
2472+
cell_alignment: Vector3DLike = ORIGIN,
24682473
row_alignments: str | None = None, # "ucd"
24692474
col_alignments: str | None = None, # "lcr"
24702475
row_heights: Iterable[float | None] | None = None,

0 commit comments

Comments
 (0)