-
Notifications
You must be signed in to change notification settings - Fork 107
/
database.h
732 lines (634 loc) · 24.8 KB
/
database.h
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
#pragma once
#include "common.h"
#include "vec.h"
#include "quat.h"
#include "array.h"
#include <assert.h>
#include <float.h>
#include <stdio.h>
#include <math.h>
//--------------------------------------
enum
{
BOUND_SM_SIZE = 16,
BOUND_LR_SIZE = 64,
};
struct database
{
array2d<vec3> bone_positions;
array2d<vec3> bone_velocities;
array2d<quat> bone_rotations;
array2d<vec3> bone_angular_velocities;
array1d<int> bone_parents;
array1d<int> range_starts;
array1d<int> range_stops;
array2d<float> features;
array1d<float> features_offset;
array1d<float> features_scale;
array2d<bool> contact_states;
array2d<float> bound_sm_min;
array2d<float> bound_sm_max;
array2d<float> bound_lr_min;
array2d<float> bound_lr_max;
int nframes() const { return bone_positions.rows; }
int nbones() const { return bone_positions.cols; }
int nranges() const { return range_starts.size; }
int nfeatures() const { return features.cols; }
int ncontacts() const { return contact_states.cols; }
};
void database_load(database& db, const char* filename)
{
FILE* f = fopen(filename, "rb");
assert(f != NULL);
array2d_read(db.bone_positions, f);
array2d_read(db.bone_velocities, f);
array2d_read(db.bone_rotations, f);
array2d_read(db.bone_angular_velocities, f);
array1d_read(db.bone_parents, f);
array1d_read(db.range_starts, f);
array1d_read(db.range_stops, f);
array2d_read(db.contact_states, f);
fclose(f);
}
void database_save_matching_features(const database& db, const char* filename)
{
FILE* f = fopen(filename, "wb");
assert(f != NULL);
array2d_write(db.features, f);
array1d_write(db.features_offset, f);
array1d_write(db.features_scale, f);
fclose(f);
}
// When we add an offset to a frame in the database there is a chance
// it will go out of the relevant range so here we can clamp it to
// the last frame of that range.
int database_trajectory_index_clamp(database& db, int frame, int offset)
{
for (int i = 0; i < db.nranges(); i++)
{
if (frame >= db.range_starts(i) && frame < db.range_stops(i))
{
return clamp(frame + offset, db.range_starts(i), db.range_stops(i) - 1);
}
}
assert(false);
return -1;
}
//--------------------------------------
void normalize_feature(
slice2d<float> features,
slice1d<float> features_offset,
slice1d<float> features_scale,
const int offset,
const int size,
const float weight = 1.0f)
{
// First compute what is essentially the mean
// value for each feature dimension
for (int j = 0; j < size; j++)
{
features_offset(offset + j) = 0.0f;
}
for (int i = 0; i < features.rows; i++)
{
for (int j = 0; j < size; j++)
{
features_offset(offset + j) += features(i, offset + j) / features.rows;
}
}
// Now compute the variance of each feature dimension
array1d<float> vars(size);
vars.zero();
for (int i = 0; i < features.rows; i++)
{
for (int j = 0; j < size; j++)
{
vars(j) += squaref(features(i, offset + j) - features_offset(offset + j)) / features.rows;
}
}
// We compute the overall std of the feature as the average
// std across all dimensions
float std = 0.0f;
for (int j = 0; j < size; j++)
{
std += sqrtf(vars(j)) / size;
}
// Features with no variation can have zero std which is
// almost always a bug.
assert(std > 0.0);
// The scale of a feature is just the std divided by the weight
for (int j = 0; j < size; j++)
{
features_scale(offset + j) = std / weight;
}
// Using the offset and scale we can then normalize the features
for (int i = 0; i < features.rows; i++)
{
for (int j = 0; j < size; j++)
{
features(i, offset + j) = (features(i, offset + j) - features_offset(offset + j)) / features_scale(offset + j);
}
}
}
void denormalize_features(
slice1d<float> features,
const slice1d<float> features_offset,
const slice1d<float> features_scale)
{
for (int i = 0; i < features.size; i++)
{
features(i) = (features(i) * features_scale(i)) + features_offset(i);
}
}
//--------------------------------------
// Here I am using a simple recursive version of forward kinematics
void forward_kinematics(
vec3& bone_position,
quat& bone_rotation,
const slice1d<vec3> bone_positions,
const slice1d<quat> bone_rotations,
const slice1d<int> bone_parents,
const int bone)
{
if (bone_parents(bone) != -1)
{
vec3 parent_position;
quat parent_rotation;
forward_kinematics(
parent_position,
parent_rotation,
bone_positions,
bone_rotations,
bone_parents,
bone_parents(bone));
bone_position = quat_mul_vec3(parent_rotation, bone_positions(bone)) + parent_position;
bone_rotation = quat_mul(parent_rotation, bone_rotations(bone));
}
else
{
bone_position = bone_positions(bone);
bone_rotation = bone_rotations(bone);
}
}
// Forward kinematics but also compute the velocities
void forward_kinematics_velocity(
vec3& bone_position,
vec3& bone_velocity,
quat& bone_rotation,
vec3& bone_angular_velocity,
const slice1d<vec3> bone_positions,
const slice1d<vec3> bone_velocities,
const slice1d<quat> bone_rotations,
const slice1d<vec3> bone_angular_velocities,
const slice1d<int> bone_parents,
const int bone)
{
//
if (bone_parents(bone) != -1)
{
vec3 parent_position;
vec3 parent_velocity;
quat parent_rotation;
vec3 parent_angular_velocity;
forward_kinematics_velocity(
parent_position,
parent_velocity,
parent_rotation,
parent_angular_velocity,
bone_positions,
bone_velocities,
bone_rotations,
bone_angular_velocities,
bone_parents,
bone_parents(bone));
bone_position = quat_mul_vec3(parent_rotation, bone_positions(bone)) + parent_position;
bone_velocity =
parent_velocity +
quat_mul_vec3(parent_rotation, bone_velocities(bone)) +
cross(parent_angular_velocity, quat_mul_vec3(parent_rotation, bone_positions(bone)));
bone_rotation = quat_mul(parent_rotation, bone_rotations(bone));
bone_angular_velocity = quat_mul_vec3(parent_rotation, bone_angular_velocities(bone)) + parent_angular_velocity;
}
else
{
bone_position = bone_positions(bone);
bone_velocity = bone_velocities(bone);
bone_rotation = bone_rotations(bone);
bone_angular_velocity = bone_angular_velocities(bone);
}
}
// Compute forward kinematics for all joints
void forward_kinematics_full(
slice1d<vec3> global_bone_positions,
slice1d<quat> global_bone_rotations,
const slice1d<vec3> local_bone_positions,
const slice1d<quat> local_bone_rotations,
const slice1d<int> bone_parents)
{
for (int i = 0; i < bone_parents.size; i++)
{
// Assumes bones are always sorted from root onwards
assert(bone_parents(i) < i);
if (bone_parents(i) == -1)
{
global_bone_positions(i) = local_bone_positions(i);
global_bone_rotations(i) = local_bone_rotations(i);
}
else
{
vec3 parent_position = global_bone_positions(bone_parents(i));
quat parent_rotation = global_bone_rotations(bone_parents(i));
global_bone_positions(i) = quat_mul_vec3(parent_rotation, local_bone_positions(i)) + parent_position;
global_bone_rotations(i) = quat_mul(parent_rotation, local_bone_rotations(i));
}
}
}
// Compute forward kinematics of just some joints using a
// mask to indicate which joints are already computed
void forward_kinematics_partial(
slice1d<vec3> global_bone_positions,
slice1d<quat> global_bone_rotations,
slice1d<bool> global_bone_computed,
const slice1d<vec3> local_bone_positions,
const slice1d<quat> local_bone_rotations,
const slice1d<int> bone_parents,
int bone)
{
if (bone_parents(bone) == -1)
{
global_bone_positions(bone) = local_bone_positions(bone);
global_bone_rotations(bone) = local_bone_rotations(bone);
global_bone_computed(bone) = true;
return;
}
if (!global_bone_computed(bone_parents(bone)))
{
forward_kinematics_partial(
global_bone_positions,
global_bone_rotations,
global_bone_computed,
local_bone_positions,
local_bone_rotations,
bone_parents,
bone_parents(bone));
}
vec3 parent_position = global_bone_positions(bone_parents(bone));
quat parent_rotation = global_bone_rotations(bone_parents(bone));
global_bone_positions(bone) = quat_mul_vec3(parent_rotation, local_bone_positions(bone)) + parent_position;
global_bone_rotations(bone) = quat_mul(parent_rotation, local_bone_rotations(bone));
global_bone_computed(bone) = true;
}
// Same but including velocity
void forward_kinematics_velocity_partial(
slice1d<vec3> global_bone_positions,
slice1d<vec3> global_bone_velocities,
slice1d<quat> global_bone_rotations,
slice1d<vec3> global_bone_angular_velocities,
slice1d<bool> global_bone_computed,
const slice1d<vec3> local_bone_positions,
const slice1d<vec3> local_bone_velocities,
const slice1d<quat> local_bone_rotations,
const slice1d<vec3> local_bone_angular_velocities,
const slice1d<int> bone_parents,
int bone)
{
if (bone_parents(bone) == -1)
{
global_bone_positions(bone) = local_bone_positions(bone);
global_bone_velocities(bone) = local_bone_velocities(bone);
global_bone_rotations(bone) = local_bone_rotations(bone);
global_bone_angular_velocities(bone) = local_bone_angular_velocities(bone);
global_bone_computed(bone) = true;
return;
}
if (!global_bone_computed(bone_parents(bone)))
{
forward_kinematics_velocity_partial(
global_bone_positions,
global_bone_velocities,
global_bone_rotations,
global_bone_angular_velocities,
global_bone_computed,
local_bone_positions,
local_bone_velocities,
local_bone_rotations,
local_bone_angular_velocities,
bone_parents,
bone_parents(bone));
}
vec3 parent_position = global_bone_positions(bone_parents(bone));
vec3 parent_velocity = global_bone_velocities(bone_parents(bone));
quat parent_rotation = global_bone_rotations(bone_parents(bone));
vec3 parent_angular_velocity = global_bone_angular_velocities(bone_parents(bone));
global_bone_positions(bone) = quat_mul_vec3(parent_rotation, local_bone_positions(bone)) + parent_position;
global_bone_velocities(bone) =
parent_velocity +
quat_mul_vec3(parent_rotation, local_bone_velocities(bone)) +
cross(parent_angular_velocity, quat_mul_vec3(parent_rotation, local_bone_positions(bone)));
global_bone_rotations(bone) = quat_mul(parent_rotation, local_bone_rotations(bone));
global_bone_angular_velocities(bone) = quat_mul_vec3(parent_rotation, local_bone_angular_velocities(bone)) + parent_angular_velocity;
global_bone_computed(bone) = true;
}
//--------------------------------------
// Compute a feature for the position of a bone relative to the simulation/root bone
void compute_bone_position_feature(database& db, int& offset, int bone, float weight = 1.0f)
{
for (int i = 0; i < db.nframes(); i++)
{
vec3 bone_position;
quat bone_rotation;
forward_kinematics(
bone_position,
bone_rotation,
db.bone_positions(i),
db.bone_rotations(i),
db.bone_parents,
bone);
bone_position = quat_mul_vec3(quat_inv(db.bone_rotations(i, 0)), bone_position - db.bone_positions(i, 0));
db.features(i, offset + 0) = bone_position.x;
db.features(i, offset + 1) = bone_position.y;
db.features(i, offset + 2) = bone_position.z;
}
normalize_feature(db.features, db.features_offset, db.features_scale, offset, 3, weight);
offset += 3;
}
// Similar but for a bone's velocity
void compute_bone_velocity_feature(database& db, int& offset, int bone, float weight = 1.0f)
{
for (int i = 0; i < db.nframes(); i++)
{
vec3 bone_position;
vec3 bone_velocity;
quat bone_rotation;
vec3 bone_angular_velocity;
forward_kinematics_velocity(
bone_position,
bone_velocity,
bone_rotation,
bone_angular_velocity,
db.bone_positions(i),
db.bone_velocities(i),
db.bone_rotations(i),
db.bone_angular_velocities(i),
db.bone_parents,
bone);
bone_velocity = quat_mul_vec3(quat_inv(db.bone_rotations(i, 0)), bone_velocity);
db.features(i, offset + 0) = bone_velocity.x;
db.features(i, offset + 1) = bone_velocity.y;
db.features(i, offset + 2) = bone_velocity.z;
}
normalize_feature(db.features, db.features_offset, db.features_scale, offset, 3, weight);
offset += 3;
}
// Compute the trajectory at 20, 40, and 60 frames in the future
void compute_trajectory_position_feature(database& db, int& offset, float weight = 1.0f)
{
for (int i = 0; i < db.nframes(); i++)
{
int t0 = database_trajectory_index_clamp(db, i, 20);
int t1 = database_trajectory_index_clamp(db, i, 40);
int t2 = database_trajectory_index_clamp(db, i, 60);
vec3 trajectory_pos0 = quat_mul_vec3(quat_inv(db.bone_rotations(i, 0)), db.bone_positions(t0, 0) - db.bone_positions(i, 0));
vec3 trajectory_pos1 = quat_mul_vec3(quat_inv(db.bone_rotations(i, 0)), db.bone_positions(t1, 0) - db.bone_positions(i, 0));
vec3 trajectory_pos2 = quat_mul_vec3(quat_inv(db.bone_rotations(i, 0)), db.bone_positions(t2, 0) - db.bone_positions(i, 0));
db.features(i, offset + 0) = trajectory_pos0.x;
db.features(i, offset + 1) = trajectory_pos0.z;
db.features(i, offset + 2) = trajectory_pos1.x;
db.features(i, offset + 3) = trajectory_pos1.z;
db.features(i, offset + 4) = trajectory_pos2.x;
db.features(i, offset + 5) = trajectory_pos2.z;
}
normalize_feature(db.features, db.features_offset, db.features_scale, offset, 6, weight);
offset += 6;
}
// Same for direction
void compute_trajectory_direction_feature(database& db, int& offset, float weight = 1.0f)
{
for (int i = 0; i < db.nframes(); i++)
{
int t0 = database_trajectory_index_clamp(db, i, 20);
int t1 = database_trajectory_index_clamp(db, i, 40);
int t2 = database_trajectory_index_clamp(db, i, 60);
vec3 trajectory_dir0 = quat_mul_vec3(quat_inv(db.bone_rotations(i, 0)), quat_mul_vec3(db.bone_rotations(t0, 0), vec3(0, 0, 1)));
vec3 trajectory_dir1 = quat_mul_vec3(quat_inv(db.bone_rotations(i, 0)), quat_mul_vec3(db.bone_rotations(t1, 0), vec3(0, 0, 1)));
vec3 trajectory_dir2 = quat_mul_vec3(quat_inv(db.bone_rotations(i, 0)), quat_mul_vec3(db.bone_rotations(t2, 0), vec3(0, 0, 1)));
db.features(i, offset + 0) = trajectory_dir0.x;
db.features(i, offset + 1) = trajectory_dir0.z;
db.features(i, offset + 2) = trajectory_dir1.x;
db.features(i, offset + 3) = trajectory_dir1.z;
db.features(i, offset + 4) = trajectory_dir2.x;
db.features(i, offset + 5) = trajectory_dir2.z;
}
normalize_feature(db.features, db.features_offset, db.features_scale, offset, 6, weight);
offset += 6;
}
// Build the Motion Matching search acceleration structure. Here we
// just use axis aligned bounding boxes regularly spaced at BOUND_SM_SIZE
// and BOUND_LR_SIZE frames
void database_build_bounds(database& db)
{
int nbound_sm = ((db.nframes() + BOUND_SM_SIZE - 1) / BOUND_SM_SIZE);
int nbound_lr = ((db.nframes() + BOUND_LR_SIZE - 1) / BOUND_LR_SIZE);
db.bound_sm_min.resize(nbound_sm, db.nfeatures());
db.bound_sm_max.resize(nbound_sm, db.nfeatures());
db.bound_lr_min.resize(nbound_lr, db.nfeatures());
db.bound_lr_max.resize(nbound_lr, db.nfeatures());
db.bound_sm_min.set(+FLT_MAX);
db.bound_sm_max.set(-FLT_MAX);
db.bound_lr_min.set(+FLT_MAX);
db.bound_lr_max.set(-FLT_MAX);
for (int i = 0; i < db.nframes(); i++)
{
int i_sm = i / BOUND_SM_SIZE;
int i_lr = i / BOUND_LR_SIZE;
for (int j = 0; j < db.nfeatures(); j++)
{
db.bound_sm_min(i_sm, j) = minf(db.bound_sm_min(i_sm, j), db.features(i, j));
db.bound_sm_max(i_sm, j) = maxf(db.bound_sm_max(i_sm, j), db.features(i, j));
db.bound_lr_min(i_lr, j) = minf(db.bound_lr_min(i_lr, j), db.features(i, j));
db.bound_lr_max(i_lr, j) = maxf(db.bound_lr_max(i_lr, j), db.features(i, j));
}
}
}
// Build all motion matching features and acceleration structure
void database_build_matching_features(
database& db,
const float feature_weight_foot_position,
const float feature_weight_foot_velocity,
const float feature_weight_hip_velocity,
const float feature_weight_trajectory_positions,
const float feature_weight_trajectory_directions)
{
int nfeatures =
3 + // Left Foot Position
3 + // Right Foot Position
3 + // Left Foot Velocity
3 + // Right Foot Velocity
3 + // Hip Velocity
6 + // Trajectory Positions 2D
6 ; // Trajectory Directions 2D
db.features.resize(db.nframes(), nfeatures);
db.features_offset.resize(nfeatures);
db.features_scale.resize(nfeatures);
int offset = 0;
compute_bone_position_feature(db, offset, Bone_LeftFoot, feature_weight_foot_position);
compute_bone_position_feature(db, offset, Bone_RightFoot, feature_weight_foot_position);
compute_bone_velocity_feature(db, offset, Bone_LeftFoot, feature_weight_foot_velocity);
compute_bone_velocity_feature(db, offset, Bone_RightFoot, feature_weight_foot_velocity);
compute_bone_velocity_feature(db, offset, Bone_Hips, feature_weight_hip_velocity);
compute_trajectory_position_feature(db, offset, feature_weight_trajectory_positions);
compute_trajectory_direction_feature(db, offset, feature_weight_trajectory_directions);
assert(offset == nfeatures);
database_build_bounds(db);
}
// Motion Matching search function essentially consists
// of comparing every feature vector in the database,
// against the query feature vector, first checking the
// query distance to the axis aligned bounding boxes used
// for the acceleration structure.
void motion_matching_search(
int& __restrict__ best_index,
float& __restrict__ best_cost,
const slice1d<int> range_starts,
const slice1d<int> range_stops,
const slice2d<float> features,
const slice1d<float> features_offset,
const slice1d<float> features_scale,
const slice2d<float> bound_sm_min,
const slice2d<float> bound_sm_max,
const slice2d<float> bound_lr_min,
const slice2d<float> bound_lr_max,
const slice1d<float> query_normalized,
const float transition_cost,
const int ignore_range_end,
const int ignore_surrounding)
{
int nfeatures = query_normalized.size;
int nranges = range_starts.size;
int curr_index = best_index;
// Find cost for current frame
if (best_index != -1)
{
best_cost = 0.0;
for (int i = 0; i < nfeatures; i++)
{
best_cost += squaref(query_normalized(i) - features(best_index, i));
}
}
float curr_cost = 0.0f;
// Search rest of database
for (int r = 0; r < nranges; r++)
{
// Exclude end of ranges from search
int i = range_starts(r);
int range_end = range_stops(r) - ignore_range_end;
while (i < range_end)
{
// Find index of current and next large box
int i_lr = i / BOUND_LR_SIZE;
int i_lr_next = (i_lr + 1) * BOUND_LR_SIZE;
// Find distance to box
curr_cost = transition_cost;
for (int j = 0; j < nfeatures; j++)
{
curr_cost += squaref(query_normalized(j) - clampf(query_normalized(j),
bound_lr_min(i_lr, j), bound_lr_max(i_lr, j)));
if (curr_cost >= best_cost)
{
break;
}
}
// If distance is greater than current best jump to next box
if (curr_cost >= best_cost)
{
i = i_lr_next;
continue;
}
// Check against small box
while (i < i_lr_next && i < range_end)
{
// Find index of current and next small box
int i_sm = i / BOUND_SM_SIZE;
int i_sm_next = (i_sm + 1) * BOUND_SM_SIZE;
// Find distance to box
curr_cost = transition_cost;
for (int j = 0; j < nfeatures; j++)
{
curr_cost += squaref(query_normalized(j) - clampf(query_normalized(j),
bound_sm_min(i_sm, j), bound_sm_max(i_sm, j)));
if (curr_cost >= best_cost)
{
break;
}
}
// If distance is greater than current best jump to next box
if (curr_cost >= best_cost)
{
i = i_sm_next;
continue;
}
// Search inside small box
while (i < i_sm_next && i < range_end)
{
// Skip surrounding frames
if (curr_index != - 1 && abs(i - curr_index) < ignore_surrounding)
{
i++;
continue;
}
// Check against each frame inside small box
curr_cost = transition_cost;
for (int j = 0; j < nfeatures; j++)
{
curr_cost += squaref(query_normalized(j) - features(i, j));
if (curr_cost >= best_cost)
{
break;
}
}
// If cost is lower than current best then update best
if (curr_cost < best_cost)
{
best_index = i;
best_cost = curr_cost;
}
i++;
}
}
}
}
}
// Search database
void database_search(
int& best_index,
float& best_cost,
const database& db,
const slice1d<float> query,
const float transition_cost = 0.0f,
const int ignore_range_end = 20,
const int ignore_surrounding = 20)
{
// Normalize Query
array1d<float> query_normalized(db.nfeatures());
for (int i = 0; i < db.nfeatures(); i++)
{
query_normalized(i) = (query(i) - db.features_offset(i)) / db.features_scale(i);
}
// Search
motion_matching_search(
best_index,
best_cost,
db.range_starts,
db.range_stops,
db.features,
db.features_offset,
db.features_scale,
db.bound_sm_min,
db.bound_sm_max,
db.bound_lr_min,
db.bound_lr_max,
query_normalized,
transition_cost,
ignore_range_end,
ignore_surrounding);
}