diff options
| author | 2026-11-11 22:28:13 +0000 | |
|---|---|---|
| committer | 2026-11-11 22:47:34 +0000 | |
| commit | cb070b2d3f2b6e241e3be1b7e0eeb90fc755255f (patch) | |
| tree | c7d90c0abe9533c87d51b5196871846855efa1ba /src/transition.c | |
| download | thesis-master.tar.gz | |
Diffstat (limited to 'src/transition.c')
| -rw-r--r-- | src/transition.c | 777 |
1 files changed, 777 insertions, 0 deletions
diff --git a/src/transition.c b/src/transition.c new file mode 100644 index 0000000..9910b27 --- /dev/null +++ b/src/transition.c | |||
| @@ -0,0 +1,777 @@ | |||
| 1 | #include "transition.h" | ||
| 2 | |||
| 3 | #include <float.h> | ||
| 4 | #include <math.h> | ||
| 5 | #include <string.h> | ||
| 6 | |||
| 7 | #define TR_EPS 1e-10 | ||
| 8 | #define TR_TIME_EPS 1e-9 | ||
| 9 | #define TR_SOL_EPS 1e-8 | ||
| 10 | #define TR_MAX_SCAN 2048 | ||
| 11 | #define TR_MAX_BISECT 96 | ||
| 12 | #define TR_INF 1e100 | ||
| 13 | |||
| 14 | static double | ||
| 15 | tr_abs (double x) | ||
| 16 | { | ||
| 17 | return x < 0.0 ? -x : x; | ||
| 18 | } | ||
| 19 | |||
| 20 | static int | ||
| 21 | tr_finite_pos (double x) | ||
| 22 | { | ||
| 23 | return isfinite (x) && x > 0.0; | ||
| 24 | } | ||
| 25 | |||
| 26 | static double | ||
| 27 | tr_clamp (double x, double lo, double hi) | ||
| 28 | { | ||
| 29 | return x < lo ? lo : (x > hi ? hi : x); | ||
| 30 | } | ||
| 31 | |||
| 32 | static int | ||
| 33 | tr_limits_valid (double vmax, double amax, double jmax) | ||
| 34 | { | ||
| 35 | return tr_finite_pos (vmax) && tr_finite_pos (amax) && tr_finite_pos (jmax); | ||
| 36 | } | ||
| 37 | |||
| 38 | struct tr_axis_limits { | ||
| 39 | double vmax; | ||
| 40 | double amax; | ||
| 41 | double jmax; | ||
| 42 | }; | ||
| 43 | |||
| 44 | struct tr_change { | ||
| 45 | double dt; | ||
| 46 | double ds; | ||
| 47 | }; | ||
| 48 | |||
| 49 | static struct tr_change | ||
| 50 | tr_speed_change (double v0, double v1, const struct tr_axis_limits *l) | ||
| 51 | { | ||
| 52 | struct tr_change c = {0}; | ||
| 53 | double dv = tr_abs (v1 - v0); | ||
| 54 | |||
| 55 | if (dv <= TR_EPS) | ||
| 56 | return c; | ||
| 57 | |||
| 58 | double dv_tri = l->amax*l->amax/l->jmax; | ||
| 59 | |||
| 60 | if (dv <= dv_tri) { | ||
| 61 | double tj = sqrt (dv/l->jmax); | ||
| 62 | c.dt = 2.0*tj; | ||
| 63 | } else { | ||
| 64 | double tj = l->amax/l->jmax; | ||
| 65 | double ta = dv/l->amax - tj; | ||
| 66 | c.dt = 2.0*tj + ta; | ||
| 67 | } | ||
| 68 | |||
| 69 | c.ds = 0.5*(v0 + v1)*c.dt; | ||
| 70 | return c; | ||
| 71 | } | ||
| 72 | |||
| 73 | static double | ||
| 74 | tr_axis_fixed_f (double vm, | ||
| 75 | double q0, | ||
| 76 | double q1, | ||
| 77 | double v0, | ||
| 78 | double v1, | ||
| 79 | double T, | ||
| 80 | const struct tr_axis_limits *l, | ||
| 81 | int *admissible_out) | ||
| 82 | { | ||
| 83 | struct tr_change c0 = tr_speed_change (v0, vm, l); | ||
| 84 | struct tr_change c1 = tr_speed_change (vm, v1, l); | ||
| 85 | double tc = T - c0.dt - c1.dt; | ||
| 86 | |||
| 87 | if (admissible_out) | ||
| 88 | *admissible_out = tc >= -1e-8; | ||
| 89 | |||
| 90 | return c0.ds + c1.ds + vm*tc - (q1 - q0); | ||
| 91 | } | ||
| 92 | |||
| 93 | static int | ||
| 94 | tr_fixed_vm_candidate (double vm, | ||
| 95 | double q0, | ||
| 96 | double q1, | ||
| 97 | double v0, | ||
| 98 | double v1, | ||
| 99 | double T, | ||
| 100 | const struct tr_axis_limits *l, | ||
| 101 | double *err_out, | ||
| 102 | double *tc_out) | ||
| 103 | { | ||
| 104 | if (vm < -l->vmax - 1e-9 || vm > l->vmax + 1e-9) | ||
| 105 | return 0; | ||
| 106 | |||
| 107 | struct tr_change c0 = tr_speed_change (v0, vm, l); | ||
| 108 | struct tr_change c1 = tr_speed_change (vm, v1, l); | ||
| 109 | double tc = T - c0.dt - c1.dt; | ||
| 110 | if (tc < -1e-8) | ||
| 111 | return 0; | ||
| 112 | |||
| 113 | double q = c0.ds + c1.ds + vm*tc; | ||
| 114 | double err = q - (q1 - q0); | ||
| 115 | |||
| 116 | if (err_out) *err_out = err; | ||
| 117 | if (tc_out) *tc_out = tc > 0.0 ? tc : 0.0; | ||
| 118 | return 1; | ||
| 119 | } | ||
| 120 | |||
| 121 | static int | ||
| 122 | tr_find_fixed_vm (double q0, | ||
| 123 | double q1, | ||
| 124 | double v0, | ||
| 125 | double v1, | ||
| 126 | double T, | ||
| 127 | const struct tr_axis_limits *l, | ||
| 128 | double *vm_out, | ||
| 129 | double *tc_out) | ||
| 130 | { | ||
| 131 | double best_vm = 0.0; | ||
| 132 | double best_tc = 0.0; | ||
| 133 | double best_abs = TR_INF; | ||
| 134 | int found_exact = 0; | ||
| 135 | |||
| 136 | double prev_vm = 0.0; | ||
| 137 | double prev_f = 0.0; | ||
| 138 | int prev_ok = 0; | ||
| 139 | |||
| 140 | for (int i = 0; i <= TR_MAX_SCAN; ++i) { | ||
| 141 | double u = (double) i / (double) TR_MAX_SCAN; | ||
| 142 | double vm = -l->vmax + 2.0*l->vmax*u; | ||
| 143 | double f = 0.0; | ||
| 144 | double tc = 0.0; | ||
| 145 | int ok = tr_fixed_vm_candidate (vm, q0, q1, v0, v1, T, l, &f, &tc); | ||
| 146 | |||
| 147 | if (ok) { | ||
| 148 | double af = tr_abs (f); | ||
| 149 | if (af < best_abs) { | ||
| 150 | best_abs = af; | ||
| 151 | best_vm = vm; | ||
| 152 | best_tc = tc; | ||
| 153 | } | ||
| 154 | |||
| 155 | if (af <= TR_SOL_EPS*fmax (1.0, tr_abs (q1 - q0))) { | ||
| 156 | *vm_out = vm; | ||
| 157 | *tc_out = tc; | ||
| 158 | return 1; | ||
| 159 | } | ||
| 160 | |||
| 161 | if (prev_ok && ((prev_f <= 0.0 && f >= 0.0) || (prev_f >= 0.0 && f <= 0.0))) { | ||
| 162 | double lo = prev_vm; | ||
| 163 | double hi = vm; | ||
| 164 | double flo = prev_f; | ||
| 165 | double fhi = f; | ||
| 166 | |||
| 167 | for (int k = 0; k < TR_MAX_BISECT; ++k) { | ||
| 168 | double mid = 0.5*(lo + hi); | ||
| 169 | int adm = 0; | ||
| 170 | double fm = tr_axis_fixed_f (mid, q0, q1, v0, v1, T, l, &adm); | ||
| 171 | if (!adm) { | ||
| 172 | /* This should not normally happen inside an admissible bracket. */ | ||
| 173 | break; | ||
| 174 | } | ||
| 175 | |||
| 176 | if (tr_abs (fm) <= TR_SOL_EPS*fmax (1.0, tr_abs (q1 - q0))) { | ||
| 177 | double ignored_err = 0.0; | ||
| 178 | tr_fixed_vm_candidate (mid, q0, q1, v0, v1, T, l, &ignored_err, &tc); | ||
| 179 | *vm_out = mid; | ||
| 180 | *tc_out = tc; | ||
| 181 | return 1; | ||
| 182 | } | ||
| 183 | |||
| 184 | if ((flo <= 0.0 && fm >= 0.0) || (flo >= 0.0 && fm <= 0.0)) { | ||
| 185 | hi = mid; | ||
| 186 | fhi = fm; | ||
| 187 | } else { | ||
| 188 | lo = mid; | ||
| 189 | flo = fm; | ||
| 190 | } | ||
| 191 | (void) fhi; | ||
| 192 | } | ||
| 193 | |||
| 194 | double mid = 0.5*(lo + hi); | ||
| 195 | double ignored_err = 0.0; | ||
| 196 | tr_fixed_vm_candidate (mid, q0, q1, v0, v1, T, l, &ignored_err, &tc); | ||
| 197 | *vm_out = mid; | ||
| 198 | *tc_out = tc; | ||
| 199 | return 1; | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | prev_vm = vm; | ||
| 204 | prev_f = f; | ||
| 205 | prev_ok = ok; | ||
| 206 | } | ||
| 207 | |||
| 208 | if (best_abs <= 1e-6*fmax (1.0, tr_abs (q1 - q0))) { | ||
| 209 | *vm_out = best_vm; | ||
| 210 | *tc_out = best_tc; | ||
| 211 | found_exact = 1; | ||
| 212 | } | ||
| 213 | |||
| 214 | return found_exact; | ||
| 215 | } | ||
| 216 | |||
| 217 | static int | ||
| 218 | tr_axis_feasible_at (double q0, | ||
| 219 | double q1, | ||
| 220 | double v0, | ||
| 221 | double v1, | ||
| 222 | double T, | ||
| 223 | const struct tr_axis_limits *l) | ||
| 224 | { | ||
| 225 | double vm, tc; | ||
| 226 | return tr_find_fixed_vm (q0, q1, v0, v1, T, l, &vm, &tc); | ||
| 227 | } | ||
| 228 | |||
| 229 | static int | ||
| 230 | tr_axis_min_duration (double q0, | ||
| 231 | double q1, | ||
| 232 | double v0, | ||
| 233 | double v1, | ||
| 234 | const struct tr_axis_limits *l, | ||
| 235 | double *T_out) | ||
| 236 | { | ||
| 237 | if (tr_abs (v0) > l->vmax + 1e-8 || tr_abs (v1) > l->vmax + 1e-8) | ||
| 238 | return TR_ERR_INFEASIBLE; | ||
| 239 | |||
| 240 | double lo = 0.0; | ||
| 241 | double hi = 1e-9; | ||
| 242 | |||
| 243 | /* Lower bound must at least allow the velocity change through some vm. */ | ||
| 244 | struct tr_change direct = tr_speed_change (v0, v1, l); | ||
| 245 | lo = 0.0; | ||
| 246 | hi = fmax (direct.dt, 1e-6); | ||
| 247 | |||
| 248 | while (!tr_axis_feasible_at (q0, q1, v0, v1, hi, l)) { | ||
| 249 | hi *= 2.0; | ||
| 250 | if (!(hi < 1e9) || !isfinite (hi)) | ||
| 251 | return TR_ERR_NO_PROFILE; | ||
| 252 | } | ||
| 253 | |||
| 254 | for (int k = 0; k < TR_MAX_BISECT; ++k) { | ||
| 255 | double mid = 0.5*(lo + hi); | ||
| 256 | if (tr_axis_feasible_at (q0, q1, v0, v1, mid, l)) | ||
| 257 | hi = mid; | ||
| 258 | else | ||
| 259 | lo = mid; | ||
| 260 | } | ||
| 261 | |||
| 262 | *T_out = hi; | ||
| 263 | return TR_OK; | ||
| 264 | } | ||
| 265 | |||
| 266 | static int | ||
| 267 | tr_axis_push_phase (struct tr_axis_plan *p, | ||
| 268 | double dt, | ||
| 269 | double j, | ||
| 270 | double *q, | ||
| 271 | double *v, | ||
| 272 | double *a, | ||
| 273 | double *t) | ||
| 274 | { | ||
| 275 | if (dt <= 1e-10) | ||
| 276 | return TR_OK; | ||
| 277 | |||
| 278 | if (p->count >= (uint32_t) (sizeof p->phases / sizeof p->phases[0])) | ||
| 279 | return TR_ERR_NUMERIC; | ||
| 280 | |||
| 281 | struct tr_axis_phase *ph = &p->phases[p->count++]; | ||
| 282 | ph->t_begin = *t; | ||
| 283 | ph->dt = dt; | ||
| 284 | ph->q0 = *q; | ||
| 285 | ph->v0 = *v; | ||
| 286 | ph->a0 = *a; | ||
| 287 | ph->j = j; | ||
| 288 | |||
| 289 | double dt2 = dt*dt; | ||
| 290 | double dt3 = dt2*dt; | ||
| 291 | *q = *q + (*v)*dt + 0.5*(*a)*dt2 + (j*dt3)/6.0; | ||
| 292 | *v = *v + (*a)*dt + 0.5*j*dt2; | ||
| 293 | *a = *a + j*dt; | ||
| 294 | *t = *t + dt; | ||
| 295 | return TR_OK; | ||
| 296 | } | ||
| 297 | |||
| 298 | static int | ||
| 299 | tr_axis_append_speed_change (struct tr_axis_plan *p, | ||
| 300 | double v_to, | ||
| 301 | const struct tr_axis_limits *l, | ||
| 302 | double *q, | ||
| 303 | double *v, | ||
| 304 | double *a, | ||
| 305 | double *t) | ||
| 306 | { | ||
| 307 | double dv = v_to - *v; | ||
| 308 | double adv = tr_abs (dv); | ||
| 309 | if (adv <= 1e-10) { | ||
| 310 | *v = v_to; | ||
| 311 | *a = 0.0; | ||
| 312 | return TR_OK; | ||
| 313 | } | ||
| 314 | |||
| 315 | double sgn = dv >= 0.0 ? 1.0 : -1.0; | ||
| 316 | double dv_tri = l->amax*l->amax/l->jmax; | ||
| 317 | |||
| 318 | if (adv <= dv_tri) { | ||
| 319 | double tj = sqrt (adv/l->jmax); | ||
| 320 | int rc = tr_axis_push_phase (p, tj, sgn*l->jmax, q, v, a, t); | ||
| 321 | if (rc != TR_OK) return rc; | ||
| 322 | rc = tr_axis_push_phase (p, tj, -sgn*l->jmax, q, v, a, t); | ||
| 323 | if (rc != TR_OK) return rc; | ||
| 324 | } else { | ||
| 325 | double tj = l->amax/l->jmax; | ||
| 326 | double ta = adv/l->amax - tj; | ||
| 327 | int rc = tr_axis_push_phase (p, tj, sgn*l->jmax, q, v, a, t); | ||
| 328 | if (rc != TR_OK) return rc; | ||
| 329 | rc = tr_axis_push_phase (p, ta, 0.0, q, v, a, t); | ||
| 330 | if (rc != TR_OK) return rc; | ||
| 331 | rc = tr_axis_push_phase (p, tj, -sgn*l->jmax, q, v, a, t); | ||
| 332 | if (rc != TR_OK) return rc; | ||
| 333 | } | ||
| 334 | |||
| 335 | *v = v_to; | ||
| 336 | *a = 0.0; | ||
| 337 | return TR_OK; | ||
| 338 | } | ||
| 339 | |||
| 340 | static int | ||
| 341 | tr_axis_build_fixed (struct tr_axis_plan *out, | ||
| 342 | double q0, | ||
| 343 | double q1, | ||
| 344 | double v0, | ||
| 345 | double v1, | ||
| 346 | double T, | ||
| 347 | const struct tr_axis_limits *l) | ||
| 348 | { | ||
| 349 | if (tr_abs (v0) > l->vmax + 1e-8 || tr_abs (v1) > l->vmax + 1e-8) | ||
| 350 | return TR_ERR_INFEASIBLE; | ||
| 351 | |||
| 352 | double vm = 0.0; | ||
| 353 | double tc = 0.0; | ||
| 354 | if (!tr_find_fixed_vm (q0, q1, v0, v1, T, l, &vm, &tc)) | ||
| 355 | return TR_ERR_NO_PROFILE; | ||
| 356 | |||
| 357 | memset (out, 0, sizeof *out); | ||
| 358 | out->duration = T; | ||
| 359 | out->q0 = q0; | ||
| 360 | out->q1 = q1; | ||
| 361 | out->v0 = v0; | ||
| 362 | out->v1 = v1; | ||
| 363 | |||
| 364 | double q = q0; | ||
| 365 | double v = v0; | ||
| 366 | double a = 0.0; | ||
| 367 | double t = 0.0; | ||
| 368 | |||
| 369 | int rc = tr_axis_append_speed_change (out, vm, l, &q, &v, &a, &t); | ||
| 370 | if (rc != TR_OK) return rc; | ||
| 371 | |||
| 372 | rc = tr_axis_push_phase (out, tc, 0.0, &q, &v, &a, &t); | ||
| 373 | if (rc != TR_OK) return rc; | ||
| 374 | |||
| 375 | rc = tr_axis_append_speed_change (out, v1, l, &q, &v, &a, &t); | ||
| 376 | if (rc != TR_OK) return rc; | ||
| 377 | |||
| 378 | /* Clamp accumulated numerical error at the boundary. */ | ||
| 379 | if (out->count > 0u) { | ||
| 380 | out->duration = T; | ||
| 381 | } | ||
| 382 | |||
| 383 | if (tr_abs (t - T) > 2e-6*fmax (1.0, T)) | ||
| 384 | return TR_ERR_NUMERIC; | ||
| 385 | if (tr_abs (q - q1) > 2e-5*fmax (1.0, tr_abs (q1 - q0))) | ||
| 386 | return TR_ERR_NUMERIC; | ||
| 387 | if (tr_abs (v - v1) > 2e-6*fmax (1.0, tr_abs (v1))) | ||
| 388 | return TR_ERR_NUMERIC; | ||
| 389 | if (tr_abs (a) > 2e-6*fmax (1.0, l->amax)) | ||
| 390 | return TR_ERR_NUMERIC; | ||
| 391 | |||
| 392 | return TR_OK; | ||
| 393 | } | ||
| 394 | |||
| 395 | static void | ||
| 396 | tr_axis_eval (const struct tr_axis_plan *p, | ||
| 397 | double t, | ||
| 398 | double *q_out, | ||
| 399 | double *v_out, | ||
| 400 | double *a_out, | ||
| 401 | double *j_out) | ||
| 402 | { | ||
| 403 | if (!p || p->count == 0u) { | ||
| 404 | if (q_out) *q_out = p ? p->q0 : 0.0; | ||
| 405 | if (v_out) *v_out = p ? p->v0 : 0.0; | ||
| 406 | if (a_out) *a_out = 0.0; | ||
| 407 | if (j_out) *j_out = 0.0; | ||
| 408 | return; | ||
| 409 | } | ||
| 410 | |||
| 411 | if (t <= 0.0) { | ||
| 412 | if (q_out) *q_out = p->q0; | ||
| 413 | if (v_out) *v_out = p->v0; | ||
| 414 | if (a_out) *a_out = 0.0; | ||
| 415 | if (j_out) *j_out = 0.0; | ||
| 416 | return; | ||
| 417 | } | ||
| 418 | |||
| 419 | if (t >= p->duration) { | ||
| 420 | if (q_out) *q_out = p->q1; | ||
| 421 | if (v_out) *v_out = p->v1; | ||
| 422 | if (a_out) *a_out = 0.0; | ||
| 423 | if (j_out) *j_out = 0.0; | ||
| 424 | return; | ||
| 425 | } | ||
| 426 | |||
| 427 | const struct tr_axis_phase *ph = &p->phases[p->count - 1u]; | ||
| 428 | for (uint32_t i = 0; i < p->count; ++i) { | ||
| 429 | const struct tr_axis_phase *cand = &p->phases[i]; | ||
| 430 | if (t < cand->t_begin + cand->dt + 1e-12) { | ||
| 431 | ph = cand; | ||
| 432 | break; | ||
| 433 | } | ||
| 434 | } | ||
| 435 | |||
| 436 | double tau = tr_clamp (t - ph->t_begin, 0.0, ph->dt); | ||
| 437 | double tau2 = tau*tau; | ||
| 438 | double tau3 = tau2*tau; | ||
| 439 | |||
| 440 | if (q_out) *q_out = ph->q0 + ph->v0*tau + 0.5*ph->a0*tau2 + (ph->j*tau3)/6.0; | ||
| 441 | if (v_out) *v_out = ph->v0 + ph->a0*tau + 0.5*ph->j*tau2; | ||
| 442 | if (a_out) *a_out = ph->a0 + ph->j*tau; | ||
| 443 | if (j_out) *j_out = ph->j; | ||
| 444 | } | ||
| 445 | |||
| 446 | |||
| 447 | static double | ||
| 448 | tr_speed_at_time (const struct tr_transition *tr, double t) | ||
| 449 | { | ||
| 450 | double vx = 0.0; | ||
| 451 | double vy = 0.0; | ||
| 452 | |||
| 453 | tr_axis_eval (&tr->x, t, NULL, &vx, NULL, NULL); | ||
| 454 | tr_axis_eval (&tr->y, t, NULL, &vy, NULL, NULL); | ||
| 455 | |||
| 456 | return hypot (vx, vy); | ||
| 457 | } | ||
| 458 | |||
| 459 | static double | ||
| 460 | tr_integrate_speed (const struct tr_transition *tr, double t0, double t1) | ||
| 461 | { | ||
| 462 | if (!tr || !(t1 > t0)) | ||
| 463 | return 0.0; | ||
| 464 | |||
| 465 | /* 16-point Gauss-Legendre rule on [t0, t1]. On every joint phase interval | ||
| 466 | * vx(t) and vy(t) are quadratic polynomials, so the integrand is smooth. | ||
| 467 | */ | ||
| 468 | static const double x[8] = { | ||
| 469 | 0.095012509837637440185, | ||
| 470 | 0.281603550779258913230, | ||
| 471 | 0.458016777657227386342, | ||
| 472 | 0.617876244402643748447, | ||
| 473 | 0.755404408355003033895, | ||
| 474 | 0.865631202387831743880, | ||
| 475 | 0.944575023073232576078, | ||
| 476 | 0.989400934991649932596 | ||
| 477 | }; | ||
| 478 | static const double w[8] = { | ||
| 479 | 0.189450610455068496285, | ||
| 480 | 0.182603415044923588867, | ||
| 481 | 0.169156519395002538189, | ||
| 482 | 0.149595988816576732081, | ||
| 483 | 0.124628971255533872052, | ||
| 484 | 0.095158511682492784810, | ||
| 485 | 0.062253523938647892863, | ||
| 486 | 0.027152459411754094852 | ||
| 487 | }; | ||
| 488 | |||
| 489 | double c = 0.5*(t0 + t1); | ||
| 490 | double h = 0.5*(t1 - t0); | ||
| 491 | double sum = 0.0; | ||
| 492 | |||
| 493 | for (int i = 0; i < 8; ++i) { | ||
| 494 | double dx = h*x[i]; | ||
| 495 | sum += w[i] * (tr_speed_at_time (tr, c - dx) + | ||
| 496 | tr_speed_at_time (tr, c + dx)); | ||
| 497 | } | ||
| 498 | |||
| 499 | return h*sum; | ||
| 500 | } | ||
| 501 | |||
| 502 | static double | ||
| 503 | tr_lookup_time_in_dense_table (const double *ts, | ||
| 504 | const double *ss, | ||
| 505 | uint32_t n, | ||
| 506 | double s) | ||
| 507 | { | ||
| 508 | if (!ts || !ss || n == 0u) | ||
| 509 | return 0.0; | ||
| 510 | |||
| 511 | if (s <= ss[0]) | ||
| 512 | return ts[0]; | ||
| 513 | if (s >= ss[n - 1u]) | ||
| 514 | return ts[n - 1u]; | ||
| 515 | |||
| 516 | uint32_t lo = 0u; | ||
| 517 | uint32_t hi = n - 1u; | ||
| 518 | |||
| 519 | while (lo + 1u < hi) { | ||
| 520 | uint32_t mid = lo + (hi - lo)/2u; | ||
| 521 | if (ss[mid] <= s) | ||
| 522 | lo = mid; | ||
| 523 | else | ||
| 524 | hi = mid; | ||
| 525 | } | ||
| 526 | |||
| 527 | double s0 = ss[lo]; | ||
| 528 | double s1 = ss[hi]; | ||
| 529 | double t0 = ts[lo]; | ||
| 530 | double t1 = ts[hi]; | ||
| 531 | |||
| 532 | if (!(s1 > s0)) | ||
| 533 | return t0; | ||
| 534 | |||
| 535 | double u = (s - s0)/(s1 - s0); | ||
| 536 | return t0 + u*(t1 - t0); | ||
| 537 | } | ||
| 538 | |||
| 539 | static int | ||
| 540 | tr_build_s_table (struct tr_transition *tr) | ||
| 541 | { | ||
| 542 | if (!tr) | ||
| 543 | return TR_ERR_INVALID_ARG; | ||
| 544 | |||
| 545 | tr->length = 0.0; | ||
| 546 | tr->s_node_count = 0u; | ||
| 547 | tr->s_step = 0.0; | ||
| 548 | |||
| 549 | if (!(tr->duration > 0.0) || !isfinite (tr->duration)) { | ||
| 550 | tr->s_node_count = 1u; | ||
| 551 | tr->s_nodes[0].s = 0.0; | ||
| 552 | tr->s_nodes[0].t = 0.0; | ||
| 553 | return TR_OK; | ||
| 554 | } | ||
| 555 | |||
| 556 | /* First build a dense cumulative table in time. This is done once at build | ||
| 557 | * time. Runtime sampling by s does not integrate and does not bisect with | ||
| 558 | * quadrature anymore. | ||
| 559 | */ | ||
| 560 | double ts[TR_S_TABLE_CAP]; | ||
| 561 | double ss[TR_S_TABLE_CAP]; | ||
| 562 | const uint32_t n = TR_S_TABLE_CAP; | ||
| 563 | |||
| 564 | ts[0] = 0.0; | ||
| 565 | ss[0] = 0.0; | ||
| 566 | |||
| 567 | double s_acc = 0.0; | ||
| 568 | double t_prev = 0.0; | ||
| 569 | |||
| 570 | for (uint32_t i = 1u; i < n; ++i) { | ||
| 571 | double t = tr->duration * ((double) i / (double) (n - 1u)); | ||
| 572 | double ds = tr_integrate_speed (tr, t_prev, t); | ||
| 573 | |||
| 574 | if (!isfinite (ds) || ds < -1e-9) | ||
| 575 | return TR_ERR_NUMERIC; | ||
| 576 | |||
| 577 | if (ds < 0.0) | ||
| 578 | ds = 0.0; | ||
| 579 | |||
| 580 | s_acc += ds; | ||
| 581 | ts[i] = t; | ||
| 582 | ss[i] = s_acc; | ||
| 583 | t_prev = t; | ||
| 584 | } | ||
| 585 | |||
| 586 | tr->length = s_acc; | ||
| 587 | |||
| 588 | if (!(tr->length > 0.0) || !isfinite (tr->length)) { | ||
| 589 | tr->s_node_count = 1u; | ||
| 590 | tr->s_step = 0.0; | ||
| 591 | tr->s_nodes[0].s = 0.0; | ||
| 592 | tr->s_nodes[0].t = 0.0; | ||
| 593 | return TR_OK; | ||
| 594 | } | ||
| 595 | |||
| 596 | tr->s_node_count = n; | ||
| 597 | tr->s_step = tr->length / (double) (n - 1u); | ||
| 598 | |||
| 599 | for (uint32_t i = 0u; i < n; ++i) { | ||
| 600 | double s_target = tr->s_step * (double) i; | ||
| 601 | if (i + 1u == n) | ||
| 602 | s_target = tr->length; | ||
| 603 | |||
| 604 | tr->s_nodes[i].s = s_target; | ||
| 605 | tr->s_nodes[i].t = tr_lookup_time_in_dense_table (ts, ss, n, s_target); | ||
| 606 | } | ||
| 607 | |||
| 608 | tr->s_nodes[0].s = 0.0; | ||
| 609 | tr->s_nodes[0].t = 0.0; | ||
| 610 | tr->s_nodes[n - 1u].s = tr->length; | ||
| 611 | tr->s_nodes[n - 1u].t = tr->duration; | ||
| 612 | |||
| 613 | return TR_OK; | ||
| 614 | } | ||
| 615 | |||
| 616 | double | ||
| 617 | tr_length_at_time (const struct tr_transition *tr, double t) | ||
| 618 | { | ||
| 619 | if (!tr || tr->s_node_count == 0u) | ||
| 620 | return 0.0; | ||
| 621 | |||
| 622 | if (tr->length <= 0.0) | ||
| 623 | return 0.0; | ||
| 624 | |||
| 625 | t = tr_clamp (t, 0.0, tr->duration); | ||
| 626 | |||
| 627 | if (t <= 0.0) | ||
| 628 | return 0.0; | ||
| 629 | if (t >= tr->duration) | ||
| 630 | return tr->length; | ||
| 631 | |||
| 632 | uint32_t lo = 0u; | ||
| 633 | uint32_t hi = tr->s_node_count - 1u; | ||
| 634 | |||
| 635 | while (lo + 1u < hi) { | ||
| 636 | uint32_t mid = lo + (hi - lo)/2u; | ||
| 637 | if (tr->s_nodes[mid].t <= t) | ||
| 638 | lo = mid; | ||
| 639 | else | ||
| 640 | hi = mid; | ||
| 641 | } | ||
| 642 | |||
| 643 | const struct tr_s_node *a = &tr->s_nodes[lo]; | ||
| 644 | const struct tr_s_node *b = &tr->s_nodes[hi]; | ||
| 645 | |||
| 646 | if (!(b->t > a->t)) | ||
| 647 | return a->s; | ||
| 648 | |||
| 649 | double u = (t - a->t)/(b->t - a->t); | ||
| 650 | return tr_clamp (a->s + u*(b->s - a->s), 0.0, tr->length); | ||
| 651 | } | ||
| 652 | |||
| 653 | double | ||
| 654 | tr_time_at_length (const struct tr_transition *tr, double s) | ||
| 655 | { | ||
| 656 | if (!tr || tr->s_node_count == 0u) | ||
| 657 | return 0.0; | ||
| 658 | |||
| 659 | if (tr->length <= 0.0) | ||
| 660 | return 0.0; | ||
| 661 | |||
| 662 | s = tr_clamp (s, 0.0, tr->length); | ||
| 663 | |||
| 664 | if (s <= 0.0) | ||
| 665 | return 0.0; | ||
| 666 | if (s >= tr->length) | ||
| 667 | return tr->duration; | ||
| 668 | |||
| 669 | /* The table is uniform by arclength, so the common path is O(1). */ | ||
| 670 | double u = s / tr->s_step; | ||
| 671 | uint32_t i = (uint32_t) floor (u); | ||
| 672 | |||
| 673 | if (i + 1u >= tr->s_node_count) | ||
| 674 | return tr->duration; | ||
| 675 | |||
| 676 | double f = u - (double) i; | ||
| 677 | const struct tr_s_node *a = &tr->s_nodes[i]; | ||
| 678 | const struct tr_s_node *b = &tr->s_nodes[i + 1u]; | ||
| 679 | |||
| 680 | return a->t + f*(b->t - a->t); | ||
| 681 | } | ||
| 682 | |||
| 683 | void | ||
| 684 | tr_sample_at_s (const struct tr_transition *tr, double s, struct tr_sample *out) | ||
| 685 | { | ||
| 686 | if (!out) | ||
| 687 | return; | ||
| 688 | |||
| 689 | memset (out, 0, sizeof *out); | ||
| 690 | if (!tr) | ||
| 691 | return; | ||
| 692 | |||
| 693 | double s_clamped = tr_clamp (s, 0.0, tr->length); | ||
| 694 | double t = tr_time_at_length (tr, s_clamped); | ||
| 695 | tr_sample (tr, t, out); | ||
| 696 | out->s = s_clamped; | ||
| 697 | } | ||
| 698 | |||
| 699 | int | ||
| 700 | tr_build (struct tr_transition *out, | ||
| 701 | struct vec2 r0, | ||
| 702 | struct vec2 r1, | ||
| 703 | struct vec2 v0, | ||
| 704 | struct vec2 v1, | ||
| 705 | double vmax, | ||
| 706 | double amax, | ||
| 707 | double jmax) | ||
| 708 | { | ||
| 709 | if (!out) | ||
| 710 | return TR_ERR_INVALID_ARG; | ||
| 711 | if (!tr_limits_valid (vmax, amax, jmax)) | ||
| 712 | return TR_ERR_LIMITS; | ||
| 713 | |||
| 714 | memset (out, 0, sizeof *out); | ||
| 715 | out->vmax = vmax; | ||
| 716 | out->amax = amax; | ||
| 717 | out->jmax = jmax; | ||
| 718 | |||
| 719 | struct tr_axis_limits lim = { vmax, amax, jmax }; | ||
| 720 | |||
| 721 | double tx = 0.0; | ||
| 722 | double ty = 0.0; | ||
| 723 | int rc = tr_axis_min_duration (r0.x, r1.x, v0.x, v1.x, &lim, &tx); | ||
| 724 | if (rc != TR_OK) return rc; | ||
| 725 | rc = tr_axis_min_duration (r0.y, r1.y, v0.y, v1.y, &lim, &ty); | ||
| 726 | if (rc != TR_OK) return rc; | ||
| 727 | |||
| 728 | double T = fmax (tx, ty); | ||
| 729 | |||
| 730 | /* Add a tiny numerical margin so the shorter axis can be reconstructed at T. */ | ||
| 731 | T += 1e-8*fmax (1.0, T); | ||
| 732 | |||
| 733 | rc = tr_axis_build_fixed (&out->x, r0.x, r1.x, v0.x, v1.x, T, &lim); | ||
| 734 | if (rc != TR_OK) return rc; | ||
| 735 | rc = tr_axis_build_fixed (&out->y, r0.y, r1.y, v0.y, v1.y, T, &lim); | ||
| 736 | if (rc != TR_OK) return rc; | ||
| 737 | |||
| 738 | out->duration = T; | ||
| 739 | |||
| 740 | rc = tr_build_s_table (out); | ||
| 741 | if (rc != TR_OK) | ||
| 742 | return rc; | ||
| 743 | |||
| 744 | return TR_OK; | ||
| 745 | } | ||
| 746 | |||
| 747 | void | ||
| 748 | tr_sample (const struct tr_transition *tr, double t, struct tr_sample *out) | ||
| 749 | { | ||
| 750 | if (!out) | ||
| 751 | return; | ||
| 752 | |||
| 753 | memset (out, 0, sizeof *out); | ||
| 754 | if (!tr) | ||
| 755 | return; | ||
| 756 | |||
| 757 | t = tr_clamp (t, 0.0, tr->duration); | ||
| 758 | out->t = t; | ||
| 759 | out->s = tr_length_at_time (tr, t); | ||
| 760 | |||
| 761 | tr_axis_eval (&tr->x, t, &out->r.x, &out->v.x, &out->a.x, &out->j.x); | ||
| 762 | tr_axis_eval (&tr->y, t, &out->r.y, &out->v.y, &out->a.y, &out->j.y); | ||
| 763 | } | ||
| 764 | |||
| 765 | const char * | ||
| 766 | tr_status_string (int status) | ||
| 767 | { | ||
| 768 | switch (status) { | ||
| 769 | case TR_OK: return "ok"; | ||
| 770 | case TR_ERR_INVALID_ARG: return "invalid argument"; | ||
| 771 | case TR_ERR_LIMITS: return "invalid limits"; | ||
| 772 | case TR_ERR_INFEASIBLE: return "infeasible boundary state"; | ||
| 773 | case TR_ERR_NUMERIC: return "numeric failure"; | ||
| 774 | case TR_ERR_NO_PROFILE: return "no transition profile found"; | ||
| 775 | default: return "unknown transition status"; | ||
| 776 | } | ||
| 777 | } | ||
