mirror of
https://github.com/3b1b/manim.git
synced 2025-07-30 13:34:19 +08:00
Refactor of TexMobject to separate out SingleStringTexMobject. Should improve TexMobject transform animations
This commit is contained in:
@ -10,8 +10,10 @@ from mobject.types.vectorized_mobject import VectorizedPoint
|
|||||||
|
|
||||||
import operator as op
|
import operator as op
|
||||||
|
|
||||||
TEX_MOB_SCALE_FACTOR = 0.05
|
# TODO list
|
||||||
|
# - Make sure if "color" is passed into TexMobject, it behaves as expected
|
||||||
|
|
||||||
|
TEX_MOB_SCALE_FACTOR = 0.05
|
||||||
|
|
||||||
class TexSymbol(VMobjectFromSVGPathstring):
|
class TexSymbol(VMobjectFromSVGPathstring):
|
||||||
def pointwise_become_partial(self, mobject, a, b):
|
def pointwise_become_partial(self, mobject, a, b):
|
||||||
@ -31,57 +33,37 @@ class TexSymbol(VMobjectFromSVGPathstring):
|
|||||||
self.set_fill(opacity=opacity)
|
self.set_fill(opacity=opacity)
|
||||||
|
|
||||||
|
|
||||||
class TexMobject(SVGMobject):
|
class SingleStringTexMobject(SVGMobject):
|
||||||
CONFIG = {
|
CONFIG = {
|
||||||
"template_tex_file": TEMPLATE_TEX_FILE,
|
"template_tex_file": TEMPLATE_TEX_FILE,
|
||||||
"stroke_width": 0,
|
"stroke_width": 0,
|
||||||
"fill_opacity": 1.0,
|
"fill_opacity": 1.0,
|
||||||
"fill_color": WHITE,
|
"fill_color": WHITE,
|
||||||
"should_center": True,
|
"should_center": True,
|
||||||
"arg_separator": " ",
|
|
||||||
"height": None,
|
"height": None,
|
||||||
"organize_left_to_right": False,
|
"organize_left_to_right": False,
|
||||||
"propagate_style_to_family": True,
|
"propagate_style_to_family": True,
|
||||||
"alignment": "",
|
"alignment": "",
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, tex_string, **kwargs):
|
||||||
digest_config(self, kwargs, locals())
|
digest_config(self, kwargs)
|
||||||
|
assert(isinstance(tex_string, str))
|
||||||
if "color" in kwargs.keys() and "fill_color" not in kwargs.keys():
|
self.tex_string = self.get_modified_expression(tex_string)
|
||||||
self.fill_color = kwargs["color"]
|
|
||||||
|
|
||||||
# TODO, Eventually remove this
|
|
||||||
if len(args) == 1 and isinstance(args[0], list):
|
|
||||||
self.args = args[0]
|
|
||||||
##
|
|
||||||
assert(all([isinstance(a, str) for a in self.args]))
|
|
||||||
self.tex_string = self.get_modified_expression()
|
|
||||||
file_name = tex_to_svg_file(
|
file_name = tex_to_svg_file(
|
||||||
self.tex_string,
|
self.tex_string,
|
||||||
self.template_tex_file
|
self.template_tex_file
|
||||||
)
|
)
|
||||||
SVGMobject.__init__(self, file_name=file_name, **kwargs)
|
SVGMobject.__init__(self, file_name=file_name, **kwargs)
|
||||||
self.scale(TEX_MOB_SCALE_FACTOR)
|
if self.height is None:
|
||||||
|
self.scale(TEX_MOB_SCALE_FACTOR)
|
||||||
if self.organize_left_to_right:
|
if self.organize_left_to_right:
|
||||||
self.organize_submobjects_left_to_right()
|
self.organize_submobjects_left_to_right()
|
||||||
|
|
||||||
def path_string_to_mobject(self, path_string):
|
def get_modified_expression(self, tex_string):
|
||||||
# Overwrite superclass default to use
|
result = self.alignment + " " + tex_string
|
||||||
# specialized path_string mobject
|
|
||||||
return TexSymbol(path_string)
|
|
||||||
|
|
||||||
def generate_points(self):
|
|
||||||
SVGMobject.generate_points(self)
|
|
||||||
if len(self.args) > 1:
|
|
||||||
self.handle_multiple_args()
|
|
||||||
|
|
||||||
def get_modified_expression(self):
|
|
||||||
result = self.arg_separator.join(self.args)
|
|
||||||
result = " ".join([self.alignment, result])
|
|
||||||
result = result.strip()
|
result = result.strip()
|
||||||
result = self.modify_special_strings(result)
|
result = self.modify_special_strings(result)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def modify_special_strings(self, tex):
|
def modify_special_strings(self, tex):
|
||||||
@ -123,27 +105,64 @@ class TexMobject(SVGMobject):
|
|||||||
def get_tex_string(self):
|
def get_tex_string(self):
|
||||||
return self.tex_string
|
return self.tex_string
|
||||||
|
|
||||||
def handle_multiple_args(self):
|
def path_string_to_mobject(self, path_string):
|
||||||
|
# Overwrite superclass default to use
|
||||||
|
# specialized path_string mobject
|
||||||
|
return TexSymbol(path_string)
|
||||||
|
|
||||||
|
def organize_submobjects_left_to_right(self):
|
||||||
|
self.sort_submobjects(lambda p: p[0])
|
||||||
|
return self
|
||||||
|
|
||||||
|
def add_background_rectangle(self, color=BLACK, opacity=0.75, **kwargs):
|
||||||
|
self.background_rectangle = BackgroundRectangle(
|
||||||
|
self, color=color,
|
||||||
|
fill_opacity=opacity,
|
||||||
|
**kwargs
|
||||||
|
)
|
||||||
|
letters = VMobject(*self.submobjects)
|
||||||
|
self.submobjects = [self.background_rectangle, letters]
|
||||||
|
return self
|
||||||
|
|
||||||
|
|
||||||
|
class TexMobject(SingleStringTexMobject):
|
||||||
|
CONFIG = {
|
||||||
|
"arg_separator": " ",
|
||||||
|
}
|
||||||
|
|
||||||
|
def __init__(self, *tex_strings, **kwargs):
|
||||||
|
digest_config(self, kwargs)
|
||||||
|
self.tex_strings = tex_strings
|
||||||
|
SingleStringTexMobject.__init__(
|
||||||
|
self, self.arg_separator.join(tex_strings), **kwargs
|
||||||
|
)
|
||||||
|
self.break_up_by_substrings()
|
||||||
|
|
||||||
|
if self.organize_left_to_right:
|
||||||
|
self.organize_submobjects_left_to_right()
|
||||||
|
|
||||||
|
def break_up_by_substrings(self):
|
||||||
"""
|
"""
|
||||||
Reorganize existing submojects one layer
|
Reorganize existing submojects one layer
|
||||||
deeper based on the structure of args (as a list of strings)
|
deeper based on the structure of tex_strings (as a list
|
||||||
|
of tex_strings)
|
||||||
"""
|
"""
|
||||||
new_submobjects = []
|
new_submobjects = []
|
||||||
curr_index = 0
|
curr_index = 0
|
||||||
self.expression_parts = list(self.args)
|
# TODO, remove this attribute
|
||||||
for expr in self.args:
|
self.expression_parts = list(self.tex_strings)
|
||||||
sub_tex_mob = TexMobject(expr, **self.CONFIG)
|
for expr in self.tex_strings:
|
||||||
sub_tex_mob.tex_string = expr # Want it unmodified
|
sub_tex_mob = SingleStringTexMobject(expr, **self.CONFIG)
|
||||||
|
sub_tex_mob.tex_string = expr # We want it unmodified
|
||||||
num_submobs = len(sub_tex_mob.submobjects)
|
num_submobs = len(sub_tex_mob.submobjects)
|
||||||
new_index = curr_index + num_submobs
|
new_index = curr_index + num_submobs
|
||||||
if num_submobs == 0:
|
if num_submobs == 0:
|
||||||
if len(self) > curr_index:
|
# For cases like empty tex_strings, we want the corresponing
|
||||||
last_submob_index = curr_index
|
# part of the whole TexMobject to be a VectorizedPoint
|
||||||
else:
|
# positioned in the right part of the TexMobject
|
||||||
last_submob_index = -1
|
sub_tex_mob.submobjects = [VectorizedPoint()]
|
||||||
sub_tex_mob.submobjects = [VectorizedPoint(
|
last_submob_index = min(curr_index, len(self.submobjects) - 1)
|
||||||
self.submobjects[last_submob_index].get_right()
|
sub_tex_mob.move_to(self.submobjects[last_submob_index], RIGHT)
|
||||||
)]
|
|
||||||
else:
|
else:
|
||||||
sub_tex_mob.submobjects = self.submobjects[curr_index:new_index]
|
sub_tex_mob.submobjects = self.submobjects[curr_index:new_index]
|
||||||
new_submobjects.append(sub_tex_mob)
|
new_submobjects.append(sub_tex_mob)
|
||||||
@ -161,15 +180,9 @@ class TexMobject(SVGMobject):
|
|||||||
else:
|
else:
|
||||||
return tex1 == tex2
|
return tex1 == tex2
|
||||||
|
|
||||||
tex_submobjects = filter(
|
|
||||||
lambda m: isinstance(m, TexMobject),
|
|
||||||
self.submobject_family()
|
|
||||||
)
|
|
||||||
if hasattr(self, "expression_parts"):
|
|
||||||
tex_submobjects.remove(self)
|
|
||||||
return VGroup(*filter(
|
return VGroup(*filter(
|
||||||
lambda m: test(tex, m.get_tex_string()),
|
lambda m: test(tex, m.get_tex_string()),
|
||||||
tex_submobjects
|
self.submobjects
|
||||||
))
|
))
|
||||||
|
|
||||||
def get_part_by_tex(self, tex, **kwargs):
|
def get_part_by_tex(self, tex, **kwargs):
|
||||||
@ -185,7 +198,7 @@ class TexMobject(SVGMobject):
|
|||||||
def set_color_by_tex_to_color_map(self, texs_to_color_map, **kwargs):
|
def set_color_by_tex_to_color_map(self, texs_to_color_map, **kwargs):
|
||||||
for texs, color in texs_to_color_map.items():
|
for texs, color in texs_to_color_map.items():
|
||||||
try:
|
try:
|
||||||
# If the given key behaves like strings
|
# If the given key behaves like tex_strings
|
||||||
texs + ''
|
texs + ''
|
||||||
self.set_color_by_tex(texs, color, **kwargs)
|
self.set_color_by_tex(texs, color, **kwargs)
|
||||||
except TypeError:
|
except TypeError:
|
||||||
@ -204,28 +217,6 @@ class TexMobject(SVGMobject):
|
|||||||
part = self.get_part_by_tex(tex, **kwargs)
|
part = self.get_part_by_tex(tex, **kwargs)
|
||||||
return self.index_of_part(part)
|
return self.index_of_part(part)
|
||||||
|
|
||||||
def organize_submobjects_left_to_right(self):
|
|
||||||
self.sort_submobjects(lambda p: p[0])
|
|
||||||
return self
|
|
||||||
|
|
||||||
def sort_submobjects_alphabetically(self):
|
|
||||||
def alphabetical_cmp(m1, m2):
|
|
||||||
if not all([isinstance(m, TexMobject) for m in m1, m2]):
|
|
||||||
return 0
|
|
||||||
return cmp(m1.get_tex_string(), m2.get_tex_string())
|
|
||||||
self.submobjects.sort(alphabetical_cmp)
|
|
||||||
return self
|
|
||||||
|
|
||||||
def add_background_rectangle(self, color=BLACK, opacity=0.75, **kwargs):
|
|
||||||
self.background_rectangle = BackgroundRectangle(
|
|
||||||
self, color=color,
|
|
||||||
fill_opacity=opacity,
|
|
||||||
**kwargs
|
|
||||||
)
|
|
||||||
letters = VMobject(*self.submobjects)
|
|
||||||
self.submobjects = [self.background_rectangle, letters]
|
|
||||||
return self
|
|
||||||
|
|
||||||
def add_background_rectangle_to_parts(self):
|
def add_background_rectangle_to_parts(self):
|
||||||
for part in self:
|
for part in self:
|
||||||
part.add_background_rectangle()
|
part.add_background_rectangle()
|
||||||
|
Reference in New Issue
Block a user