3b1b-manim/manimlib/mobject/value_tracker.py

64 lines
1.7 KiB
Python
Raw Normal View History

from __future__ import annotations
import numpy as np
from manimlib.mobject.mobject import Mobject
2021-08-19 08:37:57 -07:00
from manimlib.utils.iterables import listify
class ValueTracker(Mobject):
"""
Not meant to be displayed. Instead the position encodes some
number, often one which another animation or continual_animation
uses for its update function, and by treating it as a mobject it can
still be animated and manipulated just like anything else.
"""
2021-01-12 07:27:32 -10:00
CONFIG = {
"value_type": np.float64,
}
2022-11-18 09:11:59 -08:00
def __init__(self, value: float | complex | np.ndarray = 0, **kwargs):
2021-08-19 08:37:57 -07:00
self.value = value
2021-01-12 07:27:32 -10:00
super().__init__(**kwargs)
def init_data(self) -> None:
2021-01-12 07:27:32 -10:00
super().init_data()
2021-08-19 08:37:57 -07:00
self.data["value"] = np.array(
listify(self.value),
ndmin=2,
dtype=self.value_type,
)
2021-01-12 07:27:32 -10:00
def get_value(self) -> float | complex:
2021-08-26 11:42:58 -07:00
result = self.data["value"][0, :]
if len(result) == 1:
return result[0]
return result
def set_value(self, value: float | complex):
2021-08-19 08:37:57 -07:00
self.data["value"][0, :] = value
return self
def increment_value(self, d_value: float | complex) -> None:
self.set_value(self.get_value() + d_value)
class ExponentialValueTracker(ValueTracker):
"""
Operates just like ValueTracker, except it encodes the value as the
exponential of a position coordinate, which changes how interpolation
behaves
"""
def get_value(self) -> float | complex:
return np.exp(ValueTracker.get_value(self))
def set_value(self, value: float | complex):
return ValueTracker.set_value(self, np.log(value))
2018-12-26 11:33:50 -08:00
class ComplexValueTracker(ValueTracker):
2021-01-12 07:27:32 -10:00
CONFIG = {
"value_type": np.complex128
}