*were*the same before the conversion, but after the conversion (due to round-off error and various technical reasons) become

*almost*the same.

Started by mv348, Dec 21 2012 12:07 AM

8 replies to this topic

Posted 21 December 2012 - 12:07 AM

I have successfully implemented an exporter to load FBX models and export to my own custom format. The downside is there are a lot of redundant key-frame transformations. Rather, I end up with transformations that *were* the same before the conversion, but after the conversion (due to round-off error and various technical reasons) become* almost* the same.

So I want to create some optimization functions to reduce the number of key-frames stored in memory. (Note, it doesn't matter so much if this optimizer is fast since it is only an exporter, it isn't executed during the game)

The general idea is that if two transformations, are 'close enough' together, then they should be replaced with the same transformation.

For example, the two vectors A =(100,0,0) and B=(100,0.00002, 7.0e-18) are essentially the same and therefore we should delete B from memory and put A in place of B. (or a pointer to A in place of B, you get the idea)

So my question is how does one determine if two elements (say two vectors) can be treated as identical?

My idea goes something like this: When comparing two vectors A and B, take the norm (or length) of A and multiply by some fixed small value (call it epsilon) then if all entries in A and B differ by less than Norm(A)*epsilon then they are considered identical.

For my setup it appears that an epsilon of 10^(-5) is as small as I can go to avoid round-off error creating redundant key-frames. Though I suppose I could go lower, and just let a few extra key-frames sneak in. Shouldn't be that big of a deal.

What do you think of this approach?

And what of quaternions? In this case I have the luxury that all vectors have the same norm. So should I just check to see if all elements differ by less than some fixed epsilon?

Thanks,

-MV

Posted 21 December 2012 - 09:57 AM

This can be a bit tricky. You can't just say "two vectors are almost the same so they should be deleted" because if you apply this same rule to all your vectors one after the other, then you will end up with no vectors!...cause they will all be close to the one that precedes it.

So instead I would suggest adding each vector together, from start to end, till the angle between the last direction and the resulting direction have deviated enough to warrant a new key frame. Basically, if the last vector plus the new vector point in the same direction, then merge the points.

Posted 21 December 2012 - 05:01 PM

Thanks for your response, Nyssa!

I understand the issue you point out but what you are proposing appears to suffer from the same issue. You say 'if they have deviated enough to warrant a new keyframe' which is exactly the same as what I am doing. The only difference (if I understand your method properly) is you are measuring the direction of (A+B) relative to A, and I am measuring the components of A vs. the components of B. The difference in direction of A from (A+B) is just as likely to be too small and incorrectly eliminate a keyframe. I think its just a question of how strict you are with the comparisons. i.e. how much is 'enough' to warrant a new keyframe.

Moreover, your method only takes direction into account and would incorrectly merge the vectors (1,1,1) and (2,2,2) since their sum (3,3,3) is in the same direction as (1,1,1). Unless I misunderstand your approach, that is.

Posted 21 December 2012 - 05:49 PM

Moreover, your method only takes direction into account and would incorrectly merge the vectors (1,1,1) and (2,2,2) since their sum (3,3,3) is in the same direction as (1,1,1).

That's correct. Sorry, I shouldn't have been talking about adding vectors together. Lets say you have 4 points (1,1,1), (2,2,2), (3,3,3), (4,4,4). If you place these points on a graph then the resulting line would be straight. Therefore there is no need for points (2,2,2) and (3,3,3) so you would just keep keys (1,1,1) and (4,4,4).

If however we have the points (1,1,1), (2,2,2), (3,-20,3), (4,4,4) you would need to keep all the points otherwise your animations won't follow the correct paths.

You would always store the first keyframe and the last keyframe anyway so that your animation runs from the first position to the end position correctly. Then you would make the direction from A to B the first comparison direction. Then test the direction from A to C. If the direction is about the same as the first comparison direction then throw away B. The next test would be from A to D and so on....

If the direction from A to C wasn't in the same direction as A to B then you would need to keep B. Because without this point your animations wouldn't follow the correct path. The direction from B to C would become your new comparison direction and you would repeat the above test for B to D and so on...

**Edited by Nyssa, 21 December 2012 - 05:54 PM.**

Posted 22 December 2012 - 09:38 AM

Yes! This is part of what I was planning after the next step. After coming up with a suitable comparison function, the next step is to use it to A. eliminate duplicate key frames, and B. eliminate key frames that are equivalent to interpolation. I had essentially the same algorithm in mind, except I would do the following:

For a key-frame B at time t, between key-frames A and C: interpolate A and C and evaluate it at t. Now call Compare ( B, interpolate(A,C,t)) and eliminate B if they are close enough.

Posted 22 December 2012 - 02:09 PM

They would only be straight if they were timed equally far apart.

That's correct. Sorry, I shouldn't have been talking about adding vectors together. Lets say you have 4 points (1,1,1), (2,2,2), (3,3,3), (4,4,4). If you place these points on a graph then the resulting line would be straight. Therefore there is no need for points (2,2,2) and (3,3,3) so you would just keep keys (1,1,1) and (4,4,4).

Just like having (1,1,1) and then (1,1,1) and then (10,10,10). The extra (1,1,1) is there to hold the pose for a specific time frame so the first (1,1,1) doesn't start immediately interpolating towards (10,10,10). This means the second (1,1,1) is not an unneeded duplicate. Removing it breaks the animation.

Posted 22 December 2012 - 07:30 PM

They would only be straight if they were timed equally far apart.

That's true Daaark.

mv, are the errors you're talking about also occurring in a frames time stamp? If you have multiple frames with the same time stamp you could simply remove any duplicates?

Posted 22 December 2012 - 08:25 PM

I have had to measure the difference between two vectors in the past, and this is a formula that works well:

relative_difference(v,w) := length(v-w)/sqrt(length(v)*length(w))

If you want to compare that quantity to a threshold, you can compute the fourth power --which doesn't involve any square roots-- and compare that to the fourth power of the threshold.

relative_difference(v,w) := length(v-w)/sqrt(length(v)*length(w))

If you want to compare that quantity to a threshold, you can compute the fourth power --which doesn't involve any square roots-- and compare that to the fourth power of the threshold.

Posted 24 December 2012 - 08:00 AM

Don't compare the keyframes, compare the differences between the frames (and the key can be removed if the two differences are almost the same). Comparing dot products is usually better for quats.

bool canRemoveMiddleFrame(vec3 v0, vec3 v1, vec3 v2, float diff) { vec3 va = v1 - v0; vec3 vb = v2 - v1; vec3 vc = va - vb; return (abs(vc.x) < diff) && (abs(vc.y) < diff) && (abs(vc.z) < diff); } bool canRemoveMiddleFrame(quat v0, quat v1, quat v2, float diff) { quat va = v1 * invert(v0); quat vb = v2 * invert(v1); float difference = dot(va, vb); return abs( difference - 1 ) < diff; }

**Edited by RobTheBloke, 24 December 2012 - 08:01 AM.**