diff --git a/manimlib/__init__.py b/manimlib/__init__.py index 0a45a9c0..13e41ec0 100644 --- a/manimlib/__init__.py +++ b/manimlib/__init__.py @@ -68,4 +68,3 @@ from manimlib.utils.rate_functions import * from manimlib.utils.simple_functions import * from manimlib.utils.sounds import * from manimlib.utils.space_ops import * -from manimlib.utils.strings import * diff --git a/manimlib/mobject/coordinate_systems.py b/manimlib/mobject/coordinate_systems.py index 05d23785..3ad01086 100644 --- a/manimlib/mobject/coordinate_systems.py +++ b/manimlib/mobject/coordinate_systems.py @@ -52,10 +52,10 @@ class CoordinateSystem(): def p2c(self, point): """Abbreviation for point_to_coords""" return self.point_to_coords(point) - + def get_origin(self): return self.c2p(*[0] * self.dimension) - + @abstractmethod def get_axes(self): raise Exception("Not implemented") @@ -336,18 +336,6 @@ class Axes(VGroup, CoordinateSystem): def get_axes(self): 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): return [self.x_range, self.y_range] diff --git a/manimlib/mobject/svg/mtex_mobject.py b/manimlib/mobject/svg/mtex_mobject.py index 31790d9a..b7854dd3 100644 --- a/manimlib/mobject/svg/mtex_mobject.py +++ b/manimlib/mobject/svg/mtex_mobject.py @@ -1,5 +1,6 @@ import itertools as it import re +import sys from types import MethodType 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 get_tex_config from manimlib.utils.tex_file_writing import display_during_execution +from manimlib.logger import log 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): return list(adjacent_pairs(iterable))[:-1] -class _LabelledTex(SVGMobject): +class _PlainTex(SVGMobject): CONFIG = { "height": None, "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 def color_str_to_label(color_str): if len(color_str) == 4: # "#RGB" => "#RRGGBB" color_str = "#" + "".join([c * 2 for c in color_str[1:]]) + return int(color_str[1:], 16) - 1 def get_mobjects_from(self, element, style): @@ -56,7 +70,7 @@ class _LabelledTex(SVGMobject): class _TexSpan(object): 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. self.script_type = script_type self.label = label @@ -70,37 +84,32 @@ class _TexSpan(object): class _TexParser(object): - def __init__(self, mtex): - self.tex_string = mtex.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 - + def __init__(self, tex_string, additional_substrings): + self.tex_string = tex_string self.tex_spans_dict = {} + self.specified_substrings = [] 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_additional_strings(strings_to_break_up) - self.merge_unbreakable_commands(unbreakable_commands) + self.break_up_by_additional_substrings(additional_substrings) + self.check_if_overlap() self.analyse_containing_labels() + self.specified_substrings = remove_list_redundancies(self.specified_substrings) @staticmethod - def label_to_color_tuple(n): + def label_to_color_tuple(rgb): # Get a unique color different from black, # or the svg file will not include the color information. - rgb = n + 1 rg, b = divmod(rgb, 256) r, g = divmod(rg, 256) 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): + if span_tuple in self.tex_spans_dict: + return + if script_type == 0: # Should be additionally labelled. label = self.current_label @@ -109,11 +118,14 @@ class _TexParser(object): tex_span = _TexSpan(script_type, label) self.tex_spans_dict[span_tuple] = tex_span - def break_up_by_braces(self): - tex_string = self.tex_string - span_tuples = [] + def add_specified_substring(self, span_tuple): + substring = self.tex_string[slice(*span_tuple)] + self.specified_substrings.append(substring) + + def get_brace_index_pairs(self): + result = [] 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. if len(match_obj.group(1)) % 2 == 1: continue @@ -123,17 +135,32 @@ class _TexParser(object): else: left_brace_index = left_brace_indices.pop() 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: - self.raise_tex_parsing_error() + self.raise_tex_parsing_error("unmatched braces") + return result - self.paired_braces_tuples = span_tuples - for span_tuple in span_tuples: - self.add_tex_span(span_tuple) + def break_up_by_double_braces(self): + # 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_specified_substring(span_tuple) + skip_pair = True def break_up_by_scripts(self): + # Match subscripts & superscripts. 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"((?= span_end: continue 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): - tex_string = self.tex_string - command_merge_spans = [] - brace_indices_dict = dict(self.paired_braces_tuples) - # Braces leading by `unbreakable_commands` shouldn't be marked. - for command in unbreakable_commands: - for match_obj in re.finditer(re.escape(command), tex_string): - merge_begin_index = match_obj.span()[1] - merge_end_index = merge_begin_index - if merge_end_index not in brace_indices_dict: + def check_if_overlap(self): + span_tuples = sorted( + self.tex_spans_dict.keys(), + key=lambda t: (t[0], -t[1]) + ) + overlapping_span_pairs = [] + for i, span_0 in enumerate(span_tuples): + for span_1 in span_tuples[i + 1 :]: + if span_0[1] <= span_1[0]: continue - while merge_end_index in brace_indices_dict: - merge_end_index = brace_indices_dict[merge_end_index] - command_merge_spans.append((merge_begin_index, merge_end_index)) - - self.tex_spans_dict = { - span_tuple: tex_span - for span_tuple, tex_span in self.tex_spans_dict.items() - if all([ - not _TexParser.contains(merge_span, span_tuple) - for merge_span in command_merge_spans - ]) - } + if span_0[1] < span_1[1]: + overlapping_span_pairs.append((span_0, span_1)) + if overlapping_span_pairs: + tex_string = self.tex_string + log.error("Overlapping substring pairs occur in MTex:") + for span_tuple_pair in overlapping_span_pairs: + log.error(", ".join( + f"\"{tex_string[slice(*span_tuple)]}\"" + for span_tuple in span_tuple_pair + )) + sys.exit(2) def analyse_containing_labels(self): for span_0, tex_span_0 in self.tex_spans_dict.items(): if tex_span_0.script_type != 0: continue 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) def get_labelled_expression(self): @@ -215,18 +240,18 @@ class _TexParser(object): if not self.tex_spans_dict: return tex_string + # Remove the span of extire tex string. indices_with_labels = sorted([ (span_tuple[i], i, span_tuple[1 - i], tex_span.label) for span_tuple, tex_span in self.tex_spans_dict.items() if tex_span.script_type == 0 for i in range(2) - ], key=lambda t: (t[0], -t[1], -t[2])) - # Add one more item to ensure all the substrings are joined. - indices_with_labels.append((len(tex_string), 0, 0, 0)) + ], key=lambda t: (t[0], -t[1], -t[2]))[1:] 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 index_with_label_pairs: + for index_with_label, next_index_with_label in _get_neighbouring_pairs( + indices_with_labels + ): index, flag, _, label = index_with_label next_index, *_ = next_index_with_label # 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] return result - def raise_tex_parsing_error(self): - raise ValueError(f"Failed to parse tex: \"{self.tex_string}\"") + def raise_tex_parsing_error(self, message): + raise ValueError(f"Failed to parse tex ({message}): \"{self.tex_string}\"") class MTex(VMobject): CONFIG = { "fill_opacity": 1.0, "stroke_width": 0, - "should_center": True, "font_size": 48, - "height": None, - "organize_left_to_right": False, "alignment": "\\centering", "tex_environment": "align*", "isolate": [], - "unbreakable_commands": ["\\begin", "\\end"], "tex_to_color_map": {}, + "generate_plain_tex_file": False, } def __init__(self, tex_string, **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.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.generate_mobject() self.init_colors() self.set_color_by_tex_to_color_map(self.tex_to_color_map) + self.scale(SCALE_FACTOR_PER_FONT_POINT * self.font_size) - if self.height is None: - self.scale(SCALE_FACTOR_PER_FONT_POINT * self.font_size) - if self.organize_left_to_right: - self.organize_submobjects_left_to_right() - - @staticmethod - def modify_tex_string(tex_string): - result = tex_string.strip("\n") - # Prevent from passing an empty string. - if not result: - result = "\\quad" + def get_additional_substrings_to_break_up(self): + result = remove_list_redundancies([ + *self.tex_to_color_map.keys(), *self.isolate + ]) + if "" in result: + result.remove("") 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): if self.tex_environment: new_tex = "\n".join([ @@ -333,7 +382,7 @@ class MTex(VMobject): new_submobjects.append(submobject) new_glyphs = [] - current_glyph_label = -1 + current_glyph_label = 0 for submob in self.submobjects: if submob.glyph_label == current_glyph_label: new_glyphs.append(submob) @@ -347,45 +396,57 @@ class MTex(VMobject): def sort_scripts_in_tex_order(self): # LaTeX always puts superscripts before subscripts. # 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, 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 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_1, span_tuple_1 = index_and_span_1 if index_0 != index_1: continue if not all([ - self.tex_spans_dict[span_tuple_0].script_type == 1, - self.tex_spans_dict[span_tuple_1].script_type == 2 + tex_spans_dict[span_tuple_0].script_type == 1, + tex_spans_dict[span_tuple_1].script_type == 2 ]): continue - submob_slice_0 = self.slice_of_part( + submob_range_0 = self.range_of_part( 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]) ) - submobs = self.submobjects - self.set_submobjects([ - *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 :] - ]) + 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 + self.set_submobjects([submobs[i] for i in indices]) def assign_submob_tex_strings(self): # 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_spans_dict = self.tex_spans_dict # Use tex strings including "_", "^". 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: label_dict[tex_span.label] = span_tuple else: @@ -402,7 +463,7 @@ class MTex(VMobject): curr_span_tuple = label_dict[curr_label] prev_span_tuple = label_dict[prev_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([ 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] @@ -415,10 +476,11 @@ class MTex(VMobject): submob.get_tex = MethodType(lambda inst: inst.tex_string, submob) def get_part_by_span_tuples(self, span_tuples): - labels = remove_list_redundancies(list(it.chain(*[ - self.tex_spans_dict[span_tuple].containing_labels + tex_spans_dict = self.tex_spans_dict + labels = set(it.chain(*[ + tex_spans_dict[span_tuple].containing_labels for span_tuple in span_tuples - ]))) + ])) return VGroup(*filter( lambda submob: submob.submob_label in labels, self.submobjects @@ -464,7 +526,10 @@ class MTex(VMobject): def set_color_by_tex_to_color_map(self, tex_to_color_map): for tex, color in list(tex_to_color_map.items()): - self.set_color_by_tex(tex, color) + try: + self.set_color_by_tex(tex, color) + except: + pass return self def indices_of_part(self, part): @@ -480,13 +545,19 @@ class MTex(VMobject): part = self.get_part_by_tex(tex, index=index) return self.indices_of_part(part) - def slice_of_part(self, part): - indices = self.indices_of_part(part) - return slice(indices[0], indices[-1] + 1) + def indices_of_all_parts_by_tex(self, tex, index=0): + all_parts = self.get_parts_by_tex(tex) + 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) - return self.slice_of_part(part) + return self.range_of_part(part) def index_of_part(self, part): return self.indices_of_part(part)[0] @@ -505,14 +576,11 @@ class MTex(VMobject): for span_tuple in self.tex_spans_dict.keys() ]) - def print_tex_strings_of_submobjects(self): - # For debugging - # Work with `index_labels()` - print("\n") - print(f"Submobjects of \"{self.get_tex()}\":") + def list_tex_strings_of_submobjects(self): + # Work with `index_labels()`. + log.debug(f"Submobjects of \"{self.get_tex()}\":") for i, submob in enumerate(self.submobjects): - print(f"{i}: \"{submob.get_tex()}\"") - print("\n") + log.debug(f"{i}: \"{submob.get_tex()}\"") class MTexText(MTex): diff --git a/manimlib/utils/color.py b/manimlib/utils/color.py index a3450486..93cc9577 100644 --- a/manimlib/utils/color.py +++ b/manimlib/utils/color.py @@ -7,8 +7,6 @@ from manimlib.constants import WHITE from manimlib.constants import COLORMAP_3B1B from manimlib.utils.bezier import interpolate 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): @@ -105,16 +103,6 @@ def random_color(): 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): """ Options for map_name: diff --git a/manimlib/utils/simple_functions.py b/manimlib/utils/simple_functions.py index e868b6a1..c6a7c5d1 100644 --- a/manimlib/utils/simple_functions.py +++ b/manimlib/utils/simple_functions.py @@ -1,14 +1,16 @@ import inspect import numpy as np import math +from functools import lru_cache def sigmoid(x): return 1.0 / (1 + np.exp(-x)) -def choose(n, r): - return math.comb(n, r) +@lru_cache(maxsize=10) +def choose(n, k): + return math.comb(n, k) def gen_choose(n, r): @@ -37,14 +39,6 @@ def clip(a, min_a, max_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): if zero_over_zero_value is not None: out = np.full_like(a, zero_over_zero_value) diff --git a/manimlib/utils/strings.py b/manimlib/utils/strings.py deleted file mode 100644 index 5e5b5d5c..00000000 --- a/manimlib/utils/strings.py +++ /dev/null @@ -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))