xref: /petsc/src/sys/classes/draw/interface/dtri.c (revision c3e4dd79e17d3f0a51a524340fae4661630fd09e)
1 
2 /*
3        Provides the calling sequences for all the basic PetscDraw routines.
4 */
5 #include <petsc/private/drawimpl.h>  /*I "petscdraw.h" I*/
6 
7 /*@
8    PetscDrawTriangle - PetscDraws a triangle  onto a drawable.
9 
10    Not Collective
11 
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
16 
17    Level: beginner
18 
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 {
23   PetscFunctionBegin;
24   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
25   PetscCheck(draw->ops->triangle,PETSC_COMM_SELF,PETSC_ERR_SUP,"This draw type %s does not support drawing triangles",((PetscObject)draw)->type_name);
26   PetscCall((*draw->ops->triangle)(draw,x1,y_1,x2,y2,x3,y3,c1,c2,c3));
27   PetscFunctionReturn(0);
28 }
29 
30 /*@
31    PetscDrawScalePopup - draws a contour scale window.
32 
33    Collective on PetscDraw
34 
35    Input Parameters:
36 +  popup - the window (often a window obtained via PetscDrawGetPopup()
37 .  min - minimum value being plotted
38 -  max - maximum value being plotted
39 
40    Level: intermediate
41 
42    Notes:
43     All processors that share the draw MUST call this routine
44 
45 .seealso: `PetscDrawGetPopup()`, `PetscDrawTensorContour()`
46 
47 @*/
48 PetscErrorCode  PetscDrawScalePopup(PetscDraw popup,PetscReal min,PetscReal max)
49 {
50   PetscBool      isnull;
51   PetscReal      xl = 0.0,yl = 0.0,xr = 1.0,yr = 1.0;
52   PetscMPIInt    rank;
53   int            i;
54   char           string[32];
55 
56   PetscFunctionBegin;
57   if (!popup) PetscFunctionReturn(0);
58   PetscValidHeaderSpecific(popup,PETSC_DRAW_CLASSID,1);
59   PetscCall(PetscDrawIsNull(popup,&isnull));
60   if (isnull) PetscFunctionReturn(0);
61   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)popup),&rank));
62 
63   PetscCall(PetscDrawCheckResizedWindow(popup));
64   PetscCall(PetscDrawClear(popup));
65   PetscCall(PetscDrawSetTitle(popup,"Contour Scale"));
66   PetscCall(PetscDrawSetCoordinates(popup,xl,yl,xr,yr));
67   PetscDrawCollectiveBegin(popup);
68   if (rank == 0) {
69     for (i=0; i<10; i++) {
70       int c = PetscDrawRealToColor((PetscReal)i/9,0,1);
71       PetscCall(PetscDrawRectangle(popup,xl,yl,xr,yr,c,c,c,c));
72       yl += 0.1;
73     }
74     for (i=0; i<10; i++) {
75       PetscReal value = min + i*(max-min)/9;
76       /* look for a value that should be zero, but is not due to round-off */
77       if (PetscAbsReal(value) < 1.e-10 && max-min > 1.e-6) value = 0.0;
78       PetscCall(PetscSNPrintf(string,sizeof(string),"%18.16e",(double)value));
79       PetscCall(PetscDrawString(popup,0.2,0.02+i/10.0,PETSC_DRAW_BLACK,string));
80     }
81   }
82   PetscDrawCollectiveEnd(popup);
83   PetscCall(PetscDrawFlush(popup));
84   PetscCall(PetscDrawSave(popup));
85   PetscFunctionReturn(0);
86 }
87 
88 typedef struct {
89   int       m,n;
90   PetscReal *x,*y,min,max,*v;
91   PetscBool showgrid;
92 } ZoomCtx;
93 
94 static PetscErrorCode PetscDrawTensorContour_Zoom(PetscDraw win,void *dctx)
95 {
96   int            i;
97   ZoomCtx        *ctx = (ZoomCtx*)dctx;
98 
99   PetscFunctionBegin;
100   PetscCall(PetscDrawTensorContourPatch(win,ctx->m,ctx->n,ctx->x,ctx->y,ctx->min,ctx->max,ctx->v));
101   if (ctx->showgrid) {
102     for (i=0; i<ctx->m; i++) {
103       PetscCall(PetscDrawLine(win,ctx->x[i],ctx->y[0],ctx->x[i],ctx->y[ctx->n-1],PETSC_DRAW_BLACK));
104     }
105     for (i=0; i<ctx->n; i++) {
106       PetscCall(PetscDrawLine(win,ctx->x[0],ctx->y[i],ctx->x[ctx->m-1],ctx->y[i],PETSC_DRAW_BLACK));
107     }
108   }
109   PetscFunctionReturn(0);
110 }
111 
112 /*@C
113    PetscDrawTensorContour - PetscDraws a contour plot for a two-dimensional array
114    that is stored as a PETSc vector.
115 
116    Collective on PetscDraw, but PetscDraw must be sequential
117 
118    Input Parameters:
119 +   draw  - the draw context
120 .   m,n   - the global number of mesh points in the x and y directions
121 .   xi,yi - the locations of the global mesh points (optional, use NULL
122             to indicate uniform spacing on [0,1])
123 -   V     - the values
124 
125    Options Database Keys:
126 +  -draw_x_shared_colormap - Indicates use of private colormap
127 -  -draw_contour_grid - PetscDraws grid contour
128 
129    Level: intermediate
130 
131 .seealso: `PetscDrawTensorContourPatch()`, `PetscDrawScalePopup()`
132 
133 @*/
134 PetscErrorCode  PetscDrawTensorContour(PetscDraw draw,int m,int n,const PetscReal xi[],const PetscReal yi[],PetscReal *v)
135 {
136   int            N = m*n;
137   PetscBool      isnull;
138   PetscDraw      popup;
139   int            xin=1,yin=1,i;
140   PetscMPIInt    size;
141   PetscReal      h;
142   ZoomCtx        ctx;
143 
144   PetscFunctionBegin;
145   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
146   PetscCall(PetscDrawIsNull(draw,&isnull));
147   if (isnull) PetscFunctionReturn(0);
148   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size));
149   PetscCheck(size <= 1,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"May only be used with single processor PetscDraw");
150   PetscCheck(N > 0,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"n %d and m %d must be positive",m,n);
151 
152   ctx.v   = v;
153   ctx.m   = m;
154   ctx.n   = n;
155   ctx.max = ctx.min = v[0];
156   for (i=0; i<N; i++) {
157     if (ctx.max < ctx.v[i]) ctx.max = ctx.v[i];
158     if (ctx.min > ctx.v[i]) ctx.min = ctx.v[i];
159   }
160   if (ctx.max - ctx.min < 1.e-7) {ctx.min -= 5.e-8; ctx.max += 5.e-8;}
161 
162   /* PetscDraw the scale window */
163   PetscCall(PetscDrawGetPopup(draw,&popup));
164   PetscCall(PetscDrawScalePopup(popup,ctx.min,ctx.max));
165 
166   ctx.showgrid = PETSC_FALSE;
167   PetscCall(PetscOptionsGetBool(((PetscObject)draw)->options,NULL,"-draw_contour_grid",&ctx.showgrid,NULL));
168 
169   /* fill up x and y coordinates */
170   if (!xi) {
171     xin      = 0;
172     PetscCall(PetscMalloc1(ctx.m,&ctx.x));
173     h        = 1.0/(ctx.m-1);
174     ctx.x[0] = 0.0;
175     for (i=1; i<ctx.m; i++) ctx.x[i] = ctx.x[i-1] + h;
176   } else ctx.x = (PetscReal*)xi;
177 
178   if (!yi) {
179     yin      = 0;
180     PetscCall(PetscMalloc1(ctx.n,&ctx.y));
181     h        = 1.0/(ctx.n-1);
182     ctx.y[0] = 0.0;
183     for (i=1; i<ctx.n; i++) ctx.y[i] = ctx.y[i-1] + h;
184   } else ctx.y = (PetscReal*)yi;
185 
186   PetscCall(PetscDrawZoom(draw,PetscDrawTensorContour_Zoom,&ctx));
187 
188   if (!xin) PetscCall(PetscFree(ctx.x));
189   if (!yin) PetscCall(PetscFree(ctx.y));
190   PetscFunctionReturn(0);
191 }
192 
193 /*@
194    PetscDrawTensorContourPatch - PetscDraws a rectangular patch of a contour plot
195    for a two-dimensional array.
196 
197    Not Collective
198 
199    Input Parameters:
200 +  draw - the draw context
201 .  m,n - the number of local mesh points in the x and y direction
202 .  x,y - the locations of the local mesh points
203 .  min,max - the minimum and maximum value in the entire contour
204 -  v - the data
205 
206    Options Database Keys:
207 .  -draw_x_shared_colormap - Activates private colormap
208 
209    Level: advanced
210 
211    Note:
212    This is a lower level support routine, usually the user will call
213    PetscDrawTensorContour().
214 
215 .seealso: `PetscDrawTensorContour()`
216 
217 @*/
218 PetscErrorCode  PetscDrawTensorContourPatch(PetscDraw draw,int m,int n,PetscReal *x,PetscReal *y,PetscReal min,PetscReal max,PetscReal *v)
219 {
220   int            c1,c2,c3,c4,i,j;
221   PetscReal      x1,x2,x3,x4,y_1,y2,y3,y4;
222 
223   PetscFunctionBegin;
224   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
225   /* PetscDraw the contour plot patch */
226   for (j=0; j<n-1; j++) {
227     for (i=0; i<m-1; i++) {
228       x1 = x[i];   y_1 = y[j];  c1 = PetscDrawRealToColor(v[i+j*m],min,max);
229       x2 = x[i+1]; y2 = y_1;    c2 = PetscDrawRealToColor(v[i+j*m+1],min,max);
230       x3 = x2;     y3 = y[j+1]; c3 = PetscDrawRealToColor(v[i+j*m+1+m],min,max);
231       x4 = x1;     y4 = y3;     c4 = PetscDrawRealToColor(v[i+j*m+m],min,max);
232 
233       PetscCall(PetscDrawTriangle(draw,x1,y_1,x2,y2,x3,y3,c1,c2,c3));
234       PetscCall(PetscDrawTriangle(draw,x1,y_1,x3,y3,x4,y4,c1,c3,c4));
235     }
236   }
237   PetscFunctionReturn(0);
238 }
239