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