mirror of
https://github.com/3b1b/manim.git
synced 2025-07-28 12:32:36 +08:00
Some refactors for MTex
This commit is contained in:
@ -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 *
|
|
||||||
|
@ -1,58 +0,0 @@
|
|||||||
directories:
|
|
||||||
# Set this to true if you want the path to video files
|
|
||||||
# to match the directory structure of the path to the
|
|
||||||
# sourcecode generating that video
|
|
||||||
mirror_module_path: False
|
|
||||||
# Where should manim output video and image files?
|
|
||||||
output: ""
|
|
||||||
# If you want to use images, manim will look to these folders to find them
|
|
||||||
raster_images: ""
|
|
||||||
vector_images: ""
|
|
||||||
# If you want to use sounds, manim will look here to find it.
|
|
||||||
sounds: ""
|
|
||||||
# Manim often generates tex_files or other kinds of serialized data
|
|
||||||
# to keep from having to generate the same thing too many times. By
|
|
||||||
# default, these will be stored at tempfile.gettempdir(), e.g. this might
|
|
||||||
# return whatever is at to the TMPDIR environment variable. If you want to
|
|
||||||
# specify them elsewhere,
|
|
||||||
temporary_storage: ""
|
|
||||||
tex:
|
|
||||||
executable: "latex"
|
|
||||||
template_file: "tex_template.tex"
|
|
||||||
intermediate_filetype: "dvi"
|
|
||||||
text_to_replace: "[tex_expression]"
|
|
||||||
# For ctex, use the following configuration
|
|
||||||
# executable: "xelatex -no-pdf"
|
|
||||||
# template_file: "ctex_template.tex"
|
|
||||||
# intermediate_filetype: "xdv"
|
|
||||||
universal_import_line: "from manimlib import *"
|
|
||||||
style:
|
|
||||||
font: "Consolas"
|
|
||||||
background_color: "#333333"
|
|
||||||
# Set the position of preview window, you can use directions, e.g. UL/DR/OL/OO/...
|
|
||||||
# also, you can also specify the position(pixel) of the upper left corner of
|
|
||||||
# the window on the monitor, e.g. "960,540"
|
|
||||||
window_position: UR
|
|
||||||
window_monitor: 0
|
|
||||||
full_screen: False
|
|
||||||
# If break_into_partial_movies is set to True, then many small
|
|
||||||
# files will be written corresponding to each Scene.play and
|
|
||||||
# Scene.wait call, and these files will then be combined
|
|
||||||
# to form the full scene. Sometimes video-editing is made
|
|
||||||
# easier when working with the broken up scene, which
|
|
||||||
# effectively has cuts at all the places you might want.
|
|
||||||
break_into_partial_movies: False
|
|
||||||
camera_qualities:
|
|
||||||
low:
|
|
||||||
resolution: "854x480"
|
|
||||||
frame_rate: 15
|
|
||||||
medium:
|
|
||||||
resolution: "1280x720"
|
|
||||||
frame_rate: 30
|
|
||||||
high:
|
|
||||||
resolution: "1920x1080"
|
|
||||||
frame_rate: 30
|
|
||||||
ultra_high:
|
|
||||||
resolution: "3840x2160"
|
|
||||||
frame_rate: 60
|
|
||||||
default_quality: "high"
|
|
@ -52,10 +52,10 @@ class CoordinateSystem():
|
|||||||
def p2c(self, point):
|
def p2c(self, point):
|
||||||
"""Abbreviation for point_to_coords"""
|
"""Abbreviation for point_to_coords"""
|
||||||
return self.point_to_coords(point)
|
return self.point_to_coords(point)
|
||||||
|
|
||||||
def get_origin(self):
|
def get_origin(self):
|
||||||
return self.c2p(*[0] * self.dimension)
|
return self.c2p(*[0] * self.dimension)
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_axes(self):
|
def get_axes(self):
|
||||||
raise Exception("Not implemented")
|
raise Exception("Not implemented")
|
||||||
@ -336,18 +336,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]
|
||||||
|
@ -15,14 +15,18 @@ from manimlib.utils.tex_file_writing import display_during_execution
|
|||||||
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,18 +35,20 @@ class _LabelledTex(SVGMobject):
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class _LabelledTex(_PlainTex):
|
||||||
@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)
|
||||||
|
|
||||||
def get_mobjects_from(self, element):
|
def get_mobjects_from(self, element):
|
||||||
result = super().get_mobjects_from(element)
|
result = super().get_mobjects_from(element)
|
||||||
for mob in result:
|
for mob in result:
|
||||||
if not hasattr(mob, "glyph_label"):
|
if not hasattr(mob, "glyph_label"):
|
||||||
mob.glyph_label = -1
|
mob.glyph_label = 0
|
||||||
try:
|
try:
|
||||||
color_str = element.getAttribute("fill")
|
color_str = element.getAttribute("fill")
|
||||||
if color_str:
|
if color_str:
|
||||||
@ -56,7 +62,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
|
||||||
@ -72,34 +78,23 @@ class _TexSpan(object):
|
|||||||
class _TexParser(object):
|
class _TexParser(object):
|
||||||
def __init__(self, mtex):
|
def __init__(self, mtex):
|
||||||
self.tex_string = mtex.tex_string
|
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
|
|
||||||
|
|
||||||
self.tex_spans_dict = {}
|
self.tex_spans_dict = {}
|
||||||
self.current_label = 0
|
self.current_label = 0
|
||||||
|
self.add_tex_span((0, len(self.tex_string)))
|
||||||
self.break_up_by_braces()
|
self.break_up_by_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_strings(mtex.strings_to_break_up)
|
||||||
self.merge_unbreakable_commands(unbreakable_commands)
|
self.merge_unbreakable_commands(mtex.unbreakable_commands)
|
||||||
self.analyse_containing_labels()
|
self.analyse_containing_labels()
|
||||||
|
|
||||||
@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 script_type == 0:
|
if script_type == 0:
|
||||||
# Should be additionally labelled.
|
# Should be additionally labelled.
|
||||||
@ -197,7 +192,7 @@ class _TexParser(object):
|
|||||||
span_tuple: tex_span
|
span_tuple: tex_span
|
||||||
for span_tuple, tex_span in self.tex_spans_dict.items()
|
for span_tuple, tex_span in self.tex_spans_dict.items()
|
||||||
if all([
|
if all([
|
||||||
not _TexParser.contains(merge_span, span_tuple)
|
not _contains(merge_span, span_tuple)
|
||||||
for merge_span in command_merge_spans
|
for merge_span in command_merge_spans
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
@ -207,7 +202,7 @@ class _TexParser(object):
|
|||||||
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,14 +210,13 @@ 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)
|
index_with_label_pairs = _get_neighbouring_pairs(indices_with_labels)
|
||||||
@ -252,53 +246,75 @@ 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": {},
|
||||||
|
"unbreakable_commands": ["\\begin", "\\end"],
|
||||||
|
"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 = " "
|
||||||
|
self.tex_string = tex_string
|
||||||
|
self.set_strings_to_break_up()
|
||||||
|
|
||||||
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)
|
||||||
|
scale_factor = SCALE_FACTOR_PER_FONT_POINT * self.font_size
|
||||||
|
self.scale(scale_factor)
|
||||||
|
|
||||||
if self.height is None:
|
def set_strings_to_break_up(self):
|
||||||
self.scale(SCALE_FACTOR_PER_FONT_POINT * self.font_size)
|
strings_to_break_up = remove_list_redundancies([
|
||||||
if self.organize_left_to_right:
|
*self.isolate, *self.tex_to_color_map.keys()
|
||||||
self.organize_submobjects_left_to_right()
|
])
|
||||||
|
if "" in strings_to_break_up:
|
||||||
|
strings_to_break_up.remove("")
|
||||||
|
self.strings_to_break_up = strings_to_break_up
|
||||||
|
|
||||||
@staticmethod
|
def generate_mobject(self):
|
||||||
def modify_tex_string(tex_string):
|
tex_parser = _TexParser(self)
|
||||||
result = tex_string.strip("\n")
|
self.tex_spans_dict = tex_parser.tex_spans_dict
|
||||||
# Prevent from passing an empty string.
|
|
||||||
if not result:
|
plain_full_tex = self.get_tex_file_body(self.tex_string)
|
||||||
result = "\\quad"
|
plain_hash_val = hash(plain_full_tex)
|
||||||
return result
|
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 \"{self.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 \"{self.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:
|
||||||
@ -333,7 +349,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)
|
||||||
@ -353,8 +369,10 @@ class MTex(VMobject):
|
|||||||
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)
|
index_and_span_pairs = _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 index_and_span_pairs:
|
||||||
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:
|
||||||
@ -364,24 +382,31 @@ class MTex(VMobject):
|
|||||||
self.tex_spans_dict[span_tuple_1].script_type == 2
|
self.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])
|
||||||
)
|
)
|
||||||
submobs = self.submobjects
|
switch_range_pairs.append((submob_range_0, submob_range_1))
|
||||||
self.set_submobjects([
|
|
||||||
*submobs[: submob_slice_1.start],
|
switch_range_pairs.sort(key=lambda pair: (pair[0].stop, -pair[0].start))
|
||||||
*submobs[submob_slice_0],
|
indices = list(range(len(self.submobjects)))
|
||||||
*submobs[submob_slice_1.stop : submob_slice_0.start],
|
for submob_range_0, submob_range_1 in switch_range_pairs:
|
||||||
*submobs[submob_slice_1],
|
indices = [
|
||||||
*submobs[submob_slice_0.stop :]
|
*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):
|
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
|
||||||
# Use tex strings including "_", "^".
|
# Use tex strings including "_", "^".
|
||||||
label_dict = {}
|
label_dict = {}
|
||||||
@ -464,7 +489,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()):
|
||||||
self.set_color_by_tex(tex, color)
|
try:
|
||||||
|
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 +508,13 @@ 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 range_of_part(self, part):
|
||||||
indices = self.indices_of_part(part)
|
indices = self.indices_of_part(part)
|
||||||
return slice(indices[0], indices[-1] + 1)
|
return range(indices[0], indices[-1] + 1)
|
||||||
|
|
||||||
def slice_of_part_by_tex(self, tex, index=0):
|
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]
|
||||||
|
@ -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,34 +1,16 @@
|
|||||||
from functools import reduce
|
|
||||||
import inspect
|
import inspect
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import operator as op
|
from scipy import special
|
||||||
|
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))
|
||||||
|
|
||||||
|
|
||||||
CHOOSE_CACHE = {}
|
@lru_cache(maxsize=10)
|
||||||
|
def choose(n, k):
|
||||||
|
return special.comb(n, k, exact=True)
|
||||||
def choose_using_cache(n, r):
|
|
||||||
if n not in CHOOSE_CACHE:
|
|
||||||
CHOOSE_CACHE[n] = {}
|
|
||||||
if r not in CHOOSE_CACHE[n]:
|
|
||||||
CHOOSE_CACHE[n][r] = choose(n, r, use_cache=False)
|
|
||||||
return CHOOSE_CACHE[n][r]
|
|
||||||
|
|
||||||
|
|
||||||
def choose(n, r, use_cache=True):
|
|
||||||
if use_cache:
|
|
||||||
return choose_using_cache(n, r)
|
|
||||||
if n < r:
|
|
||||||
return 0
|
|
||||||
if r == 0:
|
|
||||||
return 1
|
|
||||||
denom = reduce(op.mul, range(1, r + 1), 1)
|
|
||||||
numer = reduce(op.mul, range(n, n - r, -1), 1)
|
|
||||||
return numer // denom
|
|
||||||
|
|
||||||
|
|
||||||
def get_num_args(function):
|
def get_num_args(function):
|
||||||
@ -53,14 +35,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))
|
|
Reference in New Issue
Block a user