Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Fixed opengl family duplicates issue and add tests (#2142)

Ryan McCauley2d98af0b2229
diff --git a/manim/mobject/opengl_mobject.py b/manim/mobject/opengl_mobject.py
index f8d4462ab..1e61549e2 100644
--- a/manim/mobject/opengl_mobject.py
+++ b/manim/mobject/opengl_mobject.py
@@ -26,6 +26,7 @@ from ..utils.iterables import (
resize_array,
resize_preserving_order,
resize_with_interpolation,
+ uniq_chain,
)
from ..utils.paths import straight_path
from ..utils.simple_functions import get_parameters
@@ -412,7 +413,7 @@ class OpenGLMobject:
def assemble_family(self):
sub_families = (sm.get_family() for sm in self.submobjects)
- self.family = [self, *it.chain(*sub_families)]
+ self.family = [self, *uniq_chain(*sub_families)]
self.refresh_has_updater_status()
self.refresh_bounding_box()
for parent in self.parents:
diff --git a/manim/utils/iterables.py b/manim/utils/iterables.py
index 716728714..a63ae79b1 100644
--- a/manim/utils/iterables.py
+++ b/manim/utils/iterables.py
@@ -171,3 +171,12 @@ def remove_nones(sequence):
def concatenate_lists(*list_of_lists):
return [item for lst in list_of_lists for item in lst]
+
+
+def uniq_chain(*args):
+ unique_items = set()
+ for x in it.chain(*args):
+ if x in unique_items:
+ continue
+ unique_items.add(x)
+ yield x
diff --git a/tests/opengl/test_family_opengl.py b/tests/opengl/test_family_opengl.py
new file mode 100644
index 000000000..3d1fea33b
--- /dev/null
+++ b/tests/opengl/test_family_opengl.py
@@ -0,0 +1,83 @@
+import numpy as np
+
+from manim import RIGHT, Circle
+from manim.mobject.opengl_mobject import OpenGLMobject
+
+
+def test_family(using_opengl_renderer):
+ """Check that the family is gathered correctly."""
+ # Check that an empty OpenGLMobject's family only contains itself
+ mob = OpenGLMobject()
+ assert mob.get_family() == [mob]
+
+ # Check that all children are in the family
+ mob = OpenGLMobject()
+ children = [OpenGLMobject() for _ in range(10)]
+ mob.add(*children)
+ family = mob.get_family()
+ assert len(family) == 1 + 10
+ assert mob in family
+ for c in children:
+ assert c in family
+
+ # Nested children should be in the family
+ mob = OpenGLMobject()
+ grandchildren = {}
+ for _ in range(10):
+ child = OpenGLMobject()
+ grandchildren[child] = [OpenGLMobject() for _ in range(10)]
+ child.add(*grandchildren[child])
+ mob.add(*list(grandchildren.keys()))
+ family = mob.get_family()
+ assert len(family) == 1 + 10 + 10 * 10
+ assert mob in family
+ for c in grandchildren:
+ assert c in family
+ for gc in grandchildren[c]:
+ assert gc in family
+
+
+def test_overlapping_family(using_opengl_renderer):
+ """Check that each member of the family is only gathered once."""
+ mob, child1, child2, = (
+ OpenGLMobject(),
+ OpenGLMobject(),
+ OpenGLMobject(),
+ )
+ gchild1, gchild2, gchild_common = OpenGLMobject(), OpenGLMobject(), OpenGLMobject()
+ child1.add(gchild1, gchild_common)
+ child2.add(gchild2, gchild_common)
+ mob.add(child1, child2)
+ family = mob.get_family()
+ assert mob in family
+ assert len(family) == 6
+ assert family.count(gchild_common) == 1
+
+
+def test_shift_family(using_opengl_renderer):
+ """Check that each member of the family is shifted along with the parent.
+
+ Importantly, here we add a common grandchild to each of the children. So
+ this test will fail if the grandchild moves twice as much as it should.
+
+ """
+ # Note shift() needs the OpenGLMobject to have a non-empty `points` attribute, so
+ # we cannot use a plain OpenGLMobject or OpenGLVMobject. We use Circle instead.
+ mob, child1, child2, = (
+ Circle(),
+ Circle(),
+ Circle(),
+ )
+ gchild1, gchild2, gchild_common = Circle(), Circle(), Circle()
+
+ child1.add(gchild1, gchild_common)
+ child2.add(gchild2, gchild_common)
+ mob.add(child1, child2)
+ family = mob.get_family()
+
+ positions_before = {m: m.get_center().copy() for m in family}
+ mob.shift(RIGHT)
+ positions_after = {m: m.get_center().copy() for m in family}
+
+ for m in family:
+ assert np.allclose(positions_before[m] + RIGHT, positions_after[m])