⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graf.c

📁 spice中支持多层次元件模型仿真的可单独运行的插件源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/**********Copyright 1990 Regents of the University of California.  All rights reserved.Author: 1988 Jeffrey M. Hsu**********//* *  Most of the gr_ module resides here, in particular, gr_init *      and gr_point, expect for the gr_ grid routines. * */#include "spice.h"#ifdef HAS_MFB#include "mfb.h"#endif /* HAS_MFB */#include "cpdefs.h"     /* for VT_ */#include "cpextern.h"#include "fteconst.h"#include "ftedebug.h"       /* for iplot */#include "ftedata.h"        /* for struct dvec */#include "ftedefs.h"        /* for FTEextern.h and IPOINT{MIN,MAX} */#include "fteinput.h"#include "ftegraph.h"#include "ftedbgra.h"#include "ftedev.h"#include "suffix.h"extern struct dbcomm *dbs;  /* for iplot *//* note: let's try to get rid of these *//* global variables *//* Graphics mode in progress, so signal handlers know to call gr_clean *//* bool gr_gmode = false; *//* for legends, set in gr_start, reset in gr_iplot and gr_init */static int plotno;static int curcolor = 1;        /* for assigning unique colors */static int curlst = 0;          /* for assigning line styles *//* invariant:  currentgraph contains the current graph *//* These are what gets plotted as points when you specify point plots */static char pointchars[128];#define DEFPOINTCHARS   "oxabcdefhgijklmnpqrstuvwyz"/* Buffer for ticmarks if given a list */static char ticbuf[1024];static char *ticlist = ticbuf;#define MAXTICS 100double *readtics();#define XFACTOR 2       /* How much to expand the X scale during iplot. */#define YFACTOR 1.5     /* How much to expand the Y scale during iplot. *//* *  Start of a new graph. *  Fill in the data that gets displayed. *  Difference from old gr_init *    we don't try to determine the look of the screen from here *    leave to lower level routines * *//* ARGSUSED */ /* since we don't really use many of them */gr_init(xlims, ylims, xname, plotname, hcopy, nplots, xdelta, ydelta, gridtype,                plottype, xlabel, ylabel, xtype, ytype, pname, commandline)        double *xlims, *ylims;          /* The size of the screen. */        char *xname, *plotname;         /* What to label things. */	char *hcopy;                    /* The raster file. */        int nplots;                     /* How many plots there will be. */        double xdelta, ydelta;          /* Line increments for the scale. */        GRIDTYPE gridtype;              /* The grid type */        PLOTTYPE plottype;              /*  and the plot type. */        char *xlabel, *ylabel;          /* Labels for axes. */        int xtype, ytype;               /* The types of the data graphed. */        char *pname;        char *commandline;              /* For xi_zoomdata() */{    GRAPH *graph;    int b;    wordlist *wl;    char *comb_title;#ifdef HAS_MFB    if ((!strcmp(dispdev->name, "MFB")	    || !strcmp(dispdev->name, "error")) && !hcopy)	DevInit( );#endif    if (!(graph = NewGraph())) {      return(false);    }    /*        The global currentgraph will always be the current graph.    */    SetGraphContext(graph->graphid);    graph->onevalue = (xname ? false : true);    /* communicate filename to plot 5 driver */    if (hcopy) {      graph->devdep = hcopy;    }    plotno = 0;    /* note: should do only once, maybe in gr_init_once */    if (!cp_getvar("pointchars", VT_STRING, pointchars))          (void) strcpy(pointchars, DEFPOINTCHARS);    if (!cp_getvar("ticmarks", VT_NUM, (char *) &graph->ticmarks)) {      if (cp_getvar("ticmarks", VT_BOOL, (char *) &b))        graph->ticmarks = 10;      else        graph->ticmarks = 0;    }    if (cp_getvar("ticlist", VT_LIST, ticlist)) {	wl = (wordlist *)vareval("ticlist");	ticlist = (char *)wl_flatten(wl);	graph->ticdata = (double *) readtics(ticlist);    } else	graph->ticdata = NULL;    /* set slow flag to stop between each plot and prompt the        user for a return       This is used mainly for graphics terminals w/o windows.    *//*    if (incremental)      slow = false;    else      (void) cp_getvar("slowplot", VT_BOOL, (char *) &slow);*/    if (!xlims || !ylims) {      internalerror("gr_init:  no range specified");      return(false);    }    /* indicate some graphics going on *//*  gr_gmode = true; */    /* save upper and lower limits */    graph->data.xmin = xlims[0];    graph->data.xmax = xlims[1];    graph->data.ymin = ylims[0];    graph->data.ymax = ylims[1];    /* note: have enum here or some better convention */    if (NewViewport(graph) == 1) {      /* note: where is the error message generated? */      /* note: undo mallocs */      fprintf(cp_err, "Can't open viewport for graphics.\n");      return(false);    }    /* layout decisions */    /* note: have to do before gr_fixgrid and after NewViewport */    graph->viewportxoff = graph->fontwidth * 8; /* 8 lines on left */    graph->viewportyoff = graph->fontheight * 4;    /* 4 on bottom */    DevClear();    graph->grid.gridtype = gridtype;    graph->plottype = plottype;    graph->grid.xdatatype = xtype;    graph->grid.ydatatype = ytype;    graph->grid.xdelta = xdelta;    graph->grid.ydelta = ydelta;    graph->grid.ysized = 0;    graph->grid.xsized = 0;    if (!graph->onevalue) {        if (xlabel) {            graph->grid.xlabel = xlabel;        } else {            graph->grid.xlabel = xname;        }        if (ylabel) {            graph->grid.ylabel = ylabel;        }    } else {        if (xlabel) {            graph->grid.xlabel = xlabel;        } else {            graph->grid.xlabel = "real";        }        if (ylabel) {            graph->grid.ylabel = ylabel;        } else {            graph->grid.ylabel = "imag";        }    }    if (!pname)	pname = "(unknown)";    if (!plotname)	plotname = "(unknown)";    comb_title = tmalloc(strlen(plotname) + strlen(pname) + 3);    sprintf(comb_title, "%s: %s", pname, plotname);    graph->plotname = comb_title;    gr_resize_internal(graph);    gr_redrawgrid(graph);    /* Set up colors and line styles. */    if (dispdev->numlinestyles == 1)        curlst = 0; /* Use the same one all the time. */    else        curlst = 1;    /* XXX Special exception for SMITH */    if (dispdev->numcolors > 2 && (graph->grid.gridtype == GRID_SMITH	|| graph->grid.gridtype == GRID_SMITHGRID))    {        curcolor = 3;    } else        curcolor = 1;    graph->commandline = copy(commandline);    return(true);}/* *  Add a point to the curve we're currently drawing. *  Should be in between a gr_init() and a gr_end() *    expect when iplotting, very bad hack *  Differences from old gr_point: *    We save points here, instead of in lower levels. *    Assume we are in right context *  Save points in data space (not screen space). *  We pass two points in so we can multiplex plots. * */voidgr_point(dv, newx, newy, oldx, oldy, np)struct dvec *dv;double newx, newy, oldx, oldy;int np;{    int oldtox, oldtoy;     /* value before clipping */    char pointc[2];    int fromx, fromy, tox, toy;    int ymin, dummy;    DatatoScreen(currentgraph, oldx, oldy, &fromx, &fromy);    DatatoScreen(currentgraph, newx, newy, &tox, &toy);/* note: we do not particularly want to clip here */    oldtox = tox; oldtoy = toy;    if (!currentgraph->grid.circular) {      if (clip_line(&fromx, &fromy, &tox, &toy,        currentgraph->viewportxoff, currentgraph->viewportyoff,        currentgraph->viewport.width + currentgraph->viewportxoff,        currentgraph->viewport.height + currentgraph->viewportyoff))        return;    } else {      if (clip_to_circle(&fromx, &fromy, &tox, &toy,            currentgraph->grid.xaxis.circular.center,            currentgraph->grid.yaxis.circular.center,            currentgraph->grid.xaxis.circular.radius))        return;    }    if (currentgraph->plottype != PLOT_POINT) {      SetLinestyle(dv->v_linestyle);    } else {      /* if PLOT_POINT,           don't want to plot an endpoint which have been clipped */      if (tox != oldtox || toy != oldtoy)    return;    }    SetColor(dv->v_color);    switch (currentgraph->plottype) {      double	*tics;      case PLOT_LIN:    /* If it's a linear plot, ignore first point since we don't want        to connect with oldx and oldy. */	if (np)	    DrawLine(fromx, fromy, tox, toy);	if (tics = (double *) currentgraph->ticdata) {	    for (; *tics < HUGE; tics++) {		if (*tics == (double) np) {		    Text("x", (int) (tox - currentgraph->fontwidth / 2),			(int) (toy - currentgraph->fontheight / 2));		    SaveText(currentgraph, "x",			(int) (tox - currentgraph->fontwidth / 2),			(int) (toy - currentgraph->fontheight / 2));		    break;		}	    }	} else if ((currentgraph->ticmarks >0) && (np > 0)	    && (np % currentgraph->ticmarks == 0))	{	    /* Draw an 'x' */	    Text("x", (int) (tox - currentgraph->fontwidth / 2),		(int) (toy - currentgraph->fontheight / 2));	    SaveText(currentgraph, "x",		(int) (tox - currentgraph->fontwidth / 2),		(int) (toy - currentgraph->fontheight / 2));	}        break;      case PLOT_COMB:        DatatoScreen(currentgraph,                (double) 0, currentgraph->datawindow.ymin,                &dummy, &ymin);        DrawLine(tox, ymin, tox, toy);        break;      case PLOT_POINT:        /* Here, gi_linestyle is the character used for the point.  */        pointc[0] = dv->v_linestyle;        pointc[1] = '\0';        Text(pointc, (int) (tox - currentgraph->fontwidth / 2),            (int) (toy - currentgraph->fontheight / 2));        /* gr_redraw will redraw this w/o our having to save it */        /* SaveText(currentgraph, pointc,            (int) (tox - currentgraph->fontwidth / 2),            (int) (toy - currentgraph->fontheight / 2)); */      default:        break;    }}static voidgr_start_internal(dv, copyvec)struct dvec *dv;bool copyvec;{    struct dveclist *link;    char *s;    /* Do something special with poles and zeros.  Poles are 'x's, and     * zeros are 'o's.     */    s = ft_typenames(dv->v_type);        if (eq(s, "pole")) {                dv->v_linestyle = 'x';                return;        } else if (eq(s, "zero")) {                dv->v_linestyle = 'o';                return;        }    /* Find a (hopefully) new line style and color. */    if (currentgraph->plottype == PLOT_POINT) {	if (pointchars[curlst - 1])	    curlst++;	else	    curlst = 2;    } else if ((curlst > 0) && (++curlst == dispdev->numlinestyles))	curlst = 2;    if ((curcolor > 0) && (++curcolor == dispdev->numcolors))	curcolor = (((currentgraph->grid.gridtype == GRID_SMITH	    || currentgraph->grid.gridtype == GRID_SMITHGRID) &&	    (dispdev->numcolors > 3)) ? 4 : 2);    if (currentgraph->plottype == PLOT_POINT)	dv->v_linestyle = pointchars[curlst - 2];    else	dv->v_linestyle = curlst;    dv->v_color = curcolor;/* note: XXX */#ifdef notdef    /* This is a minor hack -- reset the color */    if (dv->v_defcolor)        ReSetColor(curcolor, dv->v_defcolor);#endif    /* save the data so we can refresh */    link = (struct dveclist *) calloc(1, sizeof(struct dveclist));    link->next = currentgraph->plotdata;    if (copyvec) {      link->vector = vec_copy(dv);      /* vec_copy doesn't set v_color or v_linestyle */      link->vector->v_color = dv->v_color;      link->vector->v_linestyle = dv->v_linestyle;      link->vector->v_flags |= VF_PERMANENT;    } else {      link->vector = dv;    }    currentgraph->plotdata = link;    /* Put the legend entry on the screen. */    drawlegend(currentgraph, plotno, dv);    plotno++;}/* start one plot of a graph */voidgr_start(dv)struct dvec *dv;{    gr_start_internal(dv, true);}/* make sure the linestyles in this graph don't exceed the number of    linestyles available in the current display device */gr_relinestyle(graph)GRAPH *graph;{    struct dveclist *link;    for (link = graph->plotdata; link; link = link->next) {      if (graph->plottype == PLOT_POINT) continue;      if (!(link->vector->v_linestyle < dispdev->numlinestyles)) {        link->vector->v_linestyle %= dispdev->numlinestyles;      }      if (!(link->vector->v_color < dispdev->numcolors)) {        link->vector->v_color %= dispdev->numcolors;      }    }}staticdrawlegend(graph, plotno, dv)GRAPH *graph;int plotno;struct dvec *dv;{    int x, y, i;    char buf[16];    x = ((plotno % 2) ? graph->viewportxoff :            ((graph->viewport.width) / 2));    y = graph->absolute.height - graph->fontheight            - ((plotno + 2) / 2) * (graph->fontheight);    i = y + graph->fontheight / 2 + 1;    SetColor(dv->v_color);    if (graph->plottype == PLOT_POINT) {        (void) sprintf(buf, "%c : ", dv->v_linestyle);        Text(buf, x + graph->viewport.width / 20                - 3 * graph->fontwidth, y);    } else {        SetLinestyle(dv->v_linestyle);        DrawLine(x, i, x + graph->viewport.width / 20, i);    }    SetColor(1);    Text(dv->v_name, x + graph->viewport.width / 20            + graph->fontwidth, y);}/* end one plot of a graph */voidgr_end(dv)struct dvec *dv;{    Update();#ifdef HAS_MFB    /* hack! */    if (!strcmp(dispdev->name, "MFB") && dv->v_link2 == NULL	&& MFBCurrent->fileDesc == 1)    {      gr_pmsg("Hit return to continue");      MFBUpdate( );	 /* XXX ??? */    }#endif}/* Print text in the bottom line. */voidgr_pmsg(text)    char *text;{    char buf[BSIZE_SP];    buf[0] = 0;    Update();    if (cp_getvar("device", VT_STRING, buf)	    && !(strcmp("/dev/tty", buf) == 0))	fprintf(cp_err, "%s", text);    else	Text(text, currentgraph->viewport.width		- (strlen(currentgraph->grid.xlabel) + 3)		* currentgraph->fontwidth,		currentgraph->absolute.height - currentgraph->fontheight);    Update();    (void) getchar();    return;}voidgr_clean(){    Update();    return;}/* call this routine after viewport size changes */gr_resize(graph)GRAPH *graph;{    double oldxratio, oldyratio;    double scalex, scaley;    struct _keyed *k;    oldxratio = graph->aspectratiox;    oldyratio = graph->aspectratioy;    graph->grid.xsized = 0;    graph->grid.ysized = 0;    gr_resize_internal(graph);    /* scale keyed text */    scalex = oldxratio / graph->aspectratiox;    scaley = oldyratio / graph->aspectratioy;    for (k = graph->keyed; k; k = k->next) {      k->x = (k->x - graph->viewportxoff) * scalex + graph->viewportxoff;      k->y = (k->y - graph->viewportyoff) * scaley + graph->viewportyoff;    }    /* X also generates an expose after a resize    This is handled in X10 by not redrawing on resizes and waiting for    the expose event to redraw.  In X11, the expose routine tries to    be clever and only redraws the region specified in an expose    event, which does not cover the entire region of the plot if the    resize was from a small window to a larger window.  So in order    to keep the clever X11 expose event handling, we have the X11    resize routine pull out expose events for that window, and we    redraw on resize also.    */#ifndef HAS_X_    gr_redraw(graph);#endif}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -