xref: /petsc/src/sys/classes/draw/interface/dtri.c (revision 811af0c4b09a35de4306c442f88bd09fdc09897d)
15c6c1daeSBarry Smith 
25c6c1daeSBarry Smith /*
35c6c1daeSBarry Smith        Provides the calling sequences for all the basic PetscDraw routines.
45c6c1daeSBarry Smith */
5af0996ceSBarry Smith #include <petsc/private/drawimpl.h> /*I "petscdraw.h" I*/
65c6c1daeSBarry Smith 
75c6c1daeSBarry Smith /*@
8*811af0c4SBarry Smith    PetscDrawTriangle - draws a triangle  onto a drawable.
95c6c1daeSBarry Smith 
105c6c1daeSBarry Smith    Not Collective
115c6c1daeSBarry Smith 
125c6c1daeSBarry Smith    Input Parameters:
135c6c1daeSBarry Smith +  draw - the drawing context
145c6c1daeSBarry Smith .  x1,y1,x2,y2,x3,y3 - the coordinates of the vertices
155c6c1daeSBarry Smith -  c1,c2,c3 - the colors of the three vertices in the same order as the xi,yi
165c6c1daeSBarry Smith 
175c6c1daeSBarry Smith    Level: beginner
185c6c1daeSBarry Smith 
19*811af0c4SBarry Smith .seealso: `PetscDraw`, `PetscDrawLine()`, `PetscDrawRectangle()`, `PetscDrawEllipse()`, `PetscDrawMarker()`, `PetscDrawPoint()`, `PetscDrawArrow()`
205c6c1daeSBarry Smith @*/
219371c9d4SSatish Balay PetscErrorCode PetscDrawTriangle(PetscDraw draw, PetscReal x1, PetscReal y_1, PetscReal x2, PetscReal y2, PetscReal x3, PetscReal y3, int c1, int c2, int c3) {
225c6c1daeSBarry Smith   PetscFunctionBegin;
235c6c1daeSBarry Smith   PetscValidHeaderSpecific(draw, PETSC_DRAW_CLASSID, 1);
24dbbe0bcdSBarry Smith   PetscUseTypeMethod(draw, triangle, x1, y_1, x2, y2, x3, y3, c1, c2, c3);
255c6c1daeSBarry Smith   PetscFunctionReturn(0);
265c6c1daeSBarry Smith }
275c6c1daeSBarry Smith 
285c6c1daeSBarry Smith /*@
29ba1e01c4SBarry Smith    PetscDrawScalePopup - draws a contour scale window.
305c6c1daeSBarry Smith 
31*811af0c4SBarry Smith    Collective on popop
325c6c1daeSBarry Smith 
335c6c1daeSBarry Smith    Input Parameters:
34*811af0c4SBarry Smith +  popup - the window (often a window obtained via `PetscDrawGetPopup()`
355c6c1daeSBarry Smith .  min - minimum value being plotted
365c6c1daeSBarry Smith -  max - maximum value being plotted
375c6c1daeSBarry Smith 
385c6c1daeSBarry Smith    Level: intermediate
395c6c1daeSBarry Smith 
40*811af0c4SBarry Smith    Note:
4195452b02SPatrick Sanan     All processors that share the draw MUST call this routine
425c6c1daeSBarry Smith 
43*811af0c4SBarry Smith .seealso: `PetscDraw`, `PetscDrawGetPopup()`, `PetscDrawTensorContour()`
445c6c1daeSBarry Smith @*/
459371c9d4SSatish Balay PetscErrorCode PetscDrawScalePopup(PetscDraw popup, PetscReal min, PetscReal max) {
465b399a63SLisandro Dalcin   PetscBool   isnull;
4748db01dbSLisandro Dalcin   PetscReal   xl = 0.0, yl = 0.0, xr = 1.0, yr = 1.0;
48e118a51fSLisandro Dalcin   PetscMPIInt rank;
49b05fc000SLisandro Dalcin   int         i;
505c6c1daeSBarry Smith   char        string[32];
515c6c1daeSBarry Smith 
525c6c1daeSBarry Smith   PetscFunctionBegin;
5345f3bb6eSLisandro Dalcin   if (!popup) PetscFunctionReturn(0);
545b399a63SLisandro Dalcin   PetscValidHeaderSpecific(popup, PETSC_DRAW_CLASSID, 1);
559566063dSJacob Faibussowitsch   PetscCall(PetscDrawIsNull(popup, &isnull));
565b399a63SLisandro Dalcin   if (isnull) PetscFunctionReturn(0);
579566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)popup), &rank));
585b399a63SLisandro Dalcin 
599566063dSJacob Faibussowitsch   PetscCall(PetscDrawCheckResizedWindow(popup));
609566063dSJacob Faibussowitsch   PetscCall(PetscDrawClear(popup));
619566063dSJacob Faibussowitsch   PetscCall(PetscDrawSetTitle(popup, "Contour Scale"));
629566063dSJacob Faibussowitsch   PetscCall(PetscDrawSetCoordinates(popup, xl, yl, xr, yr));
63d0609cedSBarry Smith   PetscDrawCollectiveBegin(popup);
64dd400576SPatrick Sanan   if (rank == 0) {
655c6c1daeSBarry Smith     for (i = 0; i < 10; i++) {
66b05fc000SLisandro Dalcin       int c = PetscDrawRealToColor((PetscReal)i / 9, 0, 1);
679566063dSJacob Faibussowitsch       PetscCall(PetscDrawRectangle(popup, xl, yl, xr, yr, c, c, c, c));
6848db01dbSLisandro Dalcin       yl += 0.1;
695c6c1daeSBarry Smith     }
705c6c1daeSBarry Smith     for (i = 0; i < 10; i++) {
7148db01dbSLisandro Dalcin       PetscReal value = min + i * (max - min) / 9;
725c6c1daeSBarry Smith       /* look for a value that should be zero, but is not due to round-off */
735c6c1daeSBarry Smith       if (PetscAbsReal(value) < 1.e-10 && max - min > 1.e-6) value = 0.0;
749566063dSJacob Faibussowitsch       PetscCall(PetscSNPrintf(string, sizeof(string), "%18.16e", (double)value));
759566063dSJacob Faibussowitsch       PetscCall(PetscDrawString(popup, 0.2, 0.02 + i / 10.0, PETSC_DRAW_BLACK, string));
765c6c1daeSBarry Smith     }
77e118a51fSLisandro Dalcin   }
78d0609cedSBarry Smith   PetscDrawCollectiveEnd(popup);
799566063dSJacob Faibussowitsch   PetscCall(PetscDrawFlush(popup));
809566063dSJacob Faibussowitsch   PetscCall(PetscDrawSave(popup));
815c6c1daeSBarry Smith   PetscFunctionReturn(0);
825c6c1daeSBarry Smith }
835c6c1daeSBarry Smith 
845c6c1daeSBarry Smith typedef struct {
855c6c1daeSBarry Smith   int        m, n;
865c6c1daeSBarry Smith   PetscReal *x, *y, min, max, *v;
875c6c1daeSBarry Smith   PetscBool  showgrid;
885c6c1daeSBarry Smith } ZoomCtx;
895c6c1daeSBarry Smith 
909371c9d4SSatish Balay static PetscErrorCode PetscDrawTensorContour_Zoom(PetscDraw win, void *dctx) {
915c6c1daeSBarry Smith   int      i;
925c6c1daeSBarry Smith   ZoomCtx *ctx = (ZoomCtx *)dctx;
935c6c1daeSBarry Smith 
945c6c1daeSBarry Smith   PetscFunctionBegin;
959566063dSJacob Faibussowitsch   PetscCall(PetscDrawTensorContourPatch(win, ctx->m, ctx->n, ctx->x, ctx->y, ctx->min, ctx->max, ctx->v));
965c6c1daeSBarry Smith   if (ctx->showgrid) {
9748a46eb9SPierre Jolivet     for (i = 0; i < ctx->m; i++) PetscCall(PetscDrawLine(win, ctx->x[i], ctx->y[0], ctx->x[i], ctx->y[ctx->n - 1], PETSC_DRAW_BLACK));
9848a46eb9SPierre Jolivet     for (i = 0; i < ctx->n; i++) PetscCall(PetscDrawLine(win, ctx->x[0], ctx->y[i], ctx->x[ctx->m - 1], ctx->y[i], PETSC_DRAW_BLACK));
995c6c1daeSBarry Smith   }
1005c6c1daeSBarry Smith   PetscFunctionReturn(0);
1015c6c1daeSBarry Smith }
1025c6c1daeSBarry Smith 
1035c6c1daeSBarry Smith /*@C
104*811af0c4SBarry Smith    PetscDrawTensorContour - draws a contour plot for a two-dimensional array
1055c6c1daeSBarry Smith 
106*811af0c4SBarry Smith    Collective on draw, but draw must be sequential
1075c6c1daeSBarry Smith 
1085c6c1daeSBarry Smith    Input Parameters:
1098f69470aSLisandro Dalcin +   draw  - the draw context
1105c6c1daeSBarry Smith .   m,n   - the global number of mesh points in the x and y directions
1110298fd71SBarry Smith .   xi,yi - the locations of the global mesh points (optional, use NULL
1125c6c1daeSBarry Smith             to indicate uniform spacing on [0,1])
1135c6c1daeSBarry Smith -   V     - the values
1145c6c1daeSBarry Smith 
1155c6c1daeSBarry Smith    Options Database Keys:
1165c6c1daeSBarry Smith +  -draw_x_shared_colormap - Indicates use of private colormap
117*811af0c4SBarry Smith -  -draw_contour_grid - draws grid contour
1185c6c1daeSBarry Smith 
1195c6c1daeSBarry Smith    Level: intermediate
1205c6c1daeSBarry Smith 
121*811af0c4SBarry Smith .seealso: `PetscDraw`, `PetscDrawTensorContourPatch()`, `PetscDrawScalePopup()`
1225c6c1daeSBarry Smith @*/
1239371c9d4SSatish Balay PetscErrorCode PetscDrawTensorContour(PetscDraw draw, int m, int n, const PetscReal xi[], const PetscReal yi[], PetscReal *v) {
1245c6c1daeSBarry Smith   int         N = m * n;
1255c6c1daeSBarry Smith   PetscBool   isnull;
1265c6c1daeSBarry Smith   PetscDraw   popup;
1275c6c1daeSBarry Smith   int         xin = 1, yin = 1, i;
1285c6c1daeSBarry Smith   PetscMPIInt size;
1295c6c1daeSBarry Smith   PetscReal   h;
1305c6c1daeSBarry Smith   ZoomCtx     ctx;
1315c6c1daeSBarry Smith 
1325c6c1daeSBarry Smith   PetscFunctionBegin;
1338f69470aSLisandro Dalcin   PetscValidHeaderSpecific(draw, PETSC_DRAW_CLASSID, 1);
1349566063dSJacob Faibussowitsch   PetscCall(PetscDrawIsNull(draw, &isnull));
135e118a51fSLisandro Dalcin   if (isnull) PetscFunctionReturn(0);
1369566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)draw), &size));
13708401ef6SPierre Jolivet   PetscCheck(size <= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "May only be used with single processor PetscDraw");
13808401ef6SPierre Jolivet   PetscCheck(N > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "n %d and m %d must be positive", m, n);
1395c6c1daeSBarry Smith 
1405c6c1daeSBarry Smith   ctx.v   = v;
1415c6c1daeSBarry Smith   ctx.m   = m;
1425c6c1daeSBarry Smith   ctx.n   = n;
1435c6c1daeSBarry Smith   ctx.max = ctx.min = v[0];
1445c6c1daeSBarry Smith   for (i = 0; i < N; i++) {
1455c6c1daeSBarry Smith     if (ctx.max < ctx.v[i]) ctx.max = ctx.v[i];
1465c6c1daeSBarry Smith     if (ctx.min > ctx.v[i]) ctx.min = ctx.v[i];
1475c6c1daeSBarry Smith   }
1489371c9d4SSatish Balay   if (ctx.max - ctx.min < 1.e-7) {
1499371c9d4SSatish Balay     ctx.min -= 5.e-8;
1509371c9d4SSatish Balay     ctx.max += 5.e-8;
1519371c9d4SSatish Balay   }
1525c6c1daeSBarry Smith 
1535c6c1daeSBarry Smith   /* PetscDraw the scale window */
1549566063dSJacob Faibussowitsch   PetscCall(PetscDrawGetPopup(draw, &popup));
1559566063dSJacob Faibussowitsch   PetscCall(PetscDrawScalePopup(popup, ctx.min, ctx.max));
1565c6c1daeSBarry Smith 
1575c6c1daeSBarry Smith   ctx.showgrid = PETSC_FALSE;
1589566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)draw)->options, NULL, "-draw_contour_grid", &ctx.showgrid, NULL));
1595c6c1daeSBarry Smith 
1605c6c1daeSBarry Smith   /* fill up x and y coordinates */
1615c6c1daeSBarry Smith   if (!xi) {
1625c6c1daeSBarry Smith     xin = 0;
1639566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(ctx.m, &ctx.x));
1645c6c1daeSBarry Smith     h        = 1.0 / (ctx.m - 1);
1655c6c1daeSBarry Smith     ctx.x[0] = 0.0;
1665c6c1daeSBarry Smith     for (i = 1; i < ctx.m; i++) ctx.x[i] = ctx.x[i - 1] + h;
167a297a907SKarl Rupp   } else ctx.x = (PetscReal *)xi;
168a297a907SKarl Rupp 
1695c6c1daeSBarry Smith   if (!yi) {
1705c6c1daeSBarry Smith     yin = 0;
1719566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(ctx.n, &ctx.y));
1725c6c1daeSBarry Smith     h        = 1.0 / (ctx.n - 1);
1735c6c1daeSBarry Smith     ctx.y[0] = 0.0;
1745c6c1daeSBarry Smith     for (i = 1; i < ctx.n; i++) ctx.y[i] = ctx.y[i - 1] + h;
175a297a907SKarl Rupp   } else ctx.y = (PetscReal *)yi;
1765c6c1daeSBarry Smith 
1779566063dSJacob Faibussowitsch   PetscCall(PetscDrawZoom(draw, PetscDrawTensorContour_Zoom, &ctx));
1785c6c1daeSBarry Smith 
1799566063dSJacob Faibussowitsch   if (!xin) PetscCall(PetscFree(ctx.x));
1809566063dSJacob Faibussowitsch   if (!yin) PetscCall(PetscFree(ctx.y));
1815c6c1daeSBarry Smith   PetscFunctionReturn(0);
1825c6c1daeSBarry Smith }
1835c6c1daeSBarry Smith 
1845c6c1daeSBarry Smith /*@
185*811af0c4SBarry Smith    PetscDrawTensorContourPatch - draws a rectangular patch of a contour plot
1865c6c1daeSBarry Smith    for a two-dimensional array.
1875c6c1daeSBarry Smith 
1885c6c1daeSBarry Smith    Not Collective
1895c6c1daeSBarry Smith 
1905c6c1daeSBarry Smith    Input Parameters:
191b05fc000SLisandro Dalcin +  draw - the draw context
1925c6c1daeSBarry Smith .  m,n - the number of local mesh points in the x and y direction
1935c6c1daeSBarry Smith .  x,y - the locations of the local mesh points
194b05fc000SLisandro Dalcin .  min,max - the minimum and maximum value in the entire contour
1955c6c1daeSBarry Smith -  v - the data
1965c6c1daeSBarry Smith 
1975c6c1daeSBarry Smith    Options Database Keys:
1985c6c1daeSBarry Smith .  -draw_x_shared_colormap - Activates private colormap
1995c6c1daeSBarry Smith 
2005c6c1daeSBarry Smith    Level: advanced
2015c6c1daeSBarry Smith 
2025c6c1daeSBarry Smith    Note:
2035c6c1daeSBarry Smith    This is a lower level support routine, usually the user will call
204*811af0c4SBarry Smith    `PetscDrawTensorContour()`.
2055c6c1daeSBarry Smith 
206*811af0c4SBarry Smith .seealso: `PetscDraw`, `PetscDrawTensorContour()`
2075c6c1daeSBarry Smith @*/
2089371c9d4SSatish Balay PetscErrorCode PetscDrawTensorContourPatch(PetscDraw draw, int m, int n, PetscReal *x, PetscReal *y, PetscReal min, PetscReal max, PetscReal *v) {
2095c6c1daeSBarry Smith   int       c1, c2, c3, c4, i, j;
210b05fc000SLisandro Dalcin   PetscReal x1, x2, x3, x4, y_1, y2, y3, y4;
2115c6c1daeSBarry Smith 
2125c6c1daeSBarry Smith   PetscFunctionBegin;
2138f69470aSLisandro Dalcin   PetscValidHeaderSpecific(draw, PETSC_DRAW_CLASSID, 1);
2145c6c1daeSBarry Smith   /* PetscDraw the contour plot patch */
2155c6c1daeSBarry Smith   for (j = 0; j < n - 1; j++) {
2165c6c1daeSBarry Smith     for (i = 0; i < m - 1; i++) {
2179371c9d4SSatish Balay       x1  = x[i];
2189371c9d4SSatish Balay       y_1 = y[j];
2199371c9d4SSatish Balay       c1  = PetscDrawRealToColor(v[i + j * m], min, max);
2209371c9d4SSatish Balay       x2  = x[i + 1];
2219371c9d4SSatish Balay       y2  = y_1;
2229371c9d4SSatish Balay       c2  = PetscDrawRealToColor(v[i + j * m + 1], min, max);
2239371c9d4SSatish Balay       x3  = x2;
2249371c9d4SSatish Balay       y3  = y[j + 1];
2259371c9d4SSatish Balay       c3  = PetscDrawRealToColor(v[i + j * m + 1 + m], min, max);
2269371c9d4SSatish Balay       x4  = x1;
2279371c9d4SSatish Balay       y4  = y3;
2289371c9d4SSatish Balay       c4  = PetscDrawRealToColor(v[i + j * m + m], min, max);
229a297a907SKarl Rupp 
2309566063dSJacob Faibussowitsch       PetscCall(PetscDrawTriangle(draw, x1, y_1, x2, y2, x3, y3, c1, c2, c3));
2319566063dSJacob Faibussowitsch       PetscCall(PetscDrawTriangle(draw, x1, y_1, x3, y3, x4, y4, c1, c3, c4));
2325c6c1daeSBarry Smith     }
2335c6c1daeSBarry Smith   }
2345c6c1daeSBarry Smith   PetscFunctionReturn(0);
2355c6c1daeSBarry Smith }
236