142963b84SBarry Smith /* 2*183a438dSBarry Smith Defines the operations for the TikZ 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" 44eecff6a3SLisandro 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 58*183a438dSBarry Smith static const char *TikZColors[] = {"white","black","red","green","cyan","blue","magenta",0,0,"orange","violet","brown","pink",0,"yellow",0}; 5942963b84SBarry Smith 6042963b84SBarry Smith PETSC_STATIC_INLINE const char *TikZColorMap(int cl) 6142963b84SBarry Smith { 6242963b84SBarry Smith return((cl < 16) ? (TikZColors[cl] ? TikZColors[cl] : "black") : "black"); 6342963b84SBarry Smith } 6442963b84SBarry Smith 6542963b84SBarry Smith /* 6642963b84SBarry Smith These macros transform from the users coordinates to the (0,0) -> (1,1) coordinate system 6742963b84SBarry Smith */ 6842963b84SBarry 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)))) 6942963b84SBarry 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)))) 7042963b84SBarry Smith 7142963b84SBarry Smith #undef __FUNCT__ 7242963b84SBarry Smith #define __FUNCT__ "PetscDrawClear_TikZ" 73eecff6a3SLisandro Dalcin static PetscErrorCode PetscDrawClear_TikZ(PetscDraw draw) 7442963b84SBarry Smith { 7542963b84SBarry Smith PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data; 765b399a63SLisandro Dalcin PetscBool written; 7742963b84SBarry Smith PetscErrorCode ierr; 7842963b84SBarry Smith 7942963b84SBarry Smith PetscFunctionBegin; 8042963b84SBarry Smith /* often PETSc generates unneeded clears, we want avoid creating empy pictures for them */ 815b399a63SLisandro Dalcin ierr = MPI_Allreduce(&win->written,&written,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)(draw)));CHKERRQ(ierr); 825b399a63SLisandro Dalcin if (!written) PetscFunctionReturn(0); 83ce94432eSBarry Smith ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_END_FRAME);CHKERRQ(ierr); 84ce94432eSBarry Smith ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_BEGIN_FRAME);CHKERRQ(ierr); 855b399a63SLisandro Dalcin win->written = PETSC_FALSE; 8671d8d82dSLisandro Dalcin PetscFunctionReturn(0); 8771d8d82dSLisandro Dalcin } 8871d8d82dSLisandro Dalcin 8971d8d82dSLisandro Dalcin #undef __FUNCT__ 9042963b84SBarry Smith #define __FUNCT__ "PetscDrawLine_TikZ" 91eecff6a3SLisandro Dalcin static PetscErrorCode PetscDrawLine_TikZ(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int cl) 9242963b84SBarry Smith { 9342963b84SBarry Smith PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data; 9442963b84SBarry Smith PetscErrorCode ierr; 9542963b84SBarry Smith 9642963b84SBarry Smith PetscFunctionBegin; 9742963b84SBarry Smith win->written = PETSC_TRUE; 98ce94432eSBarry 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); 9942963b84SBarry Smith PetscFunctionReturn(0); 10042963b84SBarry Smith } 10142963b84SBarry Smith 10242963b84SBarry Smith #undef __FUNCT__ 10342963b84SBarry Smith #define __FUNCT__ "PetscDrawString_TikZ" 104eecff6a3SLisandro Dalcin static PetscErrorCode PetscDrawString_TikZ(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[]) 10542963b84SBarry Smith { 10642963b84SBarry Smith PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data; 10742963b84SBarry Smith PetscErrorCode ierr; 10842963b84SBarry Smith 10942963b84SBarry Smith PetscFunctionBegin; 11042963b84SBarry Smith win->written = PETSC_TRUE; 111ce94432eSBarry 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); 11242963b84SBarry Smith PetscFunctionReturn(0); 11342963b84SBarry Smith } 11442963b84SBarry Smith 11542963b84SBarry Smith #undef __FUNCT__ 116d6ed00deSBarry Smith #define __FUNCT__ "PetscDrawStringVertical_TikZ" 117eecff6a3SLisandro Dalcin static PetscErrorCode PetscDrawStringVertical_TikZ(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[]) 118d6ed00deSBarry Smith { 119d6ed00deSBarry Smith PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data; 120d6ed00deSBarry Smith PetscErrorCode ierr; 121d6ed00deSBarry Smith size_t len; 122d6ed00deSBarry Smith PetscReal width; 123d6ed00deSBarry Smith 124d6ed00deSBarry Smith PetscFunctionBegin; 125d6ed00deSBarry Smith win->written = PETSC_TRUE; 126d6ed00deSBarry Smith ierr = PetscStrlen(text,&len);CHKERRQ(ierr); 127d6ed00deSBarry Smith ierr = PetscDrawStringGetSize(draw,&width,NULL);CHKERRQ(ierr); 128d6ed00deSBarry Smith yl = yl - len*width*(draw->coor_yr - draw->coor_yl)/(draw->coor_xr - draw->coor_xl); 129d6ed00deSBarry 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); 130d6ed00deSBarry Smith PetscFunctionReturn(0); 131d6ed00deSBarry Smith } 132d6ed00deSBarry Smith 133d6ed00deSBarry Smith #undef __FUNCT__ 13451fa3d41SBarry Smith #define __FUNCT__ "PetscDrawStringBoxed_TikZ" 13542963b84SBarry Smith /* 13642963b84SBarry Smith Does not handle multiline strings correctly 13742963b84SBarry Smith */ 138eecff6a3SLisandro Dalcin static PetscErrorCode PetscDrawStringBoxed_TikZ(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,int ct,const char text[],PetscReal *w,PetscReal *h) 13942963b84SBarry Smith { 14042963b84SBarry Smith PetscDraw_TikZ *win = (PetscDraw_TikZ*)draw->data; 14142963b84SBarry Smith PetscErrorCode ierr; 14242963b84SBarry Smith size_t len; 14342963b84SBarry Smith 14442963b84SBarry Smith PetscFunctionBegin; 14542963b84SBarry Smith win->written = PETSC_TRUE; 146ce94432eSBarry 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); 14742963b84SBarry Smith 14842963b84SBarry Smith /* make up totally bogus height and width of box */ 14942963b84SBarry Smith ierr = PetscStrlen(text,&len);CHKERRQ(ierr); 15042963b84SBarry Smith if (w) *w = .07*len; 15142963b84SBarry Smith if (h) *h = .07; 15242963b84SBarry Smith PetscFunctionReturn(0); 15342963b84SBarry Smith } 15442963b84SBarry Smith 155d6ed00deSBarry Smith #undef __FUNCT__ 156d6ed00deSBarry Smith #define __FUNCT__ "PetscDrawStringGetSize_TikZ" 157eecff6a3SLisandro Dalcin static PetscErrorCode PetscDrawStringGetSize_TikZ(PetscDraw draw,PetscReal *x,PetscReal *y) 158d6ed00deSBarry Smith { 159d6ed00deSBarry Smith PetscFunctionBegin; 160d6ed00deSBarry Smith if (x) *x = .014*(draw->coor_xr - draw->coor_xl)/((draw->port_xr - draw->port_xl)); 161d6ed00deSBarry Smith if (y) *y = .05*(draw->coor_yr - draw->coor_yl)/((draw->port_yr - draw->port_yl)); 162d6ed00deSBarry Smith PetscFunctionReturn(0); 163d6ed00deSBarry Smith } 164d6ed00deSBarry Smith 16542963b84SBarry Smith static struct _PetscDrawOps DvOps = { 0, 16642963b84SBarry Smith 0, 16742963b84SBarry Smith PetscDrawLine_TikZ, 16842963b84SBarry Smith 0, 16942963b84SBarry Smith 0, 17042963b84SBarry Smith 0, 17142963b84SBarry Smith 0, 17242963b84SBarry Smith PetscDrawString_TikZ, 173d6ed00deSBarry Smith PetscDrawStringVertical_TikZ, 17442963b84SBarry Smith 0, 175d6ed00deSBarry Smith PetscDrawStringGetSize_TikZ, 17642963b84SBarry Smith 0, 17742963b84SBarry Smith PetscDrawClear_TikZ, 17842963b84SBarry Smith 0, 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 PetscDrawDestroy_TikZ, 19042963b84SBarry Smith 0, 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, 20051fa3d41SBarry Smith PetscDrawStringBoxed_TikZ}; 20142963b84SBarry Smith 20242963b84SBarry Smith #undef __FUNCT__ 20342963b84SBarry Smith #define __FUNCT__ "PetscDrawCreate_TikZ" 2048cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscDrawCreate_TikZ(PetscDraw draw) 20542963b84SBarry Smith { 20642963b84SBarry Smith PetscDraw_TikZ *win; 20742963b84SBarry Smith PetscErrorCode ierr; 20842963b84SBarry Smith 20942963b84SBarry Smith PetscFunctionBegin; 21042963b84SBarry Smith ierr = PetscMemcpy(draw->ops,&DvOps,sizeof(DvOps));CHKERRQ(ierr); 211b00a9115SJed Brown ierr = PetscNew(&win);CHKERRQ(ierr); 2123bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)draw,sizeof(PetscDraw_TikZ));CHKERRQ(ierr); 213a297a907SKarl Rupp 21442963b84SBarry Smith draw->data = (void*) win; 21542963b84SBarry Smith 21642963b84SBarry Smith if (draw->title) { 21742963b84SBarry Smith ierr = PetscStrallocpy(draw->title,&win->filename);CHKERRQ(ierr); 21842963b84SBarry Smith } else { 21942963b84SBarry Smith const char *fname; 22042963b84SBarry Smith ierr = PetscObjectGetName((PetscObject)draw,&fname);CHKERRQ(ierr); 22142963b84SBarry Smith ierr = PetscStrallocpy(fname,&win->filename);CHKERRQ(ierr); 22242963b84SBarry Smith } 223ce94432eSBarry Smith ierr = PetscFOpen(PetscObjectComm((PetscObject)draw),win->filename,"w",&win->fd);CHKERRQ(ierr); 224ce94432eSBarry Smith ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_BEGIN_DOCUMENT);CHKERRQ(ierr); 225ce94432eSBarry Smith ierr = PetscFPrintf(PetscObjectComm((PetscObject)draw),win->fd,TikZ_BEGIN_FRAME);CHKERRQ(ierr); 226a297a907SKarl Rupp 22742963b84SBarry Smith win->written = PETSC_FALSE; 22842963b84SBarry Smith PetscFunctionReturn(0); 22942963b84SBarry Smith } 230