diff options
| author | 2026-11-11 22:28:13 +0000 | |
|---|---|---|
| committer | 2026-11-11 22:47:34 +0000 | |
| commit | cb070b2d3f2b6e241e3be1b7e0eeb90fc755255f (patch) | |
| tree | c7d90c0abe9533c87d51b5196871846855efa1ba /src/compensations.c | |
| download | thesis-cb070b2d3f2b6e241e3be1b7e0eeb90fc755255f.tar.gz | |
Diffstat (limited to 'src/compensations.c')
| -rw-r--r-- | src/compensations.c | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/src/compensations.c b/src/compensations.c new file mode 100644 index 0000000..5d87c22 --- /dev/null +++ b/src/compensations.c | |||
| @@ -0,0 +1,181 @@ | |||
| 1 | #include "compensations.h" | ||
| 2 | #include <math.h> | ||
| 3 | #include "da.h" | ||
| 4 | #include "transition.h" | ||
| 5 | |||
| 6 | static double | ||
| 7 | distance_to_change_speed (double v0, double v1, double vmax, double amax, double jmax) | ||
| 8 | { | ||
| 9 | double dv = fabs (v1 - v0); | ||
| 10 | if (dv <= 1e-12) | ||
| 11 | return 0; | ||
| 12 | |||
| 13 | double dv_crit = amax*amax/jmax; | ||
| 14 | double t = dv <= dv_crit ? 2*sqrt (dv/jmax) : dv/amax + amax/jmax; | ||
| 15 | |||
| 16 | return t*(v0 + v1)/2; | ||
| 17 | } | ||
| 18 | |||
| 19 | double vmax = 100/1.41421356237; | ||
| 20 | double amax = 800/1.41421356237; | ||
| 21 | double jmax = 16000/1.41421356237; | ||
| 22 | |||
| 23 | static struct compensation | ||
| 24 | synthesize_compensation_loop (const struct critical_point *cp) | ||
| 25 | { | ||
| 26 | struct compensation comp = { | ||
| 27 | .initial_position = cp->r, | ||
| 28 | .from = cp->left, | ||
| 29 | .to = cp->left, | ||
| 30 | .replace = CR_FROM_END_TO_END, | ||
| 31 | }; | ||
| 32 | |||
| 33 | // struct tr_transition tr; | ||
| 34 | // tr_build (&tr, cp->r, cp->r, cp->v_left, cp->v_right, vmax, amax, jmax); | ||
| 35 | // for (double s = 0.1; s < tr.length; s += 0.1) { | ||
| 36 | // struct tr_sample sample; | ||
| 37 | // tr_sample_at_s (&tr, s, &sample); | ||
| 38 | // DA_APPEND (comp.polyline, sample.r); | ||
| 39 | // } | ||
| 40 | |||
| 41 | double s0 = distance_to_change_speed (cp->left->f, 0, vmax, amax, jmax); | ||
| 42 | double s1 = distance_to_change_speed (0, cp->right->f, vmax, amax, jmax); | ||
| 43 | |||
| 44 | struct vec2 out = vec2_add (cp->r, vec2_scale (cp->t_left , s0)); | ||
| 45 | struct vec2 in = vec2_sub (cp->r, vec2_scale (cp->t_right, s1)); | ||
| 46 | |||
| 47 | DA_APPEND (comp.polyline, out); | ||
| 48 | DA_APPEND (comp.polyline, in ); | ||
| 49 | DA_APPEND (comp.polyline, cp->r); | ||
| 50 | |||
| 51 | return comp; | ||
| 52 | } | ||
| 53 | |||
| 54 | static struct compensation | ||
| 55 | synthesize_compensation_transfer (const struct critical_point *cp_out, | ||
| 56 | const struct critical_point *cp_in) | ||
| 57 | { | ||
| 58 | struct compensation comp = { | ||
| 59 | .initial_position = cp_out->r, | ||
| 60 | .from = cp_out->right, | ||
| 61 | .to = cp_in ->left, | ||
| 62 | .replace = CR_FROM_BEG_TO_END, | ||
| 63 | }; | ||
| 64 | |||
| 65 | // struct tr_transition tr; | ||
| 66 | // tr_build (&tr, cp_out->r, cp_in->r, cp_out->v_left, cp_in->v_right, vmax, amax, jmax); | ||
| 67 | // for (double s = 0.1; s < tr.length; s += 0.1) { | ||
| 68 | // struct tr_sample sample; | ||
| 69 | // tr_sample_at_s (&tr, s, &sample); | ||
| 70 | // DA_APPEND (comp.polyline, sample.r); | ||
| 71 | // } | ||
| 72 | |||
| 73 | double s0 = distance_to_change_speed (cp_out->left->f, 0, vmax, amax, jmax); | ||
| 74 | double s1 = distance_to_change_speed (0, cp_in->right->f, vmax, amax, jmax); | ||
| 75 | |||
| 76 | struct vec2 out = vec2_add (cp_out->r, vec2_scale (cp_out->t_left , s0)); | ||
| 77 | struct vec2 in = vec2_sub (cp_in ->r, vec2_scale (cp_in ->t_right, s1)); | ||
| 78 | |||
| 79 | DA_APPEND (comp.polyline, out); | ||
| 80 | DA_APPEND (comp.polyline, in ); | ||
| 81 | DA_APPEND (comp.polyline, cp_in->r); | ||
| 82 | |||
| 83 | return comp; | ||
| 84 | } | ||
| 85 | |||
| 86 | static struct compensation | ||
| 87 | synthesize_compensation_in (const struct critical_point *cp) | ||
| 88 | { | ||
| 89 | struct compensation comp = { | ||
| 90 | .initial_position = cp->s, | ||
| 91 | .from = cp->left, | ||
| 92 | .to = cp->left, | ||
| 93 | .replace = CR_FROM_BEG_TO_END, | ||
| 94 | }; | ||
| 95 | |||
| 96 | double s = distance_to_change_speed (0, cp->right->f, vmax, amax, jmax); | ||
| 97 | struct vec2 in = vec2_sub (cp->r, vec2_scale (cp->t_right, s)); | ||
| 98 | |||
| 99 | DA_APPEND (comp.polyline, in); | ||
| 100 | DA_APPEND (comp.polyline, cp->r); | ||
| 101 | |||
| 102 | return comp; | ||
| 103 | } | ||
| 104 | |||
| 105 | static struct compensation | ||
| 106 | synthesize_compensation_beg (const struct critical_point *cp) | ||
| 107 | { | ||
| 108 | struct compensation comp = { | ||
| 109 | .initial_position = cp->r, | ||
| 110 | .from = cp->right, | ||
| 111 | .to = cp->right, | ||
| 112 | .replace = CR_FROM_BEG_TO_BEG, | ||
| 113 | }; | ||
| 114 | |||
| 115 | double s = distance_to_change_speed (0, cp->right->f, vmax, amax, jmax); | ||
| 116 | struct vec2 in = vec2_sub (cp->r, vec2_scale (cp->t_right, s)); | ||
| 117 | |||
| 118 | DA_APPEND (comp.polyline, in); | ||
| 119 | DA_APPEND (comp.polyline, cp->r); | ||
| 120 | |||
| 121 | return comp; | ||
| 122 | } | ||
| 123 | |||
| 124 | static struct compensation | ||
| 125 | synthesize_compensation_end (const struct critical_point *cp) | ||
| 126 | { | ||
| 127 | struct compensation comp = { | ||
| 128 | .initial_position = cp->r, | ||
| 129 | .from = cp->left, | ||
| 130 | .to = cp->left, | ||
| 131 | .replace = CR_FROM_END_TO_END, | ||
| 132 | }; | ||
| 133 | |||
| 134 | double s = distance_to_change_speed (cp->left->f, 0, vmax, amax, jmax); | ||
| 135 | struct vec2 out = vec2_add (cp->r, vec2_scale (cp->t_left, s)); | ||
| 136 | |||
| 137 | DA_APPEND (comp.polyline, out); | ||
| 138 | |||
| 139 | return comp; | ||
| 140 | } | ||
| 141 | |||
| 142 | struct compensations | ||
| 143 | synthesize_compensations (const struct critical_points *cps) | ||
| 144 | { | ||
| 145 | struct compensations comps = {0}; | ||
| 146 | |||
| 147 | for (uint32_t i = 0; i < cps->count; ++i) { | ||
| 148 | struct critical_point *cp = &cps->items[i]; | ||
| 149 | struct compensation comp = {0}; | ||
| 150 | switch (cp->type) { | ||
| 151 | case CPT_IN: { | ||
| 152 | comp = synthesize_compensation_in (cp); | ||
| 153 | } break; | ||
| 154 | case CPT_OUT: { // TRANSFER | ||
| 155 | i += 1; | ||
| 156 | if (i == cps->count) { | ||
| 157 | comp = synthesize_compensation_end (cp); | ||
| 158 | } else { | ||
| 159 | struct critical_point *out = cp; | ||
| 160 | struct critical_point *in = &cps->items[i]; | ||
| 161 | ASSERT (in->type == CPT_IN); | ||
| 162 | comp = synthesize_compensation_transfer (out, in); | ||
| 163 | } | ||
| 164 | } break; | ||
| 165 | case CPT_LOOP: { | ||
| 166 | comp = synthesize_compensation_loop (cp); | ||
| 167 | } break; | ||
| 168 | case CPT_BEG: { | ||
| 169 | comp = synthesize_compensation_beg (cp); | ||
| 170 | } break; | ||
| 171 | case CPT_END: { | ||
| 172 | comp = synthesize_compensation_end (cp); | ||
| 173 | } break; | ||
| 174 | default: ASSERT (!"unexpected critical point type"); | ||
| 175 | } | ||
| 176 | |||
| 177 | DA_APPEND (comps, comp); | ||
| 178 | } | ||
| 179 | |||
| 180 | return comps; | ||
| 181 | } | ||
