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