Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Speed up width/height/depth by reducing copying (#3180)

Kevin Lubick1edcd99250f5
diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.py
index 8e40cc8c6..b9ef39af8 100644
--- a/manim/mobject/mobject.py
+++ b/manim/mobject/mobject.py
@@ -1842,16 +1842,29 @@ class Mobject:
self.become(self.saved_state)
return self
- ##
-
- def reduce_across_dimension(self, points_func, reduce_func, dim):
- points = self.get_all_points()
- if points is None or len(points) == 0:
- # Note, this default means things like empty VGroups
- # will appear to have a center at [0, 0, 0]
+ def reduce_across_dimension(self, reduce_func, dim: int) -> float:
+ """Find the min or max value from a dimension across all points in this and submobjects."""
+ assert dim >= 0 and dim <= 2
+ if len(self.submobjects) == 0 and len(self.points) == 0:
+ # If we have no points and no submobjects, return 0 (e.g. center)
return 0
- values = points_func(points[:, dim])
- return reduce_func(values)
+
+ # If we do not have points (but do have submobjects)
+ # use only the points from those.
+ if len(self.points) == 0:
+ rv = None
+ else:
+ # Otherwise, be sure to include our own points
+ rv = reduce_func(self.points[:, dim])
+ # Recursively ask submobjects (if any) for the biggest/
+ # smallest dimension they have and compare it to the return value.
+ for mobj in self.submobjects:
+ value = mobj.reduce_across_dimension(reduce_func, dim)
+ if rv is None:
+ rv = value
+ else:
+ rv = reduce_func([value, rv])
+ return rv
def nonempty_submobjects(self):
return [
@@ -1860,13 +1873,23 @@ class Mobject:
if len(submob.submobjects) != 0 or len(submob.points) != 0
]
- def get_merged_array(self, array_attr):
+ def get_merged_array(self, array_attr) -> np.ndarray:
+ """Return all of a given attribute from this mobject and all submobjects.
+
+ May contain duplicates; the order is in a depth-first (pre-order)
+ traversal of the submobjects.
+ """
result = getattr(self, array_attr)
for submob in self.submobjects:
result = np.append(result, submob.get_merged_array(array_attr), axis=0)
return result
- def get_all_points(self):
+ def get_all_points(self) -> np.ndarray:
+ """Return all points from this mobject and all submobjects.
+
+ May contain duplicates; the order is in a depth-first (pre-order)
+ traversal of the submobjects.
+ """
return self.get_merged_array("points")
# Getters
@@ -1987,10 +2010,9 @@ class Mobject:
def length_over_dim(self, dim):
"""Measure the length of an :class:`~.Mobject` in a certain direction."""
return self.reduce_across_dimension(
- np.max,
- np.max,
+ max,
dim,
- ) - self.reduce_across_dimension(np.min, np.min, dim)
+ ) - self.reduce_across_dimension(min, dim)
def get_coord(self, dim, direction=ORIGIN):
"""Meant to generalize ``get_x``, ``get_y`` and ``get_z``"""
diff --git a/tests/module/mobject/mobject/test_mobject.py b/tests/module/mobject/mobject/test_mobject.py
index 7ce936c02..bb228eb79 100644
--- a/tests/module/mobject/mobject/test_mobject.py
+++ b/tests/module/mobject/mobject/test_mobject.py
@@ -1,8 +1,9 @@
from __future__ import annotations
+import numpy as np
import pytest
-from manim import Mobject
+from manim import DL, UR, Circle, Mobject, Rectangle, Square, VGroup
def test_mobject_add():
@@ -49,3 +50,110 @@ def test_mobject_remove():
assert len(obj.submobjects) == 10
assert obj.remove(Mobject()) is obj
+
+
+def test_mobject_dimensions_single_mobject():
+ # A Mobject with no points and no submobjects has no dimensions
+ empty = Mobject()
+ assert empty.width == 0
+ assert empty.height == 0
+ assert empty.depth == 0
+
+ has_points = Mobject()
+ has_points.points = np.array([[-1, -2, -3], [1, 3, 5]])
+ assert has_points.width == 2
+ assert has_points.height == 5
+ assert has_points.depth == 8
+
+ rect = Rectangle(width=3, height=5)
+
+ assert rect.width == 3
+ assert rect.height == 5
+ assert rect.depth == 0
+
+ # Dimensions should be recalculated after scaling
+ rect.scale(2.0)
+ assert rect.width == 6
+ assert rect.height == 10
+ assert rect.depth == 0
+
+ # Dimensions should not be dependent on location
+ rect.move_to([-3, -4, -5])
+ assert rect.width == 6
+ assert rect.height == 10
+ assert rect.depth == 0
+
+ circ = Circle(radius=2)
+
+ assert circ.width == 4
+ assert circ.height == 4
+ assert circ.depth == 0
+
+
+def is_close(x, y):
+ return abs(x - y) < 0.00001
+
+
+def test_mobject_dimensions_nested_mobjects():
+ vg = VGroup()
+
+ for x in range(-5, 8, 1):
+ row = VGroup()
+ vg += row
+ for y in range(-17, 2, 1):
+ for z in range(0, 10, 1):
+ s = Square().move_to([x, y, z / 10])
+ row += s
+
+ assert vg.width == 14.0, vg.width
+ assert vg.height == 20.0, vg.height
+ assert is_close(vg.depth, 0.9), vg.depth
+
+ # Dimensions should be recalculated after scaling
+ vg.scale(0.5)
+ assert vg.width == 7.0, vg.width
+ assert vg.height == 10.0, vg.height
+ assert is_close(vg.depth, 0.45), vg.depth
+
+ # Adding a mobject changes the bounds/dimensions
+ rect = Rectangle(width=3, height=5)
+ rect.move_to([9, 3, 1])
+ vg += rect
+ assert vg.width == 13.0, vg.width
+ assert is_close(vg.height, 18.5), vg.height
+ assert is_close(vg.depth, 0.775), vg.depth
+
+
+def test_mobject_dimensions_mobjects_with_no_points_are_at_origin():
+ rect = Rectangle(width=2, height=3)
+ rect.move_to([-4, -5, 0])
+ outer_group = VGroup(rect)
+
+ # This is as one would expect
+ assert outer_group.width == 2
+ assert outer_group.height == 3
+
+ # Adding a mobject with no points has a quirk of adding a "point"
+ # to [0, 0, 0] (the origin). This changes the size of the outer
+ # group because now the bottom left corner is at [-5, -6.5, 0]
+ # but the upper right corner is [0, 0, 0] instead of [-3, -3.5, 0]
+ outer_group.add(VGroup())
+ assert outer_group.width == 5
+ assert outer_group.height == 6.5
+
+
+def test_mobject_dimensions_has_points_and_children():
+ outer_rect = Rectangle(width=3, height=6)
+ inner_rect = Rectangle(width=2, height=1)
+ inner_rect.align_to(outer_rect.get_corner(UR), DL)
+ outer_rect.add(inner_rect)
+
+ # The width of a mobject should depend both on its points and
+ # the points of all children mobjects.
+ assert outer_rect.width == 5 # 3 from outer_rect, 2 from inner_rect
+ assert outer_rect.height == 7 # 6 from outer_rect, 1 from inner_rect
+ assert outer_rect.depth == 0
+
+ assert inner_rect.width == 2
+ assert inner_rect.height == 1
+ assert inner_rect.depth == 0