Skip to content

Commit

Permalink
added 'simplify' util, version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarx committed Jun 12, 2023
1 parent 70305dd commit 725d8b5
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name='keyframed',
version='0.3.13',
version='0.3.14',
author='David Marx',
long_description=README,
long_description_content_type='text/markdown',
Expand Down
5 changes: 4 additions & 1 deletion src/keyframed/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
)
from .serialization import to_yaml

from .utils import simplify


__all__ = [
'bisect_left_keyframe',
Expand All @@ -28,7 +30,8 @@
'Keyframe',
'ParameterGroup',
'register_interpolation_method',
'SmoothCurve',
'simplify',
'SinusoidalCurve',
'SmoothCurve',
'to_yaml',
]
2 changes: 1 addition & 1 deletion src/keyframed/curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def interpolator_arguments(self):
return {}

def __eq__(self, other) -> bool:
return self.value == other
return self.value == other
def __repr__(self) -> str:
#d = f"Keyframe(t={self.t}, value={self.value}, interpolation_method='{self.interpolation_method}')"
d = self.to_dict()
Expand Down
22 changes: 21 additions & 1 deletion src/keyframed/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,29 @@
from copy import deepcopy
import random, string

def simplify(curve):
j=1
while j < (len(curve._data)-1):
kf_prev = curve._data.peekitem(j-1)[1]
kf_this = curve._data.peekitem(j)[1]
kf_next = curve._data.peekitem(j+1)[1]

if not (kf_prev.value == kf_this.value == kf_next.value):
j+=1
continue
if not (kf_prev.interpolation_method == kf_this.interpolation_method == kf_next.interpolation_method):
j+=1
continue
if not (kf_prev._interpolator_arguments == kf_this._interpolator_arguments == kf_next._interpolator_arguments):
j+=1
continue
curve._data.popitem(j)
return curve


def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
# via https://stackoverflow.com/questions/2257441/random-string-generation-with-upper-case-letters-and-digits
return ''.join(random.choice(chars) for _ in range(size))
return ''.join(random.choice(chars) for _ in range(size))


class DictValuesArithmeticFriendly(UserDict):
Expand Down
9 changes: 8 additions & 1 deletion tests/test_curve.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from keyframed import Curve, Keyframe
from keyframed.curve import ensure_sorteddict_of_keyframes

from keyframed.utils import simplify
#from keyframed import simplify

import pytest
from sortedcontainers import SortedDict
Expand Down Expand Up @@ -163,3 +164,9 @@ def test_curve_interpolation():
###############################


def test_simplify():
c = Curve(((0,1), (1,1), (2,1), (3,1), (4,2), (5,1), (6,1)))
c2 = simplify(c.copy())
assert len(simplify._data) == 5
for i in range(10):
assert c[i] == c2[i]

0 comments on commit 725d8b5

Please sign in to comment.