forked from orangeduck/Motion-Matching
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.h
1147 lines (977 loc) · 39.2 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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#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>
#include <vector>
//--------------------------------------
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;
}
int database_pushed_index_clamp(int frame, int offset)
{
std::vector<int> start_frame = {0, 53, 115, 189, 290, 395, 485, 568, 687, 745, 825, 917, 1034, 1236, 1310};
std::vector<int> stop_frame = {53, 115, 189, 290, 395, 485, 568, 687, 745, 825, 917, 1034, 1236, 1310, 1705};
for (int i = 0; i < stop_frame.size(); i++)
{
if (frame >= start_frame[i] && frame < stop_frame[i])
{
return clamp(frame + offset, start_frame[i], stop_frame[i] - 1);
}
}
assert(false);
return -1;
}
//--------------------------------------
void normalize_feature_vector(
slice2d<float> features,
slice1d<float> features_offset,
slice1d<float> features_scale,
const int offset,
const int size,
std::vector<float> weights)
{
// 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 / weights[j];
}
// 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 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;
}
//--------------------------------------
// void compute_motion_idx_feature(database& db, int& offset, float weight)
// {
// for (int i = 0; i < db.nframes(); i++) {
// int stop_index = -1;
// for (int j = 1; j <= 7; j += 2) {
// if (i <= db.range_stops(j)) {
// stop_index = j;
// break;
// }
// }
// switch(stop_index) {
// case 1:
// db.features(i, offset + 0) = 0;
// break;
// case 3:
// db.features(i, offset + 0) = 1;
// break;
// case 5:
// db.features(i, offset + 0) = 2;
// break;
// case 7:
// db.features(i, offset + 0) = 3;
// break;
// // case 9:
// // db.features(i, offset + 0) = 4;
// // break;
// }
// }
// normalize_feature(db.features, db.features_offset, db.features_scale, offset, 1, weight);
// offset += 1;
// }
void compute_motion_idx_feature(database& db, int& offset, float weight)
{
for (int i = 0; i < db.range_stops(1); i++) {
db.features(i, offset + 0) = 0;
}
for (int i = db.range_stops(1); i < db.nframes(); i++) {
db.features(i, offset + 0) = 1;
}
// for (int i = db.range_stops(2); i < db.range_stops(3); i++) {
// db.features(i, offset + 0) = 2;
// }
// for (int i = db.range_stops(3); i < db.range_stops(4); i++) {
// db.features(i, offset + 0) = 3;
// }
// for (int i = db.range_stops(4); i < db.range_stops(4); i++) {
// db.features(i, offset + 0) = 4;
// }
// for (int i = db.range_stops(5); i < db.range_stops(5); i++) {
// db.features(i, offset + 0) = 5;
// }
// for (int i = db.range_stops(6); i < db.nframes(); i++) {
// db.features(i, offset + 0) = 6;
// }
normalize_feature(db.features, db.features_offset, db.features_scale, offset, 1, weight);
offset += 1;
}
//--------------------------------------
// 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, 15);
int t1 = database_trajectory_index_clamp(db, i, 30);
int t2 = database_trajectory_index_clamp(db, i, 45);
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));
trajectory_pos0.y += db.bone_positions(t0, 1).y - db.bone_positions(i, 1).y;
trajectory_pos1.y += db.bone_positions(t1, 1).y - db.bone_positions(i, 1).y;
trajectory_pos2.y += db.bone_positions(t2, 1).y - db.bone_positions(i, 1).y;
db.features(i, offset + 0) = trajectory_pos0.x;
db.features(i, offset + 1) = trajectory_pos0.y;
db.features(i, offset + 2) = trajectory_pos0.z;
db.features(i, offset + 3) = trajectory_pos1.x;
db.features(i, offset + 4) = trajectory_pos1.y;
db.features(i, offset + 5) = trajectory_pos1.z;
db.features(i, offset + 6) = trajectory_pos2.x;
db.features(i, offset + 7) = trajectory_pos2.y;
db.features(i, offset + 8) = trajectory_pos2.z;
}
// normalize_feature(db.features, db.features_offset, db.features_scale, offset, 9, weight);
std::vector<float> weights;
for(int i=0;i<9;i++){
weights.push_back(weight);
}
weights[2] *= 0.25;
weights[5] *= 0.25;
weights[8] *= 0.25;
weights[1] *= 2.0;
weights[4] *= 2.0;
weights[7] *= 2.0;
weights[0] *= 0.75;
weights[3] *= 0.75;
weights[6] *= 0.75;
for(int i=0;i<3;i++){
weights[i] *= 2.0;
}
normalize_feature_vector(db.features, db.features_offset, db.features_scale, offset, 9, weights);
//
offset += 9;
}
// 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, 15);
int t1 = database_trajectory_index_clamp(db, i, 30);
int t2 = database_trajectory_index_clamp(db, i, 45);
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;
}
void compute_other_position_feature(database& db, database& db2, int& offset, float weight = 1.0f)
{
for (int i = 0; i < 1310; i++)
{
vec3 bone_position = quat_mul_vec3(quat_inv(db.bone_rotations(i, 0)), db2.bone_positions(i,0) - db.bone_positions(i, 0));
db.features(i, offset + 0) = bone_position.x;
db.features(i, offset + 1) = bone_position.z;
}
normalize_feature(db.features, db.features_offset, db.features_scale, offset, 2, weight);
offset += 2;
}
void compute_other_rotation_feature(database& db, database& db2, int& offset, float weight = 1.0f)
{
for (int i = 0; i < 1310; i++)
{
vec3 bone_rotations = quat_mul_vec3(quat_inv(db.bone_rotations(i, 0)), quat_mul_vec3(db2.bone_rotations(i, 0), vec3(0, 0, 1)));
db.features(i, offset + 0) = bone_rotations.x;
db.features(i, offset + 1) = bone_rotations.z;
}
normalize_feature(db.features, db.features_offset, db.features_scale, offset, 2, weight);
offset += 2;
}
void compute_other_velocity_feature(database& db, database& db2, int& offset, float weight = 1.0f)
{
for (int i = 0; i < db.nframes(); i++)
{
vec3 bone_velocity = quat_mul_vec3(quat_inv(db.bone_rotations(i, 0)), db2.bone_velocities(i, 0));
db.features(i, offset + 0) = bone_velocity.x;
db.features(i, offset + 1) = bone_velocity.z;
}
normalize_feature(db.features, db.features_offset, db.features_scale, offset, 2, weight);
offset += 2;
}
void compute_chair_position_feature(database& db, int& offset, float weight = 1.0f)
{
for (int i = db.range_starts(2); i < db.range_stops(20); i++) // 2 11 -> 14 23 2 20
{
const vec3 chair_position = vec3{0.0, 0.0, 0.0};
vec3 pos = quat_mul_vec3(quat_inv(db.bone_rotations(i, 0)), chair_position - db.bone_positions(i, 0));
db.features(i, offset + 0) = pos.x;
db.features(i, offset + 1) = pos.z;
}
normalize_feature(db.features, db.features_offset, db.features_scale, offset, 2, weight);
offset += 2;
}
void compute_chair_direction_feature(database& db, int& offset, float weight = 1.0f)
{
for (int i = db.range_starts(2); i < db.range_stops(20); i++)
{
const quat chair_direction = quat_from_angle_axis(0.0, vec3(0,0,1));
vec3 relative_chair_dir = quat_mul_vec3(quat_inv(db.bone_rotations(i, 0)), quat_mul_vec3(chair_direction, vec3(0, 0, 1)));
db.features(i, offset + 0) = relative_chair_dir.x;
db.features(i, offset + 1) = relative_chair_dir.z;
}
normalize_feature(db.features, db.features_offset, db.features_scale, offset, 2, weight);
offset += 2;
}
//-------------------------------------------------------------------------------------------------------------------------------------------
// 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,
database& db2,
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,
const float feature_weight_chair_position,
const float feature_weight_chair_direction,
const float feature_weight_other)
{
int nfeatures =
3 + // Left Foot Position
3 + // Right Foot Position
// 3 + // Hip Position
3 + // Left Foot Velocity
3 + // Right Foot Velocity
3 + // Hip Velocity
9 + // Trajectory Positions 3D
6 + // Trajectory Directions 2D
2 +
2 +
2 +
2 ;
db.features.resize(db.nframes(), nfeatures);
db.features_offset.resize(nfeatures);
db.features_scale.resize(nfeatures);
int offset = 0;
// compute_motion_idx_feature(db, offset, feature_weight_motion_idx);
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_position_feature(db, offset, Bone_Hips, 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);
compute_chair_position_feature(db, offset, feature_weight_chair_position);
compute_chair_direction_feature(db, offset, feature_weight_chair_direction);
compute_other_position_feature(db, db2, offset, feature_weight_other);
compute_other_rotation_feature(db, db2, offset, feature_weight_other);
assert(offset == nfeatures);
database_build_bounds(db);
}
// Terrain
void terrain_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
9 + // Trajectory Positions 3D
6 ; // Trajectory Directions 2D
db.features.resize(db.nframes(), nfeatures);
db.features_offset.resize(nfeatures);
db.features_scale.resize(nfeatures);
int offset = 0;
// compute_motion_idx_feature(db, offset, feature_weight_motion_idx);
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);
}
// Chair
void chair_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_chair_position,
const float feature_weight_chair_direction)
{
int nfeatures =
3 + // Left Foot Position
3 + // Right Foot Position
3 + // Left Foot Velocity
3 + // Right Foot Velocity
3 + // Hip Velocity
2 + // Relative 2D position of the chair local to the character
2 ; // Relative 2D facing direction of the chair local to the character
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_chair_position_feature(db, offset, feature_weight_chair_position);
compute_chair_direction_feature(db, offset, feature_weight_chair_direction);
assert(offset == nfeatures);
database_build_bounds(db);
}
// Push
// pushing
void pushing_database_build_matching_features(
database& db,
database& db2,
const float feature_weight_foot_position,
const float feature_weight_foot_velocity,
const float feature_weight_hip_velocity,
const float feature_weight_other)
{
int nfeatures =
3 + // Left Foot Position
3 + // Right Foot Position
3 + // Left Foot Velocity
3 + // Right Foot Velocity
3 + // Hip Velocity
2 + // relative position to blue character
2 ; // relative rotation to blue character
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_other_position_feature(db, db2, offset, feature_weight_other);
compute_other_rotation_feature(db, db2, offset, feature_weight_other);
assert(offset == nfeatures);
database_build_bounds(db);
}
// pushed
void pushed_database_build_matching_features(
database& db2,
database& db,
const float feature_weight_other,
const float feature_weight_motion_idx)
{
int nfeatures =
2 + // relative position
2 + // relative rotation
1 ; // motion_idx
db2.features.resize(db2.nframes(), nfeatures);
db2.features_offset.resize(nfeatures);
db2.features_scale.resize(nfeatures);
int offset = 0;
compute_other_position_feature(db2, db, offset, feature_weight_other);
compute_other_rotation_feature(db2, db, offset, feature_weight_other);
compute_motion_idx_feature(db2, offset, feature_weight_motion_idx);
assert(offset == nfeatures);
database_build_bounds(db2);
}
//-------------------------------------------------------------------------------------------------------------
// 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)
{