Actual source code: dtri.c

  1: /*$Id: dtri.c,v 1.49 2001/03/23 23:20:08 balay Exp $*/
  2: /*
  3:        Provides the calling sequences for all the basic PetscDraw routines.
  4: */
  5: #include "src/sys/src/draw/drawimpl.h"  /*I "petscdraw.h" I*/

  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 (PetscAbsDouble(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;
 87:   Scalar     *v;
 88:   PetscTruth showgrid;
 89: } ZoomCtx;

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

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

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

113:    Collective on PetscDraw, but PetscDraw must be sequential

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

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

126:    Level: intermediate

128:    Concepts: contour plot
129:    Concepts: drawing^contour plot

131: .seealso: PetscDrawTensorContourPatch()

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

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

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

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

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

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

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

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

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

198:   return(0);
199: }

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

205:    Not Collective

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

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

217:    Level: advanced

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

223:    Concepts: contour plot

225: .seealso: PetscDrawTensorContour()

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

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

236:   /* PetscDraw the contour plot patch */
237:   for (j=0; j<n-1; j++) {
238:     for (i=0; i<m-1; i++) {
239: #if !defined(PETSC_USE_COMPLEX)
240:       x1 = x[i];  y_1 = y[j];  c1 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m] - min));
241:       x2 = x[i+1];y2 = y_1;    c2 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m+1]-min));
242:       x3 = x2;    y3 = y[j+1];c3 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m+1+m]-min));
243:       x4 = x1;    y4 = y3;    c4 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m+m]-min));
244: #else
245:       x1 = x[i];  y_1 = y[j];  c1 = (int)(PETSC_DRAW_BASIC_COLORS + scale*PetscRealPart(v[i+j*m]-min));
246:       x2 = x[i+1];y2 = y_1;    c2 = (int)(PETSC_DRAW_BASIC_COLORS + scale*PetscRealPart(v[i+j*m+1]-min));
247:       x3 = x2;    y3 = y[j+1];c3 = (int)(PETSC_DRAW_BASIC_COLORS + scale*PetscRealPart(v[i+j*m+1+m]-min));
248:       x4 = x1;    y4 = y3;    c4 = (int)(PETSC_DRAW_BASIC_COLORS + scale*PetscRealPart(v[i+j*m+m]-min));
249: #endif
250:       PetscDrawTriangle(draw,x1,y_1,x2,y2,x3,y3,c1,c2,c3);
251:       PetscDrawTriangle(draw,x1,y_1,x3,y3,x4,y4,c1,c3,c4);
252:     }
253:   }

255:   return(0);
256: }