cursor positions always saved in true x values (no log taken for logx axes) avoid too many log10() / pow(10, ...) conversions

This commit is contained in:
stefan schippers 2024-04-01 00:25:50 +02:00
parent 8eecd73c50
commit 318c52cd0f
4 changed files with 49 additions and 83 deletions

View File

@ -246,7 +246,7 @@ void backannotate_at_cursor_b_pos(xRect *r, Graph_ctx *gr)
end = (gr->gx1 <= gr->gx2) ? gr->gx2 : gr->gx1;
dbg(1, "start=%g, end=%g\n", start, end);
if(gr->logx) {
cursor2 = pow(10, cursor2);
/* cursor2 = pow(10, cursor2); */
start = pow(10, start);
end = pow(10, end);
}
@ -380,16 +380,14 @@ static int waves_callback(int event, int mx, int my, KeySym key, int button, int
/* set cursor position from master graph x-axis */
if(event == MotionNotify && (state & Button1Mask) && (xctx->graph_flags & 16 )) {
xctx->graph_cursor1_x = G_X(xctx->mousex);
xctx->graph_flags &= ~128;
if(xctx->graph_flags & 2) xctx->graph_flags |= 128 * gr->logx;
if(gr->logx) xctx->graph_cursor1_x = pow(10, xctx->graph_cursor1_x);
}
/* move cursor2 */
/* set cursor position from master graph x-axis */
else if(event == MotionNotify && (state & Button1Mask) && (xctx->graph_flags & 32 )) {
int floaters = there_are_floaters();
xctx->graph_cursor2_x = G_X(xctx->mousex);
xctx->graph_flags &= ~256;
if(xctx->graph_flags & 2) xctx->graph_flags |= 256 * gr->logx;
if(gr->logx) xctx->graph_cursor2_x = pow(10, xctx->graph_cursor2_x);
if(tclgetboolvar("live_cursor2_backannotate")) {
backannotate_at_cursor_b_pos(r, gr);
if(floaters) set_modify(-2); /* update floater caches to reflect actual backannotation */
@ -421,55 +419,44 @@ static int waves_callback(int event, int mx, int my, KeySym key, int button, int
if(event == ButtonPress && button == Button1) {
/* dragging cursors when mouse is very close */
if(xctx->graph_flags & 2) {
double c = xctx->graph_cursor1_x;
if(!gr->logx && (xctx->graph_flags & 128)) {
c = pow(10, c);
double cursor1 = xctx->graph_cursor1_x;
if(gr->logx ) {
cursor1 = mylog10(cursor1);
}
if(gr->logx && !(xctx->graph_flags & 128)) {
c = log10(c);
}
if(fabs(xctx->mousex - W_X(c)) < 10) {
if(fabs(xctx->mousex - W_X(cursor1)) < 10) {
xctx->graph_flags |= 16; /* Start move cursor1 */
}
}
if(xctx->graph_flags & 4) {
double c = xctx->graph_cursor2_x;
if(!gr->logx && (xctx->graph_flags & 256)) {
c = pow(10, c);
double cursor2 = xctx->graph_cursor2_x;
if(gr->logx) {
cursor2 = mylog10(cursor2);
}
if(gr->logx && !(xctx->graph_flags & 256)) {
c = log10(c);
}
if(fabs(xctx->mousex - W_X(c)) < 10) {
if(fabs(xctx->mousex - W_X(cursor2)) < 10) {
xctx->graph_flags |= 32; /* Start move cursor2 */
}
}
}
else if(event == ButtonPress && button == Button3) {
/* Numerically set cursor position */
if( (xctx->graph_flags & 2) && fabs(xctx->mousex - W_X(xctx->graph_cursor1_x)) < 10) {
if(xctx->graph_flags & 2) {
double cursor1 = xctx->graph_cursor1_x;
if(gr->logx) {
double pos = pow(10., xctx->graph_cursor1_x);
tclvareval("input_line {Pos:} {} ", dtoa_eng(pos), NULL);
if(tclresult()[0]) {
pos = mylog10(atof_eng(tclresult()));
tclvareval("xschem set cursor1_x ", dtoa(pos), NULL);
}
} else {
cursor1 = mylog10(cursor1);
}
if(fabs(xctx->mousex - W_X(cursor1)) < 10) {
tclvareval("input_line {Pos:} {xschem set cursor1_x} ", dtoa_eng(xctx->graph_cursor1_x), NULL);
}
event = 0; /* avoid further processing ButtonPress that might set GRAPHPAH */
redraw_all_at_end = 1;
}
if( (xctx->graph_flags & 4) && fabs(xctx->mousex - W_X(xctx->graph_cursor2_x)) < 10) {
if(xctx->graph_flags & 4) {
double cursor2 = xctx->graph_cursor2_x;
if(gr->logx) {
double pos = pow(10., xctx->graph_cursor2_x);
tclvareval("input_line {Pos:} {} ", dtoa_eng(pos), NULL);
if(tclresult()[0]) {
pos = mylog10(atof_eng(tclresult()));
tclvareval("xschem set cursor2_x ", dtoa(pos), NULL);
}
} else {
cursor2 = mylog10(cursor2);
}
if(fabs(xctx->mousex - W_X(cursor2)) < 10) {
tclvareval("input_line {Pos:} {xschem set cursor2_x} ", dtoa_eng(xctx->graph_cursor2_x), NULL);
}
event = 0; /* avoid further processing ButtonPress that might set GRAPHPAH */
@ -484,19 +471,19 @@ static int waves_callback(int event, int mx, int my, KeySym key, int button, int
/* x cursor1 toggle */
else if((key == 'a' && rstate == 0) ) {
xctx->graph_flags ^= 2;
xctx->graph_flags &= ~128;
if(xctx->graph_flags & 2) xctx->graph_flags |= 128 * gr->logx;
need_all_redraw = 1;
if(xctx->graph_flags & 2) xctx->graph_cursor1_x = G_X(xctx->mousex);
if(xctx->graph_flags & 2) {
xctx->graph_cursor1_x = G_X(xctx->mousex);
if(gr->logx) xctx->graph_cursor1_x = pow(10, xctx->graph_cursor1_x);
}
}
/* x cursor2 toggle */
else if((key == 'b') ) {
int floaters = there_are_floaters();
xctx->graph_flags ^= 4;
xctx->graph_flags &= ~256;
if(xctx->graph_flags & 4) xctx->graph_flags |= 256 * gr->logx;
if(xctx->graph_flags & 4) {
xctx->graph_cursor2_x = G_X(xctx->mousex);
if(gr->logx) xctx->graph_cursor2_x = pow(10, xctx->graph_cursor2_x);
if(tclgetboolvar("live_cursor2_backannotate")) {
backannotate_at_cursor_b_pos(r, gr);
if(floaters) set_modify(-2); /* update floater caches to reflect actual backannotation */

View File

@ -2902,7 +2902,7 @@ void setup_graph_data(int i, int skip, Graph_ctx *gr)
static void draw_cursor(double active_cursorx, double other_cursorx, int cursor_color, Graph_ctx *gr)
{
double xx = W_X(active_cursorx);
double xx, pos = active_cursorx;
double tx1, ty1, tx2, ty2, dtmp;
int tmp;
char tmpstr[100];
@ -2910,9 +2910,10 @@ static void draw_cursor(double active_cursorx, double other_cursorx, int cursor_
short flip = (other_cursorx > active_cursorx) ? 0 : 1;
int xoffs = flip ? 3 : -3;
if(gr->logx) pos = mylog10(pos);
xx = W_X(pos);
if(xx >= gr->x1 && xx <= gr->x2) {
drawline(cursor_color, NOW, xx, gr->ry1, xx, gr->ry2, 1, NULL);
if(gr->logx) active_cursorx = pow(10, active_cursorx);
if(gr->unitx != 1.0)
my_snprintf(tmpstr, S(tmpstr), "%.5g%c", gr->unitx * active_cursorx , gr->unitx_suffix);
else
@ -2929,9 +2930,11 @@ static void draw_cursor_difference(double c1, double c2, Graph_ctx *gr)
char tmpstr[100];
double txtsize = gr->txtsizex;
double tx1, ty1, tx2, ty2;
double aa = W_X(c1);
double cc1 = gr->logx ? mylog10(c1) : c1;
double cc2 = gr->logx ? mylog10(c2) : c2;
double aa = W_X(cc1);
double a = CLIP(aa, gr->x1, gr->x2);
double bb = W_X(c2);
double bb = W_X(cc2);
double b = CLIP(bb, gr->x1, gr->x2);
double diff = fabs(b - a);
double diffw;
@ -2941,12 +2944,7 @@ static void draw_cursor_difference(double c1, double c2, Graph_ctx *gr)
double yline;
/* if(gr->logx) return; */
if(gr->logx) {
diffw = fabs(pow(10, c2) - pow(10, c1));
} else {
diffw = fabs(c2 - c1);
}
diffw = fabs(c2 - c1);
if(gr->unitx != 1.0)
my_snprintf(tmpstr, S(tmpstr), "%.4g%c", gr->unitx * diffw , gr->unitx_suffix);
@ -3085,10 +3083,11 @@ static void show_node_measures(int measure_p, double measure_x, double measure_p
double diffx;
char *fmt1, *fmt2;
double yy1;
double cursor1 = gr->logx ? mylog10(xctx->graph_cursor1_x) : xctx->graph_cursor1_x;
yy1 = xctx->raw->values[idx][measure_p-1];
diffy = xctx->raw->values[idx][measure_p] - yy1;
diffx = measure_x - measure_prev_x;
yy = yy1 + diffy / diffx * (xctx->graph_cursor1_x - measure_prev_x);
yy = yy1 + diffy / diffx * (cursor1 - measure_prev_x);
if(XSIGN0(gr->gy1) != XSIGN0(gr->gy2) && fabs(yy) < 1e-4 * fabs(gr->gh)) yy = 0.0;
if(yy != 0.0 && fabs(yy * gr->unity) < 1.0e-3) {
fmt1="%.2e";
@ -3471,8 +3470,6 @@ int find_closest_wave(int i, Graph_ctx *gr)
* 2: draw x-cursor1
* 4: draw x-cursor2
* 8: all drawing, if not set do only XCopyArea / x-cursor if specified
* 128: cursor1 is log scale
* 256: cursor2 is log scale
* ct is a pointer used in windows for cairo
*/
void draw_graph(int i, const int flags, Graph_ctx *gr, void *ct)
@ -3717,7 +3714,9 @@ void draw_graph(int i, const int flags, Graph_ctx *gr, void *ct)
if(dataset == -1 || dataset == sweepvar_wrap) {
/* cursor1: show measurements on nodes in graph */
if(measure_p[wcnt] == -1 && flags & 2 && cnt) {
if(XSIGN(xx - xctx->graph_cursor1_x) != XSIGN(prev_x - xctx->graph_cursor1_x)) {
double cursor1 = xctx->graph_cursor1_x;
if(gr->logx) cursor1 = mylog10(cursor1);
if(XSIGN(xx - cursor1) != XSIGN(prev_x - cursor1)) {
measure_p[wcnt] = p;
measure_x[wcnt] = xx;
measure_prev_x[wcnt] = prev_x;
@ -3789,33 +3788,19 @@ void draw_graph(int i, const int flags, Graph_ctx *gr, void *ct)
* bbox(SET_INSIDE, 0.0, 0.0, 0.0, 0.0);
*/
if(flags & 8) {
double c1 = xctx->graph_cursor1_x;
double c2 = xctx->graph_cursor2_x;
if(flags & 6) {
if(!gr->logx && (xctx->graph_flags & 128)) {
c1 = pow(10, c1);
}
if(gr->logx && !(xctx->graph_flags & 128)) {
c1 = log10(c1);
}
if(!gr->logx && (xctx->graph_flags & 256)) {
c2 = pow(10, c2);
}
if(gr->logx && !(xctx->graph_flags & 256)) {
c2 = log10(c2);
}
}
double cursor1 = xctx->graph_cursor1_x;
double cursor2 = xctx->graph_cursor2_x;
/* cursor1 */
if((flags & 2)) {
draw_cursor(c1, c2, 1, gr);
draw_cursor(cursor1, cursor2, 1, gr);
}
/* cursor2 */
if((flags & 4)) {
draw_cursor(c2, c1, 3, gr);
draw_cursor(cursor2, cursor1, 3, gr);
}
/* difference between cursors */
if((flags & 2) && (flags & 4)) {
draw_cursor_difference(c1, c2, gr);
draw_cursor_difference(cursor1, cursor2, gr);
}
}
if(flags & 1) { /* copy save buffer to screen */
@ -4468,10 +4453,8 @@ void draw(void)
dbg(1, "draw(): window: %d %d %d %d\n",xctx->areax1, xctx->areay1, xctx->areax2, xctx->areay2);
if(!xctx->only_probes) drawgrid();
/* 2: draw cursor 1
* 4: draw cursor 2
* 128: cursor 1 is log scale
* 256: cursor 2 is log scale */
draw_graph_all((xctx->graph_flags & (2 | 4 | 128 | 256)) + 8); /* xctx->graph_flags for cursors */
* 4: draw cursor 2 */
draw_graph_all((xctx->graph_flags & (2 | 4)) + 8); /* xctx->graph_flags for cursors */
draw_images_all();
x1 = X_TO_XSCHEM(xctx->areax1);

View File

@ -756,10 +756,8 @@ int xschem(ClientData clientdata, Tcl_Interp *interp, int argc, const char * arg
flags = atoi(argv[3]);
} else {
/* 2: draw cursor 1
* 4: draw cursor 2
* 128: cursor 1 is log scale
* 256: cursor 2 is log scale */
flags = 1 + 8 + (xctx->graph_flags & (2 + 4 + 128 + 256));
* 4: draw cursor 2 */
flags = 1 + 8 + (xctx->graph_flags & (2 + 4));
}
setup_graph_data(i, 0, &xctx->graph_struct);
draw_graph(i, flags, &xctx->graph_struct, NULL);

View File

@ -1098,8 +1098,6 @@ typedef struct {
* 16: move cursor1
* 32: move cursor2
* 64: show measurement tooltip
* 128: cursor1 is log scale
* 256: cursor2 is log scale
*/
int graph_flags;
int graph_master; /* graph where mouse operations are started, used to lock x-axis */