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

67 lines
1.8 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
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from manimlib.typing import Self
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.
"""
2022-12-16 10:02:12 -08:00
value_type: type = np.float64
2022-12-16 10:02:12 -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_uniforms(self) -> None:
super().init_uniforms()
self.uniforms["value"] = np.array(
2021-08-19 08:37:57 -07:00
listify(self.value),
dtype=self.value_type,
)
2021-01-12 07:27:32 -10:00
def get_value(self) -> float | complex | np.ndarray:
result = self.uniforms["value"]
2021-08-26 11:42:58 -07:00
if len(result) == 1:
return result[0]
return result
2023-01-31 14:19:58 -08:00
def set_value(self, value: float | complex | np.ndarray) -> Self:
self.uniforms["value"][:] = 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):
2022-12-16 10:02:12 -08:00
value_type: type = np.complex128