2022-02-15 14:37:15 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-03-31 18:05:02 -07:00
|
|
|
import numpy as np
|
2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.mobject.mobject import Mobject
|
2021-08-19 08:37:57 -07:00
|
|
|
from manimlib.utils.iterables import listify
|
2018-03-31 18:05:02 -07:00
|
|
|
|
2023-02-02 10:54:47 -08:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from manimlib.typing import Self
|
|
|
|
|
2018-03-31 18:05:02 -07:00
|
|
|
|
2018-08-15 16:23:29 -07:00
|
|
|
class ValueTracker(Mobject):
|
2018-03-31 18:05:02 -07:00
|
|
|
"""
|
2021-01-06 12:21:49 -08:00
|
|
|
Not meant to be displayed. Instead the position encodes some
|
2018-03-31 18:05:02 -07:00
|
|
|
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
|
2018-04-06 13:58:59 -07:00
|
|
|
|
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)
|
2018-03-31 18:05:02 -07:00
|
|
|
|
2023-01-15 12:34:28 -08:00
|
|
|
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
|
|
|
|
2023-01-15 12:34:28 -08: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
|
2018-03-31 18:05:02 -07:00
|
|
|
|
2023-01-31 14:19:58 -08:00
|
|
|
def set_value(self, value: float | complex | np.ndarray) -> Self:
|
2023-01-15 12:34:28 -08:00
|
|
|
self.uniforms["value"][:] = value
|
2018-03-31 18:05:02 -07:00
|
|
|
return self
|
|
|
|
|
2022-02-15 14:37:15 +08:00
|
|
|
def increment_value(self, d_value: float | complex) -> None:
|
2018-03-31 18:05:02 -07:00
|
|
|
self.set_value(self.get_value() + d_value)
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-31 18:05:02 -07:00
|
|
|
class ExponentialValueTracker(ValueTracker):
|
|
|
|
"""
|
|
|
|
Operates just like ValueTracker, except it encodes the value as the
|
2018-04-06 13:58:59 -07:00
|
|
|
exponential of a position coordinate, which changes how interpolation
|
2018-03-31 18:05:02 -07:00
|
|
|
behaves
|
|
|
|
"""
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2022-02-15 14:37:15 +08:00
|
|
|
def get_value(self) -> float | complex:
|
2018-08-15 16:23:29 -07:00
|
|
|
return np.exp(ValueTracker.get_value(self))
|
2018-03-31 18:05:02 -07:00
|
|
|
|
2022-02-15 14:37:15 +08:00
|
|
|
def set_value(self, value: float | complex):
|
2018-08-15 16:23:29 -07:00
|
|
|
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
|