aboutsummaryrefslogtreecommitdiff
path: root/src/bez7.c
blob: d63014c1924c27aaa194df054cd8b3242cb4de86 (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
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <raylib.h>
#include <rlgl.h>
#include <raymath.h>

typedef Vector2 bezier7[8];

struct input {
  Vector2 r_beg;
  Vector2 r_end;
  Vector2 v_beg;
  Vector2 v_end;
  float v_max;
  float a_max;
  float j_max;
};

#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


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);
}

Vector2
sample_bezier7 (const bezier7 pts, float t)
{
    float u = 1.0f - t;

    float t2 = t * t;
    float t3 = t2 * t;
    float t4 = t3 * t;
    float t5 = t4 * t;
    float t6 = t5 * t;
    float t7 = t6 * t;

    /* Precompute powers of u (1 - t) */
    float u2 = u * u;
    float u3 = u2 * u;
    float u4 = u3 * u;
    float u5 = u4 * u;
    float u6 = u5 * u;
    float u7 = u6 * u;

    /* Bernstein basis polynomials for degree 7 */
    float b0 = u7;
    float b1 = 7.0f  * u6 * t;
    float b2 = 21.0f * u5 * t2;
    float b3 = 35.0f * u4 * t3;
    float b4 = 35.0f * u3 * t4;
    float b5 = 21.0f * u2 * t5;
    float b6 = 7.0f  * u  * t6;
    float b7 = t7;

    Vector2 result;

    /* Calculate final x and y by blending control points with basis weights */
    result.x = (b0 * pts[0].x) + (b1 * pts[1].x) + (b2 * pts[2].x) + (b3 * pts[3].x) +
               (b4 * pts[4].x) + (b5 * pts[5].x) + (b6 * pts[6].x) + (b7 * pts[7].x);

    result.y = (b0 * pts[0].y) + (b1 * pts[1].y) + (b2 * pts[2].y) + (b3 * pts[3].y) +
               (b4 * pts[4].y) + (b5 * pts[5].y) + (b6 * pts[6].y) + (b7 * pts[7].y);

    return result;
}

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

  for (float t = 0; t <= 1; t += 0.01) {
    DrawCircleV (sample_bezier7 (cp, t), 2, BLACK);
  }
}

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 (),
  };

  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,
  };

  bezier7 cp;

  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;
  float T = 10;
  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_F)) show_fps = !show_fps;

    if (IsKeyDown (KEY_LEFT )) T -= .1;
    if (IsKeyDown (KEY_RIGHT)) T += .1;


    if (1 || recompute) {
      // chromosome_to_control_points (cp, in, ch);
      cp[0] = in.r_beg;
      cp[1] = Vector2Add (in.r_beg, Vector2Scale (in.v_beg, +1*T/7));
      cp[2] = Vector2Add (in.r_beg, Vector2Scale (in.v_beg, +2*T/7));
      cp[3] = Vector2Add (in.r_beg, Vector2Scale (in.v_beg, +3*T/7));
      cp[4] = Vector2Add (in.r_end, Vector2Scale (in.v_end, -3*T/7));
      cp[5] = Vector2Add (in.r_end, Vector2Scale (in.v_end, -2*T/7));
      cp[6] = Vector2Add (in.r_end, Vector2Scale (in.v_end, -1*T/7));
      cp[7] = in.r_end;
      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);

    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;
    }

    draw_bezier7 (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) {
      recompute = true;
    }

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

  CloseWindow ();

  return EXIT_SUCCESS;
}