-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
2070 lines (1795 loc) · 93.6 KB
/
main.c
File metadata and controls
2070 lines (1795 loc) · 93.6 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
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
#include "raylib.h"
#include "raymath.h"
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#define MAX_BONES 8
#define HIST_CAP 900
#define TRAIL_CAP 500
#ifndef PI
#define PI 3.14159265358979323846f
#endif
typedef struct {
float v[HIST_CAP];
int head;
int count;
} History;
typedef struct {
Vector2 v[TRAIL_CAP];
int head;
int count;
} Trail;
static float ClampFloat(float x, float lo, float hi) {
if (x < lo) return lo;
if (x > hi) return hi;
return x;
}
static float WrapPi(float x) {
while (x > PI) x -= 2.0f * PI;
while (x < -PI) x += 2.0f * PI;
return x;
}
static void PushHistory(History *h, float x) {
h->head = (h->head + 1) % HIST_CAP;
h->v[h->head] = x;
if (h->count < HIST_CAP) h->count++;
}
static float HistAt(const History *h, int age) {
if (h->count == 0) return 0.0f;
if (age < 0) age = 0;
if (age >= h->count) age = h->count - 1;
int idx = h->head - age;
while (idx < 0) idx += HIST_CAP;
return h->v[idx];
}
static void PushTrail(Trail *t, Vector2 p) {
t->head = (t->head + 1) % TRAIL_CAP;
t->v[t->head] = p;
if (t->count < TRAIL_CAP) t->count++;
}
static Vector2 TrailAt(const Trail *t, int age) {
if (t->count == 0) return (Vector2){0};
if (age < 0) age = 0;
if (age >= t->count) age = t->count - 1;
int idx = t->head - age;
while (idx < 0) idx += TRAIL_CAP;
return t->v[idx];
}
static void ComputeFK(
int n,
const float *angles,
const float *lengths,
Vector2 base,
Vector2 *joints,
Vector2 *endEff
) {
joints[0] = base;
float s = 0.0f;
Vector2 p = base;
for (int i = 0; i < n; i++) {
s += angles[i];
p.x += lengths[i] * cosf(s);
p.y += lengths[i] * sinf(s);
joints[i + 1] = p;
}
*endEff = p;
}
static void ComputeJacobian(int n, const Vector2 *joints, Vector2 endEff, float J[2][MAX_BONES]) {
for (int i = 0; i < n; i++) {
Vector2 r = Vector2Subtract(endEff, joints[i]);
J[0][i] = -r.y;
J[1][i] = r.x;
}
}
static void DrawPanelFrame(Rectangle r, const char *title, Color accent) {
DrawRectangleRounded(r, 0.08f, 12, (Color){12, 20, 30, 220});
DrawRectangleRoundedLinesEx(r, 0.08f, 12, 2.0f, (Color){50, 90, 125, 255});
Rectangle top = {r.x + 1, r.y + 1, r.width - 2, 24};
DrawRectangleRounded(top, 0.2f, 8, (Color){18, 32, 48, 255});
DrawRectangle((int)r.x + 10, (int)r.y + 10, 4, 10, accent);
DrawText(title, (int)r.x + 20, (int)r.y + 6, 14, (Color){220, 240, 255, 255});
}
static void DrawPanelGrid(Rectangle r, int nx, int ny, Color c) {
for (int i = 1; i < nx; i++) {
float x = r.x + r.width * ((float)i / (float)nx);
DrawLineV((Vector2){x, r.y}, (Vector2){x, r.y + r.height}, c);
}
for (int j = 1; j < ny; j++) {
float y = r.y + r.height * ((float)j / (float)ny);
DrawLineV((Vector2){r.x, y}, (Vector2){r.x + r.width, y}, c);
}
}
static void DrawLinePlot(
Rectangle r,
const History *h,
float yMin,
float yMax,
Color line,
int sampleCount
) {
Rectangle plot = {r.x + 8, r.y + 32, r.width - 16, r.height - 40};
DrawRectangleLinesEx(plot, 1, (Color){70, 105, 135, 220});
DrawPanelGrid(plot, 8, 6, (Color){35, 60, 82, 180});
if (h->count < 2) return;
int n = sampleCount;
if (n > h->count) n = h->count;
if (n < 2) return;
for (int i = 1; i < n; i++) {
float a = HistAt(h, n - i);
float b = HistAt(h, n - 1 - i);
float ta = ClampFloat((a - yMin) / (yMax - yMin + 1e-6f), 0.0f, 1.0f);
float tb = ClampFloat((b - yMin) / (yMax - yMin + 1e-6f), 0.0f, 1.0f);
float x0 = plot.x + plot.width * ((float)(i - 1) / (float)(n - 1));
float y0 = plot.y + plot.height * (1.0f - ta);
float x1 = plot.x + plot.width * ((float)i / (float)(n - 1));
float y1 = plot.y + plot.height * (1.0f - tb);
DrawLineEx((Vector2){x0, y0}, (Vector2){x1, y1}, 2.0f, line);
}
}
static void DrawDualLinePlot(
Rectangle r,
const History *a,
const History *b,
float aMin,
float aMax,
float bMin,
float bMax,
Color ca,
Color cb,
int sampleCount
) {
Rectangle plot = {r.x + 8, r.y + 32, r.width - 16, r.height - 40};
DrawRectangleLinesEx(plot, 1, (Color){70, 105, 135, 220});
DrawPanelGrid(plot, 8, 6, (Color){35, 60, 82, 180});
int n = sampleCount;
if (n > a->count) n = a->count;
if (n > b->count) n = b->count;
if (n < 2) return;
for (int i = 1; i < n; i++) {
float va0 = HistAt(a, n - i);
float va1 = HistAt(a, n - 1 - i);
float vb0 = HistAt(b, n - i);
float vb1 = HistAt(b, n - 1 - i);
float ta0 = ClampFloat((va0 - aMin) / (aMax - aMin + 1e-6f), 0.0f, 1.0f);
float ta1 = ClampFloat((va1 - aMin) / (aMax - aMin + 1e-6f), 0.0f, 1.0f);
float tb0 = ClampFloat((vb0 - bMin) / (bMax - bMin + 1e-6f), 0.0f, 1.0f);
float tb1 = ClampFloat((vb1 - bMin) / (bMax - bMin + 1e-6f), 0.0f, 1.0f);
float x0 = plot.x + plot.width * ((float)(i - 1) / (float)(n - 1));
float x1 = plot.x + plot.width * ((float)i / (float)(n - 1));
DrawLineEx((Vector2){x0, plot.y + plot.height * (1.0f - ta0)}, (Vector2){x1, plot.y + plot.height * (1.0f - ta1)}, 2.0f, ca);
DrawLineEx((Vector2){x0, plot.y + plot.height * (1.0f - tb0)}, (Vector2){x1, plot.y + plot.height * (1.0f - tb1)}, 2.0f, cb);
}
}
static void DrawPhasePlot(Rectangle r, const History *hx, const History *hy, float lim) {
Rectangle plot = {r.x + 8, r.y + 32, r.width - 16, r.height - 40};
DrawRectangleLinesEx(plot, 1, (Color){70, 105, 135, 220});
DrawPanelGrid(plot, 8, 8, (Color){35, 60, 82, 180});
Vector2 c = {plot.x + plot.width * 0.5f, plot.y + plot.height * 0.5f};
DrawLineV((Vector2){c.x, plot.y}, (Vector2){c.x, plot.y + plot.height}, (Color){90, 130, 160, 200});
DrawLineV((Vector2){plot.x, c.y}, (Vector2){plot.x + plot.width, c.y}, (Color){90, 130, 160, 200});
int n = hx->count;
if (hy->count < n) n = hy->count;
if (n > 260) n = 260;
if (n < 2) return;
for (int i = 1; i < n; i++) {
float ex0 = HistAt(hx, n - i);
float ey0 = HistAt(hy, n - i);
float ex1 = HistAt(hx, n - 1 - i);
float ey1 = HistAt(hy, n - 1 - i);
float x0 = c.x + ClampFloat(ex0 / lim, -1.0f, 1.0f) * plot.width * 0.47f;
float y0 = c.y - ClampFloat(ey0 / lim, -1.0f, 1.0f) * plot.height * 0.47f;
float x1 = c.x + ClampFloat(ex1 / lim, -1.0f, 1.0f) * plot.width * 0.47f;
float y1 = c.y - ClampFloat(ey1 / lim, -1.0f, 1.0f) * plot.height * 0.47f;
int alpha = 50 + (int)(205.0f * ((float)i / (float)n));
DrawLineEx((Vector2){x0, y0}, (Vector2){x1, y1}, 2.0f, (Color){255, 110, 90, (unsigned char)alpha});
}
}
static void DrawJointPlot(Rectangle r, const History jointHist[MAX_BONES], int n, int highlightJoint) {
static const Color palette[MAX_BONES] = {
{255, 99, 132, 255}, {255, 173, 96, 255}, {250, 220, 120, 255}, {93, 230, 145, 255},
{90, 180, 255, 255}, {180, 130, 255, 255}, {255, 140, 220, 255}, {130, 255, 255, 255},
};
Rectangle plot = {r.x + 8, r.y + 32, r.width - 16, r.height - 40};
DrawRectangleLinesEx(plot, 1, (Color){70, 105, 135, 220});
DrawPanelGrid(plot, 8, 8, (Color){35, 60, 82, 180});
for (int j = 0; j < n; j++) {
int samples = jointHist[j].count;
if (samples > 180) samples = 180;
if (samples < 2) continue;
for (int i = 1; i < samples; i++) {
float a0 = HistAt(&jointHist[j], samples - i);
float a1 = HistAt(&jointHist[j], samples - 1 - i);
float t0 = ClampFloat((a0 + PI) / (2.0f * PI), 0.0f, 1.0f);
float t1 = ClampFloat((a1 + PI) / (2.0f * PI), 0.0f, 1.0f);
float x0 = plot.x + plot.width * ((float)(i - 1) / (float)(samples - 1));
float y0 = plot.y + plot.height * (1.0f - t0);
float x1 = plot.x + plot.width * ((float)i / (float)(samples - 1));
float y1 = plot.y + plot.height * (1.0f - t1);
Color c = palette[j % MAX_BONES];
if (highlightJoint >= 0) {
if (j == highlightJoint) {
DrawLineEx((Vector2){x0, y0}, (Vector2){x1, y1}, 3.0f, c);
} else {
DrawLineEx((Vector2){x0, y0}, (Vector2){x1, y1}, 1.2f, (Color){c.r, c.g, c.b, 70});
}
} else {
DrawLineEx((Vector2){x0, y0}, (Vector2){x1, y1}, 1.8f, c);
}
}
}
}
static void DrawWorkspacePanel(
Rectangle r,
Vector2 base,
float maxReach,
float innerReach,
Vector2 endEff,
Vector2 target,
const Trail *trail,
bool targetInWorkspace
) {
Rectangle plot = {r.x + 8, r.y + 32, r.width - 16, r.height - 40};
DrawRectangleLinesEx(plot, 1, (Color){70, 105, 135, 220});
Vector2 c = {plot.x + plot.width * 0.5f, plot.y + plot.height * 0.5f};
float s = 0.46f * (plot.height < plot.width ? plot.height : plot.width) / (maxReach + 1e-6f);
DrawCircleLinesV(c, maxReach * s, (Color){80, 150, 200, 220});
DrawCircleLinesV(c, innerReach * s, (Color){80, 100, 160, 180});
int n = trail->count;
if (n > 220) n = 220;
for (int i = 1; i < n; i++) {
Vector2 p0 = TrailAt(trail, n - i);
Vector2 p1 = TrailAt(trail, n - 1 - i);
Vector2 d0 = Vector2Scale(Vector2Subtract(p0, base), s);
Vector2 d1 = Vector2Scale(Vector2Subtract(p1, base), s);
int alpha = 40 + (int)(160.0f * ((float)i / (float)n));
DrawLineEx(Vector2Add(c, d0), Vector2Add(c, d1), 2.0f, (Color){120, 240, 255, (unsigned char)alpha});
}
Vector2 te = Vector2Add(c, Vector2Scale(Vector2Subtract(endEff, base), s));
Vector2 tt = Vector2Add(c, Vector2Scale(Vector2Subtract(target, base), s));
DrawCircleV(te, 4, (Color){120, 240, 255, 255});
DrawCircleLinesV(tt, 6, targetInWorkspace ? (Color){255, 190, 80, 240} : (Color){255, 80, 80, 240});
DrawCircleV(tt, 2, targetInWorkspace ? (Color){255, 190, 80, 240} : (Color){255, 80, 80, 240});
}
static void DrawJacobianPanel(Rectangle r, const float J[2][MAX_BONES], int n, int highlightJoint) {
static const Color palette[MAX_BONES] = {
{255, 99, 132, 255}, {255, 173, 96, 255}, {250, 220, 120, 255}, {93, 230, 145, 255},
{90, 180, 255, 255}, {180, 130, 255, 255}, {255, 140, 220, 255}, {130, 255, 255, 255},
};
Rectangle plot = {r.x + 8, r.y + 32, r.width - 16, r.height - 40};
DrawRectangleLinesEx(plot, 1, (Color){70, 105, 135, 220});
DrawPanelGrid(plot, 6, 6, (Color){35, 60, 82, 180});
Vector2 c = {plot.x + plot.width * 0.5f, plot.y + plot.height * 0.5f};
DrawLineV((Vector2){plot.x, c.y}, (Vector2){plot.x + plot.width, c.y}, (Color){90, 130, 160, 200});
DrawLineV((Vector2){c.x, plot.y}, (Vector2){c.x, plot.y + plot.height}, (Color){90, 130, 160, 200});
float maxNorm = 1.0f;
for (int i = 0; i < n; i++) {
float norm = sqrtf(J[0][i] * J[0][i] + J[1][i] * J[1][i]);
if (norm > maxNorm) maxNorm = norm;
}
float scale = 0.42f * (plot.height < plot.width ? plot.height : plot.width) / (maxNorm + 1e-6f);
for (int i = 0; i < n; i++) {
Vector2 v = {J[0][i] * scale, -J[1][i] * scale};
Color pc = palette[i % MAX_BONES];
if (highlightJoint >= 0) {
if (i == highlightJoint) {
DrawLineEx(c, Vector2Add(c, v), 4.3f, pc);
DrawCircleV(Vector2Add(c, v), 5, pc);
} else {
DrawLineEx(c, Vector2Add(c, v), 2.0f, (Color){pc.r, pc.g, pc.b, 75});
DrawCircleV(Vector2Add(c, v), 3, (Color){pc.r, pc.g, pc.b, 75});
}
} else {
DrawLineEx(c, Vector2Add(c, v), 3.0f, pc);
DrawCircleV(Vector2Add(c, v), 4, pc);
}
}
}
static void DrawScreenButtons(int activePage) {
Rectangle b1 = {18, 12, 54, 28};
Rectangle b2 = {78, 12, 54, 28};
Rectangle b3 = {138, 12, 54, 28};
Rectangle b4 = {198, 12, 54, 28};
Rectangle b5 = {258, 12, 54, 28};
Rectangle b6 = {318, 12, 54, 28};
Rectangle b7 = {378, 12, 54, 28};
Color on = (Color){70, 190, 255, 255};
Color off = (Color){28, 48, 70, 240};
DrawRectangleRounded(b1, 0.23f, 8, activePage == 1 ? on : off);
DrawRectangleRounded(b2, 0.23f, 8, activePage == 2 ? on : off);
DrawRectangleRounded(b3, 0.23f, 8, activePage == 3 ? on : off);
DrawRectangleRounded(b4, 0.23f, 8, activePage == 4 ? on : off);
DrawRectangleRounded(b5, 0.23f, 8, activePage == 5 ? on : off);
DrawRectangleRounded(b6, 0.23f, 8, activePage == 6 ? on : off);
DrawRectangleRounded(b7, 0.23f, 8, activePage == 7 ? on : off);
DrawRectangleRoundedLinesEx(b1, 0.23f, 8, 2.0f, (Color){130, 190, 240, 255});
DrawRectangleRoundedLinesEx(b2, 0.23f, 8, 2.0f, (Color){130, 190, 240, 255});
DrawRectangleRoundedLinesEx(b3, 0.23f, 8, 2.0f, (Color){130, 190, 240, 255});
DrawRectangleRoundedLinesEx(b4, 0.23f, 8, 2.0f, (Color){130, 190, 240, 255});
DrawRectangleRoundedLinesEx(b5, 0.23f, 8, 2.0f, (Color){130, 190, 240, 255});
DrawRectangleRoundedLinesEx(b6, 0.23f, 8, 2.0f, (Color){130, 190, 240, 255});
DrawRectangleRoundedLinesEx(b7, 0.23f, 8, 2.0f, (Color){130, 190, 240, 255});
DrawText("[1]", (int)b1.x + 12, (int)b1.y + 6, 16, (Color){8, 18, 28, 255});
DrawText("[2]", (int)b2.x + 12, (int)b2.y + 6, 16, (Color){8, 18, 28, 255});
DrawText("[3]", (int)b3.x + 12, (int)b3.y + 6, 16, (Color){8, 18, 28, 255});
DrawText("[4]", (int)b4.x + 12, (int)b4.y + 6, 16, (Color){8, 18, 28, 255});
DrawText("[5]", (int)b5.x + 12, (int)b5.y + 6, 16, (Color){8, 18, 28, 255});
DrawText("[6]", (int)b6.x + 12, (int)b6.y + 6, 16, (Color){8, 18, 28, 255});
DrawText("[7]", (int)b7.x + 12, (int)b7.y + 6, 16, (Color){8, 18, 28, 255});
}
static void DrawMatrixTable(
Rectangle panel,
const char *title,
const float *values,
int rows,
int cols,
int stride,
Color accent,
int highlightCol
) {
DrawPanelFrame(panel, title, accent);
Rectangle r = {panel.x + 8, panel.y + 32, panel.width - 16, panel.height - 40};
DrawRectangleLinesEx(r, 1, (Color){70, 105, 135, 220});
float rowLabelW = 36.0f;
float colW = (r.width - rowLabelW) / (float)cols;
float rowH = r.height / (float)(rows + 1);
DrawRectangleRec((Rectangle){r.x, r.y, r.width, rowH}, (Color){18, 38, 54, 220});
for (int c = 0; c <= cols; c++) {
float x = r.x + rowLabelW + colW * (float)c;
DrawLineV((Vector2){x, r.y}, (Vector2){x, r.y + r.height}, (Color){45, 73, 96, 210});
}
for (int rr = 0; rr <= rows + 1; rr++) {
float y = r.y + rowH * (float)rr;
DrawLineV((Vector2){r.x, y}, (Vector2){r.x + r.width, y}, (Color){45, 73, 96, 210});
}
for (int c = 0; c < cols; c++) {
if (c == highlightCol) {
DrawRectangleRec(
(Rectangle){r.x + rowLabelW + colW * c + 1.0f, r.y + 1.0f, colW - 2.0f, r.height - 2.0f},
(Color){255, 185, 85, 28}
);
}
char name[16];
snprintf(name, sizeof(name), "c%d", c);
DrawText(name, (int)(r.x + rowLabelW + colW * c + 8), (int)(r.y + 4), 12, (Color){180, 220, 245, 255});
}
for (int rr = 0; rr < rows; rr++) {
char label[16];
snprintf(label, sizeof(label), "r%d", rr);
DrawText(label, (int)(r.x + 7), (int)(r.y + rowH * (rr + 1) + 5), 12, (Color){180, 220, 245, 255});
for (int c = 0; c < cols; c++) {
char vbuf[32];
snprintf(vbuf, sizeof(vbuf), "%7.2f", values[rr * stride + c]);
DrawText(vbuf, (int)(r.x + rowLabelW + colW * c + 6), (int)(r.y + rowH * (rr + 1) + 5), 12, (Color){210, 235, 255, 255});
}
}
}
static void DrawJointStateTable(
Rectangle panel,
const float *lengths,
const float *angles,
const float *vel,
const float *dq,
int n,
int highlightJoint
) {
DrawPanelFrame(panel, "Joint State Table", (Color){125, 210, 255, 255});
Rectangle r = {panel.x + 8, panel.y + 32, panel.width - 16, panel.height - 40};
DrawRectangleLinesEx(r, 1, (Color){70, 105, 135, 220});
int cols = 5;
float colW = r.width / (float)cols;
float rowH = r.height / (float)(n + 1);
DrawRectangleRec((Rectangle){r.x, r.y, r.width, rowH}, (Color){18, 38, 54, 220});
const char *headers[5] = {"joint", "len", "theta", "omega", "dq"};
for (int c = 0; c < cols; c++) {
DrawText(headers[c], (int)(r.x + colW * c + 6), (int)(r.y + 4), 12, (Color){180, 220, 245, 255});
}
for (int c = 1; c < cols; c++) {
float x = r.x + colW * (float)c;
DrawLineV((Vector2){x, r.y}, (Vector2){x, r.y + r.height}, (Color){45, 73, 96, 210});
}
for (int rr = 1; rr <= n + 1; rr++) {
float y = r.y + rowH * (float)rr;
DrawLineV((Vector2){r.x, y}, (Vector2){r.x + r.width, y}, (Color){45, 73, 96, 210});
}
for (int i = 0; i < n; i++) {
char cell[32];
float y = r.y + rowH * (float)(i + 1) + 4;
if (i == highlightJoint) {
DrawRectangleRec(
(Rectangle){r.x + 1.0f, r.y + rowH * (float)(i + 1) + 1.0f, r.width - 2.0f, rowH - 2.0f},
(Color){255, 185, 85, 28}
);
}
snprintf(cell, sizeof(cell), "%d", i);
DrawText(cell, (int)(r.x + colW * 0 + 6), (int)y, 12, (Color){210, 235, 255, 255});
snprintf(cell, sizeof(cell), "%.1f", lengths[i]);
DrawText(cell, (int)(r.x + colW * 1 + 6), (int)y, 12, (Color){210, 235, 255, 255});
snprintf(cell, sizeof(cell), "%.3f", angles[i]);
DrawText(cell, (int)(r.x + colW * 2 + 6), (int)y, 12, (Color){210, 235, 255, 255});
snprintf(cell, sizeof(cell), "%.3f", vel[i]);
DrawText(cell, (int)(r.x + colW * 3 + 6), (int)y, 12, (Color){210, 235, 255, 255});
snprintf(cell, sizeof(cell), "%.3f", dq[i]);
DrawText(cell, (int)(r.x + colW * 4 + 6), (int)y, 12, (Color){210, 235, 255, 255});
}
}
static void DrawJointPositionTable(Rectangle panel, const Vector2 *joints, int nPlusOne, int highlightJoint) {
DrawPanelFrame(panel, "Joint Position Buffer", (Color){190, 165, 255, 255});
Rectangle r = {panel.x + 8, panel.y + 32, panel.width - 16, panel.height - 40};
DrawRectangleLinesEx(r, 1, (Color){70, 105, 135, 220});
int cols = 3;
float colW = r.width / (float)cols;
float rowH = r.height / (float)(nPlusOne + 1);
DrawRectangleRec((Rectangle){r.x, r.y, r.width, rowH}, (Color){18, 38, 54, 220});
const char *headers[3] = {"idx", "x", "y"};
for (int c = 0; c < cols; c++) {
DrawText(headers[c], (int)(r.x + colW * c + 6), (int)(r.y + 4), 12, (Color){180, 220, 245, 255});
}
for (int c = 1; c < cols; c++) {
float x = r.x + colW * (float)c;
DrawLineV((Vector2){x, r.y}, (Vector2){x, r.y + r.height}, (Color){45, 73, 96, 210});
}
for (int rr = 1; rr <= nPlusOne + 1; rr++) {
float y = r.y + rowH * (float)rr;
DrawLineV((Vector2){r.x, y}, (Vector2){r.x + r.width, y}, (Color){45, 73, 96, 210});
}
for (int i = 0; i < nPlusOne; i++) {
char buf[40];
float y = r.y + rowH * (float)(i + 1) + 4;
if (i == highlightJoint) {
DrawRectangleRec(
(Rectangle){r.x + 1.0f, r.y + rowH * (float)(i + 1) + 1.0f, r.width - 2.0f, rowH - 2.0f},
(Color){255, 185, 85, 28}
);
}
snprintf(buf, sizeof(buf), "%d", i);
DrawText(buf, (int)(r.x + 6), (int)y, 12, (Color){210, 235, 255, 255});
snprintf(buf, sizeof(buf), "%.1f", joints[i].x);
DrawText(buf, (int)(r.x + colW + 6), (int)y, 12, (Color){210, 235, 255, 255});
snprintf(buf, sizeof(buf), "%.1f", joints[i].y);
DrawText(buf, (int)(r.x + colW * 2 + 6), (int)y, 12, (Color){210, 235, 255, 255});
}
}
static void DrawDataStructMetaPanel(
Rectangle panel,
const History *errNorm,
const Trail *trail,
int nBones,
int selected,
int hovered,
float gain,
float lambda,
bool ikEnabled
) {
DrawPanelFrame(panel, "Data Structures + Runtime", (Color){255, 185, 125, 255});
Rectangle r = {panel.x + 12, panel.y + 34, panel.width - 24, panel.height - 44};
DrawRectangleLinesEx(r, 1, (Color){70, 105, 135, 220});
char line[160];
int y = (int)r.y + 8;
snprintf(line, sizeof(line), "float angles[%d], angVel[%d], lengths[%d]", MAX_BONES, MAX_BONES, MAX_BONES);
DrawText(line, (int)r.x + 8, y, 13, (Color){210, 235, 255, 255});
y += 18;
snprintf(line, sizeof(line), "Vector2 joints[%d], J[2][%d]", MAX_BONES + 1, MAX_BONES);
DrawText(line, (int)r.x + 8, y, 13, (Color){210, 235, 255, 255});
y += 18;
snprintf(line, sizeof(line), "History errNorm{head=%d,count=%d,cap=%d}", errNorm->head, errNorm->count, HIST_CAP);
DrawText(line, (int)r.x + 8, y, 13, (Color){210, 235, 255, 255});
y += 18;
snprintf(line, sizeof(line), "Trail endTrail{head=%d,count=%d,cap=%d}", trail->head, trail->count, TRAIL_CAP);
DrawText(line, (int)r.x + 8, y, 13, (Color){210, 235, 255, 255});
y += 18;
snprintf(line, sizeof(line), "nBones=%d selectedJoint=%d", nBones, selected);
DrawText(line, (int)r.x + 8, y, 13, (Color){210, 235, 255, 255});
y += 18;
snprintf(line, sizeof(line), "hoveredJoint=%d", hovered);
DrawText(line, (int)r.x + 8, y, 13, (Color){255, 210, 130, 255});
y += 18;
snprintf(line, sizeof(line), "gain=%.3f lambda=%.3f ik=%s", gain, lambda, ikEnabled ? "ON" : "OFF");
DrawText(line, (int)r.x + 8, y, 13, (Color){210, 235, 255, 255});
y += 18;
DrawText("Solver: dq = J^T (J J^T + lambda^2 I)^-1 e", (int)r.x + 8, y, 13, (Color){255, 200, 140, 255});
}
static void DrawInfluenceBarsPanel(Rectangle panel, const float J[2][MAX_BONES], const float *dq, int n, int highlightJoint) {
DrawPanelFrame(panel, "Per-Joint Influence", (Color){120, 245, 190, 255});
Rectangle r = {panel.x + 8, panel.y + 32, panel.width - 16, panel.height - 40};
DrawRectangleLinesEx(r, 1, (Color){70, 105, 135, 220});
float rowH = r.height / (float)n;
float left = r.x + 34.0f;
float right = r.x + r.width - 8.0f;
float w = right - left;
float maxNorm = 1.0f;
float maxDQ = 1e-3f;
for (int i = 0; i < n; i++) {
float norm = sqrtf(J[0][i] * J[0][i] + J[1][i] * J[1][i]);
if (norm > maxNorm) maxNorm = norm;
float adq = fabsf(dq[i]);
if (adq > maxDQ) maxDQ = adq;
}
for (int i = 0; i < n; i++) {
float y = r.y + rowH * i;
if (i == highlightJoint) {
DrawRectangleRec((Rectangle){r.x + 1, y + 1, r.width - 2, rowH - 2}, (Color){255, 185, 85, 24});
}
DrawText(TextFormat("j%d", i), (int)r.x + 6, (int)(y + 4), 12, (Color){210, 235, 255, 255});
float norm = sqrtf(J[0][i] * J[0][i] + J[1][i] * J[1][i]) / (maxNorm + 1e-6f);
float adq = fabsf(dq[i]) / (maxDQ + 1e-6f);
float y1 = y + rowH * 0.22f;
float y2 = y + rowH * 0.58f;
DrawRectangleRec((Rectangle){left, y1, w * norm, rowH * 0.22f}, (Color){110, 210, 255, 210});
DrawRectangleRec((Rectangle){left, y2, w * adq, rowH * 0.22f}, (Color){130, 255, 150, 210});
DrawRectangleLines((int)left, (int)y1, (int)w, (int)(rowH * 0.22f), (Color){55, 90, 120, 210});
DrawRectangleLines((int)left, (int)y2, (int)w, (int)(rowH * 0.22f), (Color){55, 90, 120, 210});
}
}
static void DrawContributionVectorPanel(Rectangle panel, const float J[2][MAX_BONES], const float *dq, int n, int highlightJoint) {
static const Color palette[MAX_BONES] = {
{255, 99, 132, 255}, {255, 173, 96, 255}, {250, 220, 120, 255}, {93, 230, 145, 255},
{90, 180, 255, 255}, {180, 130, 255, 255}, {255, 140, 220, 255}, {130, 255, 255, 255},
};
DrawPanelFrame(panel, "Task-Space Contribution Vectors", (Color){255, 170, 120, 255});
Rectangle r = {panel.x + 8, panel.y + 32, panel.width - 16, panel.height - 40};
DrawRectangleLinesEx(r, 1, (Color){70, 105, 135, 220});
DrawPanelGrid(r, 6, 6, (Color){35, 60, 82, 180});
Vector2 c = {r.x + r.width * 0.5f, r.y + r.height * 0.5f};
DrawLineV((Vector2){c.x, r.y}, (Vector2){c.x, r.y + r.height}, (Color){90, 130, 160, 200});
DrawLineV((Vector2){r.x, c.y}, (Vector2){r.x + r.width, c.y}, (Color){90, 130, 160, 200});
Vector2 contrib[MAX_BONES] = {0};
Vector2 sum = {0};
float maxMag = 1.0f;
for (int i = 0; i < n; i++) {
contrib[i] = (Vector2){J[0][i] * dq[i], J[1][i] * dq[i]};
sum = Vector2Add(sum, contrib[i]);
float m = Vector2Length(contrib[i]);
if (m > maxMag) maxMag = m;
}
if (Vector2Length(sum) > maxMag) maxMag = Vector2Length(sum);
float s = 0.44f * (r.width < r.height ? r.width : r.height) / (maxMag + 1e-6f);
for (int i = 0; i < n; i++) {
Color col = palette[i % MAX_BONES];
Color dcol = col;
if (highlightJoint >= 0 && i != highlightJoint) dcol = (Color){col.r, col.g, col.b, 75};
DrawLineEx(c, Vector2Add(c, Vector2Scale(contrib[i], s)), i == highlightJoint ? 4.0f : 2.4f, dcol);
}
DrawLineEx(c, Vector2Add(c, Vector2Scale(sum, s)), 4.5f, (Color){255, 235, 140, 255});
DrawCircleV(Vector2Add(c, Vector2Scale(sum, s)), 4, (Color){255, 235, 140, 255});
}
static void DrawManipEllipsePanel(Rectangle panel, const float JJt[2][2], Vector2 err) {
DrawPanelFrame(panel, "Manipulability Ellipse", (Color){175, 225, 255, 255});
Rectangle r = {panel.x + 8, panel.y + 32, panel.width - 16, panel.height - 40};
DrawRectangleLinesEx(r, 1, (Color){70, 105, 135, 220});
DrawPanelGrid(r, 8, 8, (Color){35, 60, 82, 180});
Vector2 c = {r.x + r.width * 0.5f, r.y + r.height * 0.5f};
DrawLineV((Vector2){c.x, r.y}, (Vector2){c.x, r.y + r.height}, (Color){90, 130, 160, 200});
DrawLineV((Vector2){r.x, c.y}, (Vector2){r.x + r.width, c.y}, (Color){90, 130, 160, 200});
float tr = JJt[0][0] + JJt[1][1];
float det = JJt[0][0] * JJt[1][1] - JJt[0][1] * JJt[1][0];
if (det < 0.0f) det = 0.0f;
float disc = tr * tr - 4.0f * det;
if (disc < 0.0f) disc = 0.0f;
float l1 = 0.5f * (tr + sqrtf(disc));
float l2 = 0.5f * (tr - sqrtf(disc));
if (l1 < 0.0f) l1 = 0.0f;
if (l2 < 0.0f) l2 = 0.0f;
float a = sqrtf(l1);
float b = sqrtf(l2);
float theta = 0.5f * atan2f(2.0f * JJt[0][1], JJt[0][0] - JJt[1][1] + 1e-6f);
float maxAxis = (a > b ? a : b);
float scale = 0.43f * (r.width < r.height ? r.width : r.height) / (maxAxis + 1e-6f);
for (int k = 0; k < 120; k++) {
float t0 = (2.0f * PI * k) / 120.0f;
float t1 = (2.0f * PI * (k + 1)) / 120.0f;
Vector2 p0 = {a * cosf(t0), b * sinf(t0)};
Vector2 p1 = {a * cosf(t1), b * sinf(t1)};
Vector2 q0 = {cosf(theta) * p0.x - sinf(theta) * p0.y, sinf(theta) * p0.x + cosf(theta) * p0.y};
Vector2 q1 = {cosf(theta) * p1.x - sinf(theta) * p1.y, sinf(theta) * p1.x + cosf(theta) * p1.y};
DrawLineEx(Vector2Add(c, Vector2Scale(q0, scale)), Vector2Add(c, Vector2Scale(q1, scale)), 2.0f, (Color){120, 225, 255, 230});
}
Vector2 major = {a * cosf(theta), a * sinf(theta)};
Vector2 minor = {-b * sinf(theta), b * cosf(theta)};
DrawLineEx(Vector2Add(c, Vector2Scale(major, -scale)), Vector2Add(c, Vector2Scale(major, scale)), 2.5f, (Color){255, 205, 130, 220});
DrawLineEx(Vector2Add(c, Vector2Scale(minor, -scale)), Vector2Add(c, Vector2Scale(minor, scale)), 2.0f, (Color){185, 255, 165, 220});
float errMag = Vector2Length(err);
if (errMag > 1e-5f) {
Vector2 eDir = Vector2Scale(err, 1.0f / errMag);
DrawLineEx(c, Vector2Add(c, Vector2Scale(eDir, 0.42f * (r.width < r.height ? r.width : r.height))), 3.0f, (Color){255, 120, 95, 220});
}
}
static void DrawSingularityRadarPanel(Rectangle panel, float manip, float cond, float maxReach) {
DrawPanelFrame(panel, "Singularity Radar", (Color){255, 165, 210, 255});
Rectangle r = {panel.x + 8, panel.y + 32, panel.width - 16, panel.height - 40};
DrawRectangleLinesEx(r, 1, (Color){70, 105, 135, 220});
Vector2 c = {r.x + r.width * 0.5f, r.y + r.height * 0.55f};
float rad = 0.34f * (r.width < r.height ? r.width : r.height);
for (int i = 1; i <= 4; i++) DrawCircleLines((int)c.x, (int)c.y, rad * i * 0.25f, (Color){45, 73, 96, 220});
for (int i = 0; i < 8; i++) {
float a = (2.0f * PI * i) / 8.0f;
DrawLineEx(c, (Vector2){c.x + cosf(a) * rad, c.y + sinf(a) * rad}, 1.0f, (Color){45, 73, 96, 220});
}
float mNorm = ClampFloat(manip / (maxReach * maxReach * 0.35f + 1e-6f), 0.0f, 1.0f);
float cBad = ClampFloat((cond - 1.0f) / 22.0f, 0.0f, 1.0f);
float safe = ClampFloat(0.65f * mNorm + 0.35f * (1.0f - cBad), 0.0f, 1.0f);
float a = (-PI * 0.75f) + safe * (PI * 1.5f);
DrawCircleSector(c, rad * 1.05f, -135, 135, 42, (Color){35, 58, 80, 120});
DrawCircleSectorLines(c, rad * 1.05f, -135, 135, 42, (Color){80, 115, 145, 220});
DrawLineEx(c, (Vector2){c.x + cosf(a) * rad * 0.94f, c.y + sinf(a) * rad * 0.94f}, 4.0f, (Color){255, 180, 110, 255});
DrawCircleV(c, 5, (Color){255, 220, 150, 255});
DrawText(TextFormat("safety %.2f", safe), (int)(r.x + 8), (int)(r.y + r.height - 40), 13, (Color){220, 240, 255, 255});
DrawText(TextFormat("manip %.2f", manip), (int)(r.x + 8), (int)(r.y + r.height - 24), 12, (Color){170, 230, 200, 255});
DrawText(TextFormat("cond %.2f", cond), (int)(r.x + 130), (int)(r.y + r.height - 24), 12, (Color){255, 195, 150, 255});
}
static void DrawLiveVectorMathPanel(Rectangle panel, Vector2 err, Vector2 vCmd, Vector2 vActual) {
DrawPanelFrame(panel, "Live Vector Math", (Color){255, 170, 120, 255});
Rectangle r = {panel.x + 8, panel.y + 32, panel.width - 16, panel.height - 40};
DrawRectangleLinesEx(r, 1, (Color){70, 105, 135, 220});
DrawPanelGrid(r, 8, 8, (Color){35, 60, 82, 180});
Vector2 c = {r.x + r.width * 0.5f, r.y + r.height * 0.58f};
DrawLineV((Vector2){r.x, c.y}, (Vector2){r.x + r.width, c.y}, (Color){90, 130, 160, 200});
DrawLineV((Vector2){c.x, r.y}, (Vector2){c.x, r.y + r.height}, (Color){90, 130, 160, 200});
float lim = 1.0f;
float eMag = Vector2Length(err);
float vMag = Vector2Length(vCmd);
float aMag = Vector2Length(vActual);
if (eMag > lim) lim = eMag;
if (vMag > lim) lim = vMag;
if (aMag > lim) lim = aMag;
lim *= 1.25f;
float s = 0.40f * (r.width < r.height ? r.width : r.height) / (lim + 1e-6f);
Vector2 e2 = Vector2Scale(err, s);
Vector2 v2 = Vector2Scale(vCmd, s);
Vector2 a2 = Vector2Scale(vActual, s);
DrawLineEx(c, Vector2Add(c, e2), 4.0f, (Color){255, 120, 95, 245});
DrawCircleV(Vector2Add(c, e2), 4, (Color){255, 120, 95, 245});
DrawLineEx(c, Vector2Add(c, v2), 4.0f, (Color){120, 220, 255, 245});
DrawCircleV(Vector2Add(c, v2), 4, (Color){120, 220, 255, 245});
DrawLineEx(c, Vector2Add(c, a2), 4.0f, (Color){175, 255, 150, 245});
DrawCircleV(Vector2Add(c, a2), 4, (Color){175, 255, 150, 245});
DrawText("e", (int)(c.x + e2.x + 6), (int)(c.y + e2.y + 2), 12, (Color){255, 150, 120, 255});
DrawText("v_cmd", (int)(c.x + v2.x + 6), (int)(c.y + v2.y + 2), 12, (Color){150, 230, 255, 255});
DrawText("v_actual", (int)(c.x + a2.x + 6), (int)(c.y + a2.y + 2), 12, (Color){175, 255, 150, 255});
DrawText("Drag target: red e updates instantly.", (int)r.x + 8, (int)r.y + 6, 12, (Color){255, 205, 180, 255});
DrawText("Blue solves from matrix; green is what arm actually did.", (int)r.x + 8, (int)r.y + 20, 12, (Color){200, 235, 255, 255});
}
static void DrawMatrixSolveIntuitionPanel(Rectangle panel, const float Areg[2][2], const float invA[2][2], bool invOk, Vector2 e, Vector2 v) {
DrawPanelFrame(panel, "Damped Solve Intuition", (Color){145, 220, 255, 255});
Rectangle r = {panel.x + 8, panel.y + 32, panel.width - 16, panel.height - 40};
DrawRectangleLinesEx(r, 1, (Color){70, 105, 135, 220});
DrawText("v = gain * (J J^T + lambda^2 I)^-1 e", (int)r.x + 8, (int)r.y + 8, 12, (Color){180, 230, 255, 255});
DrawText(TextFormat("e=[%.2f %.2f]^T v=[%.2f %.2f]^T", e.x, e.y, v.x, v.y), (int)r.x + 8, (int)r.y + 24, 12, (Color){210, 235, 255, 255});
Rectangle m1 = {r.x + 8, r.y + 44, r.width * 0.43f, r.height - 52};
Rectangle m2 = {r.x + r.width * 0.52f, r.y + 44, r.width * 0.43f, r.height - 52};
DrawRectangleLinesEx(m1, 1, (Color){55, 90, 120, 220});
DrawRectangleLinesEx(m2, 1, (Color){55, 90, 120, 220});
DrawText("A = JJ^T + lambda^2 I", (int)m1.x + 6, (int)m1.y + 4, 11, (Color){170, 225, 255, 255});
DrawText("A^-1", (int)m2.x + 6, (int)m2.y + 4, 11, (Color){190, 235, 255, 255});
float cellW1 = (m1.width - 16) * 0.5f;
float cellH1 = (m1.height - 28) * 0.5f;
float cellW2 = (m2.width - 16) * 0.5f;
float cellH2 = (m2.height - 28) * 0.5f;
float maxA = fabsf(Areg[0][0]);
if (fabsf(Areg[0][1]) > maxA) maxA = fabsf(Areg[0][1]);
if (fabsf(Areg[1][0]) > maxA) maxA = fabsf(Areg[1][0]);
if (fabsf(Areg[1][1]) > maxA) maxA = fabsf(Areg[1][1]);
if (maxA < 1e-6f) maxA = 1.0f;
for (int rr = 0; rr < 2; rr++) {
for (int cc = 0; cc < 2; cc++) {
float val = Areg[rr][cc];
int alpha = (int)(40 + 180 * ClampFloat(fabsf(val) / maxA, 0.0f, 1.0f));
Rectangle c = {m1.x + 8 + cc * cellW1, m1.y + 20 + rr * cellH1, cellW1 - 2, cellH1 - 2};
DrawRectangleRec(c, (Color){120, 200, 255, (unsigned char)alpha});
DrawText(TextFormat("%.2f", val), (int)c.x + 8, (int)c.y + 8, 12, (Color){8, 20, 30, 255});
}
}
if (invOk) {
float maxI = fabsf(invA[0][0]);
if (fabsf(invA[0][1]) > maxI) maxI = fabsf(invA[0][1]);
if (fabsf(invA[1][0]) > maxI) maxI = fabsf(invA[1][0]);
if (fabsf(invA[1][1]) > maxI) maxI = fabsf(invA[1][1]);
if (maxI < 1e-6f) maxI = 1.0f;
for (int rr = 0; rr < 2; rr++) {
for (int cc = 0; cc < 2; cc++) {
float val = invA[rr][cc];
int alpha = (int)(40 + 180 * ClampFloat(fabsf(val) / maxI, 0.0f, 1.0f));
Rectangle c = {m2.x + 8 + cc * cellW2, m2.y + 20 + rr * cellH2, cellW2 - 2, cellH2 - 2};
DrawRectangleRec(c, (Color){140, 235, 180, (unsigned char)alpha});
DrawText(TextFormat("%.3f", val), (int)c.x + 8, (int)c.y + 8, 12, (Color){8, 20, 30, 255});
}
}
} else {
DrawText("near singular", (int)m2.x + 10, (int)m2.y + 28, 12, (Color){255, 140, 140, 255});
}
}
static void DrawJointMotionBreakdownPanel(Rectangle panel, const float J[2][MAX_BONES], const float *dq, int n, int highlightJoint) {
DrawPanelFrame(panel, "dq Breakdown: dq_i = J_i^T v", (Color){165, 255, 180, 255});
Rectangle r = {panel.x + 8, panel.y + 32, panel.width - 16, panel.height - 40};
DrawRectangleLinesEx(r, 1, (Color){70, 105, 135, 220});
float rowH = r.height / (float)n;
float maxDQ = 1e-3f;
for (int i = 0; i < n; i++) {
float a = fabsf(dq[i]);
if (a > maxDQ) maxDQ = a;
}
for (int i = 0; i < n; i++) {
float y = r.y + rowH * i;
if (i == highlightJoint) DrawRectangleRec((Rectangle){r.x + 1, y + 1, r.width - 2, rowH - 2}, (Color){255, 185, 85, 24});
DrawText(TextFormat("j%d", i), (int)r.x + 6, (int)y + 4, 12, (Color){210, 235, 255, 255});
DrawText(TextFormat("J=[%.2f %.2f]", J[0][i], J[1][i]), (int)r.x + 36, (int)y + 4, 12, (Color){160, 220, 255, 255});
DrawText(TextFormat("dq=%.3f", dq[i]), (int)(r.x + r.width - 88), (int)y + 4, 12, (Color){180, 255, 190, 255});
float w = (r.width - 16) * ClampFloat(fabsf(dq[i]) / maxDQ, 0.0f, 1.0f);
DrawRectangleRec((Rectangle){r.x + 8, y + rowH * 0.56f, w, rowH * 0.30f}, (Color){120, 255, 170, 210});
DrawRectangleLines((int)r.x + 8, (int)(y + rowH * 0.56f), (int)(r.width - 16), (int)(rowH * 0.30f), (Color){55, 90, 120, 210});
}
}
static void DrawCommandActualComparePanel(Rectangle panel, const History *vxCmd, const History *vyCmd, const History *vxAct, const History *vyAct) {
DrawPanelFrame(panel, "Commanded vs Actual Motion", (Color){230, 190, 255, 255});
Rectangle r = {panel.x + 8, panel.y + 32, panel.width - 16, panel.height - 40};
DrawRectangleLinesEx(r, 1, (Color){70, 105, 135, 220});
DrawPanelGrid(r, 8, 8, (Color){35, 60, 82, 180});
int n = 200;
if (vxCmd->count < n) n = vxCmd->count;
if (vyCmd->count < n) n = vyCmd->count;
if (vxAct->count < n) n = vxAct->count;
if (vyAct->count < n) n = vyAct->count;
if (n < 2) return;
float lim = 1.0f;
for (int i = 0; i < n; i++) {
float a = fabsf(HistAt(vxCmd, i)); if (a > lim) lim = a;
float b = fabsf(HistAt(vyCmd, i)); if (b > lim) lim = b;
float c = fabsf(HistAt(vxAct, i)); if (c > lim) lim = c;
float d = fabsf(HistAt(vyAct, i)); if (d > lim) lim = d;
}
lim *= 1.15f;
for (int i = 1; i < n; i++) {
float x0 = r.x + r.width * (float)(i - 1) / (float)(n - 1);
float x1 = r.x + r.width * (float)i / (float)(n - 1);
float c0 = HistAt(vxCmd, n - i);
float c1 = HistAt(vxCmd, n - 1 - i);
float a0 = HistAt(vxAct, n - i);
float a1 = HistAt(vxAct, n - 1 - i);
float yc0 = r.y + r.height * (0.5f - 0.45f * (c0 / lim));
float yc1 = r.y + r.height * (0.5f - 0.45f * (c1 / lim));
float ya0 = r.y + r.height * (0.5f - 0.45f * (a0 / lim));
float ya1 = r.y + r.height * (0.5f - 0.45f * (a1 / lim));
DrawLineEx((Vector2){x0, yc0}, (Vector2){x1, yc1}, 2.0f, (Color){130, 220, 255, 230});
DrawLineEx((Vector2){x0, ya0}, (Vector2){x1, ya1}, 2.0f, (Color){170, 255, 160, 230});
}
DrawText("x: cmd blue, actual green (drag target to see lag/overshoot)", (int)r.x + 8, (int)r.y + 6, 12, (Color){210, 235, 255, 255});
}
static void DrawLinearizationResidualPanel(Rectangle panel, Vector2 dPred, Vector2 dActual) {
DrawPanelFrame(panel, "Local Linearization Check", (Color){255, 180, 120, 255});
Rectangle r = {panel.x + 8, panel.y + 32, panel.width - 16, panel.height - 40};
DrawRectangleLinesEx(r, 1, (Color){70, 105, 135, 220});
DrawPanelGrid(r, 8, 8, (Color){35, 60, 82, 180});
Vector2 c = {r.x + r.width * 0.5f, r.y + r.height * 0.58f};
DrawLineV((Vector2){r.x, c.y}, (Vector2){r.x + r.width, c.y}, (Color){90, 130, 160, 200});
DrawLineV((Vector2){c.x, r.y}, (Vector2){c.x, r.y + r.height}, (Color){90, 130, 160, 200});
Vector2 res = Vector2Subtract(dActual, dPred);
float lim = 1.0f;
float mp = Vector2Length(dPred); if (mp > lim) lim = mp;
float ma = Vector2Length(dActual); if (ma > lim) lim = ma;
float mr = Vector2Length(res); if (mr > lim) lim = mr;
lim *= 1.25f;
float s = 0.40f * (r.width < r.height ? r.width : r.height) / (lim + 1e-6f);
DrawLineEx(c, Vector2Add(c, Vector2Scale(dPred, s)), 4.0f, (Color){120, 220, 255, 245});
DrawLineEx(c, Vector2Add(c, Vector2Scale(dActual, s)), 4.0f, (Color){175, 255, 150, 245});
DrawLineEx(c, Vector2Add(c, Vector2Scale(res, s)), 4.0f, (Color){255, 120, 95, 245});
DrawText("pred J*dq*dt", (int)r.x + 8, (int)r.y + 6, 12, (Color){130, 220, 255, 255});
DrawText("actual dx", (int)r.x + 110, (int)r.y + 6, 12, (Color){175, 255, 150, 255});
DrawText("residual", (int)r.x + 190, (int)r.y + 6, 12, (Color){255, 140, 110, 255});
}
static void DrawJacobianHeatmapPanel(Rectangle panel, const float J[2][MAX_BONES], int n, int highlightJoint) {
DrawPanelFrame(panel, "Jacobian Sensitivity Heatmap", (Color){150, 225, 255, 255});
Rectangle r = {panel.x + 8, panel.y + 32, panel.width - 16, panel.height - 40};
DrawRectangleLinesEx(r, 1, (Color){70, 105, 135, 220});
float rowLabelW = 36.0f;
float colW = (r.width - rowLabelW) / (float)n;
float rowH = r.height / 3.0f;
float maxAbs = 1e-6f;
for (int rr = 0; rr < 2; rr++) {
for (int c = 0; c < n; c++) {
float a = fabsf(J[rr][c]);
if (a > maxAbs) maxAbs = a;
}
}
for (int c = 0; c < n; c++) {
if (c == highlightJoint) {
DrawRectangleRec((Rectangle){r.x + rowLabelW + c * colW + 1, r.y + 1, colW - 2, r.height - 2}, (Color){255, 185, 85, 24});
}
}
for (int rr = 0; rr < 2; rr++) {
DrawText(rr == 0 ? "dx/dq" : "dy/dq", (int)r.x + 4, (int)(r.y + rowH * (rr + 1) + 6), 12, (Color){190, 225, 245, 255});
for (int c = 0; c < n; c++) {
float v = J[rr][c];
float t = ClampFloat(fabsf(v) / maxAbs, 0.0f, 1.0f);
Color base = v >= 0 ? (Color){130, 235, 175, (unsigned char)(40 + 190 * t)} : (Color){255, 140, 120, (unsigned char)(40 + 190 * t)};
Rectangle cell = {r.x + rowLabelW + c * colW + 1, r.y + rowH * (rr + 1) + 1, colW - 2, rowH - 2};
DrawRectangleRec(cell, base);
DrawText(TextFormat("%.1f", v), (int)cell.x + 4, (int)cell.y + 6, 11, (Color){8, 20, 30, 255});
}
}
for (int c = 0; c <= n; c++) {
float x = r.x + rowLabelW + c * colW;
DrawLineV((Vector2){x, r.y}, (Vector2){x, r.y + r.height}, (Color){45, 73, 96, 210});
}
for (int rr = 0; rr <= 3; rr++) {
float y = r.y + rowH * rr;
DrawLineV((Vector2){r.x, y}, (Vector2){r.x + r.width, y}, (Color){45, 73, 96, 210});
}
}
static void DrawSelectedJointWhatIfPanel(Rectangle panel, int selectedJoint, float theta, float len, Vector2 endEff, const float J[2][MAX_BONES]) {
DrawPanelFrame(panel, "Selected Joint What-If", (Color){190, 255, 170, 255});
Rectangle r = {panel.x + 8, panel.y + 32, panel.width - 16, panel.height - 40};
DrawRectangleLinesEx(r, 1, (Color){70, 105, 135, 220});
DrawPanelGrid(r, 8, 8, (Color){35, 60, 82, 180});
Vector2 c = {r.x + r.width * 0.5f, r.y + r.height * 0.62f};
DrawLineV((Vector2){r.x, c.y}, (Vector2){r.x + r.width, c.y}, (Color){90, 130, 160, 200});
DrawLineV((Vector2){c.x, r.y}, (Vector2){c.x, r.y + r.height}, (Color){90, 130, 160, 200});
float delta = 0.12f;
Vector2 dir = {J[0][selectedJoint], J[1][selectedJoint]};
float mag = Vector2Length(dir);
if (mag > 1e-6f) dir = Vector2Scale(dir, 1.0f / mag);
Vector2 pPlus = Vector2Add(endEff, Vector2Scale(dir, len * delta * 0.35f));
Vector2 pMinus = Vector2Add(endEff, Vector2Scale(dir, -len * delta * 0.35f));
float lim = len * 0.42f;
if (lim < 1.0f) lim = 1.0f;
float s = 0.40f * (r.width < r.height ? r.width : r.height) / lim;
Vector2 eP = Vector2Scale(Vector2Subtract(pPlus, endEff), s);
Vector2 eM = Vector2Scale(Vector2Subtract(pMinus, endEff), s);
DrawLineEx(c, Vector2Add(c, eP), 4.0f, (Color){130, 220, 255, 240});
DrawLineEx(c, Vector2Add(c, eM), 4.0f, (Color){255, 160, 120, 240});
DrawCircleV(c, 4, (Color){220, 235, 255, 255});
DrawCircleV(Vector2Add(c, eP), 4, (Color){130, 220, 255, 255});
DrawCircleV(Vector2Add(c, eM), 4, (Color){255, 160, 120, 255});
DrawText(TextFormat("j%d theta=%.3f len=%.1f", selectedJoint, theta, len), (int)r.x + 8, (int)r.y + 6, 12, (Color){210, 235, 255, 255});
DrawText("+dtheta effect", (int)r.x + 8, (int)r.y + 20, 12, (Color){140, 225, 255, 255});
DrawText("-dtheta effect", (int)r.x + 106, (int)r.y + 20, 12, (Color){255, 170, 130, 255});
}
static void DrawErrorCapturePanel(Rectangle panel, const History *errN, const History *cond, const History *dqNorm) {
DrawPanelFrame(panel, "Error Capture + Control Effort", (Color){230, 190, 255, 255});
Rectangle r = {panel.x + 8, panel.y + 32, panel.width - 16, panel.height - 40};
DrawRectangleLinesEx(r, 1, (Color){70, 105, 135, 220});
DrawPanelGrid(r, 8, 8, (Color){35, 60, 82, 180});
int n = 220;