mirror of
https://github.com/3b1b/manim.git
synced 2025-09-01 00:48:45 +00:00
Merge branch 'master' of github.com:3b1b/manim into video-work
This commit is contained in:
commit
62c7a89a8d
6 changed files with 209 additions and 214 deletions
|
@ -68,4 +68,3 @@ from manimlib.utils.rate_functions import *
|
||||||
from manimlib.utils.simple_functions import *
|
from manimlib.utils.simple_functions import *
|
||||||
from manimlib.utils.sounds import *
|
from manimlib.utils.sounds import *
|
||||||
from manimlib.utils.space_ops import *
|
from manimlib.utils.space_ops import *
|
||||||
from manimlib.utils.strings import *
|
|
||||||
|
|
|
@ -337,18 +337,6 @@ class Axes(VGroup, CoordinateSystem):
|
||||||
def get_axes(self):
|
def get_axes(self):
|
||||||
return self.axes
|
return self.axes
|
||||||
|
|
||||||
def get_axis(self, index):
|
|
||||||
return self.get_axes()[index]
|
|
||||||
|
|
||||||
def get_x_axis(self):
|
|
||||||
return self.get_axis(0)
|
|
||||||
|
|
||||||
def get_y_axis(self):
|
|
||||||
return self.get_axis(1)
|
|
||||||
|
|
||||||
def get_z_axis(self):
|
|
||||||
return self.get_axis(2)
|
|
||||||
|
|
||||||
def get_all_ranges(self):
|
def get_all_ranges(self):
|
||||||
return [self.x_range, self.y_range]
|
return [self.x_range, self.y_range]
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import itertools as it
|
import itertools as it
|
||||||
import re
|
import re
|
||||||
|
import sys
|
||||||
from types import MethodType
|
from types import MethodType
|
||||||
|
|
||||||
from manimlib.mobject.svg.svg_mobject import SVGMobject
|
from manimlib.mobject.svg.svg_mobject import SVGMobject
|
||||||
|
@ -10,19 +11,24 @@ from manimlib.utils.iterables import remove_list_redundancies
|
||||||
from manimlib.utils.tex_file_writing import tex_to_svg_file
|
from manimlib.utils.tex_file_writing import tex_to_svg_file
|
||||||
from manimlib.utils.tex_file_writing import get_tex_config
|
from manimlib.utils.tex_file_writing import get_tex_config
|
||||||
from manimlib.utils.tex_file_writing import display_during_execution
|
from manimlib.utils.tex_file_writing import display_during_execution
|
||||||
|
from manimlib.logger import log
|
||||||
|
|
||||||
|
|
||||||
SCALE_FACTOR_PER_FONT_POINT = 0.001
|
SCALE_FACTOR_PER_FONT_POINT = 0.001
|
||||||
|
|
||||||
|
|
||||||
tex_hash_to_mob_map = {}
|
TEX_HASH_TO_MOB_MAP = {}
|
||||||
|
|
||||||
|
|
||||||
|
def _contains(span_0, span_1):
|
||||||
|
return span_0[0] <= span_1[0] and span_1[1] <= span_0[1]
|
||||||
|
|
||||||
|
|
||||||
def _get_neighbouring_pairs(iterable):
|
def _get_neighbouring_pairs(iterable):
|
||||||
return list(adjacent_pairs(iterable))[:-1]
|
return list(adjacent_pairs(iterable))[:-1]
|
||||||
|
|
||||||
|
|
||||||
class _LabelledTex(SVGMobject):
|
class _PlainTex(SVGMobject):
|
||||||
CONFIG = {
|
CONFIG = {
|
||||||
"height": None,
|
"height": None,
|
||||||
"path_string_config": {
|
"path_string_config": {
|
||||||
|
@ -31,11 +37,19 @@ class _LabelledTex(SVGMobject):
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class _LabelledTex(_PlainTex):
|
||||||
|
def __init__(self, file_name=None, **kwargs):
|
||||||
|
super().__init__(file_name, **kwargs)
|
||||||
|
for glyph in self:
|
||||||
|
glyph.glyph_label = _LabelledTex.color_str_to_label(glyph.fill_color)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def color_str_to_label(color_str):
|
def color_str_to_label(color_str):
|
||||||
if len(color_str) == 4:
|
if len(color_str) == 4:
|
||||||
# "#RGB" => "#RRGGBB"
|
# "#RGB" => "#RRGGBB"
|
||||||
color_str = "#" + "".join([c * 2 for c in color_str[1:]])
|
color_str = "#" + "".join([c * 2 for c in color_str[1:]])
|
||||||
|
|
||||||
return int(color_str[1:], 16) - 1
|
return int(color_str[1:], 16) - 1
|
||||||
|
|
||||||
def get_mobjects_from(self, element, style):
|
def get_mobjects_from(self, element, style):
|
||||||
|
@ -56,7 +70,7 @@ class _LabelledTex(SVGMobject):
|
||||||
|
|
||||||
class _TexSpan(object):
|
class _TexSpan(object):
|
||||||
def __init__(self, script_type, label):
|
def __init__(self, script_type, label):
|
||||||
# script_type: 0 for normal, 1 for subscript, 2 for superscript.
|
# `script_type`: 0 for normal, 1 for subscript, 2 for superscript.
|
||||||
# Only those spans with `script_type == 0` will be colored.
|
# Only those spans with `script_type == 0` will be colored.
|
||||||
self.script_type = script_type
|
self.script_type = script_type
|
||||||
self.label = label
|
self.label = label
|
||||||
|
@ -70,37 +84,32 @@ class _TexSpan(object):
|
||||||
|
|
||||||
|
|
||||||
class _TexParser(object):
|
class _TexParser(object):
|
||||||
def __init__(self, mtex):
|
def __init__(self, tex_string, additional_substrings):
|
||||||
self.tex_string = mtex.tex_string
|
self.tex_string = tex_string
|
||||||
strings_to_break_up = remove_list_redundancies([
|
|
||||||
*mtex.isolate, *mtex.tex_to_color_map.keys(), mtex.tex_string
|
|
||||||
])
|
|
||||||
if "" in strings_to_break_up:
|
|
||||||
strings_to_break_up.remove("")
|
|
||||||
unbreakable_commands = mtex.unbreakable_commands
|
|
||||||
|
|
||||||
self.tex_spans_dict = {}
|
self.tex_spans_dict = {}
|
||||||
|
self.specified_substrings = []
|
||||||
self.current_label = 0
|
self.current_label = 0
|
||||||
self.break_up_by_braces()
|
self.brace_index_pairs = self.get_brace_index_pairs()
|
||||||
|
self.add_tex_span((0, len(tex_string)))
|
||||||
|
self.break_up_by_double_braces()
|
||||||
self.break_up_by_scripts()
|
self.break_up_by_scripts()
|
||||||
self.break_up_by_additional_strings(strings_to_break_up)
|
self.break_up_by_additional_substrings(additional_substrings)
|
||||||
self.merge_unbreakable_commands(unbreakable_commands)
|
self.check_if_overlap()
|
||||||
self.analyse_containing_labels()
|
self.analyse_containing_labels()
|
||||||
|
self.specified_substrings = remove_list_redundancies(self.specified_substrings)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def label_to_color_tuple(n):
|
def label_to_color_tuple(rgb):
|
||||||
# Get a unique color different from black,
|
# Get a unique color different from black,
|
||||||
# or the svg file will not include the color information.
|
# or the svg file will not include the color information.
|
||||||
rgb = n + 1
|
|
||||||
rg, b = divmod(rgb, 256)
|
rg, b = divmod(rgb, 256)
|
||||||
r, g = divmod(rg, 256)
|
r, g = divmod(rg, 256)
|
||||||
return r, g, b
|
return r, g, b
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def contains(span_0, span_1):
|
|
||||||
return span_0[0] <= span_1[0] and span_1[1] <= span_0[1]
|
|
||||||
|
|
||||||
def add_tex_span(self, span_tuple, script_type=0, label=-1):
|
def add_tex_span(self, span_tuple, script_type=0, label=-1):
|
||||||
|
if span_tuple in self.tex_spans_dict:
|
||||||
|
return
|
||||||
|
|
||||||
if script_type == 0:
|
if script_type == 0:
|
||||||
# Should be additionally labelled.
|
# Should be additionally labelled.
|
||||||
label = self.current_label
|
label = self.current_label
|
||||||
|
@ -109,11 +118,14 @@ class _TexParser(object):
|
||||||
tex_span = _TexSpan(script_type, label)
|
tex_span = _TexSpan(script_type, label)
|
||||||
self.tex_spans_dict[span_tuple] = tex_span
|
self.tex_spans_dict[span_tuple] = tex_span
|
||||||
|
|
||||||
def break_up_by_braces(self):
|
def add_specified_substring(self, span_tuple):
|
||||||
tex_string = self.tex_string
|
substring = self.tex_string[slice(*span_tuple)]
|
||||||
span_tuples = []
|
self.specified_substrings.append(substring)
|
||||||
|
|
||||||
|
def get_brace_index_pairs(self):
|
||||||
|
result = []
|
||||||
left_brace_indices = []
|
left_brace_indices = []
|
||||||
for match_obj in re.finditer(r"(\\*)(\{|\})", tex_string):
|
for match_obj in re.finditer(r"(\\*)(\{|\})", self.tex_string):
|
||||||
# Braces following even numbers of backslashes are counted.
|
# Braces following even numbers of backslashes are counted.
|
||||||
if len(match_obj.group(1)) % 2 == 1:
|
if len(match_obj.group(1)) % 2 == 1:
|
||||||
continue
|
continue
|
||||||
|
@ -123,17 +135,32 @@ class _TexParser(object):
|
||||||
else:
|
else:
|
||||||
left_brace_index = left_brace_indices.pop()
|
left_brace_index = left_brace_indices.pop()
|
||||||
right_brace_index = match_obj.span(2)[1]
|
right_brace_index = match_obj.span(2)[1]
|
||||||
span_tuples.append((left_brace_index, right_brace_index))
|
result.append((left_brace_index, right_brace_index))
|
||||||
if left_brace_indices:
|
if left_brace_indices:
|
||||||
self.raise_tex_parsing_error()
|
self.raise_tex_parsing_error("unmatched braces")
|
||||||
|
return result
|
||||||
|
|
||||||
self.paired_braces_tuples = span_tuples
|
def break_up_by_double_braces(self):
|
||||||
for span_tuple in span_tuples:
|
# Match paired double braces (`{{...}}`).
|
||||||
|
skip_pair = False
|
||||||
|
for prev_span_tuple, span_tuple in _get_neighbouring_pairs(
|
||||||
|
self.brace_index_pairs
|
||||||
|
):
|
||||||
|
if skip_pair:
|
||||||
|
skip_pair = False
|
||||||
|
continue
|
||||||
|
if all([
|
||||||
|
span_tuple[0] == prev_span_tuple[0] - 1,
|
||||||
|
span_tuple[1] == prev_span_tuple[1] + 1
|
||||||
|
]):
|
||||||
self.add_tex_span(span_tuple)
|
self.add_tex_span(span_tuple)
|
||||||
|
self.add_specified_substring(span_tuple)
|
||||||
|
skip_pair = True
|
||||||
|
|
||||||
def break_up_by_scripts(self):
|
def break_up_by_scripts(self):
|
||||||
|
# Match subscripts & superscripts.
|
||||||
tex_string = self.tex_string
|
tex_string = self.tex_string
|
||||||
brace_indices_dict = dict(self.tex_spans_dict.keys())
|
brace_indices_dict = dict(self.brace_index_pairs)
|
||||||
for match_obj in re.finditer(r"((?<!\\)(_|\^)\s*)|(\s+(_|\^)\s*)", tex_string):
|
for match_obj in re.finditer(r"((?<!\\)(_|\^)\s*)|(\s+(_|\^)\s*)", tex_string):
|
||||||
script_type = 1 if "_" in match_obj.group() else 2
|
script_type = 1 if "_" in match_obj.group() else 2
|
||||||
token_begin, token_end = match_obj.span()
|
token_begin, token_end = match_obj.span()
|
||||||
|
@ -142,7 +169,7 @@ class _TexParser(object):
|
||||||
else:
|
else:
|
||||||
content_match_obj = re.match(r"\w|\\[a-zA-Z]+", tex_string[token_end:])
|
content_match_obj = re.match(r"\w|\\[a-zA-Z]+", tex_string[token_end:])
|
||||||
if not content_match_obj:
|
if not content_match_obj:
|
||||||
self.raise_tex_parsing_error()
|
self.raise_tex_parsing_error("unclear subscript/superscript")
|
||||||
content_span = tuple([
|
content_span = tuple([
|
||||||
index + token_end for index in content_match_obj.span()
|
index + token_end for index in content_match_obj.span()
|
||||||
])
|
])
|
||||||
|
@ -154,11 +181,11 @@ class _TexParser(object):
|
||||||
label=label
|
label=label
|
||||||
)
|
)
|
||||||
|
|
||||||
def break_up_by_additional_strings(self, strings_to_break_up):
|
def break_up_by_additional_substrings(self, additional_substrings):
|
||||||
tex_string = self.tex_string
|
tex_string = self.tex_string
|
||||||
all_span_tuples = []
|
all_span_tuples = []
|
||||||
for string in strings_to_break_up:
|
for string in additional_substrings:
|
||||||
# Only matches non-crossing strings.
|
# Only match non-crossing strings.
|
||||||
for match_obj in re.finditer(re.escape(string), tex_string):
|
for match_obj in re.finditer(re.escape(string), tex_string):
|
||||||
all_span_tuples.append(match_obj.span())
|
all_span_tuples.append(match_obj.span())
|
||||||
|
|
||||||
|
@ -175,39 +202,37 @@ class _TexParser(object):
|
||||||
if span_begin >= span_end:
|
if span_begin >= span_end:
|
||||||
continue
|
continue
|
||||||
span_tuple = (span_begin, span_end)
|
span_tuple = (span_begin, span_end)
|
||||||
if span_tuple not in self.tex_spans_dict:
|
|
||||||
self.add_tex_span(span_tuple)
|
self.add_tex_span(span_tuple)
|
||||||
|
self.add_specified_substring(span_tuple)
|
||||||
|
|
||||||
def merge_unbreakable_commands(self, unbreakable_commands):
|
def check_if_overlap(self):
|
||||||
tex_string = self.tex_string
|
span_tuples = sorted(
|
||||||
command_merge_spans = []
|
self.tex_spans_dict.keys(),
|
||||||
brace_indices_dict = dict(self.paired_braces_tuples)
|
key=lambda t: (t[0], -t[1])
|
||||||
# Braces leading by `unbreakable_commands` shouldn't be marked.
|
)
|
||||||
for command in unbreakable_commands:
|
overlapping_span_pairs = []
|
||||||
for match_obj in re.finditer(re.escape(command), tex_string):
|
for i, span_0 in enumerate(span_tuples):
|
||||||
merge_begin_index = match_obj.span()[1]
|
for span_1 in span_tuples[i + 1 :]:
|
||||||
merge_end_index = merge_begin_index
|
if span_0[1] <= span_1[0]:
|
||||||
if merge_end_index not in brace_indices_dict:
|
|
||||||
continue
|
continue
|
||||||
while merge_end_index in brace_indices_dict:
|
if span_0[1] < span_1[1]:
|
||||||
merge_end_index = brace_indices_dict[merge_end_index]
|
overlapping_span_pairs.append((span_0, span_1))
|
||||||
command_merge_spans.append((merge_begin_index, merge_end_index))
|
if overlapping_span_pairs:
|
||||||
|
tex_string = self.tex_string
|
||||||
self.tex_spans_dict = {
|
log.error("Overlapping substring pairs occur in MTex:")
|
||||||
span_tuple: tex_span
|
for span_tuple_pair in overlapping_span_pairs:
|
||||||
for span_tuple, tex_span in self.tex_spans_dict.items()
|
log.error(", ".join(
|
||||||
if all([
|
f"\"{tex_string[slice(*span_tuple)]}\""
|
||||||
not _TexParser.contains(merge_span, span_tuple)
|
for span_tuple in span_tuple_pair
|
||||||
for merge_span in command_merge_spans
|
))
|
||||||
])
|
sys.exit(2)
|
||||||
}
|
|
||||||
|
|
||||||
def analyse_containing_labels(self):
|
def analyse_containing_labels(self):
|
||||||
for span_0, tex_span_0 in self.tex_spans_dict.items():
|
for span_0, tex_span_0 in self.tex_spans_dict.items():
|
||||||
if tex_span_0.script_type != 0:
|
if tex_span_0.script_type != 0:
|
||||||
continue
|
continue
|
||||||
for span_1, tex_span_1 in self.tex_spans_dict.items():
|
for span_1, tex_span_1 in self.tex_spans_dict.items():
|
||||||
if _TexParser.contains(span_1, span_0):
|
if _contains(span_1, span_0):
|
||||||
tex_span_1.containing_labels.append(tex_span_0.label)
|
tex_span_1.containing_labels.append(tex_span_0.label)
|
||||||
|
|
||||||
def get_labelled_expression(self):
|
def get_labelled_expression(self):
|
||||||
|
@ -215,18 +240,18 @@ class _TexParser(object):
|
||||||
if not self.tex_spans_dict:
|
if not self.tex_spans_dict:
|
||||||
return tex_string
|
return tex_string
|
||||||
|
|
||||||
|
# Remove the span of extire tex string.
|
||||||
indices_with_labels = sorted([
|
indices_with_labels = sorted([
|
||||||
(span_tuple[i], i, span_tuple[1 - i], tex_span.label)
|
(span_tuple[i], i, span_tuple[1 - i], tex_span.label)
|
||||||
for span_tuple, tex_span in self.tex_spans_dict.items()
|
for span_tuple, tex_span in self.tex_spans_dict.items()
|
||||||
if tex_span.script_type == 0
|
if tex_span.script_type == 0
|
||||||
for i in range(2)
|
for i in range(2)
|
||||||
], key=lambda t: (t[0], -t[1], -t[2]))
|
], key=lambda t: (t[0], -t[1], -t[2]))[1:]
|
||||||
# Add one more item to ensure all the substrings are joined.
|
|
||||||
indices_with_labels.append((len(tex_string), 0, 0, 0))
|
|
||||||
|
|
||||||
result = tex_string[: indices_with_labels[0][0]]
|
result = tex_string[: indices_with_labels[0][0]]
|
||||||
index_with_label_pairs = _get_neighbouring_pairs(indices_with_labels)
|
for index_with_label, next_index_with_label in _get_neighbouring_pairs(
|
||||||
for index_with_label, next_index_with_label in index_with_label_pairs:
|
indices_with_labels
|
||||||
|
):
|
||||||
index, flag, _, label = index_with_label
|
index, flag, _, label = index_with_label
|
||||||
next_index, *_ = next_index_with_label
|
next_index, *_ = next_index_with_label
|
||||||
# Adding one more pair of braces will help maintain the glyghs of tex file...
|
# Adding one more pair of braces will help maintain the glyghs of tex file...
|
||||||
|
@ -244,62 +269,86 @@ class _TexParser(object):
|
||||||
result += tex_string[index : next_index]
|
result += tex_string[index : next_index]
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def raise_tex_parsing_error(self):
|
def raise_tex_parsing_error(self, message):
|
||||||
raise ValueError(f"Failed to parse tex: \"{self.tex_string}\"")
|
raise ValueError(f"Failed to parse tex ({message}): \"{self.tex_string}\"")
|
||||||
|
|
||||||
|
|
||||||
class MTex(VMobject):
|
class MTex(VMobject):
|
||||||
CONFIG = {
|
CONFIG = {
|
||||||
"fill_opacity": 1.0,
|
"fill_opacity": 1.0,
|
||||||
"stroke_width": 0,
|
"stroke_width": 0,
|
||||||
"should_center": True,
|
|
||||||
"font_size": 48,
|
"font_size": 48,
|
||||||
"height": None,
|
|
||||||
"organize_left_to_right": False,
|
|
||||||
"alignment": "\\centering",
|
"alignment": "\\centering",
|
||||||
"tex_environment": "align*",
|
"tex_environment": "align*",
|
||||||
"isolate": [],
|
"isolate": [],
|
||||||
"unbreakable_commands": ["\\begin", "\\end"],
|
|
||||||
"tex_to_color_map": {},
|
"tex_to_color_map": {},
|
||||||
|
"generate_plain_tex_file": False,
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, tex_string, **kwargs):
|
def __init__(self, tex_string, **kwargs):
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
self.tex_string = MTex.modify_tex_string(tex_string)
|
tex_string = tex_string.strip()
|
||||||
|
# Prevent from passing an empty string.
|
||||||
|
if not tex_string:
|
||||||
|
tex_string = "\\quad"
|
||||||
|
self.tex_string = tex_string
|
||||||
|
|
||||||
tex_parser = _TexParser(self)
|
self.generate_mobject()
|
||||||
self.tex_spans_dict = tex_parser.tex_spans_dict
|
|
||||||
|
|
||||||
new_tex = tex_parser.get_labelled_expression()
|
|
||||||
full_tex = self.get_tex_file_body(new_tex)
|
|
||||||
hash_val = hash(full_tex)
|
|
||||||
if hash_val not in tex_hash_to_mob_map:
|
|
||||||
with display_during_execution(f"Writing \"{tex_string}\""):
|
|
||||||
filename = tex_to_svg_file(full_tex)
|
|
||||||
svg_mob = _LabelledTex(filename)
|
|
||||||
tex_hash_to_mob_map[hash_val] = svg_mob
|
|
||||||
self.add(*[
|
|
||||||
submob.copy()
|
|
||||||
for submob in tex_hash_to_mob_map[hash_val]
|
|
||||||
])
|
|
||||||
self.build_submobjects()
|
|
||||||
|
|
||||||
self.init_colors()
|
self.init_colors()
|
||||||
self.set_color_by_tex_to_color_map(self.tex_to_color_map)
|
self.set_color_by_tex_to_color_map(self.tex_to_color_map)
|
||||||
|
|
||||||
if self.height is None:
|
|
||||||
self.scale(SCALE_FACTOR_PER_FONT_POINT * self.font_size)
|
self.scale(SCALE_FACTOR_PER_FONT_POINT * self.font_size)
|
||||||
if self.organize_left_to_right:
|
|
||||||
self.organize_submobjects_left_to_right()
|
|
||||||
|
|
||||||
@staticmethod
|
def get_additional_substrings_to_break_up(self):
|
||||||
def modify_tex_string(tex_string):
|
result = remove_list_redundancies([
|
||||||
result = tex_string.strip("\n")
|
*self.tex_to_color_map.keys(), *self.isolate
|
||||||
# Prevent from passing an empty string.
|
])
|
||||||
if not result:
|
if "" in result:
|
||||||
result = "\\quad"
|
result.remove("")
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def get_parser(self):
|
||||||
|
return _TexParser(self.tex_string, self.get_additional_substrings_to_break_up())
|
||||||
|
|
||||||
|
def generate_mobject(self):
|
||||||
|
tex_string = self.tex_string
|
||||||
|
tex_parser = self.get_parser()
|
||||||
|
self.tex_spans_dict = tex_parser.tex_spans_dict
|
||||||
|
self.specified_substrings = tex_parser.specified_substrings
|
||||||
|
|
||||||
|
plain_full_tex = self.get_tex_file_body(tex_string)
|
||||||
|
plain_hash_val = hash(plain_full_tex)
|
||||||
|
if plain_hash_val in TEX_HASH_TO_MOB_MAP:
|
||||||
|
self.add(*TEX_HASH_TO_MOB_MAP[plain_hash_val].copy())
|
||||||
|
return self
|
||||||
|
|
||||||
|
labelled_expression = tex_parser.get_labelled_expression()
|
||||||
|
full_tex = self.get_tex_file_body(labelled_expression)
|
||||||
|
hash_val = hash(full_tex)
|
||||||
|
if hash_val in TEX_HASH_TO_MOB_MAP and not self.generate_plain_tex_file:
|
||||||
|
self.add(*TEX_HASH_TO_MOB_MAP[hash_val].copy())
|
||||||
|
return self
|
||||||
|
|
||||||
|
with display_during_execution(f"Writing \"{tex_string}\""):
|
||||||
|
filename = tex_to_svg_file(full_tex)
|
||||||
|
svg_mob = _LabelledTex(filename)
|
||||||
|
self.add(*svg_mob.copy())
|
||||||
|
self.build_submobjects()
|
||||||
|
TEX_HASH_TO_MOB_MAP[hash_val] = self
|
||||||
|
if not self.generate_plain_tex_file:
|
||||||
|
return self
|
||||||
|
|
||||||
|
with display_during_execution(f"Writing \"{tex_string}\""):
|
||||||
|
filename = tex_to_svg_file(plain_full_tex)
|
||||||
|
plain_svg_mob = _PlainTex(filename)
|
||||||
|
svg_mob = TEX_HASH_TO_MOB_MAP[hash_val]
|
||||||
|
for plain_submob, submob in zip(plain_svg_mob, svg_mob):
|
||||||
|
plain_submob.glyph_label = submob.glyph_label
|
||||||
|
self.add(*plain_svg_mob.copy())
|
||||||
|
self.build_submobjects()
|
||||||
|
TEX_HASH_TO_MOB_MAP[plain_hash_val] = self
|
||||||
|
return self
|
||||||
|
|
||||||
def get_tex_file_body(self, new_tex):
|
def get_tex_file_body(self, new_tex):
|
||||||
if self.tex_environment:
|
if self.tex_environment:
|
||||||
new_tex = "\n".join([
|
new_tex = "\n".join([
|
||||||
|
@ -333,7 +382,7 @@ class MTex(VMobject):
|
||||||
new_submobjects.append(submobject)
|
new_submobjects.append(submobject)
|
||||||
|
|
||||||
new_glyphs = []
|
new_glyphs = []
|
||||||
current_glyph_label = -1
|
current_glyph_label = 0
|
||||||
for submob in self.submobjects:
|
for submob in self.submobjects:
|
||||||
if submob.glyph_label == current_glyph_label:
|
if submob.glyph_label == current_glyph_label:
|
||||||
new_glyphs.append(submob)
|
new_glyphs.append(submob)
|
||||||
|
@ -347,45 +396,57 @@ class MTex(VMobject):
|
||||||
def sort_scripts_in_tex_order(self):
|
def sort_scripts_in_tex_order(self):
|
||||||
# LaTeX always puts superscripts before subscripts.
|
# LaTeX always puts superscripts before subscripts.
|
||||||
# This function sorts the submobjects of scripts in the order of tex given.
|
# This function sorts the submobjects of scripts in the order of tex given.
|
||||||
|
tex_spans_dict = self.tex_spans_dict
|
||||||
index_and_span_list = sorted([
|
index_and_span_list = sorted([
|
||||||
(index, span_tuple)
|
(index, span_tuple)
|
||||||
for span_tuple, tex_span in self.tex_spans_dict.items()
|
for span_tuple, tex_span in tex_spans_dict.items()
|
||||||
if tex_span.script_type != 0
|
if tex_span.script_type != 0
|
||||||
for index in span_tuple
|
for index in span_tuple
|
||||||
])
|
])
|
||||||
index_and_span_pair = _get_neighbouring_pairs(index_and_span_list)
|
|
||||||
for index_and_span_0, index_and_span_1 in index_and_span_pair:
|
switch_range_pairs = []
|
||||||
|
for index_and_span_0, index_and_span_1 in _get_neighbouring_pairs(
|
||||||
|
index_and_span_list
|
||||||
|
):
|
||||||
index_0, span_tuple_0 = index_and_span_0
|
index_0, span_tuple_0 = index_and_span_0
|
||||||
index_1, span_tuple_1 = index_and_span_1
|
index_1, span_tuple_1 = index_and_span_1
|
||||||
if index_0 != index_1:
|
if index_0 != index_1:
|
||||||
continue
|
continue
|
||||||
if not all([
|
if not all([
|
||||||
self.tex_spans_dict[span_tuple_0].script_type == 1,
|
tex_spans_dict[span_tuple_0].script_type == 1,
|
||||||
self.tex_spans_dict[span_tuple_1].script_type == 2
|
tex_spans_dict[span_tuple_1].script_type == 2
|
||||||
]):
|
]):
|
||||||
continue
|
continue
|
||||||
submob_slice_0 = self.slice_of_part(
|
submob_range_0 = self.range_of_part(
|
||||||
self.get_part_by_span_tuples([span_tuple_0])
|
self.get_part_by_span_tuples([span_tuple_0])
|
||||||
)
|
)
|
||||||
submob_slice_1 = self.slice_of_part(
|
submob_range_1 = self.range_of_part(
|
||||||
self.get_part_by_span_tuples([span_tuple_1])
|
self.get_part_by_span_tuples([span_tuple_1])
|
||||||
)
|
)
|
||||||
|
switch_range_pairs.append((submob_range_0, submob_range_1))
|
||||||
|
|
||||||
|
switch_range_pairs.sort(key=lambda pair: (pair[0].stop, -pair[0].start))
|
||||||
|
indices = list(range(len(self.submobjects)))
|
||||||
|
for submob_range_0, submob_range_1 in switch_range_pairs:
|
||||||
|
indices = [
|
||||||
|
*indices[: submob_range_1.start],
|
||||||
|
*indices[submob_range_0.start : submob_range_0.stop],
|
||||||
|
*indices[submob_range_1.stop : submob_range_0.start],
|
||||||
|
*indices[submob_range_1.start : submob_range_1.stop],
|
||||||
|
*indices[submob_range_0.stop :]
|
||||||
|
]
|
||||||
|
|
||||||
submobs = self.submobjects
|
submobs = self.submobjects
|
||||||
self.set_submobjects([
|
self.set_submobjects([submobs[i] for i in indices])
|
||||||
*submobs[: submob_slice_1.start],
|
|
||||||
*submobs[submob_slice_0],
|
|
||||||
*submobs[submob_slice_1.stop : submob_slice_0.start],
|
|
||||||
*submobs[submob_slice_1],
|
|
||||||
*submobs[submob_slice_0.stop :]
|
|
||||||
])
|
|
||||||
|
|
||||||
def assign_submob_tex_strings(self):
|
def assign_submob_tex_strings(self):
|
||||||
# Not sure whether this is the best practice...
|
# Not sure whether this is the best practice...
|
||||||
# Just a temporary hack for supporting `TransformMatchingTex`.
|
# This temporarily supports `TransformMatchingTex`.
|
||||||
tex_string = self.tex_string
|
tex_string = self.tex_string
|
||||||
|
tex_spans_dict = self.tex_spans_dict
|
||||||
# Use tex strings including "_", "^".
|
# Use tex strings including "_", "^".
|
||||||
label_dict = {}
|
label_dict = {}
|
||||||
for span_tuple, tex_span in self.tex_spans_dict.items():
|
for span_tuple, tex_span in tex_spans_dict.items():
|
||||||
if tex_span.script_type != 0:
|
if tex_span.script_type != 0:
|
||||||
label_dict[tex_span.label] = span_tuple
|
label_dict[tex_span.label] = span_tuple
|
||||||
else:
|
else:
|
||||||
|
@ -402,7 +463,7 @@ class MTex(VMobject):
|
||||||
curr_span_tuple = label_dict[curr_label]
|
curr_span_tuple = label_dict[curr_label]
|
||||||
prev_span_tuple = label_dict[prev_label]
|
prev_span_tuple = label_dict[prev_label]
|
||||||
next_span_tuple = label_dict[next_label]
|
next_span_tuple = label_dict[next_label]
|
||||||
containing_labels = self.tex_spans_dict[curr_span_tuple].containing_labels
|
containing_labels = tex_spans_dict[curr_span_tuple].containing_labels
|
||||||
tex_string_spans.append([
|
tex_string_spans.append([
|
||||||
prev_span_tuple[1] if prev_label in containing_labels else curr_span_tuple[0],
|
prev_span_tuple[1] if prev_label in containing_labels else curr_span_tuple[0],
|
||||||
next_span_tuple[0] if next_label in containing_labels else curr_span_tuple[1]
|
next_span_tuple[0] if next_label in containing_labels else curr_span_tuple[1]
|
||||||
|
@ -415,10 +476,11 @@ class MTex(VMobject):
|
||||||
submob.get_tex = MethodType(lambda inst: inst.tex_string, submob)
|
submob.get_tex = MethodType(lambda inst: inst.tex_string, submob)
|
||||||
|
|
||||||
def get_part_by_span_tuples(self, span_tuples):
|
def get_part_by_span_tuples(self, span_tuples):
|
||||||
labels = remove_list_redundancies(list(it.chain(*[
|
tex_spans_dict = self.tex_spans_dict
|
||||||
self.tex_spans_dict[span_tuple].containing_labels
|
labels = set(it.chain(*[
|
||||||
|
tex_spans_dict[span_tuple].containing_labels
|
||||||
for span_tuple in span_tuples
|
for span_tuple in span_tuples
|
||||||
])))
|
]))
|
||||||
return VGroup(*filter(
|
return VGroup(*filter(
|
||||||
lambda submob: submob.submob_label in labels,
|
lambda submob: submob.submob_label in labels,
|
||||||
self.submobjects
|
self.submobjects
|
||||||
|
@ -464,7 +526,10 @@ class MTex(VMobject):
|
||||||
|
|
||||||
def set_color_by_tex_to_color_map(self, tex_to_color_map):
|
def set_color_by_tex_to_color_map(self, tex_to_color_map):
|
||||||
for tex, color in list(tex_to_color_map.items()):
|
for tex, color in list(tex_to_color_map.items()):
|
||||||
|
try:
|
||||||
self.set_color_by_tex(tex, color)
|
self.set_color_by_tex(tex, color)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def indices_of_part(self, part):
|
def indices_of_part(self, part):
|
||||||
|
@ -480,13 +545,19 @@ class MTex(VMobject):
|
||||||
part = self.get_part_by_tex(tex, index=index)
|
part = self.get_part_by_tex(tex, index=index)
|
||||||
return self.indices_of_part(part)
|
return self.indices_of_part(part)
|
||||||
|
|
||||||
def slice_of_part(self, part):
|
def indices_of_all_parts_by_tex(self, tex, index=0):
|
||||||
indices = self.indices_of_part(part)
|
all_parts = self.get_parts_by_tex(tex)
|
||||||
return slice(indices[0], indices[-1] + 1)
|
return list(it.chain(*[
|
||||||
|
self.indices_of_part(part) for part in all_parts
|
||||||
|
]))
|
||||||
|
|
||||||
def slice_of_part_by_tex(self, tex, index=0):
|
def range_of_part(self, part):
|
||||||
|
indices = self.indices_of_part(part)
|
||||||
|
return range(indices[0], indices[-1] + 1)
|
||||||
|
|
||||||
|
def range_of_part_by_tex(self, tex, index=0):
|
||||||
part = self.get_part_by_tex(tex, index=index)
|
part = self.get_part_by_tex(tex, index=index)
|
||||||
return self.slice_of_part(part)
|
return self.range_of_part(part)
|
||||||
|
|
||||||
def index_of_part(self, part):
|
def index_of_part(self, part):
|
||||||
return self.indices_of_part(part)[0]
|
return self.indices_of_part(part)[0]
|
||||||
|
@ -505,14 +576,11 @@ class MTex(VMobject):
|
||||||
for span_tuple in self.tex_spans_dict.keys()
|
for span_tuple in self.tex_spans_dict.keys()
|
||||||
])
|
])
|
||||||
|
|
||||||
def print_tex_strings_of_submobjects(self):
|
def list_tex_strings_of_submobjects(self):
|
||||||
# For debugging
|
# Work with `index_labels()`.
|
||||||
# Work with `index_labels()`
|
log.debug(f"Submobjects of \"{self.get_tex()}\":")
|
||||||
print("\n")
|
|
||||||
print(f"Submobjects of \"{self.get_tex()}\":")
|
|
||||||
for i, submob in enumerate(self.submobjects):
|
for i, submob in enumerate(self.submobjects):
|
||||||
print(f"{i}: \"{submob.get_tex()}\"")
|
log.debug(f"{i}: \"{submob.get_tex()}\"")
|
||||||
print("\n")
|
|
||||||
|
|
||||||
|
|
||||||
class MTexText(MTex):
|
class MTexText(MTex):
|
||||||
|
|
|
@ -7,8 +7,6 @@ from manimlib.constants import WHITE
|
||||||
from manimlib.constants import COLORMAP_3B1B
|
from manimlib.constants import COLORMAP_3B1B
|
||||||
from manimlib.utils.bezier import interpolate
|
from manimlib.utils.bezier import interpolate
|
||||||
from manimlib.utils.iterables import resize_with_interpolation
|
from manimlib.utils.iterables import resize_with_interpolation
|
||||||
from manimlib.utils.simple_functions import clip_in_place
|
|
||||||
from manimlib.utils.space_ops import normalize
|
|
||||||
|
|
||||||
|
|
||||||
def color_to_rgb(color):
|
def color_to_rgb(color):
|
||||||
|
@ -105,16 +103,6 @@ def random_color():
|
||||||
return Color(rgb=(random.random() for i in range(3)))
|
return Color(rgb=(random.random() for i in range(3)))
|
||||||
|
|
||||||
|
|
||||||
def get_shaded_rgb(rgb, point, unit_normal_vect, light_source):
|
|
||||||
to_sun = normalize(light_source - point)
|
|
||||||
factor = 0.5 * np.dot(unit_normal_vect, to_sun)**3
|
|
||||||
if factor < 0:
|
|
||||||
factor *= 0.5
|
|
||||||
result = rgb + factor
|
|
||||||
clip_in_place(rgb + factor, 0, 1)
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
def get_colormap_list(map_name="viridis", n_colors=9):
|
def get_colormap_list(map_name="viridis", n_colors=9):
|
||||||
"""
|
"""
|
||||||
Options for map_name:
|
Options for map_name:
|
||||||
|
|
|
@ -1,14 +1,16 @@
|
||||||
import inspect
|
import inspect
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import math
|
import math
|
||||||
|
from functools import lru_cache
|
||||||
|
|
||||||
|
|
||||||
def sigmoid(x):
|
def sigmoid(x):
|
||||||
return 1.0 / (1 + np.exp(-x))
|
return 1.0 / (1 + np.exp(-x))
|
||||||
|
|
||||||
|
|
||||||
def choose(n, r):
|
@lru_cache(maxsize=10)
|
||||||
return math.comb(n, r)
|
def choose(n, k):
|
||||||
|
return math.comb(n, k)
|
||||||
|
|
||||||
|
|
||||||
def gen_choose(n, r):
|
def gen_choose(n, r):
|
||||||
|
@ -37,14 +39,6 @@ def clip(a, min_a, max_a):
|
||||||
return a
|
return a
|
||||||
|
|
||||||
|
|
||||||
def clip_in_place(array, min_val=None, max_val=None):
|
|
||||||
if max_val is not None:
|
|
||||||
array[array > max_val] = max_val
|
|
||||||
if min_val is not None:
|
|
||||||
array[array < min_val] = min_val
|
|
||||||
return array
|
|
||||||
|
|
||||||
|
|
||||||
def fdiv(a, b, zero_over_zero_value=None):
|
def fdiv(a, b, zero_over_zero_value=None):
|
||||||
if zero_over_zero_value is not None:
|
if zero_over_zero_value is not None:
|
||||||
out = np.full_like(a, zero_over_zero_value)
|
out = np.full_like(a, zero_over_zero_value)
|
||||||
|
|
|
@ -1,42 +0,0 @@
|
||||||
import re
|
|
||||||
import string
|
|
||||||
|
|
||||||
|
|
||||||
def to_camel_case(name):
|
|
||||||
return "".join([
|
|
||||||
[c for c in part if c not in string.punctuation + string.whitespace].capitalize()
|
|
||||||
for part in name.split("_")
|
|
||||||
])
|
|
||||||
|
|
||||||
|
|
||||||
def initials(name, sep_values=[" ", "_"]):
|
|
||||||
return "".join([
|
|
||||||
(s[0] if s else "")
|
|
||||||
for s in re.split("|".join(sep_values), name)
|
|
||||||
])
|
|
||||||
|
|
||||||
|
|
||||||
def camel_case_initials(name):
|
|
||||||
return [c for c in name if c.isupper()]
|
|
||||||
|
|
||||||
|
|
||||||
def complex_string(complex_num):
|
|
||||||
return [c for c in str(complex_num) if c not in "()"]
|
|
||||||
|
|
||||||
|
|
||||||
def split_string_to_isolate_substrings(full_string, *isolate):
|
|
||||||
"""
|
|
||||||
Given a string, and an arbitrary number of possible substrings,
|
|
||||||
to isolate, this returns a list of strings which would concatenate
|
|
||||||
to make the full string, and in which these special substrings
|
|
||||||
appear as their own elements.
|
|
||||||
|
|
||||||
For example,split_string_to_isolate_substrings("to be or not to be", "to", "be")
|
|
||||||
would return ["to", " ", "be", " or not ", "to", " ", "be"]
|
|
||||||
"""
|
|
||||||
pattern = "|".join(*(
|
|
||||||
"({})".format(re.escape(ss))
|
|
||||||
for ss in isolate
|
|
||||||
))
|
|
||||||
pieces = re.split(pattern, full_string)
|
|
||||||
return list(filter(lambda s: s, pieces))
|
|
Loading…
Add table
Reference in a new issue