-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathKeyLookup.cpp
More file actions
469 lines (440 loc) · 12.8 KB
/
Copy pathKeyLookup.cpp
File metadata and controls
469 lines (440 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include "common.h"
int SECONDSOFANIMATION = 10;
static const int NUM_QUERIES = 1000;
static const int NUM_NODES = 145;
static int minFrameRate = 10;
static int maxFrameRate = 15;
static const float RATIO_OF_NON_SCALING = 0.85f;
//BEGIN
// basic animation key lookup
struct FullAnimKey {
float time;
Vec3 translation;
Vec3 scale;
Vec4 rotation; // sijk quaternion
};
struct FullAnim {
int numKeys;
FullAnimKey *keys;
FullAnimKey GetKeyAtTimeBinary( float t ) {
int l = 0, h = numKeys-1;
int m = (l+h) / 2;
while( l < h ) {
if( t < keys[m].time ) {
h = m-1;
} else {
l = m;
}
m = (l+h+1) / 2;
}
return keys[m];
}
FullAnimKey GetKeyAtTimeLinear( float t ) {
int i = 0;
while( i < numKeys ) {
if( keys[i].time > t ) {
--i;
break;
}
++i;
}
if( i < 0 )
return keys[0];
return keys[i];
}
};
// looking up keys by time
struct DataOnlyAnimKey {
Vec3 translation;
Vec3 scale;
Vec4 rotation; // sijk quaternion
};
struct DataOnlyAnim {
int numKeys;
float *keyTime;
DataOnlyAnimKey *keys;
DataOnlyAnimKey GetKeyAtTimeBinary( float t ) {
int l = 0, h = numKeys-1;
int m = (l+h) / 2;
while( l < h ) {
if( t < keyTime[m] ) {
h = m-1;
} else {
l = m;
}
m = (l+h+1) / 2;
}
return keys[m];
}
DataOnlyAnimKey GetKeyAtTimeLinear( float t ) {
int i = 0;
while( i < numKeys ) {
if( keyTime[i] > t ) {
--i;
break;
}
++i;
}
if( i < 0 )
return keys[0];
return keys[i];
}
};
struct ClumpedAnim {
int numKeys;
float *keyTime;
DataOnlyAnimKey *keys;
static const int numPrefetchedKeyTimes = (64-sizeof(int)-sizeof(float*)-sizeof(DataOnlyAnimKey*))/sizeof(float);
static const int keysPerLump = 64/sizeof(float);
float firstStage[numPrefetchedKeyTimes];
DataOnlyAnimKey GetKeyAtTimeBinary( float t ) {
for( int start = 0; start < numPrefetchedKeyTimes; ++start ) {
if( firstStage[start] > t ) {
int l = start*keysPerLump;
int h = l + keysPerLump;
h = h > numKeys ? numKeys : h;
return GetKeyAtTimeBinary( t, l, h+1 );
}
}
return GetKeyAtTimeBinary( t, numPrefetchedKeyTimes*keysPerLump, numKeys );
}
DataOnlyAnimKey GetKeyAtTimeBinary( float t, int l, int h ) {
int m = (l+h) / 2;
while( l < h ) {
if( t < keyTime[m] ) {
h = m-1;
} else {
l = m;
}
m = (l+h+1) / 2;
}
return keys[m];
}
DataOnlyAnimKey GetKeyAtTimeLinear( float t ) {
for( int start = 0; start < numPrefetchedKeyTimes; ++start ) {
if( firstStage[start] > t ) {
int l = start*keysPerLump;
return GetKeyAtTimeLinear( t, l );
}
}
return GetKeyAtTimeLinear( t, numPrefetchedKeyTimes*keysPerLump );
}
DataOnlyAnimKey GetKeyAtTimeLinear( float t, int startIndex ) {
int i = startIndex;
while( i < numKeys ) {
if( keyTime[i] > t ) {
--i;
break;
}
++i;
}
if( i < 0 )
return keys[0];
return keys[i];
}
};
//END
struct TranslationKey { float t; Vec3 pos; };
struct RotationKey { float t; Vec4 quat; };
struct ScaleKey { float t; Vec3 scale; };
typedef std::vector<TranslationKey> TKVec;
typedef std::vector<RotationKey> RKVec;
typedef std::vector<ScaleKey> SKVec;
struct AnimData {
TKVec tKeys;
RKVec rKeys;
SKVec sKeys;
};
AnimData PrepareData( uint32_t seed = 1234, bool bTranslationAnimation = false, bool bScaleAnimation = false ) {
TKVec tKeys;
RKVec rKeys;
SKVec sKeys;
pcg32_random_t rng;
pcg32_srandom_r(&rng, seed, 5678);
// Make a ten second long animation, with keys at various times
int secondsOfAnimation = SECONDSOFANIMATION;
// How many keys? At 60fps, motion capture can get very high framerates, but
// maybe about 10-15 keyframes per second is about right.
int keyCount = pcg32_random_r_range(&rng,
minFrameRate*secondsOfAnimation,
maxFrameRate*secondsOfAnimation);
//printf( "Keys[%i] %s translation, %s scale\n",
//keyCount,
//bTranslationAnimation?"with":"without",
//bScaleAnimation?"with":"without"
//);
#ifndef NDEBUG
int sharedKeys = 1;
if( bTranslationAnimation || bScaleAnimation ) {
// if there is animation on the translation or scale components, then
// somewhere between 2 and all of the keyframes will have the same timing.
// (always share first and last keyframes times)
sharedKeys = pcg32_random_r_range(&rng, 2, keyCount+1);
assert( sharedKeys <= keyCount ); // just ensuring my random gen is actually behaving
}
#endif
// generate some keyframe data
for( int i = 0; i < keyCount; ++i ) {
Vec4 r; // simulate a quaternion
r.x = pcg32_random_r_rangef(&rng, -1, 1);
r.y = pcg32_random_r_rangef(&rng, -1, 1);
r.z = pcg32_random_r_rangef(&rng, -1, 1);
r.w = 1.0f - sqrt( dot(r,r));
rKeys.push_back(RotationKey{-1,r});
if( i == 0 || bTranslationAnimation ) {
Vec3 t;
t.x = pcg32_random_r_rangef(&rng, -10, 10);
t.y = pcg32_random_r_rangef(&rng, -10, 10);
t.z = pcg32_random_r_rangef(&rng, -10, 10);
tKeys.push_back(TranslationKey{-1,t});
}
if( i == 0 || bScaleAnimation ) {
Vec3 s;
s.x = pcg32_random_r_rangef(&rng, 0.1f, 2.0f);
s.y = pcg32_random_r_rangef(&rng, 0.1f, 2.0f);
s.z = pcg32_random_r_rangef(&rng, 0.1f, 2.0f);
sKeys.push_back(ScaleKey{-1,s});
}
}
if(!bTranslationAnimation) {
// set an initial keyframe at time 0
tKeys[0].t = 0;
}
if(!bScaleAnimation) {
// set an initial keyframe at time 0
sKeys[0].t = 0;
}
std::vector<float> keyTimes;
keyTimes.push_back(0.0f);
keyTimes.push_back(secondsOfAnimation);
// now generate some basic key times
for( int i = 2; i < keyCount; ++i ) {
keyTimes.push_back(
pcg32_random_r_rangef(&rng, 0.1f, secondsOfAnimation -0.1f)
); // arbitrary reduction of range
}
std::sort(keyTimes.begin(), keyTimes.end());
// apply these times to the rotation keys
for( int i = 0; i < keyCount; ++i ) {
rKeys[i].t = keyTimes[i];
if( bTranslationAnimation ) {
tKeys[i].t = keyTimes[i];
}
if( bScaleAnimation ) {
sKeys[i].t = keyTimes[i];
}
}
#if 0 // unsharing the key times
int unsharedKeys = keyCount - sharedKeys;
for( int i = 0; i < unsharedKeys; ++i ) {
int keyToJitter = pcg32_random_r_range(&rng, 1, keyCount-1 ); // any key between the start and end
// we're only after making sure they aren't the exact same time value, so just jitter them.
if( bTranslationAnimation ) {
tKeys[i].t = pcg32_random_r_rangef(&rng, tKeys[i-1].t, tKeys[i+1].t );
}
if( bScaleAnimation ) {
sKeys[i].t = pcg32_random_r_rangef(&rng, sKeys[i-1].t, sKeys[i+1].t );
}
}
#endif
#ifndef NDEBUG
// check we did all good
for( auto r : rKeys ) {
assert( r.t >= 0.0f );
assert( r.t <= secondsOfAnimation );
}
for( auto t : tKeys ) {
assert( t.t >= 0.0f );
assert( t.t <= secondsOfAnimation );
}
for( auto s : sKeys ) {
assert( s.t >= 0.0f );
assert( s.t <= secondsOfAnimation );
}
#endif
return AnimData { tKeys, rKeys, sKeys, };
}
void FromData( FullAnim &out, const AnimData &in ) {
std::vector<FullAnimKey> outkeys;
for( size_t i = 0; i < in.rKeys.size(); ++i ) {
auto r = in.rKeys[i];
auto t = in.tKeys.size()==1?in.tKeys[0]:in.tKeys[i];
auto s = in.sKeys.size()==1?in.sKeys[0]:in.sKeys[i];
FullAnimKey ak;
ak.time = r.t;
ak.rotation = r.quat;
ak.translation = t.pos;
ak.scale = s.scale;
outkeys.push_back( ak );
}
out.keys = new FullAnimKey[outkeys.size()];
std::copy( outkeys.begin(), outkeys.end(), out.keys );
out.numKeys = outkeys.size();
}
void FromData( DataOnlyAnim &out, const AnimData &in ) {
std::vector<DataOnlyAnimKey> outkeys;
std::vector<float> times;
for( size_t i = 0; i < in.rKeys.size(); ++i ) {
auto r = in.rKeys[i];
auto t = in.tKeys.size()==1?in.tKeys[0]:in.tKeys[i];
auto s = in.sKeys.size()==1?in.sKeys[0]:in.sKeys[i];
times.push_back( r.t );
DataOnlyAnimKey ak;
ak.rotation = r.quat;
ak.translation = t.pos;
ak.scale = s.scale;
outkeys.push_back( ak );
}
out.keys = new DataOnlyAnimKey[outkeys.size()];
std::copy( outkeys.begin(), outkeys.end(), out.keys );
out.keyTime = new float[times.size()];
std::copy( times.begin(), times.end(), out.keyTime );
out.numKeys = outkeys.size();
}
void FromData( ClumpedAnim &out, const AnimData &in ) {
std::vector<DataOnlyAnimKey> outkeys;
std::vector<float> times;
for( size_t i = 0; i < in.rKeys.size(); ++i ) {
auto r = in.rKeys[i];
auto t = in.tKeys.size()==1?in.tKeys[0]:in.tKeys[i];
auto s = in.sKeys.size()==1?in.sKeys[0]:in.sKeys[i];
times.push_back( r.t );
DataOnlyAnimKey ak;
ak.rotation = r.quat;
ak.translation = t.pos;
ak.scale = s.scale;
outkeys.push_back( ak );
}
out.keys = new DataOnlyAnimKey[outkeys.size()];
std::copy( outkeys.begin(), outkeys.end(), out.keys );
out.keyTime = new float[times.size()];
std::copy( times.begin(), times.end(), out.keyTime );
out.numKeys = outkeys.size();
// fill out the precached times
for( size_t i = 0; i < ClumpedAnim::numPrefetchedKeyTimes; ++i ) {
size_t targetIndex = (i+1)*ClumpedAnim::keysPerLump;
if( targetIndex < times.size() ) {
out.firstStage[i] = times[targetIndex];
} else {
out.firstStage[i] = HUGE_VALF;
}
}
}
struct HierarchyOutputData {
struct NodeData {
Vec3 translation;
Vec3 scale;
Vec4 rotation; // sijk quaternion
};
NodeData nodeData[NUM_NODES];
};
template<typename AnimType>
struct TestHierarchy {
AnimType animForNode[NUM_NODES];
void SetupNode( int node, const AnimData &ad ) {
FromData( animForNode[node], ad );
}
HierarchyOutputData GetAtTBinary( float t ) {
HierarchyOutputData hod;
for( int i = 0; i < NUM_NODES; ++i ) {
auto keyData = animForNode[i].GetKeyAtTimeBinary( t );
hod.nodeData[i].translation = keyData.translation;
hod.nodeData[i].rotation = keyData.rotation;
hod.nodeData[i].scale = keyData.scale;
}
return hod;
}
HierarchyOutputData GetAtTLinear( float t ) {
HierarchyOutputData hod;
for( int i = 0; i < NUM_NODES; ++i ) {
auto keyData = animForNode[i].GetKeyAtTimeLinear( t );
hod.nodeData[i].translation = keyData.translation;
hod.nodeData[i].rotation = keyData.rotation;
hod.nodeData[i].scale = keyData.scale;
}
return hod;
}
};
volatile HierarchyOutputData output_data;
struct Data {
TestHierarchy<FullAnim> fullAnimHierarchy;
TestHierarchy<DataOnlyAnim> dataOnlyHierarchy;
TestHierarchy<ClumpedAnim> clumpedHierarchy;
std::vector<float> queries;
Data() {
AnimData ad = PrepareData( 1000, true, false );
fullAnimHierarchy.SetupNode( 0, ad );
dataOnlyHierarchy.SetupNode( 0, ad );
clumpedHierarchy.SetupNode( 0, ad );
for( int i = 1; i < NUM_NODES; ++i ) {
// let's have some scaling on the last few nodes
bool bScaled = i>(RATIO_OF_NON_SCALING*NUM_NODES);
ad = PrepareData( 1000+i, false, bScaled );
fullAnimHierarchy.SetupNode( i, ad );
dataOnlyHierarchy.SetupNode( i, ad );
clumpedHierarchy.SetupNode( i, ad );
}
{
pcg32_random_t rng;
pcg32_srandom_r(&rng, 1234312, 55544);
for( int i = 0; i < NUM_QUERIES; ++i ) {
float t = pcg32_random_r_rangef(&rng, -0.5f, SECONDSOFANIMATION + 0.5f );
queries.push_back( t );
}
}
}
};
Data *gData;
void TestFullAnimBinary() {
for( auto t : gData->queries ) {
HierarchyOutputData hod = gData->fullAnimHierarchy.GetAtTBinary(t);
memcpy( (void*)&output_data, &hod, sizeof( output_data ) );
}
}
void TestFullAnimLinear() {
for( auto t : gData->queries ) {
HierarchyOutputData hod = gData->fullAnimHierarchy.GetAtTLinear(t);
memcpy( (void*)&output_data, &hod, sizeof( output_data ) );
}
}
void TestDataOnlyBinary() {
for( auto t : gData->queries ) {
HierarchyOutputData hod = gData->dataOnlyHierarchy.GetAtTBinary(t);
memcpy( (void*)&output_data, &hod, sizeof( output_data ) );
}
}
void TestDataOnlyLinear() {
for( auto t : gData->queries ) {
HierarchyOutputData hod = gData->dataOnlyHierarchy.GetAtTLinear(t);
memcpy( (void*)&output_data, &hod, sizeof( output_data ) );
}
}
void TestClumpedBinary() {
for( auto t : gData->queries ) {
HierarchyOutputData hod = gData->clumpedHierarchy.GetAtTBinary(t);
memcpy( (void*)&output_data, &hod, sizeof( output_data ) );
}
}
void TestClumpedLinear() {
for( auto t : gData->queries ) {
HierarchyOutputData hod = gData->clumpedHierarchy.GetAtTLinear(t);
memcpy( (void*)&output_data, &hod, sizeof( output_data ) );
}
}
int main() {
Data data; gData = &data;
Test tests[] = {
(Test){ TestFullAnimBinary, "Full anim key - binary search" },
(Test){ TestDataOnlyBinary, "Data only key - binary search" },
(Test){ TestClumpedBinary, "Pre-indexed - binary search" },
(Test){ TestFullAnimLinear, "Full anim key - linear search" },
(Test){ TestDataOnlyLinear, "Data only key - linear search" },
(Test){ TestClumpedLinear, "Pre-indexed - linear search" },
};
printf( "Animation key lookup tests\n" );
printf( "Clumped precached keys = %i clumps of %i keys (max key = %i)\n\n", ClumpedAnim::numPrefetchedKeyTimes, ClumpedAnim::keysPerLump, ClumpedAnim::numPrefetchedKeyTimes * ClumpedAnim::keysPerLump );
printf( "Each hierarchy has %i nodes.\n", NUM_NODES );
printf( "Each hierarchy is queried %i times\n", NUM_QUERIES );
RunTests( tests );
return 0;
}