Actual source code: dtri.c

  1: /*
  2:        Provides the calling sequences for all the basic PetscDraw routines.
  3: */
 4:  #include src/sys/src/draw/drawimpl.h

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

 11:    Not Collective

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

 18:    Level: beginner

 20:    Concepts: drawing^triangle
 21:    Concepts: graphics^triangle
 22:    Concepts: triangle

 24: @*/
 25: PetscErrorCode PetscDrawTriangle(PetscDraw draw,PetscReal x1,PetscReal y_1,PetscReal x2,PetscReal y2,PetscReal x3,PetscReal y3,
 26:                  int c1,int c2,int c3)
 27: {
 29:   PetscTruth isnull;

 33:   PetscTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);
 34:   if (isnull) return(0);
 35:   (*draw->ops->triangle)(draw,x1,y_1,x2,y2,x3,y3,c1,c2,c3);
 36:   return(0);
 37: }


 42: /*@
 43:        PetscDrawScalePopup - PetscDraws a contour scale window. 

 45:      Collective on PetscDraw

 47:   Input Parameters:
 48: +    popup - the window (often a window obtained via PetscDrawGetPopup()
 49: .    min - minimum value being plotted
 50: -    max - maximum value being plotted

 52:   Level: intermediate

 54:   Notes:
 55:      All processors that share the draw MUST call this routine

 57: @*/
 58: PetscErrorCode PetscDrawScalePopup(PetscDraw popup,PetscReal min,PetscReal max)
 59: {
 60:   PetscReal xl = 0.0,yl = 0.0,xr = 1.0,yr = 1.0,value;
 62:   int       i,c = PETSC_DRAW_BASIC_COLORS,rank;
 63:   char      string[32];
 64:   MPI_Comm  comm;

 67:   PetscDrawCheckResizedWindow(popup);
 68:   PetscObjectGetComm((PetscObject)popup,&comm);
 69:   MPI_Comm_rank(comm,&rank);
 70:   if (rank) return(0);

 72:   for (i=0; i<10; i++) {
 73:     PetscDrawRectangle(popup,xl,yl,xr,yr,c,c,c,c);
 74:     yl += .1; yr += .1; c = (int)((double)c + (245. - PETSC_DRAW_BASIC_COLORS)/9.);
 75:   }
 76:   for (i=0; i<10; i++) {
 77:     value = min + i*(max-min)/9.0;
 78:     /* look for a value that should be zero, but is not due to round-off */
 79:     if (PetscAbsReal(value) < 1.e-10 && max-min > 1.e-6) value = 0.0;
 80:     sprintf(string,"%g",value);
 81:     PetscDrawString(popup,.2,.02 + i/10.0,PETSC_DRAW_BLACK,string);
 82:   }
 83:   PetscDrawSetTitle(popup,"Contour Scale");
 84:   PetscDrawFlush(popup);
 85:   return(0);
 86: }

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

 96: static PetscErrorCode PetscDrawTensorContour_Zoom(PetscDraw win,void *dctx)
 97: {
 99:   int     i;
100:   ZoomCtx *ctx = (ZoomCtx*)dctx;

103:   PetscDrawTensorContourPatch(win,ctx->m,ctx->n,ctx->x,ctx->y,ctx->max,ctx->min,ctx->v);
104:   if (ctx->showgrid) {
105:     for (i=0; i<ctx->m; i++) {
106:       PetscDrawLine(win,ctx->x[i],ctx->y[0],ctx->x[i],ctx->y[ctx->n-1],PETSC_DRAW_BLACK);
107:     }
108:     for (i=0; i<ctx->n; i++) {
109:       PetscDrawLine(win,ctx->x[0],ctx->y[i],ctx->x[ctx->m-1],ctx->y[i],PETSC_DRAW_BLACK);
110:     }
111:   }
112:   return(0);
113: }

117: /*@C
118:    PetscDrawTensorContour - PetscDraws a contour plot for a two-dimensional array
119:    that is stored as a PETSc vector.

121:    Collective on PetscDraw, but PetscDraw must be sequential

123:    Input Parameters:
124: +   win   - the window to draw in
125: .   m,n   - the global number of mesh points in the x and y directions
126: .   xi,yi - the locations of the global mesh points (optional, use PETSC_NULL
127:             to indicate uniform spacing on [0,1])
128: -   V     - the values

130:    Options Database Keys:
131: +  -draw_x_shared_colormap - Indicates use of private colormap
132: -  -draw_contour_grid - PetscDraws grid contour

134:    Level: intermediate

136:    Concepts: contour plot
137:    Concepts: drawing^contour plot

139: .seealso: PetscDrawTensorContourPatch()

141: @*/
142: PetscErrorCode PetscDrawTensorContour(PetscDraw win,int m,int n,const PetscReal xi[],const PetscReal yi[],PetscReal *v)
143: {
145:   int           N = m*n;
146:   PetscTruth    isnull;
147:   PetscDraw     popup;
148:   MPI_Comm      comm;
149:   int           xin=1,yin=1,i;
150:   PetscMPIInt   size;
151:   PetscReal     h;
152:   ZoomCtx       ctx;

155:   PetscDrawIsNull(win,&isnull); if (isnull) return(0);
156:   PetscObjectGetComm((PetscObject)win,&comm);
157:   MPI_Comm_size(comm,&size);
158:   if (size > 1) SETERRQ(PETSC_ERR_ARG_WRONG,"May only be used with single processor PetscDraw");

160:   if (N <= 0) {
161:     SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"n %d and m %d must be positive",m,n);
162:   }

164:   /* create scale window */
165:   PetscDrawGetPopup(win,&popup);
166:   PetscDrawCheckResizedWindow(win);

168:   ctx.v   = v;
169:   ctx.m   = m;
170:   ctx.n   = n;
171:   ctx.max = ctx.min = v[0];
172:   for (i=0; i<N; i++) {
173:     if (ctx.max < ctx.v[i]) ctx.max = ctx.v[i];
174:     if (ctx.min > ctx.v[i]) ctx.min = ctx.v[i];
175:   }
176:   if (ctx.max - ctx.min < 1.e-7) {ctx.min -= 5.e-8; ctx.max += 5.e-8;}

178:   /* PetscDraw the scale window */
179:   if (popup) {PetscDrawScalePopup(popup,ctx.min,ctx.max);}

181:   PetscOptionsHasName(PETSC_NULL,"-draw_contour_grid",&ctx.showgrid);

183:   /* fill up x and y coordinates */
184:   if (!xi) {
185:     xin      = 0;
186:     PetscMalloc(ctx.m*sizeof(PetscReal),&ctx.x);
187:     h        = 1.0/(ctx.m-1);
188:     ctx.x[0] = 0.0;
189:     for (i=1; i<ctx.m; i++) ctx.x[i] = ctx.x[i-1] + h;
190:   } else {
191:     ctx.x = (PetscReal*)xi;
192:   }
193:   if (!yi) {
194:     yin      = 0;
195:     PetscMalloc(ctx.n*sizeof(PetscReal),&ctx.y);
196:     h        = 1.0/(ctx.n-1);
197:     ctx.y[0] = 0.0;
198:     for (i=1; i<ctx.n; i++) ctx.y[i] = ctx.y[i-1] + h;
199:   } else {
200:     ctx.y = (PetscReal *)yi;
201:   }

203:   PetscDrawZoom(win,(PetscErrorCode (*)(PetscDraw,void *))PetscDrawTensorContour_Zoom,&ctx);
204: 
205:   if (!xin) {PetscFree(ctx.x);}
206:   if (!yin) {PetscFree(ctx.y);}

208:   return(0);
209: }

213: /*@
214:    PetscDrawTensorContourPatch - PetscDraws a rectangular patch of a contour plot 
215:    for a two-dimensional array.

217:    Not Collective

219:    Input Parameters:
220: +  win - the window to draw in
221: .  m,n - the number of local mesh points in the x and y direction
222: .  x,y - the locations of the local mesh points
223: .  max,min - the maximum and minimum value in the entire contour
224: -  v - the data

226:    Options Database Keys:
227: .  -draw_x_shared_colormap - Activates private colormap

229:    Level: advanced

231:    Note: 
232:    This is a lower level support routine, usually the user will call
233:    PetscDrawTensorContour(). 

235:    Concepts: contour plot

237: .seealso: PetscDrawTensorContour()

239: @*/
240: PetscErrorCode PetscDrawTensorContourPatch(PetscDraw draw,int m,int n,PetscReal *x,PetscReal *y,PetscReal max,PetscReal min,PetscReal *v)
241: {
243:   int           c1,c2,c3,c4,i,j;
244:   PetscReal     x1,x2,x3,x4,y_1,y2,y3,y4,scale;

247:   scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/(max - min);

249:   /* PetscDraw the contour plot patch */
250:   for (j=0; j<n-1; j++) {
251:     for (i=0; i<m-1; i++) {
252:       x1 = x[i];  y_1 = y[j];  c1 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m] - min));
253:       x2 = x[i+1];y2 = y_1;    c2 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m+1]-min));
254:       x3 = x2;    y3 = y[j+1];c3 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m+1+m]-min));
255:       x4 = x1;    y4 = y3;    c4 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m+m]-min));
256:       PetscDrawTriangle(draw,x1,y_1,x2,y2,x3,y3,c1,c2,c3);
257:       PetscDrawTriangle(draw,x1,y_1,x3,y3,x4,y4,c1,c3,c4);
258:     }
259:   }

261:   return(0);
262: }