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: /*@
  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:    Concepts: drawing^triangle
 20:    Concepts: graphics^triangle
 21:    Concepts: triangle

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

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


 39: /*@
 40:        PetscDrawScalePopup - PetscDraws a contour scale window. 

 42:      Collective on PetscDraw

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

 49:   Level: intermediate

 51:   Notes:
 52:      All processors that share the draw MUST call this routine

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

 63:   PetscDrawCheckResizedWindow(popup);
 64:   PetscObjectGetComm((PetscObject)popup,&comm);
 65:   MPI_Comm_rank(comm,&rank);
 66:   if (rank) return(0);

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

 84: typedef struct {
 85:   int        m,n;
 86:   PetscReal  *x,*y,min,max,*v;
 87:   PetscTruth showgrid;
 88: } ZoomCtx;

 90: static int PetscDrawTensorContour_Zoom(PetscDraw win,void *dctx)
 91: {
 92:   int     i,ierr;
 93:   ZoomCtx *ctx = (ZoomCtx*)dctx;

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

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

112:    Collective on PetscDraw, but PetscDraw must be sequential

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

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

125:    Level: intermediate

127:    Concepts: contour plot
128:    Concepts: drawing^contour plot

130: .seealso: PetscDrawTensorContourPatch()

132: @*/
133: int PetscDrawTensorContour(PetscDraw win,int m,int n,const PetscReal xi[],const PetscReal yi[],PetscReal *v)
134: {
135:   int           N = m*n,ierr;
136:   PetscTruth    isnull;
137:   PetscDraw     popup;
138:   MPI_Comm      comm;
139:   int           xin=1,yin=1,i,size;
140:   PetscReal     h;
141:   ZoomCtx       ctx;

144:   PetscDrawIsNull(win,&isnull); if (isnull) return(0);
145:   PetscObjectGetComm((PetscObject)win,&comm);
146:   MPI_Comm_size(comm,&size);
147:   if (size > 1) SETERRQ(1,"May only be used with single processor PetscDraw");

149:   if (N <= 0) {
150:     SETERRQ2(1,"n %d and m %d must be positive",m,n);
151:   }

153:   /* create scale window */
154:   PetscDrawGetPopup(win,&popup);
155:   PetscDrawCheckResizedWindow(win);

157:   ctx.v   = v;
158:   ctx.m   = m;
159:   ctx.n   = n;
160:   ctx.max = ctx.min = v[0];
161:   for (i=0; i<N; i++) {
162:     if (ctx.max < ctx.v[i]) ctx.max = ctx.v[i];
163:     if (ctx.min > ctx.v[i]) ctx.min = ctx.v[i];
164:   }
165:   if (ctx.max - ctx.min < 1.e-7) {ctx.min -= 5.e-8; ctx.max += 5.e-8;}

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

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

172:   /* fill up x and y coordinates */
173:   if (!xi) {
174:     xin      = 0;
175:     ierr     = PetscMalloc(ctx.m*sizeof(PetscReal),&ctx.x);
176:     h        = 1.0/(ctx.m-1);
177:     ctx.x[0] = 0.0;
178:     for (i=1; i<ctx.m; i++) ctx.x[i] = ctx.x[i-1] + h;
179:   } else {
180:     ctx.x = (PetscReal*)xi;
181:   }
182:   if (!yi) {
183:     yin      = 0;
184:     ierr     = PetscMalloc(ctx.n*sizeof(PetscReal),&ctx.y);
185:     h        = 1.0/(ctx.n-1);
186:     ctx.y[0] = 0.0;
187:     for (i=1; i<ctx.n; i++) ctx.y[i] = ctx.y[i-1] + h;
188:   } else {
189:     ctx.y = (PetscReal *)yi;
190:   }

192:   PetscDrawZoom(win,(int (*)(PetscDraw,void *))PetscDrawTensorContour_Zoom,&ctx);
193: 
194:   if (!xin) {PetscFree(ctx.x);}
195:   if (!yin) {PetscFree(ctx.y);}

197:   return(0);
198: }

200: /*@
201:    PetscDrawTensorContourPatch - PetscDraws a rectangular patch of a contour plot 
202:    for a two-dimensional array.

204:    Not Collective

206:    Input Parameters:
207: +  win - the window to draw in
208: .  m,n - the number of local mesh points in the x and y direction
209: .  x,y - the locations of the local mesh points
210: .  max,min - the maximum and minimum value in the entire contour
211: -  v - the data

213:    Options Database Keys:
214: .  -draw_x_shared_colormap - Activates private colormap

216:    Level: advanced

218:    Note: 
219:    This is a lower level support routine, usually the user will call
220:    PetscDrawTensorContour(). 

222:    Concepts: contour plot

224: .seealso: PetscDrawTensorContour()

226: @*/
227: int PetscDrawTensorContourPatch(PetscDraw draw,int m,int n,PetscReal *x,PetscReal *y,PetscReal max,PetscReal min,PetscReal *v)
228: {
229:   int           c1,c2,c3,c4,i,j,ierr;
230:   PetscReal     x1,x2,x3,x4,y_1,y2,y3,y4,scale;

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

235:   /* PetscDraw the contour plot patch */
236:   for (j=0; j<n-1; j++) {
237:     for (i=0; i<m-1; i++) {
238:       x1 = x[i];  y_1 = y[j];  c1 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m] - min));
239:       x2 = x[i+1];y2 = y_1;    c2 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m+1]-min));
240:       x3 = x2;    y3 = y[j+1];c3 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m+1+m]-min));
241:       x4 = x1;    y4 = y3;    c4 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m+m]-min));
242:       PetscDrawTriangle(draw,x1,y_1,x2,y2,x3,y3,c1,c2,c3);
243:       PetscDrawTriangle(draw,x1,y_1,x3,y3,x4,y4,c1,c3,c4);
244:     }
245:   }

247:   return(0);
248: }