Skip to content

Commit

Permalink
EASINGS= ... partial(eased_lerp, ease=...)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarx committed Dec 13, 2023
1 parent c647129 commit d6d345f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
19 changes: 17 additions & 2 deletions src/keyframed/interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Callable
import numpy as np
import torch
from functools import partial

def bisect_left_keyframe(k: Number, curve:'Curve', *args, **kargs) -> 'Keyframe':
"""
Expand Down Expand Up @@ -90,8 +91,11 @@ def exp_decay(t, curve, decay_rate):
return v0 * math.exp(-td * decay_rate)

def sine_wave(t, curve, wavelength=None, frequency=None, phase=0, amplitude=1):
if (wavelength is None) and (frequency is not None):
wavelength = 1/frequency
if (wavelength is None):
if (frequency is not None):
wavelength = 1/frequency
else:
wavelength = 4 # interpolate from 0 to pi/2
return amplitude * math.sin(2*math.pi*t / wavelength + phase)

INTERPOLATORS={
Expand All @@ -104,6 +108,17 @@ def sine_wave(t, curve, wavelength=None, frequency=None, phase=0, amplitude=1):
'sine_wave':sine_wave,
}

EASINGS={
None:bisect_left_value,
'previous':bisect_left_value,
'next':bisect_right_value,
'linear':partial(eased_lerp, ease=lambda t: t),
'sin':partial(eased_lerp, ease=lambda t: math.sin(t * math.pi / 2)),
'sin^2':partial(eased_lerp, ease=lambda t: math.sin(t * math.pi / 2)**2),

}


def register_interpolation_method(name:str, f:Callable):
"""
Adds a new interpolation method to the INTERPOLATORS registry.
Expand Down
19 changes: 16 additions & 3 deletions tests/test_interpolation.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import pytest
import math

from keyframed import Curve, Keyframe

from keyframed.interpolation import (
register_interpolation_method,
bisect_left_value,
INTERPOLATORS
INTERPOLATORS,
EASINGS
)


Expand Down Expand Up @@ -117,4 +118,16 @@ def test_curve_w_kf_specified_interpolator():
c1 = Curve({0:1, 5:Keyframe(t=5,value=1,interpolation_method='linear'), 9:5})
assert c1[7] == 3

###############
###############

def test_sin():
c1 = Curve({0:0, 2:1}, default_interpolation=EASINGS['sin'])
assert c1[1] == math.sin(math.pi/4)

def test_sin():
c1 = Curve({0:0, 2:2}, default_interpolation=EASINGS['sin'])
assert c1[1] == 2*math.sin(math.pi/4)

def test_sin2():
c1 = Curve({0:0, 2:1}, default_interpolation=EASINGS['sin^2'])
assert c1[1] == math.sin(math.pi/4)**2

0 comments on commit d6d345f

Please sign in to comment.