• Advertisement
Sign in to follow this  

Keyframe removal algorithm

This topic is 1426 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am looking for a fast algorithm to remove keyframes based on a maximum error. So if a keyframe is near enough to what can be interpolated from the other keyframes, then that keyframe should be removed. I am using linear interpolation only, so this should be relatively simple to do.

 

For example, let's say I have these keyframes, with their times and values:

0s 10

0.4s 11.1

0.8s 12

2.0s 20

2.9s 38

3.0s 40

 

In this example I can remove the key at 2.9s, because interpolating the keys at 2.0s and 3.0s would give the exact same value. Depending on the error threshold I might also be able to delete the key at 0.4s, since removing that one gives an error of only 0.1.

 

I need an algorithm that can handle several floating point data types, like floats, 2D positions and colours (rgba, so 4D).

 

Of course I managed to come up with simple brute force algorithms for this, but I would like to use something more efficient. I add a lot of keys in real-time over time and ideally the algorithm would do a small amount of work every time a keyframe is added, instead of doing a lot of work at the end, since otherwise I would get hickups whenever I need to finish a block of keyframes.

 

I tried searching for algorithms for this, but I mostly found info about things like MPEG keyframes and tools in Blender.

 

Thanks in advance! :)

Share this post


Link to post
Share on other sites
Advertisement

Are you inserting one keyframe at a time or multiple frames at once?  It doesn't sound like it would be too expensive to do the brute force as each key is inserted, since you'd only be looking at the adjacent keyframes.

 

Other than that, nothing is really coming to mind.  The best I can offer is maybe try rejiggering the data and see if anything leaps out at you.  Perhaps convert each frame to a delta value?

Share this post


Link to post
Share on other sites


I need an algorithm that can handle several floating point data types, like floats, 2D positions and colours (rgba, so 4D).

It would be easier to help you if you would provide something more detailed than "interpolating keyframes" as the goal. Keyframes for what? Animation? Video? Are you interpolating colors?

Share this post


Link to post
Share on other sites

 


I need an algorithm that can handle several floating point data types, like floats, 2D positions and colours (rgba, so 4D).

It would be easier to help you if you would provide something more detailed than "interpolating keyframes" as the goal. Keyframes for what? Animation? Video? Are you interpolating colors?

 

I am interpolating various different things with the same algorithm. Colours, positions, angles, but also things like cooldowns and health. However, this doesn't matter for the algorithm: they are just numbers being linearly interpolated, and the numbers can be 1D, 2D, 3D or 4D.

 

 

Are you inserting one keyframe at a time or multiple frames at once?  It doesn't sound like it would be too expensive to do the brute force as each key is inserted, since you'd only be looking at the adjacent keyframes.

I am indeed inserting keyframes one at a time.

 

I tried the approach of checking adjacent keyframes on insert already and it turns out that it is incorrect: with certain fluent curve-like changes removing the previous key if the new one allows it removes the curve entirely and can give (theoretically) infinite error. So I need to store lists of potentially removable keyframes and check that every frame, but that last can grow as long as the entire list of keyframes in certain cases, meaning that inserting is suddenly O(n).

Edited by Oogst

Share this post


Link to post
Share on other sites

Not knowing what your "brute force" method comprises, it's likely the most efficient. If you have several data types and you're looking for a function or routine that will handle different data type inputs to do a LERP (for instance), that function, either explicitly or "under the hood," will have to make appropriate conversions itself to use a common (4D) LERP call, or simply branch to one of 4 LERPS (1D, 2D, 3D, 4D). The GPU can do that sort of thing perhaps faster than the CPU, but for just a few calcs at a time, accessing the GPU is expensive.

 

You could also write your method directly in assembly for whatever FPU you have.

Share this post


Link to post
Share on other sites

 

I am indeed inserting keyframes one at a time.

 

I tried the approach of checking adjacent keyframes on insert already and it turns out that it is incorrect: with certain fluent curve-like changes removing the previous key if the new one allows it removes the curve entirely and can give (theoretically) infinite error. So I need to store lists of potentially removable keyframes and check that every frame, but that last can grow as long as the entire list of keyframes in certain cases, meaning that inserting is suddenly O(n).

 

 

Can you give an example, I'm afraid I don't understand the causes of the error.  Is it that if you over simplify, you lose the curve and get a line?

Share this post


Link to post
Share on other sites

It is actually pretty simple.

Try to remove the given keyframe, then sample the keytrack at the given time value of the key again.

Compare that to the version where the keyframe did not get removed.

 

Now you have an error value that gets introduced. You simply compare that error value in a way corresponding to the type of data it represents.

For a Vector3 you can just compare the distance, for a Quaternion you can do some special check etc.

 

If the error is below the threshold remove the keyframe permanently, otherwise keep it.

Iterate over all kefyrames this way. The first and last keyframe will most likely be special cases that you always need to keep.

 

Hope that helps smile.png

 

Edit: Ah I didn't see you need to add lot of keyframes in realtime. You could adapt this algorithm to that though, by checking if you can remove some of the last added keyframes. Not the last one, but say the one before, maybe batching it if you add multiple at the same time. Or already do this check while adding keyframes, it shouldn't be too hard, the principle idea stays the same :)

Edited by Buckshag

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement