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