1*5c6c1daeSBarry Smith 2*5c6c1daeSBarry Smith /* 3*5c6c1daeSBarry Smith Provides the calling sequences for all the basic PetscDraw routines. 4*5c6c1daeSBarry Smith */ 5*5c6c1daeSBarry Smith #include <petsc-private/drawimpl.h> /*I "petscdraw.h" I*/ 6*5c6c1daeSBarry Smith 7*5c6c1daeSBarry Smith #undef __FUNCT__ 8*5c6c1daeSBarry Smith #define __FUNCT__ "PetscDrawTriangle" 9*5c6c1daeSBarry Smith /*@ 10*5c6c1daeSBarry Smith PetscDrawTriangle - PetscDraws a triangle onto a drawable. 11*5c6c1daeSBarry Smith 12*5c6c1daeSBarry Smith Not Collective 13*5c6c1daeSBarry Smith 14*5c6c1daeSBarry Smith Input Parameters: 15*5c6c1daeSBarry Smith + draw - the drawing context 16*5c6c1daeSBarry Smith . x1,y1,x2,y2,x3,y3 - the coordinates of the vertices 17*5c6c1daeSBarry Smith - c1,c2,c3 - the colors of the three vertices in the same order as the xi,yi 18*5c6c1daeSBarry Smith 19*5c6c1daeSBarry Smith Level: beginner 20*5c6c1daeSBarry Smith 21*5c6c1daeSBarry Smith Concepts: drawing^triangle 22*5c6c1daeSBarry Smith Concepts: graphics^triangle 23*5c6c1daeSBarry Smith Concepts: triangle 24*5c6c1daeSBarry Smith 25*5c6c1daeSBarry Smith @*/ 26*5c6c1daeSBarry Smith PetscErrorCode PetscDrawTriangle(PetscDraw draw,PetscReal x1,PetscReal y_1,PetscReal x2,PetscReal y2,PetscReal x3,PetscReal y3, 27*5c6c1daeSBarry Smith int c1,int c2,int c3) 28*5c6c1daeSBarry Smith { 29*5c6c1daeSBarry Smith PetscErrorCode ierr; 30*5c6c1daeSBarry Smith PetscBool isnull; 31*5c6c1daeSBarry Smith 32*5c6c1daeSBarry Smith PetscFunctionBegin; 33*5c6c1daeSBarry Smith PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1); 34*5c6c1daeSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);CHKERRQ(ierr); 35*5c6c1daeSBarry Smith if (isnull) PetscFunctionReturn(0); 36*5c6c1daeSBarry Smith ierr = (*draw->ops->triangle)(draw,x1,y_1,x2,y2,x3,y3,c1,c2,c3);CHKERRQ(ierr); 37*5c6c1daeSBarry Smith PetscFunctionReturn(0); 38*5c6c1daeSBarry Smith } 39*5c6c1daeSBarry Smith 40*5c6c1daeSBarry Smith 41*5c6c1daeSBarry Smith #undef __FUNCT__ 42*5c6c1daeSBarry Smith #define __FUNCT__ "PetscDrawScalePopup" 43*5c6c1daeSBarry Smith /*@ 44*5c6c1daeSBarry Smith PetscDrawScalePopup - PetscDraws a contour scale window. 45*5c6c1daeSBarry Smith 46*5c6c1daeSBarry Smith Collective on PetscDraw 47*5c6c1daeSBarry Smith 48*5c6c1daeSBarry Smith Input Parameters: 49*5c6c1daeSBarry Smith + popup - the window (often a window obtained via PetscDrawGetPopup() 50*5c6c1daeSBarry Smith . min - minimum value being plotted 51*5c6c1daeSBarry Smith - max - maximum value being plotted 52*5c6c1daeSBarry Smith 53*5c6c1daeSBarry Smith Level: intermediate 54*5c6c1daeSBarry Smith 55*5c6c1daeSBarry Smith Notes: 56*5c6c1daeSBarry Smith All processors that share the draw MUST call this routine 57*5c6c1daeSBarry Smith 58*5c6c1daeSBarry Smith @*/ 59*5c6c1daeSBarry Smith PetscErrorCode PetscDrawScalePopup(PetscDraw popup,PetscReal min,PetscReal max) 60*5c6c1daeSBarry Smith { 61*5c6c1daeSBarry Smith PetscReal xl = 0.0,yl = 0.0,xr = 2.0,yr = 1.0,value; 62*5c6c1daeSBarry Smith PetscErrorCode ierr; 63*5c6c1daeSBarry Smith int i,c = PETSC_DRAW_BASIC_COLORS,rank; 64*5c6c1daeSBarry Smith char string[32]; 65*5c6c1daeSBarry Smith MPI_Comm comm; 66*5c6c1daeSBarry Smith 67*5c6c1daeSBarry Smith PetscFunctionBegin; 68*5c6c1daeSBarry Smith ierr = PetscDrawCheckResizedWindow(popup);CHKERRQ(ierr); 69*5c6c1daeSBarry Smith ierr = PetscObjectGetComm((PetscObject)popup,&comm);CHKERRQ(ierr); 70*5c6c1daeSBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 71*5c6c1daeSBarry Smith if (rank) PetscFunctionReturn(0); 72*5c6c1daeSBarry Smith 73*5c6c1daeSBarry Smith for (i=0; i<10; i++) { 74*5c6c1daeSBarry Smith ierr = PetscDrawRectangle(popup,xl,yl,xr,yr,c,c,c,c);CHKERRQ(ierr); 75*5c6c1daeSBarry Smith yl += .1; yr += .1; c = (int)((double)c + (245. - PETSC_DRAW_BASIC_COLORS)/9.); 76*5c6c1daeSBarry Smith } 77*5c6c1daeSBarry Smith for (i=0; i<10; i++) { 78*5c6c1daeSBarry Smith value = min + i*(max-min)/9.0; 79*5c6c1daeSBarry Smith /* look for a value that should be zero, but is not due to round-off */ 80*5c6c1daeSBarry Smith if (PetscAbsReal(value) < 1.e-10 && max-min > 1.e-6) value = 0.0; 81*5c6c1daeSBarry Smith sprintf(string,"%18.16e",(double)value); 82*5c6c1daeSBarry Smith ierr = PetscDrawString(popup,.2,.02 + i/10.0,PETSC_DRAW_BLACK,string);CHKERRQ(ierr); 83*5c6c1daeSBarry Smith } 84*5c6c1daeSBarry Smith ierr = PetscDrawSetTitle(popup,"Contour Scale");CHKERRQ(ierr); 85*5c6c1daeSBarry Smith ierr = PetscDrawFlush(popup);CHKERRQ(ierr); 86*5c6c1daeSBarry Smith PetscFunctionReturn(0); 87*5c6c1daeSBarry Smith } 88*5c6c1daeSBarry Smith 89*5c6c1daeSBarry Smith typedef struct { 90*5c6c1daeSBarry Smith int m,n; 91*5c6c1daeSBarry Smith PetscReal *x,*y,min,max,*v; 92*5c6c1daeSBarry Smith PetscBool showgrid; 93*5c6c1daeSBarry Smith } ZoomCtx; 94*5c6c1daeSBarry Smith 95*5c6c1daeSBarry Smith #undef __FUNCT__ 96*5c6c1daeSBarry Smith #define __FUNCT__ "PetscDrawTensorContour_Zoom" 97*5c6c1daeSBarry Smith static PetscErrorCode PetscDrawTensorContour_Zoom(PetscDraw win,void *dctx) 98*5c6c1daeSBarry Smith { 99*5c6c1daeSBarry Smith PetscErrorCode ierr; 100*5c6c1daeSBarry Smith int i; 101*5c6c1daeSBarry Smith ZoomCtx *ctx = (ZoomCtx*)dctx; 102*5c6c1daeSBarry Smith 103*5c6c1daeSBarry Smith PetscFunctionBegin; 104*5c6c1daeSBarry Smith ierr = PetscDrawTensorContourPatch(win,ctx->m,ctx->n,ctx->x,ctx->y,ctx->max,ctx->min,ctx->v);CHKERRQ(ierr); 105*5c6c1daeSBarry Smith if (ctx->showgrid) { 106*5c6c1daeSBarry Smith for (i=0; i<ctx->m; i++) { 107*5c6c1daeSBarry Smith ierr = PetscDrawLine(win,ctx->x[i],ctx->y[0],ctx->x[i],ctx->y[ctx->n-1],PETSC_DRAW_BLACK);CHKERRQ(ierr); 108*5c6c1daeSBarry Smith } 109*5c6c1daeSBarry Smith for (i=0; i<ctx->n; i++) { 110*5c6c1daeSBarry Smith ierr = PetscDrawLine(win,ctx->x[0],ctx->y[i],ctx->x[ctx->m-1],ctx->y[i],PETSC_DRAW_BLACK);CHKERRQ(ierr); 111*5c6c1daeSBarry Smith } 112*5c6c1daeSBarry Smith } 113*5c6c1daeSBarry Smith PetscFunctionReturn(0); 114*5c6c1daeSBarry Smith } 115*5c6c1daeSBarry Smith 116*5c6c1daeSBarry Smith #undef __FUNCT__ 117*5c6c1daeSBarry Smith #define __FUNCT__ "PetscDrawTensorContour" 118*5c6c1daeSBarry Smith /*@C 119*5c6c1daeSBarry Smith PetscDrawTensorContour - PetscDraws a contour plot for a two-dimensional array 120*5c6c1daeSBarry Smith that is stored as a PETSc vector. 121*5c6c1daeSBarry Smith 122*5c6c1daeSBarry Smith Collective on PetscDraw, but PetscDraw must be sequential 123*5c6c1daeSBarry Smith 124*5c6c1daeSBarry Smith Input Parameters: 125*5c6c1daeSBarry Smith + win - the window to draw in 126*5c6c1daeSBarry Smith . m,n - the global number of mesh points in the x and y directions 127*5c6c1daeSBarry Smith . xi,yi - the locations of the global mesh points (optional, use PETSC_NULL 128*5c6c1daeSBarry Smith to indicate uniform spacing on [0,1]) 129*5c6c1daeSBarry Smith - V - the values 130*5c6c1daeSBarry Smith 131*5c6c1daeSBarry Smith Options Database Keys: 132*5c6c1daeSBarry Smith + -draw_x_shared_colormap - Indicates use of private colormap 133*5c6c1daeSBarry Smith - -draw_contour_grid - PetscDraws grid contour 134*5c6c1daeSBarry Smith 135*5c6c1daeSBarry Smith Level: intermediate 136*5c6c1daeSBarry Smith 137*5c6c1daeSBarry Smith Concepts: contour plot 138*5c6c1daeSBarry Smith Concepts: drawing^contour plot 139*5c6c1daeSBarry Smith 140*5c6c1daeSBarry Smith .seealso: PetscDrawTensorContourPatch() 141*5c6c1daeSBarry Smith 142*5c6c1daeSBarry Smith @*/ 143*5c6c1daeSBarry Smith PetscErrorCode PetscDrawTensorContour(PetscDraw win,int m,int n,const PetscReal xi[],const PetscReal yi[],PetscReal *v) 144*5c6c1daeSBarry Smith { 145*5c6c1daeSBarry Smith PetscErrorCode ierr; 146*5c6c1daeSBarry Smith int N = m*n; 147*5c6c1daeSBarry Smith PetscBool isnull; 148*5c6c1daeSBarry Smith PetscDraw popup; 149*5c6c1daeSBarry Smith MPI_Comm comm; 150*5c6c1daeSBarry Smith int xin=1,yin=1,i; 151*5c6c1daeSBarry Smith PetscMPIInt size; 152*5c6c1daeSBarry Smith PetscReal h; 153*5c6c1daeSBarry Smith ZoomCtx ctx; 154*5c6c1daeSBarry Smith 155*5c6c1daeSBarry Smith PetscFunctionBegin; 156*5c6c1daeSBarry Smith ierr = PetscDrawIsNull(win,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); 157*5c6c1daeSBarry Smith ierr = PetscObjectGetComm((PetscObject)win,&comm);CHKERRQ(ierr); 158*5c6c1daeSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 159*5c6c1daeSBarry Smith if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"May only be used with single processor PetscDraw"); 160*5c6c1daeSBarry Smith if (N <= 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"n %d and m %d must be positive",m,n); 161*5c6c1daeSBarry Smith 162*5c6c1daeSBarry Smith /* create scale window */ 163*5c6c1daeSBarry Smith ierr = PetscDrawGetPopup(win,&popup);CHKERRQ(ierr); 164*5c6c1daeSBarry Smith ierr = PetscDrawCheckResizedWindow(win);CHKERRQ(ierr); 165*5c6c1daeSBarry Smith 166*5c6c1daeSBarry Smith ctx.v = v; 167*5c6c1daeSBarry Smith ctx.m = m; 168*5c6c1daeSBarry Smith ctx.n = n; 169*5c6c1daeSBarry Smith ctx.max = ctx.min = v[0]; 170*5c6c1daeSBarry Smith for (i=0; i<N; i++) { 171*5c6c1daeSBarry Smith if (ctx.max < ctx.v[i]) ctx.max = ctx.v[i]; 172*5c6c1daeSBarry Smith if (ctx.min > ctx.v[i]) ctx.min = ctx.v[i]; 173*5c6c1daeSBarry Smith } 174*5c6c1daeSBarry Smith if (ctx.max - ctx.min < 1.e-7) {ctx.min -= 5.e-8; ctx.max += 5.e-8;} 175*5c6c1daeSBarry Smith 176*5c6c1daeSBarry Smith /* PetscDraw the scale window */ 177*5c6c1daeSBarry Smith if (popup) {ierr = PetscDrawScalePopup(popup,ctx.min,ctx.max);CHKERRQ(ierr);} 178*5c6c1daeSBarry Smith 179*5c6c1daeSBarry Smith ctx.showgrid = PETSC_FALSE; 180*5c6c1daeSBarry Smith ierr = PetscOptionsGetBool(PETSC_NULL,"-draw_contour_grid",&ctx.showgrid,PETSC_NULL);CHKERRQ(ierr); 181*5c6c1daeSBarry Smith 182*5c6c1daeSBarry Smith /* fill up x and y coordinates */ 183*5c6c1daeSBarry Smith if (!xi) { 184*5c6c1daeSBarry Smith xin = 0; 185*5c6c1daeSBarry Smith ierr = PetscMalloc(ctx.m*sizeof(PetscReal),&ctx.x);CHKERRQ(ierr); 186*5c6c1daeSBarry Smith h = 1.0/(ctx.m-1); 187*5c6c1daeSBarry Smith ctx.x[0] = 0.0; 188*5c6c1daeSBarry Smith for (i=1; i<ctx.m; i++) ctx.x[i] = ctx.x[i-1] + h; 189*5c6c1daeSBarry Smith } else { 190*5c6c1daeSBarry Smith ctx.x = (PetscReal*)xi; 191*5c6c1daeSBarry Smith } 192*5c6c1daeSBarry Smith if (!yi) { 193*5c6c1daeSBarry Smith yin = 0; 194*5c6c1daeSBarry Smith ierr = PetscMalloc(ctx.n*sizeof(PetscReal),&ctx.y);CHKERRQ(ierr); 195*5c6c1daeSBarry Smith h = 1.0/(ctx.n-1); 196*5c6c1daeSBarry Smith ctx.y[0] = 0.0; 197*5c6c1daeSBarry Smith for (i=1; i<ctx.n; i++) ctx.y[i] = ctx.y[i-1] + h; 198*5c6c1daeSBarry Smith } else { 199*5c6c1daeSBarry Smith ctx.y = (PetscReal *)yi; 200*5c6c1daeSBarry Smith } 201*5c6c1daeSBarry Smith 202*5c6c1daeSBarry Smith ierr = PetscDrawZoom(win,(PetscErrorCode (*)(PetscDraw,void *))PetscDrawTensorContour_Zoom,&ctx);CHKERRQ(ierr); 203*5c6c1daeSBarry Smith 204*5c6c1daeSBarry Smith if (!xin) {ierr = PetscFree(ctx.x);CHKERRQ(ierr);} 205*5c6c1daeSBarry Smith if (!yin) {ierr = PetscFree(ctx.y);CHKERRQ(ierr);} 206*5c6c1daeSBarry Smith 207*5c6c1daeSBarry Smith PetscFunctionReturn(0); 208*5c6c1daeSBarry Smith } 209*5c6c1daeSBarry Smith 210*5c6c1daeSBarry Smith #undef __FUNCT__ 211*5c6c1daeSBarry Smith #define __FUNCT__ "PetscDrawTensorContourPatch" 212*5c6c1daeSBarry Smith /*@ 213*5c6c1daeSBarry Smith PetscDrawTensorContourPatch - PetscDraws a rectangular patch of a contour plot 214*5c6c1daeSBarry Smith for a two-dimensional array. 215*5c6c1daeSBarry Smith 216*5c6c1daeSBarry Smith Not Collective 217*5c6c1daeSBarry Smith 218*5c6c1daeSBarry Smith Input Parameters: 219*5c6c1daeSBarry Smith + win - the window to draw in 220*5c6c1daeSBarry Smith . m,n - the number of local mesh points in the x and y direction 221*5c6c1daeSBarry Smith . x,y - the locations of the local mesh points 222*5c6c1daeSBarry Smith . max,min - the maximum and minimum value in the entire contour 223*5c6c1daeSBarry Smith - v - the data 224*5c6c1daeSBarry Smith 225*5c6c1daeSBarry Smith Options Database Keys: 226*5c6c1daeSBarry Smith . -draw_x_shared_colormap - Activates private colormap 227*5c6c1daeSBarry Smith 228*5c6c1daeSBarry Smith Level: advanced 229*5c6c1daeSBarry Smith 230*5c6c1daeSBarry Smith Note: 231*5c6c1daeSBarry Smith This is a lower level support routine, usually the user will call 232*5c6c1daeSBarry Smith PetscDrawTensorContour(). 233*5c6c1daeSBarry Smith 234*5c6c1daeSBarry Smith Concepts: contour plot 235*5c6c1daeSBarry Smith 236*5c6c1daeSBarry Smith .seealso: PetscDrawTensorContour() 237*5c6c1daeSBarry Smith 238*5c6c1daeSBarry Smith @*/ 239*5c6c1daeSBarry Smith PetscErrorCode PetscDrawTensorContourPatch(PetscDraw draw,int m,int n,PetscReal *x,PetscReal *y,PetscReal max,PetscReal min,PetscReal *v) 240*5c6c1daeSBarry Smith { 241*5c6c1daeSBarry Smith PetscErrorCode ierr; 242*5c6c1daeSBarry Smith int c1,c2,c3,c4,i,j; 243*5c6c1daeSBarry Smith PetscReal x1,x2,x3,x4,y_1,y2,y3,y4,scale; 244*5c6c1daeSBarry Smith 245*5c6c1daeSBarry Smith PetscFunctionBegin; 246*5c6c1daeSBarry Smith scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/(max - min); 247*5c6c1daeSBarry Smith 248*5c6c1daeSBarry Smith /* PetscDraw the contour plot patch */ 249*5c6c1daeSBarry Smith for (j=0; j<n-1; j++) { 250*5c6c1daeSBarry Smith for (i=0; i<m-1; i++) { 251*5c6c1daeSBarry Smith x1 = x[i]; y_1 = y[j]; c1 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m] - min)); 252*5c6c1daeSBarry Smith x2 = x[i+1];y2 = y_1; c2 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m+1]-min)); 253*5c6c1daeSBarry Smith x3 = x2; y3 = y[j+1];c3 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m+1+m]-min)); 254*5c6c1daeSBarry Smith x4 = x1; y4 = y3; c4 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m+m]-min)); 255*5c6c1daeSBarry Smith ierr = PetscDrawTriangle(draw,x1,y_1,x2,y2,x3,y3,c1,c2,c3);CHKERRQ(ierr); 256*5c6c1daeSBarry Smith ierr = PetscDrawTriangle(draw,x1,y_1,x3,y3,x4,y4,c1,c3,c4);CHKERRQ(ierr); 257*5c6c1daeSBarry Smith } 258*5c6c1daeSBarry Smith } 259*5c6c1daeSBarry Smith 260*5c6c1daeSBarry Smith PetscFunctionReturn(0); 261*5c6c1daeSBarry Smith } 262