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 - draws 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: `PetscDraw`, `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: {
 24:   PetscUseTypeMethod(draw, triangle, x1, y_1, x2, y2, x3, y3, c1, c2, c3);
 25:   return 0;
 26: }

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

 31:    Collective

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

 38:    Level: intermediate

 40:    Note:
 41:     All processors that share the draw MUST call this routine

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

 53:   if (!popup) return 0;
 55:   PetscDrawIsNull(popup, &isnull);
 56:   if (isnull) return 0;
 57:   MPI_Comm_rank(PetscObjectComm((PetscObject)popup), &rank);

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

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

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

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

103: /*@C
104:    PetscDrawTensorContour - draws a contour plot for a two-dimensional array

106:    Collective on draw, but draw must be sequential

108:    Input Parameters:
109: +   draw  - the draw context
110: .   m,n   - the global number of mesh points in the x and y directions
111: .   xi,yi - the locations of the global mesh points (optional, use NULL
112:             to indicate uniform spacing on [0,1])
113: -   V     - the values

115:    Options Database Keys:
116: +  -draw_x_shared_colormap - Indicates use of private colormap
117: -  -draw_contour_grid - draws grid contour

119:    Level: intermediate

121: .seealso: `PetscDraw`, `PetscDrawTensorContourPatch()`, `PetscDrawScalePopup()`
122: @*/
123: PetscErrorCode PetscDrawTensorContour(PetscDraw draw, int m, int n, const PetscReal xi[], const PetscReal yi[], PetscReal *v)
124: {
125:   int         N = m * n;
126:   PetscBool   isnull;
127:   PetscDraw   popup;
128:   int         xin = 1, yin = 1, i;
129:   PetscMPIInt size;
130:   PetscReal   h;
131:   ZoomCtx     ctx;

134:   PetscDrawIsNull(draw, &isnull);
135:   if (isnull) return 0;
136:   MPI_Comm_size(PetscObjectComm((PetscObject)draw), &size);

140:   ctx.v   = v;
141:   ctx.m   = m;
142:   ctx.n   = n;
143:   ctx.max = ctx.min = v[0];
144:   for (i = 0; i < N; i++) {
145:     if (ctx.max < ctx.v[i]) ctx.max = ctx.v[i];
146:     if (ctx.min > ctx.v[i]) ctx.min = ctx.v[i];
147:   }
148:   if (ctx.max - ctx.min < 1.e-7) {
149:     ctx.min -= 5.e-8;
150:     ctx.max += 5.e-8;
151:   }

153:   /* PetscDraw the scale window */
154:   PetscDrawGetPopup(draw, &popup);
155:   PetscDrawScalePopup(popup, ctx.min, ctx.max);

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

160:   /* fill up x and y coordinates */
161:   if (!xi) {
162:     xin = 0;
163:     PetscMalloc1(ctx.m, &ctx.x);
164:     h        = 1.0 / (ctx.m - 1);
165:     ctx.x[0] = 0.0;
166:     for (i = 1; i < ctx.m; i++) ctx.x[i] = ctx.x[i - 1] + h;
167:   } else ctx.x = (PetscReal *)xi;

169:   if (!yi) {
170:     yin = 0;
171:     PetscMalloc1(ctx.n, &ctx.y);
172:     h        = 1.0 / (ctx.n - 1);
173:     ctx.y[0] = 0.0;
174:     for (i = 1; i < ctx.n; i++) ctx.y[i] = ctx.y[i - 1] + h;
175:   } else ctx.y = (PetscReal *)yi;

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

179:   if (!xin) PetscFree(ctx.x);
180:   if (!yin) PetscFree(ctx.y);
181:   return 0;
182: }

184: /*@
185:    PetscDrawTensorContourPatch - draws a rectangular patch of a contour plot
186:    for a two-dimensional array.

188:    Not Collective

190:    Input Parameters:
191: +  draw - the draw context
192: .  m,n - the number of local mesh points in the x and y direction
193: .  x,y - the locations of the local mesh points
194: .  min,max - the minimum and maximum value in the entire contour
195: -  v - the data

197:    Options Database Keys:
198: .  -draw_x_shared_colormap - Activates private colormap

200:    Level: advanced

202:    Note:
203:    This is a lower level support routine, usually the user will call
204:    `PetscDrawTensorContour()`.

206: .seealso: `PetscDraw`, `PetscDrawTensorContour()`
207: @*/
208: PetscErrorCode PetscDrawTensorContourPatch(PetscDraw draw, int m, int n, PetscReal *x, PetscReal *y, PetscReal min, PetscReal max, PetscReal *v)
209: {
210:   int       c1, c2, c3, c4, i, j;
211:   PetscReal x1, x2, x3, x4, y_1, y2, y3, y4;

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

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