Actual source code: dtri.c
petsc-3.14.6 2021-03-30
2: /*
3: Provides the calling sequences for all the basic PetscDraw routines.
4: */
5: #include <petsc/private/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: .seealso: 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: {
27: if (!draw->ops->triangle) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"This draw type %s does not support drawing triangles",((PetscObject)draw)->type_name);
28: (*draw->ops->triangle)(draw,x1,y_1,x2,y2,x3,y3,c1,c2,c3);
29: return(0);
30: }
32: /*@
33: PetscDrawScalePopup - draws a contour scale window.
35: Collective on PetscDraw
37: Input Parameters:
38: + popup - the window (often a window obtained via PetscDrawGetPopup()
39: . min - minimum value being plotted
40: - max - maximum value being plotted
42: Level: intermediate
44: Notes:
45: All processors that share the draw MUST call this routine
47: .seealso: PetscDrawGetPopup(), PetscDrawTensorContour()
49: @*/
50: PetscErrorCode PetscDrawScalePopup(PetscDraw popup,PetscReal min,PetscReal max)
51: {
52: PetscBool isnull;
53: PetscReal xl = 0.0,yl = 0.0,xr = 1.0,yr = 1.0;
54: PetscMPIInt rank;
56: int i;
57: char string[32];
60: if (!popup) return(0);
62: PetscDrawIsNull(popup,&isnull);
63: if (isnull) return(0);
64: MPI_Comm_rank(PetscObjectComm((PetscObject)popup),&rank);
66: PetscDrawCheckResizedWindow(popup);
67: PetscDrawClear(popup);
68: PetscDrawSetTitle(popup,"Contour Scale");
69: PetscDrawSetCoordinates(popup,xl,yl,xr,yr);
70: PetscDrawCollectiveBegin(popup);
71: if (!rank) {
72: for (i=0; i<10; i++) {
73: int c = PetscDrawRealToColor((PetscReal)i/9,0,1);
74: PetscDrawRectangle(popup,xl,yl,xr,yr,c,c,c,c);
75: yl += 0.1;
76: }
77: for (i=0; i<10; i++) {
78: PetscReal value = min + i*(max-min)/9;
79: /* look for a value that should be zero, but is not due to round-off */
80: if (PetscAbsReal(value) < 1.e-10 && max-min > 1.e-6) value = 0.0;
81: PetscSNPrintf(string,sizeof(string),"%18.16e",(double)value);
82: PetscDrawString(popup,0.2,0.02+i/10.0,PETSC_DRAW_BLACK,string);
83: }
84: }
85: PetscDrawCollectiveEnd(popup);
86: PetscDrawFlush(popup);
87: PetscDrawSave(popup);
88: return(0);
89: }
91: typedef struct {
92: int m,n;
93: PetscReal *x,*y,min,max,*v;
94: PetscBool showgrid;
95: } ZoomCtx;
97: static PetscErrorCode PetscDrawTensorContour_Zoom(PetscDraw win,void *dctx)
98: {
100: int i;
101: ZoomCtx *ctx = (ZoomCtx*)dctx;
104: PetscDrawTensorContourPatch(win,ctx->m,ctx->n,ctx->x,ctx->y,ctx->min,ctx->max,ctx->v);
105: if (ctx->showgrid) {
106: for (i=0; i<ctx->m; i++) {
107: PetscDrawLine(win,ctx->x[i],ctx->y[0],ctx->x[i],ctx->y[ctx->n-1],PETSC_DRAW_BLACK);
108: }
109: for (i=0; i<ctx->n; i++) {
110: PetscDrawLine(win,ctx->x[0],ctx->y[i],ctx->x[ctx->m-1],ctx->y[i],PETSC_DRAW_BLACK);
111: }
112: }
113: return(0);
114: }
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: + draw - the draw context
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 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
136: .seealso: PetscDrawTensorContourPatch(), PetscDrawScalePopup()
138: @*/
139: PetscErrorCode PetscDrawTensorContour(PetscDraw draw,int m,int n,const PetscReal xi[],const PetscReal yi[],PetscReal *v)
140: {
142: int N = m*n;
143: PetscBool isnull;
144: PetscDraw popup;
145: int xin=1,yin=1,i;
146: PetscMPIInt size;
147: PetscReal h;
148: ZoomCtx ctx;
152: PetscDrawIsNull(draw,&isnull);
153: if (isnull) return(0);
154: MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);
155: if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"May only be used with single processor PetscDraw");
156: if (N <= 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"n %d and m %d must be positive",m,n);
158: ctx.v = v;
159: ctx.m = m;
160: ctx.n = n;
161: ctx.max = ctx.min = v[0];
162: for (i=0; i<N; i++) {
163: if (ctx.max < ctx.v[i]) ctx.max = ctx.v[i];
164: if (ctx.min > ctx.v[i]) ctx.min = 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: PetscDrawGetPopup(draw,&popup);
170: PetscDrawScalePopup(popup,ctx.min,ctx.max);
172: ctx.showgrid = PETSC_FALSE;
173: PetscOptionsGetBool(((PetscObject)draw)->options,NULL,"-draw_contour_grid",&ctx.showgrid,NULL);
175: /* fill up x and y coordinates */
176: if (!xi) {
177: xin = 0;
178: PetscMalloc1(ctx.m,&ctx.x);
179: h = 1.0/(ctx.m-1);
180: ctx.x[0] = 0.0;
181: for (i=1; i<ctx.m; i++) ctx.x[i] = ctx.x[i-1] + h;
182: } else ctx.x = (PetscReal*)xi;
184: if (!yi) {
185: yin = 0;
186: PetscMalloc1(ctx.n,&ctx.y);
187: h = 1.0/(ctx.n-1);
188: ctx.y[0] = 0.0;
189: for (i=1; i<ctx.n; i++) ctx.y[i] = ctx.y[i-1] + h;
190: } else ctx.y = (PetscReal*)yi;
192: PetscDrawZoom(draw,PetscDrawTensorContour_Zoom,&ctx);
194: if (!xin) {PetscFree(ctx.x);}
195: if (!yin) {PetscFree(ctx.y);}
196: return(0);
197: }
199: /*@
200: PetscDrawTensorContourPatch - PetscDraws a rectangular patch of a contour plot
201: for a two-dimensional array.
203: Not Collective
205: Input Parameters:
206: + draw - the draw context
207: . m,n - the number of local mesh points in the x and y direction
208: . x,y - the locations of the local mesh points
209: . min,max - the minimum and maximum value in the entire contour
210: - v - the data
212: Options Database Keys:
213: . -draw_x_shared_colormap - Activates private colormap
215: Level: advanced
217: Note:
218: This is a lower level support routine, usually the user will call
219: PetscDrawTensorContour().
221: .seealso: PetscDrawTensorContour()
223: @*/
224: PetscErrorCode PetscDrawTensorContourPatch(PetscDraw draw,int m,int n,PetscReal *x,PetscReal *y,PetscReal min,PetscReal max,PetscReal *v)
225: {
227: int c1,c2,c3,c4,i,j;
228: PetscReal x1,x2,x3,x4,y_1,y2,y3,y4;
232: /* PetscDraw the contour plot patch */
233: for (j=0; j<n-1; j++) {
234: for (i=0; i<m-1; i++) {
235: x1 = x[i]; y_1 = y[j]; c1 = PetscDrawRealToColor(v[i+j*m],min,max);
236: x2 = x[i+1]; y2 = y_1; c2 = PetscDrawRealToColor(v[i+j*m+1],min,max);
237: x3 = x2; y3 = y[j+1]; c3 = PetscDrawRealToColor(v[i+j*m+1+m],min,max);
238: x4 = x1; y4 = y3; c4 = PetscDrawRealToColor(v[i+j*m+m],min,max);
240: PetscDrawTriangle(draw,x1,y_1,x2,y2,x3,y3,c1,c2,c3);
241: PetscDrawTriangle(draw,x1,y_1,x3,y3,x4,y4,c1,c3,c4);
242: }
243: }
244: return(0);
245: }