To allow a custom dataclass to be interpolated using the Interpolator class, it must inherit from Interpolatable and implement a create_interpolator(self, timestamps) method.
If the fields within your dataclass are not themselves Interpolatable, you must specify the interpolation method using the interpolation_method metadata key on the field.
Interpolatable provides several metadata dictionaries to simplify this:
Interpolatable.LINEARInterpolatable.CUBIC_SPLINEInterpolatable.SLERPInterpolatable.ROTATION_SPLINEInterpolatable.DEFAULT_RIGID_TRANSFORM_INTERPOLATION
import dataclasses
from typing import Any
import numpy as np
from physical_ai_av.utils.interpolation import Interpolatable, Interpolator
@dataclasses.dataclass
class MyState(Interpolatable):
# Field is Interpolatable (if MyState implements create_interpolator)
position: np.ndarray
# Field is a raw type, so we must provide metadata
velocity: np.ndarray = dataclasses.field(
metadata=Interpolatable.LINEAR
)
def create_interpolator(self, timestamps: np.ndarray) -> Interpolator["MyState"]:
return Interpolator(timestamps, self)
# Usage
timestamps = np.array([0, 100], dtype=np.int64)
state = MyState(position=np.array([0,0,0]), velocity=np.array([1,1,1]))
interpolator = state.create_interpolator(timestamps)
# Interpolate at t=50
new_state = interpolator(50)