Changed how align_submojects works, partly to be simply, partly to shift to using null mobjects instead of ocpies

This commit is contained in:
Grant Sanderson 2019-02-11 20:51:57 -08:00
parent 390e774160
commit da134f7c0b

View file

@ -939,7 +939,8 @@ class Mobject(Container):
The simplest mobject to be transformed to or from self. The simplest mobject to be transformed to or from self.
Should by a point of the appropriate type Should by a point of the appropriate type
""" """
raise Exception("Not implemented") message = "get_point_mobject not implemented for {}"
raise Exception(message.format(self.__class__.__name__))
def align_points(self, mobject): def align_points(self, mobject):
count1 = self.get_num_points() count1 = self.get_num_points()
@ -954,23 +955,12 @@ class Mobject(Container):
raise Exception("Not implemented") raise Exception("Not implemented")
def align_submobjects(self, mobject): def align_submobjects(self, mobject):
# If one is empty, and the other is not, mob1 = self
# push it into its submobject list mob2 = mobject
self_has_points, mob_has_points = [ n1 = len(mob1.submobjects)
mob.get_num_points() > 0 n2 = len(mob2.submobjects)
for mob in (self, mobject) mob1.add_n_more_submobjects(max(0, n2 - n1))
] mob2.add_n_more_submobjects(max(0, n1 - n2))
if self_has_points and not mob_has_points:
mobject.null_point_align(self)
elif mob_has_points and not self_has_points:
self.null_point_align(mobject)
self_count = len(self.submobjects)
mob_count = len(mobject.submobjects)
diff = self_count - mob_count
if diff < 0:
self.add_n_more_submobjects(-diff)
elif diff > 0:
mobject.add_n_more_submobjects(diff)
return self return self
def null_point_align(self, mobject): def null_point_align(self, mobject):
@ -992,19 +982,31 @@ class Mobject(Container):
return self return self
def add_n_more_submobjects(self, n): def add_n_more_submobjects(self, n):
if n == 0:
return
curr = len(self.submobjects) curr = len(self.submobjects)
if n > 0 and curr == 0: if curr == 0:
self.add(self.copy()) # If empty, simply add n point mobjects
n -= 1 self.submobjects = [
curr += 1 self.get_point_mobject()
indices = curr * np.arange(curr + n) // (curr + n) for k in range(n)
new_submobjects = [] ]
for index in indices: return
submob = self.submobjects[index]
if submob in new_submobjects: target = curr + n
submob = self.repeat_submobject(submob) # TODO, factor this out to utils
new_submobjects.append(submob) repeat_indices = (np.arange(target) * curr) // target
self.submobjects = new_submobjects split_factors = [
sum(repeat_indices == i)
for i in range(curr)
]
new_submobs = []
for submob, sf in zip(self.submobjects, split_factors):
new_submobs.append(submob)
for k in range(1, sf):
new_submobs.append(submob.get_point_mobject())
self.submobjects = new_submobs
return self return self
def repeat_submobject(self, submob): def repeat_submobject(self, submob):