2009-08-07 16:37:51 +03:00
|
|
|
/*
|
|
|
|
* meas.c - Measurements
|
|
|
|
*
|
|
|
|
* Written 2009 by Werner Almesberger
|
|
|
|
* Copyright 2009 by Werner Almesberger
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "coord.h"
|
|
|
|
#include "expr.h"
|
|
|
|
#include "obj.h"
|
|
|
|
#include "inst.h"
|
|
|
|
#include "meas.h"
|
|
|
|
|
|
|
|
|
2009-08-17 23:42:51 +03:00
|
|
|
int n_samples;
|
|
|
|
|
|
|
|
|
2009-08-07 16:37:51 +03:00
|
|
|
struct num eval_unit(const struct expr *expr, const struct frame *frame);
|
|
|
|
|
|
|
|
|
2009-08-18 23:52:09 +03:00
|
|
|
void reset_samples(struct sample **samples, int n)
|
2009-08-07 16:37:51 +03:00
|
|
|
{
|
|
|
|
struct sample *next;
|
2009-08-17 23:42:51 +03:00
|
|
|
int i;
|
2009-08-07 16:37:51 +03:00
|
|
|
|
2009-08-18 23:52:09 +03:00
|
|
|
for (i = 0; i != n; i++)
|
2009-08-17 23:42:51 +03:00
|
|
|
while (samples[i]) {
|
|
|
|
next = samples[i]->next;
|
|
|
|
free(samples[i]);
|
|
|
|
samples[i] = next;
|
|
|
|
}
|
2009-08-07 16:37:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void meas_start(void)
|
|
|
|
{
|
2009-08-17 23:42:51 +03:00
|
|
|
const struct frame *frame;
|
2009-08-07 16:37:51 +03:00
|
|
|
struct vec *vec;
|
|
|
|
|
2009-08-17 23:42:51 +03:00
|
|
|
n_samples = 0;
|
2009-08-07 16:37:51 +03:00
|
|
|
for (frame = frames; frame; frame = frame->next)
|
|
|
|
for (vec = frame->vecs; vec; vec = vec->next)
|
2009-08-17 23:42:51 +03:00
|
|
|
vec->n = n_samples++;
|
2009-08-07 16:37:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-17 23:42:51 +03:00
|
|
|
void meas_post(const struct vec *vec, struct coord pos)
|
2009-08-07 16:37:51 +03:00
|
|
|
{
|
|
|
|
struct sample **walk, *new;
|
|
|
|
|
2009-08-17 23:42:51 +03:00
|
|
|
for (walk = &curr_pkg->samples[vec->n]; *walk; walk = &(*walk)->next) {
|
2009-08-07 16:37:51 +03:00
|
|
|
if (pos.y < (*walk)->pos.y)
|
|
|
|
break;
|
|
|
|
if (pos.y > (*walk)->pos.y)
|
|
|
|
continue;
|
|
|
|
if (pos.x < (*walk)->pos.x)
|
|
|
|
break;
|
|
|
|
if (pos.x == (*walk)->pos.x)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
new = alloc_type(struct sample);
|
|
|
|
new->pos = pos;
|
|
|
|
new->next = *walk;
|
|
|
|
*walk = new;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-08 21:50:17 +03:00
|
|
|
/* ----- lt operators ------------------------------------------------------ */
|
|
|
|
|
|
|
|
|
2009-08-08 00:47:51 +03:00
|
|
|
int lt_x(struct coord a, struct coord b)
|
2009-08-07 16:37:51 +03:00
|
|
|
{
|
|
|
|
return a.x < b.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-08 00:47:51 +03:00
|
|
|
int lt_y(struct coord a, struct coord b)
|
2009-08-07 16:37:51 +03:00
|
|
|
{
|
|
|
|
return a.y < b.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-08 00:47:51 +03:00
|
|
|
int lt_xy(struct coord a, struct coord b)
|
2009-08-07 16:37:51 +03:00
|
|
|
{
|
|
|
|
return a.y < b.y || (a.y == b.y && a.x < b.x);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-08 21:50:17 +03:00
|
|
|
/* ----- measurement type map ---------------------------------------------- */
|
|
|
|
|
|
|
|
|
2009-08-08 00:47:51 +03:00
|
|
|
static lt_op_type lt_op[mt_n] = {
|
2009-08-07 16:37:51 +03:00
|
|
|
lt_xy,
|
|
|
|
lt_x,
|
|
|
|
lt_y,
|
|
|
|
lt_xy,
|
|
|
|
lt_x,
|
|
|
|
lt_y
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static int is_next[mt_n] = {
|
|
|
|
1, 1, 1,
|
|
|
|
0, 0, 0
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-08-08 21:50:17 +03:00
|
|
|
/* ----- search functions -------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2009-08-09 04:51:20 +03:00
|
|
|
static int closer(int da, int db)
|
|
|
|
{
|
|
|
|
int abs_a, abs_b;
|
|
|
|
|
|
|
|
abs_a = da < 0 ? -da : da;
|
|
|
|
abs_b = db < 0 ? -db : db;
|
|
|
|
if (abs_a < abs_b)
|
|
|
|
return 1;
|
|
|
|
if (abs_a > abs_b)
|
|
|
|
return 0;
|
|
|
|
/*
|
|
|
|
* Really *all* other things being equal, pick the one that protrudes
|
|
|
|
* in the positive direction.
|
|
|
|
*/
|
|
|
|
return da > db;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-08 00:47:51 +03:00
|
|
|
static int better_next(lt_op_type lt,
|
2009-08-09 04:51:20 +03:00
|
|
|
struct coord a0, struct coord b0, struct coord b)
|
2009-08-07 16:37:51 +03:00
|
|
|
{
|
|
|
|
/* if we don't have any suitable point A0 < B0 yet, use this one */
|
|
|
|
if (!lt(a0, b0))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* B must be strictly greater than A0 */
|
|
|
|
if (!lt(a0, b))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* if we can get closer to A0, do so */
|
|
|
|
if (lt(b, b0))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* reject B > B0 */
|
|
|
|
if (lt(b0, b))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* B == B0 along the coordinate we measure. Now give the other
|
|
|
|
* coordinate a chance. This gives us a stable sort order and it
|
|
|
|
* makes meas/measx/measy usually select the same point.
|
|
|
|
*/
|
2009-08-09 04:51:20 +03:00
|
|
|
if (lt == lt_xy)
|
2009-08-07 16:37:51 +03:00
|
|
|
return 0;
|
|
|
|
if (lt == lt_x)
|
2009-08-09 04:51:20 +03:00
|
|
|
return closer(b.y-a0.y, b0.y-a0.y);
|
2009-08-07 16:37:51 +03:00
|
|
|
if (lt == lt_y)
|
2009-08-09 04:51:20 +03:00
|
|
|
return closer(b.x-a0.x, b0.x-a0.x);
|
2009-08-07 16:37:51 +03:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-08 00:47:51 +03:00
|
|
|
/*
|
|
|
|
* In order to obtain a stable order, we sort points equal on the measured
|
|
|
|
* coordinate also by xy:
|
|
|
|
*
|
|
|
|
* if (*a < a0) use *a
|
|
|
|
* else if (*a == a0 && *a <xy a0) use *a
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct coord meas_find_min(lt_op_type lt, const struct sample *s)
|
|
|
|
{
|
|
|
|
struct coord min;
|
|
|
|
|
|
|
|
min = s->pos;
|
|
|
|
while (s) {
|
|
|
|
if (lt(s->pos, min) ||
|
|
|
|
(!lt(min, s->pos) && lt_xy(s->pos, min)))
|
|
|
|
min = s->pos;
|
|
|
|
s = s->next;
|
|
|
|
}
|
|
|
|
return min;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct coord meas_find_next(lt_op_type lt, const struct sample *s,
|
|
|
|
struct coord ref)
|
|
|
|
{
|
|
|
|
struct coord next;
|
|
|
|
|
|
|
|
next = s->pos;
|
|
|
|
while (s) {
|
2009-08-09 04:51:20 +03:00
|
|
|
if (better_next(lt, ref, next, s->pos))
|
2009-08-08 00:47:51 +03:00
|
|
|
next = s->pos;
|
|
|
|
s = s->next;
|
|
|
|
}
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct coord meas_find_max(lt_op_type lt, const struct sample *s)
|
|
|
|
{
|
|
|
|
struct coord max;
|
|
|
|
|
|
|
|
max = s->pos;
|
|
|
|
while (s) {
|
|
|
|
if (lt(max, s->pos) ||
|
|
|
|
(!lt(s->pos, max) && lt_xy(max, s->pos)))
|
|
|
|
max = s->pos;
|
|
|
|
s = s->next;
|
|
|
|
}
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-08 21:50:17 +03:00
|
|
|
/* ----- instantiation ----------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2009-08-17 23:42:51 +03:00
|
|
|
static int instantiate_meas_pkg(void)
|
2009-08-07 16:37:51 +03:00
|
|
|
{
|
2009-08-09 03:06:54 +03:00
|
|
|
struct obj *obj;
|
|
|
|
const struct meas *meas;
|
2009-08-07 16:37:51 +03:00
|
|
|
struct coord a0, b0;
|
2009-08-08 00:47:51 +03:00
|
|
|
lt_op_type lt;
|
2009-08-07 16:37:51 +03:00
|
|
|
struct num offset;
|
|
|
|
|
2009-08-09 03:06:54 +03:00
|
|
|
for (obj = root_frame->objs; obj; obj = obj->next) {
|
|
|
|
if (obj->type != ot_meas)
|
2009-08-07 16:37:51 +03:00
|
|
|
continue;
|
2009-08-09 03:06:54 +03:00
|
|
|
meas = &obj->u.meas;
|
2009-08-17 23:42:51 +03:00
|
|
|
if (!curr_pkg->samples[obj->base->n] ||
|
|
|
|
!curr_pkg->samples[meas->high->n])
|
2009-08-13 12:30:16 +03:00
|
|
|
continue;
|
2009-08-07 16:37:51 +03:00
|
|
|
|
|
|
|
lt = lt_op[meas->type];
|
2009-08-17 23:42:51 +03:00
|
|
|
a0 = meas_find_min(lt, curr_pkg->samples[obj->base->n]);
|
2009-08-08 00:47:51 +03:00
|
|
|
if (is_next[meas->type])
|
2009-08-17 23:42:51 +03:00
|
|
|
b0 = meas_find_next(lt,
|
|
|
|
curr_pkg->samples[meas->high->n], a0);
|
2009-08-08 00:47:51 +03:00
|
|
|
else
|
2009-08-17 23:42:51 +03:00
|
|
|
b0 = meas_find_max(lt,
|
|
|
|
curr_pkg->samples[meas->high->n]);
|
2009-08-07 16:37:51 +03:00
|
|
|
|
2009-08-08 21:50:17 +03:00
|
|
|
if (!meas->offset)
|
|
|
|
offset.n = 0;
|
|
|
|
else {
|
|
|
|
offset = eval_unit(meas->offset, root_frame);
|
2009-08-13 12:30:16 +03:00
|
|
|
if (is_undef(offset)) {
|
|
|
|
instantiation_error = obj;
|
2009-08-08 21:50:17 +03:00
|
|
|
return 0;
|
2009-08-13 12:30:16 +03:00
|
|
|
}
|
2009-08-08 21:50:17 +03:00
|
|
|
}
|
2009-08-09 03:06:54 +03:00
|
|
|
inst_meas(obj,
|
2009-08-07 16:37:51 +03:00
|
|
|
meas->inverted ? b0 : a0, meas->inverted ? a0 : b0,
|
|
|
|
offset.n);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2009-08-17 23:42:51 +03:00
|
|
|
|
|
|
|
|
|
|
|
int instantiate_meas(void)
|
|
|
|
{
|
|
|
|
struct pkg *pkg;
|
|
|
|
|
|
|
|
curr_frame = pkgs->insts[ip_frame];
|
|
|
|
for (pkg = pkgs; pkg; pkg = pkg->next)
|
|
|
|
if (pkg->name) {
|
|
|
|
inst_select_pkg(pkg->name);
|
|
|
|
if (!instantiate_meas_pkg())
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|