xref: /petsc/src/sys/classes/draw/interface/dtri.c (revision af0996ce37bc06907c37d8d91773840993d61e62)
15c6c1daeSBarry Smith 
25c6c1daeSBarry Smith /*
35c6c1daeSBarry Smith        Provides the calling sequences for all the basic PetscDraw routines.
45c6c1daeSBarry Smith */
5*af0996ceSBarry Smith #include <petsc/private/drawimpl.h>  /*I "petscdraw.h" I*/
65c6c1daeSBarry Smith 
75c6c1daeSBarry Smith #undef __FUNCT__
85c6c1daeSBarry Smith #define __FUNCT__ "PetscDrawTriangle"
95c6c1daeSBarry Smith /*@
105c6c1daeSBarry Smith    PetscDrawTriangle - PetscDraws a triangle  onto a drawable.
115c6c1daeSBarry Smith 
125c6c1daeSBarry Smith    Not Collective
135c6c1daeSBarry Smith 
145c6c1daeSBarry Smith    Input Parameters:
155c6c1daeSBarry Smith +  draw - the drawing context
165c6c1daeSBarry Smith .  x1,y1,x2,y2,x3,y3 - the coordinates of the vertices
175c6c1daeSBarry Smith -  c1,c2,c3 - the colors of the three vertices in the same order as the xi,yi
185c6c1daeSBarry Smith 
195c6c1daeSBarry Smith    Level: beginner
205c6c1daeSBarry Smith 
215c6c1daeSBarry Smith    Concepts: drawing^triangle
225c6c1daeSBarry Smith    Concepts: graphics^triangle
235c6c1daeSBarry Smith    Concepts: triangle
245c6c1daeSBarry Smith 
255c6c1daeSBarry Smith @*/
26a297a907SKarl Rupp PetscErrorCode  PetscDrawTriangle(PetscDraw draw,PetscReal x1,PetscReal y_1,PetscReal x2,PetscReal y2,PetscReal x3,PetscReal y3,int c1,int c2,int c3)
275c6c1daeSBarry Smith {
285c6c1daeSBarry Smith   PetscErrorCode ierr;
295c6c1daeSBarry Smith   PetscBool      isnull;
305c6c1daeSBarry Smith 
315c6c1daeSBarry Smith   PetscFunctionBegin;
325c6c1daeSBarry Smith   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
335c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);CHKERRQ(ierr);
345c6c1daeSBarry Smith   if (isnull) PetscFunctionReturn(0);
35d6ed00deSBarry Smith   if (!draw->ops->triangle) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No triangles with this PetscDrawType");
365c6c1daeSBarry Smith   ierr = (*draw->ops->triangle)(draw,x1,y_1,x2,y2,x3,y3,c1,c2,c3);CHKERRQ(ierr);
375c6c1daeSBarry Smith   PetscFunctionReturn(0);
385c6c1daeSBarry Smith }
395c6c1daeSBarry Smith 
405c6c1daeSBarry Smith 
415c6c1daeSBarry Smith #undef __FUNCT__
425c6c1daeSBarry Smith #define __FUNCT__ "PetscDrawScalePopup"
435c6c1daeSBarry Smith /*@
445c6c1daeSBarry Smith        PetscDrawScalePopup - PetscDraws a contour scale window.
455c6c1daeSBarry Smith 
465c6c1daeSBarry Smith      Collective on PetscDraw
475c6c1daeSBarry Smith 
485c6c1daeSBarry Smith   Input Parameters:
495c6c1daeSBarry Smith +    popup - the window (often a window obtained via PetscDrawGetPopup()
505c6c1daeSBarry Smith .    min - minimum value being plotted
515c6c1daeSBarry Smith -    max - maximum value being plotted
525c6c1daeSBarry Smith 
535c6c1daeSBarry Smith   Level: intermediate
545c6c1daeSBarry Smith 
555c6c1daeSBarry Smith   Notes:
565c6c1daeSBarry Smith      All processors that share the draw MUST call this routine
575c6c1daeSBarry Smith 
585c6c1daeSBarry Smith @*/
595c6c1daeSBarry Smith PetscErrorCode  PetscDrawScalePopup(PetscDraw popup,PetscReal min,PetscReal max)
605c6c1daeSBarry Smith {
615c6c1daeSBarry Smith   PetscReal      xl = 0.0,yl = 0.0,xr = 2.0,yr = 1.0,value;
625c6c1daeSBarry Smith   PetscErrorCode ierr;
635c6c1daeSBarry Smith   int            i,c = PETSC_DRAW_BASIC_COLORS,rank;
645c6c1daeSBarry Smith   char           string[32];
655c6c1daeSBarry Smith   MPI_Comm       comm;
665c6c1daeSBarry Smith 
675c6c1daeSBarry Smith   PetscFunctionBegin;
685c6c1daeSBarry Smith   ierr = PetscDrawCheckResizedWindow(popup);CHKERRQ(ierr);
690b874712SBarry Smith   ierr = PetscDrawSynchronizedClear(popup);CHKERRQ(ierr);
705c6c1daeSBarry Smith   ierr = PetscObjectGetComm((PetscObject)popup,&comm);CHKERRQ(ierr);
715c6c1daeSBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
725c6c1daeSBarry Smith   if (rank) PetscFunctionReturn(0);
735c6c1daeSBarry Smith 
745c6c1daeSBarry Smith   for (i=0; i<10; i++) {
755c6c1daeSBarry Smith     ierr = PetscDrawRectangle(popup,xl,yl,xr,yr,c,c,c,c);CHKERRQ(ierr);
76a297a907SKarl Rupp 
77a297a907SKarl Rupp     yl += .1; yr += .1;
78a297a907SKarl Rupp     c = (int)((double)c + (245. - PETSC_DRAW_BASIC_COLORS)/9.);
795c6c1daeSBarry Smith   }
805c6c1daeSBarry Smith   for (i=0; i<10; i++) {
815c6c1daeSBarry Smith     value = min + i*(max-min)/9.0;
825c6c1daeSBarry Smith     /* look for a value that should be zero, but is not due to round-off */
835c6c1daeSBarry Smith     if (PetscAbsReal(value) < 1.e-10 && max-min > 1.e-6) value = 0.0;
845c6c1daeSBarry Smith     sprintf(string,"%18.16e",(double)value);
855c6c1daeSBarry Smith     ierr = PetscDrawString(popup,.2,.02 + i/10.0,PETSC_DRAW_BLACK,string);CHKERRQ(ierr);
865c6c1daeSBarry Smith   }
875c6c1daeSBarry Smith   ierr = PetscDrawSetTitle(popup,"Contour Scale");CHKERRQ(ierr);
885c6c1daeSBarry Smith   ierr = PetscDrawFlush(popup);CHKERRQ(ierr);
895c6c1daeSBarry Smith   PetscFunctionReturn(0);
905c6c1daeSBarry Smith }
915c6c1daeSBarry Smith 
925c6c1daeSBarry Smith typedef struct {
935c6c1daeSBarry Smith   int       m,n;
945c6c1daeSBarry Smith   PetscReal *x,*y,min,max,*v;
955c6c1daeSBarry Smith   PetscBool showgrid;
965c6c1daeSBarry Smith } ZoomCtx;
975c6c1daeSBarry Smith 
985c6c1daeSBarry Smith #undef __FUNCT__
995c6c1daeSBarry Smith #define __FUNCT__ "PetscDrawTensorContour_Zoom"
1005c6c1daeSBarry Smith static PetscErrorCode PetscDrawTensorContour_Zoom(PetscDraw win,void *dctx)
1015c6c1daeSBarry Smith {
1025c6c1daeSBarry Smith   PetscErrorCode ierr;
1035c6c1daeSBarry Smith   int            i;
1045c6c1daeSBarry Smith   ZoomCtx        *ctx = (ZoomCtx*)dctx;
1055c6c1daeSBarry Smith 
1065c6c1daeSBarry Smith   PetscFunctionBegin;
1075c6c1daeSBarry Smith   ierr = PetscDrawTensorContourPatch(win,ctx->m,ctx->n,ctx->x,ctx->y,ctx->max,ctx->min,ctx->v);CHKERRQ(ierr);
1085c6c1daeSBarry Smith   if (ctx->showgrid) {
1095c6c1daeSBarry Smith     for (i=0; i<ctx->m; i++) {
1105c6c1daeSBarry Smith       ierr = PetscDrawLine(win,ctx->x[i],ctx->y[0],ctx->x[i],ctx->y[ctx->n-1],PETSC_DRAW_BLACK);CHKERRQ(ierr);
1115c6c1daeSBarry Smith     }
1125c6c1daeSBarry Smith     for (i=0; i<ctx->n; i++) {
1135c6c1daeSBarry Smith       ierr = PetscDrawLine(win,ctx->x[0],ctx->y[i],ctx->x[ctx->m-1],ctx->y[i],PETSC_DRAW_BLACK);CHKERRQ(ierr);
1145c6c1daeSBarry Smith     }
1155c6c1daeSBarry Smith   }
1165c6c1daeSBarry Smith   PetscFunctionReturn(0);
1175c6c1daeSBarry Smith }
1185c6c1daeSBarry Smith 
1195c6c1daeSBarry Smith #undef __FUNCT__
1205c6c1daeSBarry Smith #define __FUNCT__ "PetscDrawTensorContour"
1215c6c1daeSBarry Smith /*@C
1225c6c1daeSBarry Smith    PetscDrawTensorContour - PetscDraws a contour plot for a two-dimensional array
1235c6c1daeSBarry Smith    that is stored as a PETSc vector.
1245c6c1daeSBarry Smith 
1255c6c1daeSBarry Smith    Collective on PetscDraw, but PetscDraw must be sequential
1265c6c1daeSBarry Smith 
1275c6c1daeSBarry Smith    Input Parameters:
1285c6c1daeSBarry Smith +   win   - the window to draw in
1295c6c1daeSBarry Smith .   m,n   - the global number of mesh points in the x and y directions
1300298fd71SBarry Smith .   xi,yi - the locations of the global mesh points (optional, use NULL
1315c6c1daeSBarry Smith             to indicate uniform spacing on [0,1])
1325c6c1daeSBarry Smith -   V     - the values
1335c6c1daeSBarry Smith 
1345c6c1daeSBarry Smith    Options Database Keys:
1355c6c1daeSBarry Smith +  -draw_x_shared_colormap - Indicates use of private colormap
1365c6c1daeSBarry Smith -  -draw_contour_grid - PetscDraws grid contour
1375c6c1daeSBarry Smith 
1385c6c1daeSBarry Smith    Level: intermediate
1395c6c1daeSBarry Smith 
1405c6c1daeSBarry Smith    Concepts: contour plot
1415c6c1daeSBarry Smith    Concepts: drawing^contour plot
1425c6c1daeSBarry Smith 
1435c6c1daeSBarry Smith .seealso: PetscDrawTensorContourPatch()
1445c6c1daeSBarry Smith 
1455c6c1daeSBarry Smith @*/
1465c6c1daeSBarry Smith PetscErrorCode  PetscDrawTensorContour(PetscDraw win,int m,int n,const PetscReal xi[],const PetscReal yi[],PetscReal *v)
1475c6c1daeSBarry Smith {
1485c6c1daeSBarry Smith   PetscErrorCode ierr;
1495c6c1daeSBarry Smith   int            N = m*n;
1505c6c1daeSBarry Smith   PetscBool      isnull;
1515c6c1daeSBarry Smith   PetscDraw      popup;
1525c6c1daeSBarry Smith   MPI_Comm       comm;
1535c6c1daeSBarry Smith   int            xin=1,yin=1,i;
1545c6c1daeSBarry Smith   PetscMPIInt    size;
1555c6c1daeSBarry Smith   PetscReal      h;
1565c6c1daeSBarry Smith   ZoomCtx        ctx;
1575c6c1daeSBarry Smith 
1585c6c1daeSBarry Smith   PetscFunctionBegin;
1595c6c1daeSBarry Smith   ierr = PetscDrawIsNull(win,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
1605c6c1daeSBarry Smith   ierr = PetscObjectGetComm((PetscObject)win,&comm);CHKERRQ(ierr);
1615c6c1daeSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
1625c6c1daeSBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"May only be used with single processor PetscDraw");
1635c6c1daeSBarry Smith   if (N <= 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"n %d and m %d must be positive",m,n);
1645c6c1daeSBarry Smith 
1655c6c1daeSBarry Smith   /* create scale window */
1665c6c1daeSBarry Smith   ierr = PetscDrawGetPopup(win,&popup);CHKERRQ(ierr);
1675c6c1daeSBarry Smith   ierr = PetscDrawCheckResizedWindow(win);CHKERRQ(ierr);
1685c6c1daeSBarry Smith 
1695c6c1daeSBarry Smith   ctx.v   = v;
1705c6c1daeSBarry Smith   ctx.m   = m;
1715c6c1daeSBarry Smith   ctx.n   = n;
1725c6c1daeSBarry Smith   ctx.max = ctx.min = v[0];
1735c6c1daeSBarry Smith   for (i=0; i<N; i++) {
1745c6c1daeSBarry Smith     if (ctx.max < ctx.v[i]) ctx.max = ctx.v[i];
1755c6c1daeSBarry Smith     if (ctx.min > ctx.v[i]) ctx.min = ctx.v[i];
1765c6c1daeSBarry Smith   }
1775c6c1daeSBarry Smith   if (ctx.max - ctx.min < 1.e-7) {ctx.min -= 5.e-8; ctx.max += 5.e-8;}
1785c6c1daeSBarry Smith 
1795c6c1daeSBarry Smith   /* PetscDraw the scale window */
1805c6c1daeSBarry Smith   if (popup) {ierr = PetscDrawScalePopup(popup,ctx.min,ctx.max);CHKERRQ(ierr);}
1815c6c1daeSBarry Smith 
1825c6c1daeSBarry Smith   ctx.showgrid = PETSC_FALSE;
1830298fd71SBarry Smith   ierr         = PetscOptionsGetBool(NULL,"-draw_contour_grid",&ctx.showgrid,NULL);CHKERRQ(ierr);
1845c6c1daeSBarry Smith 
1855c6c1daeSBarry Smith   /* fill up x and y coordinates */
1865c6c1daeSBarry Smith   if (!xi) {
1875c6c1daeSBarry Smith     xin      = 0;
188785e854fSJed Brown     ierr     = PetscMalloc1(ctx.m,&ctx.x);CHKERRQ(ierr);
1895c6c1daeSBarry Smith     h        = 1.0/(ctx.m-1);
1905c6c1daeSBarry Smith     ctx.x[0] = 0.0;
1915c6c1daeSBarry Smith     for (i=1; i<ctx.m; i++) ctx.x[i] = ctx.x[i-1] + h;
192a297a907SKarl Rupp   } else ctx.x = (PetscReal*)xi;
193a297a907SKarl Rupp 
1945c6c1daeSBarry Smith   if (!yi) {
1955c6c1daeSBarry Smith     yin      = 0;
196785e854fSJed Brown     ierr     = PetscMalloc1(ctx.n,&ctx.y);CHKERRQ(ierr);
1975c6c1daeSBarry Smith     h        = 1.0/(ctx.n-1);
1985c6c1daeSBarry Smith     ctx.y[0] = 0.0;
1995c6c1daeSBarry Smith     for (i=1; i<ctx.n; i++) ctx.y[i] = ctx.y[i-1] + h;
200a297a907SKarl Rupp   } else ctx.y = (PetscReal*)yi;
2015c6c1daeSBarry Smith 
2025c6c1daeSBarry Smith   ierr = PetscDrawZoom(win,(PetscErrorCode (*)(PetscDraw,void*))PetscDrawTensorContour_Zoom,&ctx);CHKERRQ(ierr);
2035c6c1daeSBarry Smith 
2045c6c1daeSBarry Smith   if (!xin) {ierr = PetscFree(ctx.x);CHKERRQ(ierr);}
2055c6c1daeSBarry Smith   if (!yin) {ierr = PetscFree(ctx.y);CHKERRQ(ierr);}
2065c6c1daeSBarry Smith   PetscFunctionReturn(0);
2075c6c1daeSBarry Smith }
2085c6c1daeSBarry Smith 
2095c6c1daeSBarry Smith #undef __FUNCT__
2105c6c1daeSBarry Smith #define __FUNCT__ "PetscDrawTensorContourPatch"
2115c6c1daeSBarry Smith /*@
2125c6c1daeSBarry Smith    PetscDrawTensorContourPatch - PetscDraws a rectangular patch of a contour plot
2135c6c1daeSBarry Smith    for a two-dimensional array.
2145c6c1daeSBarry Smith 
2155c6c1daeSBarry Smith    Not Collective
2165c6c1daeSBarry Smith 
2175c6c1daeSBarry Smith    Input Parameters:
2185c6c1daeSBarry Smith +  win - the window to draw in
2195c6c1daeSBarry Smith .  m,n - the number of local mesh points in the x and y direction
2205c6c1daeSBarry Smith .  x,y - the locations of the local mesh points
2215c6c1daeSBarry Smith .  max,min - the maximum and minimum value in the entire contour
2225c6c1daeSBarry Smith -  v - the data
2235c6c1daeSBarry Smith 
2245c6c1daeSBarry Smith    Options Database Keys:
2255c6c1daeSBarry Smith .  -draw_x_shared_colormap - Activates private colormap
2265c6c1daeSBarry Smith 
2275c6c1daeSBarry Smith    Level: advanced
2285c6c1daeSBarry Smith 
2295c6c1daeSBarry Smith    Note:
2305c6c1daeSBarry Smith    This is a lower level support routine, usually the user will call
2315c6c1daeSBarry Smith    PetscDrawTensorContour().
2325c6c1daeSBarry Smith 
2335c6c1daeSBarry Smith    Concepts: contour plot
2345c6c1daeSBarry Smith 
2355c6c1daeSBarry Smith .seealso: PetscDrawTensorContour()
2365c6c1daeSBarry Smith 
2375c6c1daeSBarry Smith @*/
2385c6c1daeSBarry Smith PetscErrorCode  PetscDrawTensorContourPatch(PetscDraw draw,int m,int n,PetscReal *x,PetscReal *y,PetscReal max,PetscReal min,PetscReal *v)
2395c6c1daeSBarry Smith {
2405c6c1daeSBarry Smith   PetscErrorCode ierr;
2415c6c1daeSBarry Smith   int            c1,c2,c3,c4,i,j;
2425c6c1daeSBarry Smith   PetscReal      x1,x2,x3,x4,y_1,y2,y3,y4,scale;
2435c6c1daeSBarry Smith 
2445c6c1daeSBarry Smith   PetscFunctionBegin;
2455c6c1daeSBarry Smith   scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/(max - min);
2465c6c1daeSBarry Smith 
2475c6c1daeSBarry Smith   /* PetscDraw the contour plot patch */
2485c6c1daeSBarry Smith   for (j=0; j<n-1; j++) {
2495c6c1daeSBarry Smith     for (i=0; i<m-1; i++) {
2505c6c1daeSBarry Smith       x1 = x[i];  y_1 = y[j]; c1 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m] - min));
2515c6c1daeSBarry Smith       x2 = x[i+1];y2 = y_1;   c2 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m+1]-min));
2525c6c1daeSBarry Smith       x3 = x2;    y3 = y[j+1];c3 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m+1+m]-min));
2535c6c1daeSBarry Smith       x4 = x1;    y4 = y3;    c4 = (int)(PETSC_DRAW_BASIC_COLORS + scale*(v[i+j*m+m]-min));
254a297a907SKarl Rupp 
2555c6c1daeSBarry Smith       ierr = PetscDrawTriangle(draw,x1,y_1,x2,y2,x3,y3,c1,c2,c3);CHKERRQ(ierr);
2565c6c1daeSBarry Smith       ierr = PetscDrawTriangle(draw,x1,y_1,x3,y3,x4,y4,c1,c3,c4);CHKERRQ(ierr);
2575c6c1daeSBarry Smith     }
2585c6c1daeSBarry Smith   }
2595c6c1daeSBarry Smith   PetscFunctionReturn(0);
2605c6c1daeSBarry Smith }
261