-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
2036 lines (1819 loc) · 98.9 KB
/
main.c
File metadata and controls
2036 lines (1819 loc) · 98.9 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 <math.h>
#include <stdbool.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX_HOT_ITEMS 512
#define PI_F 3.14159265358979323846f
#define SENSOR_HIST_MAX 160
typedef enum {
PHASE_SIMULATE = 0,
PHASE_PREDICT_STATE,
PHASE_PREDICT_COV,
PHASE_INNOVATION,
PHASE_GAIN,
PHASE_UPDATE,
PHASE_COUNT
} EkfPhase;
typedef struct {
float m[4][4];
} Mat4;
typedef struct {
float m[2][4];
} Mat24;
typedef struct {
float m[2][2];
} Mat2;
typedef struct {
Rectangle rect;
char title[96];
char desc[320];
} HotItem;
typedef struct {
float x[4];
Mat4 P;
Mat4 Q;
Mat24 H;
Mat2 R;
float trueState[4];
float z[2];
float y[2];
Mat2 S;
float K[4][2];
float dt;
float simTime;
EkfPhase phase;
int frameCounter;
bool hasMeasurement;
bool paused;
float stepSpeed;
float uiScale;
bool showHelp;
bool showGoal;
Vector2 worldOrigin;
float worldScale;
float splitMainX;
float splitLeftY;
float splitBottomX;
float splitRightY;
bool dragMain;
bool dragLeftY;
bool dragBottomX;
bool dragRightY;
float goalSplitX;
float goalSplitY;
bool dragGoalX;
bool dragGoalY;
float histT[SENSOR_HIST_MAX];
float histZR[SENSOR_HIST_MAX];
float histZB[SENSOR_HIST_MAX];
float histPR[SENSOR_HIST_MAX];
float histPB[SENSOR_HIST_MAX];
float histYR[SENSOR_HIST_MAX];
float histYB[SENSOR_HIST_MAX];
int histCount;
int histHead;
} AppState;
static HotItem gHotItems[MAX_HOT_ITEMS];
static int gHotCount = 0;
static void UpdateMeasurementJacobian(AppState *s, const float x[4]);
static float RandNormal(void) {
float u1 = ((float)GetRandomValue(1, 10000)) / 10001.0f;
float u2 = ((float)GetRandomValue(1, 10000)) / 10001.0f;
float r = sqrtf(-2.0f * logf(u1));
float theta = 2.0f * PI_F * u2;
return r * cosf(theta);
}
static void HotBegin(void) {
gHotCount = 0;
}
static void HotAdd(Rectangle rect, const char *title, const char *desc) {
if (gHotCount >= MAX_HOT_ITEMS) return;
HotItem *h = &gHotItems[gHotCount++];
h->rect = rect;
snprintf(h->title, sizeof(h->title), "%s", title ? title : "");
snprintf(h->desc, sizeof(h->desc), "%s", desc ? desc : "");
}
static void HotAddFmt(Rectangle rect, const char *title, const char *fmt, ...) {
if (gHotCount >= MAX_HOT_ITEMS) return;
HotItem *h = &gHotItems[gHotCount++];
h->rect = rect;
snprintf(h->title, sizeof(h->title), "%s", title ? title : "");
va_list args;
va_start(args, fmt);
vsnprintf(h->desc, sizeof(h->desc), fmt ? fmt : "", args);
va_end(args);
}
static void DrawTextScaled(const char *text, Vector2 pos, float size, Color color) {
Font f = GetFontDefault();
DrawTextEx(f, text, pos, size, 1.0f, color);
}
static float ClampF(float v, float lo, float hi) {
if (v < lo) return lo;
if (v > hi) return hi;
return v;
}
static Vector2 MeasureTextScaled(const char *text, float size) {
Font f = GetFontDefault();
return MeasureTextEx(f, text, size, 1.0f);
}
static void HotAddText(const char *text, Vector2 pos, float size, const char *title, const char *desc) {
Vector2 m = MeasureTextScaled(text, size);
HotAdd((Rectangle){ pos.x - 2, pos.y - 2, m.x + 4, m.y + 4 }, title, desc);
}
static bool DrawButton(Rectangle r, const char *label, float fontSize, Color bg, Color fg) {
Vector2 m = GetMousePosition();
bool hover = CheckCollisionPointRec(m, r);
bool click = hover && IsMouseButtonPressed(MOUSE_LEFT_BUTTON);
Color c = bg;
if (hover) {
c.r = (unsigned char)fminf(255.0f, c.r + 22.0f);
c.g = (unsigned char)fminf(255.0f, c.g + 22.0f);
c.b = (unsigned char)fminf(255.0f, c.b + 22.0f);
}
DrawRectangleRounded(r, 0.18f, 8, c);
DrawRectangleRoundedLinesEx(r, 0.18f, 8, 2.0f, Fade(fg, 0.7f));
Vector2 t = MeasureTextScaled(label, fontSize);
DrawTextScaled(label,
(Vector2){ r.x + (r.width - t.x) * 0.5f, r.y + (r.height - t.y) * 0.5f },
fontSize, fg);
return click;
}
static float DrawSlider(Rectangle r, float minv, float maxv, float *value, float fontSize) {
Vector2 m = GetMousePosition();
bool hover = CheckCollisionPointRec(m, r);
bool down = IsMouseButtonDown(MOUSE_LEFT_BUTTON);
if (hover && down) {
float t = (m.x - r.x) / r.width;
if (t < 0.0f) t = 0.0f;
if (t > 1.0f) t = 1.0f;
*value = minv + t * (maxv - minv);
}
float t = (*value - minv) / (maxv - minv);
float knobX = r.x + t * r.width;
DrawRectangleRounded(r, 0.45f, 8, (Color){ 32, 43, 60, 255 });
Rectangle fill = r;
fill.width *= t;
DrawRectangleRounded(fill, 0.45f, 8, (Color){ 86, 188, 255, 255 });
DrawCircleV((Vector2){ knobX, r.y + r.height * 0.5f }, r.height * 0.45f,
hover ? (Color){ 240, 248, 255, 255 } : (Color){ 206, 232, 255, 255 });
char buf[64];
snprintf(buf, sizeof(buf), "%.2f", *value);
DrawTextScaled(buf, (Vector2){ r.x + r.width + 10, r.y - 4 }, fontSize, (Color){ 220, 235, 255, 255 });
return *value;
}
static Mat4 Mat4Identity(void) {
Mat4 a = {0};
for (int i = 0; i < 4; i++) a.m[i][i] = 1.0f;
return a;
}
static Mat4 Mat4Add(Mat4 a, Mat4 b) {
Mat4 c = {0};
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
c.m[i][j] = a.m[i][j] + b.m[i][j];
return c;
}
static Mat4 Mat4Sub(Mat4 a, Mat4 b) {
Mat4 c = {0};
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
c.m[i][j] = a.m[i][j] - b.m[i][j];
return c;
}
static Mat4 Mat4Mul(Mat4 a, Mat4 b) {
Mat4 c = {0};
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
float s = 0.0f;
for (int k = 0; k < 4; k++) s += a.m[i][k] * b.m[k][j];
c.m[i][j] = s;
}
}
return c;
}
static Mat4 Mat4Transpose(Mat4 a) {
Mat4 t = {0};
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
t.m[i][j] = a.m[j][i];
return t;
}
static Mat2 Mat2Add(Mat2 a, Mat2 b) {
Mat2 c = {0};
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
c.m[i][j] = a.m[i][j] + b.m[i][j];
return c;
}
static Mat2 Mat2Inverse(Mat2 a) {
float det = a.m[0][0] * a.m[1][1] - a.m[0][1] * a.m[1][0];
if (fabsf(det) < 1e-6f) det = (det >= 0.0f) ? 1e-6f : -1e-6f;
float invd = 1.0f / det;
Mat2 inv = {0};
inv.m[0][0] = a.m[1][1] * invd;
inv.m[0][1] = -a.m[0][1] * invd;
inv.m[1][0] = -a.m[1][0] * invd;
inv.m[1][1] = a.m[0][0] * invd;
return inv;
}
static void ResetFilter(AppState *s) {
memset(s->x, 0, sizeof(s->x));
s->x[0] = -6.0f;
s->x[1] = -3.0f;
s->x[2] = 1.8f;
s->x[3] = 0.8f;
s->trueState[0] = -8.0f;
s->trueState[1] = -5.0f;
s->trueState[2] = 2.4f;
s->trueState[3] = 1.3f;
s->P = Mat4Identity();
s->P.m[0][0] = 4.0f;
s->P.m[1][1] = 4.0f;
s->P.m[2][2] = 1.5f;
s->P.m[3][3] = 1.5f;
s->Q = (Mat4){0};
s->Q.m[0][0] = 0.02f;
s->Q.m[1][1] = 0.02f;
s->Q.m[2][2] = 0.06f;
s->Q.m[3][3] = 0.06f;
s->H = (Mat24){0};
UpdateMeasurementJacobian(s, s->x);
s->R = (Mat2){0};
s->R.m[0][0] = 0.65f; // range variance
s->R.m[1][1] = 0.030f; // bearing variance (rad^2)
s->phase = PHASE_SIMULATE;
s->simTime = 0.0f;
s->hasMeasurement = false;
s->histCount = 0;
s->histHead = 0;
// Prefill sensor history so the Goal table/plots are populated immediately.
const int seedN = 24;
float sigmaR = sqrtf(s->R.m[0][0]);
float sigmaB = sqrtf(s->R.m[1][1]);
for (int i = 0; i < seedN; i++) {
float tt = i * s->dt;
float tpx = s->trueState[0] + s->trueState[2] * tt;
float tpy = s->trueState[1] + s->trueState[3] * tt;
float ppx = s->x[0] + s->x[2] * tt;
float ppy = s->x[1] + s->x[3] * tt;
float tr = sqrtf(tpx * tpx + tpy * tpy);
float tb = atan2f(tpy, tpx);
float pr = sqrtf(fmaxf(1e-6f, ppx * ppx + ppy * ppy));
float pb = atan2f(ppy, ppx);
float zr = tr + sigmaR * 0.35f * sinf(0.7f * i);
float zb = tb + sigmaB * 0.40f * cosf(0.5f * i);
while (zb > PI_F) zb -= 2.0f * PI_F;
while (zb < -PI_F) zb += 2.0f * PI_F;
float yr = zr - pr;
float yb = zb - pb;
while (yb > PI_F) yb -= 2.0f * PI_F;
while (yb < -PI_F) yb += 2.0f * PI_F;
int idx = s->histHead;
s->histT[idx] = tt;
s->histZR[idx] = zr;
s->histZB[idx] = zb;
s->histPR[idx] = pr;
s->histPB[idx] = pb;
s->histYR[idx] = yr;
s->histYB[idx] = yb;
s->histHead = (s->histHead + 1) % SENSOR_HIST_MAX;
if (s->histCount < SENSOR_HIST_MAX) s->histCount++;
}
}
static Mat4 TransitionMatrix(float dt) {
Mat4 F = Mat4Identity();
F.m[0][2] = dt;
F.m[1][3] = dt;
return F;
}
static float NormalizeAngle(float a) {
while (a > PI_F) a -= 2.0f * PI_F;
while (a < -PI_F) a += 2.0f * PI_F;
return a;
}
static void UpdateMeasurementJacobian(AppState *s, const float x[4]) {
float px = x[0];
float py = x[1];
float r2 = px * px + py * py;
float r = sqrtf(fmaxf(r2, 1e-6f));
if (r2 < 1e-6f) r2 = 1e-6f;
s->H = (Mat24){0};
s->H.m[0][0] = px / r;
s->H.m[0][1] = py / r;
s->H.m[1][0] = -py / r2;
s->H.m[1][1] = px / r2;
}
static void SimulateTruthAndMeasurement(AppState *s) {
float t = s->simTime;
float ax = 0.7f * sinf(0.7f * t) + 0.25f * cosf(1.5f * t);
float ay = 0.65f * cosf(0.8f * t);
s->trueState[2] += ax * s->dt;
s->trueState[3] += ay * s->dt;
s->trueState[0] += s->trueState[2] * s->dt;
s->trueState[1] += s->trueState[3] * s->dt;
float px = s->trueState[0];
float py = s->trueState[1];
float range = sqrtf(px * px + py * py);
float bearing = atan2f(py, px);
float sigmaR = sqrtf(s->R.m[0][0]);
float sigmaB = sqrtf(s->R.m[1][1]);
s->z[0] = range + sigmaR * RandNormal();
s->z[1] = NormalizeAngle(bearing + sigmaB * RandNormal());
s->hasMeasurement = true;
}
static void PredictState(AppState *s) {
Mat4 F = TransitionMatrix(s->dt);
float nx[4] = {0};
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
nx[i] += F.m[i][j] * s->x[j];
memcpy(s->x, nx, sizeof(nx));
}
static void PredictCovariance(AppState *s) {
Mat4 F = TransitionMatrix(s->dt);
s->P = Mat4Add(Mat4Mul(Mat4Mul(F, s->P), Mat4Transpose(F)), s->Q);
}
static void ComputeInnovation(AppState *s) {
float px = s->x[0];
float py = s->x[1];
float range = sqrtf(fmaxf(1e-6f, px * px + py * py));
float bearing = atan2f(py, px);
float hx[2] = { range, bearing };
UpdateMeasurementJacobian(s, s->x);
s->y[0] = s->z[0] - hx[0];
s->y[1] = NormalizeAngle(s->z[1] - hx[1]);
int idx = s->histHead;
s->histT[idx] = s->simTime;
s->histZR[idx] = s->z[0];
s->histZB[idx] = s->z[1];
s->histPR[idx] = hx[0];
s->histPB[idx] = hx[1];
s->histYR[idx] = s->y[0];
s->histYB[idx] = s->y[1];
s->histHead = (s->histHead + 1) % SENSOR_HIST_MAX;
if (s->histCount < SENSOR_HIST_MAX) s->histCount++;
float HP[2][4] = {0};
for (int i = 0; i < 2; i++)
for (int j = 0; j < 4; j++)
for (int k = 0; k < 4; k++)
HP[i][j] += s->H.m[i][k] * s->P.m[k][j];
Mat2 S = {0};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
float v = 0.0f;
for (int k = 0; k < 4; k++) v += HP[i][k] * s->H.m[j][k];
S.m[i][j] = v;
}
}
s->S = Mat2Add(S, s->R);
}
static void ComputeGain(AppState *s) {
Mat2 SInv = Mat2Inverse(s->S);
float PHt[4][2] = {0};
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 2; j++) {
float v = 0.0f;
for (int k = 0; k < 4; k++) v += s->P.m[i][k] * s->H.m[j][k];
PHt[i][j] = v;
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 2; j++) {
s->K[i][j] = PHt[i][0] * SInv.m[0][j] + PHt[i][1] * SInv.m[1][j];
}
}
}
static void UpdateFilter(AppState *s) {
for (int i = 0; i < 4; i++) {
s->x[i] += s->K[i][0] * s->y[0] + s->K[i][1] * s->y[1];
}
Mat4 I = Mat4Identity();
Mat4 KH = {0};
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
KH.m[i][j] = s->K[i][0] * s->H.m[0][j] + s->K[i][1] * s->H.m[1][j];
}
}
s->P = Mat4Mul(Mat4Sub(I, KH), s->P);
s->simTime += s->dt;
}
static void StepPhase(AppState *s) {
switch (s->phase) {
case PHASE_SIMULATE:
SimulateTruthAndMeasurement(s);
s->phase = PHASE_PREDICT_STATE;
break;
case PHASE_PREDICT_STATE:
PredictState(s);
s->phase = PHASE_PREDICT_COV;
break;
case PHASE_PREDICT_COV:
PredictCovariance(s);
s->phase = PHASE_INNOVATION;
break;
case PHASE_INNOVATION:
if (s->hasMeasurement) {
ComputeInnovation(s);
s->phase = PHASE_GAIN;
}
break;
case PHASE_GAIN:
ComputeGain(s);
s->phase = PHASE_UPDATE;
break;
case PHASE_UPDATE:
UpdateFilter(s);
s->phase = PHASE_SIMULATE;
break;
default:
s->phase = PHASE_SIMULATE;
break;
}
}
static Vector2 WorldToScreen(AppState *s, Rectangle view, Vector2 p) {
Vector2 center = { view.x + view.width * 0.5f, view.y + view.height * 0.5f };
return (Vector2){ center.x + p.x * s->worldScale, center.y - p.y * s->worldScale };
}
static void DrawCovarianceEllipse(AppState *s, Rectangle view, Color color) {
float a = s->P.m[0][0];
float b = s->P.m[0][1];
float c = s->P.m[1][1];
float tr = a + c;
float det = a * c - b * b;
float disc = fmaxf(0.0f, tr * tr * 0.25f - det);
float l1 = tr * 0.5f + sqrtf(disc);
float l2 = tr * 0.5f - sqrtf(disc);
if (l1 < 1e-4f) l1 = 1e-4f;
if (l2 < 1e-4f) l2 = 1e-4f;
float angle = 0.5f * atan2f(2.0f * b, a - c);
float rx = 2.2f * sqrtf(l1);
float ry = 2.2f * sqrtf(l2);
Vector2 centerW = { s->x[0], s->x[1] };
const int N = 64;
Vector2 prev = {0};
for (int i = 0; i <= N; i++) {
float t = (float)i / (float)N * 2.0f * PI_F;
float ex = rx * cosf(t);
float ey = ry * sinf(t);
float wx = centerW.x + ex * cosf(angle) - ey * sinf(angle);
float wy = centerW.y + ex * sinf(angle) + ey * cosf(angle);
Vector2 p = WorldToScreen(s, view, (Vector2){ wx, wy });
if (i > 0) DrawLineEx(prev, p, 2.4f, color);
prev = p;
}
}
static void DrawWorldGrid(AppState *s, Rectangle view) {
DrawRectangleRounded(view, 0.02f, 4, (Color){ 19, 27, 39, 255 });
DrawRectangleRoundedLinesEx(view, 0.02f, 4, 2.0f, (Color){ 82, 119, 159, 120 });
for (int i = -12; i <= 12; i++) {
Vector2 a = WorldToScreen(s, view, (Vector2){ (float)i, -12.0f });
Vector2 b = WorldToScreen(s, view, (Vector2){ (float)i, 12.0f });
DrawLineEx(a, b, (i == 0) ? 2.2f : 1.0f, (i == 0) ? (Color){ 120, 180, 235, 150 } : (Color){ 70, 95, 120, 75 });
}
for (int j = -12; j <= 12; j++) {
Vector2 a = WorldToScreen(s, view, (Vector2){ -12.0f, (float)j });
Vector2 b = WorldToScreen(s, view, (Vector2){ 12.0f, (float)j });
DrawLineEx(a, b, (j == 0) ? 2.2f : 1.0f, (j == 0) ? (Color){ 120, 180, 235, 150 } : (Color){ 70, 95, 120, 75 });
}
HotAdd(view,
"State Space",
"2D position space. True target, noisy sensor point, EKF estimate, velocity vector, and the covariance ellipse live here.");
}
static void DrawStateViews(AppState *s, Rectangle view, float fontSize) {
DrawWorldGrid(s, view);
Vector2 pTrue = WorldToScreen(s, view, (Vector2){ s->trueState[0], s->trueState[1] });
Vector2 pEst = WorldToScreen(s, view, (Vector2){ s->x[0], s->x[1] });
float mzx = s->z[0] * cosf(s->z[1]);
float mzy = s->z[0] * sinf(s->z[1]);
Vector2 pZ = WorldToScreen(s, view, (Vector2){ mzx, mzy });
Vector2 pSensor = WorldToScreen(s, view, (Vector2){ 0.0f, 0.0f });
DrawCovarianceEllipse(s, view, (Color){ 255, 205, 94, 220 });
DrawCircleV(pTrue, 7.5f, (Color){ 113, 234, 143, 255 });
DrawCircleV(pZ, 6.0f, (Color){ 255, 110, 110, 240 });
DrawCircleV(pEst, 7.5f, (Color){ 110, 200, 255, 255 });
DrawCircleV(pSensor, 6.0f, (Color){ 255, 226, 150, 240 });
DrawLineEx(pSensor, pZ, 1.3f, (Color){ 255, 208, 130, 160 });
DrawCircleLines((int)pSensor.x, (int)pSensor.y, s->z[0] * s->worldScale, (Color){ 255, 208, 130, 70 });
Vector2 vHead = WorldToScreen(s, view, (Vector2){ s->x[0] + s->x[2] * 0.8f, s->x[1] + s->x[3] * 0.8f });
DrawLineEx(pEst, vHead, 2.6f, (Color){ 130, 205, 255, 220 });
DrawCircleV(vHead, 4.0f, (Color){ 130, 205, 255, 220 });
DrawLineEx(pEst, pZ, 1.9f, (Color){ 255, 130, 130, 220 });
DrawTextScaled("true", (Vector2){ pTrue.x + 8, pTrue.y - 16 }, fontSize * 0.78f, (Color){ 170, 245, 180, 255 });
DrawTextScaled("measurement", (Vector2){ pZ.x + 8, pZ.y - 16 }, fontSize * 0.78f, (Color){ 255, 170, 170, 255 });
DrawTextScaled("estimate", (Vector2){ pEst.x + 8, pEst.y - 16 }, fontSize * 0.78f, (Color){ 168, 218, 255, 255 });
DrawTextScaled("sensor", (Vector2){ pSensor.x + 8, pSensor.y - 16 }, fontSize * 0.72f, (Color){ 255, 228, 162, 255 });
HotAddText("true", (Vector2){ pTrue.x + 8, pTrue.y - 16 }, fontSize * 0.78f, "True Label", "Ground-truth trajectory used only for debugging, never fed into the filter.");
HotAddText("measurement", (Vector2){ pZ.x + 8, pZ.y - 16 }, fontSize * 0.78f, "Measurement Label", "Sensor output after noise. This is what EKF actually receives.");
HotAddText("estimate", (Vector2){ pEst.x + 8, pEst.y - 16 }, fontSize * 0.78f, "Estimate Label", "Current posterior state estimate after EKF correction.");
HotAddText("sensor", (Vector2){ pSensor.x + 8, pSensor.y - 16 }, fontSize * 0.72f, "Sensor Label", "Range-bearing sensor origin.");
HotAdd((Rectangle){ pTrue.x - 10, pTrue.y - 10, 20, 20 },
"True State",
"Ground truth from simulation. The EKF never directly sees this value.");
HotAdd((Rectangle){ pEst.x - 10, pEst.y - 10, 20, 20 },
"Estimated State x",
"Filter estimate x = [px, py, vx, vy]. This is what EKF believes right now.");
HotAdd((Rectangle){ pZ.x - 9, pZ.y - 9, 18, 18 },
"Measurement z",
"Noisy sensor reading in polar (range, bearing), plotted in Cartesian for intuition.");
HotAdd((Rectangle){ pSensor.x - 9, pSensor.y - 9, 18, 18 },
"Sensor Origin",
"Range/bearing sensor frame origin. Circles and rays show measured geometry.");
HotAdd((Rectangle){ pEst.x - 2, pEst.y - 2, vHead.x - pEst.x + 4, vHead.y - pEst.y + 4 },
"Velocity Vector",
"Heading and speed from the estimated velocity components.");
}
static void DrawCovBars(AppState *s, Rectangle r, float fontSize) {
DrawRectangleRounded(r, 0.04f, 6, (Color){ 20, 30, 43, 255 });
DrawRectangleRoundedLinesEx(r, 0.04f, 6, 1.5f, (Color){ 90, 130, 170, 120 });
const char *labels[4] = { "Pxx", "Pyy", "Pvx", "Pvy" };
float vals[4] = { s->P.m[0][0], s->P.m[1][1], s->P.m[2][2], s->P.m[3][3] };
float maxv = 8.0f;
for (int i = 0; i < 4; i++) {
float y = r.y + 30 + i * 40;
Vector2 lp = { r.x + 10, y };
DrawTextScaled(labels[i], lp, fontSize * 0.8f, (Color){ 196, 215, 235, 255 });
const char *ld[4] = {
"Variance of position x error.",
"Variance of position y error.",
"Variance of velocity x error.",
"Variance of velocity y error."
};
HotAddText(labels[i], lp, fontSize * 0.8f, labels[i], ld[i]);
Rectangle bar = { r.x + 70, y + 3, r.width - 85, 18 };
DrawRectangleRounded(bar, 0.35f, 6, (Color){ 40, 58, 78, 255 });
Rectangle fill = bar;
fill.width *= fminf(vals[i] / maxv, 1.0f);
DrawRectangleRounded(fill, 0.35f, 6, (Color){ 255, 202, 102, 240 });
HotAddFmt(bar, "Covariance Bar", "%s bar. Normalized uncertainty %.1f%% of displayed range.", labels[i], fminf(vals[i] / maxv, 1.0f) * 100.0f);
char buf[32];
snprintf(buf, sizeof(buf), "%.3f", vals[i]);
Vector2 vp = { bar.x + bar.width - 55, y };
DrawTextScaled(buf, vp, fontSize * 0.74f, (Color){ 244, 234, 188, 255 });
Vector2 vm = MeasureTextScaled(buf, fontSize * 0.74f);
HotAddFmt((Rectangle){ vp.x - 2, vp.y - 2, vm.x + 4, vm.y + 4 },
"Covariance Value", "%s = %.5f. This is variance, not standard deviation.", labels[i], vals[i]);
}
Vector2 covTitle = { r.x + 10, r.y + 8 };
DrawTextScaled("Uncertainty by state axis", covTitle, fontSize * 0.86f, (Color){ 220, 234, 250, 255 });
HotAddText("Uncertainty by state axis", covTitle, fontSize * 0.86f, "Covariance Summary", "Bar chart of uncertainty magnitudes for each state component.");
HotAdd(r,
"Covariance Diagonal",
"How uncertain the filter is in each state component. Bigger means less confidence.");
}
static void DrawArrowLine(Vector2 a, Vector2 b, float thick, Color c) {
DrawLineEx(a, b, thick, c);
Vector2 d = (Vector2){ b.x - a.x, b.y - a.y };
float n = sqrtf(d.x * d.x + d.y * d.y);
if (n < 1e-3f) return;
d.x /= n;
d.y /= n;
Vector2 p = (Vector2){ -d.y, d.x };
float h = 8.0f + thick * 0.9f;
Vector2 l = (Vector2){ b.x - d.x * h, b.y - d.y * h };
Vector2 t1 = (Vector2){ l.x + p.x * (4.0f + thick), l.y + p.y * (4.0f + thick) };
Vector2 t2 = (Vector2){ l.x - p.x * (4.0f + thick), l.y - p.y * (4.0f + thick) };
DrawTriangle(b, t1, t2, c);
}
static Color JacobianCellColor(float v, float vmax) {
float t = fminf(fabsf(v) / vmax, 1.0f);
if (v >= 0.0f) {
return (Color){
(unsigned char)(50 + 130 * t),
(unsigned char)(80 + 150 * t),
(unsigned char)(120 + 90 * t),
235
};
}
return (Color){
(unsigned char)(120 + 120 * t),
(unsigned char)(65 + 70 * t),
(unsigned char)(50 + 80 * t),
235
};
}
static void DrawMatrixHeatmap4(Rectangle r, float m[4][4], const char *title, const char *sym, float fontSize) {
static const char *stateName[4] = { "px", "py", "vx", "vy" };
DrawTextScaled(title, (Vector2){ r.x + 8, r.y + 6 }, fontSize * 0.78f, (Color){ 222, 235, 250, 255 });
float x = r.x + 8;
float y = r.y + 28;
float cw = (r.width - 16) / 4.0f;
float ch = (r.height - 36) / 4.0f;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
Rectangle c = { x + j * cw, y + i * ch, cw - 2, ch - 2 };
DrawRectangleRounded(c, 0.14f, 4, JacobianCellColor(m[i][j], 1.0f));
char b[32];
snprintf(b, sizeof(b), "%.2f", m[i][j]);
DrawTextScaled(b, (Vector2){ c.x + 4, c.y + ch * 0.25f }, fontSize * 0.58f, (Color){ 246, 246, 248, 255 });
HotAddFmt(c, "Jacobian Cell",
"%s[%d,%d] = %.4f. Local derivative d(%s_next)/d(%s).",
sym, i, j, m[i][j], stateName[i], stateName[j]);
}
}
}
static void DrawMatrixHeatmap24(Rectangle r, Mat24 m, const char *title, const char *sym, float fontSize) {
static const char *measName[2] = { "range", "bearing" };
static const char *stateName[4] = { "px", "py", "vx", "vy" };
DrawTextScaled(title, (Vector2){ r.x + 8, r.y + 6 }, fontSize * 0.78f, (Color){ 222, 235, 250, 255 });
float x = r.x + 8;
float y = r.y + 28;
float cw = (r.width - 16) / 4.0f;
float ch = (r.height - 36) / 2.0f;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
Rectangle c = { x + j * cw, y + i * ch, cw - 2, ch - 2 };
DrawRectangleRounded(c, 0.14f, 4, JacobianCellColor(m.m[i][j], 1.0f));
char b[32];
snprintf(b, sizeof(b), "%.2f", m.m[i][j]);
DrawTextScaled(b, (Vector2){ c.x + 4, c.y + ch * 0.24f }, fontSize * 0.56f, (Color){ 246, 246, 248, 255 });
HotAddFmt(c, "Jacobian Cell",
"%s[%d,%d] = %.4f. Local derivative d(%s)/d(%s).",
sym, i, j, m.m[i][j], measName[i], stateName[j]);
}
}
}
static void DrawJacobianPanel(AppState *s, Rectangle r, float fontSize) {
DrawRectangleRounded(r, 0.04f, 6, (Color){ 20, 30, 43, 255 });
DrawRectangleRoundedLinesEx(r, 0.04f, 6, 1.5f, (Color){ 90, 130, 170, 120 });
DrawTextScaled("Jacobian view (visual + numeric)", (Vector2){ r.x + 10, r.y + 8 }, fontSize * 0.86f, (Color){ 220, 234, 250, 255 });
Rectangle top = { r.x + 8, r.y + 28, r.width - 16, r.height * 0.58f };
Rectangle geom = { r.x + 8, top.y + top.height + 6, r.width - 16, r.height - (top.height + 42) };
Rectangle fRect = { top.x, top.y, top.width * 0.62f, top.height };
Rectangle hRect = { fRect.x + fRect.width + 8, top.y, top.width - (fRect.width + 8), top.height };
Mat4 F = TransitionMatrix(s->dt);
DrawMatrixHeatmap4(fRect, F.m, "F = df/dx (4x4)", "F", fontSize);
DrawMatrixHeatmap24(hRect, s->H, "H = dh/dx (2x4)", "H", fontSize);
DrawRectangleRounded(geom, 0.08f, 6, (Color){ 14, 22, 34, 255 });
DrawRectangleRoundedLinesEx(geom, 0.08f, 6, 1.2f, (Color){ 86, 120, 160, 140 });
DrawTextScaled("Geometric influence graph", (Vector2){ geom.x + 8, geom.y + 6 }, fontSize * 0.72f, (Color){ 245, 210, 140, 255 });
float t = (float)GetTime();
float pulse = 0.6f + 0.4f * sinf(t * 4.2f);
float sy = geom.y + geom.height * 0.56f;
Vector2 npx = { geom.x + 48, sy - 20 };
Vector2 npy = { geom.x + 48, sy + 20 };
Vector2 nvx = { geom.x + 132, sy - 20 };
Vector2 nvy = { geom.x + 132, sy + 20 };
Vector2 mzx = { geom.x + geom.width - 54, sy - 20 };
Vector2 mzy = { geom.x + geom.width - 54, sy + 20 };
Vector2 nodes[4] = { npx, npy, nvx, nvy };
const char *labels[4] = { "px", "py", "vx", "vy" };
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
float w = F.m[i][j];
if (fabsf(w) < 0.01f) continue;
float thick = 1.0f + 2.4f * fabsf(w);
Color c = (w >= 0.0f) ? (Color){ 122, 204, 255, (unsigned char)(180 + 60 * pulse) }
: (Color){ 255, 155, 120, (unsigned char)(180 + 60 * pulse) };
DrawArrowLine(nodes[j], nodes[i], thick, c);
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
float w = s->H.m[i][j];
if (fabsf(w) < 0.01f) continue;
Vector2 mNode = (i == 0) ? mzx : mzy;
float thick = 1.2f + 2.2f * fabsf(w);
DrawArrowLine(nodes[j], mNode, thick, (Color){ 255, 218, 118, (unsigned char)(190 + 55 * pulse) });
}
}
for (int i = 0; i < 4; i++) {
DrawCircleV(nodes[i], 11, (Color){ 62, 98, 136, 255 });
DrawCircleLines((int)nodes[i].x, (int)nodes[i].y, 11, (Color){ 175, 212, 245, 255 });
DrawTextScaled(labels[i], (Vector2){ nodes[i].x - 7, nodes[i].y - 6 }, fontSize * 0.58f, (Color){ 228, 240, 255, 255 });
}
DrawCircleV(mzx, 11, (Color){ 114, 92, 48, 255 });
DrawCircleV(mzy, 11, (Color){ 114, 92, 48, 255 });
DrawCircleLines((int)mzx.x, (int)mzx.y, 11, (Color){ 255, 223, 158, 255 });
DrawCircleLines((int)mzy.x, (int)mzy.y, 11, (Color){ 255, 223, 158, 255 });
DrawTextScaled("zx", (Vector2){ mzx.x - 7, mzx.y - 6 }, fontSize * 0.56f, (Color){ 255, 238, 196, 255 });
DrawTextScaled("zy", (Vector2){ mzy.x - 7, mzy.y - 6 }, fontSize * 0.56f, (Color){ 255, 238, 196, 255 });
HotAdd(fRect,
"State Jacobian F",
"Linearization of process model f around current estimate. It maps state perturbations into predicted perturbations.");
HotAdd(hRect,
"Measurement Jacobian H",
"Linearization of measurement model h. It maps state perturbations into measurement perturbations.");
HotAdd(geom,
"Jacobian Geometry",
"Node graph of partial derivatives. Arrows show who influences who; thicker means stronger local sensitivity.");
HotAdd(r,
"Jacobian View",
"Heatmap intensity shows sensitivity magnitude. Blue is positive, warm is negative.");
}
static void DrawPhaseVisualLab(AppState *s, Rectangle r, float fontSize) {
DrawRectangleRounded(r, 0.05f, 6, (Color){ 12, 18, 28, 255 });
DrawRectangleRoundedLinesEx(r, 0.05f, 6, 1.2f, (Color){ 92, 130, 176, 120 });
Vector2 phaseLabPos = { r.x + 10, r.y + 8 };
DrawTextScaled("Phase Visual Lab", phaseLabPos, fontSize * 0.82f, (Color){ 248, 216, 132, 255 });
HotAddText("Phase Visual Lab", phaseLabPos, fontSize * 0.82f, "Phase Visual Lab", "Animated, phase-specific geometric explanation of the active EKF step.");
float t = (float)GetTime();
float pulse = 0.6f + 0.4f * sinf(t * 4.0f);
Rectangle c = { r.x + 8, r.y + 30, r.width - 16, r.height - 38 };
Vector2 o = { c.x + c.width * 0.5f, c.y + c.height * 0.52f };
if (s->phase == PHASE_SIMULATE) {
Vector2 p = { c.x + 4, c.y + 4 };
DrawTextScaled("simulate noisy sensor data", p, fontSize * 0.62f, (Color){ 186, 214, 255, 255 });
HotAddText("simulate noisy sensor data", p, fontSize * 0.62f, "Simulate Phase", "Creates synthetic truth and noisy sensor sample for this cycle.");
float tr = sqrtf(s->trueState[0] * s->trueState[0] + s->trueState[1] * s->trueState[1]);
float tb = atan2f(s->trueState[1], s->trueState[0]);
float mr = s->z[0];
float mb = s->z[1];
Vector2 pTrue = { o.x + cosf(tb) * tr * 16.0f, o.y - sinf(tb) * tr * 16.0f };
Vector2 pMeas = { o.x + cosf(mb) * mr * 16.0f, o.y - sinf(mb) * mr * 16.0f };
DrawCircleLines((int)o.x, (int)o.y, tr * 16.0f, (Color){ 118, 182, 255, 120 });
DrawLineEx(o, pTrue, 1.8f, (Color){ 120, 220, 140, 220 });
DrawLineEx(o, pMeas, 1.8f, (Color){ 255, 120, 120, 220 });
for (int i = 0; i < 24; i++) {
float a = mb + (i - 12) * 0.02f;
float rr = mr + 0.22f * sinf(t * 5.0f + i);
Vector2 q = { o.x + cosf(a) * rr * 16.0f, o.y - sinf(a) * rr * 16.0f };
DrawCircleV(q, 1.8f, (Color){ 255, 155, 155, (unsigned char)(60 + i * 6) });
}
DrawCircleV(o, 5.5f, (Color){ 255, 222, 152, 230 });
DrawCircleV(pTrue, 5.0f, (Color){ 118, 232, 148, 255 });
DrawCircleV(pMeas, 5.0f, (Color){ 255, 115, 115, 255 });
} else if (s->phase == PHASE_PREDICT_STATE) {
Vector2 p = { c.x + 4, c.y + 4 };
DrawTextScaled("state propagation x^- = f(x)", p, fontSize * 0.62f, (Color){ 186, 214, 255, 255 });
HotAddText("state propagation x^- = f(x)", p, fontSize * 0.62f, "Predict State Phase", "Applies the process model to move state estimate forward in time.");
Vector2 x0 = { c.x + 42, o.y + 18 };
Vector2 x1 = { x0.x + s->x[2] * s->dt * 120.0f, x0.y - s->x[3] * s->dt * 120.0f };
DrawCircleV(x0, 6.5f, (Color){ 126, 176, 240, 220 });
DrawArrowLine(x0, x1, 3.0f, (Color){ 126, 212, 255, 245 });
for (int i = 1; i <= 5; i++) {
float f = (float)i / 5.0f;
Vector2 p = { x0.x + (x1.x - x0.x) * f, x0.y + (x1.y - x0.y) * f };
DrawCircleV(p, 2.5f, (Color){ 140, 214, 255, (unsigned char)(80 + 30 * i) });
}
DrawCircleV(x1, 7.0f + 2.0f * pulse, (Color){ 100, 230, 255, (unsigned char)(120 + 80 * pulse) });
} else if (s->phase == PHASE_PREDICT_COV) {
Vector2 p = { c.x + 4, c.y + 4 };
DrawTextScaled("covariance transform P^- = FPF^T + Q", p, fontSize * 0.62f, (Color){ 186, 214, 255, 255 });
HotAddText("covariance transform P^- = FPF^T + Q", p, fontSize * 0.62f, "Predict Covariance Phase", "Pushes uncertainty through dynamics and adds process noise.");
float e1 = sqrtf(fmaxf(0.01f, s->P.m[0][0])) * 18.0f;
float e2 = sqrtf(fmaxf(0.01f, s->P.m[1][1])) * 18.0f;
for (int i = 0; i < 28; i++) {
float a = (float)i / 28.0f * 2.0f * PI_F;
Vector2 p = { o.x + cosf(a) * e1 * (0.7f + 0.3f * pulse), o.y + sinf(a) * e2 * (0.7f + 0.3f * pulse) };
DrawCircleV(p, 1.8f, (Color){ 255, 214, 124, 220 });
}
DrawEllipseLines((int)o.x, (int)o.y, e1 * 0.82f, e2 * 0.82f, (Color){ 120, 184, 255, 140 });
DrawEllipseLines((int)o.x, (int)o.y, e1, e2, (Color){ 255, 202, 112, 230 });
} else if (s->phase == PHASE_INNOVATION) {
Vector2 p = { c.x + 4, c.y + 4 };
DrawTextScaled("innovation y = z - h(x^-)", p, fontSize * 0.62f, (Color){ 186, 214, 255, 255 });
HotAddText("innovation y = z - h(x^-)", p, fontSize * 0.62f, "Innovation Phase", "Computes residual between actual and predicted measurement.");
float hr = sqrtf(fmaxf(1e-6f, s->x[0] * s->x[0] + s->x[1] * s->x[1]));
float hb = atan2f(s->x[1], s->x[0]);
Vector2 pPred = { o.x + cosf(hb) * hr * 15.0f, o.y - sinf(hb) * hr * 15.0f };
Vector2 pMeas = { o.x + cosf(s->z[1]) * s->z[0] * 15.0f, o.y - sinf(s->z[1]) * s->z[0] * 15.0f };
DrawCircleV(pPred, 6.0f, (Color){ 128, 204, 255, 240 });
DrawCircleV(pMeas, 6.0f, (Color){ 255, 122, 122, 240 });
DrawArrowLine(pPred, pMeas, 2.6f, (Color){ 255, 180, 110, 240 });
DrawCircleLines((int)o.x, (int)o.y, hr * 15.0f, (Color){ 130, 190, 250, 90 });
DrawCircleLines((int)o.x, (int)o.y, s->z[0] * 15.0f, (Color){ 255, 168, 130, 100 });
} else if (s->phase == PHASE_GAIN) {
Vector2 p = { c.x + 4, c.y + 4 };
DrawTextScaled("gain K maps innovation into state", p, fontSize * 0.62f, (Color){ 186, 214, 255, 255 });
HotAddText("gain K maps innovation into state", p, fontSize * 0.62f, "Gain Phase", "Builds optimal correction gain from uncertainty and sensor trust.");
Vector2 yNode = { c.x + 44, o.y };
DrawCircleV(yNode, 13.0f, (Color){ 255, 132, 122, 235 });
DrawTextScaled("y", (Vector2){ yNode.x - 5, yNode.y - 7 }, fontSize * 0.7f, RAYWHITE);
for (int i = 0; i < 4; i++) {
Vector2 st = { c.x + c.width - 56, c.y + 34 + i * ((c.height - 52) / 4.0f) };
float kMag = sqrtf(s->K[i][0] * s->K[i][0] + s->K[i][1] * s->K[i][1]);
DrawArrowLine(yNode, st, 1.2f + 6.0f * fminf(1.0f, kMag), (Color){ 130, 214, 255, (unsigned char)(120 + 120 * pulse) });
DrawCircleV(st, 8.5f, (Color){ 92, 140, 205, 245 });
char b[8];
snprintf(b, sizeof(b), "x%d", i);
DrawTextScaled(b, (Vector2){ st.x - 8, st.y - 6 }, fontSize * 0.56f, (Color){ 234, 242, 255, 255 });
}
} else if (s->phase == PHASE_UPDATE) {
Vector2 p = { c.x + 4, c.y + 4 };
DrawTextScaled("update blends prediction + correction", p, fontSize * 0.62f, (Color){ 186, 214, 255, 255 });
HotAddText("update blends prediction + correction", p, fontSize * 0.62f, "Update Phase", "Applies correction and shrinks uncertainty for posterior state.");
Vector2 a = { c.x + 50, o.y + 15 };
Vector2 b = { c.x + c.width - 58, o.y - 10 };
float alpha = 0.5f + 0.45f * sinf(t * 3.4f);
Vector2 m = { a.x + (b.x - a.x) * alpha, a.y + (b.y - a.y) * alpha };
DrawCircleV(a, 7.0f, (Color){ 120, 180, 240, 220 });
DrawCircleV(b, 8.0f, (Color){ 90, 238, 220, 245 });
DrawArrowLine(a, b, 2.0f, (Color){ 145, 215, 255, 140 });
DrawCircleV(m, 6.5f + 2.5f * pulse, (Color){ 255, 214, 128, (unsigned char)(120 + 110 * pulse) });
DrawTextScaled("x^-", (Vector2){ a.x - 12, a.y + 12 }, fontSize * 0.58f, (Color){ 176, 208, 245, 255 });
DrawTextScaled("x", (Vector2){ b.x - 4, b.y + 12 }, fontSize * 0.58f, (Color){ 176, 238, 224, 255 });
}
}
static void DrawCodeLineVisual(AppState *s, Rectangle r, float fontSize,
int phase, int row, const char *line, const char *desc) {
DrawRectangleRounded(r, 0.05f, 6, (Color){ 10, 16, 26, 255 });
DrawRectangleRoundedLinesEx(r, 0.05f, 6, 1.2f, (Color){ 104, 146, 198, 130 });
char header[128];
snprintf(header, sizeof(header), "Line Visual: phase %d line %d", phase, row + 1);
DrawTextScaled(header, (Vector2){ r.x + 10, r.y + 8 }, fontSize * 0.74f, (Color){ 255, 220, 145, 255 });
DrawTextScaled(line, (Vector2){ r.x + 10, r.y + 28 }, fontSize * 0.64f, (Color){ 206, 230, 255, 255 });
DrawTextScaled(desc, (Vector2){ r.x + 10, r.y + 48 }, fontSize * 0.58f, (Color){ 166, 204, 238, 255 });
Rectangle c = { r.x + 8, r.y + 72, r.width - 16, r.height - 80 };
DrawRectangleRounded(c, 0.06f, 6, (Color){ 14, 23, 36, 255 });
DrawRectangleRoundedLinesEx(c, 0.06f, 6, 1.0f, (Color){ 88, 122, 166, 120 });
float t = (float)GetTime();
float pulse = 0.55f + 0.45f * sinf(t * 4.0f + (float)(phase * 9 + row));
Vector2 o = { c.x + c.width * 0.5f, c.y + c.height * 0.5f };
if (phase == PHASE_SIMULATE) {
if (row == 0) {
float rr = sqrtf(s->trueState[0] * s->trueState[0] + s->trueState[1] * s->trueState[1]) * 14.0f;
DrawCircleLines((int)o.x, (int)o.y, rr, (Color){ 120, 200, 255, 220 });
DrawCircleV(o, 5, (Color){ 255, 230, 150, 255 });
} else if (row == 1) {
float b = atan2f(s->trueState[1], s->trueState[0]);
Vector2 p = { o.x + cosf(b) * 120.0f, o.y - sinf(b) * 120.0f };
DrawArrowLine(o, p, 3.0f, (Color){ 120, 230, 170, 240 });
} else if (row == 2) {
for (int i = 0; i < 40; i++) {
float a = (float)i * 0.17f + t;
float rj = s->z[0] * 14.0f + 16.0f * sinf(t * 5.0f + i);
Vector2 p = { o.x + cosf(a) * rj, o.y + sinf(a) * rj };
DrawCircleV(p, 1.8f, (Color){ 255, 138, 138, 190 });
}
} else if (row == 3) {
float b = s->z[1];
for (int i = -16; i <= 16; i++) {
float a = b + i * 0.02f;
Vector2 p = { o.x + cosf(a) * 130.0f, o.y - sinf(a) * 130.0f };
DrawLineEx(o, p, 1.2f, (Color){ 255, 200, 120, (unsigned char)(70 + 4 * (16 - abs(i))) });
}
} else {
DrawCircleV(o, 22.0f + 12.0f * pulse, (Color){ 122, 216, 255, (unsigned char)(80 + 120 * pulse) });
DrawTextScaled("measurement ready", (Vector2){ o.x - 52, o.y - 6 }, fontSize * 0.58f, RAYWHITE);
}
} else if (phase == PHASE_PREDICT_STATE) {
float sx = c.x + 30;
float sy = c.y + c.height * 0.7f;
if (row == 0) {
for (int i = 0; i < 4; i++) {
DrawRectangleRounded((Rectangle){ sx + i * 52, sy - 70, 42, 42 }, 0.18f, 4, (Color){ 78, 116, 168, 220 });
}
} else if (row == 1) {
Vector2 a = { sx, sy };
Vector2 b = { sx + s->x[2] * s->dt * 100.0f + 130.0f, sy - s->x[3] * s->dt * 100.0f - 60.0f };
DrawArrowLine(a, b, 3.2f, (Color){ 122, 214, 255, 240 });
for (int i = 1; i <= 7; i++) {
float u = (float)i / 7.0f;
DrawCircleV((Vector2){ a.x + (b.x - a.x) * u, a.y + (b.y - a.y) * u }, 2.0f, (Color){ 170, 232, 255, 180 });
}
} else {
DrawRectangleRounded((Rectangle){ sx + 160, sy - 55, 120, 90 }, 0.2f, 6, (Color){ 94, 176, 220, (unsigned char)(70 + 120 * pulse) });
DrawTextScaled("x <- nx", (Vector2){ sx + 192, sy - 12 }, fontSize * 0.66f, (Color){ 235, 245, 255, 255 });
}
} else if (phase == PHASE_PREDICT_COV) {
if (row == 0) {
DrawRectangleRounded((Rectangle){ c.x + 24, c.y + 24, 74, 54 }, 0.12f, 5, (Color){ 95, 143, 204, 220 });
DrawRectangleRounded((Rectangle){ c.x + 130, c.y + 24, 74, 54 }, 0.12f, 5, (Color){ 95, 143, 204, 220 });
DrawRectangleRounded((Rectangle){ c.x + 236, c.y + 24, 74, 54 }, 0.12f, 5, (Color){ 95, 143, 204, 220 });
} else if (row == 1 || row == 2) {
float rx = sqrtf(fmaxf(0.01f, s->P.m[0][0])) * 18.0f;
float ry = sqrtf(fmaxf(0.01f, s->P.m[1][1])) * 18.0f;
DrawEllipseLines((int)o.x, (int)o.y, rx * (0.75f + 0.25f * pulse), ry * (0.75f + 0.25f * pulse), (Color){ 255, 206, 112, 230 });
DrawEllipseLines((int)o.x, (int)o.y, rx * 0.6f, ry * 0.6f, (Color){ 130, 196, 255, 180 });
} else {
for (int i = 0; i < 12; i++) {
float a = t * 2.2f + i * 0.52f;
DrawCircleV((Vector2){ o.x + cosf(a) * 95, o.y + sinf(a) * 55 }, 3.0f, (Color){ 255, 182, 108, 210 });