aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 7200ffe2a8e0e88e7926c66145c972f05a0a5fa1 (plain)
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
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <raylib.h>
#include <rlgl.h>
#include <raymath.h>

#define RAYGUI_IMPLEMENTATION
#include <raygui.h>

#include "retiming.h"
#include "cma-es.h"

#define RGBA(HEX) (Color) {   \
     (HEX) >> 3*8 & 0xFF,     \
     (HEX) >> 2*8 & 0xFF,     \
     (HEX) >> 1*8 & 0xFF,     \
     (HEX) >> 0*8 & 0xFF,     \
  }

// #define LABEL_WIDTH  140
#define LABEL_WIDTH  300
#define SLIDER_WIDTH 40
#define ITEM_WIDTH (LABEL_WIDTH + SLIDER_WIDTH)
#define ITEM_HEIGHT 15
#define ITEM_MARGIN 5
#define PANEL_WIDTH (ITEM_WIDTH + 2*ITEM_MARGIN)
#define PANEL_HEIGHT(N) (RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT + (N)*(ITEM_HEIGHT + ITEM_MARGIN) + ITEM_MARGIN)

#ifdef DARK
#define BACKGROUND RGBA (0x000000FF)
#else
#define BACKGROUND RGBA (0xFFFFFFFF)
#endif

#define ARROW_THICKNESS  2.5
#define ARROW_LENGTH    15
#define ARROW_ANGLE     10

#define CONTROL_POINT_RADIUS          5
#define CONTROL_POINT_RADIUS_FOCUSED 10

enum menu {
  MENU_VISUALIZATION,
  MENU_INPUT,
  MENU_CHROMOSOME,
  MENU_METRICS,
  MENU_WEIGHTS,
  MENU_COUNT
} menu = MENU_VISUALIZATION;

const char *menu_to_label[] = {
  [MENU_VISUALIZATION] = "VISUALIZATION",
  [MENU_INPUT] = "INPUT",
  [MENU_CHROMOSOME] = "CHROMOSOME",
  [MENU_METRICS] = "METRICS",
  [MENU_WEIGHTS] = "WEIGHTS",
};

struct fitness_breakdown fb;
struct fitness_weights w = {
  .time           = 1.5,
  .length         = 1.5,
  .max_abs_kappa  = 30,
  .max_abs_dkappa = 30,
  .max_abs_a      = 30,
  .max_abs_j      = 25,
  .rms_kappa      = 2,
  .rms_dkappa     = 4,
  .rms_a          = 3,
  .rms_j          = 0.35,
  .violation_a    = 3.5,
  .violation_j    = 0.2,
  .terminal       = 100,
};

void
draw_arrow (Vector2 beg, Vector2 end, float thick, Color c)
{
  Vector2 off = Vector2Scale (Vector2Normalize (Vector2Subtract (end, beg)),
                              ARROW_LENGTH);
  Vector2 ahl = Vector2Add (end, Vector2Rotate (off, +ARROW_ANGLE));
  Vector2 ahr = Vector2Add (end, Vector2Rotate (off, -ARROW_ANGLE));

  DrawLineEx (beg, end, thick, c);
  DrawLineEx (end, ahl, thick, c);
  DrawLineEx (end, ahr, thick, c);

  DrawCircleV (beg, thick/2., c);
  DrawCircleV (end, thick/2., c);
  DrawCircleV (ahl, thick/2., c);
  DrawCircleV (ahr, thick/2., c);
}

void
draw_bezier5 (bezier5 cp)
{
  char text[] = "P0";
  DrawLineStrip (cp, 6, BLUE);
  for (int i = 0; i < 6; ++i) {
    DrawCircleLinesV (cp[i], 4, BLUE);
    text[1] = '0' + i;
    DrawText (text, cp[i].x - 5, cp[i].y + 7, 14, BLACK);
  }
}

void
chromosome_to_control_points (bezier5 cp, struct input in, struct chromosome ch)
{
  Vector2 e_beg = Vector2Normalize (in.v_beg);
  Vector2 e_end = Vector2Normalize (in.v_end);
  Vector2 n_beg = { -e_beg.y, +e_beg.x };
  Vector2 n_end = { -e_end.y, +e_end.x };

  cp[0] = in.r_beg;
  cp[1] = Vector2Add (in.r_beg, Vector2Scale (e_beg, +ch.rho*ch.tau_1));
  cp[2] = Vector2Add (in.r_beg, Vector2Scale (Vector2Add (Vector2Scale (e_beg, ch.alp_2), Vector2Scale (n_beg, ch.bet_2)), +ch.rho));
  cp[3] = Vector2Add (in.r_end, Vector2Scale (Vector2Add (Vector2Scale (e_end, ch.alp_3), Vector2Scale (n_end, ch.bet_3)), -ch.rho));
  cp[4] = Vector2Add (in.r_end, Vector2Scale (e_end, -ch.rho*ch.tau_4));
  cp[5] = in.r_end;
}

struct chromosome
rand_chromosome (void)
{
  return (struct chromosome) {
    10 + 50 * (rand () /(float) RAND_MAX),
    .5 +  5 * (rand () /(float) RAND_MAX),
    .5 +  5 * (rand () /(float) RAND_MAX),
    .5 +  5 * (rand () /(float) RAND_MAX),
    .5 +  5 * (rand () /(float) RAND_MAX),
    .5 +  5 * (rand () /(float) RAND_MAX),
    .5 +  5 * (rand () /(float) RAND_MAX),
  };
}

float sample_offset = 10;
float sample_spacing = PATH_SAMPLE_COUNT/4;
bool sample_dr = true;
bool sample_ddr = true;

Color
lerp_color (Color a, Color b, float t)
{
  Color c;
  c.r = (unsigned char) (a.r + (b.r - a.r)*t);
  c.g = (unsigned char) (a.g + (b.g - a.g)*t);
  c.b = (unsigned char) (a.b + (b.b - a.b)*t);
  c.a = (unsigned char) (a.a + (b.a - a.a)*t);
  return c;
}

float ease_out_cubic (float x) { return 1 - powf (1 - x, 3); }
float ease_out_quint (float x) { return 1 - powf (1 - x, 5); }
float ease_out_sin   (float x) { return   sinf (PI*x/2); }
float ease_inout_sin (float x) { return -(cosf (PI*x) - 1)/2; }

void
compute_ribbon_join (const struct path_table *p, int i, float half,
                     Vector2 *l, Vector2 *r)
{
  Vector2 t;

  if (i == 0) {
    t = Vector2Normalize (Vector2Subtract (p->r[1], p->r[0]));
  } else if (i == PATH_SAMPLE_COUNT - 1) {
    t = Vector2Normalize (Vector2Subtract (p->r[i], p->r[i - 1]));
  } else {
    t = Vector2Normalize (Vector2Subtract (p->r[i + 1], p->r[i - 1]));
  }

  Vector2 n = { -t.y, t.x };

  *l = Vector2Add (p->r[i], Vector2Scale (n,  half));
  *r = Vector2Add (p->r[i], Vector2Scale (n, -half));
}

void
draw_path_gradient_ribbon (const struct path_table *p,
                           float thick, Color c0, Color c1)
{
  if (PATH_SAMPLE_COUNT < 2 || thick <= 0) return;

  float half = thick/2;

  Vector2 l0, r0, l1, r1;
  compute_ribbon_join (p, 0, half, &l0, &r0);

  rlBegin (RL_TRIANGLES);
  for (int i = 0; i < PATH_SAMPLE_COUNT - 1; ++i) {
    compute_ribbon_join (p, i + 1, half, &l1, &r1);

    float u0 = ease_out_cubic (fabsf (p->kappa[i + 0])/p->max_abs_kappa);
    float u1 = ease_out_cubic (fabsf (p->kappa[i + 1])/p->max_abs_kappa);

    Color col0 = lerp_color (c0, c1, u0);
    Color col1 = lerp_color (c0, c1, u1);

    rlColor4ub (col0.r, col0.g, col0.b, col0.a); rlVertex2f (l0.x, l0.y);
    rlColor4ub (col1.r, col1.g, col1.b, col1.a); rlVertex2f (l1.x, l1.y);
    rlColor4ub (col0.r, col0.g, col0.b, col0.a); rlVertex2f (r0.x, r0.y);

    rlColor4ub (col0.r, col0.g, col0.b, col0.a); rlVertex2f (r0.x, r0.y);
    rlColor4ub (col1.r, col1.g, col1.b, col1.a); rlVertex2f (l1.x, l1.y);
    rlColor4ub (col1.r, col1.g, col1.b, col1.a); rlVertex2f (r1.x, r1.y);

    l0 = l1;
    r0 = r1;
  } rlEnd ();
}

void
draw_path (const struct path_table *p, float thick)
{
  const int c = PATH_SAMPLE_COUNT;
  // DrawCircleV (p->r[    0], thick/2., BLUE);
  // DrawCircleV (p->r[c - 1], thick/2., BLUE);
  draw_path_gradient_ribbon (p, thick, BLUE, RED);
  for (int i = sample_offset; i < c; i += sample_spacing) {
    Vector2 r = p->r[i];
    if (sample_dr ) draw_arrow (r, Vector2Add (r, Vector2Scale (Vector2Normalize (p-> dr[i]), 30)), thick/2, MAGENTA);
    if (sample_ddr) draw_arrow (r, Vector2Add (r, Vector2Scale (Vector2Normalize (p->ddr[i]), 30)), thick/2, PURPLE);
  }
}

void
plot_motion_profile (Rectangle bb, const struct motion_profile *mp,
                     float v_max, float a_max, float j_max)
{
  const int n = PATH_SAMPLE_COUNT;
  static Vector2 line[PATH_SAMPLE_COUNT];

  float t = mp->t[n - 1];
  for (int i = 0; i < n; ++i)
    line[i].x = Lerp (bb.x, bb.x + bb.width, mp->t[i]/t);

  // axis
  Vector2 origin = { bb.x, bb.y + bb.height /(float) 2 };
  DrawLineEx (origin, Vector2Add (origin, (Vector2) { bb.width, 0 }), 1, GRAY);
  for (int i = 0; i < n; ++i) {
    DrawLineEx ((Vector2) { line[i].x, origin.y + 5 },
                (Vector2) { line[i].x, origin.y - 5 }, 1, GRAY);
  }

  // velocity
  for (int i = 0; i < n; ++i)
    line[i].y = Lerp (origin.y, bb.y + 2, mp->v[i]/fabsf (mp->v_max));
  DrawSplineBezierCubic (line, n, 1, BLUE);

  // acceleration
  for (int i = 0; i < n; ++i)
    line[i].y = Lerp (origin.y, bb.y + 2, mp->a[i]/fabsf (mp->a_max));
  DrawSplineBezierCubic (line, n, 1, ORANGE);

  // jerk
  for (int i = 0; i < n; ++i)
    line[i].y = Lerp (origin.y, bb.y + 2, mp->j[i]/fabsf (mp->j_max));
  DrawSplineBezierCubic (line, n, 1, RED);
}

int
panel_slider (int x, int y, int c, const char *label, const char *fmt, float *var, float min, float max)
{
  GuiLabel            ((Rectangle) { x + ITEM_MARGIN,               y + c*(ITEM_HEIGHT + ITEM_MARGIN),  LABEL_WIDTH, ITEM_HEIGHT }, TextFormat ("%s: %s", label, TextFormat (fmt, *var)));
  return GuiSliderBar ((Rectangle) { x + ITEM_MARGIN + LABEL_WIDTH, y + c*(ITEM_HEIGHT + ITEM_MARGIN), SLIDER_WIDTH, ITEM_HEIGHT }, NULL, NULL, var, min, max);
}

int
panel_var (int x, int y, int c, const char *label, const char *fmt, float *var)
{
  GuiLabel ((Rectangle) { x + ITEM_MARGIN, y + c*(ITEM_HEIGHT + ITEM_MARGIN), ITEM_WIDTH, ITEM_HEIGHT },
            TextFormat ("%s: %s", label, TextFormat (fmt, *var)));
  return 0;
}

int
panel_bool (int x, int y, int c, const char *label, bool *checked)
{
  return GuiCheckBox ((Rectangle) {
    x + ITEM_MARGIN, y + c*(ITEM_HEIGHT + ITEM_MARGIN),
    ITEM_HEIGHT, ITEM_HEIGHT
  }, label, checked);
}

int
main (int argc, char *argv[])
{
  srand (time (NULL));

  SetConfigFlags (FLAG_MSAA_4X_HINT);
  SetConfigFlags (FLAG_WINDOW_RESIZABLE);
  InitWindow (800, 450, "GA");
  SetTargetFPS (60);

  Camera2D camera = {
    .zoom = 1.0f,
  };

  Vector2 screen = {
    .x = GetScreenWidth  (),
    .y = GetScreenHeight (),
  };

  GuiSetStyle(DEFAULT, TEXT_SIZE, 20);
  // GuiGetStyle ();

  struct input in = {
    .r_beg = { 100, 100 },
    // .r_end = { 100, 100 },
    .r_end = { 300, 300 },
    .v_beg = { 100,  10 },
    .v_end = { 100,  10 },
    .v_max = 250,
    .a_max = 500,
    .j_max = 16,
  };

  bezier5 cp;
  struct chromosome ch = rand_chromosome ();
  struct path_table pt;
  struct motion_profile mp;

  float arrow_thickness = ARROW_THICKNESS;
  float spline_thickness = ARROW_THICKNESS;
  bool move_r_beg = false;
  bool move_r_end = false;
  bool move_v_beg = false;
  bool move_v_end = false;
  bool recompute = true;
  bool show_fps = true;
  bool show_menu = true;
  bool show_graph = true;
  bool draw_cp = true;
  bool animate_sample_offset = false;
  float animation_speed = 20;
  int panel_item = 0;
  bool resizing_graph = false;
  int graph_height = screen.y/3;
  while (!WindowShouldClose ()) {
    screen = (Vector2) {
      .x = GetScreenWidth  (),
      .y = GetScreenHeight (),
    };
    float  t = GetTime      ();
    float dt = GetFrameTime ();

    if (IsMouseButtonDown (MOUSE_BUTTON_RIGHT)) {
      Vector2 delta = GetMouseDelta ();
      delta = Vector2Scale (delta, -1.0f/camera.zoom);
      camera.target = Vector2Add (camera.target, delta);
    }

    if (IsKeyPressed (KEY_LEFT )) menu = menu ? menu - 1 : MENU_COUNT - 1;
    if (IsKeyPressed (KEY_RIGHT)) menu = menu < MENU_COUNT - 1 ? menu + 1 : 0;

    if (IsKeyPressed (KEY_R)) {
      ch = rand_chromosome ();
      recompute = true;
    }

    if (IsKeyDown (KEY_O)) {
      struct chromosome res;
      float cost;
      example_cmaes_usage (&in, &ch, &w, &res, &cost);
      ch = res;
      recompute = true;
    }

    if (IsKeyPressed (KEY_M)) show_menu = !show_menu;
    if (IsKeyPressed (KEY_G)) show_graph = !show_graph;
    if (IsKeyPressed (KEY_F)) show_fps = !show_fps;

    if (recompute) {
      chromosome_to_control_points (cp, in, ch);
      build_path_table (&pt, cp);
      build_motion_profile (&mp, &pt,
                            Vector2Length (in.v_beg),
                            Vector2Length (in.v_end),
                            in.v_max, in.a_max, in.j_max);
      fb = evaluate_motion_fitness (&pt, &mp,
                                    Vector2Length (in.v_beg),
                                    Vector2Length (in.v_end),
                                    in.v_max, in.a_max, in.j_max, &w);

      recompute = false;
    }

    float wheel = GetMouseWheelMove();
    if (wheel != 0) {
      Vector2 offset = GetMousePosition ();
      camera.target = GetScreenToWorld2D (offset, camera);
      camera.offset = offset;
      camera.zoom = Clamp (expf (logf (camera.zoom) + .2*wheel), .125, 64);
    }

    Vector2 mouse_hud = GetMousePosition ();
    Vector2 mouse = GetScreenToWorld2D (mouse_hud, camera);

    if (IsMouseButtonDown (MOUSE_BUTTON_LEFT)
        && mouse_hud.y > screen.y - graph_height
        && mouse_hud.y < screen.y - graph_height + RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT) {
      resizing_graph = true;
    }

    if (resizing_graph) {
      graph_height = Clamp (RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT + 2, screen.y - 4,
                            screen.y - mouse_hud.y + RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT/2);
      if (IsMouseButtonReleased (MOUSE_BUTTON_LEFT))
        resizing_graph = false;
    }

    BeginDrawing ();
    ClearBackground (BACKGROUND);
    BeginMode2D (camera);

    if (CheckCollisionPointCircle (mouse, in.r_beg, 10)) {
      DrawCircleLinesV (in.r_beg, 10, BLUE);
      if (!move_v_beg && IsMouseButtonDown (MOUSE_BUTTON_LEFT))
        move_r_beg = true;
    } else if (CheckCollisionPointCircle (mouse, in.r_end, 10)) {
      DrawCircleLinesV (in.r_end, 10, BLUE);
      if (!move_v_end && IsMouseButtonDown (MOUSE_BUTTON_LEFT))
        move_r_end = true;
    } else if (CheckCollisionPointCircle (mouse, Vector2Add (in.r_beg, in.v_beg), 10)) {
      DrawCircleLinesV (Vector2Add (in.r_beg, in.v_beg), 10, BLUE);
      if (!move_r_beg && IsMouseButtonDown (MOUSE_BUTTON_LEFT))
        move_v_beg = true;
    } else if (CheckCollisionPointCircle (mouse, Vector2Add (in.r_end, in.v_end), 10)) {
      DrawCircleLinesV (Vector2Add (in.r_end, in.v_end), 10, BLUE);
      if (!move_r_end && IsMouseButtonDown (MOUSE_BUTTON_LEFT))
        move_v_end = true;
    }

    if (move_r_beg) {
      in.r_beg = mouse;
      if (IsMouseButtonReleased (MOUSE_BUTTON_LEFT))
        move_r_beg = false;
    }
    if (move_r_end) {
      in.r_end = mouse;
      if (IsMouseButtonReleased (MOUSE_BUTTON_LEFT))
        move_r_end = false;
    }
    if (move_v_beg) {
      in.v_beg = Vector2Subtract (mouse, in.r_beg);
      if (IsMouseButtonReleased (MOUSE_BUTTON_LEFT))
        move_v_beg = false;
    }
    if (move_v_end) {
      in.v_end = Vector2Subtract (mouse, in.r_end);
      if (IsMouseButtonReleased (MOUSE_BUTTON_LEFT))
        move_v_end = false;
    }

    if (animate_sample_offset)
    sample_offset = (int) (animation_speed*t) % (int) sample_spacing;

    // rlPushMatrix ();
    // rlTranslatef (0, 25*50, 0);
    // rlRotatef (90, 1, 0, 0);
    // DrawGrid (100, 50);
    // rlPopMatrix ();

    draw_path (&pt, spline_thickness);
    if (draw_cp) draw_bezier5 (cp);
    draw_arrow (in.r_beg, Vector2Add (in.r_beg, in.v_beg), arrow_thickness, GREEN);
    draw_arrow (in.r_end, Vector2Add (in.r_end, in.v_end), arrow_thickness, RED);

    EndMode2D ();

    if (move_r_beg ||
        move_r_end ||
        move_v_beg ||
        move_v_end) {
      GuiLock ();
      recompute = true;
    }

    if (show_menu) {
      int x = 2;
      int y = 2;

      GuiPanel ((Rectangle) { x, y, PANEL_WIDTH, PANEL_HEIGHT (panel_item) }, menu_to_label[menu]);
      y += RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT;
      y += ITEM_MARGIN;
      panel_item = 0;
      switch (menu) {
      case MENU_VISUALIZATION:
        panel_slider (x, y, panel_item++, "Arrow",   "%.2f",  &arrow_thickness,  1,  10);
        panel_slider (x, y, panel_item++, "Spline",  "%.2f",  &spline_thickness, 1,  10);
        panel_slider (x, y, panel_item++, "Spacing", "%.0f",  &sample_spacing,   2,  PATH_SAMPLE_COUNT - 1);
        panel_slider (x, y, panel_item++, "Offset",  "%.0f",  &sample_offset,    0,  sample_spacing);
        panel_slider (x, y, panel_item++, "Speed",   "%.0f",  &animation_speed,  0,  30);
        panel_bool   (x, y, panel_item++, "Animate offset", &animate_sample_offset);
        panel_bool   (x, y, panel_item++, "Draw dr",  &sample_dr);
        panel_bool   (x, y, panel_item++, "Draw ddr", &sample_ddr);
        panel_bool   (x, y, panel_item++, "Draw cp",  &draw_cp);
        break;

      case MENU_INPUT:
        recompute |= panel_slider (x, y, panel_item++, "v_lim", "%.2f",  &in.v_max, fmaxf (Vector2Length (in.v_beg), Vector2Length (in.v_beg)), 500);
        recompute |= panel_slider (x, y, panel_item++, "a_lim", "%.2f",  &in.a_max, 1, 500);
        recompute |= panel_slider (x, y, panel_item++, "j_lim", "%.2f",  &in.j_max, 1, 100);
        panel_var (x, y, panel_item++, "r_beg.x", "%.2f", &in.r_beg.x);
        panel_var (x, y, panel_item++, "r_beg.y", "%.2f", &in.r_beg.y);
        panel_var (x, y, panel_item++, "r_end.x", "%.2f", &in.r_end.x);
        panel_var (x, y, panel_item++, "r_end.y", "%.2f", &in.r_end.y);
        panel_var (x, y, panel_item++, "v_beg.x", "%.2f", &in.v_beg.x);
        panel_var (x, y, panel_item++, "v_beg.y", "%.2f", &in.v_beg.y);
        panel_var (x, y, panel_item++, "v_end.x", "%.2f", &in.v_end.x);
        panel_var (x, y, panel_item++, "v_end.y", "%.2f", &in.v_end.y);
        break;

      case MENU_CHROMOSOME:
        recompute |= panel_slider (x, y, panel_item++, "rho",     "%.2f",  &ch.rho,    1, 1000);
        recompute |= panel_slider (x, y, panel_item++, "tau_1",   "%.2f",  &ch.tau_1,  0,   10);
        recompute |= panel_slider (x, y, panel_item++, "alpha_2", "%.2f",  &ch.alp_2,  0,   10);
        recompute |= panel_slider (x, y, panel_item++, "beta_2",  "%+.2f", &ch.bet_2, -5,    5);
        recompute |= panel_slider (x, y, panel_item++, "alpha_3", "%.2f",  &ch.alp_3,  0,   10);
        recompute |= panel_slider (x, y, panel_item++, "beta_3",  "%+.2f", &ch.bet_3, -5,    5);
        recompute |= panel_slider (x, y, panel_item++, "tau_4",   "%.2f",  &ch.tau_4,  0,   10);
        break;

      case MENU_WEIGHTS:
        recompute |= panel_slider (x, y, panel_item++, "w_time",        "%.2f", &w.time,           0,  10);
        recompute |= panel_slider (x, y, panel_item++, "w_length",      "%.2f", &w.length,         0,  10);
        recompute |= panel_slider (x, y, panel_item++, "w_max_kappa",   "%.2f", &w.max_abs_kappa,  0, 100);
        recompute |= panel_slider (x, y, panel_item++, "w_max_dkappa",  "%.2f", &w.max_abs_dkappa, 0, 100);
        recompute |= panel_slider (x, y, panel_item++, "w_max_a",       "%.2f", &w.max_abs_a,      0, 100);
        recompute |= panel_slider (x, y, panel_item++, "w_max_j",       "%.2f", &w.max_abs_j,      0, 100);
        recompute |= panel_slider (x, y, panel_item++, "w_rms_kappa",   "%.2f", &w.rms_kappa,      0,  10);
        recompute |= panel_slider (x, y, panel_item++, "w_rms_dkappa",  "%.2f", &w.rms_dkappa,     0,  10);
        recompute |= panel_slider (x, y, panel_item++, "w_rms_a",       "%.2f", &w.rms_a,          0,  10);
        recompute |= panel_slider (x, y, panel_item++, "w_rms_j",       "%.2f", &w.rms_j,          0,   1);
        recompute |= panel_slider (x, y, panel_item++, "w_violation_a", "%.2f", &w.violation_a,    0,  10);
        recompute |= panel_slider (x, y, panel_item++, "w_violation_j", "%.2f", &w.violation_j,    0,   1);
        recompute |= panel_slider (x, y, panel_item++, "w_terminal",    "%.2f", &w.terminal,       0, 100);
        break;

      case MENU_METRICS:
        panel_var (x, y, panel_item++, "Length",   "%.2f", &pt.s[PATH_SAMPLE_COUNT - 1]);
        panel_var (x, y, panel_item++, "Duration", "%.2f", &mp.t[PATH_SAMPLE_COUNT - 1]);
        panel_var (x, y, panel_item++, "Cost", "%.2f", &fb.total_cost);

        panel_var (x, y, panel_item++, "v_max", "%.2f", &mp.v_max);
        panel_var (x, y, panel_item++, "a_max", "%.2f", &mp.a_max);
        panel_var (x, y, panel_item++, "j_max", "%.2f", &mp.j_max);

        panel_var (x, y, panel_item++, "time_cost",            "%.2f", &fb.time_cost);
        panel_var (x, y, panel_item++, "length_cost",          "%.2f", &fb.length_cost);
        panel_var (x, y, panel_item++, "kappa_cost",           "%.2f", &fb.kappa_cost);
        panel_var (x, y, panel_item++, "dkappa_cost",          "%.2f", &fb.dkappa_cost);
        panel_var (x, y, panel_item++, "accel_rms_cost",       "%.2f", &fb.accel_rms_cost);
        panel_var (x, y, panel_item++, "jerk_rms_cost",        "%.2f", &fb.jerk_rms_cost);
        panel_var (x, y, panel_item++, "accel_violation_cost", "%.2f", &fb.accel_violation_cost);
        panel_var (x, y, panel_item++, "jerk_violation_cost",  "%.2f", &fb.jerk_violation_cost);
        panel_var (x, y, panel_item++, "terminal_cost",        "%.2f", &fb.terminal_cost);
        panel_var (x, y, panel_item++, "failure_cost",         "%.2f", &fb.failure_cost);
        panel_var (x, y, panel_item++, "duration",             "%.2f", &fb.duration);
        panel_var (x, y, panel_item++, "length",               "%.2f", &fb.length);
        panel_var (x, y, panel_item++, "kappa_rms",            "%.2f", &fb.kappa_rms);
        panel_var (x, y, panel_item++, "dkappa_rms",           "%.2f", &fb.dkappa_rms);
        panel_var (x, y, panel_item++, "accel_rms",            "%.2f", &fb.accel_rms);
        panel_var (x, y, panel_item++, "jerk_rms",             "%.2f", &fb.jerk_rms);
        panel_var (x, y, panel_item++, "accel_violation",      "%.2f", &fb.accel_violation);
        panel_var (x, y, panel_item++, "jerk_violation",       "%.2f", &fb.jerk_violation);
        break;
      default: break;
      }
    }
    GuiUnlock ();

    if (show_graph) {
      Rectangle bb;
      bb.x = 2;
      bb.width = screen.x - 4;
      bb.height = graph_height;
      bb.y = screen.y - 2 - bb.height;
      GuiPanel (bb, "GRAPH");
      bb.y      += RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT;
      bb.height -= RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT;
      plot_motion_profile (bb, &mp, in.v_max, in.a_max, in.j_max);
    }

    if (show_fps)
      DrawFPS (screen.x - 75, 0);
    EndDrawing ();
  }

  CloseWindow ();

  return EXIT_SUCCESS;
}