meas.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * meas.c - Measurements
  3. *
  4. * Written 2009, 2010, 2012 by Werner Almesberger
  5. * Copyright 2009, 2010, 2012 by Werner Almesberger
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <stdlib.h>
  13. #include "util.h"
  14. #include "coord.h"
  15. #include "expr.h"
  16. #include "obj.h"
  17. #include "inst.h"
  18. #include "meas.h"
  19. int n_samples;
  20. struct num eval_unit(const struct expr *expr, const struct frame *frame);
  21. void reset_samples(struct sample **samples, int n)
  22. {
  23. struct sample *next;
  24. int i;
  25. for (i = 0; i != n; i++)
  26. while (samples[i]) {
  27. next = samples[i]->next;
  28. bitset_free(samples[i]->frame_set);
  29. free(samples[i]);
  30. samples[i] = next;
  31. }
  32. }
  33. void meas_start(void)
  34. {
  35. const struct frame *frame;
  36. struct vec *vec;
  37. n_samples = 0;
  38. for (frame = frames; frame; frame = frame->next)
  39. for (vec = frame->vecs; vec; vec = vec->next)
  40. vec->n = n_samples++;
  41. }
  42. void meas_post(const struct vec *vec, struct coord pos,
  43. const struct bitset *frame_set)
  44. {
  45. struct sample **walk, *new;
  46. for (walk = &curr_pkg->samples[vec->n]; *walk; walk = &(*walk)->next) {
  47. if (pos.y < (*walk)->pos.y)
  48. break;
  49. if (pos.y > (*walk)->pos.y)
  50. continue;
  51. if (pos.x < (*walk)->pos.x)
  52. break;
  53. if (pos.x != (*walk)->pos.x)
  54. continue;
  55. if (bitset_ge((*walk)->frame_set, frame_set))
  56. return;
  57. if (bitset_ge(frame_set, (*walk)->frame_set)) {
  58. bitset_or((*walk)->frame_set, frame_set);
  59. return;
  60. }
  61. }
  62. new = alloc_type(struct sample);
  63. new->pos = pos;
  64. new->frame_set = bitset_clone(frame_set);
  65. new->next = *walk;
  66. *walk = new;
  67. }
  68. /* ----- lt operators ------------------------------------------------------ */
  69. int lt_x(struct coord a, struct coord b)
  70. {
  71. return a.x < b.x;
  72. }
  73. int lt_y(struct coord a, struct coord b)
  74. {
  75. return a.y < b.y;
  76. }
  77. int lt_xy(struct coord a, struct coord b)
  78. {
  79. return a.y < b.y || (a.y == b.y && a.x < b.x);
  80. }
  81. /* ----- measurement type map ---------------------------------------------- */
  82. static lt_op_type lt_op[mt_n] = {
  83. lt_xy,
  84. lt_x,
  85. lt_y,
  86. lt_xy,
  87. lt_x,
  88. lt_y
  89. };
  90. static int is_next[mt_n] = {
  91. 1, 1, 1,
  92. 0, 0, 0
  93. };
  94. /* ----- search functions -------------------------------------------------- */
  95. static int closer(int da, int db)
  96. {
  97. int abs_a, abs_b;
  98. abs_a = da < 0 ? -da : da;
  99. abs_b = db < 0 ? -db : db;
  100. if (abs_a < abs_b)
  101. return 1;
  102. if (abs_a > abs_b)
  103. return 0;
  104. /*
  105. * Really *all* other things being equal, pick the one that protrudes
  106. * in the positive direction.
  107. */
  108. return da > db;
  109. }
  110. static int better_next(lt_op_type lt,
  111. struct coord a0, struct coord b0, struct coord b)
  112. {
  113. /* if we don't have any suitable point A0 < B0 yet, use this one */
  114. if (!lt(a0, b0))
  115. return 1;
  116. /* B must be strictly greater than A0 */
  117. if (!lt(a0, b))
  118. return 0;
  119. /* if we can get closer to A0, do so */
  120. if (lt(b, b0))
  121. return 1;
  122. /* reject B > B0 */
  123. if (lt(b0, b))
  124. return 0;
  125. /*
  126. * B == B0 along the coordinate we measure. Now give the other
  127. * coordinate a chance. This gives us a stable sort order and it
  128. * makes meas/measx/measy usually select the same point.
  129. */
  130. if (lt == lt_xy)
  131. return 0;
  132. if (lt == lt_x)
  133. return closer(b.y-a0.y, b0.y-a0.y);
  134. if (lt == lt_y)
  135. return closer(b.x-a0.x, b0.x-a0.x);
  136. abort();
  137. }
  138. /*
  139. * In order to obtain a stable order, we sort points equal on the measured
  140. * coordinate also by xy:
  141. *
  142. * if (*a < a0) use *a
  143. * else if (*a == a0 && *a <xy a0) use *a
  144. */
  145. const struct sample *meas_find_min(lt_op_type lt, const struct sample *s,
  146. const struct bitset *qual)
  147. {
  148. const struct sample *min = NULL;
  149. while (s) {
  150. if (!qual || bitset_ge(s->frame_set, qual))
  151. if (!min || lt(s->pos, min->pos) ||
  152. (!lt(min->pos, s->pos) && lt_xy(s->pos, min->pos)))
  153. min = s;
  154. s = s->next;
  155. }
  156. return min;
  157. }
  158. const struct sample *meas_find_next(lt_op_type lt, const struct sample *s,
  159. struct coord ref, const struct bitset *qual)
  160. {
  161. const struct sample *next = NULL;
  162. while (s) {
  163. if (!qual || bitset_ge(s->frame_set, qual))
  164. if (!next || better_next(lt, ref, next->pos, s->pos))
  165. next = s;
  166. s = s->next;
  167. }
  168. return next;
  169. }
  170. const struct sample *meas_find_max(lt_op_type lt, const struct sample *s,
  171. const struct bitset *qual)
  172. {
  173. const struct sample *max = NULL;
  174. while (s) {
  175. if (!qual || bitset_ge(s->frame_set, qual))
  176. if (!max || lt(max->pos, s->pos) ||
  177. (!lt(s->pos, max->pos) && lt_xy(max->pos, s->pos)))
  178. max = s;
  179. s = s->next;
  180. }
  181. return max;
  182. }
  183. /* ----- instantiation ----------------------------------------------------- */
  184. static struct bitset *make_frame_set(struct frame_qual *qual, int n_frames)
  185. {
  186. struct bitset *set;
  187. set = bitset_new(n_frames);
  188. while (qual) {
  189. bitset_set(set, qual->frame->n);
  190. qual = qual->next;
  191. }
  192. return set;
  193. }
  194. static int instantiate_meas_pkg(int n_frames)
  195. {
  196. struct obj *obj;
  197. const struct meas *meas;
  198. struct bitset *set;
  199. const struct sample *a0, *b0;
  200. lt_op_type lt;
  201. for (obj = frames->objs; obj; obj = obj->next) {
  202. if (obj->type != ot_meas)
  203. continue;
  204. meas = &obj->u.meas;
  205. /* optimization. not really needed anymore. */
  206. if (!curr_pkg->samples[obj->base->n] ||
  207. !curr_pkg->samples[meas->high->n])
  208. continue;
  209. lt = lt_op[meas->type];
  210. set = make_frame_set(meas->low_qual, n_frames);
  211. a0 = meas_find_min(lt, curr_pkg->samples[obj->base->n], set);
  212. bitset_free(set);
  213. if (!a0)
  214. continue;
  215. set = make_frame_set(meas->high_qual, n_frames);
  216. if (is_next[meas->type])
  217. b0 = meas_find_next(lt,
  218. curr_pkg->samples[meas->high->n], a0->pos, set);
  219. else
  220. b0 = meas_find_max(lt,
  221. curr_pkg->samples[meas->high->n], set);
  222. bitset_free(set);
  223. if (!b0)
  224. continue;
  225. inst_meas(obj,
  226. meas->inverted ? b0->pos : a0->pos,
  227. meas->inverted ? a0->pos : b0->pos);
  228. }
  229. return 1;
  230. }
  231. static void purge_meas(struct pkg *pkg)
  232. {
  233. struct inst **anchor, *inst;
  234. anchor = pkg->insts+ip_meas;
  235. while (*anchor)
  236. if ((*anchor)->u.meas.valid) {
  237. anchor = &(*anchor)->next;
  238. } else {
  239. inst = *anchor;
  240. *anchor = inst->next;
  241. free(inst);
  242. }
  243. }
  244. int instantiate_meas(int n_frames)
  245. {
  246. struct pkg *pkg;
  247. frame_instantiating = pkgs->insts[ip_frame];
  248. for (pkg = pkgs; pkg; pkg = pkg->next)
  249. if (pkg->name) {
  250. inst_select_pkg(pkg->name, 0);
  251. if (!instantiate_meas_pkg(n_frames))
  252. return 0;
  253. purge_meas(pkg);
  254. }
  255. return 1;
  256. }