Actual source code: dtri.c

  1: /*$Id: dtri.c,v 1.53 2001/08/22 20:28:40 bsmith Exp $*/
  2: /*
  3:        Provides the calling sequences for all the basic PetscDraw routines.
  4: */
 5:  #include src/sys/src/draw/drawimpl.h

  7: #undef __FUNCT__  
  9: /*@
 10:    PetscDrawTriangle - PetscDraws a triangle  onto a drawable.

 12:    Not Collective

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

 19:    Level: beginner

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

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

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


 41: #undef __FUNCT__  
 43: /*@
 44:        PetscDrawScalePopup - PetscDraws a contour scale window. 

 46:      Collective on PetscDraw

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

 53:   Level: intermediate

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

 58: @*/
 59: int PetscDrawScalePopup(PetscDraw popup,PetscReal min,PetscReal max)
 60: {
 61:   PetscReal xl = 0.0,yl = 0.0,xr = 1.0,yr = 1.0,value;
 62:   int       i,c = PETSC_DRAW_BASIC_COLORS,rank,ierr;
 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;

 94: #undef __FUNCT__  
 96: static int PetscDrawTensorContour_Zoom(PetscDraw win,void *dctx)
 97: {
 98:   int     i,ierr;
 99:   ZoomCtx *ctx = (ZoomCtx*)dctx;

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

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

120:    Collective on PetscDraw, but PetscDraw must be sequential

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

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

133:    Level: intermediate

135:    Concepts: contour plot
136:    Concepts: drawing^contour plot

138: .seealso: PetscDrawTensorContourPatch()

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

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

157:   if (N <= 0) {
158:     SETERRQ2(1,"n %d and m %d must be positive",m,n);
159:   }

161:   /* create scale window */
162:   PetscDrawGetPopup(win,&popup);
163:   PetscDrawCheckResizedWindow(win);

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

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

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

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

200:   PetscDrawZoom(win,(int (*)(PetscDraw,void *))PetscDrawTensorContour_Zoom,&ctx);
201: 
202:   if (!xin) {PetscFree(ctx.x);}
203:   if (!yin) {PetscFree(ctx.y);}

205:   return(0);
206: }

208: #undef __FUNCT__  
210: /*@
211:    PetscDrawTensorContourPatch - PetscDraws a rectangular patch of a contour plot 
212:    for a two-dimensional array.

214:    Not Collective

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

223:    Options Database Keys:
224: .  -draw_x_shared_colormap - Activates private colormap

226:    Level: advanced

228:    Note: 
229:    This is a lower level support routine, usually the user will call
230:    PetscDrawTensorContour(). 

232:    Concepts: contour plot

234: .seealso: PetscDrawTensorContour()

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

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

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

257:   return(0);
258: }