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

59 lines
1.5 KiB
Python
Raw Normal View History

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,
}
def __init__(self, value=0, **kwargs):
2021-08-19 08:37:57 -07:00
self.value = value
2021-01-12 07:27:32 -10:00
super().__init__(**kwargs)
2021-01-12 07:27:32 -10:00
def init_data(self):
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):
2021-08-19 08:37:57 -07:00
return self.data["value"][0, :]
def set_value(self, value):
2021-08-19 08:37:57 -07:00
self.data["value"][0, :] = value
return self
def increment_value(self, d_value):
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):
return np.exp(ValueTracker.get_value(self))
def set_value(self, value):
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
}