Actual source code: dtri.c


  2: /*
  3:        Provides the calling sequences for all the basic PetscDraw routines.
  4: */
  5: #include <petsc/private/drawimpl.h>

  7: /*@
  8:    PetscDrawTriangle - PetscDraws a triangle  onto a drawable.

 10:    Not Collective

 12:    Input Parameters:
 13: +  draw - the drawing context
 14: .  x1,y1,x2,y2,x3,y3 - the coordinates of the vertices
 15: -  c1,c2,c3 - the colors of the three vertices in the same order as the xi,yi

 17:    Level: beginner

 19: .seealso: PetscDrawLine(), PetscDrawRectangle(), PetscDrawEllipse(), PetscDrawMarker(), PetscDrawPoint(), PetscDrawArrow()
 20: @*/
 21: PetscErrorCode  PetscDrawTriangle(PetscDraw draw,PetscReal x1,PetscReal y_1,PetscReal x2,PetscReal y2,PetscReal x3,PetscReal y3,int c1,int c2,int c3)
 22: {
 25:   (*draw->ops->triangle)(draw,x1,y_1,x2,y2,x3,y3,c1,c2,c3);
 26:   return 0;
 27: }

 29: /*@
 30:    PetscDrawScalePopup - draws a contour scale window.

 32:    Collective on PetscDraw

 34:    Input Parameters:
 35: +  popup - the window (often a window obtained via PetscDrawGetPopup()
 36: .  min - minimum value being plotted
 37: -  max - maximum value being plotted

 39:    Level: intermediate

 41:    Notes:
 42:     All processors that share the draw MUST call this routine

 44: .seealso: PetscDrawGetPopup(), PetscDrawTensorContour()

 46: @*/
 47: PetscErrorCode  PetscDrawScalePopup(PetscDraw popup,PetscReal min,PetscReal max)
 48: {
 49:   PetscBool      isnull;
 50:   PetscReal      xl = 0.0,yl = 0.0,xr = 1.0,yr = 1.0;
 51:   PetscMPIInt    rank;
 52:   int            i;
 53:   char           string[32];

 56:   if (!popup) return 0;
 58:   PetscDrawIsNull(popup,&isnull);
 59:   if (isnull) return 0;
 60:   MPI_Comm_rank(PetscObjectComm((PetscObject)popup),&rank);

 62:   PetscDrawCheckResizedWindow(popup);
 63:   PetscDrawClear(popup);
 64:   PetscDrawSetTitle(popup,"Contour Scale");
 65:   PetscDrawSetCoordinates(popup,xl,yl,xr,yr);
 66:   PetscDrawCollectiveBegin(popup);
 67:   if (rank == 0) {
 68:     for (i=0; i<10; i++) {
 69:       int c = PetscDrawRealToColor((PetscReal)i/9,0,1);
 70:       PetscDrawRectangle(popup,xl,yl,xr,yr,c,c,c,c);
 71:       yl += 0.1;
 72:     }
 73:     for (i=0; i<10; i++) {
 74:       PetscReal value = min + i*(max-min)/9;
 75:       /* look for a value that should be zero, but is not due to round-off */
 76:       if (PetscAbsReal(value) < 1.e-10 && max-min > 1.e-6) value = 0.0;
 77:       PetscSNPrintf(string,sizeof(string),"%18.16e",(double)value);
 78:       PetscDrawString(popup,0.2,0.02+i/10.0,PETSC_DRAW_BLACK,string);
 79:     }
 80:   }
 81:   PetscDrawCollectiveEnd(popup);
 82:   PetscDrawFlush(popup);
 83:   PetscDrawSave(popup);
 84:   return 0;
 85: }

 87: typedef struct {
 88:   int       m,n;
 89:   PetscReal *x,*y,min,max,*v;
 90:   PetscBool showgrid;
 91: } ZoomCtx;

 93: static PetscErrorCode PetscDrawTensorContour_Zoom(PetscDraw win,void *dctx)
 94: {
 95:   int            i;
 96:   ZoomCtx        *ctx = (ZoomCtx*)dctx;

 98:   PetscDrawTensorContourPatch(win,ctx->m,ctx->n,ctx->x,ctx->y,ctx->min,ctx->max,ctx->v);
 99:   if (ctx->showgrid) {
100:     for (i=0; i<ctx->m; i++) {
101:       PetscDrawLine(win,ctx->x[i],ctx->y[0],ctx->x[i],ctx->y[ctx->n-1],PETSC_DRAW_BLACK);
102:     }
103:     for (i=0; i<ctx->n; i++) {
104:       PetscDrawLine(win,ctx->x[0],ctx->y[i],ctx->x[ctx->m-1],ctx->y[i],PETSC_DRAW_BLACK);
105:     }
106:   }
107:   return 0;
108: }

110: /*@C
111:    PetscDrawTensorContour - PetscDraws a contour plot for a two-dimensional array
112:    that is stored as a PETSc vector.

114:    Collective on PetscDraw, but PetscDraw must be sequential

116:    Input Parameters:
117: +   draw  - the draw context
118: .   m,n   - the global number of mesh points in the x and y directions
119: .   xi,yi - the locations of the global mesh points (optional, use NULL
120:             to indicate uniform spacing on [0,1])
121: -   V     - the values

123:    Options Database Keys:
124: +  -draw_x_shared_colormap - Indicates use of private colormap
125: -  -draw_contour_grid - PetscDraws grid contour

127:    Level: intermediate

129: .seealso: PetscDrawTensorContourPatch(), PetscDrawScalePopup()

131: @*/
132: PetscErrorCode  PetscDrawTensorContour(PetscDraw draw,int m,int n,const PetscReal xi[],const PetscReal yi[],PetscReal *v)
133: {
134:   int            N = m*n;
135:   PetscBool      isnull;
136:   PetscDraw      popup;
137:   int            xin=1,yin=1,i;
138:   PetscMPIInt    size;
139:   PetscReal      h;
140:   ZoomCtx        ctx;

143:   PetscDrawIsNull(draw,&isnull);
144:   if (isnull) return 0;
145:   MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);

149:   ctx.v   = v;
150:   ctx.m   = m;
151:   ctx.n   = n;
152:   ctx.max = ctx.min = v[0];
153:   for (i=0; i<N; i++) {
154:     if (ctx.max < ctx.v[i]) ctx.max = ctx.v[i];
155:     if (ctx.min > ctx.v[i]) ctx.min = ctx.v[i];
156:   }
157:   if (ctx.max - ctx.min < 1.e-7) {ctx.min -= 5.e-8; ctx.max += 5.e-8;}

159:   /* PetscDraw the scale window */
160:   PetscDrawGetPopup(draw,&popup);
161:   PetscDrawScalePopup(popup,ctx.min,ctx.max);

163:   ctx.showgrid = PETSC_FALSE;
164:   PetscOptionsGetBool(((PetscObject)draw)->options,NULL,"-draw_contour_grid",&ctx.showgrid,NULL);

166:   /* fill up x and y coordinates */
167:   if (!xi) {
168:     xin      = 0;
169:     PetscMalloc1(ctx.m,&ctx.x);
170:     h        = 1.0/(ctx.m-1);
171:     ctx.x[0] = 0.0;
172:     for (i=1; i<ctx.m; i++) ctx.x[i] = ctx.x[i-1] + h;
173:   } else ctx.x = (PetscReal*)xi;

175:   if (!yi) {
176:     yin      = 0;
177:     PetscMalloc1(ctx.n,&ctx.y);
178:     h        = 1.0/(ctx.n-1);
179:     ctx.y[0] = 0.0;
180:     for (i=1; i<ctx.n; i++) ctx.y[i] = ctx.y[i-1] + h;
181:   } else ctx.y = (PetscReal*)yi;

183:   PetscDrawZoom(draw,PetscDrawTensorContour_Zoom,&ctx);

185:   if (!xin) PetscFree(ctx.x);
186:   if (!yin) PetscFree(ctx.y);
187:   return 0;
188: }

190: /*@
191:    PetscDrawTensorContourPatch - PetscDraws a rectangular patch of a contour plot
192:    for a two-dimensional array.

194:    Not Collective

196:    Input Parameters:
197: +  draw - the draw context
198: .  m,n - the number of local mesh points in the x and y direction
199: .  x,y - the locations of the local mesh points
200: .  min,max - the minimum and maximum value in the entire contour
201: -  v - the data

203:    Options Database Keys:
204: .  -draw_x_shared_colormap - Activates private colormap

206:    Level: advanced

208:    Note:
209:    This is a lower level support routine, usually the user will call
210:    PetscDrawTensorContour().

212: .seealso: PetscDrawTensorContour()

214: @*/
215: PetscErrorCode  PetscDrawTensorContourPatch(PetscDraw draw,int m,int n,PetscReal *x,PetscReal *y,PetscReal min,PetscReal max,PetscReal *v)
216: {
217:   int            c1,c2,c3,c4,i,j;
218:   PetscReal      x1,x2,x3,x4,y_1,y2,y3,y4;

221:   /* PetscDraw the contour plot patch */
222:   for (j=0; j<n-1; j++) {
223:     for (i=0; i<m-1; i++) {
224:       x1 = x[i];   y_1 = y[j];  c1 = PetscDrawRealToColor(v[i+j*m],min,max);
225:       x2 = x[i+1]; y2 = y_1;    c2 = PetscDrawRealToColor(v[i+j*m+1],min,max);
226:       x3 = x2;     y3 = y[j+1]; c3 = PetscDrawRealToColor(v[i+j*m+1+m],min,max);
227:       x4 = x1;     y4 = y3;     c4 = PetscDrawRealToColor(v[i+j*m+m],min,max);

229:       PetscDrawTriangle(draw,x1,y_1,x2,y2,x3,y3,c1,c2,c3);
230:       PetscDrawTriangle(draw,x1,y_1,x3,y3,x4,y4,c1,c3,c4);
231:     }
232:   }
233:   return 0;
234: }