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