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