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