-
Notifications
You must be signed in to change notification settings - Fork 4
/
blendspace.cpp
1976 lines (1677 loc) · 66.6 KB
/
blendspace.cpp
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
extern "C"
{
#include "raylib.h"
#include "raymath.h"
#define RAYGUI_IMPLEMENTATION
#include "raygui.h"
}
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
#include "common.h"
#include "vec.h"
#include "mat.h"
#include "quat.h"
#include "array.h"
#include "character.h"
#include "database.h"
#include "triangulate.h"
#include "nnet.h"
#include "spring.h"
#include <initializer_list>
#include <vector>
#include <functional>
//--------------------------------------
static inline Vector3 to_Vector3(vec3 v)
{
return (Vector3){ v.x, v.y, v.z };
}
//--------------------------------------
// Perform linear blend skinning and copy
// result into mesh data. Update and upload
// deformed vertex positions and normals to GPU
void deform_character_mesh(
Mesh& mesh,
const character& c,
const slice1d<vec3> bone_anim_positions,
const slice1d<quat> bone_anim_rotations,
const slice1d<int> bone_parents)
{
linear_blend_skinning_positions(
slice1d<vec3>(mesh.vertexCount, (vec3*)mesh.vertices),
c.positions,
c.bone_weights,
c.bone_indices,
c.bone_rest_positions,
c.bone_rest_rotations,
bone_anim_positions,
bone_anim_rotations);
linear_blend_skinning_normals(
slice1d<vec3>(mesh.vertexCount, (vec3*)mesh.normals),
c.normals,
c.bone_weights,
c.bone_indices,
c.bone_rest_rotations,
bone_anim_rotations);
UpdateMeshBuffer(mesh, 0, mesh.vertices, mesh.vertexCount * 3 * sizeof(float), 0);
UpdateMeshBuffer(mesh, 2, mesh.normals, mesh.vertexCount * 3 * sizeof(float), 0);
}
Mesh make_character_mesh(character& c)
{
Mesh mesh = { 0 };
mesh.vertexCount = c.positions.size;
mesh.triangleCount = c.triangles.size / 3;
mesh.vertices = (float*)MemAlloc(c.positions.size * 3 * sizeof(float));
mesh.texcoords = (float*)MemAlloc(c.texcoords.size * 2 * sizeof(float));
mesh.normals = (float*)MemAlloc(c.normals.size * 3 * sizeof(float));
mesh.indices = (unsigned short*)MemAlloc(c.triangles.size * sizeof(unsigned short));
memcpy(mesh.vertices, c.positions.data, c.positions.size * 3 * sizeof(float));
memcpy(mesh.texcoords, c.texcoords.data, c.texcoords.size * 2 * sizeof(float));
memcpy(mesh.normals, c.normals.data, c.normals.size * 3 * sizeof(float));
memcpy(mesh.indices, c.triangles.data, c.triangles.size * sizeof(unsigned short));
UploadMesh(&mesh, true);
return mesh;
}
//--------------------------------------
float orbit_camera_update_azimuth(
const float azimuth,
const float mouse_dx,
const float dt)
{
return azimuth + 1.0f * dt * -mouse_dx;
}
float orbit_camera_update_altitude(
const float altitude,
const float mouse_dy,
const float dt)
{
return clampf(altitude + 1.0f * dt * mouse_dy, 0.0, 0.4f * PIf);
}
float orbit_camera_update_distance(
const float distance,
const float dt)
{
return clampf(distance + 20.0f * dt * -GetMouseWheelMove(), 0.1f, 100.0f);
}
// Updates the camera using the orbit cam controls
void orbit_camera_update(
Camera3D& cam,
float& camera_azimuth,
float& camera_altitude,
float& camera_distance,
const vec3 target,
const float mouse_dx,
const float mouse_dy,
const float dt)
{
camera_azimuth = orbit_camera_update_azimuth(camera_azimuth, mouse_dx, dt);
camera_altitude = orbit_camera_update_altitude(camera_altitude, mouse_dy, dt);
camera_distance = orbit_camera_update_distance(camera_distance, dt);
quat rotation_azimuth = quat_from_angle_axis(camera_azimuth, vec3(0, 1, 0));
vec3 position = quat_mul_vec3(rotation_azimuth, vec3(0, 0, camera_distance));
vec3 axis = normalize(cross(position, vec3(0, 1, 0)));
quat rotation_altitude = quat_from_angle_axis(camera_altitude, axis);
vec3 eye = target + quat_mul_vec3(rotation_altitude, position);
cam.target = (Vector3){ target.x, target.y, target.z };
cam.position = (Vector3){ eye.x, eye.y, eye.z };
}
//--------------------------------------
// Fits a Delauney triangulation to the given set of points
// int the parameter space.
void fit_triangulation(
array1d<delauney_tri>& parameter_tris,
const slice2d<float> animation_parameters)
{
assert(animation_parameters.cols == 2);
// Allocate triangles array
parameter_tris.resize(animation_parameters.rows * 3);
parameter_tris.zero();
// Allocate space for points which includes index
// so we do not lose ordering after sorting
array1d<delauney_point> delauney_points(animation_parameters.rows + 3);
delauney_points.zero();
for (int i = 0; i < animation_parameters.rows; i++)
{
delauney_points(i).index = i;
delauney_points(i).x = animation_parameters(i, 0);
delauney_points(i).y = animation_parameters(i, 1);
}
// Fit triangulation
int tri_num = 0;
int status = delauney_triangulate(
&tri_num,
parameter_tris,
animation_parameters.rows,
delauney_points);
assert(status == 0);
// Copy found points into triangles array
parameter_tris.resize(tri_num);
for (int i = 0; i < parameter_tris.size; i++)
{
parameter_tris(i).p1 = delauney_points(parameter_tris(i).p1).index;
parameter_tris(i).p2 = delauney_points(parameter_tris(i).p2).index;
parameter_tris(i).p3 = delauney_points(parameter_tris(i).p3).index;
}
}
//--------------------------------------
// Computes a weighted average of bone positions
void weighted_average_positions(
slice1d<vec3> out_positions,
slice2d<vec3> anim_positions,
slice1d<float> blend_weights)
{
out_positions.zero();
for (int i = 0; i < blend_weights.size; i++)
{
for (int j = 0; j < out_positions.size; j++)
{
out_positions(j) += blend_weights(i) * anim_positions(i, j);
}
}
}
// Computes a weighted average of bone rotations
// See: https://theorangeduck.com/page/quaternion-weighted-average
void weighted_average_rotations(
slice1d<quat> out_rotations,
slice1d<mat4> accum_rotations,
const slice2d<quat> anim_rotations,
const slice1d<float> blend_weights)
{
assert(anim_rotations.rows == blend_weights.size);
assert(anim_rotations.cols == out_rotations.size);
assert(anim_rotations.cols == accum_rotations.size);
accum_rotations.zero();
for (int i = 0; i < anim_rotations.rows; i++)
{
for (int j = 0; j < anim_rotations.cols; j++)
{
quat q = anim_rotations(i, j);
accum_rotations(j) = accum_rotations(j) + blend_weights(i) * mat4(
q.w*q.w, q.w*q.x, q.w*q.y, q.w*q.z,
q.x*q.w, q.x*q.x, q.x*q.y, q.x*q.z,
q.y*q.w, q.y*q.x, q.y*q.y, q.y*q.z,
q.z*q.w, q.z*q.x, q.z*q.y, q.z*q.z);
}
}
for (int j = 0; j < anim_rotations.cols; j++)
{
vec4 guess = vec4(1, 0, 0, 0);
vec4 u = mat4_svd_dominant_eigen(accum_rotations(j), guess, 64, 1e-8f);
vec4 v = normalize(mat4_transpose_mul_vec4(accum_rotations(j), u));
out_rotations(j) = quat_abs(quat(v.x, v.y, v.z, v.w));
}
}
// Computes a cheaper weighted average of bone rotations
// using the reference pose
void weighted_average_rotations_ref(
slice1d<quat> out_rotations,
const slice1d<quat> reference_rotations,
const slice2d<quat> anim_rotations,
const slice1d<float> blend_weights)
{
assert(anim_rotations.rows == blend_weights.size);
assert(anim_rotations.cols == out_rotations.size);
assert(anim_rotations.cols == reference_rotations.size);
out_rotations.zero();
for (int i = 0; i < anim_rotations.rows; i++)
{
for (int j = 0; j < anim_rotations.cols; j++)
{
out_rotations(j) = out_rotations(j) + blend_weights(i) *
quat_abs(quat_inv_mul(
reference_rotations(j), anim_rotations(i, j)));
}
}
for (int j = 0; j < anim_rotations.cols; j++)
{
out_rotations(j) = quat_abs(quat_mul(
reference_rotations(j), quat_normalize(out_rotations(j))));
}
}
// Samples a pose from the database for the given animation
// at the given normalized time value between 0 and 1
void database_sample(
slice1d<vec3> sample_positions,
slice1d<quat> sample_rotations,
database& db,
int anim,
float time)
{
float frame_time = time * (db.range_stops(anim) - db.range_starts(anim)) + db.range_starts(anim);
int i0 = clamp((int)frame_time + 0, db.range_starts(anim), db.range_stops(anim) - 1);
int i1 = clamp((int)frame_time + 1, db.range_starts(anim), db.range_stops(anim) - 1);
float alpha = fmod(frame_time, 1.0f);
for (int j = 0; j < sample_positions.size; j++)
{
sample_positions(j) = lerp(db.bone_positions(i0, j), db.bone_positions(i1, j), alpha);
sample_rotations(j) = quat_slerp_shortest(db.bone_rotations(i0, j), db.bone_rotations(i1, j), alpha);
}
}
//--------------------------------------
// Computes the average animation parameters for
// speed and turning angle (average angular velocity)
void compute_average_animation_parameters_speed_turn(
slice2d<float> animation_parameters,
database& db,
float dt)
{
for (int i = 0; i < db.nranges(); i++)
{
int range_frame_num = db.range_stops(i) - db.range_starts(i) - 1;
// Compute average angular velocity
float angvel = 0.0f;
for (int j = db.range_starts(i); j < db.range_stops(i) - 1; j++)
{
angvel += quat_to_scaled_angle_axis(quat_abs(quat_mul_inv(
db.bone_rotations(j + 1, 0),
db.bone_rotations(j + 0, 0)))).y / (range_frame_num * dt);
}
// Map into range 0 to 1
animation_parameters(i, 0) = angvel / 9.0f + 0.5f;
}
for (int i = 0; i < db.nranges(); i++)
{
int range_frame_num = db.range_stops(i) - db.range_starts(i) - 1;
// Compute average speed
float speed = 0.0f;
for (int j = db.range_starts(i); j < db.range_stops(i) - 1; j++)
{
speed += length(
db.bone_positions(j + 1, 0) -
db.bone_positions(j + 0, 0)) / (range_frame_num * dt);
}
// Map into range 0 to 1
animation_parameters(i, 1) = 1.0f - speed / 4.0f;
}
}
// Computes the animation root rotation and location accounting
// for looping (and so can be provided a large frame parameter)
static inline void animation_root_looped(
vec3& out_pos,
quat& out_rot,
slice2d<vec3> bone_positions,
slice2d<quat> bone_rotations,
int frame)
{
int nframes = bone_positions.rows;
out_pos = vec3();
out_rot = quat();
while (frame >= nframes)
{
// given frame is larger than the number of frames insert
// full change in location and rotation for the whole anim
vec3 pos_diff = quat_inv_mul_vec3(bone_rotations(0,0), bone_positions(nframes - 1,0) - bone_positions(0,0));
quat rot_diff = quat_mul_inv(bone_rotations(nframes - 1,0), bone_rotations(0,0));
out_pos = quat_mul_vec3(out_rot, pos_diff) + out_pos;
out_rot = quat_mul(rot_diff, out_rot);
frame -= nframes;
}
// Get the change in location and rotation up to the given frame
vec3 pos_diff = quat_inv_mul_vec3(bone_rotations(0,0), bone_positions(frame,0) - bone_positions(0,0));
quat rot_diff = quat_mul_inv(bone_rotations(frame,0), bone_rotations(0,0));
out_pos = quat_mul_vec3(out_rot, pos_diff) + out_pos;
out_rot = quat_mul(rot_diff, out_rot);
}
// Compute the per-frame animation parameters for speed and turn rate
void compute_frame_animation_parameters_speed_turn(
slice2d<float> animation_parameters,
database& db,
float dt,
float normalized_time)
{
for (int i = 0; i < db.nranges(); i++)
{
int range_frame_num = db.range_stops(i) - db.range_starts(i);
int frame = db.range_starts(i) + clamp((int)(normalized_time * (range_frame_num - 1)), 0, range_frame_num - 2);
float angvel = quat_to_scaled_angle_axis(quat_abs(quat_mul_inv(
db.bone_rotations(frame + 0, 0),
db.bone_rotations(frame + 1, 0)))).y / dt;
animation_parameters(i, 0) = angvel / 9.0f + 0.5f;
}
for (int i = 0; i < db.nranges(); i++)
{
int range_frame_num = db.range_stops(i) - db.range_starts(i);
int frame = db.range_starts(i) + clamp((int)(normalized_time * (range_frame_num - 1)), 0, range_frame_num - 2);
float speed = length(
db.bone_positions(frame + 0, 0) -
db.bone_positions(frame + 1, 0)) / dt;
animation_parameters(i, 1) = 1.0f - speed / 4.0f;
}
}
// Compute the average future trajectory position and direction animation parameters
void compute_average_animation_parameters_trajectory(
slice2d<float> animation_parameters,
database& db)
{
for (int i = 0; i < db.nranges(); i++)
{
int range_frame_num = db.range_stops(i) - db.range_starts(i);
for (int s = 0; s < 3; s++)
{
vec3 pos = vec3();
vec3 dir = vec3();
for (int j = 0; j < range_frame_num; j++)
{
vec3 base_pos;
quat base_rot;
animation_root_looped(
base_pos, base_rot,
db.bone_positions.slice(db.range_starts(i), db.range_stops(i)),
db.bone_rotations.slice(db.range_starts(i), db.range_stops(i)),
j);
vec3 off_pos;
quat off_rot;
animation_root_looped(
off_pos, off_rot,
db.bone_positions.slice(db.range_starts(i), db.range_stops(i)),
db.bone_rotations.slice(db.range_starts(i), db.range_stops(i)),
j + (s + 1) * 20);
vec3 loc_pos = quat_inv_mul_vec3(base_rot, off_pos - base_pos);
vec3 loc_dir = quat_inv_mul_vec3(base_rot, quat_mul_vec3(off_rot, vec3(0,0,1)));
pos += loc_pos / range_frame_num;
dir += loc_dir / range_frame_num;
}
dir = normalize(dir);
animation_parameters(i, s * 4 + 0) = pos.x;
animation_parameters(i, s * 4 + 1) = pos.z;
animation_parameters(i, s * 4 + 2) = dir.x;
animation_parameters(i, s * 4 + 3) = dir.z;
}
}
}
// Compute the per-frame future trajectory positions and directions
void compute_frame_animation_parameters_trajectory(
slice2d<float> animation_parameters,
database& db,
float normalized_time)
{
for (int i = 0; i < db.nranges(); i++)
{
int range_frame_num = db.range_stops(i) - db.range_starts(i);
int frame = db.range_starts(i) + clamp((int)(normalized_time * (range_frame_num - 1)), 0, range_frame_num - 2);
for (int s = 0; s < 3; s++)
{
vec3 base_pos;
quat base_rot;
animation_root_looped(
base_pos, base_rot,
db.bone_positions.slice(db.range_starts(i), db.range_stops(i)),
db.bone_rotations.slice(db.range_starts(i), db.range_stops(i)),
frame);
vec3 off_pos;
quat off_rot;
animation_root_looped(
off_pos, off_rot,
db.bone_positions.slice(db.range_starts(i), db.range_stops(i)),
db.bone_rotations.slice(db.range_starts(i), db.range_stops(i)),
frame + (s + 1) * 20);
vec3 pos = quat_inv_mul_vec3(base_rot, off_pos - base_pos);
vec3 dir = quat_inv_mul_vec3(base_rot, quat_mul_vec3(off_rot, vec3(0,0,1)));
animation_parameters(i, s * 4 + 0) = pos.x;
animation_parameters(i, s * 4 + 1) = pos.z;
animation_parameters(i, s * 4 + 2) = dir.x;
animation_parameters(i, s * 4 + 3) = dir.z;
}
}
}
// Clamp and normalize blend weights so that they are greater than
// zero and sum to one
void clamp_normalize_blend_weights(slice1d<float> blend_weights)
{
float total_blend_weight = 0.0f;
for (int i = 0; i < blend_weights.size; i++)
{
blend_weights(i) = maxf(blend_weights(i), 0.0f);
total_blend_weight += blend_weights(i);
}
for (int i = 0; i < blend_weights.size; i++)
{
blend_weights(i) /= total_blend_weight;
}
}
// Compute the distances in the parameter space from the
// current query to all of the animation parameters
void compute_query_distances(
slice1d<float> query_distances,
const slice2d<float> animation_parameters,
const slice1d<float> query_parameters)
{
assert(query_distances.size == animation_parameters.rows);
for (int i = 0; i < animation_parameters.rows; i++)
{
query_distances(i) = 0.0f;
for (int j = 0; j < animation_parameters.cols; j++)
{
query_distances(i) += squaref(
query_parameters(j) - animation_parameters(i, j));
}
query_distances(i) = sqrtf(query_distances(i));
}
}
// Compute blend weights given a blend matrix and distances
void compute_blend_weights(
slice1d<float> blend_weights,
const slice1d<float> query_distances,
const slice2d<float> blend_matrix)
{
mat_mul_vec(blend_weights, blend_matrix, query_distances);
}
// Fit the blend matrix to the given animation parameters
void fit_blend_matrix(
slice2d<float> blend_matrix,
const slice2d<float> animation_parameters)
{
int nanims = animation_parameters.rows;
int nparams = animation_parameters.cols;
// Compute Pairwise Distances
array2d<float> distances(nanims, nanims);
for (int i = 0; i < nanims; i++)
{
for (int j = 0; j < nanims; j++)
{
distances(i, j) = 0.0f;
for (int k = 0; k < nparams; k++)
{
distances(i, j) += squaref(
animation_parameters(i, k) -
animation_parameters(j, k));
}
distances(i, j) = sqrtf(distances(i, j));
}
}
// Subtract epsilon from diagonal this helps the stability
// of the decomposition and solve
for (int i = 0; i < nanims; i++)
{
distances(i, i) -= 1e-4f;
}
// Decompose in place
array1d<int> row_order(nanims);
array1d<float> row_scale(nanims);
bool success = mat_lu_decompose_inplace(distances, row_order, row_scale);
assert(success);
// Write associated blend weights into blend matrix
for (int i = 0; i < nanims; i++)
{
for (int j = 0; j < nanims; j++)
{
blend_matrix(i, j) = i == j ? 1.0f : 0.0f;
}
}
// Solve for blend matrix in-place
for (int i = 0; i < nanims; i++)
{
mat_lu_solve_inplace(blend_matrix(i), distances, row_order);
}
}
//--------------------------------------
// Project a point onto a line segment
static inline vec2 triangulation_edge_proj(vec2 p, vec2 a, vec2 b)
{
float l2 = dot(a - b, a - b);
if (l2 == 0.0f) { return a; }
float t = clampf(dot(p - a, b - a) / l2, 0.0f, 1.0f);
return a + t * (b - a);
}
// Project a point onto the triangulation convex hull
void project_onto_convex_hull(
slice1d<float> query_parameters,
const slice1d<float> target_parameters,
const slice2d<float> animation_parameters,
const slice1d<delauney_tri> parameter_tris)
{
float best_dist = FLT_MAX;
vec2 best_point = vec2();
for (int i = 0; i < parameter_tris.size; i++)
{
int p1 = parameter_tris(i).p1;
int p2 = parameter_tris(i).p2;
int p3 = parameter_tris(i).p3;
vec2 a = vec2(animation_parameters(p1,0), animation_parameters(p1,1));
vec2 b = vec2(animation_parameters(p2,0), animation_parameters(p2,1));
vec2 c = vec2(animation_parameters(p3,0), animation_parameters(p3,1));
vec2 p = vec2(target_parameters(0), target_parameters(1));
vec2 v0 = b - a, v1 = c - a, v2 = p - a;
float d00 = dot(v0, v0);
float d01 = dot(v0, v1);
float d11 = dot(v1, v1);
float d20 = dot(v2, v0);
float d21 = dot(v2, v1);
float denom = d00 * d11 - d01 * d01;
float v = (d11 * d20 - d01 * d21) / denom;
float w = (d00 * d21 - d01 * d20) / denom;
float u = 1.0f - v - w;
if ((u >= 0.0f) && (v >= 0.0f) && (u + v < 1.0f))
{
query_parameters(0) = target_parameters(0);
query_parameters(1) = target_parameters(1);
return;
}
vec2 e1 = triangulation_edge_proj(p, a, b);
vec2 e2 = triangulation_edge_proj(p, b, c);
vec2 e3 = triangulation_edge_proj(p, c, a);
float d1 = length(e1 - p);
float d2 = length(e2 - p);
float d3 = length(e3 - p);
if (d1 < best_dist)
{
best_dist = d1;
best_point = e1;
}
if (d2 < best_dist)
{
best_dist = d2;
best_point = e2;
}
if (d3 < best_dist)
{
best_dist = d3;
best_point = e3;
}
}
query_parameters(0) = best_point.x;
query_parameters(1) = best_point.y;
}
// Compute the blend weights for the query point given
// a triangulation of the space. Assumes the query point
// is within the convex hull.
void compute_blend_weights_triangulation(
slice1d<float> blend_weights,
const slice2d<float> animation_parameters,
const slice1d<float> query_parameters,
const slice1d<delauney_tri> parameter_tris)
{
blend_weights.zero();
for (int i = 0; i < parameter_tris.size; i++)
{
int p1 = parameter_tris(i).p1;
int p2 = parameter_tris(i).p2;
int p3 = parameter_tris(i).p3;
vec2 a = vec2(animation_parameters(p1,0), animation_parameters(p1,1));
vec2 b = vec2(animation_parameters(p2,0), animation_parameters(p2,1));
vec2 c = vec2(animation_parameters(p3,0), animation_parameters(p3,1));
vec2 p = vec2(query_parameters(0), query_parameters(1));
vec2 v0 = b - a, v1 = c - a, v2 = p - a;
float d00 = dot(v0, v0);
float d01 = dot(v0, v1);
float d11 = dot(v1, v1);
float d20 = dot(v2, v0);
float d21 = dot(v2, v1);
float denom = d00 * d11 - d01 * d01;
float v = (d11 * d20 - d01 * d21) / denom;
float w = (d00 * d21 - d01 * d20) / denom;
float u = 1.0f - v - w;
float eps = 1e-3f;
if ((u >= 0.0f - eps) && (v >= 0.0f - eps) && (u + v < 1.0f + eps))
{
blend_weights(p1) = clampf(u, 0.0f, 1.0f);
blend_weights(p2) = clampf(v, 0.0f, 1.0f);
blend_weights(p3) = clampf(w, 0.0f, 1.0f);
float sum = blend_weights(p1) + blend_weights(p2) + blend_weights(p3);
blend_weights(p1) /= sum;
blend_weights(p2) /= sum;
blend_weights(p3) /= sum;
return;
}
}
blend_weights(0) = 1.0f;
return;
}
//--------------------------------------
void draw_axis(const vec3 pos, const quat rot, const float scale = 1.0f)
{
vec3 axis0 = pos + quat_mul_vec3(rot, scale * vec3(1.0f, 0.0f, 0.0f));
vec3 axis1 = pos + quat_mul_vec3(rot, scale * vec3(0.0f, 1.0f, 0.0f));
vec3 axis2 = pos + quat_mul_vec3(rot, scale * vec3(0.0f, 0.0f, 1.0f));
DrawLine3D(to_Vector3(pos), to_Vector3(axis0), RED);
DrawLine3D(to_Vector3(pos), to_Vector3(axis1), GREEN);
DrawLine3D(to_Vector3(pos), to_Vector3(axis2), BLUE);
}
void draw_axis(const vec3 pos, const mat3 rot, const float scale = 1.0f)
{
vec3 axis0 = pos + mat3_mul_vec3(rot, scale * vec3(1.0f, 0.0f, 0.0f));
vec3 axis1 = pos + mat3_mul_vec3(rot, scale * vec3(0.0f, 1.0f, 0.0f));
vec3 axis2 = pos + mat3_mul_vec3(rot, scale * vec3(0.0f, 0.0f, 1.0f));
DrawLine3D(to_Vector3(pos), to_Vector3(axis0), RED);
DrawLine3D(to_Vector3(pos), to_Vector3(axis1), GREEN);
DrawLine3D(to_Vector3(pos), to_Vector3(axis2), BLUE);
}
void draw_trajectory_parameters(slice1d<float> parameters, Color color)
{
vec3 pos0 = vec3(parameters(0), 0, parameters(1));
vec3 dir0 = vec3(parameters(2), 0, parameters(3));
vec3 pos1 = vec3(parameters(4), 0, parameters(5));
vec3 dir1 = vec3(parameters(6), 0, parameters(7));
vec3 pos2 = vec3(parameters(8), 0, parameters(9));
vec3 dir2 = vec3(parameters(10), 0, parameters(11));
DrawSphereWires(to_Vector3(pos0), 0.05f, 4, 10, color);
DrawSphereWires(to_Vector3(pos1), 0.05f, 4, 10, color);
DrawSphereWires(to_Vector3(pos2), 0.05f, 4, 10, color);
DrawLine3D(to_Vector3(pos0), to_Vector3(pos0 + 0.6f * dir0), color);
DrawLine3D(to_Vector3(pos1), to_Vector3(pos1 + 0.6f * dir1), color);
DrawLine3D(to_Vector3(pos2), to_Vector3(pos2 + 0.6f * dir2), color);
DrawLine3D(to_Vector3(pos0), to_Vector3(pos1), color);
DrawLine3D(to_Vector3(pos1), to_Vector3(pos2), color);
}
//--------------------------------------
static float viridis_data[][3] = {
{0.267004, 0.004874, 0.329415},
{0.268510, 0.009605, 0.335427},
{0.269944, 0.014625, 0.341379},
{0.271305, 0.019942, 0.347269},
{0.272594, 0.025563, 0.353093},
{0.273809, 0.031497, 0.358853},
{0.274952, 0.037752, 0.364543},
{0.276022, 0.044167, 0.370164},
{0.277018, 0.050344, 0.375715},
{0.277941, 0.056324, 0.381191},
{0.278791, 0.062145, 0.386592},
{0.279566, 0.067836, 0.391917},
{0.280267, 0.073417, 0.397163},
{0.280894, 0.078907, 0.402329},
{0.281446, 0.084320, 0.407414},
{0.281924, 0.089666, 0.412415},
{0.282327, 0.094955, 0.417331},
{0.282656, 0.100196, 0.422160},
{0.282910, 0.105393, 0.426902},
{0.283091, 0.110553, 0.431554},
{0.283197, 0.115680, 0.436115},
{0.283229, 0.120777, 0.440584},
{0.283187, 0.125848, 0.444960},
{0.283072, 0.130895, 0.449241},
{0.282884, 0.135920, 0.453427},
{0.282623, 0.140926, 0.457517},
{0.282290, 0.145912, 0.461510},
{0.281887, 0.150881, 0.465405},
{0.281412, 0.155834, 0.469201},
{0.280868, 0.160771, 0.472899},
{0.280255, 0.165693, 0.476498},
{0.279574, 0.170599, 0.479997},
{0.278826, 0.175490, 0.483397},
{0.278012, 0.180367, 0.486697},
{0.277134, 0.185228, 0.489898},
{0.276194, 0.190074, 0.493001},
{0.275191, 0.194905, 0.496005},
{0.274128, 0.199721, 0.498911},
{0.273006, 0.204520, 0.501721},
{0.271828, 0.209303, 0.504434},
{0.270595, 0.214069, 0.507052},
{0.269308, 0.218818, 0.509577},
{0.267968, 0.223549, 0.512008},
{0.266580, 0.228262, 0.514349},
{0.265145, 0.232956, 0.516599},
{0.263663, 0.237631, 0.518762},
{0.262138, 0.242286, 0.520837},
{0.260571, 0.246922, 0.522828},
{0.258965, 0.251537, 0.524736},
{0.257322, 0.256130, 0.526563},
{0.255645, 0.260703, 0.528312},
{0.253935, 0.265254, 0.529983},
{0.252194, 0.269783, 0.531579},
{0.250425, 0.274290, 0.533103},
{0.248629, 0.278775, 0.534556},
{0.246811, 0.283237, 0.535941},
{0.244972, 0.287675, 0.537260},
{0.243113, 0.292092, 0.538516},
{0.241237, 0.296485, 0.539709},
{0.239346, 0.300855, 0.540844},
{0.237441, 0.305202, 0.541921},
{0.235526, 0.309527, 0.542944},
{0.233603, 0.313828, 0.543914},
{0.231674, 0.318106, 0.544834},
{0.229739, 0.322361, 0.545706},
{0.227802, 0.326594, 0.546532},
{0.225863, 0.330805, 0.547314},
{0.223925, 0.334994, 0.548053},
{0.221989, 0.339161, 0.548752},
{0.220057, 0.343307, 0.549413},
{0.218130, 0.347432, 0.550038},
{0.216210, 0.351535, 0.550627},
{0.214298, 0.355619, 0.551184},
{0.212395, 0.359683, 0.551710},
{0.210503, 0.363727, 0.552206},
{0.208623, 0.367752, 0.552675},
{0.206756, 0.371758, 0.553117},
{0.204903, 0.375746, 0.553533},
{0.203063, 0.379716, 0.553925},
{0.201239, 0.383670, 0.554294},
{0.199430, 0.387607, 0.554642},
{0.197636, 0.391528, 0.554969},
{0.195860, 0.395433, 0.555276},
{0.194100, 0.399323, 0.555565},
{0.192357, 0.403199, 0.555836},
{0.190631, 0.407061, 0.556089},
{0.188923, 0.410910, 0.556326},
{0.187231, 0.414746, 0.556547},
{0.185556, 0.418570, 0.556753},
{0.183898, 0.422383, 0.556944},
{0.182256, 0.426184, 0.557120},
{0.180629, 0.429975, 0.557282},
{0.179019, 0.433756, 0.557430},
{0.177423, 0.437527, 0.557565},
{0.175841, 0.441290, 0.557685},
{0.174274, 0.445044, 0.557792},
{0.172719, 0.448791, 0.557885},
{0.171176, 0.452530, 0.557965},
{0.169646, 0.456262, 0.558030},
{0.168126, 0.459988, 0.558082},
{0.166617, 0.463708, 0.558119},
{0.165117, 0.467423, 0.558141},
{0.163625, 0.471133, 0.558148},
{0.162142, 0.474838, 0.558140},
{0.160665, 0.478540, 0.558115},
{0.159194, 0.482237, 0.558073},
{0.157729, 0.485932, 0.558013},
{0.156270, 0.489624, 0.557936},
{0.154815, 0.493313, 0.557840},
{0.153364, 0.497000, 0.557724},
{0.151918, 0.500685, 0.557587},
{0.150476, 0.504369, 0.557430},
{0.149039, 0.508051, 0.557250},
{0.147607, 0.511733, 0.557049},
{0.146180, 0.515413, 0.556823},
{0.144759, 0.519093, 0.556572},
{0.143343, 0.522773, 0.556295},
{0.141935, 0.526453, 0.555991},
{0.140536, 0.530132, 0.555659},
{0.139147, 0.533812, 0.555298},
{0.137770, 0.537492, 0.554906},
{0.136408, 0.541173, 0.554483},
{0.135066, 0.544853, 0.554029},
{0.133743, 0.548535, 0.553541},
{0.132444, 0.552216, 0.553018},
{0.131172, 0.555899, 0.552459},
{0.129933, 0.559582, 0.551864},
{0.128729, 0.563265, 0.551229},
{0.127568, 0.566949, 0.550556},
{0.126453, 0.570633, 0.549841},
{0.125394, 0.574318, 0.549086},
{0.124395, 0.578002, 0.548287},
{0.123463, 0.581687, 0.547445},
{0.122606, 0.585371, 0.546557},
{0.121831, 0.589055, 0.545623},
{0.121148, 0.592739, 0.544641},
{0.120565, 0.596422, 0.543611},
{0.120092, 0.600104, 0.542530},
{0.119738, 0.603785, 0.541400},
{0.119512, 0.607464, 0.540218},
{0.119423, 0.611141, 0.538982},
{0.119483, 0.614817, 0.537692},
{0.119699, 0.618490, 0.536347},
{0.120081, 0.622161, 0.534946},
{0.120638, 0.625828, 0.533488},
{0.121380, 0.629492, 0.531973},
{0.122312, 0.633153, 0.530398},
{0.123444, 0.636809, 0.528763},
{0.124780, 0.640461, 0.527068},
{0.126326, 0.644107, 0.525311},
{0.128087, 0.647749, 0.523491},
{0.130067, 0.651384, 0.521608},
{0.132268, 0.655014, 0.519661},
{0.134692, 0.658636, 0.517649},
{0.137339, 0.662252, 0.515571},
{0.140210, 0.665859, 0.513427},
{0.143303, 0.669459, 0.511215},
{0.146616, 0.673050, 0.508936},
{0.150148, 0.676631, 0.506589},
{0.153894, 0.680203, 0.504172},
{0.157851, 0.683765, 0.501686},
{0.162016, 0.687316, 0.499129},
{0.166383, 0.690856, 0.496502},
{0.170948, 0.694384, 0.493803},
{0.175707, 0.697900, 0.491033},
{0.180653, 0.701402, 0.488189},
{0.185783, 0.704891, 0.485273},
{0.191090, 0.708366, 0.482284},
{0.196571, 0.711827, 0.479221},
{0.202219, 0.715272, 0.476084},
{0.208030, 0.718701, 0.472873},
{0.214000, 0.722114, 0.469588},
{0.220124, 0.725509, 0.466226},
{0.226397, 0.728888, 0.462789},
{0.232815, 0.732247, 0.459277},
{0.239374, 0.735588, 0.455688},
{0.246070, 0.738910, 0.452024},
{0.252899, 0.742211, 0.448284},
{0.259857, 0.745492, 0.444467},
{0.266941, 0.748751, 0.440573},
{0.274149, 0.751988, 0.436601},
{0.281477, 0.755203, 0.432552},
{0.288921, 0.758394, 0.428426},
{0.296479, 0.761561, 0.424223},
{0.304148, 0.764704, 0.419943},
{0.311925, 0.767822, 0.415586},
{0.319809, 0.770914, 0.411152},
{0.327796, 0.773980, 0.406640},
{0.335885, 0.777018, 0.402049},
{0.344074, 0.780029, 0.397381},
{0.352360, 0.783011, 0.392636},
{0.360741, 0.785964, 0.387814},
{0.369214, 0.788888, 0.382914},
{0.377779, 0.791781, 0.377939},
{0.386433, 0.794644, 0.372886},
{0.395174, 0.797475, 0.367757},
{0.404001, 0.800275, 0.362552},
{0.412913, 0.803041, 0.357269},
{0.421908, 0.805774, 0.351910},
{0.430983, 0.808473, 0.346476},
{0.440137, 0.811138, 0.340967},
{0.449368, 0.813768, 0.335384},
{0.458674, 0.816363, 0.329727},
{0.468053, 0.818921, 0.323998},
{0.477504, 0.821444, 0.318195},
{0.487026, 0.823929, 0.312321},
{0.496615, 0.826376, 0.306377},
{0.506271, 0.828786, 0.300362},
{0.515992, 0.831158, 0.294279},
{0.525776, 0.833491, 0.288127},
{0.535621, 0.835785, 0.281908},
{0.545524, 0.838039, 0.275626},
{0.555484, 0.840254, 0.269281},
{0.565498, 0.842430, 0.262877},