mirror of
https://github.com/ManimCommunity/manim.git
synced 2026-06-22 10:01:47 +00:00
Removed :class:~.GraphScene, :class:~.NumberLineOld and parameters for :class:~.ChangingDecimal (#1860)
* remove NumberLineOld and GraphScene * delete graphscene tests, shift plotfunctions test * rewrite tests and fix __init__.py * remove deprecation warning * Update manim/animation/numbers.py Co-authored-by: Jason Villanueva <a@jsonvillanueva.com>
This commit is contained in:
parent
10e2ddd6c6
commit
6eed276330
10 changed files with 10 additions and 1481 deletions
|
|
@ -83,7 +83,6 @@ from .mobject.types.vectorized_mobject import *
|
|||
from .mobject.value_tracker import *
|
||||
from .mobject.vector_field import *
|
||||
from .renderer.cairo_renderer import *
|
||||
from .scene.graph_scene import *
|
||||
from .scene.moving_camera_scene import *
|
||||
from .scene.reconfigurable_scene import *
|
||||
from .scene.sample_space_scene import *
|
||||
|
|
|
|||
|
|
@ -4,20 +4,13 @@ __all__ = ["ChangingDecimal", "ChangeDecimalToValue"]
|
|||
|
||||
|
||||
import typing
|
||||
import warnings
|
||||
|
||||
from ..animation.animation import Animation
|
||||
from ..mobject.numbers import DecimalNumber
|
||||
from ..utils.bezier import interpolate
|
||||
from ..utils.deprecation import deprecated_params
|
||||
|
||||
|
||||
class ChangingDecimal(Animation):
|
||||
@deprecated_params(
|
||||
"tracked_mobject position_update_func",
|
||||
until="v0.6.0",
|
||||
message="Use a mobject updater instead.",
|
||||
)
|
||||
def __init__(
|
||||
self,
|
||||
decimal_mob: DecimalNumber,
|
||||
|
|
|
|||
|
|
@ -55,8 +55,6 @@ from ..utils.config_ops import merge_dicts_recursively, update_dict_recursively
|
|||
from ..utils.simple_functions import binary_search
|
||||
from ..utils.space_ops import angle_of_vector
|
||||
|
||||
# TODO: There should be much more code reuse between Axes, NumberPlane and GraphScene
|
||||
|
||||
|
||||
class CoordinateSystem:
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -1,8 +1,7 @@
|
|||
"""Mobject representing a number line."""
|
||||
|
||||
__all__ = ["NumberLine", "UnitInterval", "NumberLineOld"]
|
||||
__all__ = ["NumberLine", "UnitInterval"]
|
||||
|
||||
import operator as op
|
||||
from typing import TYPE_CHECKING, Dict, Union
|
||||
|
||||
import numpy as np
|
||||
|
|
@ -17,7 +16,6 @@ from ..mobject.types.vectorized_mobject import VGroup
|
|||
from ..utils.bezier import interpolate
|
||||
from ..utils.color import LIGHT_GREY
|
||||
from ..utils.config_ops import merge_dicts_recursively
|
||||
from ..utils.deprecation import deprecated
|
||||
from ..utils.simple_functions import fdiv
|
||||
from ..utils.space_ops import normalize
|
||||
|
||||
|
|
@ -403,7 +401,6 @@ class UnitInterval(NumberLine):
|
|||
def __init__(
|
||||
self,
|
||||
unit_size=10,
|
||||
tick_frequency=0.1,
|
||||
numbers_with_elongated_ticks=None,
|
||||
decimal_number_config=None,
|
||||
**kwargs
|
||||
|
|
@ -422,241 +419,10 @@ class UnitInterval(NumberLine):
|
|||
else decimal_number_config
|
||||
)
|
||||
|
||||
NumberLine.__init__(
|
||||
self,
|
||||
x_range=[0, 1, 0.1],
|
||||
super().__init__(
|
||||
x_range=(0, 1, 0.1),
|
||||
unit_size=unit_size,
|
||||
numbers_with_elongated_ticks=numbers_with_elongated_ticks,
|
||||
decimal_number_config=decimal_number_config,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
|
||||
@deprecated(
|
||||
since="v0.7.0",
|
||||
until="v0.9.0",
|
||||
replacement="NumberLine",
|
||||
message="NumberLineOld will be removed alongside GraphScene ",
|
||||
)
|
||||
class NumberLineOld(Line):
|
||||
"""The old version of :class:`NumberLine`.
|
||||
|
||||
Exists for compatibility with :class:`~.GraphScene`.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
color=LIGHT_GREY,
|
||||
unit_size=1,
|
||||
width=None,
|
||||
rotation=0,
|
||||
include_ticks=True,
|
||||
tick_size=0.1,
|
||||
tick_frequency=1,
|
||||
# Defaults to value near x_min s.t. 0 is a tick
|
||||
# TODO, rename this
|
||||
leftmost_tick=None,
|
||||
# Change name
|
||||
numbers_with_elongated_ticks=[0],
|
||||
include_numbers=False,
|
||||
numbers_to_show=None,
|
||||
longer_tick_multiple=2,
|
||||
number_at_center=0,
|
||||
number_scale_val=0.75,
|
||||
label_direction=DOWN,
|
||||
line_to_number_buff=MED_SMALL_BUFF,
|
||||
include_tip=False,
|
||||
tip_width=0.25,
|
||||
tip_height=0.25,
|
||||
add_start=0, # extend number line by this amount at its starting point
|
||||
add_end=0, # extend number line by this amount at its end point
|
||||
decimal_number_config={"num_decimal_places": 0},
|
||||
exclude_zero_from_default_numbers=False,
|
||||
x_min=-config["frame_x_radius"],
|
||||
x_max=config["frame_x_radius"],
|
||||
**kwargs
|
||||
):
|
||||
self.unit_size = unit_size
|
||||
self.include_ticks = include_ticks
|
||||
self.tick_size = tick_size
|
||||
self.rotation = rotation
|
||||
self.tick_frequency = tick_frequency
|
||||
self.leftmost_tick = leftmost_tick
|
||||
self.include_numbers = include_numbers
|
||||
self.numbers_to_show = numbers_to_show
|
||||
self.longer_tick_multiple = longer_tick_multiple
|
||||
self.number_at_center = number_at_center
|
||||
self.number_scale_val = number_scale_val
|
||||
self.label_direction = label_direction
|
||||
self.line_to_number_buff = line_to_number_buff
|
||||
self.include_tip = include_tip
|
||||
self.tip_width = tip_width
|
||||
self.tip_height = tip_height
|
||||
self.add_start = add_start
|
||||
self.add_end = add_end
|
||||
self.decimal_number_config = decimal_number_config
|
||||
self.exclude_zero_from_default_numbers = exclude_zero_from_default_numbers
|
||||
self.x_min = x_min
|
||||
self.x_max = x_max
|
||||
|
||||
start = unit_size * self.x_min * RIGHT
|
||||
end = unit_size * self.x_max * RIGHT
|
||||
Line.__init__(
|
||||
self,
|
||||
start=start - add_start * RIGHT,
|
||||
end=end + add_end * RIGHT,
|
||||
color=color,
|
||||
**kwargs,
|
||||
)
|
||||
if width is not None:
|
||||
self.width = width
|
||||
self.unit_size = self.get_unit_size()
|
||||
self.shift(-self.number_to_point(self.number_at_center))
|
||||
|
||||
self.numbers_with_elongated_ticks = [
|
||||
nbr
|
||||
for nbr in numbers_with_elongated_ticks
|
||||
if self.x_min <= nbr <= self.x_max
|
||||
]
|
||||
|
||||
self.init_leftmost_tick()
|
||||
if self.include_tip:
|
||||
self.add_tip()
|
||||
if self.include_ticks:
|
||||
self.add_tick_marks()
|
||||
self.rotate_about_zero(self.rotation)
|
||||
if self.include_numbers:
|
||||
self.add_numbers()
|
||||
|
||||
def rotate_about_zero(self, angle, axis=OUT, **kwargs):
|
||||
return self.rotate_about_number(0, angle, axis, **kwargs)
|
||||
|
||||
def rotate_about_number(self, number, angle, axis=OUT, **kwargs):
|
||||
return self.rotate(angle, axis, about_point=self.n2p(number), **kwargs)
|
||||
|
||||
def init_leftmost_tick(self):
|
||||
if self.leftmost_tick is None:
|
||||
self.leftmost_tick = op.mul(
|
||||
self.tick_frequency, np.ceil(self.x_min / self.tick_frequency)
|
||||
)
|
||||
|
||||
def add_tick_marks(self):
|
||||
tick_size = self.tick_size
|
||||
self.tick_marks = VGroup(
|
||||
*[self.get_tick(x, tick_size) for x in self.get_tick_numbers()]
|
||||
)
|
||||
big_tick_size = tick_size * self.longer_tick_multiple
|
||||
self.big_tick_marks = VGroup(
|
||||
*[
|
||||
self.get_tick(x, big_tick_size)
|
||||
for x in self.numbers_with_elongated_ticks
|
||||
]
|
||||
)
|
||||
self.add(
|
||||
self.tick_marks,
|
||||
self.big_tick_marks,
|
||||
)
|
||||
|
||||
def get_tick(self, x, size=None):
|
||||
if size is None:
|
||||
size = self.tick_size
|
||||
result = Line(size * DOWN, size * UP)
|
||||
result.rotate(self.get_angle())
|
||||
result.move_to(self.number_to_point(x))
|
||||
result.match_style(self)
|
||||
return result
|
||||
|
||||
def get_tick_marks(self):
|
||||
return VGroup(
|
||||
*self.tick_marks,
|
||||
*self.big_tick_marks,
|
||||
)
|
||||
|
||||
def get_tick_numbers(self):
|
||||
u = -1 if self.include_tip and self.add_end == 0 else 1
|
||||
return np.arange(
|
||||
self.leftmost_tick,
|
||||
self.x_max + u * self.tick_frequency / 2,
|
||||
self.tick_frequency,
|
||||
)
|
||||
|
||||
def number_to_point(self, number):
|
||||
alpha = float(number - self.x_min) / (self.x_max - self.x_min)
|
||||
return interpolate(
|
||||
self.get_start() + self.add_start * RIGHT,
|
||||
self.get_end() - self.add_end * RIGHT,
|
||||
alpha,
|
||||
)
|
||||
|
||||
def point_to_number(self, point):
|
||||
start_point, end_point = self.get_start_and_end()
|
||||
full_vect = end_point - start_point
|
||||
unit_vect = normalize(full_vect)
|
||||
|
||||
def distance_from_start(p):
|
||||
return np.dot(p - start_point, unit_vect)
|
||||
|
||||
proportion = fdiv(distance_from_start(point), distance_from_start(end_point))
|
||||
return interpolate(self.x_min, self.x_max, proportion)
|
||||
|
||||
def n2p(self, number):
|
||||
"""Abbreviation for number_to_point"""
|
||||
return self.number_to_point(number)
|
||||
|
||||
def p2n(self, point):
|
||||
"""Abbreviation for point_to_number"""
|
||||
return self.point_to_number(point)
|
||||
|
||||
def get_unit_size(self):
|
||||
return self.get_length() / (self.x_max - self.x_min)
|
||||
|
||||
def get_unit_vector(self):
|
||||
return super().get_unit_vector() * self.unit_size
|
||||
|
||||
def default_numbers_to_display(self):
|
||||
if self.numbers_to_show is not None:
|
||||
return self.numbers_to_show
|
||||
numbers = np.arange(
|
||||
np.floor(self.leftmost_tick),
|
||||
np.ceil(self.x_max),
|
||||
)
|
||||
if self.exclude_zero_from_default_numbers:
|
||||
numbers = numbers[numbers != 0]
|
||||
return numbers
|
||||
|
||||
def get_number_mobject(
|
||||
self,
|
||||
number,
|
||||
number_config=None,
|
||||
scale_val=None,
|
||||
direction=None,
|
||||
buff=None,
|
||||
):
|
||||
number_config = merge_dicts_recursively(
|
||||
self.decimal_number_config,
|
||||
number_config or {},
|
||||
)
|
||||
if scale_val is None:
|
||||
scale_val = self.number_scale_val
|
||||
if direction is None:
|
||||
direction = self.label_direction
|
||||
buff = buff or self.line_to_number_buff
|
||||
num_mob = DecimalNumber(number, **number_config)
|
||||
num_mob.scale(scale_val)
|
||||
num_mob.next_to(self.number_to_point(number), direction=direction, buff=buff)
|
||||
return num_mob
|
||||
|
||||
def get_number_mobjects(self, *numbers, **kwargs):
|
||||
if len(numbers) == 0:
|
||||
numbers = self.default_numbers_to_display()
|
||||
return VGroup(
|
||||
*[self.get_number_mobject(number, **kwargs) for number in numbers]
|
||||
)
|
||||
|
||||
def get_labels(self):
|
||||
return self.get_number_mobjects()
|
||||
|
||||
def add_numbers(self, *numbers, **kwargs):
|
||||
self.numbers = self.get_number_mobjects(*numbers, **kwargs)
|
||||
self.add(self.numbers)
|
||||
return self
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
Binary file not shown.
BIN
tests/control_data/graphical_units_data/plot/plot_functions.npz
Normal file
BIN
tests/control_data/graphical_units_data/plot/plot_functions.npz
Normal file
Binary file not shown.
|
|
@ -2,47 +2,6 @@ import numpy as np
|
|||
import pytest
|
||||
|
||||
from manim.mobject.coordinate_systems import Axes
|
||||
from manim.scene.graph_scene import GraphScene
|
||||
|
||||
|
||||
def test_axes_without_shift():
|
||||
"""Test whether axes are not shifted when origin is in plot range."""
|
||||
G = GraphScene(
|
||||
x_min=-1, x_max=2, x_axis_label="", y_min=-2, y_max=5, y_axis_label=""
|
||||
)
|
||||
G.setup_axes()
|
||||
assert all(np.isclose(G.graph_origin, G.x_axis.n2p(0)))
|
||||
assert all(np.isclose(G.graph_origin, G.y_axis.n2p(0)))
|
||||
|
||||
|
||||
def test_axes_with_x_shift():
|
||||
"""Test whether x-axis is shifted when 0 is not in plot range of x-axis."""
|
||||
G = GraphScene(
|
||||
x_min=2, x_max=8, x_axis_label="", y_min=-2, y_max=5, y_axis_label=""
|
||||
)
|
||||
G.setup_axes()
|
||||
assert all(np.isclose(G.graph_origin, G.x_axis.n2p(2)))
|
||||
assert all(np.isclose(G.graph_origin, G.y_axis.n2p(0)))
|
||||
|
||||
|
||||
def test_axes_with_y_shift():
|
||||
"""Test whether y-axis is shifted when 0 is not in plot range of y-axis."""
|
||||
G = GraphScene(
|
||||
x_min=-1, x_max=2, x_axis_label="", y_min=1, y_max=5, y_axis_label=""
|
||||
)
|
||||
G.setup_axes()
|
||||
assert all(np.isclose(G.graph_origin, G.x_axis.n2p(0)))
|
||||
assert all(np.isclose(G.graph_origin, G.y_axis.n2p(1)))
|
||||
|
||||
|
||||
def test_axes_with_xy_shift():
|
||||
"""Test whether both axes are shifted when origin is not in plot range."""
|
||||
G = GraphScene(
|
||||
x_min=1, x_max=5, x_axis_label="", y_min=-5, y_max=-1, y_axis_label=""
|
||||
)
|
||||
G.setup_axes()
|
||||
assert all(np.isclose(G.graph_origin, G.x_axis.n2p(1)))
|
||||
assert all(np.isclose(G.graph_origin, G.y_axis.n2p(-5)))
|
||||
|
||||
|
||||
def test_axes_origin_shift():
|
||||
|
|
|
|||
|
|
@ -18,6 +18,13 @@ def test_axes(scene):
|
|||
scene.add(graph, labels)
|
||||
|
||||
|
||||
@frames_comparison()
|
||||
def test_plot_functions(scene):
|
||||
ax = Axes(x_range=(-10, 10.3), y_range=(-1.5, 1.5))
|
||||
graph = ax.get_graph(lambda x: x ** 2)
|
||||
scene.add(ax, graph)
|
||||
|
||||
|
||||
@frames_comparison
|
||||
def test_custom_coordinates(scene):
|
||||
ax = Axes(x_range=[0, 10])
|
||||
|
|
|
|||
|
|
@ -1,16 +0,0 @@
|
|||
from manim import *
|
||||
from tests.test_graphical_units.testing.frames_comparison import frames_comparison
|
||||
|
||||
__module_test__ = "graphscene"
|
||||
|
||||
|
||||
@frames_comparison(base_scene=GraphScene)
|
||||
def test_PlotFunctions(scene):
|
||||
scene.x_min = -10
|
||||
scene.x_max = 10.3
|
||||
scene.y_min = -1.5
|
||||
scene.y_max = 1.5
|
||||
constants.TEX_TEMPLATE = TexTemplate()
|
||||
scene.setup_axes()
|
||||
f = scene.get_graph(lambda x: x ** 2)
|
||||
scene.add(f)
|
||||
Loading…
Add table
Add a link
Reference in a new issue