142963b84SBarry Smith /* 242963b84SBarry Smith Defines the operations for the X PetscDraw implementation. 342963b84SBarry Smith */ 442963b84SBarry Smith 5af0996ceSBarry Smith #include <petsc/private/drawimpl.h> /*I "petscsys.h" I*/ 642963b84SBarry Smith 742963b84SBarry Smith typedef struct { 842963b84SBarry Smith char *filename; 942963b84SBarry Smith FILE *fd; 1042963b84SBarry Smith PetscBool written; /* something has been written to the current frame */ 1142963b84SBarry Smith } PetscDraw_TikZ; 1242963b84SBarry Smith 1342963b84SBarry Smith #define TikZ_BEGIN_DOCUMENT "\\documentclass{beamer}\n\n\ 1442963b84SBarry Smith \\usepackage{tikz}\n\ 1542963b84SBarry Smith \\usepackage{pgflibraryshapes}\n\ 1642963b84SBarry Smith \\usetikzlibrary{backgrounds}\n\ 1742963b84SBarry Smith \\usetikzlibrary{arrows}\n\ 1842963b84SBarry Smith \\newenvironment{changemargin}[2]{%%\n\ 1942963b84SBarry Smith \\begin{list}{}{%%\n\ 2042963b84SBarry Smith \\setlength{\\topsep}{0pt}%%\n\ 2142963b84SBarry Smith \\setlength{\\leftmargin}{#1}%%\n\ 2242963b84SBarry Smith \\setlength{\\rightmargin}{#2}%%\n\ 2342963b84SBarry Smith \\setlength{\\listparindent}{\\parindent}%%\n\ 2442963b84SBarry Smith \\setlength{\\itemindent}{\\parindent}%%\n\ 2542963b84SBarry Smith \\setlength{\\parsep}{\\parskip}%%\n\ 2642963b84SBarry Smith }%%\n\ 2742963b84SBarry Smith \\item[]}{\\end{list}}\n\n\ 2842963b84SBarry Smith \\begin{document}\n" 2942963b84SBarry Smith 3042963b84SBarry Smith #define TikZ_BEGIN_FRAME "\\begin{frame}{}\n\ 3142963b84SBarry Smith \\begin{changemargin}{-1cm}{0cm}\n\ 3242963b84SBarry Smith \\begin{center}\n\ 3342963b84SBarry Smith \\begin{tikzpicture}[scale = 10.00,font=\\fontsize{8}{8}\\selectfont]\n" 3442963b84SBarry Smith 3542963b84SBarry Smith #define TikZ_END_FRAME "\\end{tikzpicture}\n\ 3642963b84SBarry Smith \\end{center}\n\ 3742963b84SBarry Smith \\end{changemargin}\n\ 3842963b84SBarry Smith \\end{frame}\n" 3942963b84SBarry Smith 4042963b84SBarry Smith #define TikZ_END_DOCUMENT "\\end{document}\n" 4142963b84SBarry Smith 4242963b84SBarry Smith #undef __FUNCT__ 4342963b84SBarry Smith #define __FUNCT__ "PetscDrawDestroy_TikZ" 44*eecff6a3SLisandro Dalcin static PetscErrorCode PetscDrawDestroy_TikZ(PetscDraw draw) 4542963b84SBarry Smith { 4642963b84SBarry Smith PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data; 4742963b84SBarry Smith PetscErrorCode ierr; 4842963b84SBarry Smith 4942963b84SBarry Smith PetscFunctionBegin; 50ce94432eSBarry Smith ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_END_FRAME);CHKERRQ(ierr); 51ce94432eSBarry Smith ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_END_DOCUMENT);CHKERRQ(ierr); 52ce94432eSBarry Smith ierr = PetscFClose(PetscObjectComm((PetscObject)draw),win->fd);CHKERRQ(ierr); 5342963b84SBarry Smith ierr = PetscFree(win->filename);CHKERRQ(ierr); 5450c74209SLisandro Dalcin ierr = PetscFree(draw->data);CHKERRQ(ierr); 5542963b84SBarry Smith PetscFunctionReturn(0); 5642963b84SBarry Smith } 5742963b84SBarry Smith 5842963b84SBarry Smith static const char *TikZColors[] = { "white", "black", "red", "green", "cyan", "blue", "magenta", 0, 0, "orange", 5942963b84SBarry Smith "violet", "brown", "pink", 0, "yellow", 0}; 6042963b84SBarry Smith 6142963b84SBarry Smith PETSC_STATIC_INLINE const char *TikZColorMap(int cl) 6242963b84SBarry Smith { 6342963b84SBarry Smith return((cl < 16) ? (TikZColors[cl] ? TikZColors[cl] : "black") : "black"); 6442963b84SBarry Smith } 6542963b84SBarry Smith 6642963b84SBarry Smith /* 6742963b84SBarry Smith These macros transform from the users coordinates to the (0,0) -> (1,1) coordinate system 6842963b84SBarry Smith */ 6942963b84SBarry Smith #define XTRANS(draw,x) (double)(((draw)->port_xl + (((x - (draw)->coor_xl)*((draw)->port_xr - (draw)->port_xl))/((draw)->coor_xr - (draw)->coor_xl)))) 7042963b84SBarry Smith #define YTRANS(draw,y) (double)(((draw)->port_yl + (((y - (draw)->coor_yl)*((draw)->port_yr - (draw)->port_yl))/((draw)->coor_yr - (draw)->coor_yl)))) 7142963b84SBarry Smith 7242963b84SBarry Smith #undef __FUNCT__ 7342963b84SBarry Smith #define __FUNCT__ "PetscDrawClear_TikZ" 74*eecff6a3SLisandro Dalcin static PetscErrorCode PetscDrawClear_TikZ(PetscDraw draw) 7542963b84SBarry Smith { 7642963b84SBarry Smith PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data; 775b399a63SLisandro Dalcin PetscBool written; 7842963b84SBarry Smith PetscErrorCode ierr; 7942963b84SBarry Smith 8042963b84SBarry Smith PetscFunctionBegin; 8142963b84SBarry Smith /* often PETSc generates unneeded clears, we want avoid creating empy pictures for them */ 825b399a63SLisandro Dalcin ierr = MPI_Allreduce(&win->written,&written,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)(draw)));CHKERRQ(ierr); 835b399a63SLisandro Dalcin if (!written) PetscFunctionReturn(0); 84ce94432eSBarry Smith ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_END_FRAME);CHKERRQ(ierr); 85ce94432eSBarry Smith ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_BEGIN_FRAME);CHKERRQ(ierr); 865b399a63SLisandro Dalcin win->written = PETSC_FALSE; 8771d8d82dSLisandro Dalcin PetscFunctionReturn(0); 8871d8d82dSLisandro Dalcin } 8971d8d82dSLisandro Dalcin 9071d8d82dSLisandro Dalcin #undef __FUNCT__ 9142963b84SBarry Smith #define __FUNCT__ "PetscDrawLine_TikZ" 92*eecff6a3SLisandro Dalcin static PetscErrorCode PetscDrawLine_TikZ(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int cl) 9342963b84SBarry Smith { 9442963b84SBarry Smith PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data; 9542963b84SBarry Smith PetscErrorCode ierr; 9642963b84SBarry Smith 9742963b84SBarry Smith PetscFunctionBegin; 9842963b84SBarry Smith win->written = PETSC_TRUE; 99ce94432eSBarry Smith ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,"\\draw [%s] (%g,%g) --(%g,%g);\n",TikZColorMap(cl),XTRANS(draw,xl),YTRANS(draw,yl),XTRANS(draw,xr),YTRANS(draw,yr));CHKERRQ(ierr); 10042963b84SBarry Smith PetscFunctionReturn(0); 10142963b84SBarry Smith } 10242963b84SBarry Smith 10342963b84SBarry Smith #undef __FUNCT__ 10442963b84SBarry Smith #define __FUNCT__ "PetscDrawString_TikZ" 105*eecff6a3SLisandro Dalcin static PetscErrorCode PetscDrawString_TikZ(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[]) 10642963b84SBarry Smith { 10742963b84SBarry Smith PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data; 10842963b84SBarry Smith PetscErrorCode ierr; 10942963b84SBarry Smith 11042963b84SBarry Smith PetscFunctionBegin; 11142963b84SBarry Smith win->written = PETSC_TRUE; 112ce94432eSBarry Smith ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,"\\node [above right, %s] at (%g,%g) {%s};\n",TikZColorMap(cl),XTRANS(draw,xl),YTRANS(draw,yl),text);CHKERRQ(ierr); 11342963b84SBarry Smith PetscFunctionReturn(0); 11442963b84SBarry Smith } 11542963b84SBarry Smith 11642963b84SBarry Smith #undef __FUNCT__ 117d6ed00deSBarry Smith #define __FUNCT__ "PetscDrawStringVertical_TikZ" 118*eecff6a3SLisandro Dalcin static PetscErrorCode PetscDrawStringVertical_TikZ(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[]) 119d6ed00deSBarry Smith { 120d6ed00deSBarry Smith PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data; 121d6ed00deSBarry Smith PetscErrorCode ierr; 122d6ed00deSBarry Smith size_t len; 123d6ed00deSBarry Smith PetscReal width; 124d6ed00deSBarry Smith 125d6ed00deSBarry Smith PetscFunctionBegin; 126d6ed00deSBarry Smith win->written = PETSC_TRUE; 127d6ed00deSBarry Smith ierr = PetscStrlen(text,&len);CHKERRQ(ierr); 128d6ed00deSBarry Smith ierr = PetscDrawStringGetSize(draw,&width,NULL);CHKERRQ(ierr); 129d6ed00deSBarry Smith yl = yl - len*width*(draw->coor_yr - draw->coor_yl)/(draw->coor_xr - draw->coor_xl); 130d6ed00deSBarry Smith ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,"\\node [rotate=90, %s] at (%g,%g) {%s};\n",TikZColorMap(cl),XTRANS(draw,xl),YTRANS(draw,yl),text);CHKERRQ(ierr); 131d6ed00deSBarry Smith PetscFunctionReturn(0); 132d6ed00deSBarry Smith } 133d6ed00deSBarry Smith 134d6ed00deSBarry Smith #undef __FUNCT__ 13551fa3d41SBarry Smith #define __FUNCT__ "PetscDrawStringBoxed_TikZ" 13642963b84SBarry Smith /* 13742963b84SBarry Smith Does not handle multiline strings correctly 13842963b84SBarry Smith */ 139*eecff6a3SLisandro Dalcin static PetscErrorCode PetscDrawStringBoxed_TikZ(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,int ct,const char text[],PetscReal *w,PetscReal *h) 14042963b84SBarry Smith { 14142963b84SBarry Smith PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data; 14242963b84SBarry Smith PetscErrorCode ierr; 14342963b84SBarry Smith size_t len; 14442963b84SBarry Smith 14542963b84SBarry Smith PetscFunctionBegin; 14642963b84SBarry Smith win->written = PETSC_TRUE; 147ce94432eSBarry Smith ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,"\\draw (%g,%g) node [rectangle, draw, align=center, inner sep=1ex] {%s};\n",XTRANS(draw,xl),YTRANS(draw,yl),text);CHKERRQ(ierr); 14842963b84SBarry Smith 14942963b84SBarry Smith /* make up totally bogus height and width of box */ 15042963b84SBarry Smith ierr = PetscStrlen(text,&len);CHKERRQ(ierr); 15142963b84SBarry Smith if (w) *w = .07*len; 15242963b84SBarry Smith if (h) *h = .07; 15342963b84SBarry Smith PetscFunctionReturn(0); 15442963b84SBarry Smith } 15542963b84SBarry Smith 156d6ed00deSBarry Smith #undef __FUNCT__ 157d6ed00deSBarry Smith #define __FUNCT__ "PetscDrawStringGetSize_TikZ" 158*eecff6a3SLisandro Dalcin static PetscErrorCode PetscDrawStringGetSize_TikZ(PetscDraw draw,PetscReal *x,PetscReal *y) 159d6ed00deSBarry Smith { 160d6ed00deSBarry Smith PetscFunctionBegin; 161d6ed00deSBarry Smith if (x) *x = .014*(draw->coor_xr - draw->coor_xl)/((draw->port_xr - draw->port_xl)); 162d6ed00deSBarry Smith if (y) *y = .05*(draw->coor_yr - draw->coor_yl)/((draw->port_yr - draw->port_yl)); 163d6ed00deSBarry Smith PetscFunctionReturn(0); 164d6ed00deSBarry Smith } 165d6ed00deSBarry Smith 16642963b84SBarry Smith static struct _PetscDrawOps DvOps = { 0, 16742963b84SBarry Smith 0, 16842963b84SBarry Smith PetscDrawLine_TikZ, 16942963b84SBarry Smith 0, 17042963b84SBarry Smith 0, 17142963b84SBarry Smith 0, 17242963b84SBarry Smith 0, 17342963b84SBarry Smith PetscDrawString_TikZ, 174d6ed00deSBarry Smith PetscDrawStringVertical_TikZ, 17542963b84SBarry Smith 0, 176d6ed00deSBarry Smith PetscDrawStringGetSize_TikZ, 17742963b84SBarry Smith 0, 17842963b84SBarry Smith PetscDrawClear_TikZ, 17942963b84SBarry Smith 0, 18042963b84SBarry Smith 0, 18142963b84SBarry Smith 0, 18242963b84SBarry Smith 0, 18342963b84SBarry Smith 0, 18442963b84SBarry Smith 0, 18542963b84SBarry Smith 0, 18642963b84SBarry Smith 0, 18742963b84SBarry Smith 0, 18842963b84SBarry Smith 0, 18942963b84SBarry Smith 0, 19042963b84SBarry Smith PetscDrawDestroy_TikZ, 19142963b84SBarry Smith 0, 19242963b84SBarry Smith 0, 19342963b84SBarry Smith 0, 19442963b84SBarry Smith 0, 19542963b84SBarry Smith 0, 19642963b84SBarry Smith 0, 19742963b84SBarry Smith 0, 19842963b84SBarry Smith 0, 19942963b84SBarry Smith 0, 20042963b84SBarry Smith 0, 20151fa3d41SBarry Smith PetscDrawStringBoxed_TikZ}; 20242963b84SBarry Smith 20342963b84SBarry Smith #undef __FUNCT__ 20442963b84SBarry Smith #define __FUNCT__ "PetscDrawCreate_TikZ" 2058cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscDrawCreate_TikZ(PetscDraw draw) 20642963b84SBarry Smith { 20742963b84SBarry Smith PetscDraw_TikZ *win; 20842963b84SBarry Smith PetscErrorCode ierr; 20942963b84SBarry Smith 21042963b84SBarry Smith PetscFunctionBegin; 21142963b84SBarry Smith ierr = PetscMemcpy(draw->ops,&DvOps,sizeof(DvOps));CHKERRQ(ierr); 212b00a9115SJed Brown ierr = PetscNew(&win);CHKERRQ(ierr); 2133bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)draw,sizeof(PetscDraw_TikZ));CHKERRQ(ierr); 214a297a907SKarl Rupp 21542963b84SBarry Smith draw->data = (void*) win; 21642963b84SBarry Smith 21742963b84SBarry Smith if (draw->title) { 21842963b84SBarry Smith ierr = PetscStrallocpy(draw->title,&win->filename);CHKERRQ(ierr); 21942963b84SBarry Smith } else { 22042963b84SBarry Smith const char *fname; 22142963b84SBarry Smith ierr = PetscObjectGetName((PetscObject)draw,&fname);CHKERRQ(ierr); 22242963b84SBarry Smith ierr = PetscStrallocpy(fname,&win->filename);CHKERRQ(ierr); 22342963b84SBarry Smith } 224ce94432eSBarry Smith ierr = PetscFOpen(PetscObjectComm((PetscObject)draw),win->filename,"w",&win->fd);CHKERRQ(ierr); 225ce94432eSBarry Smith ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_BEGIN_DOCUMENT);CHKERRQ(ierr); 226ce94432eSBarry Smith ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_BEGIN_FRAME);CHKERRQ(ierr); 227a297a907SKarl Rupp 22842963b84SBarry Smith win->written = PETSC_FALSE; 22942963b84SBarry Smith PetscFunctionReturn(0); 23042963b84SBarry Smith } 231