Jump to content

  • Log In with Google      Sign In   
  • Create Account


Keyframe removal algorithm


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Oogst   Members   -  Reputation: 360

Like
1Likes
Like

Posted 20 February 2014 - 01:10 PM

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! :)


My dev blog
Ronimo Games (my game dev company)
Awesomenauts (2D MOBA for Steam/PS4/PS3/360)
Swords & Soldiers (2D RTS for Wii/PS3/Steam/mobile)

Swords & Soldiers 2 (coming to WiiU)
Proun (abstract racing game for PC, coming to mobile)
Cello Fortress (live performance game controlled by cello)


Sponsor:

#2 ferrous   Members   -  Reputation: 1946

Like
0Likes
Like

Posted 20 February 2014 - 07:24 PM

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?



#3 Buckeye   Crossbones+   -  Reputation: 4589

Like
0Likes
Like

Posted 20 February 2014 - 08:08 PM


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?


Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.


#4 Oogst   Members   -  Reputation: 360

Like
0Likes
Like

Posted 21 February 2014 - 02:17 AM

 


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, 21 February 2014 - 02:18 AM.

My dev blog
Ronimo Games (my game dev company)
Awesomenauts (2D MOBA for Steam/PS4/PS3/360)
Swords & Soldiers (2D RTS for Wii/PS3/Steam/mobile)

Swords & Soldiers 2 (coming to WiiU)
Proun (abstract racing game for PC, coming to mobile)
Cello Fortress (live performance game controlled by cello)


#5 Buckeye   Crossbones+   -  Reputation: 4589

Like
0Likes
Like

Posted 21 February 2014 - 06:20 AM

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.


Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.


#6 ferrous   Members   -  Reputation: 1946

Like
0Likes
Like

Posted 21 February 2014 - 05:58 PM

 

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?



#7 Buckshag   Members   -  Reputation: 518

Like
0Likes
Like

Posted 22 February 2014 - 02:34 PM

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, 22 February 2014 - 02:46 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS